人人商城

file.ctrl.php 22KB

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