session.ctrl.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. load()->model('mc');
  8. $dos = array('openid', 'userinfo', 'check');
  9. $do = in_array($do, $dos) ? $do : 'openid';
  10. $account_api = WeAccount::createByUniacid();
  11. if ($do == 'openid') {
  12. $code = $_GPC['code'];
  13. $openid = $_GPC['openid'];
  14. if (empty($openid) && !empty($_W['openid'])) {
  15. $openid = $_W['openid'];
  16. }
  17. if (empty($_W['account']['oauth']) || (empty($code) && empty($openid))) {
  18. exit('通信错误,请在微信中重新发起请求');
  19. }
  20. if (!empty($openid)) {
  21. $_SESSION['openid'] = $oauth['openid'];
  22. $fans = mc_fansinfo($openid);
  23. if (!empty($fans)) {
  24. $account_api->result(0, '', array('sessionid' => $_W['session_id'], 'userinfo' => $fans));
  25. } else {
  26. $account_api->result(1, 'openid不存在');
  27. }
  28. }
  29. $oauth = $account_api->getOauthInfo($code);
  30. if (!empty($oauth) && !is_error($oauth)) {
  31. $_SESSION['openid'] = $oauth['openid'];
  32. $_SESSION['session_key'] = $oauth['session_key'];
  33. $fans = mc_fansinfo($oauth['openid']);
  34. if (empty($fans)) {
  35. $record = array(
  36. 'openid' => $oauth['openid'],
  37. 'unionid' => $oauth['unionid'],
  38. 'uid' => 0,
  39. 'acid' => $_W['acid'],
  40. 'uniacid' => $_W['uniacid'],
  41. 'salt' => random(8),
  42. 'updatetime' => TIMESTAMP,
  43. 'nickname' => '',
  44. 'follow' => '1',
  45. 'followtime' => TIMESTAMP,
  46. 'unfollowtime' => 0,
  47. 'tag' => '',
  48. );
  49. $email = md5($oauth['openid']).'@we7.cc';
  50. $email_exists_member = pdo_getcolumn('mc_members', array('email' => $email, 'uniacid' => $_W['uniacid']), 'uid');
  51. if (!empty($email_exists_member)) {
  52. $uid = $email_exists_member;
  53. } else {
  54. $default_groupid = pdo_fetchcolumn('SELECT groupid FROM ' .tablename('mc_groups') . ' WHERE uniacid = :uniacid AND isdefault = 1', array(':uniacid' => $_W['uniacid']));
  55. $data = array(
  56. 'uniacid' => $_W['uniacid'],
  57. 'email' => $email,
  58. 'salt' => random(8),
  59. 'groupid' => $default_groupid,
  60. 'createtime' => TIMESTAMP,
  61. 'password' => md5($message['from'] . $data['salt'] . $_W['config']['setting']['authkey']),
  62. 'nickname' => '',
  63. 'avatar' => '',
  64. 'gender' => '',
  65. 'nationality' => '',
  66. 'resideprovince' => '',
  67. 'residecity' => '',
  68. );
  69. pdo_insert('mc_members', $data);
  70. $uid = pdo_insertid();
  71. }
  72. $record['uid'] = $uid;
  73. $_SESSION['uid'] = $uid;
  74. pdo_insert('mc_mapping_fans', $record);
  75. } else {
  76. $userinfo = $fans['tag'];
  77. $uid = $fans['uid'];
  78. }
  79. if (empty($userinfo)) {
  80. $userinfo = array(
  81. 'openid' => $oauth['openid'],
  82. );
  83. }
  84. $_SESSION['userinfo'] = base64_encode(iserializer($userinfo));
  85. $account_api->result(0, '', array('sessionid' => $_W['session_id'], 'userinfo' => $fans, 'openid' => $oauth['openid']));
  86. } else {
  87. $account_api->result(1, $oauth['message']);
  88. }
  89. } elseif ($do == 'userinfo') {
  90. $encrypt_data = $_GPC['encryptedData'];
  91. $iv = $_GPC['iv'];
  92. if (empty($_SESSION['session_key']) || empty($encrypt_data) || empty($iv)) {
  93. $account_api->result(1, '请先登录');
  94. }
  95. $sign = sha1($_POST['rawData'].$_SESSION['session_key']);
  96. if ($sign !== $_GPC['signature']) {
  97. $account_api->result(1, '签名错误');
  98. }
  99. $userinfo = $account_api->pkcs7Encode($encrypt_data, $iv);
  100. $userinfo['nickname'] = $userinfo['nickName'];
  101. $_SESSION['userinfo'] = base64_encode(iserializer($userinfo));
  102. $fans = mc_fansinfo($userinfo['openId']);
  103. $fans_update = array(
  104. 'nickname' => $userinfo['nickName'],
  105. 'unionid' => $userinfo['unionId'],
  106. 'tag' => base64_encode(iserializer(array(
  107. 'subscribe' => 1,
  108. 'openid' => $userinfo['openId'],
  109. 'nickname' => $userinfo['nickName'],
  110. 'sex' => $userinfo['gender'],
  111. 'language' => $userinfo['language'],
  112. 'city' => $userinfo['city'],
  113. 'province' => $userinfo['province'],
  114. 'country' => $userinfo['country'],
  115. 'headimgurl' => $userinfo['avatarUrl'],
  116. ))),
  117. );
  118. /*if (!empty($userinfo['unionId'])) {
  119. $union_fans = pdo_get('mc_mapping_fans', array('unionid' => $userinfo['unionId'], 'openid !=' => $userinfo['openId']));
  120. if (!empty($union_fans['uid'])) {
  121. if (!empty($fans['uid'])) {
  122. }
  123. $fans_update['uid'] = $union_fans['uid'];
  124. $_SESSION['uid'] = $union_fans['uid'];
  125. }
  126. }*/
  127. $member = mc_fetch($fans['uid']);
  128. if (!empty($member)) {
  129. pdo_update('mc_members', array('nickname' => $userinfo['nickName'], 'avatar' => $userinfo['avatarUrl'], 'gender' => $userinfo['gender']), array('uid' => $fans['uid']));
  130. } else {
  131. $default_groupid = pdo_fetchcolumn('SELECT groupid FROM ' .tablename('mc_groups') . ' WHERE uniacid = :uniacid AND isdefault = 1', array(':uniacid' => $_W['uniacid']));
  132. $member = array(
  133. 'uniacid' => $_W['uniacid'],
  134. 'email' => md5($_SESSION['openid']).'@we7.cc',
  135. 'salt' => random(8),
  136. 'groupid' => $default_groupid,
  137. 'createtime' => TIMESTAMP,
  138. 'password' => md5($userinfo['openId'] . $member['salt'] . $_W['config']['setting']['authkey']),
  139. 'nickname' => $userinfo['nickName'],
  140. 'avatar' => $userinfo['avatarUrl'],
  141. 'gender' => $userinfo['gender'],
  142. 'nationality' => '',
  143. 'resideprovince' => '',
  144. 'residecity' => '',
  145. );
  146. pdo_insert('mc_members', $member);
  147. $fans_update['uid'] = pdo_insertid();
  148. }
  149. pdo_update('mc_mapping_fans', $fans_update, array('fanid' => $fans['fanid']));
  150. unset($member['password']);
  151. unset($member['salt']);
  152. $account_api->result(0, '', $member);
  153. } elseif ($do == 'check') {
  154. if (!empty($_W['openid'])) {
  155. $account_api->result(0);
  156. } else {
  157. $account_api->result(1, 'session失效,请重新发起登录请求');
  158. }
  159. }