session_helper.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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(array_key_exists('wx_author',$_SESSION))
  49. {
  50. if(array_key_exists('handled',$_SESSION['wx_author']))
  51. {
  52. $user_info = $_SESSION['wx_author']['user_info'];
  53. return $user_info['nickname'];
  54. }
  55. }
  56. if(isset($_SESSION['member_truename']) && !empty($_SESSION['member_truename'])) {
  57. return $_SESSION['member_truename'];
  58. }
  59. if(isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile']))
  60. {
  61. $mobile = $_SESSION['member_mobile'];
  62. if(strlen($mobile) == 11) {
  63. return substr_replace($mobile, '****', 3, 4);
  64. }
  65. }
  66. return '';
  67. }
  68. static public function isLogin() {
  69. return ($_SESSION['is_login'] == 1);
  70. }
  71. static public function logined()
  72. {
  73. return ($_SESSION['is_login'] == 1);
  74. }
  75. static public function isapp()
  76. {
  77. if(isset($_SESSION['is_app'])) {
  78. return ($_SESSION['is_app'] == true);
  79. } else {
  80. return false;
  81. }
  82. }
  83. static public function isVerfiyMobile()
  84. {
  85. if(self::isLogin()) {
  86. return true;
  87. }
  88. else
  89. {
  90. if(isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96. }
  97. static public function is_man() {
  98. return (intval($_SESSION['member_sex']) == 1);
  99. }
  100. static function avatar()
  101. {
  102. if(empty($_SESSION['member_avatar']))
  103. {
  104. if(array_key_exists('wx_author',$_SESSION))
  105. {
  106. if(array_key_exists('handled',$_SESSION['wx_author']))
  107. {
  108. $user_info = $_SESSION['wx_author']['user_info'];
  109. return $user_info['headimgurl'];
  110. }
  111. }
  112. if(self::is_man()) {
  113. $img = 'male.png';
  114. } else {
  115. $img = 'female.png';
  116. }
  117. return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
  118. }
  119. else
  120. {
  121. $path = $_SESSION['member_avatar'];
  122. if(strncasecmp($path,"http://",strlen("http://")) == 0) {
  123. $url = $path;
  124. } else {
  125. $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
  126. }
  127. return $url;
  128. }
  129. }
  130. static public function cur_mobile()
  131. {
  132. if(self::isVerfiyMobile()) {
  133. return $_SESSION['member_mobile'];
  134. } else {
  135. return '';
  136. }
  137. }
  138. static public function parase_wxinfo($wxinfo)
  139. {
  140. $user = urldecode($wxinfo);
  141. if(empty($user)) return false;
  142. $user = json_decode($user,true);
  143. if($user == false) return false;
  144. $info = array();
  145. $info['member_nickname'] = $user['nickname'];
  146. $info['member_avatar'] = $user['headimgurl'];
  147. $info['member_sex'] = intval($user['sex']);
  148. return $info;
  149. }
  150. static public function filter_info($info)
  151. {
  152. $member_info = array();
  153. if(isset($info['member_nickname']) && !empty($info['member_nickname'])) {
  154. $member_info['member_nickname'] = $info['member_nickname'];
  155. }
  156. if(isset($info['member_truename']) && !empty($info['member_truename'])) {
  157. if(!isset($member_info['member_nickname'])) {
  158. $member_info['member_nickname'] = $info['member_truename'];
  159. }
  160. $member_info['member_truename'] = $info['member_truename'];
  161. }
  162. if(isset($info['member_mobile']) && !empty($info['member_mobile']))
  163. {
  164. $mobile = $info['member_mobile'];
  165. if(!isset($member_info['member_nickname'])) {
  166. $member_info['member_nickname'] = substr_replace($mobile, '****', 3, 4);
  167. }
  168. $member_info['member_mobile'] = $info['member_mobile'];
  169. }
  170. return $member_info;
  171. }
  172. static public function filter_member_info($member_info,$openid)
  173. {
  174. field_helper::validate_null_string($member_info, 'member_mobile,member_wxopenid,member_name,' .
  175. 'member_truename,member_signname,member_avatar,member_email,member_birthday');
  176. field_helper::validate_null_string($member_info, 'member_sex', '0');
  177. field_helper::validate_null_string($member_info, 'member_nickname', substr_replace($openid, '****', 3, 4));
  178. return $member_info;
  179. }
  180. static public function parse_contacts($contacts)
  181. {
  182. if(!isset($contacts)) {
  183. return false;
  184. }
  185. $contacts = json_decode(urldecode($contacts));
  186. if($contacts == false) {
  187. return false;
  188. }
  189. $ar_contact = array();
  190. foreach ($contacts as $mobile)
  191. {
  192. $mobile = session_helper::mobile_valid($mobile);
  193. if($mobile == false) {
  194. continue;
  195. }
  196. if(algorithm::bsearch($mobile,$ar_contact) != -1) {
  197. continue;
  198. }
  199. array_push($ar_contact,$mobile);
  200. sort($ar_contact);
  201. }
  202. return $ar_contact;
  203. }
  204. static public function session_id() {
  205. return empty($_SESSION['MPHPSESSID']) ? "" : $_SESSION['MPHPSESSID'];
  206. }
  207. static public function need_wechat_author()
  208. {
  209. if(BASE_SITE_URL != 'http://p.lrlz.com') {
  210. return false;
  211. }
  212. if(util::from_wechat() == false) {
  213. return false;
  214. }
  215. else
  216. {
  217. if(array_key_exists('wx_author',$_SESSION))
  218. {
  219. if(array_key_exists('handled',$_SESSION['wx_author']))
  220. {
  221. $user_info = $_SESSION['wx_author']['user_info'];
  222. $loginer = new \login\unionid_log($user_info['unionid']);
  223. if(self::logined()) {
  224. $loginer->bind($user_info);
  225. $loginer->login();
  226. unset($_SESSION['wx_author']);
  227. }
  228. else
  229. {
  230. if($loginer->ismember() == true)
  231. {
  232. $loginer->bind($user_info);
  233. $loginer->login();
  234. unset($_SESSION['wx_author']);
  235. }
  236. }
  237. }
  238. return false;
  239. }
  240. $author_time = $_SESSION['wxauthor_time'];
  241. $max_time = 2 * 86400;
  242. if(time() - $author_time > $max_time) {
  243. return true;
  244. }
  245. if(!empty($_SESSION['member_wxunionid']) && !empty($_SESSION['member_wxopenid']))
  246. {
  247. return false;
  248. }
  249. else {
  250. return true;
  251. }
  252. }
  253. }
  254. static public function pub_openid()
  255. {
  256. if(empty($_SESSION['member_wxopenid'])) {
  257. return false;
  258. } else {
  259. return $_SESSION['member_wxopenid'];
  260. }
  261. }
  262. static public function unionid()
  263. {
  264. if(!empty($_SESSION['member_wxunionid'])) {
  265. return $_SESSION['member_wxunionid'];
  266. }
  267. if(array_key_exists('wx_author',$_SESSION))
  268. {
  269. if(array_key_exists('handled',$_SESSION['wx_author']))
  270. {
  271. $user_info = $_SESSION['wx_author']['user_info'];
  272. return $user_info['unionid'];
  273. }
  274. }
  275. return false;
  276. }
  277. }