Abstract.php
13 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
<?php
abstract class YHMAuth_User_Abstract implements YHMAuth_User_Interface
{
protected $_passwordType = 'md5-hex';
protected $_channel = 'web';
protected $_session = array();
protected $_cookieLifttime = 31536000;
protected $_secretKey = '';
abstract public function signin($profile, $inputPassword = '', $persistent = 'Y', $append = array());
abstract public function register($profile, $password, array $append = array());
abstract public function signOut($token);
abstract function associate($profile, array $append = array());
protected $_channelMap = array();
protected $_uid = 0;
/**
* 渠道到
* @param $channel
*/
public function __construct($channel)
{
if (!isset(YHMAuth_Maps::$channelMap[$channel])) {
throw new Exception('没有 ' . $channel . ' 这个渠道.');
}
$this->setChannel($channel);
}
public function passwordType($type = 'md5-hex')
{
$this->_passwordType = $type;
return $this;
}
public function setChannel($channel = 'web')
{
$this->_channel = $channel;
$this->_channelMap = YHMAuth_Maps::$channelMap[$channel];
return $this;
}
/**
* @param $namespace
* @return mixed
*/
private function _initSession($namespace)
{
if (empty($this->_session[$namespace])) {
$this->_session[$namespace] = new Q_Core_SessionNamespace ($namespace);
}
return $this->_session[$namespace];
}
/**
* 获取Session
*
* @param string $key
* @param string $namespace
* @param string $sessionKey
* @return int
*/
public function getSession($key = 'uid', $namespace = 'passport_sys', $sessionKey = 'profile_info')
{
$sessionData = $this->_initSession($namespace)->__get($sessionKey);
if (empty ($sessionData) || empty ($sessionData [$key])) {
return 0;
}
return $sessionData [$key];
}
/**
* 存Session
* @param $sessionData
* @param string $namespace
* @param string $sessionKey
* @return mixed
*/
public function setSession($sessionData, $namespace = 'passport_sys', $sessionKey = 'profile_info')
{
$sessionData ['ip'] = Q_Utils_Request::getClientIp();
$sessionData ['token_session_key'] = md5(uniqid(mt_rand(), true) . '_' . $sessionData ['ip']);
return $this->_initSession($namespace)->__set($sessionKey, $sessionData);
}
/**
* 删除Session
*
* @param string $namespace
* @param string $session_key
* @return true
*/
public function unsetSession($namespace = 'passport_sys', $sessionKey = 'profile_info')
{
return $this->_initSession($namespace)->__unset($sessionKey);
}
/**
* 获取 domain
* @return string
*/
protected function _domain()
{
if (ip2long($_SERVER['HTTP_HOST']) == false) {
return '.' . preg_replace("/^(.*\.)?([^.]*\..*)$/", "$2", $_SERVER['HTTP_HOST']);
}
return '';
}
/**
* 设置Cookie
* @param string $sessionKey
* @param $value
* @param int $expire
* @param null $domain
*/
public function setCookieID($value, $domain = null, $sessionKey = '_UID', $expire = 31536000)
{
$_domain = ($domain !== null) ? $domain : $this->_domain();
if ($expire > 0) {
$expire = time() + $expire;
}
Q_Core_Cookie::setCookie($sessionKey, $_domain, $value, $expire);
}
/**
* 组合账户名
* @param array $_baseProfile
* @param array $_profile
* @return string
*/
protected function _makeNickName(array $_profile = array(), $_baseProfile = array())
{
if (!empty ($_baseProfile ['nickname'])) {
$profileName = $_baseProfile ['nickname'];
} elseif (!empty ($_baseProfile ['username'])) {
$profileName = $_baseProfile ['username'];
} elseif (!empty($_profile['email'])) {
$profileName = $_profile ['email'];
} elseif (!empty($_profile['mobile'])) {
$profileName = $_profile ['mobile'];
} else {
$profileName = 'YOHO!';
}
return $profileName;
}
/**
* 处理钩子
* @param array $package
* @param $dispatchName
*/
protected function _dispatch(array $package, $dispatchName)
{
$defaultDispatch = array();
$channelDispatch = array();
if (!empty(YHMAuth_Maps::$defaultDispatch[$dispatchName])) {
$defaultDispatch = YHMAuth_Maps::$defaultDispatch[$dispatchName];
}
if (!empty($this->_channelMap[$dispatchName])) {
$channelDispatch = $this->_channelMap[$dispatchName];
}
$_fun = array_merge($defaultDispatch, $channelDispatch);
if (!empty($_fun)) {
foreach ($_fun as $fun) {
if (method_exists($fun, 'run')) {
try {
$fun::run($package);
} catch (Exception $e) {
if ($fun::catchException() == true) {
throw new Exception($e->getMessage(), $e->getCode());
}
}
}
}
}
}
/**
* 登录前处理( 对数据验证后 )
* @param array $package
*/
protected function signinPreDispatch($profile, $inputPassword, array $package = array())
{
$package['profile'] = $profile;
$package['inputPassword'] = $inputPassword;
$this->_dispatch($package, 'signin_preDispatch');
}
/**
* 登录结束后处理
* @param array $package
*/
protected function signinEndDispatch($uid, array $package = array())
{
$package['uid'] = $uid;
$this->_dispatch($package, 'signin_endDispatch');
}
/**
* 注册前处理
* @param array $package
*/
protected function registerPreDispatch($profile, $password, array $package = array())
{
$package['profile'] = $profile;
$package['password'] = $password;
$this->_dispatch($package, 'register_preDispatch');
}
/**
* 注册后处理
* @param array $package
*/
protected function registerEndDispatch($uid, array $package = array())
{
$package['uid'] = $uid;
$this->_dispatch($package, 'register_endDispatch');
}
/**
* 退出前处理
* @param array $package
*/
protected function signOutPreDispatch($uid, array $package = array())
{
$package['uid'] = $uid;
$this->_dispatch($package, 'signOut_preDispatch');
}
/**
* 退出后处理
* @param array $package
*/
protected function signOutEndDispatch($uid, array $package = array())
{
$package['uid'] = $uid;
$this->_dispatch($package, 'signOut_endDispatch');
}
/**
* 联合登录前处理
* @param array $package
*/
protected function associatePreDispatch($profile, array $package = array())
{
$package['profile'] = $profile;
$this->_dispatch($package, 'associate_preDispatch');
}
/**
* 联合登录后处理
* @param array $package
*/
protected function associateEndDispatch($uid, array $package = array())
{
$package['uid'] = $uid;
$this->_dispatch($package, 'associate_endDispatch');
}
/**
* 设置当前UID
* @param $uid
* @return $this
*/
public function setUid($uid)
{
$this->_uid = $uid;
return $this;
}
/**
* 获取登录后的信息
* @return array
*/
public function getProfile()
{
$profile = QINPassport_Models_Profile_Client::getProfileByUid($this->_uid);
if (empty($profile)) {
return array();
}
$_baseProfile = QINPassport_Models_Profile_Base_Client::getBaseInfoByUid($this->_uid);
if (empty($_baseProfile)) {
$_baseProfile = array();
}
return array(
'uid' => $this->_uid,
'email' => $profile['email'],
'mobile' => $profile['mobile'],
'password' => $profile['password'],
'username' => $this->_makeNickName($profile, $_baseProfile),
'session_key' => $this->getSession('token_session_key')
);
}
/**
* 修改密码
* @param $uid
* @param $newPassword
* @param $originalPassword
* @param $isVerify
* @return int
*/
public function changePassword($uid, $newPassword, $originalPassword = '', $isVerify = 'Y')
{
//throw new Exception('系统故障无法修改密码,请稍后再试.');
$_profile = QINPassport_Models_Profile_Client::getProfileByUid($uid);
if (empty($_profile)) {
throw new Exception('找不到用户信息.', 10006);
}
if ($isVerify == 'Y') {
$authPass = Q_Utils_AuthCode::authPassword($originalPassword, $_profile['password']);
if ($authPass === false) {
throw new Exception('原始密码错误.', 10003);
}
}
$_password = Q_Utils_AuthCode::makePass($newPassword);
return QINPassport_Models_Profile_Client::updatePasswordByUid($_password, $uid);
}
/**
* 密码校验
* @param $inputPassword
* @param $uid
* @return bool
*/
public function verifyPassword($inputPassword, $uid)
{
$profile = QINPassport_Models_Profile_Client::getProfileByUid($uid);
if (empty($profile)) {
return false;
}
$password = $profile['password'];
if (strpos($password, ':') === false) {
$authPass = md5($inputPassword) === strtolower($password) ? true : false;
$authPass === true ? QCPassport_Models_Base_Client::upPassword($uid, $inputPassword) : false;
} else {
$authPass = Q_Utils_AuthCode::authPassword($inputPassword, $password);
}
return $authPass;
}
/**
* 获取当前登录的用户UID
* @return int
*/
public function getUid()
{
return $this->_uid;
}
/**
* 修改用户信息
* @param $uid
* @param $email
* @param $mobile
* @return bool
*/
public function updateUserinfo($uid, $email, $mobile)
{
if ((empty($email) && empty($mobile)) || empty($uid)) {
return false;
}
if (!empty($email)) {
QINPassport_Models_Profile_Client::updateEmailByUid($email, (int)$uid);
}
if (!empty($mobile)) {
QINPassport_Models_Profile_Client::updateMobileByUid($mobile, (int)$uid);
}
return true;
}
/**
* iframe/script浏览器设置状态调用接口
* @return string
*/
public function getSyncStatusLink($uid, $state)
{
return '';
}
/**
* 根据Email获取用户信息
* @param $email
* @return array
*/
public function getProfileByEmail($email)
{
return QINPassport_Models_Profile_Client::getProfileByEmailNoCache($email);
}
/**
* 根据mobile获取用户信息
* @param $mobile
*/
public function getProfileByMobile($mobile)
{
return QINPassport_Models_Profile_Client::getProfileByMobileNoCache($mobile);
}
/**
* 获取UUC当前的登录用户链接, (可使用JS,使用JSONP方式获取)
* @return string
*/
public function getCurrentLoginUidLink()
{
return '';
}
/**
* 根据sid
* @param $sid
* @return array
*/
public function getUidBySid($sid)
{
return array();
}
/**
* 通过UID登录
* @param $uid
* @return bool
*/
public function signinByUid($uid, array $append = array())
{
$_baseProfile = QINPassport_Models_Profile_Base_Client::getBaseInfoByUid($uid);
$_vipInfo = QINPassport_Models_Vip_Client::getVipDetailInfo($uid);
$vipTitle = $_vipInfo['curVipInfo']['title'];
if (empty($_baseProfile) && !empty($append['nick_name'])) {
$profileName = htmlentities($append['nick_name']);
} else {
$profileName = $this->_makeNickName(array('email' => '', 'mobile' => ''), $_baseProfile);
}
$this->setSession(array('uid' => $uid));
$tokenSessionKey = $this->getSession('token_session_key');
$_cookieValue = $profileName . '::' . $uid . '::' . $vipTitle . '::' . $tokenSessionKey;
$this->setCookieID($_cookieValue);
return true;
}
/**
* 更具SSO UID 登录
* @return bool
*/
public function signinBySid($sid, array $append = array())
{
return true;
}
/**
* 上传头像
* @param $heading
* @return bool
*/
public function uploadHead($heading)
{
if (empty($heading)) {
return false;
}
$upload = new YHMUpload_Qiniu_Upload();
$upload->downloadAndUploadHeading($heading);
return true;
}
}