session_helper.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. require_once (BASE_ROOT_PATH . '/helper/bonus_helper.php');
  14. class session_helper
  15. {
  16. const mobile_login = 1;
  17. const wxopen_login = 2;
  18. const mobile_len = 11;
  19. const device_ios = 1;
  20. const device_android = 2;
  21. static public function mobile_valid($mobile)
  22. {
  23. if (!isset($mobile) || empty($mobile) || strlen($mobile) < self::mobile_len) {
  24. return false;
  25. }
  26. $ret = preg_match('/^1(?:3[0-9]|5[012356789]|8[0256789]|7[0678])(-?)\d{4}\1\d{4}$/', $mobile, $arr);
  27. if ($ret == false || count($arr) <= 0) {
  28. return false;
  29. } else {
  30. $mobile = $arr[0];
  31. }
  32. $mobile = str_replace('-', '', $mobile);
  33. $mobile = substr($mobile, -self::mobile_len);
  34. $validate = new Validator();
  35. $validate->setValidate(Validator::verify_mobile($mobile));
  36. $err = $validate->validate();
  37. if (empty($err)) {
  38. return $mobile;
  39. } else {
  40. return false;
  41. }
  42. }
  43. static public function memberid()
  44. {
  45. return intval($_SESSION['member_id']);
  46. }
  47. static public function nickname()
  48. {
  49. if (isset($_SESSION['member_nickname']) && !empty($_SESSION['member_nickname'])) {
  50. return $_SESSION['member_nickname'];
  51. }
  52. if (array_key_exists('wx_author', $_SESSION)) {
  53. if (array_key_exists('handled', $_SESSION['wx_author'])) {
  54. $user_info = $_SESSION['wx_author']['user_info'];
  55. return $user_info['nickname'];
  56. }
  57. }
  58. if (isset($_SESSION['member_truename']) && !empty($_SESSION['member_truename'])) {
  59. return $_SESSION['member_truename'];
  60. }
  61. if (isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
  62. $mobile = $_SESSION['member_mobile'];
  63. if (strlen($mobile) == 11) {
  64. return substr_replace($mobile, '****', 3, 4);
  65. }
  66. }
  67. return '';
  68. }
  69. static public function isLogin()
  70. {
  71. return ($_SESSION['is_login'] == 1);
  72. }
  73. static public function logined()
  74. {
  75. return ($_SESSION['is_login'] == 1);
  76. }
  77. static public function isapp()
  78. {
  79. if (isset($_SESSION['is_app'])) {
  80. return ($_SESSION['is_app'] == true);
  81. } else {
  82. return false;
  83. }
  84. }
  85. static public function isVerfiyMobile()
  86. {
  87. if (self::isLogin()) {
  88. return true;
  89. } else {
  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. {
  99. return (intval($_SESSION['member_sex']) == 1);
  100. }
  101. static public function sex()
  102. {
  103. $sex = intval($_SESSION['member_sex']);
  104. if ($sex == 1) {
  105. return $sex;
  106. } else {
  107. return 0;
  108. }
  109. }
  110. static function avatar()
  111. {
  112. if (empty($_SESSION['member_avatar'])) {
  113. if (array_key_exists('wx_author', $_SESSION)) {
  114. if (array_key_exists('handled', $_SESSION['wx_author'])) {
  115. $user_info = $_SESSION['wx_author']['user_info'];
  116. return $user_info['headimgurl'];
  117. }
  118. }
  119. if (self::is_man()) {
  120. $img = 'male.png';
  121. } else {
  122. $img = 'female.png';
  123. }
  124. return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
  125. } else {
  126. $path = $_SESSION['member_avatar'];
  127. if (strncasecmp($path, "http://", strlen("http://")) == 0) {
  128. $url = $path;
  129. } else {
  130. $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
  131. }
  132. return $url;
  133. }
  134. }
  135. static public function cur_mobile()
  136. {
  137. if (self::isVerfiyMobile()) {
  138. return $_SESSION['member_mobile'];
  139. } else {
  140. return '';
  141. }
  142. }
  143. static public function parase_wxinfo($wxinfo)
  144. {
  145. $user = urldecode($wxinfo);
  146. if (empty($user)) return false;
  147. $user = json_decode($user, true);
  148. if ($user == false) return false;
  149. $info = array();
  150. $info['member_nickname'] = $user['nickname'];
  151. $info['member_avatar'] = $user['headimgurl'];
  152. $info['member_sex'] = intval($user['sex']);
  153. return $info;
  154. }
  155. static public function filter_info($info)
  156. {
  157. $member_info = array();
  158. if (isset($info['member_nickname']) && !empty($info['member_nickname'])) {
  159. $member_info['member_nickname'] = $info['member_nickname'];
  160. }
  161. if (isset($info['member_truename']) && !empty($info['member_truename'])) {
  162. if (!isset($member_info['member_nickname'])) {
  163. $member_info['member_nickname'] = $info['member_truename'];
  164. }
  165. $member_info['member_truename'] = $info['member_truename'];
  166. }
  167. if (isset($info['member_mobile']) && !empty($info['member_mobile'])) {
  168. $mobile = $info['member_mobile'];
  169. if (!isset($member_info['member_nickname'])) {
  170. $member_info['member_nickname'] = substr_replace($mobile, '****', 3, 4);
  171. }
  172. $member_info['member_mobile'] = $info['member_mobile'];
  173. }
  174. return $member_info;
  175. }
  176. static public function filter_member_info($member_info, $openid)
  177. {
  178. field_helper::validate_null_string($member_info, 'member_mobile,member_wxopenid,member_name,' .
  179. 'member_truename,member_signname,member_avatar,member_email,member_birthday');
  180. field_helper::validate_null_string($member_info, 'member_sex', '0');
  181. field_helper::validate_null_string($member_info, 'member_nickname', substr_replace($openid, '****', 3, 4));
  182. return $member_info;
  183. }
  184. static public function parse_contacts($contacts)
  185. {
  186. if (!isset($contacts)) {
  187. return false;
  188. }
  189. $contacts = json_decode(urldecode($contacts));
  190. if ($contacts == false) {
  191. return false;
  192. }
  193. $ar_contact = array();
  194. foreach ($contacts as $mobile)
  195. {
  196. $mobile = session_helper::mobile_valid($mobile);
  197. if ($mobile == false) {
  198. continue;
  199. }
  200. if (algorithm::bsearch($mobile, $ar_contact) != -1) {
  201. continue;
  202. }
  203. array_push($ar_contact, $mobile);
  204. sort($ar_contact);
  205. }
  206. return $ar_contact;
  207. }
  208. static public function session_id()
  209. {
  210. return empty($_SESSION['MPHPSESSID']) ? "" : $_SESSION['MPHPSESSID'];
  211. }
  212. static public function need_wechat_author()
  213. {
  214. if (BASE_SITE_URL != 'http://p.lrlz.com') {
  215. return false;
  216. }
  217. if (util::from_wechat() == false) {
  218. return false;
  219. }
  220. else
  221. {
  222. if (array_key_exists('wx_author', $_SESSION))
  223. {
  224. if (array_key_exists('handled', $_SESSION['wx_author']))
  225. {
  226. $user_info = $_SESSION['wx_author']['user_info'];
  227. $loginer = new login\unionid_log($user_info['unionid']);
  228. if ($loginer->ismember() == true) {
  229. $loginer->bind($user_info);
  230. $loginer->login();
  231. unset($_SESSION['wx_author']);
  232. }
  233. }
  234. return false;
  235. }
  236. else
  237. {
  238. if (!empty($_SESSION['member_wxunionid']) && !empty($_SESSION['member_wxopenid']))
  239. {
  240. $author_time = $_SESSION['wxauthor_time'];
  241. $max_time = 2 * 86400;
  242. if (time() - $author_time > $max_time) {
  243. return true;
  244. } else {
  245. return false;
  246. }
  247. }
  248. else {
  249. return true;
  250. }
  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. $user_info = $_SESSION['wx_author']['user_info'];
  271. return $user_info['unionid'];
  272. }
  273. }
  274. return false;
  275. }
  276. static public function thief($fromid, &$err)
  277. {
  278. $thief = new bonus\thief_vilator($fromid);
  279. return $thief->thief($err);
  280. }
  281. static public function first_order()
  282. {
  283. if (array_key_exists('order_num', $_SESSION)) {
  284. $order_num = $_SESSION['order_num'];
  285. }
  286. else {
  287. $mod_member = Model('member');
  288. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  289. if (empty($minfo)) return false;
  290. $order_num = intval($minfo['order_num']);
  291. if ($order_num > 0) {
  292. $_SESSION['order_num'] = $order_num;
  293. }
  294. }
  295. if ($order_num == 0) {
  296. return true;
  297. } else {
  298. return false;
  299. }
  300. }
  301. static public function can_send()
  302. {
  303. if (array_key_exists('order_num', $_SESSION)) {
  304. $order_num = $_SESSION['order_num'];
  305. }
  306. else
  307. {
  308. $mod_member = Model('member');
  309. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  310. if (empty($minfo)) return false;
  311. $order_num = intval($minfo['order_num']);
  312. if ($order_num > 0) {
  313. $_SESSION['order_num'] = $order_num;
  314. }
  315. }
  316. return $order_num > 0;
  317. }
  318. static public function add_order()
  319. {
  320. if (!array_key_exists('order_num', $_SESSION)) {
  321. $mod_member = Model('member');
  322. $mod_member->editMember(['member_id' => self::memberid()],['order_num' => array('exp', 'order_num+1'),'lastest_order' => time()]);
  323. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  324. $_SESSION['order_num'] = intval($minfo['order_num']);
  325. } else {
  326. $_SESSION['order_num'] += 1;
  327. }
  328. }
  329. static public function share_id()
  330. {
  331. if(self::logined()) {
  332. return self::memberid();
  333. }
  334. else
  335. {
  336. if(array_key_exists('relay_id',$_SESSION)) {
  337. return $_SESSION['relay_id'];
  338. } else {
  339. return 0;
  340. }
  341. }
  342. }
  343. static public function relay_id()
  344. {
  345. if(array_key_exists('relay_id',$_SESSION)) {
  346. return $_SESSION['relay_id'];
  347. } else {
  348. return 0;
  349. }
  350. }
  351. static public function set_relay($relay_id)
  352. {
  353. $relay_id = intval($relay_id);
  354. if($relay_id > 0) {
  355. $_SESSION['relay_id'] = $relay_id;
  356. }
  357. }
  358. static public function client_type()
  359. {
  360. if($_SESSION['client_type'] == 'android') {
  361. return self::device_android;
  362. }
  363. elseif($_SESSION['client_type'] == 'ios') {
  364. return self::device_ios;
  365. }
  366. else {
  367. return 0;
  368. }
  369. }
  370. static public function version_code()
  371. {
  372. if(self::client_type() == self::device_android) {
  373. return $_SESSION['client_version'];
  374. }
  375. elseif(self::client_type() == self::device_ios) {
  376. return $_SESSION['client_version'] * 100;
  377. }
  378. else {
  379. return 0;
  380. }
  381. }
  382. //缓存数据给个人页面使用
  383. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  384. static public function address_num()
  385. {
  386. if(array_key_exists('address_num',$_SESSION)) {
  387. return $_SESSION['address_num'];
  388. }
  389. else
  390. {
  391. $mod_addr = Model('address');
  392. $_SESSION['address_num'] = $mod_addr->getAddressCount(['member_id' => self::memberid()]);
  393. return $_SESSION['address_num'];
  394. }
  395. }
  396. static public function clear_addr()
  397. {
  398. if (array_key_exists('address_num', $_SESSION)) {
  399. unset($_SESSION['address_num']);
  400. }
  401. }
  402. static public function favorate_num()
  403. {
  404. if(array_key_exists('favorate_num',$_SESSION)) {
  405. return $_SESSION['favorate_num'];
  406. }
  407. else
  408. {
  409. $mod_favorites = Model('favorites');
  410. $_SESSION['favorate_num'] = $mod_favorites->getFavoritesCount(['member_id' => self::memberid()]);
  411. return $_SESSION['favorate_num'];
  412. }
  413. }
  414. static public function clear_favorate()
  415. {
  416. if (array_key_exists('favorate_num', $_SESSION)) {
  417. unset($_SESSION['favorate_num']);
  418. }
  419. }
  420. static public function fcode_num()
  421. {
  422. if(array_key_exists('fcode_num',$_SESSION)) {
  423. return $_SESSION['fcode_num'];
  424. }
  425. else
  426. {
  427. $mod_favorites = Model('goods_fcode');
  428. $_SESSION['fcode_num'] = $mod_favorites->getUsableFcodeCount(self::cur_mobile());
  429. return $_SESSION['fcode_num'];
  430. }
  431. }
  432. static public function clear_fcode()
  433. {
  434. if (array_key_exists('fcode_num', $_SESSION)) {
  435. unset($_SESSION['fcode_num']);
  436. }
  437. }
  438. }