Auth.class.php
25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
<?php
/**
* 权限认证,账户操作类
*
* @author XMG 2014.2.7
* @copyright yoho.cn
* @name Facade_Auth
* @version 0.1
*
*/
class Facade_Auth
{
/**
* 账号类型 - 邮箱
* @var int
*/
const AUTH_TYPE_EMAIL = 1 ;
/**
* 账号类型 - 手机
* @var int
*/
const AUTH_TYPE_MOBILE = 2 ;
/**
* 账号类型 - 微博
* @var int
*/
const AUTH_TYPE_WEIBO = 3 ;
/**
* 账号类型 - QQ
* @var int
*/
const AUTH_TYPE_QQ = 4 ;
/**
* 账号类型 - 支付宝
* @var int
*/
const AUTH_TYPE_ALIPAY = 5 ;
/**
* 账号类型 - 人人
* @var int
*/
const AUTH_TYPE_RENREN = 6 ;
/**
* 账号类型 - 豆瓣
* @var int
*/
const AUTH_TYPE_DOUBAN = 7 ;
/**
* 账号类型 - FACEBOOK
* @var int
*/
const AUTH_TYPE_FACEBOOK = 8 ;
/**
* 账号类型 - INSTAGRAM
* @var int
*/
const AUTH_TYPE_INSTAGRAM = 9 ;
/**
* 账号类型 - 腾讯微博
* @var int
*/
const AUTH_TYPE_QQ_WB = 10;
/**
* 账号类型 - 微信
* @var int
*/
const AUTH_TYPE_WEXIN = 11;
/**
* 数据层
*
* @var Service_Admin_Auditing
*/
private static $service = null;
/**
* 数据层 -- 登录
*
* @var Service_Admin_Auditing
*/
private static $service_session = null;
//COOKIE配置
const COOKIE_MERGED = 'yh_passport';
const PERSIST_LIFE = 2592000; // 一个月
const AUTH_EXPIRE = 60;
/**
* 返回Service对象
*
* @return Service_Auth
*/
public static function service ()
{
if ( self::$service == null )
{
self::$service = new Service_Auth();
}
return self::$service;
}
/**
* 返回SESSION服务对象
*
* @return Service_Session
*/
public static function serviceSession ()
{
if ( self::$service_session == null )
{
self::$service_session = new Service_Session();
}
return self::$service_session;
}
/**
* 返回允许的Auth类型
*/
public static function getAllowAuthType()
{
return array
(
self::AUTH_TYPE_EMAIL => '邮箱',
self::AUTH_TYPE_MOBILE => '手机号',
self::AUTH_TYPE_WEIBO => '新浪Weibo',
self::AUTH_TYPE_QQ => 'QQ',
self::AUTH_TYPE_ALIPAY => '支付宝',
self::AUTH_TYPE_RENREN => '人人网',
self::AUTH_TYPE_DOUBAN => '豆瓣网',
self::AUTH_TYPE_FACEBOOK => 'FACE BOOK',
self::AUTH_TYPE_INSTAGRAM => 'INSTAGRAM',
self::AUTH_TYPE_WEXIN => 'wechat'
);
}
/**
* 校验是否为手机
* @param unknown $mobile
*/
public static function isMobile($mobile)
{
return preg_match('/^1[3|4|5|7|8][0-9]|999\d{8}$/', $mobile);
}
/**
* 构建密码及盐 如果指定盐,则使用该盐进行加密密码 ,否则,自动生成盐
* @param string $password 明文密码
* @param string $salt 盐
*/
public static function buildPassword($password,$salt = '')
{
$password = Lib_AuthCode::makePass($password);
return array('password' => $password, 'salt' => '' );
}
/**
* 比对检测密码是否正确 ,是否允许登录
* @param unknown $inputPassword 明文密码
* @param unknown $encryptPassword 加密密码
* @param unknown $salt 盐
*/
public static function ComparisonPassword($inputPassword, $encryptPassword)
{
$passwordList = explode(':', trim($encryptPassword));
$crypt = $passwordList[0];
$salt = '';
if (count($passwordList) == 2)
{
$salt = $passwordList[1];
}
$decode = Lib_AuthCode::getCrypted($inputPassword . $salt, $salt);
if ($crypt != $decode)
{
return false;
}
return true;
}
/**
* 设置或更新密码
* @param unknown $uid
* @param unknown $password
* @param unknown $password_salt
*/
public static function setPassword($uid, $password,$checkUser = false )
{
do
{
if (!$uid || !$password)
{
$ret = array('code' => Config_Code::$error['missing_parameter']['code'],'message' => Config_Code::$error['missing_parameter']['message']);
break ;
}
if (strlen($password) < 6)
{
$ret = array('code' => Config_Code::$error['pwd_format']['code'], 'message' => Config_Code::$error['pwd_format']['message']);
break ;
}
if ($checkUser)
{
if (!Facade_Userinfo::getUserinfoByUid($uid))
{
$ret = array('code' => Config_Code::$error['user_not_exist']['code'],'message' => Config_Code::$error['user_not_exist']['message']);
break ;
}
}
$pwd_info = self::buildPassword($password);
$update = self::service()->setPassword($uid, $pwd_info['password'], '' );
if ($update)
{
$ret = true;
break ;
}
$ret = array('code' => Config_Code::$error['system']['code'],'message' => Config_Code::$error['system']['message']);
break ;
}while (false);
return $ret ;
}
/**
* 设置登录验证信息
*
* @param integer $uid 用户ID
* @param integer $auth_type 验证的类型
* @param string $auth_id 验证的信息,与$auth_type具有唯一性
* @return boolean, array
*/
public static function setAuth($uid, $auth_type, $auth_id)
{
do
{
// 校验参数
if (!$uid || !$auth_type || !$auth_id)
{
$ret = array('code' => Config_Code::$error['missing_parameter']['code'], 'message' => Config_Code::$error['missing_parameter']['message']);
break;
}
// 检测账号类型
$allowAuthType = self::getAllowAuthType();
if (!isset($allowAuthType[$auth_type]))
{
$ret = array('code' => Config_Code::$error['account_type']['code'], 'message' => Config_Code::$error['account_type']['message']);
break;
}
// 获取用户信息 -- 校验用户是否存在
$userinfo = Facade_Userinfo::getUserinfoByUid($uid);
if (!$userinfo)
{
$ret = array('code' => Config_Code::$error['user_not_exist']['code'], 'message' => Config_Code::$error['user_not_exist']['message']);
break;
}
//TODO 判断是否存在当前账号是否已经存在
//$exist_auth = self::getAuthByTypeAndAid($auth_type, $auth_id);
$exist_auth = self::getAuthByTypeAndAidNocache($auth_type, $auth_id);
if(!empty($exist_auth))
{
$ret = array('code' => Config_Code::$error['account_exist']['code'], 'message' => Config_Code::$error['account_exist']['message']);
break;
}
// 设置auth
if (!self::service()->setAuth($uid, $auth_type, $auth_id) )
{
$ret = array('code' => Config_Code::$error['account_set']['code'], 'message' => Config_Code::$error['account_set']['message']);
break;
}
$ret = true;
} while (false);
return $ret;
}
/**
* 解除绑定
* @param unknown $id
* @param unknown $type
*/
public static function delAuth($uid, $type,$auth_id)
{
do
{
if (!$uid || !$type || !$auth_id)
{
$ret = array('code' => Config_Code::$error['missing_parameter']['code'], 'message' => Config_Code::$error['missing_parameter']['message']);
break;
}
//获取所有账号
$all_auth = Facade_Auth::getAllAuth($uid);
if ($all_auth && count($all_auth) <= 1)
{
//不允许解除绑定
$ret = array('code' => Config_Code::$error['account_only_one']['code'], 'msg' => Config_Code::$error['account_only_one']['message']);
break;
}
//删除绑定
if (self::service()->delAuth($uid, $type, $auth_id))
{
return true;
}
$ret = array('code' => Config_Code::$error['system']['code'], 'msg' => Config_Code::$error['system']['message']);
break;
}while (false);
return $ret ;
}
/**
* 修改登录账号
*
* @param integer $id 唯一的SSO ID
* @param integer $type 登录类型
* @param string $auth_id 原来的登录账号
* @param string $new_auth_id 新的登录账号
* @return boolean (false:失败, true:成功)
*/
public static function updAuth($id, $type, $auth_id, $new_auth_id)
{
$result = false;
if (is_numeric($id) && is_numeric($type) && isset($auth_id) && isset($new_auth_id))
{
$result = self::service()->updAuth($id, $type, $auth_id, $new_auth_id);
}
return $result;
}
/**
* 获取登录账号的信息
*
* @param type $type 登录类型
* @param type $auth_id 登录账号
* @return array
*/
public static function getAuthByTypeAndAid($type, $auth_id)
{
$result = array();
if (isset($auth_id) && is_numeric($type))
{
$result = self::service()->getAuthByTypeAndAid($type, $auth_id);
if(!$result)
{
if($type==self::AUTH_TYPE_FACEBOOK)
{
$auth_id = self::service()->getFacebookRela($auth_id);
$result = self::service()->getAuthByTypeAndAid($type, $auth_id);
}
}
}
return $result;
}
/**
* 获取登录账号的信息 没缓存
*
* @param type $type 登录类型
* @param type $auth_id 登录账号
* @return array
*/
public static function getAuthByTypeAndAidNocache($type, $auth_id)
{
$result = array();
if (isset($auth_id) && is_numeric($type))
{
$result = self::service()->getAuthByTypeAndAidNocache($type, $auth_id);
if(!$result)
{
if($type==self::AUTH_TYPE_FACEBOOK)
{
$auth_id = self::service()->getFacebookRela($auth_id);
$result = self::service()->getAuthByTypeAndAidNocache($type, $auth_id);
}
}
}
return $result;
}
/**
* 获取所有口令列表
*/
public static function getAllAuth($uid)
{
if (!$uid)
{
return array();
}
return self::service()->getAllAuth($uid);
}
/**
* 注册 -- 供邮箱和手机号注册
* @param string $account
* @param string $pwd
*/
public static function register($account, $password ,$app_id,$area = '')
{
$auth_type = $uid = null ;
do
{
//验证账户
if ($account == '')
{
$ret = array('code' => Config_Code::$error['account_format']['code'], 'message' => Config_Code::$error['account_format']['message']);
break ;
}
$service = self::service();
// 验证账号类型
//2015-4-13 wuxiao app增加国际手机号登录注册
if (self::isMobile($account))
{//如果符合国内手机号规则
//手机
$auth_type = self::AUTH_TYPE_MOBILE ;
//如果area非空并且area不是86,就是国际手机号
if (!empty($area) && (trim((string)$area, '0') != '86')){
$area = trim((string)$area, '0');
$account = "00{$area}-{$account}";
}
}elseif (is_numeric($account) && !empty($area) && (trim((string)$area, '0') != '86'))
{//如果不符合国内手机号规则但是是数字,必须带有area并且are不是86,就是国际手机号
$auth_type = self::AUTH_TYPE_MOBILE ;
$area = trim((string)$area, '0');
$account = "00{$area}-{$account}";
}elseif (filter_var($account,FILTER_VALIDATE_EMAIL))
{
//邮箱
$auth_type = self::AUTH_TYPE_EMAIL ;
}else
{
//其它 -- 系统错误
$ret = array('code' => Config_Code::$error['account_format']['code'], 'message' => Config_Code::$error['account_format']['message']);
break ;
}
//验证密码
if ($password == '')
{
$ret = array('code' => Config_Code::$error['pwd_format']['code'], 'message' => Config_Code::$error['pwd_format']['message']);
break ;
}
if (strlen($password) < 6)
{
$ret = array('code' => Config_Code::$error['pwd_format']['code'], 'message' => Config_Code::$error['pwd_format']['message']);
break ;
}
//检测用户是否存在
$auth_info = $service->getAuthByTypeAndAid($auth_type, $account);
if ($auth_info)
{
$ret = array('code' => Config_Code::$error['user_exist']['code'], 'message' => Config_Code::$error['user_exist']['message']);
break ;
}
//创建用户
$userinfo = array('gender' => 3, 'app_id' => $app_id);//初始化用户信息--必须
$uid = $service->setUserinfo($userinfo);
if (!$uid)
{
$ret = array('code' => Config_Code::$error['system']['code'], 'message' => Config_Code::$error['system']['code']);
break ;
}
$pwd_info = self::buildPassword($password);
//绑定登录账号
$bind = $service->setAuth($uid, $auth_type, $account);
//设置密码
$setPwd = self::setPassword($uid, $password);
if (!$bind || $setPwd !== true)
{
//回滚用户信息--删除该用户
$service->_deleteUserinfo($uid);
$ret = array('code' => Config_Code::$error['system']['code'], 'message' => Config_Code::$error['system']['code']);
break ;
}
//成功返回
$ret = array('code' => Config_Code::$success['user_register']['code'], 'message' => Config_Code::$success['user_register']['message'],'uid' => $uid);
}while (false);
return $ret ;
}
/**
* 根据账号获取用户auth信息
* @param unknown $account
* @param string $auth_type
*/
public static function getAuthByAccount($account , $auth_type = null, $area = '')
{
$account = trim($account);
if (!$account)
{
return array();
}
if (!empty($area) && (trim((string)$area, '0') != '86')){
$area = trim((string)$area, '0');
$account = "00{$area}-{$account}";
$auth_type = self::AUTH_TYPE_MOBILE ;
}
//如果没有强制指定账号类型,则计算其是否为邮箱或手机号
if (!$auth_type)
{
// 验证账号类型
if (self::isMobile($account))
{
//手机
$auth_type = self::AUTH_TYPE_MOBILE ;
}elseif (filter_var($account,FILTER_VALIDATE_EMAIL))
{
//邮箱
$auth_type = self::AUTH_TYPE_EMAIL ;
}else
{
return array();
}
}
$ret = self::service()->getAuthByTypeAndAid($auth_type, $account);
return $ret ? $ret : array() ;
}
/**
* 获取密码
* @param unknown $uid
*/
public static function getPassword($uid)
{
$ret = self::service()->getPassword($uid) ;
return isset($ret['password'])?$ret['password']:'';
}
/**
* 检测登录
* @param string $account 账号
* @param string $persistent 是否自动登录
* @param string $inputPassword 密码
* @param boolean $is_login 是否登录
* @return array array(code => message )
* code:
* 200: 成功登录
* 500: 系统错误
* 400001: 对不起,该用户不存
* 400002: 密码错误
* 400003: 由于涉及违规操作,该账号已被封
*/
public static function checkLogin ($account, $inputPassword, $setCookie = true, $persistent = 0 , $is_login = true, $area = '')
{
$auth_type = null ;
do
{
if (!$account || !$inputPassword)
{
$ret = array('code' => Config_Code::$error['missing_parameter']['code'], 'message' => Config_Code::$error['missing_parameter']['message']);
break ;
}
// 验证账号类型
//2015-4-13 wuxiao app增加国际手机号登录注册
if (self::isMobile($account))
{//如果符合国内手机号规则
//手机
$auth_type = self::AUTH_TYPE_MOBILE ;
//如果area非空并且area不是86,就是国际手机号
if (!empty($area) && (trim((string)$area, '0') != '86')){
$area = trim((string)$area, '0');
$account = "00{$area}-{$account}";
}
}elseif (is_numeric($account) && !empty($area) && (trim((string)$area, '0') != '86'))
{//如果不符合国内手机号规则但是是数字,必须带有area并且are不是86,就是国际手机号
$auth_type = self::AUTH_TYPE_MOBILE ;
$area = trim((string)$area, '0');
$account = "00{$area}-{$account}";
}elseif (filter_var($account,FILTER_VALIDATE_EMAIL))
{
//邮箱
$auth_type = self::AUTH_TYPE_EMAIL ;
}else
{
$ret = array('code' => Config_Code::$error['account_format']['code'], 'message' => Config_Code::$error['account_format']['message']);
break ;
}
//获取用户口令信息
$auth = self::service()->getAuthByTypeAndAidNocache($auth_type, $account);
//检测口令是否存在
if (!$auth)
{
//file_put_contents('/tmp/sso-'.date('Y-m-d').'.log', '登录Facade错误['.date('H:i:s').']-账户:'.$account.' 类型:'.$auth_type.' 地区码:'.$area."\n", FILE_APPEND);
$ret = array('code' => Config_Code::$error['user_not_exist']['code'], 'message' => Config_Code::$error['user_not_exist']['message']);
break ;
}
$uid = $auth['uid'] ;
//是否禁用
if (Facade_Userinfo::isDisabled($uid))
{
$ret = array('code' => Config_Code::$error['user_disabled']['code'], 'message' => Config_Code::$error['user_disabled']['message']);
break ;
}
$pwd_info = self::service()->getPassword($uid) ;
if (!$pwd_info)
{
$ret = array('code' => Config_Code::$error['pwd_error']['code'], 'message' => Config_Code::$error['pwd_error']['message']);
break ;
}
//比对密码是否正确
if(!self::ComparisonPassword($inputPassword, $pwd_info['password'], $pwd_info['password_salt']))
{
$ret = array('code' => Config_Code::$error['pwd_error']['code'], 'message' => Config_Code::$error['pwd_error']['message']);
break ;
}
//如果不需要登录,仅检验,则直接返回用户信息
if ($is_login === false)
{
$ret = array('code' => Config_Code::$success['user_login']['code'],'message' => Config_Code::$success['user_login']['message'],'uid' => $uid);
break ;
}
// 是否自动登录
$persistent = $persistent ? true : false;
// 是否要设置cookie. 在手机登录的时候, 没有cookie
$loginRet = self::login($uid, $persistent, $setCookie);
if(!$loginRet)
{
//系统错误
$ret = array('code' => Config_Code::$error['system']['code'],'message' => Config_Code::$error['system']['message']);
break;
}
$ret = array('code' => Config_Code::$success['user_login']['code'],'message' => Config_Code::$success['user_login']['message'],'uid' => $uid);
}while (false);
return $ret ;
}
/**
* 登录操作
*
* @param $uid integer
* @param $rememberme boolean
* @return boolean
*/
public static function login ($uid, $rememberMe = true, $setCookie = true)
{
//@todo xiaoma 2013.3.8 校验当前是否已登录,如果已经登录,则不再进行重新登录,否则一直存入Session表
$login_uid = self::getAuthedUid() ;
$session = self::serviceSession();
$agent = $session->getAgentTxt() ;
$sessionKey = false;
if ($login_uid == $uid && $agent == 'web' && !empty($uid))
{
$sessionKey = $session->getUserLastSession($uid);
}
else
{
//@FIXED 先检查是否有该用户,否则不予登录 XMG - 2014.03.27 00:38
if (!Facade_Userinfo::getUserinfoByUid($uid))
{
return false ;
}
$sessionKey = self::createSessionKey ($uid, $rememberMe);
if (isset($sessionKey) && !empty($sessionKey) && $setCookie)
{
self::setAuthedCookie($uid, $sessionKey, $rememberMe);
}
}
return $sessionKey ? $sessionKey : false ;
}
/**
* 登出系统
*
* @param int $set_uid 指定强制退出的用户ID
* @return true
*/
public static function logout ($set_uid = 0)
{
$uid = false;
// 是否是管理员制定强制退出的
$adminKick = false;
if ($set_uid)
{
$uid = $set_uid;
$adminKick = true;
}
else
{
$uid = self::getAuthedUid();
}
if ($uid)
{
$status = false;
// 如果是管理员退出的, 则删除所有此用户的session
if ($adminKick)
{
$status = self::serviceSession()->deleteSessionByUid($uid);
}
else
{
$sessionkey = self::getAuthedCookieSession();
// 否则只删除当前的
$status = self::serviceSession()->deleteSession($sessionkey);
}
}
if ($set_uid === 0)
{
// 删除新系统的登录Cookie
self::unsetAuthedCookie();
}
return true;
}
/**
* 得到浏览器端用户cookie's sessionkey
*
* @return string | false
*/
public static function getAuthedCookieSession ()
{
return ($a = self::getAuthedCookieArray()) ? $a['magic'] : false;
}
/**
* 得到Session中的用户编码
*
* @return integer false
*/
public static function getAuthedUid ()
{
$return = false;
$aAuth = self::getAuthedCookieArray();
if ($aAuth)
{
$aSession = self::serviceSession()->getSession($aAuth['magic']);
if ($aSession)
{
$return = $aSession['uid'];
}
else
{
self::unsetAuthedCookie();
}
}
return $return;
}
/**
* 得到浏览器端COOKIE 验证数组
*
* @return array | false
*/
public static function getAuthedCookieArray ()
{
$return = false;
if (! isset($_COOKIE[self::COOKIE_MERGED]))
{
$return = false;
}
else
{
$values = explode(',', $_COOKIE[self::COOKIE_MERGED]);
$uid = isset($values[0]) ? (int) $values[0] : false;
//sessionkey
$magic = isset($values[1]) ? self::authCookieCryptDecode($values[1]) : false;
$persist = isset($values[2]) ? $values[2] === 'y' : false;
$authExpire = isset($values[3]) ? intval($values[3], 10) : time() + self::AUTH_EXPIRE;
$return = array('uid' => $uid, 'magic' => $magic, 'persist' => $persist,
'authexpire' => $authExpire);
}
return $return;
}
/**
* 得到已经登录的用户session全部信息
*
* @return array false
*/
public static function getAuthedSessionArray ()
{
$return = false;
$aAuth = self::getAuthedCookieArray();
$aSession = self::serviceSession()->getSession($aAuth['magic']);
if ($aAuth && $aSession && $aSession['uid'] == $aAuth['uid'])
{
$return = $aSession;
}
else
{
self::unsetAuthedCookie();
}
return $return;
}
/**
* 根据SID获取用户UID
* @param unknown $sid
*/
public static function getSessionBySid($sid)
{
return self::serviceSession()->getSession($sid);
}
/**
* 解密存储到浏览器端的cookie内容
*
* @param $string string
* @return string
*/
private static function authCookieCryptDecode ($string)
{
$str1 = substr($string, 0, 16);
$str2 = substr($string, - 16);
$return = $str1 . $str2;
return strlen($return) == 32 ? $return : '';
}
/**
* 摧毁客户端用户Cookie
*
* @return void
*/
private static function unsetAuthedCookie ()
{
$time = time() - self::PERSIST_LIFE;
if (isset($_COOKIE[self::COOKIE_MERGED]))
{
setcookie(self::COOKIE_MERGED, null, $time, '/', COOKIE_DOMAIN);
}
}
/**
* 设置浏览器端用户cookie(uid+sesionkey+是否有效+过期时间)
*
* @param $uid integer 用户编码
* @param $sessionkey string
* @param $persistent boolean
* @return boolean
*/
private static function setAuthedCookie ($uid, $sessionkey, $persistent = true)
{
$expire = $persistent ? time() + self::PERSIST_LIFE : 0;
$authExpire = time() + self::AUTH_EXPIRE;
if ($sessionkey)
{
$values = $uid . ',' . self::authCookieCryptEncode($sessionkey) .
',' . ($persistent ? 'y' : 'n') . ',' . $authExpire;
//TODO,记录用户登录信息(用户UID, IP,sessionkey 时间, agent, 用户类型)
return setcookie(self::COOKIE_MERGED, $values, $expire, '/', COOKIE_DOMAIN);
}
else
{
return false;
}
}
/**
* 创建SessionKey
* @param unknown $uid
* @param string $rememberMe
* @return Ambigous <NULL, integer, unknown, string, Sring, long, number, multitype:NULL , boolean>
*/
private static function createSessionKey ($uid, $rememberMe = true)
{
$sessionKey = null;
$create = self::serviceSession()->createSession($uid,$rememberMe);
if ($create)
{
$sessionKey = $create['sessionkey'];
}
return $sessionKey;
}
/**
* 加密存储到浏览器端的cookie内容信息
*
* @param $string string
* @return string
*/
private static function authCookieCryptEncode ($string)
{
$str1 = substr($string, 0, 16);
$str2 = substr(md5('crypt.yoho.cn' . mt_rand(1, 999999)), 0, 8);
$str3 = substr($string, 16);
return $str1 . $str2 . $str3;
}
}