Uc.php
4.8 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
<?php
namespace app\common\controller;
use app\common\library\Auth;
use think\Controller;
use think\Loader;
//UC通信接口基类,抽象类,必须继承使用
define('API_ADDUSER', 1); //note 用户添加 API 接口开关
define('API_DELETEUSER', 1); //note 用户删除 API 接口开关
define('API_GETTAG', 1); //note 获取标签 API 接口开关
define('API_SYNLOGIN', 1); //note 同步登录 API 接口开关
define('API_SYNLOGOUT', 1); //note 同步登出 API 接口开关
define('API_UPDATEPW', 1); //note 更改用户密码 开关
define('API_UPDATEINFO', 1); //note 更改用户信息 开关
define('API_UPDATEBADWORDS', 1); //note 更新关键字列表 开关
define('API_UPDATEHOSTS', 1); //note 更新域名解析缓存 开关
define('API_UPDATEAPPS', 1); //note 更新应用列表 开关
define('API_UPDATECLIENT', 1); //note 更新客户端缓存 开关
define('API_UPDATECREDIT', 1); //note 更新用户积分 开关
define('API_GETCREDITSETTINGS', 1); //note 向 UCenter 提供积分设置 开关
define('API_GETCREDIT', 1); //note 获取用户的某项积分 开关
define('API_UPDATECREDITSETTINGS', 1); //note 更新应用积分设置 开关
define('API_RETURN_SUCCEED', '1');
define('API_RETURN_FAILED', '-1');
define('API_RETURN_FORBIDDEN', '-2');
abstract class Uc extends Controller
{
/**
*
* @var \app\common\library\Auth
*/
protected $user = null;
public $code; //code参数原始字符串
public $action; //解析code得到的动作名
public $error = NULL;
public $get; //get数据
public $post; //post数据
protected $appdir; //uc_client所在目录
/**
* 初始化方法
*/
public function _initialize()
{
parent::_initialize();
$modulename = $this->request->module();
$controllername = strtolower($this->request->controller());
$actionname = strtolower($this->request->action());
$path = '/' . $modulename . '/' . str_replace('.', '/', $controllername) . '/' . $actionname;
$this->user = Auth::instance();
// 设置当前请求的URI
$this->user->setRequestUri($path);
// 检测当前是否登录并进行初始化
//$this->user->init();
//
//加载UC函数库
//加载UC XML类库
Loader::import('fast.ucenter.common.Functions');
Loader::import('fast.ucenter.common.XML');
$this->initConfig(); //初始化UC应用配置
$this->initRequest(); //初始化请求
}
function initConfig()
{
if (!defined('UC_API'))
{
$this->error('未发现uc常量配置信息');
}
}
function _serialize($arr, $htmlon = 0)
{
return xml_serialize($arr, $htmlon);
}
/**
* 解析请求
* @return boolean
*/
public function initRequest()
{
$code = $this->request->get('code');
parse_str(_uc_authcode($code, 'DECODE', UC_KEY), $get);
if (get_magic_quotes_gpc())
{
$get = _uc_stripslashes($get);
}
if (empty($get) || !isset($get['time']) || !isset($get['action']))
{
$this->error = '非法请求';
return false;
}
$timestamp = time();
if ($timestamp - $get['time'] > 36001111)
{
$this->error = '请求有效期已过';
return false;
}
$requestdata = file_get_contents('php://input');
$this->code = $code;
$this->action = strtolower(parse_name($get['action'], '1'));
$this->get = $get;
$this->post = @xml_unserialize($requestdata);
$this->appdir = EXTEND_PATH . 'fast/ucenter/client/';
// 定义允许请求的接口
$allowaction = ['test', 'adduser', 'deleteuser', 'gettag', 'synregister', 'synlogin', 'synlogout', 'updateinfo', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcreditsettings', 'updatecreditsettings'];
if (!in_array($this->action, $allowaction))
{
$this->error = '请求不允许';
return false;
}
}
/**
* 响应ucserver的通信请求,调用相应方法,输出最终结果并结束整个流程
*/
public function response()
{
if ($this->_before_response())
{
if ($this->error !== NULL)
{
exit($this->error);
}
$response = call_user_func(array($this, $this->action));
}
if ($this->_after_response($response))
{
exit($response);
}
exit('-1');
}
protected function _before_response()
{
return true;
}
protected function _after_response($response = "")
{
return true;
}
public function test()
{
return API_RETURN_SUCCEED;
}
}