人人商城

api.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. <?php
  2. define('IN_API', true);
  3. require_once './framework/bootstrap.inc.php';
  4. load()->model('reply');
  5. load()->model('attachment');
  6. load()->model('visit');
  7. load()->app('common');
  8. load()->classs('wesession');
  9. $hash = $_GPC['hash'];
  10. if(!empty($hash)) {
  11. $id = pdo_fetchcolumn("SELECT acid FROM " . tablename('account') . " WHERE hash = :hash", array(':hash' => $hash));
  12. }
  13. if(!empty($_GPC['appid'])) {
  14. $appid = ltrim($_GPC['appid'], '/');
  15. if ($appid == 'wx570bc396a51b8ff8') {
  16. $_W['account'] = array(
  17. 'type' => '3',
  18. 'key' => 'wx570bc396a51b8ff8',
  19. 'level' => 4,
  20. 'token' => 'platformtestaccount'
  21. );
  22. } else {
  23. $id = pdo_fetchcolumn("SELECT acid FROM " . tablename('account_wechats') . " WHERE `key` = :appid", array(':appid' => $appid));
  24. }
  25. }
  26. if(empty($id)) {
  27. $id = intval($_GPC['id']);
  28. }
  29. if (!empty($id)) {
  30. $uniacid = pdo_getcolumn('account', array('acid' => $id), 'uniacid');
  31. $_W['account'] = uni_fetch($uniacid);
  32. }
  33. if(empty($_W['account'])) {
  34. exit('initial error hash or id');
  35. }
  36. if(empty($_W['account']['token'])) {
  37. exit('initial missing token');
  38. }
  39. $_W['debug'] = intval($_GPC['debug']);
  40. $_W['acid'] = $_W['account']['acid'];
  41. $_W['uniacid'] = $_W['account']['uniacid'];
  42. $_W['uniaccount'] = uni_fetch($_W['uniacid']);
  43. $_W['account']['groupid'] = $_W['uniaccount']['groupid'];
  44. $_W['account']['qrcode'] = $_W['attachurl'].'qrcode_'.$_W['acid'].'.jpg?time='.$_W['timestamp'];
  45. $_W['account']['avatar'] = $_W['attachurl'].'headimg_'.$_W['acid'].'.jpg?time='.$_W['timestamp'];
  46. $_W['attachurl'] = attachment_set_attach_url();
  47. visit_update_today('web', 'we7_api');
  48. $engine = new WeEngine();
  49. if (!empty($_W['setting']['copyright']['status'])) {
  50. $engine->died('抱歉,站点已关闭,关闭原因:' . $_W['setting']['copyright']['reason']);
  51. }
  52. if (!empty($_W['uniaccount']['endtime']) && TIMESTAMP > $_W['uniaccount']['endtime']) {
  53. $engine->died('抱歉,您的公众号已过期,请及时联系管理员');
  54. }
  55. if($_W['isajax'] && $_W['ispost'] && $_GPC['flag'] == 1) {
  56. $engine->encrypt();
  57. }
  58. if($_W['isajax'] && $_W['ispost'] && $_GPC['flag'] == 2) {
  59. $engine->decrypt();
  60. }
  61. load()->func('compat.biz');
  62. $_W['isajax'] = false;
  63. $engine->start();
  64. class WeEngine {
  65. private $account = null;
  66. private $modules = array();
  67. public $keyword = array();
  68. public $message = array();
  69. public function __construct() {
  70. global $_W;
  71. $this->account = WeAccount::create($_W['account']);
  72. if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  73. $_W['modules'] = uni_modules();
  74. $this->modules = array_keys($_W['modules']);
  75. $this->modules[] = 'cover';
  76. $this->modules[] = 'default';
  77. $this->modules[] = 'reply';
  78. $this->modules = array_unique ($this->modules);
  79. }
  80. }
  81. public function encrypt() {
  82. global $_W;
  83. if(empty($this->account)) {
  84. exit('Miss Account.');
  85. }
  86. $timestamp = TIMESTAMP;
  87. $nonce = random(5);
  88. $token = $_W['account']['token'];
  89. $signkey = array($token, TIMESTAMP, $nonce);
  90. sort($signkey, SORT_STRING);
  91. $signString = implode($signkey);
  92. $signString = sha1($signString);
  93. $_GET['timestamp'] = $timestamp;
  94. $_GET['nonce'] = $nonce;
  95. $_GET['signature'] = $signString;
  96. $postStr = file_get_contents('php://input');
  97. if(!empty($_W['account']['encodingaeskey']) && strlen($_W['account']['encodingaeskey']) == 43 && !empty($_W['account']['key']) && $_W['setting']['development'] != 1) {
  98. $data = $this->account->encryptMsg($postStr);
  99. $array = array('encrypt_type' => 'aes', 'timestamp' => $timestamp, 'nonce' => $nonce, 'signature' => $signString, 'msg_signature' => $data[0], 'msg' => $data[1]);
  100. } else {
  101. $data = array('', '');
  102. $array = array('encrypt_type' => '', 'timestamp' => $timestamp, 'nonce' => $nonce, 'signature' => $signString, 'msg_signature' => $data[0], 'msg' => $data[1]);
  103. }
  104. exit(json_encode($array));
  105. }
  106. public function decrypt() {
  107. global $_W;
  108. if(empty($this->account)) {
  109. exit('Miss Account.');
  110. }
  111. $postStr = file_get_contents('php://input');
  112. if(!empty($_W['account']['encodingaeskey']) && strlen($_W['account']['encodingaeskey']) == 43 && !empty($_W['account']['key']) && $_W['setting']['development'] != 1) {
  113. $resp = $this->account->local_decryptMsg($postStr);
  114. } else {
  115. $resp = $postStr;
  116. }
  117. exit($resp);
  118. }
  119. public function start() {
  120. global $_W;
  121. if(empty($this->account)) {
  122. exit('Miss Account.');
  123. }
  124. if(!$this->account->checkSign()) {
  125. exit('Check Sign Fail.');
  126. }
  127. if(strtolower($_SERVER['REQUEST_METHOD']) == 'get') {
  128. $row = array();
  129. $row['isconnect'] = 1;
  130. pdo_update('account', $row, array('acid' => $_W['acid']));
  131. cache_delete(cache_system_key('uniaccount', array('uniacid' => $_W['uniacid'])));
  132. exit(htmlspecialchars($_GET['echostr']));
  133. }
  134. if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  135. $postStr = file_get_contents('php://input');
  136. if(!empty($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes') {
  137. $postStr = $this->account->decryptMsg($postStr);
  138. }
  139. WeUtility::logging('trace', $postStr);
  140. $message = $this->account->parse($postStr);
  141. $this->message = $message;
  142. if(empty($message)) {
  143. WeUtility::logging('waring', 'Request Failed');
  144. exit('Request Failed');
  145. }
  146. $_W['openid'] = $message['from'];
  147. $_W['fans'] = array('from_user' => $_W['openid']);
  148. $this->booking($message);
  149. if($message['event'] == 'unsubscribe') {
  150. $this->receive(array(), array(), array());
  151. exit();
  152. }
  153. $sessionid = md5($message['from'] . $message['to'] . $_W['uniacid']);
  154. session_id($sessionid);
  155. WeSession::start($_W['uniacid'], $_W['openid']);
  156. $_SESSION['openid'] = $_W['openid'];
  157. $pars = $this->analyze($message);
  158. $pars[] = array(
  159. 'message' => $message,
  160. 'module' => 'default',
  161. 'rule' => '-1',
  162. );
  163. $hitParam['rule'] = -2;
  164. $hitParam['module'] = '';
  165. $hitParam['message'] = $message;
  166. $hitKeyword = array();
  167. $response = array();
  168. foreach($pars as $par) {
  169. if(empty($par['module'])) {
  170. continue;
  171. }
  172. $par['message'] = $message;
  173. $response = $this->process($par);
  174. if($this->isValidResponse($response)) {
  175. $hitParam = $par;
  176. if(!empty($par['keyword'])) {
  177. $hitKeyword = $par['keyword'];
  178. }
  179. break;
  180. }
  181. }
  182. $response_debug = $response;
  183. $pars_debug = $pars;
  184. if($hitParam['module'] == 'default' && is_array($response) && is_array($response['params'])) {
  185. foreach($response['params'] as $par) {
  186. if(empty($par['module'])) {
  187. continue;
  188. }
  189. $response = $this->process($par);
  190. if($this->isValidResponse($response)) {
  191. $hitParam = $par;
  192. if(!empty($par['keyword'])) {
  193. $hitKeyword = $par['keyword'];
  194. }
  195. break;
  196. }
  197. }
  198. }
  199. WeUtility::logging('params', var_export($hitParam, true));
  200. WeUtility::logging('response', $response);
  201. $resp = $this->account->response($response);
  202. if(!empty($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes') {
  203. $resp = $this->account->encryptMsg($resp);
  204. $resp = $this->account->xmlDetract($resp);
  205. }
  206. if($_W['debug']) {
  207. $_W['debug_data'] = array(
  208. 'resp' => $resp,
  209. 'is_default' => 0
  210. );
  211. if(count($pars_debug) == 1) {
  212. $_W['debug_data']['is_default'] = 1;
  213. $_W['debug_data']['params'] = $response_debug['params'];
  214. } else {
  215. array_pop($pars_debug);
  216. $_W['debug_data']['params'] = $pars_debug;
  217. }
  218. $_W['debug_data']['hitparam'] = $hitParam;
  219. $_W['modules']['cover'] = array('title' => '入口封面', 'name' => 'cover');
  220. load()->web('template');
  221. $process = template('utility/emulator', TEMPLATE_FETCH);
  222. echo json_encode(array('resp' => $resp, 'process' => $process));
  223. exit();
  224. }
  225. if ($resp !== 'success') {
  226. $mapping = array(
  227. '[from]' => $this->message['from'],
  228. '[to]' => $this->message['to'],
  229. '[rule]' => $this->params['rule']
  230. );
  231. $resp = str_replace(array_keys($mapping), array_values($mapping), $resp);
  232. }
  233. $reply_times_info = (array)$_SESSION['__reply_times'];
  234. if ($reply_times_info['content'] == $message['content']) {
  235. $new_times = intval($reply_times_info['times']) + 1;
  236. } else {
  237. $new_times = 1;
  238. }
  239. $_SESSION['__reply_times'] = array('content' => $message['content'], 'date' => date('Y-m-d'), 'times' => $new_times);
  240. ob_start();
  241. echo $resp;
  242. ob_start();
  243. $this->receive($hitParam, $hitKeyword, $response);
  244. ob_end_clean();
  245. exit();
  246. }
  247. WeUtility::logging('waring', 'Request Failed');
  248. exit('Request Failed');
  249. }
  250. private function isValidResponse($response) {
  251. if ($response === 'success') {
  252. return true;
  253. }
  254. if(is_array($response)) {
  255. if($response['type'] == 'text' && !empty($response['content'])) {
  256. return true;
  257. }
  258. if($response['type'] == 'news' && !empty($response['items'])) {
  259. return true;
  260. }
  261. if(!in_array($response['type'], array('text', 'news', 'image'))) {
  262. return true;
  263. }
  264. }
  265. return false;
  266. }
  267. private function booking($message) {
  268. global $_W;
  269. if ($message['event'] == 'unsubscribe' || $message['event'] == 'subscribe') {
  270. $todaystat = pdo_get('stat_fans', array('date' => date('Ymd'), 'uniacid' => $_W['uniacid']));
  271. if ($message['event'] == 'unsubscribe') {
  272. if (empty($todaystat)) {
  273. $updatestat = array(
  274. 'new' => 0,
  275. 'uniacid' => $_W['uniacid'],
  276. 'cancel' => 1,
  277. 'cumulate' => 0,
  278. 'date' => date('Ymd'),
  279. );
  280. pdo_insert('stat_fans', $updatestat);
  281. } else {
  282. $updatestat = array(
  283. 'cancel' => $todaystat['cancel'] + 1,
  284. );
  285. $updatestat['cumulate'] = 0;
  286. pdo_update('stat_fans', $updatestat, array('id' => $todaystat['id']));
  287. }
  288. } elseif ($message['event'] == 'subscribe') {
  289. if (empty($todaystat)) {
  290. $updatestat = array(
  291. 'new' => 1,
  292. 'uniacid' => $_W['uniacid'],
  293. 'cancel' => 0,
  294. 'cumulate' => 0,
  295. 'date' => date('Ymd'),
  296. );
  297. pdo_insert('stat_fans', $updatestat);
  298. } else {
  299. $updatestat = array(
  300. 'new' => $todaystat['new'] + 1,
  301. 'cumulate' => 0,
  302. );
  303. pdo_update('stat_fans', $updatestat, array('id' => $todaystat['id']));
  304. }
  305. }
  306. }
  307. load()->model('mc');
  308. $setting = uni_setting($_W['uniacid'], array('passport'));
  309. $fans = mc_fansinfo($message['from']);
  310. $default_groupid = cache_load(cache_system_key('defaultgroupid', array('uniacid' => $_W['uniacid'])));
  311. if (empty($default_groupid)) {
  312. $default_groupid = pdo_fetchcolumn('SELECT groupid FROM ' .tablename('mc_groups') . ' WHERE uniacid = :uniacid AND isdefault = 1', array(':uniacid' => $_W['uniacid']));
  313. cache_write(cache_system_key('defaultgroupid', array('uniacid' => $_W['uniacid'])), $default_groupid);
  314. }
  315. if(!empty($fans)) {
  316. if ($message['event'] == 'unsubscribe') {
  317. cache_build_memberinfo($fans['uid']);
  318. pdo_update('mc_mapping_fans', array('follow' => 0, 'unfollowtime' => TIMESTAMP), array('fanid' => $fans['fanid']));
  319. pdo_delete('mc_fans_tag_mapping', array('fanid' => $fans['fanid']));
  320. } elseif ($message['event'] != 'ShakearoundUserShake' && $message['type'] != 'trace') {
  321. $rec = array();
  322. if (empty($fans['follow'])) {
  323. $rec['follow'] = 1;
  324. $rec['followtime'] = $message['time'];
  325. }
  326. $member = array();
  327. if(!empty($fans['uid'])){
  328. $member = mc_fetch($fans['uid']);
  329. }
  330. if (empty($member)) {
  331. if (!isset($setting['passport']) || empty($setting['passport']['focusreg'])) {
  332. $data = array(
  333. 'uniacid' => $_W['uniacid'],
  334. 'email' => md5($message['from']).'@we7.cc',
  335. 'salt' => random(8),
  336. 'groupid' => $default_groupid,
  337. 'createtime' => TIMESTAMP,
  338. );
  339. $data['password'] = md5($message['from'] . $data['salt'] . $_W['config']['setting']['authkey']);
  340. pdo_insert('mc_members', $data);
  341. $rec['uid'] = pdo_insertid();
  342. }
  343. }
  344. if(!empty($rec)){
  345. pdo_update('mc_mapping_fans', $rec, array('openid' => $message['from']));
  346. }
  347. }
  348. } else {
  349. if ($message['event'] == 'subscribe' || $message['type'] == 'text' || $message['type'] == 'image') {
  350. load()->model('mc');
  351. $force_init_member = false;
  352. if (!isset($setting['passport']) || empty($setting['passport']['focusreg'])) {
  353. $force_init_member = true;
  354. }
  355. mc_init_fans_info($message['from'], $force_init_member);
  356. }
  357. }
  358. }
  359. private function receive($par, $keyword, $response) {
  360. global $_W;
  361. fastcgi_finish_request();
  362. $subscribe = cache_load(cache_system_key('module_receive_enable'));
  363. if (empty($subscribe)) {
  364. $subscribe = cache_build_module_subscribe_type();
  365. }
  366. $modules = uni_modules();
  367. $obj = WeUtility::createModuleReceiver('core');
  368. $obj->message = $this->message;
  369. $obj->params = $par;
  370. $obj->response = $response;
  371. $obj->keyword = $keyword;
  372. $obj->module = 'core';
  373. $obj->uniacid = $_W['uniacid'];
  374. $obj->acid = $_W['acid'];
  375. if(method_exists($obj, 'receive')) {
  376. @$obj->receive();
  377. }
  378. load()->func('communication');
  379. if (empty($subscribe[$this->message['type']]) && !empty($this->message['event'])) {
  380. $subscribe[$this->message['type']] = $subscribe[strtolower($this->message['event'])];
  381. }
  382. if (!empty($subscribe[$this->message['type']])) {
  383. foreach ($subscribe[$this->message['type']] as $modulename) {
  384. $params = array(
  385. 'i' => $GLOBALS['uniacid'],
  386. 'modulename' => $modulename,
  387. 'request' => json_encode($par),
  388. 'response' => json_encode($response),
  389. 'message' => json_encode($this->message),
  390. );
  391. $response = ihttp_request(wurl('utility/subscribe/receive'), $params, array(), 10);
  392. if (is_error($response) || $response['code'] != 200) {
  393. $response = ihttp_request($_W['siteroot'] . 'web/' . wurl('utility/subscribe/receive'), $params, array(), 10);
  394. }
  395. }
  396. }
  397. }
  398. private function analyze(&$message) {
  399. global $_W;
  400. $params = array();
  401. if(in_array($message['type'], array('event', 'qr'))) {
  402. $params = call_user_func_array(array($this, 'analyze' . $message['type']), array(&$message));
  403. if(!empty($params)) {
  404. return (array)$params;
  405. }
  406. }
  407. if(!empty($_SESSION['__contextmodule']) && in_array($_SESSION['__contextmodule'], $this->modules)) {
  408. if($_SESSION['__contextexpire'] > TIMESTAMP) {
  409. $params[] = array(
  410. 'message' => $message,
  411. 'module' => $_SESSION['__contextmodule'],
  412. 'rule' => $_SESSION['__contextrule'],
  413. 'priority' => $_SESSION['__contextpriority'],
  414. 'context' => true
  415. );
  416. return $params;
  417. } else {
  418. unset($_SESSION);
  419. session_destroy();
  420. }
  421. }
  422. $reply_times_info = (array)$_SESSION['__reply_times'];
  423. if (!empty($_W['account']['setting']) && !empty($reply_times_info) && intval($_W['account']['setting']['reply_setting']) > 0 && strtotime($reply_times_info['date']) >= strtotime(date('Y-m-d')) && $reply_times_info['times'] >= $_W['account']['setting']['reply_setting'] && $reply_times_info['content'] == $message['content']) {
  424. exit('success');
  425. }
  426. if(method_exists($this, 'analyze' . $message['type'])) {
  427. $temp = call_user_func_array(array($this, 'analyze' . $message['type']), array(&$message));
  428. if(!empty($temp) && is_array($temp)){
  429. $params += $temp;
  430. }
  431. } else {
  432. $params += $this->handler($message['type']);
  433. }
  434. return $params;
  435. }
  436. private function analyzeSubscribe(&$message) {
  437. global $_W;
  438. $params = array();
  439. $message['type'] = 'text';
  440. $message['redirection'] = true;
  441. if(!empty($message['scene'])) {
  442. $message['source'] = 'qr';
  443. $sceneid = trim($message['scene']);
  444. $scene_condition = '';
  445. if (is_numeric($sceneid)) {
  446. $scene_condition = " `qrcid` = '{$sceneid}'";
  447. }else{
  448. $scene_condition = " `scene_str` = '{$sceneid}'";
  449. }
  450. $qr = pdo_fetch("SELECT `id`, `keyword` FROM " . tablename('qrcode') . " WHERE {$scene_condition} AND `uniacid` = '{$_W['uniacid']}'");
  451. if(!empty($qr)) {
  452. $message['content'] = $qr['keyword'];
  453. if (!empty($qr['type']) && $qr['type'] == 'scene') {
  454. $message['msgtype'] = 'text';
  455. }
  456. $params += $this->analyzeText($message);
  457. return $params;
  458. }
  459. }
  460. $message['source'] = 'subscribe';
  461. $setting = uni_setting($_W['uniacid'], array('welcome'));
  462. if(!empty($setting['welcome'])) {
  463. $message['content'] = $setting['welcome'];
  464. $params += $this->analyzeText($message);
  465. }
  466. return $params;
  467. }
  468. private function analyzeQR(&$message) {
  469. global $_W;
  470. $params = array();
  471. $params = $this->handler($message['type']);
  472. if (!empty($params)) {
  473. return $params;
  474. }
  475. $message['type'] = 'text';
  476. $message['redirection'] = true;
  477. if(!empty($message['scene'])) {
  478. $message['source'] = 'qr';
  479. $sceneid = trim($message['scene']);
  480. $scene_condition = '';
  481. if (is_numeric($sceneid)) {
  482. $scene_condition = " `qrcid` = '{$sceneid}'";
  483. }else{
  484. $scene_condition = " `scene_str` = '{$sceneid}'";
  485. }
  486. $qr = pdo_fetch("SELECT `id`, `keyword` FROM " . tablename('qrcode') . " WHERE {$scene_condition} AND `uniacid` = '{$_W['uniacid']}'");
  487. }
  488. if (empty($qr) && !empty($message['ticket'])) {
  489. $message['source'] = 'qr';
  490. $ticket = trim($message['ticket']);
  491. if(!empty($ticket)) {
  492. $qr = pdo_fetchall("SELECT `id`, `keyword` FROM " . tablename('qrcode') . " WHERE `uniacid` = '{$_W['uniacid']}' AND ticket = '{$ticket}'");
  493. if(!empty($qr)) {
  494. if(count($qr) != 1) {
  495. $qr = array();
  496. } else {
  497. $qr = $qr[0];
  498. }
  499. }
  500. }
  501. }
  502. if(!empty($qr)) {
  503. $message['content'] = $qr['keyword'];
  504. if (!empty($qr['type']) && $qr['type'] == 'scene') {
  505. $message['msgtype'] = 'text';
  506. }
  507. $params += $this->analyzeText($message);
  508. }
  509. return $params;
  510. }
  511. public function analyzeText(&$message, $order = 0) {
  512. global $_W;
  513. $pars = array();
  514. $order = intval($order);
  515. if(!isset($message['content'])) {
  516. return $pars;
  517. }
  518. $condition = <<<EOF
  519. `uniacid` IN ( 0, {$_W['uniacid']} )
  520. AND
  521. (
  522. ( `type` = 1 AND `content` = :c1 )
  523. or
  524. ( `type` = 2 AND instr(:c2, `content`) )
  525. or
  526. ( `type` = 3 AND :c3 REGEXP `content` )
  527. or
  528. ( `type` = 4 )
  529. )
  530. AND `status`=1
  531. EOF;
  532. $params = array();
  533. $params[':c1'] = $message['content'];
  534. $params[':c2'] = $message['content'];
  535. $params[':c3'] = $message['content'];
  536. if (intval($order) > 0) {
  537. $condition .= " AND `displayorder` > :order";
  538. $params[':order'] = $order;
  539. }
  540. $keywords = reply_keywords_search($condition, $params);
  541. if(empty($keywords)) {
  542. return $pars;
  543. }
  544. foreach($keywords as $keyword) {
  545. $params = array(
  546. 'message' => $message,
  547. 'module' => $keyword['module'],
  548. 'rule' => $keyword['rid'],
  549. 'priority' => $keyword['displayorder'],
  550. 'keyword' => $keyword,
  551. 'reply_type' => $keyword['reply_type']
  552. );
  553. $pars[] = $params;
  554. }
  555. return $pars;
  556. }
  557. private function analyzeEvent(&$message) {
  558. if (strtolower($message['event']) == 'subscribe') {
  559. return $this->analyzeSubscribe($message);
  560. }
  561. if (strtolower($message['event']) == 'click') {
  562. $message['content'] = strval($message['eventkey']);
  563. return $this->analyzeClick($message);
  564. }
  565. if (in_array($message['event'], array('pic_photo_or_album', 'pic_weixin', 'pic_sysphoto'))) {
  566. pdo_delete('menu_event', array('createtime <' => $GLOBALS['_W']['timestamp'] - 100, 'openid' => $message['from']), 'OR');
  567. if (!empty($message['sendpicsinfo']['count'])) {
  568. foreach ($message['sendpicsinfo']['piclist'] as $item) {
  569. pdo_insert('menu_event', array(
  570. 'uniacid' => $GLOBALS['_W']['uniacid'],
  571. 'keyword' => $message['eventkey'],
  572. 'type' => $message['event'],
  573. 'picmd5' => $item,
  574. 'openid' => $message['from'],
  575. 'createtime' => TIMESTAMP,
  576. ));
  577. }
  578. } else {
  579. pdo_insert('menu_event', array(
  580. 'uniacid' => $GLOBALS['_W']['uniacid'],
  581. 'keyword' => $message['eventkey'],
  582. 'type' => $message['event'],
  583. 'picmd5' => $item,
  584. 'openid' => $message['from'],
  585. 'createtime' => TIMESTAMP,
  586. ));
  587. }
  588. $message['content'] = strval($message['eventkey']);
  589. $message['source'] = $message['event'];
  590. return $this->analyzeText($message);
  591. }
  592. if (!empty($message['eventkey'])) {
  593. $message['content'] = strval($message['eventkey']);
  594. $message['type'] = 'text';
  595. $message['redirection'] = true;
  596. $message['source'] = $message['event'];
  597. return $this->analyzeText($message);
  598. }
  599. return $this->handler($message['event']);
  600. }
  601. private function analyzeClick(&$message) {
  602. if(!empty($message['content']) || $message['content'] !== '') {
  603. $message['type'] = 'text';
  604. $message['redirection'] = true;
  605. $message['source'] = 'click';
  606. return $this->analyzeText($message);
  607. }
  608. return array();
  609. }
  610. private function analyzeImage(&$message) {
  611. load()->func('communication');
  612. if (!empty($message['picurl'])) {
  613. $response = ihttp_get($message['picurl']);
  614. if (!empty($response)) {
  615. $md5 = md5($response['content']);
  616. $event = pdo_get('menu_event', array('picmd5' => $md5), array('keyword', 'type'));
  617. if (!empty($event['keyword'])) {
  618. pdo_delete('menu_event', array('picmd5' => $md5));
  619. } else {
  620. $event = pdo_get('menu_event', array('openid' => $message['from']), array('keyword', 'type'));
  621. }
  622. if (!empty($event)) {
  623. $message['content'] = $event['keyword'];
  624. $message['eventkey'] = $event['keyword'];
  625. $message['type'] = 'text';
  626. $message['event'] = $event['type'];
  627. $message['redirection'] = true;
  628. $message['source'] = $event['type'];
  629. return $this->analyzeText($message);
  630. }
  631. }
  632. return $this->handler('image');
  633. }
  634. }
  635. private function analyzeVoice(&$message) {
  636. $params = $this->handler('voice');
  637. if (empty($params) && !empty($message['recognition'])) {
  638. $message['type'] = 'text';
  639. $message['redirection'] = true;
  640. $message['source'] = 'voice';
  641. $message['content'] = $message['recognition'];
  642. return $this->analyzeText($message);
  643. } else {
  644. return $params;
  645. }
  646. }
  647. private function handler($type) {
  648. if(empty($type)) {
  649. return array();
  650. }
  651. global $_W;
  652. $params = array();
  653. $setting = uni_setting($_W['uniacid'], array('default_message'));
  654. $default_message = $setting['default_message'];
  655. if(is_array($default_message) && !empty($default_message[$type]['type'])) {
  656. if ($default_message[$type]['type'] == 'keyword') {
  657. $message = $this->message;
  658. $message['type'] = 'text';
  659. $message['redirection'] = true;
  660. $message['source'] = $type;
  661. $message['content'] = $default_message[$type]['keyword'];
  662. return $this->analyzeText($message);
  663. } else {
  664. $params[] = array(
  665. 'message' => $this->message,
  666. 'module' => is_array($default_message[$type]) ? $default_message[$type]['module'] : $default_message[$type],
  667. 'rule' => '-1',
  668. );
  669. return $params;
  670. }
  671. }
  672. return array();
  673. }
  674. private function process($param) {
  675. global $_W;
  676. if(empty($param['module']) || !in_array($param['module'], $this->modules)) {
  677. return false;
  678. }
  679. if ($param['module'] == 'reply') {
  680. $processor = WeUtility::createModuleProcessor('core');
  681. } else {
  682. $processor = WeUtility::createModuleProcessor($param['module']);
  683. }
  684. $processor->message = $param['message'];
  685. $processor->rule = $param['rule'];
  686. $processor->reply_type = $param['reply_type'];
  687. $processor->priority = intval($param['priority']);
  688. $processor->inContext = $param['context'] === true;
  689. $response = $processor->respond();
  690. if(empty($response)) {
  691. return false;
  692. }
  693. return $response;
  694. }
  695. public function died($content = '') {
  696. global $_W, $engine;
  697. if (empty($content)) {
  698. exit('');
  699. }
  700. $response['FromUserName'] = $engine->message['to'];
  701. $response['ToUserName'] = $engine->message['from'];
  702. $response['MsgType'] = 'text';
  703. $response['Content'] = htmlspecialchars_decode($content);
  704. $response['CreateTime'] = TIMESTAMP;
  705. $response['FuncFlag'] = 0;
  706. $xml = array2xml($response);
  707. if(!empty($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes') {
  708. $resp = $engine->account->encryptMsg($xml);
  709. $resp = $engine->account->xmlDetract($resp);
  710. } else {
  711. $resp = $xml;
  712. }
  713. exit($resp);
  714. }
  715. }