attachment.ctrl.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. if ($do == 'upload_remote') {
  12. if (!empty($_W['setting']['remote_complete_info']['type'])) {
  13. $result = file_dir_remote_upload(ATTACHMENT_ROOT . 'images');
  14. if (is_error($result)) {
  15. iajax(1, $result['message']);
  16. } else {
  17. if (file_dir_exist_image(ATTACHMENT_ROOT . 'images')) {
  18. iajax(2);
  19. } else {
  20. iajax(0);
  21. }
  22. }
  23. } else {
  24. iajax(1, '请先填写并开启远程附件设置');
  25. }
  26. }
  27. if ($do == 'global') {
  28. if (empty($_W['setting']['upload'])) {
  29. $upload = $_W['config']['upload'];
  30. } else {
  31. $upload = $_W['setting']['upload'];
  32. }
  33. $post_max_size = ini_get('post_max_size');
  34. $post_max_size = $post_max_size > 0 ? bytecount($post_max_size) / 1024 : 0;
  35. $upload_max_filesize = ini_get('upload_max_filesize');
  36. if (checksubmit('submit')) {
  37. $harmtype = array('asp','php','jsp','js','css','php3','php4','php5','ashx','aspx','exe','cgi');
  38. switch ($_GPC['key']) {
  39. case 'attachment_limit':
  40. $upload['attachment_limit'] = max(0, intval($_GPC['value'])); break;
  41. case 'image_thumb':
  42. $upload['image']['thumb'] = empty($_GPC['value']) ? 0 : 1;
  43. break;
  44. case 'image_width':
  45. $upload['image']['width'] = intval($_GPC['value']); break;
  46. case 'image_extentions':
  47. $upload['image']['extentions'] = array();
  48. $image_extentions = explode("\n", safe_gpc_string($_GPC['value']));
  49. foreach ($image_extentions as $item) {
  50. $item = safe_gpc_string(trim($item));
  51. if (!empty($item) && !in_array($item, $harmtype) && !in_array($item, $upload['image']['extentions'])) {
  52. $upload['image']['extentions'][] = $item;
  53. }
  54. }
  55. break;
  56. case 'image_limit':
  57. $upload['image']['limit'] = max(0, min(intval($_GPC['value']), $post_max_size)); break;
  58. case 'image_zip_percentage':
  59. $zip_percentage = intval($_GPC['value']);
  60. $upload['image']['zip_percentage'] = $zip_percentage;
  61. if ($zip_percentage <= 0 || $zip_percentage > 100) {
  62. $upload['image']['zip_percentage'] = 100; }
  63. break;
  64. case 'audio_extentions':
  65. $upload['audio']['extentions'] = array();
  66. $audio_extentions = explode("\n", safe_gpc_string($_GPC['value']));
  67. foreach ($audio_extentions as $item) {
  68. $item = safe_gpc_string(trim($item));
  69. if (!empty($item) && !in_array($item, $harmtype) && !in_array($item, $upload['audio']['extentions'])) {
  70. $upload['audio']['extentions'][] = $item;
  71. }
  72. }
  73. break;
  74. case 'audio_limit':
  75. $upload['audio']['limit'] = max(0, min(intval($_GPC['value']), $post_max_size)); break;
  76. }
  77. setting_save($upload, 'upload');
  78. iajax(0, '更新设置成功', url('system/attachment'));
  79. }
  80. if (empty($upload['image']['thumb'])) {
  81. $upload['image']['thumb'] = 0;
  82. } else {
  83. $upload['image']['thumb'] = 1;
  84. }
  85. $upload['image']['width'] = intval($upload['image']['width']);
  86. if (empty($upload['image']['width'])) {
  87. $upload['image']['width'] = 800;
  88. }
  89. if (!empty($upload['image']['extentions']) && is_array($upload['image']['extentions'])) {
  90. $upload['image']['extentions'] = implode("\n", $upload['image']['extentions']);
  91. }
  92. if (!empty($upload['audio']['extentions']) && is_array($upload['audio']['extentions'])) {
  93. $upload['audio']['extentions'] = implode("\n", $upload['audio']['extentions']);
  94. }
  95. if(empty($upload['image']['zip_percentage'])) {
  96. $upload['image']['zip_percentage'] = 100;
  97. }
  98. }
  99. if ($do == 'remote') {
  100. $remote = $_W['setting']['remote_complete_info'];
  101. $remote_urls = array(
  102. 'alioss' => array('old_url' => $remote['alioss']['url']),
  103. 'ftp' => array('old_url' => $remote['ftp']['url']),
  104. 'qiniu' => array('old_url' => $remote['qiniu']['url']),
  105. 'cos' => array('old_url' => $remote['cos']['url']),
  106. );
  107. if (checksubmit('submit')) {
  108. $remote = array(
  109. 'type' => intval($_GPC['type']),
  110. 'ftp' => array(
  111. 'ssl' => intval($_GPC['ftp']['ssl']),
  112. 'host' => $_GPC['ftp']['host'],
  113. 'port' => $_GPC['ftp']['port'],
  114. 'username' => $_GPC['ftp']['username'],
  115. 'password' => strexists($_GPC['ftp']['password'], '*') ? $_W['setting']['remote_complete_info']['ftp']['password'] : $_GPC['ftp']['password'],
  116. 'pasv' => intval($_GPC['ftp']['pasv']),
  117. 'dir' => $_GPC['ftp']['dir'],
  118. 'url' => $_GPC['ftp']['url'],
  119. 'overtime' => intval($_GPC['ftp']['overtime']),
  120. ),
  121. 'alioss' => array(
  122. 'key' => $_GPC['alioss']['key'],
  123. 'secret' => strexists($_GPC['alioss']['secret'], '*') ? $_W['setting']['remote_complete_info']['alioss']['secret'] : $_GPC['alioss']['secret'],
  124. 'bucket' => $_GPC['alioss']['bucket'],
  125. 'internal' => $_GPC['alioss']['internal'],
  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. attachment_replace_article_remote_url($remote_urls['alioss']['old_url'], $remote['alioss']['url']);
  167. } elseif ($remote['type'] == ATTACH_FTP) {
  168. if (empty($remote['ftp']['host'])) {
  169. itoast('FTP服务器地址为必填项.', '', '');
  170. }
  171. if (empty($remote['ftp']['username'])) {
  172. itoast('FTP帐号为必填项.', '', '');
  173. }
  174. if (empty($remote['ftp']['password'])) {
  175. itoast('FTP密码为必填项.', '', '');
  176. }
  177. attachment_replace_article_remote_url($remote_urls['ftp']['old_url'], $_GPC['ftp']['url']);
  178. } elseif ($remote['type'] == ATTACH_QINIU) {
  179. if (empty($remote['qiniu']['accesskey'])) {
  180. itoast('请填写Accesskey', referer(), 'info');
  181. }
  182. if (empty($remote['qiniu']['secretkey'])) {
  183. itoast('secretkey', referer(), 'info');
  184. }
  185. if (empty($remote['qiniu']['bucket'])) {
  186. itoast('请填写bucket', referer(), 'info');
  187. }
  188. if (empty($remote['qiniu']['url'])) {
  189. itoast('请填写url', referer(), 'info');
  190. } else {
  191. $remote['qiniu']['url'] = strexists($remote['qiniu']['url'], 'http') ? trim($remote['qiniu']['url'], '/') : 'http://'. trim($remote['qiniu']['url'], '/');
  192. }
  193. attachment_replace_article_remote_url($remote_urls['qiniu']['old_url'], $remote['qiniu']['url']);
  194. $auth = attachment_qiniu_auth($remote['qiniu']['accesskey'], $remote['qiniu']['secretkey'], $remote['qiniu']['bucket']);
  195. if (is_error($auth)) {
  196. $message = $auth['message']['error'] == 'bad token' ? 'Accesskey或Secretkey填写错误, 请检查后重新提交' : 'bucket填写错误或是bucket所对应的存储区域选择错误,请检查后重新提交';
  197. itoast($message, referer(), 'info');
  198. }
  199. } elseif ($remote['type'] == ATTACH_COS) {
  200. if (empty($remote['cos']['appid'])) {
  201. itoast('请填写APPID', referer(), 'info');
  202. }
  203. if (empty($remote['cos']['secretid'])) {
  204. itoast('请填写SECRETID', referer(), 'info');
  205. }
  206. if (empty($remote['cos']['secretkey'])) {
  207. itoast('请填写SECRETKEY', referer(), 'info');
  208. }
  209. if (empty($remote['cos']['bucket'])) {
  210. itoast('请填写BUCKET', referer(), 'info');
  211. }
  212. $remote['cos']['bucket'] = str_replace("-{$remote['cos']['appid']}", '', trim($remote['cos']['bucket']));
  213. if (empty($url)) {
  214. $url = sprintf('https://%s-%s.cos%s.myqcloud.com', $bucket, $appid, $_GPC['local']);
  215. }
  216. if (empty($remote['cos']['url'])) {
  217. $remote['cos']['url'] = sprintf('https://%s-%s.cos%s.myqcloud.com', $remote['cos']['bucket'], $remote['cos']['appid'], $remote['cos']['local']);
  218. }
  219. $remote['cos']['url'] = rtrim($remote['cos']['url'], '/');
  220. attachment_replace_article_remote_url($remote_urls['cos']['old_url'], $remote['cos']['url']);
  221. $auth = attachment_cos_auth($remote['cos']['bucket'], $remote['cos']['appid'], $remote['cos']['secretid'], $remote['cos']['secretkey'], $remote['cos']['local']);
  222. if (is_error($auth)) {
  223. itoast($auth['message'], referer(), 'info');
  224. }
  225. }
  226. $_W['setting']['remote_complete_info']['type'] = $remote['type'];
  227. $_W['setting']['remote_complete_info']['alioss'] = $remote['alioss'];
  228. $_W['setting']['remote_complete_info']['ftp'] = $remote['ftp'];
  229. $_W['setting']['remote_complete_info']['qiniu'] = $remote['qiniu'];
  230. $_W['setting']['remote_complete_info']['cos'] = $remote['cos'];
  231. setting_save($_W['setting']['remote_complete_info'], 'remote');
  232. itoast('远程附件配置信息更新成功!', url('system/attachment/remote'), 'success');
  233. }
  234. $bucket_datacenter = attachment_alioss_datacenters();
  235. $local_attachment = file_dir_exist_image(ATTACHMENT_ROOT . 'images');
  236. }
  237. if ($do == 'buckets') {
  238. $key = $_GPC['key'];
  239. $secret = $_GPC['secret'];
  240. $buckets = attachment_alioss_buctkets($key, $secret);
  241. if (is_error($buckets)) {
  242. iajax(-1, '');
  243. }
  244. $bucket_datacenter = attachment_alioss_datacenters();
  245. $bucket = array();
  246. foreach ($buckets as $key => $value) {
  247. $value['loca_name'] = $key. '@@'. $bucket_datacenter[$value['location']];
  248. $bucket[] = $value;
  249. }
  250. iajax(1, $bucket, '');
  251. }
  252. if($do == 'ftp') {
  253. load()->library('ftp');
  254. $ftp_config = array(
  255. 'hostname' => trim($_GPC['host']),
  256. 'username' => trim($_GPC['username']),
  257. 'password' => strexists($_GPC['password'], '*') ? $_W['setting']['remote_complete_info']['ftp']['password'] : trim($_GPC['password']),
  258. 'port' => intval($_GPC['port']),
  259. 'ssl' => trim($_GPC['ssl']),
  260. 'passive' => trim($_GPC['pasv']),
  261. 'timeout' => intval($_GPC['overtime']),
  262. 'rootdir' => trim($_GPC['dir']),
  263. );
  264. $url = trim($_GPC['url']);
  265. $filename = 'MicroEngine.ico';
  266. $ftp = new Ftp($ftp_config);
  267. if (true === $ftp->connect()) {
  268. if ($ftp->upload(ATTACHMENT_ROOT .'images/global/'. $filename, $filename)) {
  269. load()->func('communication');
  270. $response = ihttp_get($url. '/'. $filename);
  271. if (is_error($response)) {
  272. iajax(-1, '配置失败,FTP远程访问url错误');
  273. }
  274. if (intval($response['code']) != 200) {
  275. iajax(-1, '配置失败,FTP远程访问url错误');
  276. }
  277. $image = getimagesizefromstring($response['content']);
  278. if (!empty($image) && strexists($image['mime'], 'image')) {
  279. iajax(0,'配置成功');
  280. } else {
  281. iajax(-1, '配置失败,FTP远程访问url错误');
  282. }
  283. } else {
  284. iajax(-1, '上传图片失败,请检查配置');
  285. }
  286. } else {
  287. iajax(-1, 'FTP服务器连接失败,请检查配置');
  288. }
  289. }
  290. if ($do == 'oss') {
  291. load()->model('attachment');
  292. $key = $_GPC['key'];
  293. $secret = strexists($_GPC['secret'], '*') ? $_W['setting']['remote_complete_info']['alioss']['secret'] : $_GPC['secret'];
  294. $bucket = $_GPC['bucket'];
  295. $buckets = attachment_alioss_buctkets($key, $secret);
  296. list($bucket, $url) = explode('@@', $_GPC['bucket']);
  297. $result = attachment_newalioss_auth($key, $secret, $bucket, $_GPC['internal']);
  298. if (is_error($result)) {
  299. iajax(-1, 'OSS-Access Key ID 或 OSS-Access Key Secret错误,请重新填写');
  300. }
  301. $ossurl = $buckets[$bucket]['location'].'.aliyuncs.com';
  302. if (!empty($_GPC['url'])) {
  303. if (!strexists($_GPC['url'], 'http://') && !strexists($_GPC['url'],'https://')) {
  304. $url = 'http://'. trim($_GPC['url']);
  305. } else {
  306. $url = trim($_GPC['url']);
  307. }
  308. $url = trim($url, '/').'/';
  309. } else {
  310. $url = 'http://'.$bucket.'.'.$buckets[$bucket]['location'].'.aliyuncs.com/';
  311. }
  312. load()->func('communication');
  313. $filename = 'MicroEngine.ico';
  314. $response = ihttp_request($url. '/'.$filename, array(), array('CURLOPT_REFERER' => $_SERVER['SERVER_NAME']));
  315. if (is_error($response)) {
  316. iajax(-1, '配置失败,阿里云访问url错误');
  317. }
  318. if (intval($response['code']) != 200) {
  319. iajax(-1, '配置失败,阿里云访问url错误,请保证bucket为公共读取的');
  320. }
  321. $image = getimagesizefromstring($response['content']);
  322. if (!empty($image) && strexists($image['mime'], 'image')) {
  323. iajax(0,'配置成功');
  324. } else {
  325. iajax(-1, '配置失败,阿里云访问url错误');
  326. }
  327. }
  328. if ($do == 'qiniu') {
  329. load()->model('attachment');
  330. $_GPC['secretkey'] = strexists($_GPC['secretkey'], '*') ? $_W['setting']['remote_complete_info']['qiniu']['secretkey'] : $_GPC['secretkey'];
  331. $auth= attachment_qiniu_auth(trim($_GPC['accesskey']), trim($_GPC['secretkey']), trim($_GPC['bucket']));
  332. if (is_error($auth)) {
  333. iajax(-1, '配置失败,请检查配置。注:请检查存储区域是否选择的是和bucket对应<br/>的区域', '');
  334. }
  335. load()->func('communication');
  336. $url = $_GPC['url'];
  337. $url = strexists($url, 'http') ? trim($url, '/') : 'http://'.trim($url, '/');
  338. $filename = 'MicroEngine.ico';
  339. $response = ihttp_request($url. '/'.$filename, array(), array('CURLOPT_REFERER' => $_SERVER['SERVER_NAME']));
  340. if (is_error($response)) {
  341. iajax(-1, '配置失败,七牛访问url错误');
  342. }
  343. if (intval($response['code']) != 200) {
  344. iajax(-1, '配置失败,七牛访问url错误,请保证bucket为公共读取的');
  345. }
  346. $image = getimagesizefromstring($response['content']);
  347. if (!empty($image) && strexists($image['mime'], 'image')) {
  348. iajax(0,'配置成功');
  349. } else {
  350. iajax(-1, '配置失败,七牛访问url错误');
  351. }
  352. }
  353. if ($do == 'cos') {
  354. load()->model('attachment');
  355. $url = $_GPC['url'];
  356. $appid = trim($_GPC['appid']);
  357. $secretid = trim($_GPC['secretid']);
  358. $secretkey = strexists($_GPC['secretkey'], '*') ? $_W['setting']['remote_complete_info']['cos']['secretkey'] : trim($_GPC['secretkey']);
  359. $bucket = str_replace("-{$appid}", '', trim($_GPC['bucket']));
  360. if (empty($url)) {
  361. $url = sprintf('https://%s-%s.cos%s.myqcloud.com', $bucket, $appid, $_GPC['local']);
  362. }
  363. $url = rtrim($url, '/');
  364. $auth= attachment_cos_auth($bucket, $appid, $secretid, $secretkey, $_GPC['local']);
  365. if (is_error($auth)) {
  366. iajax(-1, '配置失败,请检查配置' . $auth['message'], '');
  367. }
  368. load()->func('communication');
  369. $filename = 'MicroEngine.ico';
  370. $response = ihttp_request($url. '/'.$filename, array(), array('CURLOPT_REFERER' => $_SERVER['SERVER_NAME']));
  371. if (is_error($response)) {
  372. iajax(-1, '配置失败,腾讯cos访问url错误');
  373. }
  374. if (intval($response['code']) != 200) {
  375. iajax(-1, '配置失败,腾讯cos访问url错误,请保证bucket为公共读取的');
  376. }
  377. $image = getimagesizefromstring($response['content']);
  378. if (!empty($image) && strexists($image['mime'], 'image')) {
  379. iajax(0,'配置成功');
  380. } else {
  381. iajax(-1, '配置失败,腾讯cos访问url错误');
  382. }
  383. }
  384. template('system/attachment');