2013-11-22 03:29:30 +00:00
|
|
|
|
<?php
|
|
|
|
|
namespace Open\Handler\ClientTokenHandler;
|
|
|
|
|
|
|
|
|
|
use \Helpers\View as view;
|
|
|
|
|
use \Helpers\dbh;
|
|
|
|
|
use \Helpers\Table;
|
|
|
|
|
use \stdClass;
|
|
|
|
|
use \Files\Files;
|
|
|
|
|
use \Users\Account;
|
|
|
|
|
use \Users\Users;
|
|
|
|
|
|
|
|
|
|
class Escience
|
|
|
|
|
{
|
|
|
|
|
private $db; //传入PDO对象误
|
|
|
|
|
private $config; //全局配置
|
|
|
|
|
|
|
|
|
|
public $table;
|
|
|
|
|
|
|
|
|
|
public $token; //token信息
|
|
|
|
|
public $userInfo; //用户信息
|
|
|
|
|
|
|
|
|
|
public $userid = 0; //用户ID
|
|
|
|
|
//这个。。。如果有,优先获得session中的,如果没,进行后面的用户比对
|
|
|
|
|
|
|
|
|
|
function __construct($token = NULL,$db = NULL)
|
|
|
|
|
{
|
|
|
|
|
if(empty($db))
|
|
|
|
|
{
|
|
|
|
|
$this->db = \Zend_Registry::get('db');
|
|
|
|
|
}else{
|
|
|
|
|
$this->db = $db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->config = \Zend_Registry::get('config');
|
|
|
|
|
$this->table = new Table();
|
|
|
|
|
|
|
|
|
|
//预处理token信息
|
|
|
|
|
$status = $this->preProcess($token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//考虑加一个接口,每个Token操作类中必须包含此函数
|
|
|
|
|
public function doit()
|
|
|
|
|
{
|
|
|
|
|
//makeUserStorage在先,必须的
|
|
|
|
|
$status = $this->makeUserStorage();
|
|
|
|
|
|
|
|
|
|
if($status!==true)
|
|
|
|
|
{
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$status = $this->makeTokenStorage();
|
|
|
|
|
|
|
|
|
|
if($status !== true)
|
|
|
|
|
{
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//预处理
|
|
|
|
|
public function preProcess($tokenData)
|
|
|
|
|
{
|
|
|
|
|
if(!is_array($tokenData))
|
|
|
|
|
{
|
|
|
|
|
return "参数错误";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->userInfo = json_decode($tokenData['userInfo'],true);
|
|
|
|
|
|
|
|
|
|
unset($tokenData['userInfo']);
|
|
|
|
|
|
|
|
|
|
$this->token = $tokenData;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//检查用户信息,如果没有用户自动注册,有用户就自动绑定。
|
|
|
|
|
public function makeUserStorage()
|
|
|
|
|
{
|
|
|
|
|
$uid = view::User('id');
|
|
|
|
|
|
|
|
|
|
if(is_numeric($uid) && $uid > 0)
|
|
|
|
|
{
|
|
|
|
|
$this->userid = $uid;
|
|
|
|
|
return "您已经登录,无需重复登录";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(empty($this->userid))
|
|
|
|
|
{
|
|
|
|
|
$current_oauth_email = $this->userInfo['cstnetId'];
|
|
|
|
|
|
|
|
|
|
$user = new Users(TRUE);
|
|
|
|
|
$current = $user->userExists($current_oauth_email);
|
|
|
|
|
|
|
|
|
|
if($current === false)
|
|
|
|
|
//自动注册用户
|
|
|
|
|
{
|
|
|
|
|
$data = array(
|
|
|
|
|
$user->account->FieldUsername => $current_oauth_email,
|
|
|
|
|
$user->account->FieldEmail => $current_oauth_email,
|
|
|
|
|
$user->account->FieldPasword => 0,
|
|
|
|
|
$user->account->FieldRealname => $this->userInfo['truename'],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$dbh = new dbh();
|
|
|
|
|
|
|
|
|
|
$id = $dbh->insert($user->account->memberTable,$data,true);
|
|
|
|
|
|
|
|
|
|
//登录
|
|
|
|
|
$user->account->storeLogin(array(
|
|
|
|
|
$user->account->FieldUsername => $data[$user->account->FieldUsername],
|
|
|
|
|
$user->account->FieldPasword => $data[$user->account->FieldPasword]
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$this->userid = $id;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//帮用户自动登录
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
|
|
$user->account->storeLogin(array(
|
|
|
|
|
$user->account->FieldUsername => $current[$user->account->FieldUsername],
|
|
|
|
|
$user->account->FieldPasword => $current[$user->account->FieldPasword]
|
|
|
|
|
),false);
|
|
|
|
|
|
|
|
|
|
$this->userid = $current[$user->account->FieldIndex];
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//生成写入token表的数据
|
|
|
|
|
//有token记录就更新,没有再插入
|
|
|
|
|
public function makeTokenStorage()
|
|
|
|
|
{
|
|
|
|
|
$data = array(
|
|
|
|
|
'access_token' => $this->token['access_token'],
|
|
|
|
|
'refresh_token' => $this->token['refresh_token'],
|
|
|
|
|
'expires_in' => $this->token['expires_in'],
|
|
|
|
|
'userid' => $this->userid,
|
|
|
|
|
"response_data" => json_encode($this->userInfo,JSON_NUMERIC_CHECK),
|
|
|
|
|
"source" => "escience"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//查看用户用escience登录的记录是否存在
|
|
|
|
|
$sql = "SELECT * FROM {$this->table->oauth_token} WHERE userid={$this->userid} AND source='{$data['source']}' LIMIT 1";
|
|
|
|
|
$rs = $this->db->query($sql);
|
|
|
|
|
$row = $rs->fetch();
|
|
|
|
|
|
|
|
|
|
$dbh = new dbh();
|
|
|
|
|
|
|
|
|
|
if(isset($row['id']))
|
|
|
|
|
{
|
|
|
|
|
$status = $dbh->update($this->table->oauth_token,$data," id={$row['id']} AND userid={$this->userid} ");
|
|
|
|
|
if($status)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}else{
|
|
|
|
|
return "更新授权信息时发生错误,请重新登录";
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
$status = $dbh->insert($this->table->oauth_token,$data);
|
|
|
|
|
if($status)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}else{
|
|
|
|
|
return "记录授权信息时发生错误,请重新登录";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|