人人商城

material.mod.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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', array('attach_id' => $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', array('attach_id' => $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::createByUniacid();
  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::createByUniacid();
  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::createByUniacid();
  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. $filesize = sizecount($filesize, true);
  354. if ($filesize > 10 && $type == 'videos') {
  355. return error(-1, '要转换的微信素材视频不能超过10M');
  356. }
  357. return $account_api->uploadMediaFixed($filepath, $type);
  358. }
  359. function material_local_upload($material_id){
  360. global $_W;
  361. $type_arr = array('1' => 'images', '2' => 'voices', '3' => 'videos');
  362. $material = pdo_get('core_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
  363. if (empty($material)) {
  364. return error('-1', '同步素材不存在或已删除');
  365. }
  366. return material_local_upload_by_url($material['attachment'], $type_arr[$material['type']]);
  367. }
  368. function material_upload_limit() {
  369. global $_W;
  370. $default = 5 * 1024 * 1024;
  371. $upload_limit = array(
  372. 'num' => '30',
  373. 'image' => $default,
  374. 'voice' => $default,
  375. 'video' => $default
  376. );
  377. $upload = $_W['setting']['upload'];
  378. if (isset($upload['image']['limit']) && (bytecount($upload['image']['limit'].'kb')>0)){
  379. $upload_limit['image'] = bytecount($upload['image']['limit'].'kb');
  380. }
  381. if (isset($upload['image']['limit']) && (bytecount($upload['audio']['limit'].'kb')>0)){
  382. $upload_limit['voice'] = $upload_limit['video'] = bytecount($upload['audio']['limit'].'kb');
  383. }
  384. return $upload_limit;
  385. }
  386. function material_news_delete($material_id){
  387. global $_W;
  388. $permission = permission_account_user_menu($_W['uid'], $_W['uniacid'], 'system');
  389. if (is_error($permission)) {
  390. return error(-1, $permission['message']);
  391. }
  392. if (empty($_W['isfounder']) && !empty($permission) && !in_array('platform_material', $permission) && !in_array('all', $permission)) {
  393. return error('-1', '您没有权限删除该文件');
  394. }
  395. $material_id = intval($material_id);
  396. $material = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
  397. if (empty($material)){
  398. return error('-2', '素材文件不存在或已删除');
  399. }
  400. if (!empty($material['media_id'])){
  401. $account_api = WeAccount::createByUniacid();
  402. $result = $account_api->delMaterial($material['media_id']);
  403. }
  404. if (is_error($result)){
  405. return $result;
  406. }
  407. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $material_id));
  408. pdo_delete('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $material_id));
  409. return $result;
  410. }
  411. function material_delete($material_id, $location){
  412. global $_W;
  413. if (empty($_W['isfounder']) && !permission_check_account_user('platform_material_delete')) {
  414. return error('-1', '您没有权限删除该文件');
  415. }
  416. $material_id = intval($material_id);
  417. $table = $location == 'wechat' ? 'wechat_attachment' : 'core_attachment';
  418. $material = pdo_get($table, array('id' => $material_id));
  419. if (empty($material)){
  420. return error('-2', '素材文件不存在或已删除');
  421. }
  422. if ($location == 'wechat' && !empty($material['media_id'])){
  423. $account_api = WeAccount::createByUniacid();
  424. $result = $account_api->delMaterial($material['media_id']);
  425. } else {
  426. if (!empty($material['uniacid'])) {
  427. $role = permission_account_user_role($_W['uid'], $material['uniacid']);
  428. if (in_array($role, array(ACCOUNT_MANAGE_NAME_OPERATOR, ACCOUNT_MANAGE_NAME_MANAGER)) && $_W['uid'] != $material['uid']) {
  429. return error('-1', '您没有权限删除该文件');
  430. }
  431. } elseif ($_W['uid'] != $material['uid']) {
  432. return error('-1', '您没有权限删除该文件');
  433. }
  434. if (!empty($_W['setting']['remote']['type'])) {
  435. $result = file_remote_delete($material['attachment']);
  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));
  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. $conditions[':uniacid'] = $_W['uniacid'];
  457. $news_model_sql = '';
  458. if (!empty($server)) {
  459. $news_model_sql = " AND a.model = :news_model";
  460. $conditions[':news_model'] = $server;
  461. }
  462. $search_sql = '';
  463. if (!empty($search)) {
  464. $search_sql = " AND (b.title LIKE :search_title OR b.author = :search_author OR b.digest LIKE :search_digest)";
  465. $conditions[':search_title'] = "%{$search}%";
  466. $conditions[':search_author'] = "%{$search}%";
  467. $conditions[':search_digest'] = "%{$search}%";
  468. }
  469. $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";
  470. $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']);
  471. $total_sql = sprintf($select_sql, "count(*)", '');
  472. $total = pdo_fetchcolumn($total_sql, $conditions);
  473. $news_list = pdo_fetchall($list_sql, $conditions);
  474. $material_list = array();
  475. if (! empty($news_list)) {
  476. foreach ($news_list as $news){
  477. if (isset($material_list[$news['attach_id']])){
  478. $material_list[$news['attach_id']]['items'][$news['displayorder']] = $news;
  479. }else{
  480. $material_list[$news['attach_id']] = array(
  481. 'id' => $news['id'],
  482. 'filename' => $news['filename'],
  483. 'attachment' => $news['attachment'],
  484. 'media_id' => $news['media_id'],
  485. 'type' => $news['type'],
  486. 'model' => $news['model'],
  487. 'tag' => $news['tag'],
  488. 'createtime' => $news['createtime'],
  489. 'items' => array($news['displayorder'] => $news),
  490. );
  491. }
  492. }
  493. }
  494. foreach ($material_list as $key => &$news) {
  495. if (isset($news['items']) && is_array($news['items'])) {
  496. foreach ($news['items'] as &$item) {
  497. $item['thumb_url'] = tomedia($item['thumb_url']);
  498. }
  499. }
  500. }
  501. unset($news_list);
  502. $pager = pagination($total, $page['page_index'], $page['page_size'],'',$context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
  503. $material_news = array('material_list' => $material_list, 'page' => $pager);
  504. return $material_news;
  505. }
  506. function material_list($type = '', $server = '', $page = array('page_index' => 1, 'page_size' => 24)) {
  507. global $_W;
  508. $tables = array(MATERIAL_LOCAL => 'core_attachment', MATERIAL_WEXIN => 'wechat_attachment');
  509. $conditions['uniacid'] = $_W['uniacid'];
  510. $table = $tables[$server];
  511. switch ($type) {
  512. case 'voice' :
  513. $conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_VOICE : 'voice';
  514. break;
  515. case 'video' :
  516. $conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_VEDIO : 'video';
  517. break;
  518. default :
  519. $conditions['type'] = $server == MATERIAL_LOCAL ? ATTACH_TYPE_IMAGE : 'image';
  520. break;
  521. }
  522. if ($server == 'local') {
  523. $material_list = pdo_getslice($table, $conditions, array($page['page_index'], $page['page_size']), $total, array(), '', 'createtime DESC');
  524. } else {
  525. $conditions['model'] = MATERIAL_WEXIN;
  526. $material_list = pdo_getslice($table, $conditions, array($page['page_index'], $page['page_size']), $total, array(), '', 'createtime DESC');
  527. if ($type == 'video'){
  528. foreach ($material_list as &$row) {
  529. $row['tag'] = $row['tag'] == '' ? array() : iunserializer($row['tag']);
  530. if (empty($row['filename'])) {
  531. $row['filename'] = $row['tag']['title'];
  532. }
  533. }
  534. unset($row);
  535. }
  536. }
  537. $pager = pagination($total, $page['page_index'], $page['page_size'],'',$context = array('before' => 5, 'after' => 4, 'isajax' => $_W['isajax']));
  538. $material_news = array('material_list' => $material_list, 'page' => $pager);
  539. return $material_news;
  540. }
  541. function material_news_to_local($attach_id) {
  542. $material = material_get($attach_id);
  543. if(is_error($material)) {
  544. return $material;
  545. }
  546. $attach_id = material_news_set($material['news'],$attach_id);
  547. if(is_error($attach_id)) {
  548. return $attach_id;
  549. }
  550. $material['items'] = $material['news']; return $material;
  551. }
  552. function material_to_local($resourceid, $uniacid, $uid, $type = 'image') {
  553. $material = material_get($resourceid);
  554. if(is_error($material)) {
  555. return $material;
  556. }
  557. return material_network_image_to_local($material['url'], $uniacid, $uid);
  558. }
  559. function material_network_image_to_local($url, $uniacid, $uid) {
  560. return material_network_to_local($url, $uniacid, $uid, 'image');
  561. }
  562. function material_network_to_local($url, $uniacid, $uid, $type = 'image') {
  563. $path = file_remote_attach_fetch($url); if(is_error($path)) {
  564. return $path;
  565. }
  566. $filename = pathinfo($path,PATHINFO_FILENAME);
  567. $data = array('uniacid' => $uniacid, 'uid' => $uid,
  568. 'filename' => $filename,
  569. 'attachment' => $path,
  570. 'type' => $type == 'image' ? ATTACH_TYPE_IMAGE : ($type == 'audio'||$type == 'voice' ? ATTACH_TYPE_VOICE : ATTACH_TYPE_VEDIO),
  571. 'createtime'=>TIMESTAMP
  572. );
  573. pdo_insert('core_attachment', $data);
  574. $id = pdo_insertid();
  575. $data['id'] = $id;
  576. $data['url'] = tomedia($path);
  577. return $data;
  578. }
  579. function material_to_wechat($attach_id, $uniacid, $uid, $acid, $type = 'image') {
  580. $result = material_local_upload($attach_id); if (is_error($result)) {
  581. return $result;
  582. }
  583. $tag = $result['url'];
  584. if($type == 'video') {
  585. $tag = serialize(array('title'=>'网络视频','description'=>'网络视频'));
  586. }
  587. $data = array('uniacid' => $uniacid, 'uid' => $uid, 'acid' => $acid,
  588. 'media_id' => $result['media_id'],
  589. 'attachment' => $result['url'],
  590. 'type' => $type,
  591. 'tag' => $tag,
  592. 'model' => 'perm',
  593. 'createtime'=>TIMESTAMP
  594. );
  595. pdo_insert('wechat_attachment', $data);
  596. $id = pdo_insertid();
  597. $data['url'] = tomedia($result['url']);
  598. $data['id'] = $id;
  599. return $data;
  600. }
  601. function material_network_image_to_wechat($url, $uniacid, $uid, $acid) {
  602. return material_network_to_wechat($url, $uniacid, $uid, $acid, 'image');
  603. }
  604. function material_network_to_wechat($url, $uniacid, $uid, $acid, $type = 'image') {
  605. $local = material_network_to_local($url, $uniacid, $uid, $type); if (is_error($local)) {
  606. return $local;
  607. }
  608. return material_to_wechat($local['id'], $uniacid, $uid, $acid, $type);
  609. }