人人商城

processor.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. class UserapiModuleProcessor extends WeModuleProcessor {
  8. private function procLocal($item) {
  9. $local = basename($item['apiurl']);
  10. $file = IA_ROOT . '/framework/builtin/userapi/api/' . $local;
  11. load()->func('communication');
  12. if (!file_exists($file)) {
  13. return array();
  14. }
  15. return include $file;
  16. }
  17. private function procRemote($item) {
  18. load()->func('communication');
  19. if (!strexists($item['apiurl'], '?')) {
  20. $item['apiurl'] .= '?';
  21. } else {
  22. $item['apiurl'] .= '&';
  23. }
  24. $sign = array(
  25. 'timestamp' => TIMESTAMP,
  26. 'nonce' => random(10, 1),
  27. );
  28. $signkey = array($item['token'], $sign['timestamp'], $sign['nonce']);
  29. sort($signkey, SORT_STRING);
  30. $sign['signature'] = sha1(implode($signkey));
  31. $item['apiurl'] .= http_build_query($sign, '', '&');
  32. $body = "<xml>" . PHP_EOL .
  33. "<ToUserName><![CDATA[{$this->message['to']}]]></ToUserName>" . PHP_EOL .
  34. "<FromUserName><![CDATA[{$this->message['from']}]]></FromUserName>" . PHP_EOL .
  35. "<CreateTime>{$this->message['time']}</CreateTime>" . PHP_EOL .
  36. "<MsgType><![CDATA[text]]></MsgType>" . PHP_EOL .
  37. "<Content><![CDATA[{$this->message['content']}]]></Content>" . PHP_EOL .
  38. "<MsgId>".TIMESTAMP."</MsgId>" . PHP_EOL .
  39. "</xml>";
  40. $response = ihttp_request($item['apiurl'], $body, array('CURLOPT_HTTPHEADER' => array('Content-Type: text/xml; charset=utf-8')));
  41. $result = array();
  42. if (!is_error($response)) {
  43. $temp = @json_decode($response['content'], true);
  44. if (is_array($temp)) {
  45. $result = $this->buildResponse($temp);
  46. } else {
  47. if (!empty($response['content'])){
  48. $obj = @isimplexml_load_string(trim($response['content']), 'SimpleXMLElement', LIBXML_NOCDATA);
  49. if($obj instanceof SimpleXMLElement) {
  50. $type = strtolower(strval($obj->MsgType));
  51. if($type == 'text') {
  52. $result = $this->respText(strval($obj->Content));
  53. }
  54. if($type == 'image') {
  55. $imid = strval($obj->Image->MediaId);
  56. $result = $this->respImage($imid);
  57. }
  58. if($type == 'voice') {
  59. $imid = strval($obj->Voice->MediaId);
  60. $result = $this->respVoice($imid);
  61. }
  62. if($type == 'video') {
  63. $video = array();
  64. $video['video'] = strval($obj->Video->MediaId);
  65. $video['thumb'] = strval($obj->Video->ThumbMediaId);
  66. $result = $this->respVideo($video);
  67. }
  68. if($type == 'music') {
  69. $music = array();
  70. $music['title'] = strval($obj->Music->Title);
  71. $music['description'] = strval($obj->Music->Description);
  72. $music['musicurl'] = strval($obj->Music->MusicUrl);
  73. $music['hqmusicurl'] = strval($obj->Music->HQMusicUrl);
  74. $result = $this->respMusic($music);
  75. }
  76. if($type == 'news') {
  77. $news = array();
  78. foreach($obj->Articles->item as $item) {
  79. $news[] = array(
  80. 'title' => strval($item->Title),
  81. 'description' => strval($item->Description),
  82. 'picurl' => strval($item->PicUrl),
  83. 'url' => strval($item->Url)
  84. );
  85. }
  86. $result = $this->respNews($news);
  87. }
  88. }
  89. }
  90. }
  91. if(@stristr($result, '{begin-context}') !== false) {
  92. $this->beginContext(0);
  93. $result = str_ireplace('{begin-context}', '', $result);
  94. }
  95. if(@stristr($result, '{end-context}') !== false) {
  96. $this->endContext();
  97. $result = str_ireplace('{end-context}', '', $result);
  98. }
  99. return $result;
  100. } else {
  101. return array();
  102. }
  103. }
  104. public function respond() {
  105. global $_W;
  106. $rid = $this->rule;
  107. $rule = pdo_fetch('SELECT * FROM ' . tablename('rule') . ' WHERE `id`=:rid', array(':rid' => $rid));
  108. $cfg = $this->module['config'];
  109. if($rule['uniacid'] == '0' && empty($cfg[$rid])) {
  110. return '';
  111. }
  112. if($this->inContext) {
  113. $rid = $_SESSION['__userapi-rid'];
  114. }
  115. $item = array();
  116. if (!empty($rid)) {
  117. $sql = "SELECT * FROM " . tablename('userapi_reply') . " WHERE `rid`=:rid ORDER BY id DESC limit 1";
  118. $item = pdo_fetch($sql, array(':rid' => $rid));
  119. if (empty($item['id'])) {
  120. return array();
  121. }
  122. }
  123. if(empty($item)) {
  124. $module = $_W['modules']['userapi'];
  125. $module['settings'] = iunserializer($module['settings']);
  126. $item['apiurl'] = $module['settings']['apiurl'];
  127. $item['default-text'] = $module['settings']['default'];
  128. }
  129. if ($item['cachetime'] > 0) {
  130. $key = md5($item['id'].$this->message['from']);
  131. $cache = pdo_fetch("SELECT * FROM " . tablename('userapi_cache') . " WHERE `key` = '$key' LIMIT 1");
  132. if (!empty($cache) && TIMESTAMP - $cache['lastupdate'] <= $item['cachetime']) {
  133. return iunserializer($cache['content']);
  134. }
  135. }
  136. $result = array();
  137. if (!strexists($item['apiurl'], 'http://') && !strexists($item['apiurl'], 'https://')) {
  138. $result = $this->procLocal($item);
  139. } else {
  140. $result = $this->procRemote($item);
  141. }
  142. if(empty($result) && !empty($item['default_text'])) {
  143. $result = $this->respText($item['default_text']);
  144. }
  145. if (!empty($result) && is_array($result)) {
  146. $result['FromUserName'] = $this->message['to'];
  147. $result['ToUserName'] = $this->message['from'];
  148. if ($item['cachetime'] > 0) {
  149. if (empty($cache)) {
  150. pdo_insert('userapi_cache', array('key' => $key, 'content' => iserializer($result), 'lastupdate' => TIMESTAMP));
  151. } else {
  152. pdo_update('userapi_cache', array('content' => iserializer($result), 'lastupdate' => TIMESTAMP), array('key' => $key));
  153. }
  154. }
  155. }
  156. return $result;
  157. }
  158. private function buildResponse($data = array()) {
  159. $result = array();
  160. $result['MsgType'] = $data['type'];
  161. $data = $data['content'];
  162. if ($result['MsgType'] == 'text') {
  163. $result['Content'] = $data;
  164. } elseif ($result['MsgType'] == 'news') {
  165. $result['ArticleCount'] = $data['ArticleCount'];
  166. $result['Articles'] = array();
  167. if (!isset($data[0])) {
  168. $temp[0] = $data;
  169. $data = $temp;
  170. }
  171. foreach ($data as $row) {
  172. $result['Articles'][] = array(
  173. 'Title' => $row['Title'],
  174. 'Description' => $row['Description'],
  175. 'PicUrl' => $row['PicUrl'],
  176. 'Url' => $row['Url'],
  177. 'TagName' => 'item',
  178. );
  179. }
  180. } elseif ($result['MsgType'] == 'music') {
  181. $result['Music'] = array(
  182. 'Title' => $data['Title'],
  183. 'Description' => $data['Description'],
  184. 'MusicUrl' => $data['MusicUrl'],
  185. 'HQMusicUrl' => $data['HQMusicUrl'],
  186. );
  187. }
  188. return $result;
  189. }
  190. protected function beginContext($expire = 3600) {
  191. if(!$this->inContext) {
  192. $_SESSION['__userapi-rid'] = $this->rule;
  193. parent::beginContext($expire);
  194. }
  195. }
  196. }