attachments.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. $session_storage = 'session_'.pc_base::load_config('system','session_storage');
  4. pc_base::load_sys_class($session_storage);
  5. if(param::get_cookie('sys_lang')) {
  6. define('SYS_STYLE',param::get_cookie('sys_lang'));
  7. } else {
  8. define('SYS_STYLE','zh-cn');
  9. }
  10. class attachments {
  11. private $att_db;
  12. function __construct() {
  13. pc_base::load_app_func('global');
  14. $this->upload_url = pc_base::load_config('system','upload_url');
  15. $this->upload_path = pc_base::load_config('system','upload_path');
  16. $this->imgext = array('jpg','gif','png','bmp','jpeg');
  17. $this->userid = $_SESSION['userid'] ? $_SESSION['userid'] : (param::get_cookie('_userid') ? param::get_cookie('_userid') : sys_auth($_POST['userid_flash'],'DECODE'));
  18. $this->isadmin = $this->admin_username = $_SESSION['roleid'] ? 1 : 0;
  19. $this->groupid = param::get_cookie('_groupid') ? param::get_cookie('_groupid') : 8;
  20. //判断是否登录
  21. if(empty($this->userid)){
  22. showmessage(L('please_login','','member'));
  23. }
  24. }
  25. /**
  26. * 常规上传
  27. */
  28. public function upload() {
  29. $grouplist = getcache('grouplist','member');
  30. if($this->isadmin==0 && !$grouplist[$this->groupid]['allowattachment']) return false;
  31. if($this->isadmin==1) define('IN_ADMIN',true);
  32. pc_base::load_sys_class('attachment','',0);
  33. $module = trim($_GET['module']);
  34. $catid = intval($_GET['catid']);
  35. $siteid = $this->get_siteid();
  36. $site_setting = get_site_setting($siteid);
  37. $site_allowext = $site_setting['upload_allowext'];
  38. $attachment = new attachment($module,$catid,$siteid);
  39. $attachment->set_userid($this->userid);
  40. $a = $attachment->upload('upload',$site_allowext);
  41. if($a){
  42. $filepath = $attachment->uploadedfiles[0]['filepath'];
  43. $fn = intval($_GET['CKEditorFuncNum']);
  44. $this->upload_json($a[0],$filepath,$attachment->uploadedfiles[0]['filename']);
  45. $attachment->mkhtml($fn,$this->upload_url.$filepath,'');
  46. }
  47. }
  48. /**
  49. * swfupload上传附件(已改)
  50. * web uploader上传
  51. */
  52. public function swfupload(){
  53. $grouplist = getcache('grouplist','member');
  54. $siteid = $this->get_siteid();
  55. if(isset($_GET['dosubmit'])){
  56. pc_base::load_sys_class('attachment','',0);
  57. $siteid = get_siteid();
  58. $attachment = new attachment($_POST['module'],$_POST['catid'],$siteid);
  59. $attachment->set_userid($_POST['userid']);
  60. $site_setting = get_site_setting($siteid);
  61. $site_allowext = $site_setting['upload_allowext'];
  62. $aids = $attachment->upload('file',$site_allowext,0,0,array(intval($_POST['thumb_width']),intval($_POST['thumb_height'])),intval($_POST['watermark_enable']));
  63. if($aids[0]) {
  64. $filename= (strtolower(CHARSET) != 'utf-8') ? iconv('gbk', 'utf-8', $attachment->uploadedfiles[0]['filename']) : $attachment->uploadedfiles[0]['filename'];
  65. if($attachment->uploadedfiles[0]['isimage']) {
  66. $res = array(
  67. 'status' => 1,
  68. 'aid' => $aids[0],
  69. 'filepath' => $this->upload_url.$attachment->uploadedfiles[0]['filepath'],
  70. 'fileext' => $attachment->uploadedfiles[0]['fileext'],
  71. 'isimage' => $attachment->uploadedfiles[0]['isimage'],
  72. 'title' => str_replace('.'.$attachment->uploadedfiles[0]['fileext'],'',$filename),
  73. 'fileimg' => $this->upload_url.$attachment->uploadedfiles[0]['filepath'],
  74. 'filename' => $filename
  75. );
  76. echo json_encode($res);
  77. } else {
  78. $fileext = $attachment->uploadedfiles[0]['fileext'];
  79. if($fileext == 'zip' || $fileext == 'rar') $fileext = 'rar';
  80. elseif($fileext == 'doc' || $fileext == 'docx') $fileext = 'doc';
  81. elseif($fileext == 'xls' || $fileext == 'xlsx') $fileext = 'xls';
  82. elseif($fileext == 'ppt' || $fileext == 'pptx') $fileext = 'ppt';
  83. elseif ($fileext == 'flv' || $fileext == 'swf' || $fileext == 'rm' || $fileext == 'rmvb') $fileext = 'flv';
  84. else $fileext = 'do';
  85. $res = array(
  86. 'status' => 1,
  87. 'aid' => $aids[0],
  88. 'filepath' => $this->upload_url.$attachment->uploadedfiles[0]['filepath'],
  89. 'fileext' => $fileext,
  90. 'isimage' => $attachment->uploadedfiles[0]['isimage'],
  91. 'title' => str_replace('.'.$attachment->uploadedfiles[0]['fileext'],'',$filename),
  92. 'fileimg' => file_icon($this->upload_url.$attachment->uploadedfiles[0]['filepath']),
  93. 'filename' => $filename
  94. );
  95. echo json_encode($res);
  96. }
  97. exit;
  98. } else {
  99. echo '0,'.$attachment->error();
  100. exit;
  101. }
  102. } else {
  103. if($this->isadmin==0 && !$grouplist[$this->groupid]['allowattachment']) showmessage(L('att_no_permission'));
  104. $args = $_GET['args'];
  105. $authkey = $_GET['authkey'];
  106. $type = $_GET['type'];
  107. if(upload_key($args) != $authkey) showmessage(L('attachment_parameter_error'));
  108. extract(getswfinit($_GET['args']));
  109. $siteid = $this->get_siteid();
  110. $site_setting = get_site_setting($siteid);
  111. $file_size_limit = sizecount($site_setting['upload_maxsize']*1024);
  112. $filetype = str_replace('|',',',$file_types_post);
  113. $att_not_used = param::get_cookie('att_json');
  114. if(empty($att_not_used) || !isset($att_not_used)) $tab_status = ' class="on"';
  115. if(!empty($att_not_used)) $div_status = ' hidden';
  116. //获取临时未处理文件列表
  117. $att = $this->att_not_used();
  118. $userid_flash=sys_auth($this->userid, 'ENCODE');
  119. // include $this->admin_tpl('swfupload');
  120. include $this->admin_tpl('webuploader');
  121. }
  122. }
  123. public function crop_upload() {
  124. // if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {
  125. //调整 版本问题 $GLOBALS["HTTP_RAW_POST_DATA"] 已经废弃
  126. $file = file_get_contents('php://input');
  127. if (isset($file)) {
  128. $pic = $file;
  129. if (isset($_GET['width']) && !empty($_GET['width'])) {
  130. $width = intval($_GET['width']);
  131. }
  132. if (isset($_GET['height']) && !empty($_GET['height'])) {
  133. $height = intval($_GET['height']);
  134. }
  135. if (isset($_GET['file']) && !empty($_GET['file'])) {
  136. $_GET['file'] = str_ireplace(array(';','php'),'',$_GET['file']);
  137. if(is_image($_GET['file'])== false || stripos($_GET['file'],'.php')!==false) exit();
  138. if (strpos($_GET['file'], pc_base::load_config('system', 'upload_url'))!==false) {
  139. $file = $_GET['file'];
  140. $basename = basename($file);
  141. if (strpos($basename, 'thumb_')!==false) {
  142. $file_arr = explode('_', $basename);
  143. $basename = array_pop($file_arr);
  144. }
  145. $fileext = strtolower(fileext($basename));
  146. if (!in_array($fileext, array('jpg', 'gif', 'jpeg', 'png', 'bmp'))) exit();
  147. $new_file = 'thumb_'.$width.'_'.$height.'_'.$basename;
  148. } else {
  149. pc_base::load_sys_class('attachment','',0);
  150. $module = trim($_GET['module']);
  151. $catid = intval($_GET['catid']);
  152. $siteid = $this->get_siteid();
  153. $attachment = new attachment($module, $catid, $siteid);
  154. $uploadedfile['filename'] = basename($_GET['file']);
  155. $uploadedfile['fileext'] = strtolower(fileext($_GET['file']));
  156. if (in_array($uploadedfile['fileext'], array('jpg', 'gif', 'jpeg', 'png', 'bmp'))) {
  157. $uploadedfile['isimage'] = 1;
  158. }
  159. $file_path = $this->upload_path.date('Y/md/');
  160. pc_base::load_sys_func('dir');
  161. dir_create($file_path);
  162. $new_file = date('Ymdhis').rand(100, 999).'.'.$uploadedfile['fileext'];
  163. $uploadedfile['filepath'] = date('Y/md/').$new_file;
  164. $aid = $attachment->add($uploadedfile);
  165. }
  166. $filepath = date('Y/md/');
  167. file_put_contents($this->upload_path.$filepath.$new_file, $pic);
  168. } else {
  169. return false;
  170. }
  171. echo pc_base::load_config('system', 'upload_url').$filepath.$new_file;
  172. exit;
  173. }
  174. }
  175. /**
  176. * 删除附件
  177. */
  178. public function swfdelete() {
  179. $attachment = pc_base::load_sys_class('attachment');
  180. $att_del_arr = explode('|',$_GET['data']);
  181. foreach($att_del_arr as $n=>$att){
  182. if($att) $attachment->delete(array('aid'=>$att,'userid'=>$this->userid,'uploadip'=>ip()));
  183. }
  184. }
  185. /**
  186. * 加载图片库
  187. */
  188. public function album_load() {
  189. if(!$this->admin_username) return false;
  190. $where = $uploadtime= '';
  191. $this->att_db= pc_base::load_model('attachment_model');
  192. if($_GET['args']) extract(getswfinit($_GET['args']));
  193. if($_GET['dosubmit']){
  194. extract($_GET['info']);
  195. $where = '';
  196. $filename = safe_replace($filename);
  197. if($filename) $where = "AND `filename` LIKE '%$filename%' ";
  198. if($uploadtime) {
  199. $start_uploadtime = strtotime($uploadtime.' 00:00:00');
  200. $stop_uploadtime = strtotime($uploadtime.' 23:59:59');
  201. $where .= "AND `uploadtime` >= '$start_uploadtime' AND `uploadtime` <= '$stop_uploadtime'";
  202. }
  203. if($where) $where = substr($where, 3);
  204. }
  205. pc_base::load_sys_class('form');
  206. $page = $_GET['page'] ? $_GET['page'] : '1';
  207. $infos = $this->att_db->listinfo($where, 'aid DESC', $page, 8,'',5);
  208. foreach($infos as $n=>$v){
  209. $ext = fileext($v['filepath']);
  210. if(in_array($ext,$this->imgext)) {
  211. $infos[$n]['src']=$this->upload_url.$v['filepath'];
  212. $infos[$n]['width']='80';
  213. } else {
  214. $infos[$n]['src']=file_icon($v['filepath']);
  215. $infos[$n]['width']='64';
  216. }
  217. }
  218. $pages = $this->att_db->pages;
  219. include $this->admin_tpl('album_list');
  220. }
  221. /**
  222. * 目录浏览模式添加图片
  223. */
  224. public function album_dir() {
  225. if(!$this->admin_username) return false;
  226. if($_GET['args']) extract(getswfinit($_GET['args']));
  227. $dir = isset($_GET['dir']) && trim($_GET['dir']) ? str_replace(array('..\\', '../', './', '.\\','..','.*'), '', trim($_GET['dir'])) : '';
  228. $filepath = $this->upload_path.$dir;
  229. $list = glob($filepath.'/'.'*');
  230. if(!empty($list)) rsort($list);
  231. $local = str_replace(array(PC_PATH, PHPCMS_PATH ,DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR), array('','',DIRECTORY_SEPARATOR), $filepath);
  232. $url = ($dir == '.' || $dir=='') ? $this->upload_url : $this->upload_url.str_replace('.', '', $dir).'/';
  233. $show_header = true;
  234. include $this->admin_tpl('album_dir');
  235. }
  236. /**
  237. * 设置upload上传的json格式cookie
  238. */
  239. private function upload_json($aid,$src,$filename) {
  240. $arr['aid'] = intval($aid);
  241. $arr['src'] = trim($src);
  242. $arr['filename'] = urlencode($filename);
  243. $json_str = json_encode($arr);
  244. $att_arr_exist = param::get_cookie('att_json');
  245. $att_arr_exist_tmp = explode('||', $att_arr_exist);
  246. if(is_array($att_arr_exist_tmp) && in_array($json_str, $att_arr_exist_tmp)) {
  247. return true;
  248. } else {
  249. $json_str = $att_arr_exist ? $att_arr_exist.'||'.$json_str : $json_str;
  250. param::set_cookie('att_json',$json_str);
  251. return true;
  252. }
  253. }
  254. /**
  255. * 设置swfupload上传的json格式cookie
  256. */
  257. public function swfupload_json() {
  258. $arr['aid'] = intval($_GET['aid']);
  259. $arr['src'] = safe_replace(trim($_GET['src']));
  260. $arr['filename'] = urlencode(safe_replace($_GET['filename']));
  261. $json_str = json_encode($arr);
  262. $att_arr_exist = param::get_cookie('att_json');
  263. $att_arr_exist_tmp = explode('||', $att_arr_exist);
  264. if(is_array($att_arr_exist_tmp) && in_array($json_str, $att_arr_exist_tmp)) {
  265. return true;
  266. } else {
  267. $json_str = $att_arr_exist ? $att_arr_exist.'||'.$json_str : $json_str;
  268. param::set_cookie('att_json',$json_str);
  269. return true;
  270. }
  271. }
  272. /**
  273. * 删除swfupload上传的json格式cookie
  274. */
  275. public function swfupload_json_del() {
  276. $arr['aid'] = intval($_GET['aid']);
  277. $arr['src'] = trim($_GET['src']);
  278. $arr['filename'] = urlencode($_GET['filename']);
  279. $json_str = json_encode($arr);
  280. $att_arr_exist = param::get_cookie('att_json');
  281. $att_arr_exist = str_replace(array($json_str,'||||'), array('','||'), $att_arr_exist);
  282. $att_arr_exist = preg_replace('/^\|\|||\|\|$/i', '', $att_arr_exist);
  283. param::set_cookie('att_json',$att_arr_exist);
  284. }
  285. private function att_not_used() {
  286. $this->att_db= pc_base::load_model('attachment_model');
  287. //获取临时未处理文件列表
  288. if($att_json = param::get_cookie('att_json')) {
  289. if($att_json) $att_cookie_arr = explode('||', $att_json);
  290. foreach ($att_cookie_arr as $_att_c) $att[] = json_decode($_att_c,true);
  291. if(is_array($att) && !empty($att)) {
  292. foreach ($att as $n=>$v) {
  293. $ext = fileext($v['src']);
  294. if(in_array($ext,$this->imgext)) {
  295. $att[$n]['fileimg']=$v['src'];
  296. $att[$n]['width']='80';
  297. $att[$n]['filename']=urldecode($v['filename']);
  298. } else {
  299. $att[$n]['fileimg']=file_icon($v['src']);
  300. $att[$n]['width']='64';
  301. $att[$n]['filename']=urldecode($v['filename']);
  302. }
  303. $this->cookie_att .= '|'.$v['src'];
  304. }
  305. }
  306. }
  307. return $att;
  308. }
  309. final public static function admin_tpl($file, $m = '') {
  310. $m = empty($m) ? ROUTE_M : $m;
  311. if(empty($m)) return false;
  312. return PC_PATH.'modules'.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$file.'.tpl.php';
  313. }
  314. final public static function get_siteid() {
  315. return get_siteid();
  316. }
  317. }
  318. ?>