attachment.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. class attachment {
  3. var $contentid;
  4. var $module;
  5. var $catid;
  6. var $attachments;
  7. var $field;
  8. var $imageexts = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
  9. var $uploadedfiles = array();
  10. var $downloadedfiles = array();
  11. var $error;
  12. var $upload_root;
  13. var $siteid;
  14. var $site = array();
  15. function __construct($module='', $catid = 0,$siteid = 0,$upload_dir = '') {
  16. $this->catid = intval($catid);
  17. $this->siteid = intval($siteid)== 0 ? 1 : intval($siteid);
  18. $this->module = $module ? $module : 'content';
  19. pc_base::load_sys_func('dir');
  20. pc_base::load_sys_class('image','','0');
  21. $this->upload_root = pc_base::load_config('system','upload_path');
  22. $this->upload_func = 'copy';
  23. $this->upload_dir = $upload_dir;
  24. }
  25. /**
  26. * 附件上传方法
  27. * @param $field 上传字段
  28. * @param $alowexts 允许上传类型
  29. * @param $maxsize 最大上传大小
  30. * @param $overwrite 是否覆盖原有文件
  31. * @param $thumb_setting 缩略图设置
  32. * @param $watermark_enable 是否添加水印
  33. */
  34. function upload($field, $alowexts = '', $maxsize = 0, $overwrite = 0,$thumb_setting = array(), $watermark_enable = 1) {
  35. if(!isset($_FILES[$field])) {
  36. $this->error = UPLOAD_ERR_OK;
  37. return false;
  38. }
  39. if(empty($alowexts) || $alowexts == '') {
  40. $site_setting = $this->_get_site_setting($this->siteid);
  41. $alowexts = $site_setting['upload_allowext'];
  42. }
  43. $fn = $_GET['CKEditorFuncNum'] ? $_GET['CKEditorFuncNum'] : '1';
  44. $this->field = $field;
  45. $this->savepath = $this->upload_root.$this->upload_dir.date('Y/md/');
  46. $this->alowexts = $alowexts;
  47. $this->maxsize = $maxsize;
  48. $this->overwrite = $overwrite;
  49. $uploadfiles = array();
  50. $description = isset($GLOBALS[$field.'_description']) ? $GLOBALS[$field.'_description'] : array();
  51. if(is_array($_FILES[$field]['error'])) {
  52. $this->uploads = count($_FILES[$field]['error']);
  53. foreach($_FILES[$field]['error'] as $key => $error) {
  54. if($error === UPLOAD_ERR_NO_FILE) continue;
  55. if($error !== UPLOAD_ERR_OK) {
  56. $this->error = $error;
  57. return false;
  58. }
  59. $uploadfiles[$key] = array('tmp_name' => $_FILES[$field]['tmp_name'][$key], 'name' => $_FILES[$field]['name'][$key], 'type' => $_FILES[$field]['type'][$key], 'size' => $_FILES[$field]['size'][$key], 'error' => $_FILES[$field]['error'][$key], 'description'=>$description[$key],'fn'=>$fn);
  60. }
  61. } else {
  62. $this->uploads = 1;
  63. if(!$description) $description = '';
  64. $uploadfiles[0] = array('tmp_name' => $_FILES[$field]['tmp_name'], 'name' => $_FILES[$field]['name'], 'type' => $_FILES[$field]['type'], 'size' => $_FILES[$field]['size'], 'error' => $_FILES[$field]['error'], 'description'=>$description,'fn'=>$fn);
  65. }
  66. if(!dir_create($this->savepath)) {
  67. $this->error = '8';
  68. return false;
  69. }
  70. if(!is_dir($this->savepath)) {
  71. $this->error = '8';
  72. return false;
  73. }
  74. @chmod($this->savepath, 0777);
  75. if(!is_writeable($this->savepath)) {
  76. $this->error = '9';
  77. return false;
  78. }
  79. if(!$this->is_allow_upload()) {
  80. $this->error = '13';
  81. return false;
  82. }
  83. $aids = array();
  84. foreach($uploadfiles as $k=>$file) {
  85. $fileext = fileext($file['name']);
  86. if($file['error'] != 0) {
  87. $this->error = $file['error'];
  88. return false;
  89. }
  90. if(!preg_match("/^(".$this->alowexts.")$/", $fileext)) {
  91. $this->error = '10';
  92. return false;
  93. }
  94. if($this->maxsize && $file['size'] > $this->maxsize) {
  95. $this->error = '11';
  96. return false;
  97. }
  98. if(!$this->isuploadedfile($file['tmp_name'])) {
  99. $this->error = '12';
  100. return false;
  101. }
  102. $temp_filename = $this->getname($fileext);
  103. $savefile = $this->savepath.$temp_filename;
  104. $savefile = preg_replace("/(php|phtml|php3|php4|jsp|exe|dll|asp|cer|asa|shtml|shtm|aspx|asax|cgi|fcgi|pl)(\.|$)/i", "_\\1\\2", $savefile);
  105. $filepath = preg_replace(new_addslashes("|^".$this->upload_root."|"), "", $savefile);
  106. if(!$this->overwrite && file_exists($savefile)) continue;
  107. $upload_func = $this->upload_func;
  108. if(@$upload_func($file['tmp_name'], $savefile)) {
  109. $this->uploadeds++;
  110. @chmod($savefile, 0644);
  111. @unlink($file['tmp_name']);
  112. $file['name'] = iconv("utf-8",CHARSET,$file['name']);
  113. $file['name'] = safe_replace($file['name']);
  114. $uploadedfile = array('filename'=>$file['name'], 'filepath'=>$filepath, 'filesize'=>$file['size'], 'fileext'=>$fileext, 'fn'=>$file['fn']);
  115. $thumb_enable = is_array($thumb_setting) && ($thumb_setting[0] > 0 || $thumb_setting[1] > 0 ) ? 1 : 0;
  116. $image = new image($thumb_enable,$this->siteid);
  117. if($thumb_enable) {
  118. $image->thumb($savefile,'',$thumb_setting[0],$thumb_setting[1]);
  119. }
  120. if($watermark_enable) {
  121. $image->watermark($savefile, $savefile);
  122. }
  123. $aids[] = $this->add($uploadedfile);
  124. }
  125. }
  126. return $aids;
  127. }
  128. /**
  129. * 附件下载
  130. * Enter description here ...
  131. * @param $field 预留字段
  132. * @param $value 传入下载内容
  133. * @param $watermark 是否加入水印
  134. * @param $ext 下载扩展名
  135. * @param $absurl 绝对路径
  136. * @param $basehref
  137. */
  138. function download($field, $value,$watermark = '0',$ext = 'gif|jpg|jpeg|bmp|png', $absurl = '', $basehref = '')
  139. {
  140. if($ext !== 'gif|jpg|jpeg|bmp|png'){
  141. if(!in_array(strtoupper($ext),array('JPG','GIF','BMP','PNG','JPEG'))) exit('附加扩展名必须为gif、jpg、jpeg、
  142. bmp、png');
  143. }
  144. global $image_d;
  145. $this->att_db = pc_base::load_model('attachment_model');
  146. $upload_url = pc_base::load_config('system','upload_url');
  147. $this->field = $field;
  148. $dir = date('Y/md/');
  149. $uploadpath = $upload_url.$dir;
  150. $uploaddir = $this->upload_root.$dir;
  151. $string = new_stripslashes($value);
  152. if(!preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\.($ext))\\2/i", $string, $matches)) return $value;
  153. $remotefileurls = array();
  154. foreach($matches[3] as $matche)
  155. {
  156. if(strpos($matche, '://') === false) continue;
  157. dir_create($uploaddir);
  158. $remotefileurls[$matche] = $this->fillurl($matche, $absurl, $basehref);
  159. }
  160. unset($matches, $string);
  161. $remotefileurls = array_unique($remotefileurls);
  162. $oldpath = $newpath = array();
  163. foreach($remotefileurls as $k=>$file) {
  164. if(strpos($file, '://') === false || strpos($file, $upload_url) !== false) continue;
  165. $filename = fileext($file);
  166. $file_name = basename($file);
  167. $filename = $this->getname($filename);
  168. $newfile = $uploaddir.$filename;
  169. $upload_func = $this->upload_func;
  170. if($upload_func($file, $newfile)) {
  171. $oldpath[] = $k;
  172. $GLOBALS['downloadfiles'][] = $newpath[] = $uploadpath.$filename;
  173. @chmod($newfile, 0777);
  174. $fileext = fileext($filename);
  175. if($watermark){
  176. watermark($newfile, $newfile,$this->siteid);
  177. }
  178. $filepath = $dir.$filename;
  179. $downloadedfile = array('filename'=>$filename, 'filepath'=>$filepath, 'filesize'=>filesize($newfile), 'fileext'=>$fileext);
  180. $aid = $this->add($downloadedfile);
  181. $this->downloadedfiles[$aid] = $filepath;
  182. }
  183. }
  184. return str_replace($oldpath, $newpath, $value);
  185. }
  186. /**
  187. * 附件删除方法
  188. * @param $where 删除sql语句
  189. */
  190. function delete($where) {
  191. $this->att_db = pc_base::load_model('attachment_model');
  192. $result = $this->att_db->select($where);
  193. foreach($result as $r) {
  194. $image = $this->upload_root.$r['filepath'];
  195. @unlink($image);
  196. $thumbs = glob(dirname($image).'/*'.basename($image));
  197. if($thumbs) foreach($thumbs as $thumb) @unlink($thumb);
  198. }
  199. return $this->att_db->delete($where);
  200. }
  201. /**
  202. * 附件添加如数据库
  203. * @param $uploadedfile 附件信息
  204. */
  205. function add($uploadedfile) {
  206. $this->att_db = pc_base::load_model('attachment_model');
  207. $uploadedfile['module'] = $this->module;
  208. $uploadedfile['catid'] = $this->catid;
  209. $uploadedfile['siteid'] = $this->siteid;
  210. $uploadedfile['userid'] = $this->userid;
  211. $uploadedfile['uploadtime'] = SYS_TIME;
  212. $uploadedfile['uploadip'] = ip();
  213. $uploadedfile['status'] = pc_base::load_config('system','attachment_stat') ? 0 : 1;
  214. $uploadedfile['authcode'] = md5($uploadedfile['filepath']);
  215. $uploadedfile['filename'] = strlen($uploadedfile['filename'])>49 ? $this->getname($uploadedfile['fileext']) : $uploadedfile['filename'];
  216. $uploadedfile['isimage'] = in_array($uploadedfile['fileext'], $this->imageexts) ? 1 : 0;
  217. $aid = $this->att_db->api_add($uploadedfile);
  218. $this->uploadedfiles[] = $uploadedfile;
  219. return $aid;
  220. }
  221. function set_userid($userid) {
  222. $this->userid = $userid;
  223. }
  224. /**
  225. * 获取缩略图地址..
  226. * @param $image 图片路径
  227. */
  228. function get_thumb($image){
  229. return str_replace('.', '_thumb.', $image);
  230. }
  231. /**
  232. * 获取附件名称
  233. * @param $fileext 附件扩展名
  234. */
  235. function getname($fileext){
  236. return date('Ymdhis').rand(100, 999).'.'.$fileext;
  237. }
  238. /**
  239. * 返回附件大小
  240. * @param $filesize 图片大小
  241. */
  242. function size($filesize) {
  243. if($filesize >= 1073741824) {
  244. $filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
  245. } elseif($filesize >= 1048576) {
  246. $filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
  247. } elseif($filesize >= 1024) {
  248. $filesize = round($filesize / 1024 * 100) / 100 . ' KB';
  249. } else {
  250. $filesize = $filesize . ' Bytes';
  251. }
  252. return $filesize;
  253. }
  254. /**
  255. * 判断文件是否是通过 HTTP POST 上传的
  256. *
  257. * @param string $file 文件地址
  258. * @return bool 所给出的文件是通过 HTTP POST 上传的则返回 TRUE
  259. */
  260. function isuploadedfile($file) {
  261. return is_uploaded_file($file) || is_uploaded_file(str_replace('\\\\', '\\', $file));
  262. }
  263. /**
  264. * 补全网址
  265. *
  266. * @param string $surl 源地址
  267. * @param string $absurl 相对地址
  268. * @param string $basehref 网址
  269. * @return string 网址
  270. */
  271. function fillurl($surl, $absurl, $basehref = '') {
  272. if($basehref != '') {
  273. $preurl = strtolower(substr($surl,0,6));
  274. if($preurl=='http://' || $preurl=='ftp://' ||$preurl=='mms://' || $preurl=='rtsp://' || $preurl=='thunde' || $preurl=='emule://'|| $preurl=='ed2k://')
  275. return $surl;
  276. else
  277. return $basehref.'/'.$surl;
  278. }
  279. $i = 0;
  280. $dstr = '';
  281. $pstr = '';
  282. $okurl = '';
  283. $pathStep = 0;
  284. $surl = trim($surl);
  285. if($surl=='') return '';
  286. $urls = @parse_url(SITE_URL);
  287. $HomeUrl = $urls['host'];
  288. $BaseUrlPath = $HomeUrl.$urls['path'];
  289. $BaseUrlPath = preg_replace("/\/([^\/]*)\.(.*)$/",'/',$BaseUrlPath);
  290. $BaseUrlPath = preg_replace("/\/$/",'',$BaseUrlPath);
  291. $pos = strpos($surl,'#');
  292. if($pos>0) $surl = substr($surl,0,$pos);
  293. if($surl[0]=='/') {
  294. $okurl = 'http://'.$HomeUrl.'/'.$surl;
  295. } elseif($surl[0] == '.') {
  296. if(strlen($surl)<=2) return '';
  297. elseif($surl[0]=='/') {
  298. $okurl = 'http://'.$BaseUrlPath.'/'.substr($surl,2,strlen($surl)-2);
  299. } else {
  300. $urls = explode('/',$surl);
  301. foreach($urls as $u) {
  302. if($u=="..") $pathStep++;
  303. else if($i<count($urls)-1) $dstr .= $urls[$i].'/';
  304. else $dstr .= $urls[$i];
  305. $i++;
  306. }
  307. $urls = explode('/', $BaseUrlPath);
  308. if(count($urls) <= $pathStep)
  309. return '';
  310. else {
  311. $pstr = 'http://';
  312. for($i=0;$i<count($urls)-$pathStep;$i++) {
  313. $pstr .= $urls[$i].'/';
  314. }
  315. $okurl = $pstr.$dstr;
  316. }
  317. }
  318. } else {
  319. $preurl = strtolower(substr($surl,0,6));
  320. if(strlen($surl)<7)
  321. $okurl = 'http://'.$BaseUrlPath.'/'.$surl;
  322. elseif($preurl=="http:/"||$preurl=='ftp://' ||$preurl=='mms://' || $preurl=="rtsp://" || $preurl=='thunde' || $preurl=='emule:'|| $preurl=='ed2k:/')
  323. $okurl = $surl;
  324. else
  325. $okurl = 'http://'.$BaseUrlPath.'/'.$surl;
  326. }
  327. $preurl = strtolower(substr($okurl,0,6));
  328. if($preurl=='ftp://' || $preurl=='mms://' || $preurl=='rtsp://' || $preurl=='thunde' || $preurl=='emule:'|| $preurl=='ed2k:/') {
  329. return $okurl;
  330. } else {
  331. $okurl = preg_replace('/^(http:\/\/)/i','',$okurl);
  332. $okurl = preg_replace('/\/{1,}/i','/',$okurl);
  333. return 'http://'.$okurl;
  334. }
  335. }
  336. /**
  337. * 是否允许上传
  338. */
  339. function is_allow_upload() {
  340. if($_groupid == 1) return true;
  341. $starttime = SYS_TIME-86400;
  342. $site_setting = $this->_get_site_setting($this->siteid);
  343. return ($uploads < $site_setting['upload_maxsize']);
  344. }
  345. /**
  346. * 返回错误信息
  347. */
  348. function error() {
  349. $UPLOAD_ERROR = array(
  350. 0 => L('att_upload_succ'),
  351. 1 => L('att_upload_limit_ini'),
  352. 2 => L('att_upload_limit_filesize'),
  353. 3 => L('att_upload_limit_part'),
  354. 4 => L('att_upload_nofile'),
  355. 5 => '',
  356. 6 => L('att_upload_notemp'),
  357. 7 => L('att_upload_temp_w_f'),
  358. 8 => L('att_upload_create_dir_f'),
  359. 9 => L('att_upload_dir_permissions'),
  360. 10 => L('att_upload_limit_ext'),
  361. 11 => L('att_upload_limit_setsize'),
  362. 12 => L('att_upload_not_allow'),
  363. 13 => L('att_upload_limit_time'),
  364. );
  365. return iconv(CHARSET,"utf-8",$UPLOAD_ERROR[$this->error]);
  366. }
  367. /**
  368. * ck编辑器返回
  369. * @param $fn
  370. * @param $fileurl 路径
  371. * @param $message 显示信息
  372. */
  373. function mkhtml($fn,$fileurl,$message) {
  374. $str='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$fn.', \''.$fileurl.'\', \''.$message.'\');</script>';
  375. exit($str);
  376. }
  377. /**
  378. * flash上传调试方法
  379. * @param $id
  380. */
  381. function uploaderror($id = 0) {
  382. file_put_contents(PHPCMS_PATH.'xxx.txt', $id);
  383. }
  384. /**
  385. * 获取站点配置信息
  386. * @param $siteid 站点id
  387. */
  388. private function _get_site_setting($siteid) {
  389. $siteinfo = getcache('sitelist', 'commons');
  390. return string2array($siteinfo[$siteid]['setting']);
  391. }
  392. }
  393. ?>