📄 正在查看:twcms/model/cms_content_attach_model.class.php
大小:3,487 字节 · 修改:2014-04-10 07:30:54 · 行数:126
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7defined('TWCMS_PATH') or exit;
8
9class cms_content_attach extends model {
10 function __construct() {
11 $this->table = ''; // 表名 (可以是 cms_article_attach cms_product_attach cms_photo_attach 等)
12 $this->pri = array('aid'); // 主键
13 $this->maxid = 'aid'; // 自增字段
14 }
15
16 // 上传并记录到数据库
17 public function uploads($config, $uid, $cid = 0, $id = 0) {
18 $up = new upload($config, 'upfile');
19 $info = $up->getFileInfo();
20
21 if($info['state'] == 'SUCCESS') {
22 $data = array(
23 'cid' => $cid,
24 'uid' => $uid,
25 'id' => $id,
26 'filename' => $info['name'],
27 'filetype' => $info['ext'],
28 'filesize' => $info['size'],
29 'filepath' => $info['path'],
30 'dateline' => $_ENV['_time'],
31 'downloads' => 0,
32 'isimage' => $info['isimage'],
33 );
34
35 $info['maxid'] = $this->create($data);
36 if(!$info['maxid']) {
37 $info['state'] = '写入附件表失败';
38 }
39 }
40
41 return $info;
42 }
43
44 // 远程图片下载并记录到数据库 ($conf 用到5个参数 maxSize upDir cid uid id)
45 public function remote_down($uri, &$conf) {
46 // php.ini 中的 allow_url_fopen 关闭时不抓取远程图片
47 if(function_exists('ini_get') && !ini_get('allow_url_fopen')) return FALSE;
48
49 // 获取请求头
50 try{ $heads = get_headers($uri, 1); }catch(Exception $e) { return FALSE; }
51
52 // 死链检测
53 if(!(strstr($heads[0], "200") && stristr($heads[0], "OK"))) return FALSE;
54
55 // Content-Type验证和格式验证
56 if(stristr($heads['Content-Type'], 'image')) {
57 $fileExt = trim(strtolower(strrchr($heads['Content-Type'], '/')), '/');
58 if(in_array($fileExt, array('jpg', 'jpeg', 'gif', 'png'))) {
59 $fileExt = ($fileExt == 'jpeg') ? 'jpg' : $fileExt;
60 }else{
61 return FALSE;
62 }
63 }else{
64 return FALSE;
65 }
66
67 try{
68 // 抓取远程图片
69 $context = stream_context_create(array('http'=>array('follow_location'=>false, 'timeout'=>60))); // 不重定向抓取
70 $img = file_get_contents($uri, false, $context);
71
72 // 图片大小验证
73 $filesize = strlen($img);
74 $maxSize = $conf['maxSize']*1024;
75 if($filesize > $maxSize) return FALSE;
76
77 // 创建图片目录
78 $dir = date('Ym/d/');
79 $updir = $conf['upDir'].$dir;
80 if(!is_dir($updir) && !mkdir($updir, 0755, true)) {
81 return FALSE;
82 }
83
84 // 图片写入自己的服务器
85 $filepath = $dir.date('His').uniqid().random(6, 3).'.'.$fileExt;
86 if(!file_put_contents($conf['upDir'].$filepath, $img)) return FALSE;
87
88 // 记录到数据库
89 $data = array(
90 'cid' => $conf['cid'],
91 'uid' => $conf['uid'],
92 'id' => $conf['id'],
93 'filename' => basename($uri),
94 'filetype' => $fileExt,
95 'filesize' => $filesize,
96 'filepath' => $filepath,
97 'dateline' => $_ENV['_time'],
98 'downloads' => 0,
99 'isimage' => 1,
100 );
101 if(!$this->create($data)) return FALSE;
102
103 return $filepath;
104 }catch(Exception $e) {
105 return FALSE;
106 }
107 }
108
109 // 删除单个附件
110 public function xdelete($aid) {
111 $data = $this->read($aid);
112 $updir = TWCMS_PATH.'upload/'.substr($this->table, 4, -7).'/';
113
114 $file = $updir.$data['filepath'];
115 $thumb = image::thumb_name($file);
116
117 try{
118 is_file($file) && unlink($file);
119 is_file($thumb) && unlink($thumb);
120 return $this->delete($aid);
121 }catch(Exception $e) {
122 return FALSE;
123 }
124 }
125}
126