processor.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 ('text' == $type) {
  52. $result = $this->respText(strval($obj->Content));
  53. }
  54. if ('image' == $type) {
  55. $imid = strval($obj->Image->MediaId);
  56. $result = $this->respImage($imid);
  57. }
  58. if ('voice' == $type) {
  59. $imid = strval($obj->Voice->MediaId);
  60. $result = $this->respVoice($imid);
  61. }
  62. if ('video' == $type) {
  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 ('music' == $type) {
  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 ('news' == $type) {
  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 (false !== @stristr($result, '{begin-context}')) {
  92. $this->beginContext(0);
  93. $result = str_ireplace('{begin-context}', '', $result);
  94. }
  95. if (false !== @stristr($result, '{end-context}')) {
  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 = table('rule')->getById($rid, $_W['uniacid']);
  108. $cfg = $this->module['config'];
  109. if ('0' == $rule['uniacid'] && empty($cfg[$rid])) {
  110. return '';
  111. }
  112. if ($this->inContext) {
  113. $rid = $_SESSION['__userapi-rid'];
  114. }
  115. $item = array();
  116. if (!empty($rid)) {
  117. $item = table('userapi_reply')->where(array('rid' => $rid))->orderby(array('id' => 'DESC'))->get();
  118. if (empty($item['id'])) {
  119. return array();
  120. }
  121. }
  122. if (empty($item)) {
  123. $module = $_W['modules']['userapi'];
  124. $module['settings'] = iunserializer($module['settings']);
  125. $item['apiurl'] = $module['settings']['apiurl'];
  126. $item['default-text'] = $module['settings']['default'];
  127. }
  128. if ($item['cachetime'] > 0) {
  129. $key = md5($item['id'] . $this->message['from']);
  130. $cache = table('userapi_cache')->where('key', $key)->get();
  131. if (!empty($cache) && TIMESTAMP - $cache['lastupdate'] <= $item['cachetime']) {
  132. return iunserializer($cache['content']);
  133. }
  134. }
  135. $result = array();
  136. if (!strexists($item['apiurl'], 'http://') && !strexists($item['apiurl'], 'https://')) {
  137. $result = $this->procLocal($item);
  138. } else {
  139. $result = $this->procRemote($item);
  140. }
  141. if (empty($result) && !empty($item['default_text'])) {
  142. $result = $this->respText($item['default_text']);
  143. }
  144. if (!empty($result) && is_array($result)) {
  145. $result['FromUserName'] = $this->message['to'];
  146. $result['ToUserName'] = $this->message['from'];
  147. if ($item['cachetime'] > 0) {
  148. if (empty($cache)) {
  149. table('userapi_cache')
  150. ->fill(array(
  151. 'key' => $key,
  152. 'content' => iserializer($result),
  153. 'lastupdate' => TIMESTAMP
  154. ))
  155. ->save();
  156. } else {
  157. table('userapi_cache')
  158. ->where(array('key' => $key))
  159. ->fill(array(
  160. 'content' => iserializer($result),
  161. 'lastupdate' => TIMESTAMP
  162. ))
  163. ->save();
  164. }
  165. }
  166. }
  167. return $result;
  168. }
  169. private function buildResponse($data = array()) {
  170. $result = array();
  171. $result['MsgType'] = $data['type'];
  172. $data = $data['content'];
  173. if ('text' == $result['MsgType']) {
  174. $result['Content'] = $data;
  175. } elseif ('news' == $result['MsgType']) {
  176. $result['ArticleCount'] = $data['ArticleCount'];
  177. $result['Articles'] = array();
  178. if (!isset($data[0])) {
  179. $temp[0] = $data;
  180. $data = $temp;
  181. }
  182. foreach ($data as $row) {
  183. $result['Articles'][] = array(
  184. 'Title' => $row['Title'],
  185. 'Description' => $row['Description'],
  186. 'PicUrl' => $row['PicUrl'],
  187. 'Url' => $row['Url'],
  188. 'TagName' => 'item',
  189. );
  190. }
  191. } elseif ('music' == $result['MsgType']) {
  192. $result['Music'] = array(
  193. 'Title' => $data['Title'],
  194. 'Description' => $data['Description'],
  195. 'MusicUrl' => $data['MusicUrl'],
  196. 'HQMusicUrl' => $data['HQMusicUrl'],
  197. );
  198. }
  199. return $result;
  200. }
  201. protected function beginContext($expire = 3600) {
  202. if (!$this->inContext) {
  203. $_SESSION['__userapi-rid'] = $this->rule;
  204. parent::beginContext($expire);
  205. }
  206. }
  207. }