人人商城

wechat_file.ctrl.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. error_reporting(0);
  8. global $_W;
  9. load()->func('file');
  10. load()->func('communication');
  11. load()->model('account');
  12. $limit = array();
  13. $limit['temp'] = array(
  14. 'image' => array(
  15. 'ext' => array('jpg', 'logo'),
  16. 'size' => 1024 * 1024,
  17. 'errmsg' => '临时图片只支持jpg/logo格式,大小不超过为1M',
  18. ),
  19. 'voice' => array(
  20. 'ext' => array('amr', 'mp3'),
  21. 'size' => 2048 * 1024,
  22. 'errmsg' => '临时语音只支持amr/mp3格式,大小不超过为2M',
  23. ),
  24. 'video' => array(
  25. 'ext' => array('mp4'),
  26. 'size' => 10240 * 1024,
  27. 'errmsg' => '临时视频只支持mp4格式,大小不超过为10M',
  28. ),
  29. 'thumb' => array(
  30. 'ext' => array('jpg', 'logo'),
  31. 'size' => 64 * 1024,
  32. 'errmsg' => '临时缩略图只支持jpg/logo格式,大小不超过为64K',
  33. ),
  34. );
  35. $limit['perm'] = array(
  36. 'image' => array(
  37. 'ext' => array('bmp', 'png', 'jpeg', 'jpg', 'gif'),
  38. 'size' => 2048 * 1024,
  39. 'max' => 5000,
  40. 'errmsg' => '永久图片只支持bmp/png/jpeg/jpg/gif格式,大小不超过为2M',
  41. ),
  42. 'voice' => array(
  43. 'ext' => array('amr', 'mp3', 'wma', 'wav', 'amr'),
  44. 'size' => 5120 * 1024,
  45. 'max' => 1000,
  46. 'errmsg' => '永久语音只支持mp3/wma/wav/amr格式,大小不超过为5M,长度不超过60秒',
  47. ),
  48. 'video' => array(
  49. 'ext' => array('rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg', 'mp4'),
  50. 'size' => 10240 * 1024 * 2,
  51. 'max' => 1000,
  52. 'errmsg' => '永久视频只支持rm/rmvb/wmv/avi/mpg/mpeg/mp4格式,大小不超过为20M',
  53. ),
  54. 'thumb' => array(
  55. 'ext' => array('bmp', 'png', 'jpeg', 'jpg', 'gif'),
  56. 'size' => 2048 * 1024,
  57. 'max' => 5000,
  58. 'errmsg' => '永久缩略图只支持bmp/png/jpeg/jpg/gif格式,大小不超过为2M',
  59. ),
  60. );
  61. $limit['file_upload'] = array(
  62. 'image' => array(
  63. 'ext' => array('jpg'),
  64. 'size' => 1024 * 1024,
  65. 'max' => -1,
  66. 'errmsg' => '图片只支持jpg格式,大小不超过为1M',
  67. )
  68. );
  69. $apis = array();
  70. $apis['temp'] = array(
  71. 'add' => 'https://api.weixin.qq.com/cgi-bin/media/upload',
  72. 'get' => 'https://api.weixin.qq.com/cgi-bin/media/get',
  73. 'post_key' => 'media'
  74. );
  75. $apis['perm'] = array(
  76. 'add' => 'https://api.weixin.qq.com/cgi-bin/material/add_material',
  77. 'get' => 'https://api.weixin.qq.com/cgi-bin/material/get_material',
  78. 'del' => 'https://api.weixin.qq.com/cgi-bin/material/del_material',
  79. 'count' => 'https://api.weixin.qq.com/cgi-bin/material/get_materialcount',
  80. 'batchget' => 'https://api.weixin.qq.com/cgi-bin/material/batchget_material',
  81. 'post_key' => 'media',
  82. );
  83. $apis['file_upload'] = array(
  84. 'add' => 'https://api.weixin.qq.com/cgi-bin/media/uploadimg',
  85. 'post_key' => 'buffer',
  86. );
  87. $result = array(
  88. 'error' => 1,
  89. 'message' => '',
  90. 'data' => ''
  91. );
  92. $type = trim($_GPC['types']);
  93. if($type == 'image' || $type == 'thumb') {
  94. $type = 'image';
  95. }
  96. if($type == 'voice' || $type == 'video') {
  97. $type = 'audio';
  98. }
  99. $setting['folder'] = "{$type}s/{$_W['uniacid']}" . '/'.date('Y/m/');
  100. if ($do == 'upload') {
  101. $type = trim($_GPC['types']);
  102. $mode = trim($_GPC['mode']);
  103. $acid = $_W['acid'];
  104. if($mode == 'perm') {
  105. $now_count = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('wechat_attachment') . ' WHERE uniacid = :aid AND acid = :acid AND model = :model AND type = :type', array(':aid' => $_W['uniacid'], ':acid' => $acid, ':model' => $mode, ':type' => $type));
  106. if($now_count >= $limit['perm'][$type]['max']) {
  107. $result['message'] = '文件数量超过限制,请先删除部分文件再上传';
  108. die(json_encode($result));
  109. }
  110. }
  111. if(empty($mode) || empty($type) || !$_W['acid']) {
  112. $result['message'] = '上传配置出错';
  113. die(json_encode($result));
  114. }
  115. if (empty($_FILES['file']['name'])) {
  116. $result['message'] = '上传失败, 请选择要上传的文件!';
  117. die(json_encode($result));
  118. }
  119. if ($_FILES['file']['error'] != 0) {
  120. $result['message'] = '上传失败, 请重试.';
  121. die(json_encode($result));
  122. }
  123. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  124. $ext = strtolower($ext);
  125. $size = intval($_FILES['file']['size']);
  126. $originname = $_FILES['file']['name'];
  127. if(!in_array($ext, $limit[$mode][$type]['ext']) || ($size > $limit[$mode][$type]['size'])) {
  128. $result['message'] = $limit[$mode][$type]['errmsg'];
  129. die(json_encode($result));
  130. }
  131. $filename = file_random_name(ATTACHMENT_ROOT .'/'. $setting['folder'], $ext);
  132. $file = file_wechat_upload($_FILES['file'], $type, $setting['folder'] . $filename);
  133. if (is_error($file)) {
  134. $result['message'] = $file['message'];
  135. die(json_encode($result));
  136. }
  137. $pathname = $file['path'];
  138. $fullname = ATTACHMENT_ROOT . '/' . $pathname;
  139. $acc = WeAccount::create($acid);
  140. $token = $acc->getAccessToken();
  141. if (is_error($token)) {
  142. $result['message'] = $token['message'];
  143. die(json_encode($result));
  144. }
  145. if($mode == 'perm' || $mode == 'temp') {
  146. $sendapi = $apis[$mode]['add'] . "?access_token={$token}&type={$type}";
  147. $media = '@'.$fullname;
  148. $data = array(
  149. 'media' => $media
  150. );
  151. if($type == 'video') {
  152. $description = array(
  153. 'title' => $filename,
  154. 'introduction' => $filename,
  155. );
  156. $data['description'] = urldecode(json_encode($description));
  157. }
  158. } elseif($mode == 'file_upload') {
  159. $sendapi = $apis[$mode]['add'] . "?access_token={$token}";
  160. $data = array(
  161. 'buffer' => '@'.$fullname
  162. );
  163. $type = 'image';
  164. }
  165. $resp = ihttp_request($sendapi, $data);
  166. if(is_error($resp)) {
  167. $result['error'] = 0;
  168. $result['message'] = $resp['message'];
  169. die(json_encode($result));
  170. }
  171. $content = @json_decode($resp['content'], true);
  172. if(empty($content)) {
  173. $result['error'] = 0;
  174. $result['message'] = "接口调用失败, 元数据: {$resp['meta']}";
  175. die(json_encode($result));
  176. }
  177. if(!empty($content['errcode'])) {
  178. $result['error'] = 0;
  179. $result['message'] = "访问微信接口错误, 错误代码: {$content['errcode']}, 错误信息: {$content['errmsg']},错误详情:{$acc->error_code($content['errcode'])}";
  180. die(json_encode($result));
  181. }
  182. if($mode == 'perm' || $mode == 'temp') {
  183. if(!empty($content['media_id'])){
  184. $result['media_id'] = $content['media_id'];
  185. }
  186. if(!empty($content['thumb_media_id'])){
  187. $result['media_id'] = $content['thumb_media_id'];
  188. }
  189. } elseif($mode == 'file_upload') {
  190. $result['media_id'] = $content['url'];
  191. }
  192. if ($type == 'image' || $type == 'thumb' ) {
  193. $file['path'] = file_image_thumb($fullname, '', 300);
  194. }
  195. if (!empty($_W['setting']['remote']['type']) && !empty($file['path'])) {
  196. $remotestatus = file_remote_upload($file['path']);
  197. if (is_error($remotestatus)) {
  198. file_delete($pathname);
  199. if($type == 'image' || $type == 'thumb'){
  200. file_delete($file['path']);
  201. }
  202. $result['error'] = 0;
  203. $result['message'] = '远程附件上传失败,请检查配置并重新上传';
  204. die(json_encode($result));
  205. } else {
  206. file_delete($pathname);
  207. if($type == 'image' || $type == 'thumb'){
  208. file_delete($file['path']);
  209. }
  210. }
  211. }
  212. $insert = array(
  213. 'uniacid' => $_W['uniacid'],
  214. 'acid' => $acid,
  215. 'uid' => $_W['uid'],
  216. 'filename' => $originname,
  217. 'attachment' => $file['path'],
  218. 'media_id' => $result['media_id'],
  219. 'type' => $type,
  220. 'model' => $mode,
  221. 'createtime' => TIMESTAMP
  222. );
  223. if($type == 'image' || $type == 'thumb') {
  224. $size = getimagesize($fullname);
  225. $insert['width'] = $size[0];
  226. $insert['height'] = $size[1];
  227. if($mode == 'perm') {
  228. $insert['tag'] = $content['url'];
  229. }
  230. if(!empty($insert['tag'])) {
  231. $insert['attachment'] = $content['url'];
  232. }
  233. $result['width'] = $size[0];
  234. $result['hieght'] = $size[1];
  235. }
  236. if($type == 'video') {
  237. $insert['tag'] = iserializer($description);
  238. }
  239. pdo_insert('wechat_attachment', $insert);
  240. $result['type'] = $type;
  241. $result['url'] = tomedia($file['path']);
  242. if($type == 'image' || $type == 'thumb') {
  243. @unlink($fullname);
  244. }
  245. if($type == 'video') {
  246. $result['title'] = $description['title'];
  247. $result['introduction'] = $description['introduction'];
  248. }
  249. $result['mode'] = $mode;
  250. die(json_encode($result));
  251. }
  252. if ($do == 'browser') {
  253. $types = array('image', 'thumb', 'voice', 'video');
  254. $type = in_array($_GPC['type'], $types) ? $_GPC['type'] : 'image';
  255. $mode = trim($_GPC['mode']);
  256. $acid = $_W['acid'];
  257. $condition = ' WHERE uniacid = :uniacid AND acid = :acid';
  258. $param = array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']);
  259. if(empty($mode)) {
  260. $condition .= ' AND type = :type AND model = :model';
  261. $param[':type'] = $type;
  262. $param[':model'] = 'perm';
  263. } else {
  264. $condition .= ' AND model = :model';
  265. $param[':model'] = $mode;
  266. }
  267. $page = intval($_GPC['page']);
  268. $page = max(1, $page);
  269. $size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
  270. $sql = 'SELECT * FROM '.tablename('wechat_attachment')."{$condition} ORDER BY id DESC LIMIT ".(($page-1) * $size).','.$size;
  271. $list = pdo_fetchall($sql, $param, 'id');
  272. foreach ($list as &$item) {
  273. $item['url'] = tomedia($item['attachment']);
  274. $item['createtime'] = date('Y-m-d H:i', $item['createtime']);
  275. if($item['type'] == 'video') {
  276. $item['tag'] = iunserializer($item['tag']);
  277. }
  278. unset($item['uid']);
  279. }
  280. $total = pdo_fetchcolumn('SELECT count(*) FROM '.tablename('wechat_attachment') . $condition, $param);
  281. iajax(0, array('page'=> pagination($total, $page, $size, '', array('before' => '2', 'after' => '3', 'ajaxcallback'=>'null')), 'items' => $list));
  282. }
  283. if ($do == 'delete') {
  284. $id = intval($_GPC['id']);
  285. $acid = $_W['acid'];
  286. $data = pdo_fetch('SELECT * FROM ' . tablename('wechat_attachment') . ' WHERE acid = :acid AND id = :id', array(':acid' => $acid, ':id' => $id));
  287. if(empty($data)) {
  288. $result['error'] = 0;
  289. $result['message'] = '素材不存在';
  290. die(json_encode($result));
  291. }
  292. $acc = WeAccount::create($acid);
  293. $token = $acc->getAccessToken();
  294. if (is_error($token)) {
  295. $result['error'] = 0;
  296. $result['message'] = $token['message'];
  297. die(json_encode($result));
  298. }
  299. $sendapi = $apis[$data['model']]['del'] . "?access_token={$token}";
  300. $post = array(
  301. 'media_id' => $data['media_id']
  302. );
  303. $resp = ihttp_request($sendapi, json_encode($post));
  304. if(is_error($resp)) {
  305. $result['error'] = 0;
  306. $result['message'] = $resp['message'];
  307. die(json_encode($result));
  308. }
  309. $content = @json_decode($resp['content'], true);
  310. if(empty($content)) {
  311. $result['error'] = 0;
  312. $result['message'] = "接口调用失败, 元数据: {$resp['meta']}";
  313. die(json_encode($result));
  314. }
  315. if(!empty($content['errcode'])) {
  316. $result['error'] = 0;
  317. $result['message'] = "访问微信接口错误, 错误代码: {$content['errcode']}, 错误信息: {$content['errmsg']},错误详情:{$acc->error_code($content['errcode'])}";
  318. die(json_encode($result));
  319. }
  320. pdo_delete('wechat_attachment', array('acid' => $acid, 'id' => $id));
  321. die(json_encode($result));
  322. }
  323. function delete_temp(){
  324. pdo_query('DELETE FROM ' . tablename('wechat_attachment') . ' WHERE createtime + 259200 < :time', array(':time' => time()));
  325. }