This repository has been archived by the owner on Feb 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
remote.php
383 lines (306 loc) · 11.1 KB
/
remote.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
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
<?php
/**
* Used by Tiki's InterTiki feature
*
* @package Tiki
* @copyright (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project. All Rights Reserved. See copyright.txt for details and a complete list of authors.
* @licence LGPL-2.1. See license.txt for details.
*/
// $Id$
$version = '0.2';
include 'tiki-setup.php';
if ($prefs['feature_intertiki'] != 'y' || $prefs['feature_intertiki_server'] != 'y' || $prefs['feature_intertiki_mymaster']) {
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<methodResponse><fault><value><struct><member><name>faultCode</name><value><int>403</int></value></member>";
echo "<member><name>faultString</name><value><string>Server is not configured</string></value></member></struct></value></fault></methodResponse>";
exit;
}
/**
* @param $file
* @param $line
*/
function lograw($file, $line)
{
$fp = fopen($file, 'a+');
fputs($fp, "$line\n");
fclose($fp);
}
/**
* @param $file
* @param $txt
* @param $user
* @param $code
* @param $from
*/
function logit($file, $txt, $user, $code, $from)
{
$tikilib = TikiLib::lib('tiki');
$line = $tikilib->get_ip_address() . " - $user - " . date('[m/d/Y:H:i:s]') . " \"$txt\" $code \"$from\"";
lograw($file, $line);
}
define('INTERTIKI_OK', 200);
define('INTERTIKI_BADKEY', 401);
define('INTERTIKI_BADUSER', 404);
$map = [
'intertiki.validate' => ['function' => 'validate'],
'intertiki.setUserInfo' => ['function' => 'set_user_info'],
'intertiki.logout' => ['function' => 'logout'],
'intertiki.cookiecheck' => ['function' => 'cookie_check'],
'intertiki.version' => ['function' => 'get_version'],
'intertiki.getUserInfo' => ['function' => 'get_user_info'],
'intertiki.getRegistrationPrefs' => ['function' => 'get_registration_prefs'],
'intertiki.registerUser' => ['function' => 'register_user']
];
$s = new XML_RPC_Server($map);
/**
* @param $params
* @return XML_RPC_Response
*/
function validate($params)
{
global $prefs;
$userlib = TikiLib::lib('user');
$tikilib = TikiLib::lib('tiki');
$logslib = TikiLib::lib('logs');
$key = $params->getParam(0);
$key = $key->scalarval();
$login = $params->getParam(1);
$login = $login->scalarval();
$pass = $params->getParam(2);
$pass = $pass->scalarval();
$slave = $params->getParam(3);
$slave = $slave->scalarval();
$hashkey = $params->getParam(4);
$hashkey = $hashkey->scalarval();
if (! isset($prefs['known_hosts'][$key]) or $prefs['known_hosts'][$key]['ip'] != $tikilib->get_ip_address()) {
$msg = tra('Invalid server key');
if (! empty($prefs['intertiki_errfile'])) {
logit($prefs['intertiki_errfile'], $msg, $key, INTERTIKI_BADKEY, $prefs['known_hosts'][$key]['name']);
}
$logslib->add_log('intertiki', $msg . ' from ' . $prefs['known_hosts'][$key]['name'], $login);
return new XML_RPC_Response(0, 101, $msg);
}
list($isvalid, $dummy, $error) = $userlib->validate_user($login, $pass);
if (! $isvalid) {
$msg = tra('Invalid username or password');
if ($prefs['intertiki_errfile']) {
logit($prefs['intertiki_errfile'], $msg, $login, INTERTIKI_BADUSER, $prefs['known_hosts'][$key]['name']);
}
$logslib->add_log('intertiki', $msg . ' from ' . $prefs['known_hosts'][$key]['name'], $login);
if (! $userlib->user_exists($login)) {
// slave client is supposed to disguise 102 code as 101 not to show
// crackers that user does not exists. 102 is required for telling slave
// to delete user there
return new XML_RPC_Response(0, 102, $msg);
} else {
return new XML_RPC_Response(0, 101, $msg);
}
}
if ($prefs['intertiki_logfile']) {
logit($prefs['intertiki_logfile'], 'logged', $login, INTERTIKI_OK, $prefs['known_hosts'][$key]['name']);
}
$userInfo = $userlib->get_user_info($login);
$userlib->create_user_cookie($userInfo['userId'], $hashkey);
if ($slave) {
$logslib->add_log('intertiki', 'auth granted from ' . $prefs['known_hosts'][$key]['name'], $login);
$user_details = $userlib->get_user_details($login);
$user_info = $userlib->get_user_info($login);
$ret['avatarData'] = new XML_RPC_Value($user_info['avatarData'], 'base64');
$ret['user_details'] = new XML_RPC_Value(serialize($user_details), 'string');
return new XML_RPC_Response(new XML_RPC_Value($ret, 'struct'));
} else {
$logslib->add_log('intertiki', 'auth granted from ' . $prefs['known_hosts'][$key]['name'], $login);
return new XML_RPC_Response(new XML_RPC_Value(1, 'boolean'));
}
}
/**
* @param $params
* @return XML_RPC_Response
*/
function set_user_info($params)
{
global $prefs;
$userlib = TikiLib::lib('user');
$tikilib = TikiLib::lib('tiki');
if ($prefs['feature_userPreferences'] != 'y') {
return new XML_RPC_Response(new XML_RPC_Value(1, 'boolean'));
}
$key = $params->getParam(0);
$key = $key->scalarval();
$login = $params->getParam(1);
$login = $login->scalarval();
if (! isset($prefs['known_hosts'][$key]) or $prefs['known_hosts'][$key]['ip'] != $tikilib->get_ip_address()) {
$msg = tra('Invalid server key');
if ($prefs['intertiki_errfile']) {
logit($prefs['intertiki_errfile'], $msg, $key, INTERTIKI_BADKEY, $prefs['known_hosts'][$key]['name']);
}
TikiLib::lib('logs')->add_log('intertiki', $msg . ' from ' . $prefs['known_hosts'][$key]['name'], $login);
return new XML_RPC_Response(0, 101, $msg);
}
$userlib->interSetUserInfo($login, $params->getParam(2));
return new XML_RPC_Response(new XML_RPC_Value(1, 'boolean'));
}
/**
* @param $params
* @return XML_RPC_Response
*/
function logout($params)
{
global $prefs;
$userlib = TikiLib::lib('user');
$tikilib = TikiLib::lib('tiki');
$logslib = TikiLib::lib('logs');
$key = $params->getParam(0);
$key = $key->scalarval();
$login = $params->getParam(1);
$login = $login->scalarval();
if (! isset($prefs['known_hosts'][$key]) or $prefs['known_hosts'][$key]['ip'] != $tikilib->get_ip_address()) {
$msg = tra('Invalid server key');
if ($prefs['intertiki_errfile']) {
logit($prefs['intertiki_errfile'], $msg, $key, INTERTIKI_BADKEY, $prefs['known_hosts'][$key]['name']);
}
$logslib->add_log('intertiki', $msg . ' from ' . $prefs['known_hosts'][$key]['name'], $login);
return new XML_RPC_Response(0, 101, $msg);
}
$userlib->user_logout($login, true);
$userInfo = $this->get_user_info($login);
$userlib->delete_user_cookie($userInfo['userId']);
if ($prefs['intertiki_logfile']) {
logit($prefs['intertiki_logfile'], 'logout', $login, INTERTIKI_OK, $prefs['known_hosts'][$key]['name']);
}
$logslib->add_log('intertiki', 'auth revoked from ' . $prefs['known_hosts'][$key]['name'], $login);
return new XML_RPC_Response(new XML_RPC_Value(1, 'boolean'));
}
/**
* @param $params
* @return XML_RPC_Response
*/
function cookie_check($params)
{
global $prefs;
$userlib = TikiLib::lib('user');
$tikilib = TikiLib::lib('tiki');
$key = $params->getParam(0);
$key = $key->scalarval();
$hash = $params->getParam(1);
$hash = $hash->scalarval();
if (! isset($prefs['known_hosts'][$key]) or $prefs['known_hosts'][$key]['ip'] != $tikilib->get_ip_address()) {
$msg = tra('Invalid server key');
if ($prefs['intertiki_errfile']) {
logit($prefs['intertiki_errfile'], $msg, $key, INTERTIKI_BADKEY, $prefs['known_hosts'][$key]['name']);
}
$hash = substr($hash, strpos($hash, '.'));
TikiLib::lib('logs')->add_log('intertiki', $msg . ' from ' . $prefs['known_hosts'][$key]['name'], $hash);
return new XML_RPC_Response(0, 101, $msg);
}
$result = $userlib->get_user_by_cookie($hash);
if ($result) {
return new XML_RPC_Response(new XML_RPC_Value($result, 'string'));
}
$msg = tra('Cookie not found');
return new XML_RPC_Response(0, 101, $msg);
}
/**
* @param $params
* @return XML_RPC_Response
*/
function get_version($params)
{
global $version;
return new XML_RPC_Response(new XML_RPC_Value($version, 'int'));
}
/**
* @param $params
* @return XML_RPC_Response
*/
function get_user_info($params)
{
global $prefs;
$userlib = TikiLib::lib('user');
$tikilib = TikiLib::lib('tiki');
$key = $params->getParam(0);
$key = $key->scalarval();
$login = $params->getParam(1);
$login = $login->scalarval();
if (! isset($prefs['known_hosts'][$key]) or $prefs['known_hosts'][$key]['ip'] != $tikilib->get_ip_address()) {
$msg = tra('Invalid server key');
if ($prefs['intertiki_errfile']) {
logit($prefs['intertiki_errfile'], $msg, $key, INTERTIKI_BADKEY, $prefs['known_hosts'][$key]['name']);
}
TikiLib::lib('logs')->add_log('intertiki', $msg . ' from ' . $prefs['known_hosts'][$key]['name'], $login);
return new XML_RPC_Response(0, 101, $msg);
}
$email = $params->getParam(2);
$email = $email->scalarval();
if (empty($login)) {
$login = empty($email) ? '' : $userlib->get_user_by_email($email);
}
if (empty($login)) {
$msg = 'Invalid username';
return new XML_RPC_Response(0, 102, $msg);
}
if (empty($email)) {
$email = $userlib->get_user_email($login);
}
$ret['login'] = new XML_RPC_Value($login, 'string');
$ret['email'] = new XML_RPC_Value($email, 'string');
return new XML_RPC_Response(new XML_RPC_Value($ret, 'struct'));
}
/**
* @param $params
* @return XML_RPC_Response
*/
function get_registration_prefs($params)
{
global $prefs;
$logslib = TikiLib::lib('logs');
$tikilib = TikiLib::lib('tiki');
$registrationlib = TikiLib::lib('registration');
$key = $params->getParam(0);
$key = $key->scalarval();
$login = $params->getParam(1);
$login = $login->scalarval();
if (! isset($prefs['known_hosts'][$key]) or $prefs['known_hosts'][$key]['ip'] != $tikilib->get_ip_address()) {
$msg = tra('Invalid server key');
if ($prefs['intertiki_errfile']) {
logit($prefs['intertiki_errfile'], $msg, $key, INTERTIKI_BADKEY, $prefs['known_hosts'][$key]['name']);
}
$logslib->add_log('intertiki', $msg . ' from ' . $prefs['known_hosts'][$key]['name'], $login);
return new XML_RPC_Response(0, 101, $msg);
}
if (! isset($prefs['known_hosts'][$key]['allowusersregister'])
|| ($prefs['known_hosts'][$key]['allowusersregister'] != 'y')
) {
return new XML_RPC_Response(0, 101, 'Users are not allowed to register via intertiki on this master.');
}
return new XML_RPC_Response(XML_RPC_encode($registrationlib->merged_prefs));
}
/**
* @param $params
* @return XML_RPC_Response
*/
function register_user($params)
{
global $prefs;
$logslib = TikiLib::lib('logs');
$tikilib = TikiLib::lib('tiki');
$registrationlib = TikiLib::lib('registration');
$key = $params->getParam(0);
$key = $key->scalarval();
$login = $params->getParam(1);
$login = $login->scalarval();
if (! isset($prefs['known_hosts'][$key]) or $prefs['known_hosts'][$key]['ip'] != $tikilib->get_ip_address()) {
$msg = tra('Invalid server key');
if ($prefs['intertiki_errfile']) {
logit($prefs['intertiki_errfile'], $msg, $key, INTERTIKI_BADKEY, $prefs['known_hosts'][$key]['name']);
}
$logslib->add_log('intertiki', $msg . ' from ' . $prefs['known_hosts'][$key]['name'], $login);
return new XML_RPC_Response(0, 101, $msg);
}
if (! isset($prefs['known_hosts'][$key]['allowusersregister'])
|| ($prefs['known_hosts'][$key]['allowusersregister'] != 'y')
) {
return new XML_RPC_Response(0, 101, 'Users are not allowed to register via intertiki on this master.');
}
$result = $registrationlib->register_new_user_from_intertiki(XML_RPC_decode($params->getParam(1)));
return new XML_RPC_Response(XML_RPC_encode($result));
}