file.ctrl.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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()->func('file');
  8. load()->func('communication');
  9. load()->model('account');
  10. load()->model('material');
  11. load()->model('attachment');
  12. load()->model('mc');
  13. if (!in_array($do, array('upload', 'fetch', 'browser', 'delete', 'image' ,'module' ,'video', 'voice', 'news', 'keyword',
  14. 'networktowechat', 'networktolocal', 'towechat', 'tolocal','wechat_upload',
  15. 'group_list', 'add_group', 'change_group', 'del_group', 'move_to_group'))) {
  16. exit('Access Denied');
  17. }
  18. $result = array(
  19. 'error' => 1,
  20. 'message' => '',
  21. 'data' => ''
  22. );
  23. error_reporting(0);
  24. $type = $_GPC['upload_type'];$type = in_array($type, array('image','audio','video')) ? $type : 'image';
  25. $option = array();
  26. $option = array_elements(array('uploadtype', 'global', 'dest_dir'), $_POST);
  27. $option['width'] = intval($option['width']);
  28. $option['global'] = $_GPC['global'];
  29. if (!empty($option['global']) && empty($_W['isfounder'])) {
  30. $result['message'] = '没有向 global 文件夹上传文件的权限.';
  31. die(json_encode($result));
  32. }
  33. $dest_dir = $_GPC['dest_dir'];if (preg_match('/^[a-zA-Z0-9_\/]{0,50}$/', $dest_dir, $out)) {
  34. $dest_dir = trim($dest_dir, '/');
  35. $pieces = explode('/', $dest_dir);
  36. if(count($pieces) > 3){
  37. $dest_dir = '';
  38. }
  39. } else {
  40. $dest_dir = '';
  41. }
  42. $module_upload_dir = '';
  43. if($dest_dir != '') {
  44. $module_upload_dir = sha1($dest_dir);
  45. }
  46. $setting = $_W['setting']['upload'][$type];
  47. $uniacid = intval($_W['uniacid']);
  48. if(isset($_GPC['uniacid'])) {
  49. $requniacid = intval($_GPC['uniacid']);
  50. attachment_reset_uniacid($requniacid);
  51. $uniacid = intval($_W['uniacid']);
  52. }
  53. if (!empty($option['global'])) {
  54. $setting['folder'] = "{$type}s/global/";
  55. if (! empty($dest_dir)) {
  56. $setting['folder'] .= '' . $dest_dir . '/';
  57. }
  58. } else {
  59. $setting['folder'] = "{$type}s/{$uniacid}";
  60. if (empty($dest_dir)) {
  61. $setting['folder'] .= '/' . date('Y/m/');
  62. } else {
  63. $setting['folder'] .= '/' . $dest_dir . '/';
  64. }
  65. }
  66. if ($do == 'fetch') {
  67. $url = trim($_GPC['url']);
  68. $resp = ihttp_get($url);
  69. if (is_error($resp)) {
  70. $result['message'] = '提取文件失败, 错误信息: ' . $resp['message'];
  71. die(json_encode($result));
  72. }
  73. if (intval($resp['code']) != 200) {
  74. $result['message'] = '提取文件失败: 未找到该资源文件.';
  75. die(json_encode($result));
  76. }
  77. $ext = '';
  78. if ($type == 'image') {
  79. switch ($resp['headers']['Content-Type']) {
  80. case 'application/x-jpg':
  81. case 'image/jpeg':
  82. $ext = 'jpg';
  83. break;
  84. case 'image/png':
  85. $ext = 'png';
  86. break;
  87. case 'image/gif':
  88. $ext = 'gif';
  89. break;
  90. default:
  91. $result['message'] = '提取资源失败, 资源文件类型错误.';
  92. die(json_encode($result));
  93. break;
  94. }
  95. } else {
  96. $result['message'] = '提取资源失败, 仅支持图片提取.';
  97. die(json_encode($result));
  98. }
  99. if (intval($resp['headers']['Content-Length']) > $setting['limit'] * 1024) {
  100. $result['message'] = '上传的媒体文件过大(' . sizecount($size) . ' > ' . sizecount($setting['limit'] * 1024);
  101. die(json_encode($result));
  102. }
  103. $originname = pathinfo($url, PATHINFO_BASENAME);
  104. $filename = file_random_name(ATTACHMENT_ROOT . '/' . $setting['folder'], $ext);
  105. $pathname = $setting['folder'] . $filename;
  106. $fullname = ATTACHMENT_ROOT . '/' . $pathname;
  107. if (file_put_contents($fullname, $resp['content']) == false) {
  108. $result['message'] = '提取失败.';
  109. die(json_encode($result));
  110. }
  111. }
  112. if ($do == 'upload') {
  113. if (empty($_FILES['file']['name'])) {
  114. $result['message'] = '上传失败, 请选择要上传的文件!';
  115. die(json_encode($result));
  116. }
  117. if ($_FILES['file']['error'] != 0) {
  118. $result['message'] = '上传失败, 请重试.';
  119. die(json_encode($result));
  120. }
  121. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  122. $ext = strtolower($ext);
  123. $size = intval($_FILES['file']['size']);
  124. $originname = $_FILES['file']['name'];
  125. $filename = file_random_name(ATTACHMENT_ROOT . '/' . $setting['folder'], $ext);
  126. $file = file_upload($_FILES['file'], $type, $setting['folder'] . $filename, true);
  127. if (is_error($file)) {
  128. $result['message'] = $file['message'];
  129. die(json_encode($result));
  130. }
  131. $pathname = $file['path'];
  132. $fullname = ATTACHMENT_ROOT . '/' . $pathname;
  133. }
  134. if ($do == 'fetch' || $do == 'upload') {
  135. if ($type == 'image') {
  136. $thumb = empty($setting['thumb']) ? 0 : 1; $width = intval($setting['width']);
  137. if (isset($option['thumb'])) {
  138. $thumb = empty($option['thumb']) ? 0 : 1;
  139. }
  140. if (isset($option['width']) && ! empty($option['width'])) {
  141. $width = intval($option['width']);
  142. }
  143. if ($thumb == 1 && $width > 0) {
  144. $thumbnail = file_image_thumb($fullname, '', $width);
  145. @unlink($fullname);
  146. if (is_error($thumbnail)) {
  147. $result['message'] = $thumbnail['message'];
  148. die(json_encode($result));
  149. } else {
  150. $filename = pathinfo($thumbnail, PATHINFO_BASENAME);
  151. $pathname = $thumbnail;
  152. $fullname = ATTACHMENT_ROOT . '/' . $pathname;
  153. }
  154. }
  155. }
  156. $info = array(
  157. 'name' => $originname,
  158. 'ext' => $ext,
  159. 'filename' => $pathname,
  160. 'attachment' => $pathname,
  161. 'url' => tomedia($pathname),
  162. 'is_image' => $type == 'image' ? 1 : 0,
  163. 'filesize' => filesize($fullname),
  164. 'group_id' => intval($_GPC['group_id'])
  165. );
  166. if ($type == 'image') {
  167. $size = getimagesize($fullname);
  168. $info['width'] = $size[0];
  169. $info['height'] = $size[1];
  170. } else {
  171. $size = filesize($fullname);
  172. $info['size'] = sizecount($size);
  173. }
  174. if (!empty($_W['setting']['remote'][$_W['uniacid']]['type'])) {
  175. $_W['setting']['remote'] = $_W['setting']['remote'][$_W['uniacid']];
  176. }
  177. if (!empty($_W['setting']['remote']['type'])) {
  178. $remotestatus = file_remote_upload($pathname);
  179. if (is_error($remotestatus)) {
  180. $result['message'] = '远程附件上传失败,请检查配置并重新上传';
  181. file_delete($pathname);
  182. die(json_encode($result));
  183. } else {
  184. file_delete($pathname);
  185. $info['url'] = tomedia($pathname);
  186. }
  187. }
  188. pdo_insert('core_attachment', array(
  189. 'uniacid' => $uniacid,
  190. 'uid' => $_W['uid'],
  191. 'filename' => $originname,
  192. 'attachment' => $pathname,
  193. 'type' => $type == 'image' ? 1 : ($type == 'audio'||$type == 'voice' ? 2 : 3),
  194. 'createtime' => TIMESTAMP,
  195. 'module_upload_dir' => $module_upload_dir,
  196. 'group_id' => intval($_GPC['group_id'])
  197. ));
  198. $info['state'] = 'SUCCESS'; die(json_encode($info));
  199. }
  200. if ($do == 'delete') {
  201. if (empty($_W['isfounder']) && $_W['role'] != ACCOUNT_MANAGE_NAME_MANAGER && $_W['role'] != ACCOUNT_MANAGE_NAME_OWNER) {
  202. iajax(1, '您没有权限删除文件');
  203. }
  204. $id = $_GPC['id'];
  205. if (!is_array($id)) {
  206. $id = array(intval($id));
  207. }
  208. $id = safe_gpc_array($id);
  209. $table = table('attachment')->where('id', $id);
  210. $table->searchWithUniacidOrUid($uniacid, $_W['uid']);
  211. $attachments = $table->getall();
  212. $delete_ids = array();
  213. foreach ($attachments as $media) {
  214. if (!empty($_W['setting']['remote']['type'])) {
  215. $status = file_remote_delete($media['attachment']);
  216. } else {
  217. $status = file_delete($media['attachment']);
  218. }
  219. if (is_error($status)) {
  220. iajax(1, $status['message']);
  221. exit;
  222. }
  223. $delete_ids[] = $media['id'];
  224. }
  225. pdo_delete('core_attachment', array('id' => $delete_ids));
  226. iajax(0, '删除成功');
  227. }
  228. $limit = array();
  229. $limit['temp'] = array(
  230. 'image' => array(
  231. 'ext' => array('jpg', 'logo'),
  232. 'size' => 1024 * 1024,
  233. 'errmsg' => '临时图片只支持jpg/logo格式,大小不超过为1M',
  234. ),
  235. 'voice' => array(
  236. 'ext' => array('amr', 'mp3'),
  237. 'size' => 2048 * 1024,
  238. 'errmsg' => '临时语音只支持amr/mp3格式,大小不超过为2M',
  239. ),
  240. 'video' => array(
  241. 'ext' => array('mp4'),
  242. 'size' => 10240 * 1024,
  243. 'errmsg' => '临时视频只支持mp4格式,大小不超过为10M',
  244. ),
  245. 'thumb' => array(
  246. 'ext' => array('jpg', 'logo'),
  247. 'size' => 64 * 1024,
  248. 'errmsg' => '临时缩略图只支持jpg/logo格式,大小不超过为64K',
  249. ),
  250. );
  251. $limit['perm'] = array(
  252. 'image' => array(
  253. 'ext' => array('bmp', 'png', 'jpeg', 'jpg', 'gif'),
  254. 'size' => 2048 * 1024,
  255. 'max' => 5000,
  256. 'errmsg' => '永久图片只支持bmp/png/jpeg/jpg/gif格式,大小不超过为2M',
  257. ),
  258. 'voice' => array(
  259. 'ext' => array('amr', 'mp3', 'wma', 'wav', 'amr'),
  260. 'size' => 5120 * 1024,
  261. 'max' => 1000,
  262. 'errmsg' => '永久语音只支持mp3/wma/wav/amr格式,大小不超过为5M,长度不超过60秒',
  263. ),
  264. 'video' => array(
  265. 'ext' => array('rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg', 'mp4'),
  266. 'size' => 10240 * 1024 * 2,
  267. 'max' => 1000,
  268. 'errmsg' => '永久视频只支持rm/rmvb/wmv/avi/mpg/mpeg/mp4格式,大小不超过为20M',
  269. ),
  270. 'thumb' => array(
  271. 'ext' => array('bmp', 'png', 'jpeg', 'jpg', 'gif'),
  272. 'size' => 2048 * 1024,
  273. 'max' => 5000,
  274. 'errmsg' => '永久缩略图只支持bmp/png/jpeg/jpg/gif格式,大小不超过为2M',
  275. ),
  276. );
  277. $limit['file_upload'] = array(
  278. 'image' => array(
  279. 'ext' => array('jpg'),
  280. 'size' => 1024 * 1024,
  281. 'max' => -1,
  282. 'errmsg' => '图片只支持jpg格式,大小不超过为1M',
  283. )
  284. );
  285. $apis = array();
  286. $apis['temp'] = array(
  287. 'add' => 'https://api.weixin.qq.com/cgi-bin/media/upload',
  288. 'get' => 'https://api.weixin.qq.com/cgi-bin/media/get',
  289. 'post_key' => 'media'
  290. );
  291. $apis['perm'] = array(
  292. 'add' => 'https://api.weixin.qq.com/cgi-bin/material/add_material',
  293. 'get' => 'https://api.weixin.qq.com/cgi-bin/material/get_material',
  294. 'del' => 'https://api.weixin.qq.com/cgi-bin/material/del_material',
  295. 'count' => 'https://api.weixin.qq.com/cgi-bin/material/get_materialcount',
  296. 'batchget' => 'https://api.weixin.qq.com/cgi-bin/material/batchget_material',
  297. 'post_key' => 'media',
  298. );
  299. $apis['file_upload'] = array(
  300. 'add' => 'https://api.weixin.qq.com/cgi-bin/media/uploadimg',
  301. 'post_key' => 'buffer',
  302. );
  303. if ($do == 'wechat_upload') {
  304. $type = trim($_GPC['upload_type']);
  305. $mode = trim($_GPC['mode']);
  306. if($type == 'image' || $type == 'thumb') {
  307. $type = 'image';
  308. }
  309. if( $type == 'audio') {
  310. $type = 'voice';
  311. }
  312. $setting['folder'] = "{$type}s/{$_W['uniacid']}" . '/'.date('Y/m/');
  313. $acid = $_W['acid'];
  314. if($mode == 'perm') {
  315. $now_count = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('wechat_attachment') . ' WHERE uniacid = :aid AND acid = :acid AND model = :model AND type = :type', array(':aid' => $_W['uniacid'], ':acid' => $acid, ':model' => $mode, ':type' => $type));
  316. if($now_count >= $limit['perm'][$type]['max']) {
  317. $result['message'] = '文件数量超过限制,请先删除部分文件再上传';
  318. die(json_encode($result));
  319. }
  320. }
  321. if(empty($mode) || empty($type) || !$_W['acid']) {
  322. $result['message'] = '上传配置出错';
  323. die(json_encode($result));
  324. }
  325. if (empty($_FILES['file']['name'])) {
  326. $result['message'] = '上传失败, 请选择要上传的文件!';
  327. die(json_encode($result));
  328. }
  329. if ($_FILES['file']['error'] != 0) {
  330. $result['message'] = '上传失败, 请重试.';
  331. die(json_encode($result));
  332. }
  333. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  334. $ext = strtolower($ext);
  335. $size = intval($_FILES['file']['size']);
  336. $originname = $_FILES['file']['name'];
  337. if(!in_array($ext, $limit[$mode][$type]['ext']) || ($size > $limit[$mode][$type]['size'])) {
  338. $result['message'] = $limit[$mode][$type]['errmsg'];
  339. die(json_encode($result));
  340. }
  341. $filename = file_random_name(ATTACHMENT_ROOT .'/'. $setting['folder'], $ext);
  342. $file = file_wechat_upload($_FILES['file'], $type, $setting['folder'] . $filename, true);
  343. if (is_error($file)) {
  344. $result['message'] = $file['message'];
  345. die(json_encode($result));
  346. }
  347. $pathname = $file['path'];
  348. $fullname = ATTACHMENT_ROOT . '/' . $pathname;
  349. $acc = WeAccount::create($acid);
  350. $token = $acc->getAccessToken();
  351. if (is_error($token)) {
  352. $result['message'] = $token['message'];
  353. die(json_encode($result));
  354. }
  355. if($mode == 'perm' || $mode == 'temp') {
  356. $sendapi = $apis[$mode]['add'] . "?access_token={$token}&type={$type}";
  357. $media = '@'.$fullname;
  358. $data = array(
  359. 'media' => $media
  360. );
  361. if($type == 'video') {
  362. $description = array(
  363. 'title' => $filename,
  364. 'introduction' => $filename,
  365. );
  366. $data['description'] = urldecode(json_encode($description));
  367. }
  368. } elseif($mode == 'file_upload') {
  369. $sendapi = $apis[$mode]['add'] . "?access_token={$token}";
  370. $data = array(
  371. 'buffer' => '@'.$fullname
  372. );
  373. $type = 'image';
  374. }
  375. $resp = ihttp_request($sendapi, $data);
  376. if(is_error($resp)) {
  377. $result['error'] = 0;
  378. $result['message'] = $resp['message'];
  379. die(json_encode($result));
  380. }
  381. $content = @json_decode($resp['content'], true);
  382. if(empty($content)) {
  383. $result['error'] = 0;
  384. $result['message'] = "接口调用失败, 元数据: {$resp['meta']}";
  385. die(json_encode($result));
  386. }
  387. if(!empty($content['errcode'])) {
  388. $result['error'] = 0;
  389. $result['message'] = "访问微信接口错误, 错误代码: {$content['errcode']}, 错误信息: {$content['errmsg']},错误详情:{$acc->errorCode($content['errcode'])}";
  390. die(json_encode($result));
  391. }
  392. if($mode == 'perm' || $mode == 'temp') {
  393. if(!empty($content['media_id'])){
  394. $result['media_id'] = $content['media_id'];
  395. }
  396. if(!empty($content['thumb_media_id'])){
  397. $result['media_id'] = $content['thumb_media_id'];
  398. }
  399. } elseif($mode == 'file_upload') {
  400. $result['media_id'] = $content['url'];
  401. }
  402. if ($type == 'image' || $type == 'thumb' ) {
  403. $file['path'] = file_image_thumb($fullname, '', 300);
  404. }
  405. if (!empty($_W['setting']['remote']['type']) && !empty($file['path'])) {
  406. $remotestatus = file_remote_upload($file['path']);
  407. if (is_error($remotestatus)) {
  408. file_delete($pathname);
  409. if($type == 'image' || $type == 'thumb'){
  410. file_delete($file['path']);
  411. }
  412. $result['error'] = 0;
  413. $result['message'] = '远程附件上传失败,请检查配置并重新上传';
  414. die(json_encode($result));
  415. } else {
  416. file_delete($pathname);
  417. if($type == 'image' || $type == 'thumb'){
  418. file_delete($file['path']);
  419. }
  420. }
  421. }
  422. $insert = array(
  423. 'uniacid' => $_W['uniacid'],
  424. 'acid' => $acid,
  425. 'uid' => $_W['uid'],
  426. 'filename' => $originname,
  427. 'attachment' => $file['path'],
  428. 'media_id' => $result['media_id'],
  429. 'type' => $type,
  430. 'model' => $mode,
  431. 'createtime' => TIMESTAMP,
  432. 'module_upload_dir' => $module_upload_dir,
  433. 'group_id' => intval($_GPC['group_id'])
  434. );
  435. if($type == 'image' || $type == 'thumb') {
  436. $size = getimagesize($fullname);
  437. $insert['width'] = $size[0];
  438. $insert['height'] = $size[1];
  439. if($mode == 'perm') {
  440. $insert['tag'] = $content['url'];
  441. }
  442. if(!empty($insert['tag'])) {
  443. $insert['attachment'] = $content['url'];
  444. }
  445. $result['width'] = $size[0];
  446. $result['hieght'] = $size[1];
  447. }
  448. if($type == 'video') {
  449. $insert['tag'] = iserializer($description);
  450. }
  451. pdo_insert('wechat_attachment', $insert);
  452. $result['type'] = $type;
  453. $result['url'] = tomedia($file['path']);
  454. if($type == 'image' || $type == 'thumb') {
  455. @unlink($fullname);
  456. }
  457. if($type == 'video') {
  458. $result['title'] = $description['title'];
  459. $result['introduction'] = $description['introduction'];
  460. }
  461. $result['mode'] = $mode;
  462. die(json_encode($result));
  463. }
  464. $type = $_GPC['type']; $resourceid = intval($_GPC['resource_id']); $uid = intval($_W['uid']);
  465. $acid = intval($_W['acid']);
  466. $url = $_GPC['url'];
  467. $isnetwork_convert = !empty($url);
  468. $islocal = $_GPC['local'] == 'local'; if ($do == 'keyword') {
  469. $keyword = addslashes($_GPC['keyword']);
  470. $pindex = max(1, $_GPC['page']);
  471. $psize = 24;
  472. $condition = array('uniacid' => $uniacid, 'status' => 1);
  473. if (!empty($keyword)) {
  474. $condition['content like'] = '%'.$keyword.'%';
  475. }
  476. $keyword_lists = pdo_getslice('rule_keyword', $condition, array($pindex, $psize), $total, array(), 'id');
  477. $result = array(
  478. 'items' => $keyword_lists,
  479. 'pager' => pagination($total, $pindex, $psize, '', array('before' => '2', 'after' => '3', 'ajaxcallback' => 'null', 'isajax' => 1)),
  480. );
  481. iajax(0, $result);
  482. }
  483. if ($do == 'module') {
  484. $enable_modules = array();
  485. $is_user_module = isset($_GPC['user_module']) ? intval($_GPC['user_module']) : 0;
  486. if ($is_user_module) {
  487. $installedmodulelist = user_modules($_W['uid']);
  488. } else {
  489. $installedmodulelist = uni_modules(false);
  490. }
  491. $sysmods = system_modules();
  492. foreach ($installedmodulelist as $k => $value) {
  493. if ($value['type'] == 'system' || in_array($value['name'], $sysmods)) {
  494. unset($installedmodulelist[$k]);
  495. continue;
  496. }
  497. $installedmodulelist[$k]['official'] = empty($value['issystem']) && (strexists($value['author'], 'WeEngine Team') || strexists($value['author'], ''));
  498. }
  499. foreach ($installedmodulelist as $name => $module) {
  500. if ($module['issystem']) {
  501. $path = '/framework/builtin/'.$module['name'];
  502. } else {
  503. $path = '../addons/'.$module['name'];
  504. }
  505. $cion = $path.'/icon-custom.jpg';
  506. if (!file_exists($cion)) {
  507. $cion = $path.'/icon.jpg';
  508. if (!file_exists($cion)) {
  509. $cion = './resource/images/nopic-small.jpg';
  510. }
  511. }
  512. $module['icon'] = $cion;
  513. if ($module['enabled'] == 1 || $is_user_module) {
  514. $enable_modules[] = $module;
  515. } else {
  516. $unenable_modules[$name] = $module;
  517. }
  518. }
  519. $result = array('items' => $enable_modules, 'pager' => '');
  520. iajax(0, $result);
  521. }
  522. if ($do == 'video' || $do == 'voice') {
  523. $server = $islocal ? MATERIAL_LOCAL : MATERIAL_WEXIN;
  524. $page_index = max(1, $_GPC['page']);
  525. $page_size = 10;
  526. $material_news_list = material_list($do, $server, array('page_index' => $page_index, 'page_size' => $page_size));
  527. $material_list = $material_news_list['material_list'];
  528. $pager = $material_news_list['page'];
  529. foreach ($material_list as &$item) {
  530. $item['url'] = tomedia($item['attachment']);
  531. unset($item['uid']);
  532. }
  533. $result = array('items' => $material_list, 'pager' => $pager);
  534. iajax(0, $result);
  535. }
  536. if ($do == 'news') {
  537. $server = $islocal ? MATERIAL_LOCAL : MATERIAL_WEXIN;
  538. $page_index = max(1, $_GPC['page']);
  539. $page_size = 24;
  540. $search = addslashes($_GPC['keyword']);
  541. $material_news_list = material_news_list($server, $search, array('page_index' => $page_index, 'page_size' => $page_size));
  542. $material_list = array_values($material_news_list['material_list']);
  543. $pager = $material_news_list['page'];
  544. $result = array('items' => $material_list, 'pager' => $pager);
  545. iajax(0, $result);
  546. }
  547. if ($do == 'image') {
  548. $year = $_GPC['year'];
  549. $month = $_GPC['month'];
  550. $page = intval($_GPC['page']);
  551. $groupid = intval($_GPC['groupid']);
  552. $page_size = 24;
  553. $page = max(1, $page);
  554. $is_local_image = $islocal == 'local' ? true : false;
  555. $attachment_table = table('attachment');
  556. $attachment_table = $attachment_table->local($is_local_image);
  557. $attachment_table->searchWithUniacid($uniacid);
  558. $attachment_table->searchWithUploadDir($module_upload_dir);
  559. if (empty($uniacid)) {
  560. $attachment_table->searchWithUid($_W['uid']);
  561. }
  562. if ($groupid >=0) {
  563. $attachment_table->searchWithGroupId($groupid);
  564. }
  565. if ($year || $month) {
  566. $start_time = strtotime("{$year}-{$month}-01");
  567. $end_time = strtotime('+1 month', $start_time);
  568. $attachment_table->searchWithTime($start_time, $end_time);
  569. }
  570. if ($islocal) {
  571. $attachment_table->searchWithType(ATTACH_TYPE_IMAGE);
  572. } else {
  573. $attachment_table->searchWithType(ATTACHMENT_IMAGE);
  574. }
  575. $attachment_table->searchWithPage($page, $page_size);
  576. $list = $attachment_table->searchAttachmentList();
  577. $total = $attachment_table->getLastQueryTotal();
  578. if (!empty($list)) {
  579. foreach ($list as &$meterial) {
  580. if ($islocal) {
  581. $meterial['url'] = tomedia($meterial['attachment']);
  582. unset($meterial['uid']);
  583. } else {
  584. $meterial['attach'] = tomedia($meterial['attachment'], true);
  585. $meterial['url'] = $meterial['attach'];
  586. }
  587. }
  588. }
  589. $pager = pagination($total, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
  590. $result = array('items' => $list, 'pager' => $pager);
  591. iajax(0, $result);
  592. }
  593. if ($do == 'tolocal' || $do == 'towechat') {
  594. if (!in_array($type, array('news', 'image', 'video', 'voice'))) {
  595. iajax(1, '转换类型不正确');
  596. return;
  597. }
  598. }
  599. if ($do == 'networktolocal') {
  600. $type = $_GPC['type'];
  601. if (!in_array($type,array('image','video'))) {
  602. $type = 'image';
  603. }
  604. $material = material_network_to_local($url, $uniacid, $uid, $type);
  605. if (is_error($material)) {
  606. iajax(1, $material['message']);
  607. return;
  608. }
  609. iajax(0, $material);
  610. }
  611. if ($do == 'tolocal') {
  612. if ($type == 'news') {
  613. $material = material_news_to_local($resourceid); } else {
  614. $material = material_to_local($resourceid, $uniacid, $uid, $type); }
  615. if (is_error($material)) {
  616. iajax(1, $material['message']);
  617. return;
  618. }
  619. iajax(0, $material);
  620. }
  621. if ($do == 'networktowechat') {
  622. $type = $_GPC['type'];
  623. if (!in_array($type,array('image','video'))) {
  624. $type = 'image';
  625. }
  626. $material = material_network_to_wechat($url, $uniacid, $uid, $acid, $type); if (is_error($material)) {
  627. iajax(1, $material['message']);
  628. return;
  629. }
  630. iajax(0, $material);
  631. return;
  632. }
  633. if ($do == 'towechat') {
  634. $material = null;
  635. if ($type != 'news') {
  636. $material = material_to_wechat($resourceid, $uniacid, $uid, $acid, $type); } else {
  637. $material = material_local_news_upload($resourceid); if (!is_error($material)) {
  638. $material['items'] = $material['news']; }
  639. }
  640. if (is_error($material)) {
  641. iajax(1, $material['message']);
  642. return;
  643. }
  644. iajax(0, $material);
  645. }
  646. $is_local_image = $islocal == 'local' ? true : false;
  647. if ($do == 'group_list') {
  648. $query = table('attachmentgroup')->where('type', $is_local_image ? 0 : 1);
  649. $query->searchWithUniacidOrUid($uniacid, $_W['uid']);
  650. $list = $query->getall();
  651. iajax(0, $list);
  652. }
  653. if ($do == 'add_group') {
  654. $table = table('attachmentgroup');
  655. $table->fill(array(
  656. 'uid' => $_W['uid'],
  657. 'uniacid'=>$uniacid,
  658. 'name'=>trim($_GPC['name']),
  659. 'type'=>$is_local_image ? 0 : 1
  660. ));
  661. $result = $table->save();
  662. if (is_error($result)) {
  663. iajax($result['errno'], $result['message']);
  664. }
  665. iajax(0, array('id'=>pdo_insertid()));
  666. }
  667. if ($do == 'change_group') {
  668. $table = table('attachmentgroup');
  669. $type = $is_local_image ? 0 : 1;
  670. $name = trim($_GPC['name']);
  671. $id = intval($_GPC['id']);
  672. $table->searchWithUniacidOrUid($uniacid, $_W['uid']);
  673. $updated = $table->where('type', $type)
  674. ->fill('name', $name)
  675. ->where('id', $id)->save();
  676. iajax($updated ? 0 : 1, $updated ? '更新成功' : '更新失败');
  677. }
  678. if ($do == 'del_group') {
  679. $table = table('attachmentgroup');
  680. $type = $is_local_image ? 0 : 1;
  681. $id = intval($_GPC['id']);
  682. $table->searchWithUniacidOrUid($uniacid, $_W['uid']);
  683. $deleted = $table->where('type', $type)->where('id', $id)->delete();
  684. iajax($deleted ? 0 : 1, $deleted ? '删除成功' : '删除失败');
  685. }
  686. if ($do == 'move_to_group') {
  687. $table = table('attachmentgroup');
  688. $group_id = intval($_GPC['id']);
  689. $ids = $_GPC['keys'];
  690. $ids = safe_gpc_array($ids);
  691. $table = table('attachment')->local($is_local_image);
  692. $updated = $table->where('id', $ids)->fill('group_id', $group_id)->save();
  693. iajax($updated ? 0 : 1, $updated ? '更新成功' : '更新失败');
  694. }