wechat.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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: wechat.inc.php 35958 2016-05-24 02:34:37Z nemohou $
  7. */
  8. if (!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. $_G['wechat']['setting'] = unserialize($_G['setting']['mobilewechat']);
  12. define('IN_WECHAT', strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false);
  13. require_once DISCUZ_ROOT . './source/plugin/wechat/wechat.lib.class.php';
  14. require_once DISCUZ_ROOT . './source/plugin/wechat/wechat.class.php';
  15. require_once DISCUZ_ROOT . './source/plugin/wechat/wsq.class.php';
  16. require_once DISCUZ_ROOT . './source/discuz_version.php';
  17. list($openid, $sid) = explode("\t", authcode(base64_decode($_GET['key']), 'DECODE', $_G['config']['security']['authkey']));
  18. $keyenc = urlencode($_GET['key']);
  19. $ac = $_GET['ac'] ? $_GET['ac'] : 'bind';
  20. if($_GET['fromapp'] == 'index') {
  21. $op = 'access';
  22. } else {
  23. $op = $_GET['op'];
  24. }
  25. $preferer = parse_url($_GET['referer']);
  26. if(!$preferer['host'] || $preferer['host'] != 'wsq.discuz.com' && $preferer['host'] != 'wsq.discuz.qq.com') {
  27. $_GET['referer'] = '';
  28. }
  29. $selfurl = $_G['siteurl'].'plugin.php?id=wechat&mobile=2&key='.$keyenc.($_GET['referer'] ? '&referer='.urlencode($_GET['referer']) : '').($_GET['username'] ? '&username='.urlencode($_GET['username']) : '').'&ac=';
  30. if(!$_G['wechat']['setting']['wechat_qrtype'] && IN_WECHAT && !$openid) {
  31. if($_G['wechat']['setting']['wechat_mtype'] != 2) {
  32. if(!empty($_G['cookie']['wechatopenid'])) {
  33. $openid = authcode($_G['cookie']['wechatopenid'], 'DECODE', $_G['config']['security']['authkey']);
  34. }
  35. if(!$openid) {
  36. showmessage('wechat:wechat_undefined');
  37. }
  38. } else {
  39. $wechat_client = new WeChatClient($_G['wechat']['setting']['wechat_appId'], $_G['wechat']['setting']['wechat_appsecret']);
  40. $openid = !empty($_G['cookie']['wechatopenid']) ? authcode($_G['cookie']['wechatopenid'], 'DECODE', $_G['config']['security']['authkey']) : '';
  41. if(!$openid) {
  42. if(empty($_GET['oauth'])) {
  43. $redirect_uri = $wechat_client->getOauthConnectUri($selfurl.$ac.'&oauth=yes');
  44. dheader('location: '.$redirect_uri);
  45. } else {
  46. $tockeninfo = $wechat_client->getAccessTokenByCode($_GET['code']);
  47. $openid = $tockeninfo['openid'];
  48. dsetcookie('wechatopenid', authcode($openid, 'ENCODE', $_G['config']['security']['authkey']), 86400);
  49. }
  50. }
  51. }
  52. } elseif($openid) {
  53. dsetcookie('wechatopenid', authcode($openid, 'ENCODE', $_G['config']['security']['authkey']), 86400);
  54. }
  55. wsq::report('loginevent');
  56. require_once libfile('function/member');
  57. if($openid) {
  58. if($ac == 'qqbind') {
  59. WeChatHook::bindOpenId($_G['uid'], $openid);
  60. wsq::report('bind');
  61. $ac = 'bind';
  62. }
  63. $wechatuser = C::t('#wechat#common_member_wechat')->fetch_by_openid($openid);
  64. if(!$wechatuser) {
  65. if($_G['uid']) {
  66. clearcookies();
  67. dheader('location: '. $selfurl.$ac);
  68. }
  69. if($_G['wechat']['setting']['wechat_allowregister'] && $_G['wechat']['setting']['wechat_allowfastregister'] && $_G['wechat']['setting']['wechat_mtype'] == 2) {
  70. $authcode = C::t('#wechat#mobile_wechat_authcode')->fetch($sid);
  71. $uid = WeChat::register(WeChat::getnewname($openid), 1);
  72. if($uid) {
  73. WeChatHook::bindOpenId($uid, $openid, 1);
  74. if($sid) {
  75. C::t('#wechat#mobile_wechat_authcode')->update($sid, array('uid' => $uid, 'status' => 1));
  76. }
  77. }
  78. wsq::report('register');
  79. }
  80. }
  81. }
  82. if($op == 'access') {
  83. $redirect = WeChat::redirect();
  84. if($redirect) {
  85. dheader('location: '.$redirect);
  86. }
  87. }
  88. if($sid) {
  89. $authcode = C::t('#wechat#mobile_wechat_authcode')->fetch($sid);
  90. if($authcode) {
  91. if($_GET['confirm'] == 'delete') {
  92. C::t('#wechat#mobile_wechat_authcode')->delete($authcode['sid']);
  93. wechat_setloginstatus($authcode['uid'], false);
  94. include template('wechat:wechat_bind_confirm');
  95. exit;
  96. }
  97. if($wechatuser && !$authcode['uid']) {
  98. $member = getuserbyuid($wechatuser['uid'], 1);
  99. if(empty($_GET['confirm']) && (!$_G['wechat']['setting']['wechat_confirmtype'] && $member['adminid'] > 0 || $_G['wechat']['setting']['wechat_confirmtype'] == 1)) {
  100. wsq::report('showauthorized');
  101. include template('wechat:wechat_bind_confirm');
  102. exit;
  103. }
  104. setloginstatus($member, 1296000);
  105. C::t('#wechat#mobile_wechat_authcode')->update($sid, array('uid' => $wechatuser['uid'], 'status' => 1));
  106. wechat_setloginstatus($wechatuser['uid'], true);
  107. wsq::report('authorized');
  108. } elseif($authcode['uid']) {
  109. $member = getuserbyuid($authcode['uid'], 1);
  110. if(empty($_GET['confirm']) && (!$_G['wechat']['setting']['wechat_confirmtype'] && $member['adminid'] > 0 || $_G['wechat']['setting']['wechat_confirmtype'] == 1)) {
  111. wsq::report('showauthorized');
  112. include template('wechat:wechat_bind_confirm');
  113. exit;
  114. }
  115. if($wechatuser) {
  116. C::t('#wechat#common_member_wechat')->delete($wechatuser['uid']);
  117. wsq::report('unbind');
  118. }
  119. setloginstatus($member, 1296000);
  120. C::t('#wechat#mobile_wechat_authcode')->update($sid, array('status' => 1));
  121. WeChatHook::bindOpenId($authcode['uid'], $openid);
  122. wsq::report('bind');
  123. $wechatuser = C::t('#wechat#common_member_wechat')->fetch_by_openid($openid);
  124. wechat_setloginstatus($authcode['uid'], true);
  125. wsq::report('authorized');
  126. }
  127. }
  128. } elseif($wechatuser) {
  129. $member = getuserbyuid($wechatuser['uid'], 1);
  130. setloginstatus($member, 1296000);
  131. wechat_setloginstatus($wechatuser['uid'], true);
  132. }
  133. if($ac == 'bind' && $_G['wechat']['setting']['wechat_qrtype']) {
  134. if(!$_G['uid'] && IN_WECHAT && $_G['wechat']['setting']['wechat_allowfastregister']) {
  135. $ac = 'wxregister';
  136. }
  137. list($_GET['username'], $wxopenid) = explode("\t", base64_decode($_GET['username']));
  138. $_GET['username'] = substr(WeChatEmoji::clear($_GET['username']), 0, 15);
  139. }
  140. if($ac == 'bind') {
  141. define('IN_MOBILE', 2);
  142. if($_G['wechat']['setting']['wechat_qrtype'] && $_GET['referer']) {
  143. $_GET['referer'] = str_replace('&state=siteregister', '&state=backlogin', $_GET['referer']);
  144. dheader('location: '.$_GET['referer']);
  145. }
  146. if($_G['wechat']['setting']['wechat_mtype'] == 2) {
  147. $defaultusername = WeChat::getnewname($openid);
  148. } else {
  149. $defaultusername = $_G['wechat']['setting']['wechat_qrtype'] ? $_GET['username'] : 'wx_'.random(5);
  150. }
  151. $defaultusername = htmlspecialchars($defaultusername);
  152. $connecturl = $_G['setting']['connect']['allow'] && !$_G['setting']['bbclosed'] ? $_G['siteurl'].'connect.php?mod=login&op=init&referer='.urlencode($selfurl.'qqbind').'&statfrom=login_simple' : '';
  153. if(IN_WECHAT) {
  154. if(!$_G['uid']) {
  155. include template('wechat:wechat_bind');
  156. } else {
  157. $redirect = WeChat::redirect();
  158. if($redirect) {
  159. dheader('location: '.$redirect);
  160. } else {
  161. dheader('location: '.$_G['siteurl']);
  162. }
  163. }
  164. } else {
  165. dheader('location: '.$_G['siteurl'].'member.php?mod=logging&action=login&referer='.dreferer());
  166. }
  167. } elseif($ac == 'login' && submitcheck('submit')) {
  168. if(!($loginperm = logincheck($_GET['username']))) {
  169. showmessage('login_strike');
  170. }
  171. if(!$_GET['password'] || $_GET['password'] != addslashes($_GET['password'])) {
  172. showmessage('profile_passwd_illegal');
  173. }
  174. if(DISCUZ_VERSION < 'X3.0') {
  175. $_GET['username'] = WeChatEmoji::clear($_GET['username']);
  176. }
  177. $result = userlogin($_GET['username'], $_GET['password'], $_GET['questionid'], $_GET['answer'], $_G['setting']['autoidselect'] ? 'auto' : $_GET['loginfield'], $_G['clientip']);
  178. if($result['status'] <= 0) {
  179. loginfailed($_GET['username']);
  180. failedip();
  181. showmessage('login_invalid', '', array('loginperm' => $loginperm - 1));
  182. }
  183. if(!$_G['wechat']['setting']['wechat_qrtype']) {
  184. if($wechatuser) {
  185. if($result['member']['uid'] != $wechatuser['uid']) {
  186. showmessage('wechat:wechat_openid_exists');
  187. }
  188. wechat_setloginstatus($result['member']['uid'], true);
  189. } else {
  190. WeChatHook::bindOpenId($result['member']['uid'], $openid);
  191. wsq::report('bind');
  192. }
  193. setloginstatus($result['member'], 1296000);
  194. showmessage('wechat:wechat_member_bind_succeed', $selfurl.'bind');
  195. } else {
  196. C::t('#wechat#common_member_wechatmp')->insert(array('uid' => $result['member']['uid'], 'openid' => $_GET['wxopenid'], 'status' => 1), false, true);
  197. wsq::report('bind');
  198. $url = wsq::wxuserregisterUrl($result['member']['uid']);
  199. showmessage('wechat:wechat_member_bind_succeed', $url);
  200. }
  201. } elseif(($ac == 'register' && submitcheck('submit') || $ac == 'wxregister') && $_G['wechat']['setting']['wechat_allowregister']) {
  202. if($wechatuser) {
  203. showmessage('wechat:wechat_openid_exists');
  204. } else {
  205. if($_G['wechat']['setting']['wechat_qrtype']) {
  206. $mpmember = C::t('#wechat#common_member_wechatmp')->fetch_by_openid($wxopenid ? $wxopenid : $_GET['wxopenid']);
  207. $mpmembers = C::t('common_member')->fetch_all(array_keys($mpmember));
  208. if ($mpmembers) {
  209. $memberfirst = array_shift($mpmembers);
  210. $member = getuserbyuid($memberfirst['uid'], 1);
  211. if($member) {
  212. setloginstatus($member, 1296000);
  213. $url = wsq::wxuserregisterUrl($memberfirst['uid']);
  214. if ($ac == 'wxregister') {
  215. dheader('location: ' . $url);
  216. } else {
  217. showmessage('wechat:wechat_member_register_succeed', $url);
  218. }
  219. }
  220. }
  221. }
  222. if(DISCUZ_VERSION < 'X3.0' && $_G['inajax']) {
  223. $_GET['username'] = WeChatEmoji::clear($_GET['username']);
  224. }
  225. if($ac == 'wxregister') {
  226. loaducenter();
  227. $user = uc_get_user($_GET['username']);
  228. if(!empty($user)) {
  229. $_GET['username'] = cutstr($_GET['username'], 7, '').'_'.random(5);
  230. }
  231. }
  232. $uid = WeChat::register($_GET['username'], $ac == 'wxregister');
  233. if($uid && $_GET['avatar']) {
  234. WeChat::syncAvatar($uid, $_GET['avatar']);
  235. }
  236. if(!$_G['wechat']['setting']['wechat_qrtype']) {
  237. WeChatHook::bindOpenId($uid, $openid, 1);
  238. wsq::report('register');
  239. showmessage('wechat:wechat_member_register_succeed', $selfurl.'bind&confirm=yes');
  240. } else {
  241. C::t('#wechat#common_member_wechatmp')->insert(array('uid' => $uid, 'openid' => $wxopenid ? $wxopenid : $_GET['wxopenid'], 'status' => 0), false, true);
  242. wsq::report('register');
  243. $url = wsq::wxuserregisterUrl($uid);
  244. if($ac == 'wxregister') {
  245. dheader('location: '.$url);
  246. } else {
  247. showmessage('wechat:wechat_member_register_succeed', $url);
  248. }
  249. }
  250. }
  251. } elseif($ac == 'logout') {
  252. if($_GET['hash'] == formhash()) {
  253. wechat_setloginstatus($_G['uid'], false);
  254. clearcookies();
  255. }
  256. mobile_core::result(array());
  257. } elseif($ac == 'unbind') {
  258. if($_GET['hash'] == formhash()) {
  259. if($wechatuser) {
  260. C::t('#wechat#common_member_wechat')->delete($wechatuser['uid']);
  261. wsq::report('unbind');
  262. }
  263. clearcookies();
  264. }
  265. mobile_core::result(array());
  266. } elseif($ac == 'unbindmp') {
  267. if($_G['wechat']['setting']['wechat_qrtype'] && $_GET['hash'] == formhash()) {
  268. C::t('#wechat#common_member_wechatmp')->delete($_GET['uid']);
  269. }
  270. mobile_core::result(array());
  271. } else {
  272. showmessage('undefined_action');
  273. }
  274. function wechat_setloginstatus($uid, $login) {
  275. C::t('#wechat#common_member_wechat')->update($uid, array('status' => $login ? 2 : 1));
  276. }