人人商城

attachment.ctrl.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. iajax(1, $result['message']);
  17. } else {
  18. if (file_dir_exist_image(ATTACHMENT_ROOT . 'images')) {
  19. iajax(2);
  20. } else {
  21. iajax(0);
  22. }
  23. }
  24. } else {
  25. iajax(1, '请先填写并开启远程附件设置');
  26. }
  27. }
  28. if ($do == 'global') {
  29. $post_max_size = ini_get('post_max_size');
  30. $post_max_size = $post_max_size > 0 ? bytecount($post_max_size) / 1024 : 0;
  31. $upload_max_filesize = ini_get('upload_max_filesize');
  32. if (checksubmit('submit')) {
  33. $harmtype = array('asp','php','jsp','js','css','php3','php4','php5','ashx','aspx','exe','cgi');
  34. $upload = $_GPC['upload'];
  35. if (!empty($upload['image']['thumb'])) {
  36. $upload['image']['thumb'] = 1;
  37. } else {
  38. $upload['image']['thumb'] = 0;
  39. }
  40. $upload['image']['width'] = intval(trim($upload['image']['width']));
  41. if (!empty($upload['image']['thumb']) && empty($upload['image']['width'])) {
  42. itoast('请设置图片缩略宽度.', '', '');
  43. }
  44. $upload['image']['limit'] = max(0, min(intval(trim($upload['image']['limit'])), $post_max_size));
  45. if (empty($upload['image']['limit'])) {
  46. itoast('请设置图片上传支持的文件大小, 单位 KB.', '', '');
  47. }
  48. if (!empty($upload['image']['extentions'])) {
  49. $upload['image']['extentions'] = explode("\n", $upload['image']['extentions']);
  50. foreach ($upload['image']['extentions'] as $key => &$row) {
  51. $row = trim($row);
  52. if (in_array($row, $harmtype)) {
  53. unset($upload['image']['extentions'][$key]);
  54. continue;
  55. }
  56. }
  57. }
  58. $upload['audio']['limit'] = max(0, min(intval(trim($upload['audio']['limit'])), $post_max_size));
  59. if (empty($upload['image']['limit'])) {
  60. itoast('请设置音频视频上传支持的文件大小, 单位 KB.', '', '');
  61. }
  62. $zip_percentage = intval($upload['image']['zip_percentage']);
  63. if($zip_percentage <=0 || $zip_percentage > 100) {
  64. $upload['image']['zip_percentage'] = 100; }
  65. if (!empty($upload['audio']['extentions'])) {
  66. $upload['audio']['extentions'] = explode("\n", $upload['audio']['extentions']);
  67. foreach ($upload['audio']['extentions'] as $key => &$row) {
  68. $row = trim($row);
  69. if (in_array($row, $harmtype)) {
  70. unset($upload['audio']['extentions'][$key]);
  71. continue;
  72. }
  73. }
  74. }
  75. if (!is_array($upload['audio']['extentions']) || count($upload['audio']['extentions']) < 1) {
  76. $upload['audio']['extentions'] = '';
  77. }
  78. $upload['attachment_limit'] = empty($upload['attachment_limit']) ? 0 : max(0, intval($upload['attachment_limit']));
  79. setting_save($upload, 'upload');
  80. itoast('更新设置成功!', url('system/attachment'), 'success');
  81. }
  82. if (empty($_W['setting']['upload'])) {
  83. $upload = $_W['config']['upload'];
  84. } else {
  85. $upload = $_W['setting']['upload'];
  86. }
  87. if (empty($upload['image']['thumb'])) {
  88. $upload['image']['thumb'] = 0;
  89. } else {
  90. $upload['image']['thumb'] = 1;
  91. }
  92. $upload['image']['width'] = intval($upload['image']['width']);
  93. if (empty($upload['image']['width'])) {
  94. $upload['image']['width'] = 800;
  95. }
  96. if (!empty($upload['image']['extentions']) && is_array($upload['image']['extentions'])) {
  97. $upload['image']['extentions'] = implode("\n", $upload['image']['extentions']);
  98. }
  99. if (!empty($upload['audio']['extentions']) && is_array($upload['audio']['extentions'])) {
  100. $upload['audio']['extentions'] = implode("\n", $upload['audio']['extentions']);
  101. }
  102. if(empty($upload['image']['zip_percentage'])) {
  103. $upload['image']['zip_percentage'] = 100;
  104. }
  105. }
  106. if ($do == 'remote') {
  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. } 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. $remote['cos']['bucket'] = str_replace("-{$remote['cos']['appid']}", '', trim($remote['cos']['bucket']));
  210. if (empty($url)) {
  211. $url = sprintf('https://%s-%s.cos%s.myqcloud.com', $bucket, $appid, $_GPC['local']);
  212. }
  213. if (empty($remote['cos']['url'])) {
  214. $remote['cos']['url'] = sprintf('https://%s-%s.cos%s.myqcloud.com', $remote['cos']['bucket'], $remote['cos']['appid'], $remote['cos']['local']);
  215. }
  216. $remote['cos']['url'] = rtrim($remote['cos']['url'], '/');
  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_dir_exist_image(ATTACHMENT_ROOT . 'images');
  233. }
  234. if ($do == 'buckets') {
  235. $key = $_GPC['key'];
  236. $secret = $_GPC['secret'];
  237. $buckets = attachment_alioss_buctkets($key, $secret);
  238. if (is_error($buckets)) {
  239. iajax(-1, '');
  240. }
  241. $bucket_datacenter = attachment_alioss_datacenters();
  242. $bucket = array();
  243. foreach ($buckets as $key => $value) {
  244. $value['loca_name'] = $key. '@@'. $bucket_datacenter[$value['location']];
  245. $bucket[] = $value;
  246. }
  247. iajax(1, $bucket, '');
  248. }
  249. if($do == 'ftp') {
  250. load()->library('ftp');
  251. $ftp_config = array(
  252. 'hostname' => trim($_GPC['host']),
  253. 'username' => trim($_GPC['username']),
  254. 'password' => strexists($_GPC['password'], '*') ? $_W['setting']['remote_complete_info']['ftp']['password'] : trim($_GPC['password']),
  255. 'port' => intval($_GPC['port']),
  256. 'ssl' => trim($_GPC['ssl']),
  257. 'passive' => trim($_GPC['pasv']),
  258. 'timeout' => intval($_GPC['overtime']),
  259. 'rootdir' => trim($_GPC['dir']),
  260. );
  261. $url = trim($_GPC['url']);
  262. $filename = 'MicroEngine.ico';
  263. $ftp = new Ftp($ftp_config);
  264. if (true === $ftp->connect()) {
  265. if ($ftp->upload(ATTACHMENT_ROOT .'images/global/'. $filename, $filename)) {
  266. load()->func('communication');
  267. $response = ihttp_get($url. '/'. $filename);
  268. if (is_error($response)) {
  269. iajax(-1, '配置失败,FTP远程访问url错误');
  270. }
  271. if (intval($response['code']) != 200) {
  272. iajax(-1, '配置失败,FTP远程访问url错误');
  273. }
  274. $image = getimagesizefromstring($response['content']);
  275. if (!empty($image) && strexists($image['mime'], 'image')) {
  276. iajax(0,'配置成功');
  277. } else {
  278. iajax(-1, '配置失败,FTP远程访问url错误');
  279. }
  280. } else {
  281. iajax(-1, '上传图片失败,请检查配置');
  282. }
  283. } else {
  284. iajax(-1, 'FTP服务器连接失败,请检查配置');
  285. }
  286. }
  287. if ($do == 'oss') {
  288. load()->model('attachment');
  289. $key = $_GPC['key'];
  290. $secret = strexists($_GPC['secret'], '*') ? $_W['setting']['remote_complete_info']['alioss']['secret'] : $_GPC['secret'];
  291. $bucket = $_GPC['bucket'];
  292. $buckets = attachment_alioss_buctkets($key, $secret);
  293. list($bucket, $url) = explode('@@', $_GPC['bucket']);
  294. $result = attachment_newalioss_auth($key, $secret, $bucket, $_GPC['internal']);
  295. if (is_error($result)) {
  296. iajax(-1, 'OSS-Access Key ID 或 OSS-Access Key Secret错误,请重新填写');
  297. }
  298. $ossurl = $buckets[$bucket]['location'].'.aliyuncs.com';
  299. if (!empty($_GPC['url'])) {
  300. if (!strexists($_GPC['url'], 'http://') && !strexists($_GPC['url'],'https://')) {
  301. $url = 'http://'. trim($_GPC['url']);
  302. } else {
  303. $url = trim($_GPC['url']);
  304. }
  305. $url = trim($url, '/').'/';
  306. } else {
  307. $url = 'http://'.$bucket.'.'.$buckets[$bucket]['location'].'.aliyuncs.com/';
  308. }
  309. load()->func('communication');
  310. $filename = 'MicroEngine.ico';
  311. $response = ihttp_request($url. '/'.$filename, array(), array('CURLOPT_REFERER' => $_SERVER['SERVER_NAME']));
  312. if (is_error($response)) {
  313. iajax(-1, '配置失败,阿里云访问url错误');
  314. }
  315. if (intval($response['code']) != 200) {
  316. iajax(-1, '配置失败,阿里云访问url错误,请保证bucket为公共读取的');
  317. }
  318. $image = getimagesizefromstring($response['content']);
  319. if (!empty($image) && strexists($image['mime'], 'image')) {
  320. iajax(0,'配置成功');
  321. } else {
  322. iajax(-1, '配置失败,阿里云访问url错误');
  323. }
  324. }
  325. if ($do == 'qiniu') {
  326. load()->model('attachment');
  327. $_GPC['secretkey'] = strexists($_GPC['secretkey'], '*') ? $_W['setting']['remote_complete_info']['qiniu']['secretkey'] : $_GPC['secretkey'];
  328. $auth= attachment_qiniu_auth(trim($_GPC['accesskey']), trim($_GPC['secretkey']), trim($_GPC['bucket']));
  329. if (is_error($auth)) {
  330. iajax(-1, '配置失败,请检查配置。注:请检查存储区域是否选择的是和bucket对应<br/>的区域', '');
  331. }
  332. load()->func('communication');
  333. $url = $_GPC['url'];
  334. $url = strexists($url, 'http') ? trim($url, '/') : 'http://'.trim($url, '/');
  335. $filename = 'MicroEngine.ico';
  336. $response = ihttp_request($url. '/'.$filename, array(), array('CURLOPT_REFERER' => $_SERVER['SERVER_NAME']));
  337. if (is_error($response)) {
  338. iajax(-1, '配置失败,七牛访问url错误');
  339. }
  340. if (intval($response['code']) != 200) {
  341. iajax(-1, '配置失败,七牛访问url错误,请保证bucket为公共读取的');
  342. }
  343. $image = getimagesizefromstring($response['content']);
  344. if (!empty($image) && strexists($image['mime'], 'image')) {
  345. iajax(0,'配置成功');
  346. } else {
  347. iajax(-1, '配置失败,七牛访问url错误');
  348. }
  349. }
  350. if ($do == 'cos') {
  351. load()->model('attachment');
  352. $url = $_GPC['url'];
  353. $appid = trim($_GPC['appid']);
  354. $secretid = trim($_GPC['secretid']);
  355. $secretkey = strexists($_GPC['secretkey'], '*') ? $_W['setting']['remote_complete_info']['cos']['secretkey'] : trim($_GPC['secretkey']);
  356. $bucket = str_replace("-{$appid}", '', trim($_GPC['bucket']));
  357. if (empty($url)) {
  358. $url = sprintf('https://%s-%s.cos%s.myqcloud.com', $bucket, $appid, $_GPC['local']);
  359. }
  360. $url = rtrim($url, '/');
  361. $auth= attachment_cos_auth($bucket, $appid, $secretid, $secretkey, $_GPC['local']);
  362. if (is_error($auth)) {
  363. iajax(-1, '配置失败,请检查配置' . $auth['message'], '');
  364. }
  365. load()->func('communication');
  366. $filename = 'MicroEngine.ico';
  367. $response = ihttp_request($url. '/'.$filename, array(), array('CURLOPT_REFERER' => $_SERVER['SERVER_NAME']));
  368. if (is_error($response)) {
  369. iajax(-1, '配置失败,腾讯cos访问url错误');
  370. }
  371. if (intval($response['code']) != 200) {
  372. iajax(-1, '配置失败,腾讯cos访问url错误,请保证bucket为公共读取的');
  373. }
  374. $image = getimagesizefromstring($response['content']);
  375. if (!empty($image) && strexists($image['mime'], 'image')) {
  376. iajax(0,'配置成功');
  377. } else {
  378. iajax(-1, '配置失败,腾讯cos访问url错误');
  379. }
  380. }
  381. template('system/attachment');