material.mod.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. <?php
  2. defined('IN_IA') or exit('Access Denied');
  3. load()->func('file');
  4. function material_sync($material, $exist_material, $type) {
  5. global $_W;
  6. $material = empty($material) ? array() : $material;
  7. foreach ($material as $news) {
  8. $attachid = '';
  9. if (empty($news['media_id']) && empty($news['article_id'])) {
  10. return error('-1', '参数有误');
  11. }
  12. $params = array('uniacid' => $_W['uniacid']);
  13. if (!empty($news['media_id'])) {
  14. $params['media_id'] = $news['media_id'];
  15. } else {
  16. $params['article_id'] = $news['article_id'];
  17. }
  18. $material_exist = pdo_get('wechat_attachment', $params);
  19. if (empty($material_exist)) {
  20. $material_data = array(
  21. 'uniacid' => $_W['uniacid'],
  22. 'acid' => $_W['acid'],
  23. 'media_id' => empty($news['media_id']) ? '' : $news['media_id'],
  24. 'type' => $type,
  25. 'model' => 'perm',
  26. 'createtime' => $news['update_time'],
  27. 'article_id' => empty($news['article_id']) ? '' : $news['article_id'],
  28. 'publish_status' => empty($news['article_id']) ? -1 : 0
  29. );
  30. if ($type == 'image') {
  31. $material_data['filename'] = $news['name'];
  32. $material_data['attachment'] = $news['url'];
  33. }
  34. if ($type == 'voice') {
  35. $material_data['filename'] = $news['name'];
  36. }
  37. if ($type == 'video') {
  38. $material_data['filename'] = $news['name'];
  39. $material_data['tag'] = iserializer(array('title' => $news['name']));
  40. }
  41. pdo_insert('wechat_attachment', $material_data);
  42. $attachid = pdo_insertid();
  43. } else {
  44. if ($type == 'image') {
  45. $material_data = array(
  46. 'createtime' => $news['update_time'],
  47. 'attachment' => $news['url'],
  48. 'filename' => $news['name']
  49. );
  50. pdo_update('wechat_attachment', $material_data, array('uniacid' => $_W['uniacid'], 'media_id' => $news['media_id']));
  51. }
  52. if ($type == 'voice') {
  53. $material_data = array(
  54. 'createtime' => $news['update_time'],
  55. 'filename' => $news['name']
  56. );
  57. pdo_update('wechat_attachment', $material_data, array('uniacid' => $_W['uniacid'], 'media_id' => $news['media_id']));
  58. }
  59. if ($type == 'video') {
  60. $tag = empty($material_exist['tag']) ? array() : iunserializer($material_exist['tag']);
  61. $material_data = array(
  62. 'filename' => $news['name'],
  63. 'createtime' => $news['update_time'],
  64. 'tag' => iserializer(array('title' => $news['name'], 'url' => $tag['url']))
  65. );
  66. pdo_update('wechat_attachment', $material_data, array('uniacid' => $_W['uniacid'], 'media_id' => $news['media_id']));
  67. }
  68. $exist_material[] = $material_exist['id'];
  69. }
  70. if (in_array($type, array('news', 'draft'))) {
  71. $attachid = empty($attachid) ? $material_exist['id'] : $attachid;
  72. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $attachid));
  73. foreach ($news['content']['news_item'] as $key => $new) {
  74. $new_data = array(
  75. 'uniacid' => $_W['uniacid'],
  76. 'attach_id' => $attachid,
  77. 'thumb_media_id' => $new['thumb_media_id'],
  78. 'thumb_url' => $new['thumb_url'],
  79. 'title' => $new['title'],
  80. 'author' => $new['author'],
  81. 'digest' => $new['digest'],
  82. 'content' => $new['content'],
  83. 'content_source_url' => $new['content_source_url'],
  84. 'show_cover_pic' => $new['show_cover_pic'],
  85. 'url' => $new['url'],
  86. 'displayorder' => $key,
  87. 'is_deleted' => empty($new['is_deleted']) ? STATUS_OFF : STATUS_ON
  88. );
  89. pdo_insert('wechat_news', $new_data);
  90. }
  91. $material_data = array(
  92. 'createtime' => $news['update_time'],
  93. 'article_id' => empty($news['article_id']) ? '' : $news['article_id'],
  94. 'publish_status' => empty($news['article_id']) ? -1 : 0
  95. );
  96. pdo_update('wechat_attachment', $material_data, array('id' => $attachid));
  97. }
  98. }
  99. return $exist_material;
  100. }
  101. function material_news_set($data, $attach_id, $type = 'news') {
  102. global $_W;
  103. $attach_id = intval($attach_id);
  104. foreach ($data as $key => $news) {
  105. if (empty($news['title']) ||
  106. (!empty($news['thumb']) && !parse_path($news['thumb'])) ||
  107. (!empty($news['url']) && !parse_path($news['url'])) ||
  108. (!empty($news['content_source_url']) && !parse_path($news['content_source_url']))
  109. ) {
  110. return error('-1', '参数有误');
  111. }
  112. if (!material_url_check($news['content_source_url']) ||
  113. (!empty($news['thumb']) && !material_url_check($news['thumb'])) ||
  114. (!empty($news['url']) && !material_url_check($news['url']))
  115. ) {
  116. return error('-3', '提交链接参数不合法');
  117. }
  118. if (empty($news['digest']) && !empty($news['content'])) {
  119. $content = str_replace(array('<br>', '&nbsp;'), array("\n", ' '), ihtml_entity_decode($news['content']));
  120. $content = strip_tags($content, '<a>');
  121. $news['digest'] = empty($content) ? '' : cutstr($content, 54);
  122. }
  123. $post_news[] = array(
  124. 'id' => intval($news['id']),
  125. 'uniacid' => $_W['uniacid'],
  126. 'thumb_url' => $news['thumb'],
  127. 'title' => addslashes($news['title']),
  128. 'author' => addslashes($news['author']),
  129. 'digest' => addslashes($news['digest']),
  130. 'content' => safe_gpc_html(htmlspecialchars_decode($news['content'], ENT_QUOTES)),
  131. 'url' => empty($news['url']) ? '' : $news['url'],
  132. 'show_cover_pic' => intval($news['show_cover_pic']),
  133. 'displayorder' => intval($key),
  134. 'thumb_media_id' => empty($news['media_id']) ? '' : addslashes($news['media_id']),
  135. 'content_source_url' => $news['content_source_url'],
  136. );
  137. }
  138. if (!empty($attach_id)) {
  139. $wechat_attachment = pdo_get('wechat_attachment', array(
  140. 'id' => $attach_id,
  141. 'uniacid' => $_W['uniacid']
  142. ));
  143. if (empty($wechat_attachment)) {
  144. return error('-2', '编辑素材不存在');
  145. }
  146. $wechat_attachment['model'] = 'local';
  147. pdo_update('wechat_attachment', $wechat_attachment, array('id' => $attach_id, 'uniacid' => $_W['uniacid']));
  148. pdo_delete('wechat_news', array('attach_id' => $attach_id, 'uniacid' => $_W['uniacid']));
  149. foreach ($post_news as $id => $news) {
  150. $news['attach_id'] = $attach_id;
  151. unset($news['id']);
  152. pdo_insert('wechat_news', $news);
  153. }
  154. cache_delete(cache_system_key('material_reply', array('attach_id' => $attach_id)));
  155. } else {
  156. $wechat_attachment = array(
  157. 'uniacid' => $_W['uniacid'],
  158. 'acid' => $_W['acid'],
  159. 'media_id' => '',
  160. 'type' => $type,
  161. 'model' => 'local',
  162. 'createtime' => TIMESTAMP,
  163. 'article_id' => '',
  164. 'publish_status' => -1
  165. );
  166. pdo_insert('wechat_attachment', $wechat_attachment);
  167. $attach_id = pdo_insertid();
  168. foreach ($post_news as $news) {
  169. unset($news['id']);
  170. $news['attach_id'] = $attach_id;
  171. $news['url'] = '';
  172. $news['thumb_media_id'] = '';
  173. if (strexists($news['thumb_url'], 'c=utility&a=wxcode&do=image&attach=')) {
  174. $local_attachment = material_network_image_to_local($news['thumb_url'], $_W['uniacid'], $_W['uid']);
  175. $news['thumb_url'] = $local_attachment['url'];
  176. }
  177. pdo_insert('wechat_news', $news);
  178. }
  179. }
  180. return $attach_id;
  181. }
  182. function material_get($attach_id) {
  183. if (empty($attach_id)) {
  184. return error(1, "素材id参数不能为空");
  185. }
  186. if (is_numeric($attach_id)) {
  187. $material = table('wechat_attachment')->getById($attach_id);
  188. } else {
  189. $media_id = trim($attach_id);
  190. $material = table('wechat_attachment')->getByMediaId($media_id);
  191. }
  192. if (!empty($material)) {
  193. if (in_array($material['type'], array('news', 'draft'))) {
  194. $news = table('wechat_news')->getAllByAttachId($material['id']);
  195. if (!empty($news)) {
  196. foreach ($news as &$news_row) {
  197. $news_row['content_source_url'] = $news_row['content_source_url'];
  198. $news_row['thumb_url'] = tomedia($news_row['thumb_url']);
  199. preg_match_all('/src=[\'\"]?([^\'\"]*)[\'\"]?/i', $news_row['content'], $match);
  200. if (!empty($match[1])) {
  201. foreach ($match[1] as $val) {
  202. if ((strexists($val, 'http://') || strexists($val, 'https://')) && (strexists($val, 'mmbiz.qlogo.cn') || strexists($val, 'mmbiz.qpic.cn'))) {
  203. $news_row['content'] = str_replace($val, tomedia($val), $news_row['content']);
  204. }
  205. }
  206. }
  207. $news_row['content'] = str_replace('data-src', 'src', $news_row['content']);
  208. }
  209. unset($news_row);
  210. } else {
  211. return error('1', '素材不存在');
  212. }
  213. $material['news'] = $news;
  214. } elseif ($material['type'] == 'image') {
  215. $material['url'] = $material['attachment'];
  216. $material['attachment'] = tomedia($material['attachment']);
  217. }
  218. return $material;
  219. } else {
  220. return error('1', "素材不存在");
  221. }
  222. }
  223. function material_build_reply($attach_id) {
  224. if (empty($attach_id)) {
  225. return error(1, "素材id参数不能为空");
  226. }
  227. $cachekey = cache_system_key('material_reply', array('attach_id' => $attach_id));
  228. $reply = cache_load($cachekey);
  229. if (!empty($reply)) {
  230. return $reply;
  231. }
  232. $reply_material = material_get($attach_id);
  233. $reply = array();
  234. if (in_array($reply_material['type'], array('news', 'draft'))) {
  235. if (!empty($reply_material['news'])) {
  236. foreach ($reply_material['news'] as $material) {
  237. $reply[] = array(
  238. 'title' => $material['title'],
  239. 'description' => $material['digest'],
  240. 'picurl' => $material['thumb_url'],
  241. 'url' => !empty($material['url']) ? $material['url'] : $material['content_source_url'],
  242. );
  243. }
  244. }
  245. }
  246. cache_write($cachekey, $reply, CACHE_EXPIRE_MIDDLE);
  247. return $reply;
  248. }
  249. function material_strip_wechat_image_proxy($content) {
  250. global $_W;
  251. $match_wechat = array();
  252. $content = htmlspecialchars_decode($content);
  253. preg_match_all('/<img.*src=[\'"](.*)[\'"].*\/?>/iU', $content, $match_wechat);
  254. if (!empty($match_wechat[1])) {
  255. foreach ($match_wechat[1] as $val) {
  256. $wechat_thumb_url = urldecode(str_replace($_W['siteroot'] . 'web/index.php?c=utility&a=wxcode&do=image&attach=', '', $val));
  257. $content = str_replace($val, $wechat_thumb_url, $content);
  258. }
  259. }
  260. return $content;
  261. }
  262. function material_get_image_url($content) {
  263. global $_W;
  264. $content = htmlspecialchars_decode($content);
  265. $match = array();
  266. $images = array();
  267. preg_match_all('/<img.*src=[\'"](.*\.(?:png|jpg|jpeg|jpe|gif))[\'"].*\/?>/iU', $content, $match);
  268. if (!empty($match[1])) {
  269. foreach ($match[1] as $val) {
  270. if ((strexists($val, 'http://') || strexists($val, 'https://')) && !strexists($val, 'mmbiz.qlogo.cn') && !strexists($val, 'mmbiz.qpic.cn')) {
  271. $images[] = $val;
  272. } else {
  273. if (strexists($val, './attachment/images/')) {
  274. $images[] = tomedia($val);
  275. }
  276. }
  277. }
  278. }
  279. return $images;
  280. }
  281. function material_parse_content($content) {
  282. global $_W;
  283. $content = material_strip_wechat_image_proxy($content);
  284. $images = material_get_image_url($content);
  285. if (!empty($images)) {
  286. foreach ($images as $image) {
  287. $thumb = file_remote_attach_fetch(tomedia($image), 1024, 'material/images');
  288. if (is_error($thumb)) {
  289. return $thumb;
  290. }
  291. $thumb = ATTACHMENT_ROOT . $thumb;
  292. $account_api = WeAccount::createByUniacid();
  293. $result = $account_api->uploadNewsThumb($thumb);
  294. if (is_error($result)) {
  295. return $result;
  296. } else {
  297. $content = str_replace($image, $result, $content);
  298. }
  299. }
  300. }
  301. return $content;
  302. }
  303. function material_local_news_upload($attach_id, $type = 'news') {
  304. global $_W;
  305. $account_api = WeAccount::createByUniacid();
  306. $material = material_get($attach_id);
  307. if (is_error($material)) {
  308. return error('-1', '获取素材文件失败');
  309. }
  310. $change_media_id = 0;
  311. foreach ($material['news'] as $news) {
  312. if (empty($news['content'])) {
  313. return error('-6', '素材内容不能为空');
  314. }
  315. $news['content'] = material_parse_content($news['content']);
  316. if (!empty($news['content_source_url'])) {
  317. $content_source_url = safe_gpc_url($news['content_source_url'], false, $_W['siteroot'] . 'app/' . $news['content_source_url']);
  318. $news['content_source_url'] = (strexists($content_source_url, 'http://') || strexists($content_source_url, 'https://')) ? $content_source_url : $_W['siteroot'] . 'app/' . $content_source_url;
  319. }
  320. if (is_error($news['content'])) {
  321. return error('-2', $news['content']['message']);
  322. }
  323. if (empty($news['thumb_media_id'])) {
  324. if (empty($news['thumb_url'])) {
  325. return error('-7', '图文封面不能为空');
  326. } else {
  327. $result = material_local_upload_by_url($news['thumb_url']);
  328. if (is_error($result)) {
  329. return error('-3', $result['message']);
  330. }
  331. $news['thumb_media_id'] = $result['media_id'];
  332. $news['thumb_url'] = $result['url'];
  333. }
  334. }
  335. pdo_update('wechat_news', $news, array('id' => $news['id']));
  336. $articles['articles'][] = $news;
  337. if (!empty($material['media_id'])) {
  338. $edit_attachment['media_id'] = $material['media_id'];
  339. $edit_attachment['index'] = $news['displayorder'];
  340. $edit_attachment['articles'] = $news;
  341. $result = $account_api->editMaterialNews($edit_attachment);
  342. if (is_error($result)) {
  343. if ($result['errno'] == 40114) {
  344. $change_media_id = $material['media_id'];
  345. break;
  346. } else {
  347. return error('-4', $result['message']);
  348. }
  349. }
  350. }
  351. }
  352. if (empty($material['media_id']) || $change_media_id) {
  353. $media_id = $account_api->addMatrialNews($articles);
  354. if (is_error($media_id)) {
  355. return error('-5', $media_id, '');
  356. }
  357. if ('news' == $type) {
  358. $material_info = $account_api->getMaterial($media_id, false);
  359. } else {
  360. $material_info = $account_api->getMaterialDraft($media_id, false);
  361. }
  362. if (!empty($material_info['news_item'])) {
  363. foreach ($material_info['news_item'] as $key => $info) {
  364. pdo_update('wechat_news', array('url' => $info['url']), array('uniacid' => $_W['uniacid'], 'attach_id' => $material['id'], 'displayorder' => $key));
  365. }
  366. }
  367. pdo_update('wechat_attachment', array(
  368. 'media_id' => $media_id,
  369. 'model' => 'perm'
  370. ), array(
  371. 'uniacid' => $_W['uniacid'],
  372. 'id' => $attach_id
  373. ));
  374. if ($change_media_id) {
  375. if ('news' == $type) {
  376. $account_api->delMaterial($change_media_id);
  377. } else {
  378. $account_api->delMaterialDraft($change_media_id);
  379. }
  380. }
  381. } else {
  382. pdo_update('wechat_attachment', array('model' => 'perm'), array('uniacid' => $_W['uniacid'], 'id' => $attach_id));
  383. }
  384. return $material;
  385. }
  386. function material_local_upload_by_url($url, $type = 'images') {
  387. global $_W;
  388. $account_api = WeAccount::createByUniacid();
  389. if (! empty($_W['setting']['remote']['type'])) {
  390. $remote_file_url = tomedia($url);
  391. $filepath = file_remote_attach_fetch($remote_file_url, 0, '');
  392. if (is_error($filepath)) {
  393. return $filepath;
  394. }
  395. $filepath = ATTACHMENT_ROOT . $filepath;
  396. } else {
  397. if (strexists(parse_url($url, PHP_URL_PATH), '/attachment/')) {
  398. $url = substr(parse_url($url, PHP_URL_PATH), strpos(parse_url($url, PHP_URL_PATH), '/attachment/') + strlen('/attachment/'));
  399. }
  400. $filepath = ATTACHMENT_ROOT . $url;
  401. }
  402. $filesize = filesize($filepath);
  403. $filesize = sizecount($filesize, true);
  404. if ($filesize > 10 && $type == 'videos') {
  405. return error(-1, '要转换的微信素材视频不能超过10M');
  406. }
  407. return $account_api->uploadMediaFixed($filepath, $type);
  408. }
  409. function material_local_upload($material_id) {
  410. global $_W;
  411. $type_arr = array('1' => 'images', '2' => 'voices', '3' => 'videos');
  412. $material = pdo_get('core_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
  413. if (empty($material)) {
  414. return error('-1', '同步素材不存在或已删除');
  415. }
  416. return material_local_upload_by_url($material['attachment'], $type_arr[$material['type']]);
  417. }
  418. function material_upload_limit() {
  419. global $_W;
  420. $default = 5 * 1024 * 1024;
  421. $upload_limit = array(
  422. 'num' => '30',
  423. 'image' => $default,
  424. 'voice' => $default,
  425. 'video' => $default
  426. );
  427. $upload = $_W['setting']['upload'];
  428. if (isset($upload['image']['limit']) && (bytecount($upload['image']['limit'] . 'kb') > 0)) {
  429. $upload_limit['image'] = bytecount($upload['image']['limit'] . 'kb');
  430. }
  431. if (isset($upload['image']['limit']) && (bytecount($upload['audio']['limit'] . 'kb') > 0)) {
  432. $upload_limit['voice'] = $upload_limit['video'] = bytecount($upload['audio']['limit'] . 'kb');
  433. }
  434. return $upload_limit;
  435. }
  436. function material_news_delete($material_id, $type = 'news') {
  437. global $_W;
  438. $permission = permission_account_user_menu($_W['uid'], $_W['uniacid'], 'system');
  439. if (is_error($permission)) {
  440. return error(-1, $permission['message']);
  441. }
  442. if (empty($_W['isfounder']) && !empty($permission) && !in_array('platform_material', $permission) && !in_array('all', $permission)) {
  443. return error('-1', '您没有权限删除该文件');
  444. }
  445. $material_id = intval($material_id);
  446. $material = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
  447. if (empty($material)) {
  448. return error('-2', '素材文件不存在或已删除');
  449. }
  450. if (!empty($material['media_id'])) {
  451. $account_api = WeAccount::createByUniacid();
  452. if ('news' == $type) {
  453. $result = $account_api->delMaterial($material['media_id']);
  454. } else {
  455. $result = $account_api->delMaterialDraft($material['media_id']);
  456. }
  457. }
  458. if (is_error($result)) {
  459. return $result;
  460. }
  461. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $material_id));
  462. pdo_delete('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
  463. return $result;
  464. }
  465. function material_delete($material_id, $location) {
  466. global $_W;
  467. if (empty($_W['isfounder']) && !permission_check_account_user('platform_material_delete')) {
  468. return error('-1', '您没有权限删除该文件');
  469. }
  470. $material_id = intval($material_id);
  471. $table = $location == 'wechat' ? 'wechat_attachment' : 'core_attachment';
  472. $material = pdo_get($table, array('id' => $material_id));
  473. if (empty($material)) {
  474. return error('-2', '素材文件不存在或已删除');
  475. }
  476. if ($location == 'wechat' && !empty($material['media_id'])) {
  477. $account_api = WeAccount::createByUniacid();
  478. $result = $account_api->delMaterial($material['media_id']);
  479. } else {
  480. if (!empty($material['uniacid'])) {
  481. $role = permission_account_user_role($_W['uid'], $material['uniacid']);
  482. if (in_array($role, array(ACCOUNT_MANAGE_NAME_OPERATOR, ACCOUNT_MANAGE_NAME_MANAGER)) && $_W['uid'] != $material['uid']) {
  483. return error('-1', '您没有权限删除该文件');
  484. }
  485. } elseif ($_W['uid'] != $material['uid']) {
  486. return error('-1', '您没有权限删除该文件');
  487. }
  488. }
  489. if (!empty($_W['setting']['remote']['type'])) {
  490. $result = file_remote_delete($material['attachment']);
  491. if (file_exists(IA_ROOT . '/' . $_W['config']['upload']['attachdir'] . '/' . $material['attachment'])) {
  492. $result = file_delete($material['attachment']);
  493. }
  494. } else {
  495. $result = file_delete($material['attachment']);
  496. }
  497. if (is_error($result)) {
  498. return error('-3', '删除文件操作发生错误');
  499. }
  500. pdo_delete($table, array('id' => $material_id, 'uniacid' => $_W['uniacid']));
  501. return $result;
  502. }
  503. function material_url_check($url) {
  504. if (empty($url)) {
  505. return true;
  506. } else {
  507. $pattern = "/^((https|http|tel):\/\/|\.\/index.php)[^\s]+/i";
  508. return preg_match($pattern, trim($url));
  509. }
  510. }
  511. function material_news_list($server = '', $search = '', $page = array('page_index' => 1, 'page_size' => 24), $type = 'news') {
  512. global $_W;
  513. $wechat_news_table = table('wechat_news');
  514. $wechat_attachment_table = table('wechat_attachment');
  515. $material_list = array();
  516. $total = 0;
  517. $type = in_array($type, array('news', 'draft', 'publish')) ? $type : 'news';
  518. if (empty($search)) {
  519. $wechat_attachment_table->searchWithUniacid($_W['uniacid']);
  520. if ('publish' == $type) {
  521. $wechat_attachment_table->searchWithArticleId(array('article_id !=' => ''));
  522. $type = 'draft';
  523. } else {
  524. $wechat_attachment_table->searchWithArticleId(array('article_id' => ''));
  525. }
  526. $wechat_attachment_table->searchWithType($type);
  527. if (!empty($server) && in_array($server, array('local', 'perm'))) {
  528. $wechat_attachment_table->searchWithModel($server);
  529. }
  530. $wechat_attachment_table->searchWithPage($page['page_index'], $page['page_size']);
  531. $news_list = $wechat_attachment_table->orderby('createtime DESC')->getall();
  532. $total = $wechat_attachment_table->getLastQueryTotal();
  533. if (! empty($news_list)) {
  534. foreach ($news_list as $news) {
  535. $news['items'] = $wechat_news_table->getAllByAttachId($news['id']);
  536. $material_list[$news['id']] = $news;
  537. }
  538. }
  539. } else {
  540. $wechat_news_table->searchKeyword("%$search%");
  541. $wechat_news_table->searchWithUniacid($_W['uniacid']);
  542. $search_attach_id = $wechat_news_table->getall();
  543. if (!empty($search_attach_id)) {
  544. foreach ($search_attach_id as $news) {
  545. if (isset($material_list[$news['attach_id']]) && !empty($material_list[$news['attach_id']])) {
  546. continue;
  547. }
  548. $wechat_attachment = $wechat_attachment_table->getById($news['attach_id']);
  549. if (empty($wechat_attachment)) {
  550. continue;
  551. }
  552. if ('publish' == $type) {
  553. if ('draft' != $wechat_attachment['type'] || empty($wechat_attachment['article_id'])) {
  554. continue;
  555. }
  556. } else {
  557. if ($wechat_attachment['type'] != $type || !empty($wechat_attachment['article_id'])) {
  558. continue;
  559. }
  560. }
  561. $material_list[$news['attach_id']] = $wechat_attachment;
  562. $material_list[$news['attach_id']]['items'] = $wechat_news_table->getAllByAttachId($news['attach_id']);
  563. }
  564. }
  565. }
  566. foreach ($material_list as $key => &$news) {
  567. if (isset($news['items']) && is_array($news['items'])) {
  568. if (empty($news['items'][0])) {
  569. $news['items'] = array_values($news['items']);
  570. }
  571. foreach ($news['items'] as &$item) {
  572. $item['digest'] = htmlspecialchars($item['digest']);
  573. $item['thumb_url'] = tomedia($item['thumb_url']);
  574. }
  575. }
  576. }
  577. unset($news_list);
  578. $pager = pagination($total, $page['page_index'], $page['page_size'], '', $context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
  579. $material_news = array('material_list' => $material_list, 'page' => $pager);
  580. return $material_news;
  581. }
  582. function material_list($type = '', $server = '', $page = array('page_index' => 1, 'page_size' => 24)) {
  583. global $_W;
  584. $tables = array(MATERIAL_LOCAL => 'core_attachment', MATERIAL_WEXIN => 'wechat_attachment');
  585. $conditions['uniacid'] = $_W['uniacid'];
  586. $table = $tables[$server];
  587. switch ($type) {
  588. case 'voice':
  589. $conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_VOICE : 'voice';
  590. break;
  591. case 'video':
  592. $conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_VEDIO : 'video';
  593. break;
  594. default:
  595. $conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_IMAGE : 'image';
  596. break;
  597. }
  598. if (!empty($_W['setting']['upload']['attachment_by_uid']) && MATERIAL_LOCAL == $server && in_array($_W['role'], array(ACCOUNT_MANAGE_NAME_CLERK, ACCOUNT_MANAGE_NAME_OPERATOR, ACCOUNT_MANAGE_NAME_MANAGER))) {
  599. $conditions['uid'] = $_W['uid'];
  600. }
  601. $order = 'createtime DESC';
  602. if (!empty($page['conditions']) && is_array($page['conditions'])) {
  603. $conditions = array_merge($conditions, $page['conditions']);
  604. }
  605. if (!empty($page['order'])) {
  606. $order = $page['order'];
  607. }
  608. if ($server == 'local') {
  609. $material_list = pdo_getslice($table, $conditions, array($page['page_index'], $page['page_size']), $total, array(), '', $order);
  610. } else {
  611. $conditions['model'] = MATERIAL_WEXIN;
  612. $material_list = pdo_getslice($table, $conditions, array($page['page_index'], $page['page_size']), $total, array(), '', $order);
  613. if ($type == 'video') {
  614. foreach ($material_list as &$row) {
  615. $row['tag'] = $row['tag'] == '' ? array() : iunserializer($row['tag']);
  616. if (empty($row['filename'])) {
  617. $row['filename'] = $row['tag']['title'];
  618. }
  619. }
  620. unset($row);
  621. }
  622. }
  623. $pager = pagination($total, $page['page_index'], $page['page_size'], '', $context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
  624. $material_news = array('material_list' => $material_list, 'page' => $pager);
  625. return $material_news;
  626. }
  627. function material_news_to_local($attach_id) {
  628. $material = material_get($attach_id);
  629. if (is_error($material)) {
  630. return $material;
  631. }
  632. $attach_id = material_news_set($material['news'], $attach_id);
  633. if (is_error($attach_id)) {
  634. return $attach_id;
  635. }
  636. $material['items'] = $material['news'];
  637. return $material;
  638. }
  639. function material_to_local($resourceid, $uniacid, $uid, $type = 'image') {
  640. $material = material_get($resourceid);
  641. if (is_error($material)) {
  642. return $material;
  643. }
  644. return material_network_image_to_local($material['attachment'], $uniacid, $uid);
  645. }
  646. function material_network_image_to_local($url, $uniacid, $uid) {
  647. return material_network_to_local($url, $uniacid, $uid, 'image');
  648. }
  649. function material_network_to_local($url, $uniacid, $uid, $type = 'image') {
  650. global $_W;
  651. $path = file_remote_attach_fetch($url);
  652. if (is_error($path)) {
  653. return $path;
  654. }
  655. if (!empty($_W['setting']['remote']['type'])) {
  656. $remotestatus = file_remote_upload($path);
  657. if (is_error($remotestatus)) {
  658. return $remotestatus;
  659. } else {
  660. file_delete($path);
  661. }
  662. }
  663. $filename = pathinfo($path, PATHINFO_FILENAME);
  664. $data = array('uniacid' => $uniacid, 'uid' => $uid,
  665. 'filename' => $filename,
  666. 'attachment' => $path,
  667. 'type' => $type == 'image' ? ATTACH_TYPE_IMAGE : ($type == 'audio' || $type == 'voice' ? ATTACH_TYPE_VOICE : ATTACH_TYPE_VEDIO),
  668. 'createtime' => TIMESTAMP
  669. );
  670. pdo_insert('core_attachment', $data);
  671. $id = pdo_insertid();
  672. $data['id'] = $id;
  673. $data['url'] = tomedia($path);
  674. return $data;
  675. }
  676. function material_to_wechat($attach_id, $uniacid, $uid, $acid, $type = 'image') {
  677. $result = material_local_upload($attach_id);
  678. if (is_error($result)) {
  679. return $result;
  680. }
  681. $tag = $result['url'];
  682. if ($type == 'video') {
  683. $tag = serialize(array('title' => '网络视频', 'description' => '网络视频'));
  684. }
  685. $data = array('uniacid' => $uniacid, 'uid' => $uid, 'acid' => $acid,
  686. 'media_id' => $result['media_id'],
  687. 'attachment' => $result['url'],
  688. 'type' => $type,
  689. 'tag' => $tag,
  690. 'model' => 'perm',
  691. 'createtime' => TIMESTAMP
  692. );
  693. pdo_insert('wechat_attachment', $data);
  694. $id = pdo_insertid();
  695. $data['url'] = tomedia($result['url']);
  696. $data['id'] = $id;
  697. return $data;
  698. }
  699. function material_network_image_to_wechat($url, $uniacid, $uid, $acid) {
  700. return material_network_to_wechat($url, $uniacid, $uid, $acid, 'image');
  701. }
  702. function material_network_to_wechat($url, $uniacid, $uid, $acid, $type = 'image') {
  703. $local = material_network_to_local($url, $uniacid, $uid, $type);
  704. if (is_error($local)) {
  705. return $local;
  706. }
  707. return material_to_wechat($local['id'], $uniacid, $uid, $acid, $type);
  708. }