session_helper.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/4/17
  6. * Time: 下午6:46
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
  9. require_once (BASE_ROOT_PATH . '/helper/relation_helper.php');
  10. require_once (BASE_ROOT_PATH . '/helper/account_helper.php');
  11. require_once (BASE_ROOT_PATH . '/helper/push_helper.php');
  12. require_once (BASE_ROOT_PATH . '/helper/login_helper.php');
  13. class session_helper
  14. {
  15. const mobile_login = 1;
  16. const wxopen_login = 2;
  17. const mobile_len = 11;
  18. static public function mobile_valid($mobile)
  19. {
  20. if(!isset($mobile) || empty($mobile) || strlen($mobile) < self::mobile_len) {
  21. return false;
  22. }
  23. $ret = preg_match('/^1(?:3[0-9]|5[012356789]|8[0256789]|7[0678])(-?)\d{4}\1\d{4}$/', $mobile, $arr);
  24. if($ret == false || count($arr) <= 0) {
  25. return false;
  26. } else {
  27. $mobile = $arr[0];
  28. }
  29. $mobile = str_replace('-','',$mobile);
  30. $mobile = substr($mobile,-self::mobile_len);
  31. $validate = new Validator();
  32. $validate->setValidate(Validator::verify_mobile($mobile));
  33. $err = $validate->validate();
  34. if(empty($err)) {
  35. return $mobile;
  36. } else {
  37. return false;
  38. }
  39. }
  40. static public function memberid() {
  41. return $_SESSION['member_id'];
  42. }
  43. static public function nickname()
  44. {
  45. if(isset($_SESSION['member_nickname']) && !empty($_SESSION['member_nickname'])) {
  46. return $_SESSION['member_nickname'];
  47. }
  48. if(isset($_SESSION['member_truename']) && !empty($_SESSION['member_truename'])) {
  49. return $_SESSION['member_truename'];
  50. }
  51. if(isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile']))
  52. {
  53. $mobile = $_SESSION['member_mobile'];
  54. if(strlen($mobile) == 11) {
  55. return substr_replace($mobile, '****', 3, 4);
  56. }
  57. }
  58. return '';
  59. }
  60. static public function isLogin() {
  61. return ($_SESSION['is_login'] == 1);
  62. }
  63. static public function logined()
  64. {
  65. return ($_SESSION['is_login'] == 1);
  66. }
  67. static public function isapp()
  68. {
  69. if(isset($_SESSION['is_app'])) {
  70. return ($_SESSION['is_app'] == true);
  71. } else {
  72. return false;
  73. }
  74. }
  75. static public function isVerfiyMobile()
  76. {
  77. if(self::isLogin()) {
  78. return true;
  79. }
  80. else
  81. {
  82. if(isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
  83. return true;
  84. } else {
  85. return false;
  86. }
  87. }
  88. }
  89. static public function is_man() {
  90. return (intval($_SESSION['member_sex']) == 1);
  91. }
  92. static function avatar()
  93. {
  94. if(empty($_SESSION['member_avatar']))
  95. {
  96. if(self::is_man()) {
  97. $img = 'male.png';
  98. } else {
  99. $img = 'female.png';
  100. }
  101. return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
  102. }
  103. else
  104. {
  105. $path = $_SESSION['member_avatar'];
  106. if(strncasecmp($path,"http://",strlen("http://")) == 0) {
  107. $url = $path;
  108. } else {
  109. $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
  110. }
  111. return $url;
  112. }
  113. }
  114. static public function cur_mobile()
  115. {
  116. if(self::isVerfiyMobile()) {
  117. return $_SESSION['member_mobile'];
  118. } else {
  119. return '';
  120. }
  121. }
  122. static public function parase_wxinfo($wxinfo)
  123. {
  124. $user = urldecode($wxinfo);
  125. if(empty($user)) return false;
  126. $user = json_decode($user,true);
  127. if($user == false) return false;
  128. $info = array();
  129. $info['member_nickname'] = $user['nickname'];
  130. $info['member_avatar'] = $user['headimgurl'];
  131. $info['member_sex'] = intval($user['sex']);
  132. return $info;
  133. }
  134. static public function filter_info($info)
  135. {
  136. $member_info = array();
  137. if(isset($info['member_nickname']) && !empty($info['member_nickname'])) {
  138. $member_info['member_nickname'] = $info['member_nickname'];
  139. }
  140. if(isset($info['member_truename']) && !empty($info['member_truename'])) {
  141. if(!isset($member_info['member_nickname'])) {
  142. $member_info['member_nickname'] = $info['member_truename'];
  143. }
  144. $member_info['member_truename'] = $info['member_truename'];
  145. }
  146. if(isset($info['member_mobile']) && !empty($info['member_mobile']))
  147. {
  148. $mobile = $info['member_mobile'];
  149. if(!isset($member_info['member_nickname'])) {
  150. $member_info['member_nickname'] = substr_replace($mobile, '****', 3, 4);
  151. }
  152. $member_info['member_mobile'] = $info['member_mobile'];
  153. }
  154. return $member_info;
  155. }
  156. static public function filter_member_info($member_info,$openid)
  157. {
  158. field_helper::validate_null_string($member_info, 'member_mobile,member_wxopenid,member_name,' .
  159. 'member_truename,member_signname,member_avatar,member_email,member_birthday');
  160. field_helper::validate_null_string($member_info, 'member_sex', '0');
  161. field_helper::validate_null_string($member_info, 'member_nickname', substr_replace($openid, '****', 3, 4));
  162. return $member_info;
  163. }
  164. static public function parse_contacts($contacts)
  165. {
  166. if(!isset($contacts)) {
  167. return false;
  168. }
  169. $contacts = json_decode(urldecode($contacts));
  170. if($contacts == false) {
  171. return false;
  172. }
  173. $ar_contact = array();
  174. foreach ($contacts as $mobile)
  175. {
  176. $mobile = session_helper::mobile_valid($mobile);
  177. if($mobile == false) {
  178. continue;
  179. }
  180. if(algorithm::bsearch($mobile,$ar_contact) != -1) {
  181. continue;
  182. }
  183. array_push($ar_contact,$mobile);
  184. sort($ar_contact);
  185. }
  186. return $ar_contact;
  187. }
  188. static public function session_id() {
  189. return empty($_SESSION['MPHPSESSID']) ? "" : $_SESSION['MPHPSESSID'];
  190. }
  191. static public function need_wechat_author()
  192. {
  193. if(BASE_SITE_URL != 'http://p.lrlz.com') {
  194. return false;
  195. }
  196. if(util::from_wechat() == false) {
  197. return false;
  198. }
  199. else
  200. {
  201. if(array_key_exists('wx_author',$_SESSION))
  202. {
  203. if(array_key_exists('handled',$_SESSION['wx_author']))
  204. {
  205. $user_info = $_SESSION['wx_author']['user_info'];
  206. $loginer = new \login\unionid_log($user_info['unionid']);
  207. if(self::logined()) {
  208. $loginer->bind($user_info);
  209. $loginer->login();
  210. unset($_SESSION['wx_author']);
  211. }
  212. else
  213. {
  214. if($loginer->ismember() == true)
  215. {
  216. $loginer->bind($user_info);
  217. $loginer->login();
  218. unset($_SESSION['wx_author']);
  219. }
  220. }
  221. }
  222. return false;
  223. }
  224. $author_time = $_SESSION['wxauthor_time'];
  225. $max_time = 2 * 86400;
  226. if(time() - $author_time > $max_time) {
  227. return true;
  228. }
  229. if(!empty($_SESSION['member_wxunionid']) && !empty($_SESSION['member_wxopenid']))
  230. {
  231. return false;
  232. }
  233. else {
  234. return true;
  235. }
  236. }
  237. }
  238. static public function pub_openid()
  239. {
  240. if(empty($_SESSION['member_wxopenid'])) {
  241. return false;
  242. } else {
  243. return $_SESSION['member_wxopenid'];
  244. }
  245. }
  246. }