response.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: response.class.php 35121 2014-11-27 06:21:38Z nemohou $
  7. */
  8. if (!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. require_once DISCUZ_ROOT . './source/plugin/wechat/wsq.class.php';
  12. class WSQResponse {
  13. private static $expire = 1296000;
  14. public static $keyword = 'LOGIN_WSQ';
  15. public static function text($param) {
  16. list($data) = $param;
  17. self::_init();
  18. global $_G;
  19. $data['content'] = diconv($data['content'], 'UTF-8');
  20. $isloginkeyword = self::_custom('text', $data['content']);
  21. if(!$_G['wechat']['setting']['wsq_allow']) {
  22. return;
  23. }
  24. $authcode = C::t('#wechat#mobile_wechat_authcode')->fetch_by_code($data['content']);
  25. if(!$authcode || $authcode['status']) {
  26. if($isloginkeyword) {
  27. wsq::report('loginclick');
  28. self::_show('access', $data['from']);
  29. }
  30. } else {
  31. wsq::report('sendnum');
  32. self::_show('sendnum', $data['from']."\t".$authcode['sid'], 60);
  33. }
  34. }
  35. public static function click($param) {
  36. list($data) = $param;
  37. self::_init();
  38. global $_G;
  39. if(!$_G['wechat']['setting']['wsq_allow']) {
  40. return;
  41. }
  42. if($data['key'] == self::$keyword) {
  43. wsq::report('loginclick');
  44. self::_show('access', $data['from']);
  45. } else {
  46. self::_custom('text', $data['key']);
  47. }
  48. }
  49. public static function custom($param) {
  50. self::_init();
  51. global $_G;
  52. if(!$_G['wechat']['setting']['wsq_allow']) {
  53. return;
  54. }
  55. self::_custom('subscribe');
  56. }
  57. public static function subscribe($param) {
  58. list($data) = $param;
  59. self::_init();
  60. global $_G;
  61. if(!$_G['wechat']['setting']['wsq_allow']) {
  62. return;
  63. }
  64. if($data['key']) {
  65. self::scan($param);
  66. } else {
  67. self::_show('access', $data['from']);
  68. }
  69. }
  70. public static function scan($param) {
  71. list($data) = $param;
  72. self::_init();
  73. global $_G;
  74. if(!$_G['wechat']['setting']['wsq_allow']) {
  75. return;
  76. }
  77. $authcode = C::t('#wechat#mobile_wechat_authcode')->fetch_by_code($data['key']);
  78. if(!$authcode || $authcode['status']) {
  79. } else {
  80. if($authcode['uid']) {
  81. $member = getuserbyuid($authcode['uid'], 1);
  82. if($member['adminid'] == 0 && !$_G['wechat']['setting']['wechat_confirmtype']) {
  83. C::t('#wechat#mobile_wechat_authcode')->update($authcode['sid'], array('uid' => $member['uid'], 'status' => 1));
  84. $authcode['sid'] = '';
  85. }
  86. } else {
  87. $wechatuser = C::t('#wechat#common_member_wechat')->fetch_by_openid($data['from']);
  88. if($wechatuser) {
  89. $member = getuserbyuid($wechatuser['uid'], 1);
  90. if($member['adminid'] == 0 && !$_G['wechat']['setting']['wechat_confirmtype']) {
  91. C::t('#wechat#mobile_wechat_authcode')->update($authcode['sid'], array('uid' => $member['uid'], 'status' => 1));
  92. $authcode['sid'] = '';
  93. }
  94. } elseif($_G['wechat']['setting']['wechat_allowregister'] && $_G['wechat']['setting']['wechat_allowfastregister'] && $_G['wechat']['setting']['wechat_mtype'] == 2) {
  95. require_once DISCUZ_ROOT . './source/plugin/wechat/wechat.class.php';
  96. require_once libfile('function/member');
  97. $uid = WeChat::register(WeChat::getnewname($data['from']), 1);
  98. if($uid) {
  99. WeChatHook::bindOpenId($uid, $data['from'], 1);
  100. C::t('#wechat#mobile_wechat_authcode')->update($authcode['sid'], array('uid' => $uid, 'status' => 1));
  101. }
  102. wsq::report('register');
  103. $authcode['sid'] = '';
  104. }
  105. }
  106. wsq::report('scanqr');
  107. self::_show('scan', $data['from']."\t".$authcode['sid']);
  108. }
  109. }
  110. public static function redirect($type) {
  111. self::_init();
  112. global $_G;
  113. if(!$_G['wechat']['setting']['wsq_allow']) {
  114. return;
  115. }
  116. if($_G['wechat']['setting']['wsq_siteid'] && !defined('IN_MOBILE_API')) {
  117. $_G['wechat']['setting']['wsq_wapdefault'] = !self::_checkrobot() ? $_G['wechat']['setting']['wsq_wapdefault'] : false;
  118. $in_wechat = $_G['wechat']['setting']['wsq_wapdefault'] ? true : strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false;
  119. $fromwap = $_G['wechat']['setting']['wsq_wapdefault'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') === false;
  120. $url = wsq::$WSQ_DOMAIN.'siteid='.$_G['wechat']['setting']['wsq_siteid'].($fromwap ? '&source=wap' : '').'&c=index&a=';
  121. if($type) {
  122. $modid = $_G['basescript'].'::'.CURMODULE;
  123. if($in_wechat) {
  124. if(($modid == 'forum::viewthread' || $modid == 'group::viewthread') && !empty($_GET['tid'])) {
  125. dheader('location: '.$url.'viewthread&tid='.$_GET['tid']);
  126. } elseif(($modid == 'forum::forumdisplay' || $modid == 'group::forumdisplay') && !empty($_GET['fid'])) {
  127. dheader('location: '.$url.'index&fid='.$_GET['fid']);
  128. } elseif($modid == 'forum::index') {
  129. dheader('location: '.$url.'index');
  130. }
  131. }
  132. } else {
  133. if(isset($_GET['referer'])) {
  134. return $_GET['referer'];
  135. } elseif(isset($_GET['pluginid'])) {
  136. return $url.'plugin&pluginid='.urlencode($_GET['pluginid']).'&param='.urlencode($_GET['param']);
  137. } else {
  138. return $url.'index';
  139. }
  140. }
  141. }
  142. }
  143. private static function _show($messagekey, $key, $expire = 0) {
  144. global $_G;
  145. if(!$_G['wechat']['setting']['wsq_allow']) {
  146. return;
  147. }
  148. $expire = $expire ? $expire : self::$expire;
  149. $key = authcode($key, 'ENCODE', $_G['config']['security']['authkey'], $expire);
  150. $url = $_G['siteurl'] . 'plugin.php?mobile=2&id=wechat&op='.$messagekey.'&key=' . urlencode(base64_encode($key));
  151. $param = array('bbname' => $_G['wechat']['setting']['wsq_sitename'], 'date' => dgmdate(TIMESTAMP + $expire, 'Y-m-d'));
  152. loadcache('wechat_response');
  153. $desc = !empty($_G['cache']['wechat_response'][$messagekey]) ? $_G['cache']['wechat_response'][$messagekey] : 'wechat_response_text_' . $messagekey;
  154. $list = array(array(
  155. 'title' => lang('plugin/wechat', 'wechat_response_text_title', $param),
  156. 'desc' => lang('plugin/wechat', $desc, $param),
  157. 'url' => $url
  158. ));
  159. echo WeChatServer::getXml4RichMsgByArray($list);
  160. exit;
  161. }
  162. private static function _custom($type, $keyword = '') {
  163. global $_G;
  164. loadcache('wechat_response');
  165. $response = & $_G['cache']['wechat_response'];
  166. $query = $type == 'text' ? $response['query']['text'][$keyword] : $response['query']['subscribe'];
  167. if($query) {
  168. if($query == self::$keyword) {
  169. return 1;
  170. }
  171. if(preg_match("/^\[resource=(\d+)\]/", $query, $r)) {
  172. $resource = C::t('#wechat#mobile_wechat_resource')->fetch($r[1]);
  173. if(!$resource['type']) {
  174. $list = array(array(
  175. 'title' => $resource['data']['title'],
  176. 'desc' => $resource['data']['desc'],
  177. 'pic' => $resource['data']['pic'],
  178. 'url' => $resource['data']['url'],
  179. ));
  180. } else {
  181. $mergeids = array_keys($resource['data']['mergeids']);
  182. $sresource = C::t('#wechat#mobile_wechat_resource')->fetch_all($mergeids);
  183. $list = array();
  184. foreach($resource['data']['mergeids'] as $id => $order) {
  185. $list[] = array(
  186. 'title' => $sresource[$id]['data']['title'],
  187. 'desc' => $sresource[$id]['data']['desc'],
  188. 'pic' => $sresource[$id]['data']['pic'],
  189. 'url' => $sresource[$id]['data']['url'],
  190. );
  191. }
  192. }
  193. echo WeChatServer::getXml4RichMsgByArray($list);
  194. exit;
  195. } else {
  196. echo WeChatServer::getXml4Txt($query);
  197. }
  198. exit;
  199. }
  200. return 0;
  201. }
  202. public static function masssendFinish($param) {
  203. list($data) = $param;
  204. if(!$data['msg_id']) {
  205. exit;
  206. }
  207. $updatedata = array(
  208. 'res_status' => $data['status'],
  209. 'res_totalcount' => $data['totalcount'],
  210. 'res_filtercount' => $data['filtercount'],
  211. 'res_sentcount' => $data['sentcount'],
  212. 'res_errorcount' => $data['errorcount'],
  213. 'res_finish_at' => $data['time']
  214. );
  215. DB::update('mobile_wechat_masssend', $updatedata, "msg_id='$data[msg_id]'");
  216. }
  217. private static function _checkrobot() {
  218. return IS_ROBOT || strpos($_SERVER['HTTP_USER_AGENT'], 'spi_der') !== false;
  219. }
  220. private static function _init() {
  221. global $_G;
  222. if(!$_G['wechat']['setting']) {
  223. $_G['wechat']['setting'] = unserialize($_G['setting']['mobilewechat']);
  224. }
  225. }
  226. }