session_helper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. static public function mobile_valid($mobile)
  20. {
  21. if(!isset($mobile) || empty($mobile) || strlen($mobile) < self::mobile_len) {
  22. return false;
  23. }
  24. $ret = preg_match('/^1(?:3[0-9]|5[012356789]|8[0256789]|7[0678])(-?)\d{4}\1\d{4}$/', $mobile, $arr);
  25. if($ret == false || count($arr) <= 0) {
  26. return false;
  27. } else {
  28. $mobile = $arr[0];
  29. }
  30. $mobile = str_replace('-','',$mobile);
  31. $mobile = substr($mobile,-self::mobile_len);
  32. $validate = new Validator();
  33. $validate->setValidate(Validator::verify_mobile($mobile));
  34. $err = $validate->validate();
  35. if(empty($err)) {
  36. return $mobile;
  37. } else {
  38. return false;
  39. }
  40. }
  41. static public function memberid() {
  42. return intval($_SESSION['member_id']);
  43. }
  44. static public function nickname()
  45. {
  46. if(isset($_SESSION['member_nickname']) && !empty($_SESSION['member_nickname'])) {
  47. return $_SESSION['member_nickname'];
  48. }
  49. if(array_key_exists('wx_author',$_SESSION))
  50. {
  51. if(array_key_exists('handled',$_SESSION['wx_author']))
  52. {
  53. $user_info = $_SESSION['wx_author']['user_info'];
  54. return $user_info['nickname'];
  55. }
  56. }
  57. if(isset($_SESSION['member_truename']) && !empty($_SESSION['member_truename'])) {
  58. return $_SESSION['member_truename'];
  59. }
  60. if(isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile']))
  61. {
  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. return ($_SESSION['is_login'] == 1);
  71. }
  72. static public function logined()
  73. {
  74. return ($_SESSION['is_login'] == 1);
  75. }
  76. static public function isapp()
  77. {
  78. if(isset($_SESSION['is_app'])) {
  79. return ($_SESSION['is_app'] == true);
  80. } else {
  81. return false;
  82. }
  83. }
  84. static public function isVerfiyMobile()
  85. {
  86. if(self::isLogin()) {
  87. return true;
  88. }
  89. else
  90. {
  91. if(isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
  92. return true;
  93. } else {
  94. return false;
  95. }
  96. }
  97. }
  98. static public function is_man() {
  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. }
  107. else {
  108. return 0;
  109. }
  110. }
  111. static function avatar()
  112. {
  113. if(empty($_SESSION['member_avatar']))
  114. {
  115. if(array_key_exists('wx_author',$_SESSION))
  116. {
  117. if(array_key_exists('handled',$_SESSION['wx_author']))
  118. {
  119. $user_info = $_SESSION['wx_author']['user_info'];
  120. return $user_info['headimgurl'];
  121. }
  122. }
  123. if(self::is_man()) {
  124. $img = 'male.png';
  125. } else {
  126. $img = 'female.png';
  127. }
  128. return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
  129. }
  130. else
  131. {
  132. $path = $_SESSION['member_avatar'];
  133. if(strncasecmp($path,"http://",strlen("http://")) == 0) {
  134. $url = $path;
  135. } else {
  136. $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
  137. }
  138. return $url;
  139. }
  140. }
  141. static public function cur_mobile()
  142. {
  143. if(self::isVerfiyMobile()) {
  144. return $_SESSION['member_mobile'];
  145. } else {
  146. return '';
  147. }
  148. }
  149. static public function parase_wxinfo($wxinfo)
  150. {
  151. $user = urldecode($wxinfo);
  152. if(empty($user)) return false;
  153. $user = json_decode($user,true);
  154. if($user == false) return false;
  155. $info = array();
  156. $info['member_nickname'] = $user['nickname'];
  157. $info['member_avatar'] = $user['headimgurl'];
  158. $info['member_sex'] = intval($user['sex']);
  159. return $info;
  160. }
  161. static public function filter_info($info)
  162. {
  163. $member_info = array();
  164. if(isset($info['member_nickname']) && !empty($info['member_nickname'])) {
  165. $member_info['member_nickname'] = $info['member_nickname'];
  166. }
  167. if(isset($info['member_truename']) && !empty($info['member_truename'])) {
  168. if(!isset($member_info['member_nickname'])) {
  169. $member_info['member_nickname'] = $info['member_truename'];
  170. }
  171. $member_info['member_truename'] = $info['member_truename'];
  172. }
  173. if(isset($info['member_mobile']) && !empty($info['member_mobile']))
  174. {
  175. $mobile = $info['member_mobile'];
  176. if(!isset($member_info['member_nickname'])) {
  177. $member_info['member_nickname'] = substr_replace($mobile, '****', 3, 4);
  178. }
  179. $member_info['member_mobile'] = $info['member_mobile'];
  180. }
  181. return $member_info;
  182. }
  183. static public function filter_member_info($member_info,$openid)
  184. {
  185. field_helper::validate_null_string($member_info, 'member_mobile,member_wxopenid,member_name,' .
  186. 'member_truename,member_signname,member_avatar,member_email,member_birthday');
  187. field_helper::validate_null_string($member_info, 'member_sex', '0');
  188. field_helper::validate_null_string($member_info, 'member_nickname', substr_replace($openid, '****', 3, 4));
  189. return $member_info;
  190. }
  191. static public function parse_contacts($contacts)
  192. {
  193. if(!isset($contacts)) {
  194. return false;
  195. }
  196. $contacts = json_decode(urldecode($contacts));
  197. if($contacts == false) {
  198. return false;
  199. }
  200. $ar_contact = array();
  201. foreach ($contacts as $mobile)
  202. {
  203. $mobile = session_helper::mobile_valid($mobile);
  204. if($mobile == false) {
  205. continue;
  206. }
  207. if(algorithm::bsearch($mobile,$ar_contact) != -1) {
  208. continue;
  209. }
  210. array_push($ar_contact,$mobile);
  211. sort($ar_contact);
  212. }
  213. return $ar_contact;
  214. }
  215. static public function session_id() {
  216. return empty($_SESSION['MPHPSESSID']) ? "" : $_SESSION['MPHPSESSID'];
  217. }
  218. static public function need_wechat_author()
  219. {
  220. if(BASE_SITE_URL != 'http://p.lrlz.com') {
  221. return false;
  222. }
  223. if(util::from_wechat() == false) {
  224. return false;
  225. }
  226. else
  227. {
  228. if(array_key_exists('wx_author',$_SESSION))
  229. {
  230. if(array_key_exists('handled',$_SESSION['wx_author']))
  231. {
  232. $user_info = $_SESSION['wx_author']['user_info'];
  233. $loginer = new \login\unionid_log($user_info['unionid']);
  234. if(self::logined()) {
  235. $loginer->bind($user_info);
  236. $loginer->login();
  237. unset($_SESSION['wx_author']);
  238. }
  239. else
  240. {
  241. if($loginer->ismember() == true)
  242. {
  243. $loginer->bind($user_info);
  244. $loginer->login();
  245. unset($_SESSION['wx_author']);
  246. }
  247. }
  248. }
  249. return false;
  250. }
  251. $author_time = $_SESSION['wxauthor_time'];
  252. $max_time = 2 * 86400;
  253. if(time() - $author_time > $max_time) {
  254. return true;
  255. }
  256. if(!empty($_SESSION['member_wxunionid']) && !empty($_SESSION['member_wxopenid']))
  257. {
  258. return false;
  259. }
  260. else {
  261. return true;
  262. }
  263. }
  264. }
  265. static public function pub_openid()
  266. {
  267. if(empty($_SESSION['member_wxopenid'])) {
  268. return false;
  269. } else {
  270. return $_SESSION['member_wxopenid'];
  271. }
  272. }
  273. static public function unionid()
  274. {
  275. if(!empty($_SESSION['member_wxunionid'])) {
  276. return $_SESSION['member_wxunionid'];
  277. }
  278. if(array_key_exists('wx_author',$_SESSION))
  279. {
  280. if(array_key_exists('handled',$_SESSION['wx_author']))
  281. {
  282. $user_info = $_SESSION['wx_author']['user_info'];
  283. return $user_info['unionid'];
  284. }
  285. }
  286. return false;
  287. }
  288. static public function thief($fromid,&$err)
  289. {
  290. $thief = new bonus\thief_vilator($fromid);
  291. return $thief->thief($err);
  292. }
  293. static public function first_order()
  294. {
  295. if (array_key_exists('order_num', $_SESSION)) {
  296. $order_num = $_SESSION['order_num'];
  297. }
  298. else
  299. {
  300. $mod_member = Model('member');
  301. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  302. if(empty($minfo)) return false;
  303. $order_num = intval($minfo['order_num']);
  304. if($order_num > 0) {
  305. $_SESSION['order_num'] = $order_num;
  306. }
  307. }
  308. if($order_num == 0) {
  309. return true;
  310. }
  311. else {
  312. return false;
  313. }
  314. }
  315. }