-
Notifications
You must be signed in to change notification settings - Fork 1
/
remote-xf.php
261 lines (210 loc) · 8.18 KB
/
remote-xf.php
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
<?php
$whitelist = array('127.0.0.1', "::1");
$token = "";
if (!in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
echo "access denied: " . $_SERVER['REMOTE_ADDR'];
die(0);
}
if ($_REQUEST['token'] != $token) {
echo "access denied: wrong token";
die(0);
}
$set_xf_db = Array(
"host" => "localhost",
"uname" => "",
"pass" => "",
"db" => ""
);
class MySQL
{
public $MySQLiObj;
public $lastSQLQuery;
public $lastSQLStatus;
public function __construct($server, $user, $password, $db, $port="3306")
{
$this->MySQLiObj = new \mysqli($server, $user, $password, $db, $port);
if(mysqli_connect_errno())
{
echo "Can't connect to database!";
die();
}
//Characterset UTF-8
$this->query("SET NAMES utf8");
}
public function __destruct()
{
$this->MySQLiObj->close();
}
public function query($sqlQuery, $resultset = false)
{
$this->lastSQLQuery = $sqlQuery;
//this->doLog($sqlQuery);
$result = $this->MySQLiObj->query($sqlQuery);
if($resultset == true)
{
if($result == false)
{
$this->lastSQLStatus = false;
}
else
{
$this->lastSQLStatus = true;
}
return $result;
}
$return = $this->makeArrayResult($result);
return $return;
}
public function escapeString($value)
{
return $this->MySQLiObj->real_escape_string($value);
}
public function lastSQLError()
{
return $this->MySQLiObj->error;
}
private function makeArrayResult($ResultObj)
{
if($ResultObj === false)
{
$this->lastSQLStatus = false;
return false;
}
else if($ResultObj === true)
{
$this->lastSQLStatus = true;
return true;
}
else if($ResultObj->num_rows == 0)
{
$this->lastSQLStatus = true;
return array();
}
else
{
$array = array();
while($line = $ResultObj->fetch_array(MYSQLI_ASSOC))
{
array_push($array, $line);
}
$this->lastSQLStatus = true;
return $array;
}
}
public function runIPSQuerySingleResult($rawquery, $errormsg) {
try {
$res = $this->query($rawquery);
if($res->num_rows === 0 || empty($res[0])) {
return returnError(4001, $errormsg);
}
return returnJson($res[0]);
} catch (Exception $e) {
return returnError(5001, "SQL Exception: " . $e);
}
}
}
function returnError($errorId, $message) {
return returnJson(array('errorId' => $errorId, 'message' => $message));
}
function returnJson($jsonObject) {
echo json_encode($jsonObject);
}
if (isset($_POST["action"]) && !empty($_POST['action'])) {
$QUERY_MAP = array(
"getUserDataByUserId" => "SELECT * FROM xf_user WHERE user_id=%s LIMIT 1;",
"getUserGroupsFromUserId" => "SELECT user_group_id FROM xf_user_group_relation WHERE user_id=%s;",
"getUserDataByUserName" => "SELECT * FROM xf_user WHERE UPPER(`username`) = UPPER('%s') LIMIT 1;",
"getUserDataByUserEmail" => "SELECT * FROM xf_user WHERE UPPER(`email`) = UPPER('%s') LIMIT 1;",
"getUserIdsFromUserGroup" => "SELECT user_id FROM xf_user_group_relation WHERE user_group_id=%s;",
"getUserGroupNameById" => "SELECT title FROM xf_user_group WHERE user_group_id=%s",
"getUserFieldContentFromUserId" => "SELECT `field_value` FROM `xf_user_field_value` WHERE user_id=%s AND field_id=%s"
);
switch (filter_var($_POST["action"])) {
case 'ips_query':
if (!isset($_POST["method"]) || empty($_POST['method'])) {
return returnError(1001, "IPS Query method not found!");
}
$xf_sql = new MySQL($set_xf_db["host"], $set_xf_db["uname"], $set_xf_db["pass"], $set_xf_db["db"]);
switch (filter_var($_POST["method"])) {
case ("getUserDataByUserId"):
if(!isset($_POST['userId']) || empty($_POST['userId'])) {
return returnError(1002, "Required POST parameter not found (userId)!");
}
$rawquery = sprintf($QUERY_MAP["getUserDataByUserId"], filter_var($_POST["userId"]));
return $xf_sql->runIPSQuerySingleResult($rawquery, "User ID was not found.");
case ("getUserGroupsFromUserId"):
if(!isset($_POST['userId']) || empty($_POST['userId'])) {
return returnError(1002, "Required POST parameter not found (userId)!");
}
$rawquery = sprintf($QUERY_MAP["getUserGroupsFromUserId"], filter_var($_POST["userId"]));
try {
$res = $xf_sql->query($rawquery);
$ret = Array();
foreach ($res as $row) {
$ret[] = $row["user_group_id"];
}
return returnJson( Array(ids => $ret));
} catch (Exception $e) {
return returnError(5001, "SQL Exception: " . $e);
}
case ("getUserDataByUserName"):
if(!isset($_POST['userName']) || empty($_POST['userName'])) {
return returnError(1002, "Required POST parameter not found (userName)!");
}
$rawquery = sprintf($QUERY_MAP["getUserDataByUserName"], filter_var($_POST["userName"]));
return $xf_sql->runIPSQuerySingleResult($rawquery, "User with the specified username was not found.");
case ("getUserDataByUserEmail"):
if(!isset($_POST['userEmail']) || empty($_POST['userEmail'])) {
return returnError(1002, "Required POST parameter not found (userEmail)!");
}
$rawquery = sprintf($QUERY_MAP["getUserDataByUserEmail"], filter_var($_POST["userEmail"]));
return $xf_sql->runIPSQuerySingleResult($rawquery, "User with the specified email was not found.");
break;
case ("getUserIdsFromUserGroup"):
if(!isset($_POST['groupId']) || empty($_POST['groupId'])) {
return returnError(1002, "Required POST parameter not found (groupId)!");
}
$rawquery = sprintf($QUERY_MAP["getUserIdsFromUserGroup"], filter_var($_POST["groupId"]));
try {
$res = $xf_sql->query($rawquery);
if($res->num_rows === 0 || empty($res[0])) {
return returnError(4001, "No users were found with the specified group ID.");
}
$ret = Array();
foreach ($res as $row) {
$ret[] = $row["user_id"];
}
return returnJson( Array(ids => $ret));
} catch (Exception $e) {
return returnError(5001, "SQL Exception: " . $e);
}
break;
case ("loginUser"):
// To be done
break;
case ("getUserGroupNameById"):
if(!isset($_POST['groupId']) || empty($_POST['groupId'])) {
return returnError(1002, "Required POST parameter not found (groupId)!");
}
$rawquery = sprintf($QUERY_MAP["getUserGroupNameById"], filter_var($_POST["groupId"]));
return $xf_sql->runIPSQuerySingleResult($rawquery, "No user group found with the specified ID");
case ("getUserFieldContentFromUserId"):
if(!isset($_POST['fieldId']) || empty($_POST['fieldId'])) {
return returnError(1002, "Required POST parameter not found (fieldId)!");
}
if(!isset($_POST['userId']) || empty($_POST['userId'])) {
return returnError(1002, "Required POST parameter not found (userId)!");
}
$rawquery = sprintf($QUERY_MAP["getUserFieldContentFromUserId"], filter_var($_POST["userId"]), filter_var($_POST["fieldId"]));
return $xf_sql->runIPSQuerySingleResult($rawquery, "No field content found for this fieldId for the specified user ID.");
default:
return returnError(1003, "Specified XF Query method not found!");
}
break;
default:
return returnError(1000, "No action found!");
}
} else {
return returnError(1000, "No action found!");
}
?>