session_helper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. $mobile = session_helper::mobile_valid($mobile);
  196. if ($mobile == false) {
  197. continue;
  198. }
  199. if (algorithm::bsearch($mobile, $ar_contact) != -1) {
  200. continue;
  201. }
  202. array_push($ar_contact, $mobile);
  203. sort($ar_contact);
  204. }
  205. return $ar_contact;
  206. }
  207. static public function session_id()
  208. {
  209. return empty($_SESSION['MPHPSESSID']) ? "" : $_SESSION['MPHPSESSID'];
  210. }
  211. static public function need_wechat_author()
  212. {
  213. if (BASE_SITE_URL != 'http://p.lrlz.com') {
  214. return false;
  215. }
  216. if (util::from_wechat() == false) {
  217. return false;
  218. }
  219. else
  220. {
  221. if (array_key_exists('wx_author', $_SESSION))
  222. {
  223. if (array_key_exists('handled', $_SESSION['wx_author']))
  224. {
  225. $user_info = $_SESSION['wx_author']['user_info'];
  226. $loginer = new login\unionid_log($user_info['unionid']);
  227. if ($loginer->ismember() == true) {
  228. $loginer->bind($user_info);
  229. $loginer->login();
  230. unset($_SESSION['wx_author']);
  231. }
  232. }
  233. return false;
  234. }
  235. else
  236. {
  237. if (!empty($_SESSION['member_wxunionid']) && !empty($_SESSION['member_wxopenid']))
  238. {
  239. $author_time = $_SESSION['wxauthor_time'];
  240. $max_time = 2 * 86400;
  241. if (time() - $author_time > $max_time) {
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. }
  247. else {
  248. return true;
  249. }
  250. }
  251. }
  252. }
  253. static public function pub_openid()
  254. {
  255. if (empty($_SESSION['member_wxopenid'])) {
  256. return false;
  257. } else {
  258. return $_SESSION['member_wxopenid'];
  259. }
  260. }
  261. static public function unionid()
  262. {
  263. if (!empty($_SESSION['member_wxunionid'])) {
  264. return $_SESSION['member_wxunionid'];
  265. }
  266. if (array_key_exists('wx_author', $_SESSION)) {
  267. if (array_key_exists('handled', $_SESSION['wx_author'])) {
  268. $user_info = $_SESSION['wx_author']['user_info'];
  269. return $user_info['unionid'];
  270. }
  271. }
  272. return false;
  273. }
  274. static public function thief($fromid, &$err)
  275. {
  276. $thief = new bonus\thief_vilator($fromid);
  277. return $thief->thief($err);
  278. }
  279. static public function first_order()
  280. {
  281. if (array_key_exists('order_num', $_SESSION)) {
  282. $order_num = $_SESSION['order_num'];
  283. } else {
  284. $mod_member = Model('member');
  285. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  286. if (empty($minfo)) return false;
  287. $order_num = intval($minfo['order_num']);
  288. if ($order_num > 0) {
  289. $_SESSION['order_num'] = $order_num;
  290. }
  291. }
  292. if ($order_num == 0) {
  293. return true;
  294. } else {
  295. return false;
  296. }
  297. }
  298. static public function can_send()
  299. {
  300. if (array_key_exists('order_num', $_SESSION)) {
  301. $order_num = $_SESSION['order_num'];
  302. } else {
  303. $mod_member = Model('member');
  304. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  305. if (empty($minfo)) return false;
  306. $order_num = intval($minfo['order_num']);
  307. if ($order_num > 0) {
  308. $_SESSION['order_num'] = $order_num;
  309. }
  310. }
  311. return $order_num > 0;
  312. }
  313. static public function add_order()
  314. {
  315. if (!array_key_exists('order_num', $_SESSION)) {
  316. $mod_member = Model('member');
  317. $mod_member->editMember(['member_id' => self::memberid()],['order_num' => array('exp', 'order_num+1'),'lastest_order' => time()]);
  318. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  319. $_SESSION['order_num'] = intval($minfo['order_num']);
  320. } else {
  321. $_SESSION['order_num'] += 1;
  322. }
  323. }
  324. static public function share_id()
  325. {
  326. if(self::logined()) {
  327. return self::memberid();
  328. }
  329. else
  330. {
  331. if(array_key_exists('relay_id',$_SESSION)) {
  332. return $_SESSION['relay_id'];
  333. } else {
  334. return 0;
  335. }
  336. }
  337. }
  338. static public function relay_id()
  339. {
  340. if(array_key_exists('relay_id',$_SESSION)) {
  341. return $_SESSION['relay_id'];
  342. } else {
  343. return 0;
  344. }
  345. }
  346. static public function set_relay($relay_id)
  347. {
  348. $relay_id = intval($relay_id);
  349. if($relay_id > 0) {
  350. $_SESSION['relay_id'] = $relay_id;
  351. }
  352. }
  353. static public function client_type()
  354. {
  355. if($_SESSION['client_type'] == 'android') {
  356. return self::device_android;
  357. }
  358. elseif($_SESSION['client_type'] == 'ios') {
  359. return self::device_ios;
  360. }
  361. else {
  362. return 0;
  363. }
  364. }
  365. static public function version_code()
  366. {
  367. if(self::client_type() == self::device_android) {
  368. return $_SESSION['client_version'];
  369. }
  370. elseif(self::client_type() == self::device_ios) {
  371. return $_SESSION['client_version'] * 100;
  372. }
  373. else {
  374. return 0;
  375. }
  376. }
  377. }