material.mod.php 22 KB

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