attachment.ctrl.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->model('setting');
  8. load()->model('attachment');
  9. $dos = array('attachment', 'remote', 'buckets', 'oss', 'cos', 'qiniu', 'ftp', 'upload_remote');
  10. $do = in_array($do, $dos) ? $do : 'global';
  11. $_W['page']['title'] = '附件设置 - 系统管理';
  12. if ($do == 'upload_remote') {
  13. if (!empty($_W['setting']['remote_complete_info']['type'])) {
  14. $result = file_dir_remote_upload(ATTACHMENT_ROOT . 'images');
  15. if (is_error($result)) {
  16. itoast($result['message'], url('system/attachment/remote'), 'info');
  17. } else {
  18. itoast('上传成功!', url('system/attachment/remote'), 'success');
  19. }
  20. } else {
  21. itoast('请先填写并开启远程附件设置。', '', 'info');
  22. }
  23. }
  24. if ($do == 'global') {
  25. $post_max_size = ini_get('post_max_size');
  26. $post_max_size = $post_max_size > 0 ? bytecount($post_max_size) / 1024 : 0;
  27. $upload_max_filesize = ini_get('upload_max_filesize');
  28. if (checksubmit('submit')) {
  29. $harmtype = array('asp','php','jsp','js','css','php3','php4','php5','ashx','aspx','exe','cgi');
  30. $upload = $_GPC['upload'];
  31. if (!empty($upload['image']['thumb'])) {
  32. $upload['image']['thumb'] = 1;
  33. } else {
  34. $upload['image']['thumb'] = 0;
  35. }
  36. $upload['image']['width'] = intval(trim($upload['image']['width']));
  37. if (!empty($upload['image']['thumb']) && empty($upload['image']['width'])) {
  38. itoast('请设置图片缩略宽度.', '', '');
  39. }
  40. $upload['image']['limit'] = max(0, min(intval(trim($upload['image']['limit'])), $post_max_size));
  41. if (empty($upload['image']['limit'])) {
  42. itoast('请设置图片上传支持的文件大小, 单位 KB.', '', '');
  43. }
  44. if (empty($upload['image']['extentions'])) {
  45. itoast('请添加支持的图片附件后缀类型', '', '');
  46. }
  47. if (!empty($upload['image']['extentions'])) {
  48. $upload['image']['extentions'] = explode("\n", $upload['image']['extentions']);
  49. foreach ($upload['image']['extentions'] as $key => &$row) {
  50. $row = trim($row);
  51. if (in_array($row, $harmtype)) {
  52. unset($upload['image']['extentions'][$key]);
  53. continue;
  54. }
  55. }
  56. }
  57. if (!is_array($upload['image']['extentions']) || count($upload['image']['extentions']) < 1) {
  58. itoast('请添加支持的图片附件后缀类型', '', '');
  59. }
  60. $upload['audio']['limit'] = max(0, min(intval(trim($upload['audio']['limit'])), $post_max_size));
  61. if (empty($upload['image']['limit'])) {
  62. itoast('请设置音频视频上传支持的文件大小, 单位 KB.', '', '');
  63. }
  64. $zip_percentage = intval($upload['image']['zip_percentage']);
  65. if($zip_percentage <=0 || $zip_percentage > 100) {
  66. $upload['image']['zip_percentage'] = 100; }
  67. if (!empty($upload['audio']['extentions'])) {
  68. $upload['audio']['extentions'] = explode("\n", $upload['audio']['extentions']);
  69. foreach ($upload['audio']['extentions'] as $key => &$row) {
  70. $row = trim($row);
  71. if (in_array($row, $harmtype)) {
  72. unset($upload['audio']['extentions'][$key]);
  73. continue;
  74. }
  75. }
  76. }
  77. if (!is_array($upload['audio']['extentions']) || count($upload['audio']['extentions']) < 1) {
  78. itoast('请添加支持的音频视频附件后缀类型', '', '');
  79. }
  80. setting_save($upload, 'upload');
  81. itoast('更新设置成功!', url('system/attachment'), 'success');
  82. }
  83. if (empty($_W['setting']['upload'])) {
  84. $upload = $_W['config']['upload'];
  85. } else {
  86. $upload = $_W['setting']['upload'];
  87. }
  88. if (empty($upload['image']['thumb'])) {
  89. $upload['image']['thumb'] = 0;
  90. } else {
  91. $upload['image']['thumb'] = 1;
  92. }
  93. $upload['image']['width'] = intval($upload['image']['width']);
  94. if (empty($upload['image']['width'])) {
  95. $upload['image']['width'] = 800;
  96. }
  97. if (!empty($upload['image']['extentions']) && is_array($upload['image']['extentions'])) {
  98. $upload['image']['extentions'] = implode("\n", $upload['image']['extentions']);
  99. }
  100. if (!empty($upload['audio']['extentions']) && is_array($upload['audio']['extentions'])) {
  101. $upload['audio']['extentions'] = implode("\n", $upload['audio']['extentions']);
  102. }
  103. if(empty($upload['image']['zip_percentage'])) {
  104. $upload['image']['zip_percentage'] = 100;
  105. }
  106. }
  107. if ($do == 'remote') {
  108. if (checksubmit('submit')) {
  109. $remote = array(
  110. 'type' => intval($_GPC['type']),
  111. 'ftp' => array(
  112. 'ssl' => intval($_GPC['ftp']['ssl']),
  113. 'host' => $_GPC['ftp']['host'],
  114. 'port' => $_GPC['ftp']['port'],
  115. 'username' => $_GPC['ftp']['username'],
  116. 'password' => strexists($_GPC['ftp']['password'], '*') ? $_W['setting']['remote_complete_info']['ftp']['password'] : $_GPC['ftp']['password'],
  117. 'pasv' => intval($_GPC['ftp']['pasv']),
  118. 'dir' => $_GPC['ftp']['dir'],
  119. 'url' => $_GPC['ftp']['url'],
  120. 'overtime' => intval($_GPC['ftp']['overtime']),
  121. ),
  122. 'alioss' => array(
  123. 'key' => $_GPC['alioss']['key'],
  124. 'secret' => strexists($_GPC['alioss']['secret'], '*') ? $_W['setting']['remote_complete_info']['alioss']['secret'] : $_GPC['alioss']['secret'],
  125. 'bucket' => $_GPC['alioss']['bucket'],
  126. ),
  127. 'qiniu' => array(
  128. 'accesskey' => trim($_GPC['qiniu']['accesskey']),
  129. 'secretkey' => strexists($_GPC['qiniu']['secretkey'], '*') ? $_W['setting']['remote_complete_info']['qiniu']['secretkey'] : trim($_GPC['qiniu']['secretkey']),
  130. 'bucket' => trim($_GPC['qiniu']['bucket']),
  131. 'url' => trim($_GPC['qiniu']['url'])
  132. ),
  133. 'cos' => array(
  134. 'appid' => trim($_GPC['cos']['appid']),
  135. 'secretid' => trim($_GPC['cos']['secretid']),
  136. 'secretkey' => strexists(trim($_GPC['cos']['secretkey']), '*') ? $_W['setting']['remote_complete_info']['cos']['secretkey'] : trim($_GPC['cos']['secretkey']),
  137. 'bucket' => trim($_GPC['cos']['bucket']),
  138. 'local' => trim($_GPC['cos']['local']),
  139. 'url' => trim($_GPC['cos']['url'])
  140. )
  141. );
  142. if ($remote['type'] == ATTACH_OSS) {
  143. if (trim($remote['alioss']['key']) == '') {
  144. itoast('阿里云OSS-Access Key ID不能为空', '', '');
  145. }
  146. if (trim($remote['alioss']['secret']) == '') {
  147. itoast('阿里云OSS-Access Key Secret不能为空', '', '');
  148. }
  149. $buckets = attachment_alioss_buctkets($remote['alioss']['key'], $remote['alioss']['secret']);
  150. if (is_error($buckets)) {
  151. itoast('OSS-Access Key ID 或 OSS-Access Key Secret错误,请重新填写', '', '');
  152. }
  153. list($remote['alioss']['bucket'], $remote['alioss']['url']) = explode('@@', $_GPC['alioss']['bucket']);
  154. if (empty($buckets[$remote['alioss']['bucket']])) {
  155. itoast('Bucket不存在或是已经被删除', '', '');
  156. }
  157. $remote['alioss']['url'] = 'http://'.$remote['alioss']['bucket'].'.'.$buckets[$remote['alioss']['bucket']]['location'].'.aliyuncs.com';
  158. $remote['alioss']['ossurl'] = $buckets[$remote['alioss']['bucket']]['location'].'.aliyuncs.com';
  159. if(!empty($_GPC['custom']['url'])) {
  160. $url = trim($_GPC['custom']['url'],'/');
  161. if (!strexists($url, 'http://') && !strexists($url, 'https://')) {
  162. $url = 'http://'.$url;
  163. }
  164. $remote['alioss']['url'] = $url;
  165. }
  166. } elseif ($remote['type'] == ATTACH_FTP) {
  167. if (empty($remote['ftp']['host'])) {
  168. itoast('FTP服务器地址为必填项.', '', '');
  169. }
  170. if (empty($remote['ftp']['username'])) {
  171. itoast('FTP帐号为必填项.', '', '');
  172. }
  173. if (empty($remote['ftp']['password'])) {
  174. itoast('FTP密码为必填项.', '', '');
  175. }
  176. } elseif ($remote['type'] == ATTACH_QINIU) {
  177. if (empty($remote['qiniu']['accesskey'])) {
  178. itoast('请填写Accesskey', referer(), 'info');
  179. }
  180. if (empty($remote['qiniu']['secretkey'])) {
  181. itoast('secretkey', referer(), 'info');
  182. }
  183. if (empty($remote['qiniu']['bucket'])) {
  184. itoast('请填写bucket', referer(), 'info');
  185. }
  186. if (empty($remote['qiniu']['url'])) {
  187. itoast('请填写url', referer(), 'info');
  188. } else {
  189. $remote['qiniu']['url'] = strexists($remote['qiniu']['url'], 'http') ? trim($remote['qiniu']['url'], '/') : 'http://'. trim($remote['qiniu']['url'], '/');
  190. }
  191. $auth = attachment_qiniu_auth($remote['qiniu']['accesskey'], $remote['qiniu']['secretkey'], $remote['qiniu']['bucket']);
  192. if (is_error($auth)) {
  193. $message = $auth['message']['error'] == 'bad token' ? 'Accesskey或Secretkey填写错误, 请检查后重新提交' : 'bucket填写错误或是bucket所对应的存储区域选择错误,请检查后重新提交';
  194. itoast($message, referer(), 'info');
  195. }
  196. } elseif ($remote['type'] == ATTACH_COS) {
  197. if (empty($remote['cos']['appid'])) {
  198. itoast('请填写APPID', referer(), 'info');
  199. }
  200. if (empty($remote['cos']['secretid'])) {
  201. itoast('请填写SECRETID', referer(), 'info');
  202. }
  203. if (empty($remote['cos']['secretkey'])) {
  204. itoast('请填写SECRETKEY', referer(), 'info');
  205. }
  206. if (empty($remote['cos']['bucket'])) {
  207. itoast('请填写BUCKET', referer(), 'info');
  208. }
  209. if (empty($remote['cos']['url'])) {
  210. $remote['cos']['url'] = 'http://'.$remote['cos']['bucket'].'-'.$remote['cos']['appid'].'.cos.myqcloud.com';
  211. } else {
  212. if (strexists($remote['cos']['url'], '.cos.myqcloud.com') && !strexists($url, '//'.$remote['cos']['bucket'].'-')) {
  213. $remote['cos']['url'] = 'http://'.$remote['cos']['bucket'].'-'.$remote['cos']['appid'].'.cos.myqcloud.com';
  214. }
  215. $remote['cos']['url'] = strexists($remote['cos']['url'], 'http') ? trim($remote['cos']['url'], '/') : 'http://'. trim($remote['cos']['url'], '/');
  216. }
  217. $auth = attachment_cos_auth($remote['cos']['bucket'], $remote['cos']['appid'], $remote['cos']['secretid'], $remote['cos']['secretkey'], $remote['cos']['local']);
  218. if (is_error($auth)) {
  219. itoast($auth['message'], referer(), 'info');
  220. }
  221. }
  222. $_W['setting']['remote_complete_info']['type'] = $remote['type'];
  223. $_W['setting']['remote_complete_info']['alioss'] = $remote['alioss'];
  224. $_W['setting']['remote_complete_info']['ftp'] = $remote['ftp'];
  225. $_W['setting']['remote_complete_info']['qiniu'] = $remote['qiniu'];
  226. $_W['setting']['remote_complete_info']['cos'] = $remote['cos'];
  227. setting_save($_W['setting']['remote_complete_info'], 'remote');
  228. itoast('远程附件配置信息更新成功!', url('system/attachment/remote'), 'success');
  229. }
  230. $remote = $_W['setting']['remote_complete_info'];
  231. $bucket_datacenter = attachment_alioss_datacenters();
  232. $local_attachment = file_tree(IA_ROOT . '/attachment/images');
  233. if (is_array($local_attachment)) {
  234. foreach ($local_attachment as $key => $attachment) {
  235. $attachment = str_replace(ATTACHMENT_ROOT . 'images/', '', $attachment);
  236. list($file_account) = explode('/', $attachment);
  237. if ($file_account == 'global' || !file_is_image($attachment)) {
  238. unset($local_attachment[$key]);
  239. }
  240. }
  241. }
  242. }
  243. if ($do == 'buckets') {
  244. $key = $_GPC['key'];
  245. $secret = $_GPC['secret'];
  246. $buckets = attachment_alioss_buctkets($key, $secret);
  247. if (is_error($buckets)) {
  248. iajax(-1, '');
  249. }
  250. $bucket_datacenter = attachment_alioss_datacenters();
  251. $bucket = array();
  252. foreach ($buckets as $key => $value) {
  253. $value['loca_name'] = $key. '@@'. $bucket_datacenter[$value['location']];
  254. $bucket[] = $value;
  255. }
  256. iajax(1, $bucket, '');
  257. }
  258. if($do == 'ftp') {
  259. load()->library('ftp');
  260. $ftp_config = array(
  261. 'hostname' => trim($_GPC['host']),
  262. 'username' => trim($_GPC['username']),
  263. 'password' => strexists($_GPC['password'], '*') ? $_W['setting']['remote_complete_info']['ftp']['password'] : trim($_GPC['password']),
  264. 'port' => intval($_GPC['port']),
  265. 'ssl' => trim($_GPC['ssl']),
  266. 'passive' => trim($_GPC['pasv']),
  267. 'timeout' => intval($_GPC['overtime']),
  268. 'rootdir' => trim($_GPC['dir']),
  269. );
  270. $url = trim($_GPC['url']);
  271. $filename = 'MicroEngine.ico';
  272. $ftp = new Ftp($ftp_config);
  273. if (true === $ftp->connect()) {
  274. if ($ftp->upload(ATTACHMENT_ROOT .'images/global/'. $filename, $filename)) {
  275. load()->func('communication');
  276. $response = ihttp_get($url. '/'. $filename);
  277. if (is_error($response)) {
  278. iajax(-1, '配置失败,FTP远程访问url错误');
  279. }
  280. if (intval($response['code']) != 200) {
  281. iajax(-1, '配置失败,FTP远程访问url错误');
  282. }
  283. $image = getimagesizefromstring($response['content']);
  284. if (!empty($image) && strexists($image['mime'], 'image')) {
  285. iajax(0,'配置成功');
  286. } else {
  287. iajax(-1, '配置失败,FTP远程访问url错误');
  288. }
  289. } else {
  290. iajax(-1, '上传图片失败,请检查配置');
  291. }
  292. } else {
  293. iajax(-1, 'FTP服务器连接失败,请检查配置');
  294. }
  295. }
  296. if ($do == 'oss') {
  297. load()->model('attachment');
  298. $key = $_GPC['key'];
  299. $secret = strexists($_GPC['secret'], '*') ? $_W['setting']['remote_complete_info']['alioss']['secret'] : $_GPC['secret'];
  300. $bucket = $_GPC['bucket'];
  301. $buckets = attachment_alioss_buctkets($key, $secret);
  302. list($bucket, $url) = explode('@@', $_GPC['bucket']);
  303. $result = attachment_newalioss_auth($key, $secret, $bucket,$url);
  304. if (is_error($result)) {
  305. iajax(-1, 'OSS-Access Key ID 或 OSS-Access Key Secret错误,请重新填写');
  306. }
  307. $ossurl = $buckets[$bucket]['location'].'.aliyuncs.com';
  308. if (!empty($_GPC['url'])) {
  309. if (!strexists($_GPC['url'], 'http://') && !strexists($_GPC['url'],'https://')) {
  310. $url = 'http://'. trim($_GPC['url']);
  311. } else {
  312. $url = trim($_GPC['url']);
  313. }
  314. $url = trim($url, '/').'/';
  315. } else {
  316. $url = 'http://'.$bucket.'.'.$buckets[$bucket]['location'].'.aliyuncs.com/';
  317. }
  318. load()->func('communication');
  319. $filename = 'MicroEngine.ico';
  320. $response = ihttp_request($url. '/'.$filename, array(), array('CURLOPT_REFERER' => $_SERVER['SERVER_NAME']));
  321. if (is_error($response)) {
  322. iajax(-1, '配置失败,阿里云访问url错误');
  323. }
  324. if (intval($response['code']) != 200) {
  325. iajax(-1, '配置失败,阿里云访问url错误,请保证bucket为公共读取的');
  326. }
  327. $image = getimagesizefromstring($response['content']);
  328. if (!empty($image) && strexists($image['mime'], 'image')) {
  329. iajax(0,'配置成功');
  330. } else {
  331. iajax(-1, '配置失败,阿里云访问url错误');
  332. }
  333. }
  334. if ($do == 'qiniu') {
  335. load()->model('attachment');
  336. $_GPC['secretkey'] = strexists($_GPC['secretkey'], '*') ? $_W['setting']['remote_complete_info']['qiniu']['secretkey'] : $_GPC['secretkey'];
  337. $auth= attachment_qiniu_auth(trim($_GPC['accesskey']), trim($_GPC['secretkey']), trim($_GPC['bucket']));
  338. if (is_error($auth)) {
  339. iajax(-1, '配置失败,请检查配置。注:请检查存储区域是否选择的是和bucket对应<br/>的区域', '');
  340. }
  341. load()->func('communication');
  342. $url = $_GPC['url'];
  343. $url = strexists($url, 'http') ? trim($url, '/') : 'http://'.trim($url, '/');
  344. $filename = 'MicroEngine.ico';
  345. $response = ihttp_request($url. '/'.$filename, array(), array('CURLOPT_REFERER' => $_SERVER['SERVER_NAME']));
  346. if (is_error($response)) {
  347. iajax(-1, '配置失败,七牛访问url错误');
  348. }
  349. if (intval($response['code']) != 200) {
  350. iajax(-1, '配置失败,七牛访问url错误,请保证bucket为公共读取的');
  351. }
  352. $image = getimagesizefromstring($response['content']);
  353. if (!empty($image) && strexists($image['mime'], 'image')) {
  354. iajax(0,'配置成功');
  355. } else {
  356. iajax(-1, '配置失败,七牛访问url错误');
  357. }
  358. }
  359. if ($do == 'cos') {
  360. load()->model('attachment');
  361. $url = $_GPC['url'];
  362. if (empty($url)) {
  363. $url = 'http://'.$_GPC['bucket'].'-'. $_GPC['appid'].'.cos.myqcloud.com';
  364. }
  365. $bucket = trim($_GPC['bucket']);
  366. $_GPC['secretkey'] = strexists($_GPC['secretkey'], '*') ? $_W['setting']['remote_complete_info']['cos']['secretkey'] : $_GPC['secretkey'];
  367. if (!strexists($url, '//'.$bucket.'-') && strexists($url, '.cos.myqcloud.com')) {
  368. $url = 'http://'.$bucket.'-'.trim($_GPC['appid']).'.cos.myqcloud.com';
  369. }
  370. $auth= attachment_cos_auth(trim($_GPC['bucket']), trim($_GPC['appid']), trim($_GPC['secretid']), trim($_GPC['secretkey']), $_GPC['local']);
  371. if (is_error($auth)) {
  372. iajax(-1, '配置失败,请检查配置', '');
  373. }
  374. load()->func('communication');
  375. $url = strexists($url, 'http') ? trim($url, '/') : 'http://'.trim($url, '/');
  376. $filename = 'MicroEngine.ico';
  377. $response = ihttp_request($url. '/'.$filename, array(), array('CURLOPT_REFERER' => $_SERVER['SERVER_NAME']));
  378. if (is_error($response)) {
  379. iajax(-1, '配置失败,腾讯cos访问url错误');
  380. }
  381. if (intval($response['code']) != 200) {
  382. iajax(-1, '配置失败,腾讯cos访问url错误,请保证bucket为公共读取的');
  383. }
  384. $image = getimagesizefromstring($response['content']);
  385. if (!empty($image) && strexists($image['mime'], 'image')) {
  386. iajax(0,'配置成功');
  387. } else {
  388. iajax(-1, '配置失败,腾讯cos访问url错误');
  389. }
  390. }
  391. template('system/attachment');