file.ctrl.php 23 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. 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']) || $ext == 'gif' ? 0 : 1;
  138. $width = intval($setting['width']);
  139. if (isset($option['thumb'])) {
  140. $thumb = empty($option['thumb']) ? 0 : 1;
  141. }
  142. if (isset($option['width']) && ! empty($option['width'])) {
  143. $width = intval($option['width']);
  144. }
  145. if ($thumb == 1 && $width > 0) {
  146. $thumbnail = file_image_thumb($fullname, '', $width);
  147. @unlink($fullname);
  148. if (is_error($thumbnail)) {
  149. $result['message'] = $thumbnail['message'];
  150. die(json_encode($result));
  151. } else {
  152. $filename = pathinfo($thumbnail, PATHINFO_BASENAME);
  153. $pathname = $thumbnail;
  154. $fullname = ATTACHMENT_ROOT . '/' . $pathname;
  155. }
  156. }
  157. }
  158. $info = array(
  159. 'name' => $originname,
  160. 'ext' => $ext,
  161. 'filename' => $pathname,
  162. 'attachment' => $pathname,
  163. 'url' => tomedia($pathname),
  164. 'is_image' => $type == 'image' ? 1 : 0,
  165. 'filesize' => filesize($fullname),
  166. 'group_id' => intval($_GPC['group_id'])
  167. );
  168. if ($type == 'image') {
  169. $size = getimagesize($fullname);
  170. $info['width'] = $size[0];
  171. $info['height'] = $size[1];
  172. } else {
  173. $size = filesize($fullname);
  174. $info['size'] = sizecount($size);
  175. }
  176. $uni_remote_setting = uni_setting_load('remote');
  177. if (empty($option['global']) && !empty($uni_remote_setting['remote']['type'])) {
  178. $_W['setting']['remote'] = $uni_remote_setting['remote'];
  179. }
  180. if (!empty($option['global'])) {
  181. $_W['setting']['remote'] = $_W['setting']['remote_complete_info'];
  182. }
  183. if (!empty($_W['setting']['remote']['type'])) {
  184. $remotestatus = file_remote_upload($pathname);
  185. if (is_error($remotestatus)) {
  186. $result['message'] = '远程附件上传失败,请检查配置并重新上传';
  187. file_delete($pathname);
  188. die(json_encode($result));
  189. } else {
  190. file_delete($pathname);
  191. $info['url'] = tomedia($pathname);
  192. }
  193. }
  194. pdo_insert('core_attachment', array(
  195. 'uniacid' => $uniacid,
  196. 'uid' => $_W['uid'],
  197. 'filename' => safe_gpc_html(htmlspecialchars_decode($originname, ENT_QUOTES)),
  198. 'attachment' => $pathname,
  199. 'type' => $type == 'image' ? 1 : ($type == 'audio'||$type == 'voice' ? 2 : 3),
  200. 'createtime' => TIMESTAMP,
  201. 'module_upload_dir' => $module_upload_dir,
  202. 'group_id' => intval($_GPC['group_id'])
  203. ));
  204. $info['state'] = 'SUCCESS'; die(json_encode($info));
  205. }
  206. if ($do == 'delete') {
  207. if (empty($_W['isfounder']) && $_W['role'] != ACCOUNT_MANAGE_NAME_MANAGER && $_W['role'] != ACCOUNT_MANAGE_NAME_OWNER) {
  208. iajax(1, '您没有权限删除文件');
  209. }
  210. $id = $_GPC['id'];
  211. if (!is_array($id)) {
  212. $id = array(intval($id));
  213. }
  214. $id = safe_gpc_array($id);
  215. $core_attachment_table = table('core_attachment');
  216. $core_attachment_table->searchWithId($id);
  217. if (empty($uniacid)) {
  218. $core_attachment_table->searchWithUid($_W['uid']);
  219. } else {
  220. $core_attachment_table->searchWithUniacid($uniacid);
  221. }
  222. $attachments = $core_attachment_table->getall();
  223. $delete_ids = array();
  224. $uni_remote_setting = uni_setting_load('remote');
  225. if (!empty($uni_remote_setting['remote']['type'])) {
  226. $_W['setting']['remote'] = $uni_remote_setting['remote'];
  227. }
  228. foreach ($attachments as $media) {
  229. if (!empty($_W['setting']['remote']['type'])) {
  230. $status = file_remote_delete($media['attachment']);
  231. } else {
  232. $status = file_delete($media['attachment']);
  233. }
  234. if (is_error($status)) {
  235. iajax(1, $status['message']);
  236. exit;
  237. }
  238. $delete_ids[] = $media['id'];
  239. }
  240. pdo_delete('core_attachment', array('id' => $delete_ids, 'uniacid' => $uniacid));
  241. iajax(0, '删除成功');
  242. }
  243. $limit = array();
  244. $limit['temp'] = array(
  245. 'image' => array(
  246. 'ext' => array('jpg', 'logo'),
  247. 'size' => 1024 * 1024,
  248. 'errmsg' => '临时图片只支持jpg/logo格式,大小不超过为1M',
  249. ),
  250. 'voice' => array(
  251. 'ext' => array('amr', 'mp3'),
  252. 'size' => 2048 * 1024,
  253. 'errmsg' => '临时语音只支持amr/mp3格式,大小不超过为2M',
  254. ),
  255. 'video' => array(
  256. 'ext' => array('mp4'),
  257. 'size' => 10240 * 1024,
  258. 'errmsg' => '临时视频只支持mp4格式,大小不超过为10M',
  259. ),
  260. 'thumb' => array(
  261. 'ext' => array('jpg', 'logo'),
  262. 'size' => 64 * 1024,
  263. 'errmsg' => '临时缩略图只支持jpg/logo格式,大小不超过为64K',
  264. ),
  265. );
  266. $limit['perm'] = array(
  267. 'image' => array(
  268. 'ext' => array('bmp', 'png', 'jpeg', 'jpg', 'gif'),
  269. 'size' => 2048 * 1024,
  270. 'max' => 5000,
  271. 'errmsg' => '永久图片只支持bmp/png/jpeg/jpg/gif格式,大小不超过为2M',
  272. ),
  273. 'voice' => array(
  274. 'ext' => array('amr', 'mp3', 'wma', 'wav', 'amr'),
  275. 'size' => 5120 * 1024,
  276. 'max' => 1000,
  277. 'errmsg' => '永久语音只支持mp3/wma/wav/amr格式,大小不超过为5M,长度不超过60秒',
  278. ),
  279. 'video' => array(
  280. 'ext' => array('rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg', 'mp4'),
  281. 'size' => 10240 * 1024 * 2,
  282. 'max' => 1000,
  283. 'errmsg' => '永久视频只支持rm/rmvb/wmv/avi/mpg/mpeg/mp4格式,大小不超过为20M',
  284. ),
  285. 'thumb' => array(
  286. 'ext' => array('bmp', 'png', 'jpeg', 'jpg', 'gif'),
  287. 'size' => 2048 * 1024,
  288. 'max' => 5000,
  289. 'errmsg' => '永久缩略图只支持bmp/png/jpeg/jpg/gif格式,大小不超过为2M',
  290. ),
  291. );
  292. $limit['file_upload'] = array(
  293. 'image' => array(
  294. 'ext' => array('jpg'),
  295. 'size' => 1024 * 1024,
  296. 'max' => -1,
  297. 'errmsg' => '图片只支持jpg格式,大小不超过为1M',
  298. )
  299. );
  300. if ($do == 'wechat_upload') {
  301. $type = trim($_GPC['upload_type']);
  302. $mode = trim($_GPC['mode']);
  303. if($type == 'image' || $type == 'thumb') {
  304. $type = 'image';
  305. }
  306. if( $type == 'audio') {
  307. $type = 'voice';
  308. }
  309. $setting['folder'] = "{$type}s/{$_W['uniacid']}" . '/'.date('Y/m/');
  310. $acid = $_W['acid'];
  311. if($mode == 'perm') {
  312. $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));
  313. if($now_count >= $limit['perm'][$type]['max']) {
  314. $result['message'] = '文件数量超过限制,请先删除部分文件再上传';
  315. die(json_encode($result));
  316. }
  317. }
  318. if(empty($mode) || empty($type) || !$_W['acid']) {
  319. $result['message'] = '上传配置出错';
  320. die(json_encode($result));
  321. }
  322. if (empty($_FILES['file']['name'])) {
  323. $result['message'] = '上传失败, 请选择要上传的文件!';
  324. die(json_encode($result));
  325. }
  326. if ($_FILES['file']['error'] != 0) {
  327. $result['message'] = '上传失败, 请重试.';
  328. die(json_encode($result));
  329. }
  330. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  331. $ext = strtolower($ext);
  332. $size = intval($_FILES['file']['size']);
  333. $originname = $_FILES['file']['name'];
  334. if(!in_array($ext, $limit[$mode][$type]['ext']) || ($size > $limit[$mode][$type]['size'])) {
  335. $result['message'] = $limit[$mode][$type]['errmsg'];
  336. die(json_encode($result));
  337. }
  338. $filename = file_random_name(ATTACHMENT_ROOT .'/'. $setting['folder'], $ext);
  339. $file = file_wechat_upload($_FILES['file'], $type, $setting['folder'] . $filename, true);
  340. if (is_error($file)) {
  341. $result['message'] = $file['message'];
  342. die(json_encode($result));
  343. }
  344. $pathname = $file['path'];
  345. $fullname = ATTACHMENT_ROOT . '/' . $pathname;
  346. $acc = WeAccount::createByUniacid();
  347. if ($mode == 'perm' || $mode == 'temp') {
  348. if ($type != 'video') {
  349. $result = $acc->uploadMediaFixed($pathname, $type);
  350. } else {
  351. $result = $acc->uploadVideoFixed($originname, $originname, $pathname);
  352. }
  353. }
  354. if($mode == 'perm' || $mode == 'temp') {
  355. if(!empty($content['media_id'])){
  356. $result['media_id'] = $content['media_id'];
  357. }
  358. if(!empty($content['thumb_media_id'])){
  359. $result['media_id'] = $content['thumb_media_id'];
  360. }
  361. } elseif($mode == 'file_upload') {
  362. $result['media_id'] = $content['url'];
  363. }
  364. if ($type == 'image' || $type == 'thumb' ) {
  365. $file['path'] = file_image_thumb($fullname, '', 300);
  366. }
  367. if (!empty($_W['setting']['remote']['type']) && !empty($file['path'])) {
  368. $remotestatus = file_remote_upload($file['path']);
  369. if (is_error($remotestatus)) {
  370. file_delete($pathname);
  371. if($type == 'image' || $type == 'thumb'){
  372. file_delete($file['path']);
  373. }
  374. $result['error'] = 0;
  375. $result['message'] = '远程附件上传失败,请检查配置并重新上传';
  376. die(json_encode($result));
  377. } else {
  378. file_delete($pathname);
  379. if($type == 'image' || $type == 'thumb'){
  380. file_delete($file['path']);
  381. }
  382. }
  383. }
  384. $insert = array(
  385. 'uniacid' => $_W['uniacid'],
  386. 'acid' => $acid,
  387. 'uid' => $_W['uid'],
  388. 'filename' => $originname,
  389. 'attachment' => $file['path'],
  390. 'media_id' => $result['media_id'],
  391. 'type' => $type,
  392. 'model' => $mode,
  393. 'createtime' => TIMESTAMP,
  394. 'module_upload_dir' => $module_upload_dir,
  395. 'group_id' => intval($_GPC['group_id'])
  396. );
  397. if($type == 'image' || $type == 'thumb') {
  398. $size = getimagesize($fullname);
  399. $insert['width'] = $size[0];
  400. $insert['height'] = $size[1];
  401. if($mode == 'perm') {
  402. $insert['tag'] = $content['url'];
  403. }
  404. if(!empty($insert['tag'])) {
  405. $insert['attachment'] = $content['url'];
  406. }
  407. $result['width'] = $size[0];
  408. $result['hieght'] = $size[1];
  409. }
  410. if($type == 'video') {
  411. $insert['tag'] = iserializer(array('title' => $originname, 'url' => ''));
  412. }
  413. pdo_insert('wechat_attachment', $insert);
  414. $result['type'] = $type;
  415. $result['url'] = tomedia($file['path']);
  416. if($type == 'image' || $type == 'thumb') {
  417. @unlink($fullname);
  418. }
  419. $result['mode'] = $mode;
  420. die(json_encode($result));
  421. }
  422. $type = $_GPC['type']; $resourceid = intval($_GPC['resource_id']); $uid = intval($_W['uid']);
  423. $acid = intval($_W['acid']);
  424. $url = $_GPC['url'];
  425. $isnetwork_convert = !empty($url);
  426. $islocal = $_GPC['local'] == 'local';
  427. if ($do == 'keyword') {
  428. $keyword = addslashes($_GPC['keyword']);
  429. $pindex = max(1, $_GPC['page']);
  430. $psize = 24;
  431. $condition = array('uniacid' => $uniacid, 'status' => 1);
  432. if (!empty($keyword)) {
  433. $condition['content like'] = '%'.$keyword.'%';
  434. }
  435. $keyword_lists = pdo_getslice('rule_keyword', $condition, array($pindex, $psize), $total, array(), 'id');
  436. $result = array(
  437. 'items' => $keyword_lists,
  438. 'pager' => pagination($total, $pindex, $psize, '', array('before' => '2', 'after' => '3', 'ajaxcallback' => 'null', 'isajax' => 1)),
  439. );
  440. iajax(0, $result);
  441. }
  442. if ($do == 'module') {
  443. $enable_modules = array();
  444. $is_user_module = isset($_GPC['user_module']) ? intval($_GPC['user_module']) : 0;
  445. $uid = empty($_GPC['uid']) || !is_numeric($_GPC['uid']) ? $_W['uid'] : intval($_GPC['uid']);
  446. $module_uniacid = empty($_GPC['module_uniacid']) || !is_numeric($_GPC['module_uniacid']) ? $_W['uniacid'] : intval($_GPC['module_uniacid']);
  447. $have_cover = $_GPC['cover'] == 'true' ? true : false;
  448. $account_all_type = uni_account_type();
  449. $module_type = in_array($_GPC['mtype'], array_keys(uni_account_type_sign())) ? $_GPC['mtype'] : '';
  450. if ($is_user_module) {
  451. $installedmodulelist = user_modules($uid);
  452. } else {
  453. $installedmodulelist = uni_modules_by_uniacid($module_uniacid);
  454. }
  455. $sysmods = module_system();
  456. foreach ($installedmodulelist as $k => $value) {
  457. if ($value['type'] == 'system' || in_array($value['name'], $sysmods)) {
  458. unset($installedmodulelist[$k]);
  459. continue;
  460. }
  461. $continue = false;
  462. foreach ($account_all_type as $account_type) {
  463. if ($module_type == $account_type['type_sign'] && $value[$account_type['module_support_name']] != $account_type['module_support_value']) {
  464. $continue = true;
  465. break;
  466. }
  467. }
  468. if ($continue) {
  469. unset($installedmodulelist[$k]);
  470. continue;
  471. }
  472. if ($have_cover) {
  473. $module_entries = module_entries($value['name'], array('cover'));
  474. if (empty($module_entries)) {
  475. unset($installedmodulelist[$k]);
  476. continue;
  477. }
  478. }
  479. $installedmodulelist[$k]['official'] = empty($value['issystem']) && (strexists($value['author'], 'WeEngine Team') || strexists($value['author'], '微擎团队'));
  480. }
  481. foreach ($installedmodulelist as $name => $module) {
  482. if ($module['issystem']) {
  483. $path = '/framework/builtin/'.$module['name'];
  484. } else {
  485. $path = '../addons/'.$module['name'];
  486. }
  487. $cion = $path.'/icon-custom.jpg';
  488. if (!file_exists($cion)) {
  489. $cion = $path.'/icon.jpg';
  490. if (!file_exists($cion)) {
  491. $cion = './resource/images/nopic-small.jpg';
  492. }
  493. }
  494. $module['icon'] = $cion;
  495. $enable_modules[] = $module;
  496. }
  497. $result = array('items' => $enable_modules, 'pager' => '');
  498. iajax(0, $result);
  499. }
  500. if ($do == 'video' || $do == 'voice') {
  501. $server = $islocal ? MATERIAL_LOCAL : MATERIAL_WEXIN;
  502. $page_index = max(1, $_GPC['page']);
  503. $page_size = 10;
  504. $material_news_list = material_list($do, $server, array('page_index' => $page_index, 'page_size' => $page_size));
  505. $material_list = $material_news_list['material_list'];
  506. $pager = $material_news_list['page'];
  507. foreach ($material_list as &$item) {
  508. $item['url'] = tomedia($item['attachment']);
  509. unset($item['uid']);
  510. }
  511. $result = array('items' => $material_list, 'pager' => $pager);
  512. iajax(0, $result);
  513. }
  514. if ($do == 'news') {
  515. $server = $islocal ? MATERIAL_LOCAL : MATERIAL_WEXIN;
  516. $page_index = max(1, $_GPC['page']);
  517. $page_size = 24;
  518. $search = addslashes($_GPC['keyword']);
  519. $material_news_list = material_news_list($server, $search, array('page_index' => $page_index, 'page_size' => $page_size));
  520. $material_list = array_values($material_news_list['material_list']);
  521. $pager = $material_news_list['page'];
  522. $result = array('items' => $material_list, 'pager' => $pager);
  523. iajax(0, $result);
  524. }
  525. if ($do == 'image') {
  526. $year = $_GPC['year'];
  527. $month = $_GPC['month'];
  528. $page = intval($_GPC['page']);
  529. $groupid = intval($_GPC['groupid']);
  530. $page_size = 10;
  531. $page = max(1, $page);
  532. $is_local_image = $islocal == 'local' ? true : false;
  533. if ($is_local_image) {
  534. $attachment_table = table('core_attachment');
  535. } else {
  536. $attachment_table = table('wechat_attachment');
  537. }
  538. $attachment_table->searchWithUniacid($uniacid);
  539. $attachment_table->searchWithUploadDir($module_upload_dir);
  540. if (empty($uniacid)) {
  541. $attachment_table->searchWithUid($_W['uid']);
  542. }
  543. if ($groupid > 0) {
  544. $attachment_table->searchWithGroupId($groupid);
  545. }
  546. if ($groupid == 0) {
  547. $attachment_table->searchWithGroupId(-1);
  548. }
  549. if ($year || $month) {
  550. $start_time = strtotime("{$year}-{$month}-01");
  551. $end_time = strtotime('+1 month', $start_time);
  552. $attachment_table->searchWithTime($start_time, $end_time);
  553. }
  554. if ($islocal) {
  555. $attachment_table->searchWithType(ATTACH_TYPE_IMAGE);
  556. } else {
  557. $attachment_table->searchWithType(ATTACHMENT_IMAGE);
  558. }
  559. $attachment_table->searchWithPage($page, $page_size);
  560. $list = $attachment_table->orderby('createtime', 'desc')->getall();
  561. $total = $attachment_table->getLastQueryTotal();
  562. if (!empty($list)) {
  563. foreach ($list as &$meterial) {
  564. if ($islocal) {
  565. if (empty($option['global'])) {
  566. $meterial['url'] = tomedia($meterial['attachment']);
  567. } else {
  568. $meterial['url'] = to_global_media($meterial['attachment']);
  569. }
  570. unset($meterial['uid']);
  571. } else {
  572. if(!empty($_W['setting']['remote']['type'])) {
  573. $meterial['attach'] = tomedia($meterial['attachment']);
  574. } else {
  575. $meterial['attach'] = tomedia($meterial['attachment'], true);
  576. }
  577. $meterial['url'] = $meterial['attach'];
  578. }
  579. }
  580. }
  581. $pager = pagination($total, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
  582. $result = array('items' => $list, 'pager' => $pager);
  583. iajax(0, $result);
  584. }
  585. if ($do == 'tolocal' || $do == 'towechat') {
  586. if (!in_array($type, array('news', 'image', 'video', 'voice'))) {
  587. iajax(1, '转换类型不正确');
  588. return;
  589. }
  590. }
  591. if ($do == 'networktolocal') {
  592. $type = $_GPC['type'];
  593. if (!in_array($type,array('image','video'))) {
  594. $type = 'image';
  595. }
  596. $material = material_network_to_local($url, $uniacid, $uid, $type);
  597. if (is_error($material)) {
  598. iajax(1, $material['message']);
  599. return;
  600. }
  601. iajax(0, $material);
  602. }
  603. if ($do == 'tolocal') {
  604. if ($type == 'news') {
  605. $material = material_news_to_local($resourceid); } else {
  606. $material = material_to_local($resourceid, $uniacid, $uid, $type); }
  607. if (is_error($material)) {
  608. iajax(1, $material['message']);
  609. return;
  610. }
  611. iajax(0, $material);
  612. }
  613. if ($do == 'networktowechat') {
  614. $type = $_GPC['type'];
  615. if (!in_array($type,array('image','video'))) {
  616. $type = 'image';
  617. }
  618. $url_host = parse_url($url, PHP_URL_HOST);
  619. $is_ip = preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url_host);
  620. if ($is_ip) {
  621. iajax(1, '网络链接不支持IP地址!');
  622. }
  623. $material = material_network_to_wechat($url, $uniacid, $uid, $acid, $type);
  624. if (is_error($material)) {
  625. iajax(1, $material['message']);
  626. return;
  627. }
  628. iajax(0, $material);
  629. return;
  630. }
  631. if ($do == 'towechat') {
  632. $material = null;
  633. if ($type != 'news') {
  634. $material = material_to_wechat($resourceid, $uniacid, $uid, $acid, $type); } else {
  635. $material = material_local_news_upload($resourceid); if (!is_error($material)) {
  636. $material['items'] = $material['news']; }
  637. }
  638. if (is_error($material)) {
  639. iajax(1, $material['message']);
  640. return;
  641. }
  642. iajax(0, $material);
  643. }
  644. $is_local_image = $islocal == 'local' ? true : false;
  645. if ($do == 'group_list') {
  646. $query = table('core_attachment_group')->where('type', $is_local_image ? 0 : 1);
  647. $query->searchWithUniacidOrUid($uniacid, $_W['uid']);
  648. $list = $query->getall();
  649. iajax(0, $list);
  650. }
  651. if ($do == 'add_group') {
  652. $table = table('core_attachment_group');
  653. $table->fill(array(
  654. 'uid' => $_W['uid'],
  655. 'uniacid'=>$uniacid,
  656. 'name'=>trim($_GPC['name']),
  657. 'type'=>$is_local_image ? 0 : 1
  658. ));
  659. $result = $table->save();
  660. if (is_error($result)) {
  661. iajax($result['errno'], $result['message']);
  662. }
  663. iajax(0, array('id'=>pdo_insertid()));
  664. }
  665. if ($do == 'change_group') {
  666. $table = table('core_attachment_group');
  667. $type = $is_local_image ? 0 : 1;
  668. $name = trim($_GPC['name']);
  669. $id = intval($_GPC['id']);
  670. $table->searchWithUniacidOrUid($uniacid, $_W['uid']);
  671. $updated = $table->where('type', $type)
  672. ->fill('name', $name)
  673. ->where('id', $id)->save();
  674. iajax($updated ? 0 : 1, $updated ? '更新成功' : '更新失败');
  675. }
  676. if ($do == 'del_group') {
  677. $table = table('core_attachment_group');
  678. $type = $is_local_image ? 0 : 1;
  679. $id = intval($_GPC['id']);
  680. $table->searchWithUniacidOrUid($uniacid, $_W['uid']);
  681. $deleted = $table->where('type', $type)->where('id', $id)->delete();
  682. iajax($deleted ? 0 : 1, $deleted ? '删除成功' : '删除失败');
  683. }
  684. if ($do == 'move_to_group') {
  685. $group_id = intval($_GPC['id']);
  686. $ids = $_GPC['keys'];
  687. $ids = safe_gpc_array($ids);
  688. if ($is_local_image) {
  689. $table = table('core_attachment');
  690. } else {
  691. $table = table('wechat_attachment');
  692. }
  693. $updated = $table->where('id', $ids)->where('uniacid', $uniacid)->fill('group_id', $group_id)->save();
  694. iajax($updated ? 0 : 1, $updated ? '更新成功' : '更新失败');
  695. }