-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqa-rest-api-token.php
More file actions
234 lines (179 loc) · 6.26 KB
/
Copy pathqa-rest-api-token.php
File metadata and controls
234 lines (179 loc) · 6.26 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
<?php
require_once QA_INCLUDE_DIR.'app/posts.php';
/**
* Class qa_rest_api_token_page
* To hanlde wechat uesr login and registration
*/
class qa_rest_api_token_page
{
public $directory;
public $urltoroot;
/**
* @param $directory
* @param $urltoroot
*/
public function load_module($directory, $urltoroot)
{
error_reporting(0);
$this->directory = $directory;
$this->urltoroot = $urltoroot;
}
/**
* @param $request
* @return bool
* match request from wechat app
*/
function match_request($request)
{
$parts = explode('/', $request);
return $parts [0] == 'api' && $parts [1] == 'v1' && $parts [2] = 'wx'; //&& sizeof ( $parts ) > 1;
}
/**
* @param $request
* process requests by keywords
*/
function process_request($request)
{
header('Content-Type: application/json');
$parts = explode('/', $request);
$resource = $parts [3];
$id = null;
$range = false;
$from = null;
$to = null;
if (sizeof($parts) == 5) {
if (is_numeric($parts [4]) && intval($parts [4]) > 0)
$id = $parts [4];
else
$resource = 'invalid';
}
/*
* Internal security (non for third-party applications)
*
* if (qa_user_permit_error ( 'plugin_rest_api_permit' )) {
http_response_code ( 401 );
$ret_val = array ();
$json_object = array ();
$json_object ['statuscode'] = '401';
$json_object ['message'] = 'Unauthorized';
$json_object ['details'] = 'The user is not authorized to use the API.';
array_push ( $ret_val, $json_object );
echo json_encode ( $ret_val, JSON_PRETTY_PRINT );
return;
} */
$method = $_SERVER['REQUEST_METHOD'];
switch ($resource) {
case 'tokens' :
if ($id == null) {
if (strcmp($method, 'POST') == 0) {
$inputJSON = file_get_contents('php://input');
$content = json_decode($inputJSON, TRUE);
echo $this->get_token($content);
} else {
echo $this->testSomething();
}
} else
echo $this->testSomething();
break;
case 'wxusers' :
if ($id == null) {
if (strcmp($method, 'POST') == 0) {
$inputJSON = file_get_contents('php://input');
$content = json_decode($inputJSON, TRUE);
echo $this->get_wx_user_info($content);
} else {
echo $this->testSomething();
}
} else
echo $this->testSomething();
break;
case 'test' :
echo $this->testSomething();
break;
default :
http_response_code(400);
$ret_val = array();
$json_object = array();
$json_object ['statuscode'] = '400';
$json_object ['message'] = 'Bad Request';
$json_object ['details'] = 'The request URI does not match the API in the system, or the operation failed for unknown reasons.';
array_push($ret_val, $json_object);
echo json_encode($ret_val, JSON_PRETTY_PRINT);
}
}
/**
* @param $content
* @return mixed|string
* get content from wechat app
* exchange imformation with wechat server
* login wechat user in q2a
* return token to wechat app in json format
*/
function get_token($content) //token, string; tokenJSON, JSON
{
$code = $content['code'];
$token = $content['token'];
$userToken = new wx_user_token($code);// construct a wx_user_token obj
$tokenArr = $userToken->getUserTokenLogin($token); //array
$ret_val = array();
array_push($ret_val, $tokenArr);
return json_encode($ret_val, JSON_PRETTY_PRINT);
}
/**
* @param $content
* @return mixed|string
* get wechat user info when authurized in wechat app
* save in database table 'qa_wxusers'
* return in json format
*/
function get_wx_user_info($content){
$userInfo = new wx_user_token($content);// construct a wx_user_token obj
$ret_val = array();
$userInfoArr = array();
$userInfoArr['nickName'] = $content['nickName'];
$userInfoArr['avatarUrl'] = $content['avatarUrl'];
$userInfoArr['gender'] = $content['gender'];
$userInfoArr['province'] = $content['province'];
$userInfoArr['city'] = $content['city'];
$userInfoArr['country'] = $content['country'];
$userInfoArr['token'] = $content['token'];
array_push($ret_val,$userInfoArr);
$saveUserInfo = $userInfo->wxUserUpdateProfiles($content);
return json_encode($ret_val, JSON_PRETTY_PRINT);
}
/**
* @param $content
* @return mixed|string
* alternative method to create wechat user in q2a database
* NOT calling really
*/
function wx_db_user_create($content)
{
$idForWXUser = null;
$nickName = $content['nickName'];
$avatarUrl = $content['avatarUrl'];
$gender = $content['gender'];
$province= $content['province'];
$city = $content['city'];
$country = $content['country'];
qa_db_query_sub(
'INSERT INTO qa_wxusers (idforwxuser,nickname,avatarurl,gender,province,city,country) ' .
'VALUES ($,$,$,$,$,$,$)',
$idForWXUser, $nickName,$avatarUrl,$gender,$province,$city,$country
);
$ret_val = array();
$userInfoArr = array();
array_push($ret_val,$userInfoArr);
return json_encode($ret_val, JSON_PRETTY_PRINT);
}
/**
* @return mixed|string
* place holder for testing
*/
function testSomething(){
$testArr = array();
$ret_val = array();
array_push($ret_val, $testArr);
return json_encode($ret_val, JSON_PRETTY_PRINT);
}
}