material.mod.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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('id' => $attach_id, 'uniacid' => $_W['uniacid']));
  121. pdo_delete('wechat_news', array('attach_id' => $attach_id, 'uniacid' => $_W['uniacid']));
  122. foreach ($post_news as $id => $news) {
  123. $news['attach_id'] = $attach_id;
  124. unset($news['id']);
  125. pdo_insert('wechat_news', $news);
  126. }
  127. cache_delete(cache_system_key('material_reply', array('attach_id' => $attach_id)));
  128. } else {
  129. $wechat_attachment = array(
  130. 'uniacid' => $_W['uniacid'],
  131. 'acid' => $_W['acid'],
  132. 'media_id' => '',
  133. 'type' => 'news',
  134. 'model' => 'local',
  135. 'createtime' => TIMESTAMP
  136. );
  137. pdo_insert('wechat_attachment', $wechat_attachment);
  138. $attach_id = pdo_insertid();
  139. foreach ($post_news as $key => $news) {
  140. $news['attach_id'] = $attach_id;
  141. pdo_insert('wechat_news', $news);
  142. }
  143. }
  144. return $attach_id;
  145. }
  146. function material_get($attach_id) {
  147. if (empty($attach_id)) {
  148. return error(1, "素材id参数不能为空");
  149. }
  150. if (is_numeric($attach_id)) {
  151. $material = table('wechat_attachment')->getById($attach_id);
  152. } else {
  153. $media_id = trim($attach_id);
  154. $material = table('wechat_attachment')->getByMediaId($media_id);
  155. }
  156. if (!empty($material)) {
  157. if ($material['type'] == 'news') {
  158. $news = table('wechat_news')->getAllByAttachId($material['id']);
  159. if (!empty($news)) {
  160. foreach ($news as &$news_row) {
  161. $news_row['content_source_url'] = $news_row['content_source_url'];
  162. $news_row['thumb_url'] = tomedia($news_row['thumb_url']);
  163. preg_match_all('/src=[\'\"]?([^\'\"]*)[\'\"]?/i', $news_row['content'], $match);
  164. if (!empty($match[1])) {
  165. foreach ($match[1] as $val) {
  166. if ((strexists($val, 'http://') || strexists($val, 'https://')) && (strexists($val, 'mmbiz.qlogo.cn') || strexists($val, 'mmbiz.qpic.cn'))) {
  167. $news_row['content'] = str_replace($val, tomedia($val), $news_row['content']);
  168. }
  169. }
  170. }
  171. $news_row['content'] = str_replace('data-src', 'src', $news_row['content']);
  172. }
  173. unset($news_row);
  174. } else {
  175. return error('1', '素材不存在');
  176. }
  177. $material['news'] = $news;
  178. } elseif ($material['type'] == 'image') {
  179. $material['url'] = $material['attachment'];
  180. $material['attachment'] = tomedia($material['attachment']);
  181. }
  182. return $material;
  183. } else {
  184. return error('1', "素材不存在");
  185. }
  186. }
  187. function material_build_reply($attach_id) {
  188. if (empty($attach_id)) {
  189. return error(1, "素材id参数不能为空");
  190. }
  191. $cachekey = cache_system_key('material_reply', array('attach_id' => $attach_id));
  192. $reply = cache_load($cachekey);
  193. if (!empty($reply)) {
  194. return $reply;
  195. }
  196. $reply_material = material_get($attach_id);
  197. $reply = array();
  198. if ($reply_material['type'] == 'news') {
  199. if (!empty($reply_material['news'])) {
  200. foreach ($reply_material['news'] as $material) {
  201. $reply[] = array(
  202. 'title' => $material['title'],
  203. 'description' => $material['digest'],
  204. 'picurl' => $material['thumb_url'],
  205. 'url' => !empty($material['content_source_url']) ? $material['content_source_url'] : $material['url'],
  206. );
  207. }
  208. }
  209. }
  210. cache_write($cachekey, $reply, CACHE_EXPIRE_MIDDLE);
  211. return $reply;
  212. }
  213. function material_strip_wechat_image_proxy($content) {
  214. global $_W;
  215. $match_wechat = array();
  216. $content = htmlspecialchars_decode($content);
  217. preg_match_all ('/<img.*src=[\'"](.*)[\'"].*\/?>/iU', $content, $match_wechat);
  218. if (!empty($match_wechat[1])) {
  219. foreach ($match_wechat[1] as $val) {
  220. $wechat_thumb_url = urldecode(str_replace($_W['siteroot'] . 'web/index.php?c=utility&a=wxcode&do=image&attach=', '', $val));
  221. $content = str_replace($val, $wechat_thumb_url, $content);
  222. }
  223. }
  224. return $content;
  225. }
  226. function material_get_image_url($content) {
  227. global $_W;
  228. $content = htmlspecialchars_decode ($content);
  229. $match = array ();
  230. $images = array ();
  231. preg_match_all ('/<img.*src=[\'"](.*\.(?:png|jpg|jpeg|jpe|gif))[\'"].*\/?>/iU', $content, $match);
  232. if (!empty($match[1])) {
  233. foreach ($match[1] as $val) {
  234. if ((strexists ($val, 'http://') || strexists ($val, 'https://')) && !strexists ($val, 'mmbiz.qlogo.cn') && !strexists ($val, 'mmbiz.qpic.cn')) {
  235. $images[] = $val;
  236. } else {
  237. if (strexists ($val, './attachment/images/')) {
  238. $images[] = tomedia ($val);
  239. }
  240. }
  241. }
  242. }
  243. return $images;
  244. }
  245. function material_parse_content($content) {
  246. global $_W;
  247. $content = material_strip_wechat_image_proxy($content);
  248. $images = material_get_image_url($content);
  249. if (!empty($images)) {
  250. foreach ($images as $image) {
  251. $thumb = file_remote_attach_fetch(tomedia($image), 1024, 'material/images');
  252. if(is_error($thumb)) {
  253. return $thumb;
  254. }
  255. $thumb = ATTACHMENT_ROOT . $thumb;
  256. $account_api = WeAccount::createByUniacid();
  257. $result = $account_api->uploadNewsThumb($thumb);
  258. if (is_error($result)) {
  259. return $result;
  260. } else {
  261. $content = str_replace($image, $result, $content);
  262. }
  263. }
  264. }
  265. return $content;
  266. }
  267. function material_local_news_upload($attach_id) {
  268. global $_W;
  269. $account_api = WeAccount::createByUniacid();
  270. $material = material_get($attach_id);
  271. if (is_error($material)){
  272. return error('-1', '获取素材文件失败');
  273. }
  274. foreach ($material['news'] as $news) {
  275. if (empty($news['content'])){
  276. return error('-6', '素材内容不能为空');
  277. }
  278. $news['content'] = material_parse_content($news['content']);
  279. if (!empty($news['content_source_url'])) {
  280. $news['content_source_url'] = safe_gpc_url($news['content_source_url'], false, $_W['siteroot'] . 'app/' . $news['content_source_url']);
  281. }
  282. if (is_error($news['content'])) {
  283. return error('-2', $news['content']['message']);
  284. }
  285. if (empty($news['thumb_media_id'])) {
  286. if (empty($news['thumb_url'])){
  287. return error('-7', '图文封面不能为空');
  288. }else{
  289. $result = material_local_upload_by_url($news['thumb_url']);
  290. if (is_error($result)){
  291. return error('-3', $result['message']);
  292. }
  293. $news['thumb_media_id'] = $result['media_id'];
  294. $news['thumb_url'] = $result['url'];
  295. }
  296. }
  297. pdo_update('wechat_news', $news, array('id' => $news['id']));
  298. if (empty($material['media_id'])){
  299. $articles['articles'][] = $news;
  300. } else {
  301. $edit_attachment['media_id'] = $material['media_id'];
  302. $edit_attachment['index'] = $news['displayorder'];
  303. $edit_attachment['articles'] = $news;
  304. $result = $account_api->editMaterialNews($edit_attachment);
  305. if (is_error($result)){
  306. return error('-4', $result['message']);
  307. }
  308. }
  309. }
  310. if (empty($material['media_id'])){
  311. $media_id = $account_api->addMatrialNews($articles);
  312. if (is_error($media_id)) {
  313. return error('-5', $media_id, '');
  314. }
  315. $material_info = $account_api->getMaterial($media_id, false);
  316. if (!empty($material_info['news_item'])) {
  317. foreach ($material_info['news_item'] as $key => $info) {
  318. pdo_update('wechat_news', array('url' => $info['url']), array('uniacid' => $_W['uniacid'], 'attach_id' => $material['id'], 'displayorder' => $key));
  319. }
  320. }
  321. pdo_update('wechat_attachment', array(
  322. 'media_id' => $media_id,
  323. 'model' => 'perm'
  324. ), array(
  325. 'uniacid' => $_W['uniacid'],
  326. 'id' => $attach_id
  327. ));
  328. } else {
  329. pdo_update('wechat_attachment', array('model' => 'perm'), array('uniacid' => $_W['uniacid'], 'id' => $attach_id));
  330. }
  331. return $material;
  332. }
  333. function material_local_upload_by_url($url, $type='images') {
  334. global $_W;
  335. $account_api = WeAccount::createByUniacid();
  336. if (! empty($_W['setting']['remote']['type'])) {
  337. $remote_file_url = tomedia($url);
  338. $filepath = file_remote_attach_fetch($remote_file_url,0,'');
  339. if(is_error($filepath)) {
  340. return $filepath;
  341. }
  342. $filepath = ATTACHMENT_ROOT . $filepath;
  343. } else {
  344. if (strexists(parse_url($url, PHP_URL_PATH), '/attachment/')) {
  345. $url = substr(parse_url($url, PHP_URL_PATH), strpos(parse_url($url, PHP_URL_PATH), '/attachment/') + strlen('/attachment/'));
  346. }
  347. $filepath = ATTACHMENT_ROOT . $url;
  348. }
  349. $filesize = filesize($filepath);
  350. $filesize = sizecount($filesize, true);
  351. if ($filesize > 10 && $type == 'videos') {
  352. return error(-1, '要转换的微信素材视频不能超过10M');
  353. }
  354. return $account_api->uploadMediaFixed($filepath, $type);
  355. }
  356. function material_local_upload($material_id){
  357. global $_W;
  358. $type_arr = array('1' => 'images', '2' => 'voices', '3' => 'videos');
  359. $material = pdo_get('core_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
  360. if (empty($material)) {
  361. return error('-1', '同步素材不存在或已删除');
  362. }
  363. return material_local_upload_by_url($material['attachment'], $type_arr[$material['type']]);
  364. }
  365. function material_upload_limit() {
  366. global $_W;
  367. $default = 5 * 1024 * 1024;
  368. $upload_limit = array(
  369. 'num' => '30',
  370. 'image' => $default,
  371. 'voice' => $default,
  372. 'video' => $default
  373. );
  374. $upload = $_W['setting']['upload'];
  375. if (isset($upload['image']['limit']) && (bytecount($upload['image']['limit'].'kb')>0)){
  376. $upload_limit['image'] = bytecount($upload['image']['limit'].'kb');
  377. }
  378. if (isset($upload['image']['limit']) && (bytecount($upload['audio']['limit'].'kb')>0)){
  379. $upload_limit['voice'] = $upload_limit['video'] = bytecount($upload['audio']['limit'].'kb');
  380. }
  381. return $upload_limit;
  382. }
  383. function material_news_delete($material_id){
  384. global $_W;
  385. $permission = permission_account_user_menu($_W['uid'], $_W['uniacid'], 'system');
  386. if (is_error($permission)) {
  387. return error(-1, $permission['message']);
  388. }
  389. if (empty($_W['isfounder']) && !empty($permission) && !in_array('platform_material', $permission) && !in_array('all', $permission)) {
  390. return error('-1', '您没有权限删除该文件');
  391. }
  392. $material_id = intval($material_id);
  393. $material = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
  394. if (empty($material)){
  395. return error('-2', '素材文件不存在或已删除');
  396. }
  397. if (!empty($material['media_id'])){
  398. $account_api = WeAccount::createByUniacid();
  399. $result = $account_api->delMaterial($material['media_id']);
  400. }
  401. if (is_error($result)){
  402. return $result;
  403. }
  404. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $material_id));
  405. pdo_delete('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
  406. return $result;
  407. }
  408. function material_delete($material_id, $location){
  409. global $_W;
  410. if (empty($_W['isfounder']) && !permission_check_account_user('platform_material_delete')) {
  411. return error('-1', '您没有权限删除该文件');
  412. }
  413. $material_id = intval($material_id);
  414. $table = $location == 'wechat' ? 'wechat_attachment' : 'core_attachment';
  415. $material = pdo_get($table, array('id' => $material_id));
  416. if (empty($material)){
  417. return error('-2', '素材文件不存在或已删除');
  418. }
  419. if ($location == 'wechat' && !empty($material['media_id'])){
  420. $account_api = WeAccount::createByUniacid();
  421. $result = $account_api->delMaterial($material['media_id']);
  422. } else {
  423. if (!empty($material['uniacid'])) {
  424. $role = permission_account_user_role($_W['uid'], $material['uniacid']);
  425. if (in_array($role, array(ACCOUNT_MANAGE_NAME_OPERATOR, ACCOUNT_MANAGE_NAME_MANAGER)) && $_W['uid'] != $material['uid']) {
  426. return error('-1', '您没有权限删除该文件');
  427. }
  428. } elseif ($_W['uid'] != $material['uid']) {
  429. return error('-1', '您没有权限删除该文件');
  430. }
  431. if (!empty($_W['setting']['remote']['type'])) {
  432. $result = file_remote_delete($material['attachment']);
  433. if (file_exists(IA_ROOT . '/' . $_W['config']['upload']['attachdir'] . '/' . $material['attachment'])) {
  434. $result = file_delete($material['attachment']);
  435. }
  436. } else {
  437. $result = file_delete($material['attachment']);
  438. }
  439. }
  440. if (is_error($result)) {
  441. return error('-3', '删除文件操作发生错误');
  442. }
  443. pdo_delete($table, array('id' => $material_id, 'uniacid' => $_W['uniacid']));
  444. return $result;
  445. }
  446. function material_url_check($url) {
  447. if (empty($url)){
  448. return true;
  449. } else {
  450. $pattern ="/^((https|http|tel):\/\/|\.\/index.php)[^\s]+/i";
  451. return preg_match($pattern, trim($url));
  452. }
  453. }
  454. function material_news_list($server = '', $search ='', $page = array('page_index' => 1, 'page_size' => 24)) {
  455. global $_W;
  456. $wechat_news_table = table('wechat_news');
  457. $wechat_attachment_table = table('wechat_attachment');
  458. $material_list = array();
  459. if (empty($search)) {
  460. $wechat_attachment_table->searchWithUniacid($_W['uniacid']);
  461. $wechat_attachment_table->searchWithType('news');
  462. if (!empty($server) && in_array($server, array('local', 'perm'))) {
  463. $wechat_attachment_table->searchWithModel($server);
  464. }
  465. $wechat_attachment_table->searchWithPage($page['page_index'], $page['page_size']);
  466. $news_list = $wechat_attachment_table->orderby('createtime DESC')->getall();
  467. $total = $wechat_attachment_table->getLastQueryTotal();
  468. if (! empty($news_list)) {
  469. foreach ($news_list as $news){
  470. $news['items'] = $wechat_news_table->getAllByAttachId($news['id']);
  471. $material_list[$news['id']] = $news;
  472. }
  473. }
  474. }else{
  475. $wechat_news_table->searchKeyword("%$search%");
  476. $wechat_news_table->searchWithUniacid($_W['uniacid']);
  477. $search_attach_id = $wechat_news_table->getall();
  478. if (!empty($search_attach_id)) {
  479. foreach ($search_attach_id as $news) {
  480. if (isset($material_list[$news['attach_id']]) && !empty($material_list[$news['attach_id']])) {
  481. continue;
  482. }
  483. $wechat_attachment = $wechat_attachment_table->getById($news['attach_id']);
  484. if (empty($wechat_attachment)) {
  485. continue;
  486. }
  487. $material_list[$news['attach_id']] = $wechat_attachment;
  488. $material_list[$news['attach_id']]['items'] = $wechat_news_table->getAllByAttachId($news['attach_id']);
  489. }
  490. }
  491. }
  492. foreach ($material_list as $key => &$news) {
  493. if (isset($news['items']) && is_array($news['items'])) {
  494. if (empty($news['items'][0])) {
  495. $news['items'] = array_values($news['items']);
  496. }
  497. foreach ($news['items'] as &$item) {
  498. $item['thumb_url'] = tomedia($item['thumb_url']);
  499. }
  500. }
  501. }
  502. unset($news_list);
  503. $pager = pagination($total, $page['page_index'], $page['page_size'],'',$context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
  504. $material_news = array('material_list' => $material_list, 'page' => $pager);
  505. return $material_news;
  506. }
  507. function material_list($type = '', $server = '', $page = array('page_index' => 1, 'page_size' => 24)) {
  508. global $_W;
  509. $tables = array(MATERIAL_LOCAL => 'core_attachment', MATERIAL_WEXIN => 'wechat_attachment');
  510. $conditions['uniacid'] = $_W['uniacid'];
  511. $table = $tables[$server];
  512. switch ($type) {
  513. case 'voice' :
  514. $conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_VOICE : 'voice';
  515. break;
  516. case 'video' :
  517. $conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_VEDIO : 'video';
  518. break;
  519. default :
  520. $conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_IMAGE : 'image';
  521. break;
  522. }
  523. if ($server == 'local') {
  524. $material_list = pdo_getslice($table, $conditions, array($page['page_index'], $page['page_size']), $total, array(), '', 'createtime DESC');
  525. } else {
  526. $conditions['model'] = MATERIAL_WEXIN;
  527. $material_list = pdo_getslice($table, $conditions, array($page['page_index'], $page['page_size']), $total, array(), '', 'createtime DESC');
  528. if ($type == 'video'){
  529. foreach ($material_list as &$row) {
  530. $row['tag'] = $row['tag'] == '' ? array() : iunserializer($row['tag']);
  531. if (empty($row['filename'])) {
  532. $row['filename'] = $row['tag']['title'];
  533. }
  534. }
  535. unset($row);
  536. }
  537. }
  538. $pager = pagination($total, $page['page_index'], $page['page_size'],'',$context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
  539. $material_news = array('material_list' => $material_list, 'page' => $pager);
  540. return $material_news;
  541. }
  542. function material_news_to_local($attach_id) {
  543. $material = material_get($attach_id);
  544. if(is_error($material)) {
  545. return $material;
  546. }
  547. $attach_id = material_news_set($material['news'],$attach_id);
  548. if(is_error($attach_id)) {
  549. return $attach_id;
  550. }
  551. $material['items'] = $material['news']; return $material;
  552. }
  553. function material_to_local($resourceid, $uniacid, $uid, $type = 'image') {
  554. $material = material_get($resourceid);
  555. if(is_error($material)) {
  556. return $material;
  557. }
  558. return material_network_image_to_local($material['url'], $uniacid, $uid);
  559. }
  560. function material_network_image_to_local($url, $uniacid, $uid) {
  561. return material_network_to_local($url, $uniacid, $uid, 'image');
  562. }
  563. function material_network_to_local($url, $uniacid, $uid, $type = 'image') {
  564. $path = file_remote_attach_fetch($url); if(is_error($path)) {
  565. return $path;
  566. }
  567. $filename = pathinfo($path,PATHINFO_FILENAME);
  568. $data = array('uniacid' => $uniacid, 'uid' => $uid,
  569. 'filename' => $filename,
  570. 'attachment' => $path,
  571. 'type' => $type == 'image' ? ATTACH_TYPE_IMAGE : ($type == 'audio'||$type == 'voice' ? ATTACH_TYPE_VOICE : ATTACH_TYPE_VEDIO),
  572. 'createtime'=>TIMESTAMP
  573. );
  574. pdo_insert('core_attachment', $data);
  575. $id = pdo_insertid();
  576. $data['id'] = $id;
  577. $data['url'] = tomedia($path);
  578. return $data;
  579. }
  580. function material_to_wechat($attach_id, $uniacid, $uid, $acid, $type = 'image') {
  581. $result = material_local_upload($attach_id); if (is_error($result)) {
  582. return $result;
  583. }
  584. $tag = $result['url'];
  585. if($type == 'video') {
  586. $tag = serialize(array('title'=>'网络视频','description'=>'网络视频'));
  587. }
  588. $data = array('uniacid' => $uniacid, 'uid' => $uid, 'acid' => $acid,
  589. 'media_id' => $result['media_id'],
  590. 'attachment' => $result['url'],
  591. 'type' => $type,
  592. 'tag' => $tag,
  593. 'model' => 'perm',
  594. 'createtime'=>TIMESTAMP
  595. );
  596. pdo_insert('wechat_attachment', $data);
  597. $id = pdo_insertid();
  598. $data['url'] = tomedia($result['url']);
  599. $data['id'] = $id;
  600. return $data;
  601. }
  602. function material_network_image_to_wechat($url, $uniacid, $uid, $acid) {
  603. return material_network_to_wechat($url, $uniacid, $uid, $acid, 'image');
  604. }
  605. function material_network_to_wechat($url, $uniacid, $uid, $acid, $type = 'image') {
  606. $local = material_network_to_local($url, $uniacid, $uid, $type); if (is_error($local)) {
  607. return $local;
  608. }
  609. return material_to_wechat($local['id'], $uniacid, $uid, $acid, $type);
  610. }