Issues (2129)

main/webservices/access_url.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * @package chamilo.webservices
6
 */
7
require_once __DIR__.'/../inc/global.inc.php';
8
9
api_protect_webservices();
10
11
$debug = true;
12
13
define('WS_ERROR_SECRET_KEY', 1);
14
define('WS_ERROR_NOT_FOUND_RESULT', 2);
15
define('WS_ERROR_INVALID_INPUT', 3);
16
define('WS_ERROR_SETTING', 4);
17
18
/**
19
 * @param int $code
20
 */
21
function return_error($code)
22
{
23
    $fault = null;
24
    switch ($code) {
25
        case WS_ERROR_SECRET_KEY:
26
            $fault = new soap_fault(
27
                'Server',
28
                '',
29
                'Secret key is not correct or params are not correctly set'
30
            );
31
            break;
32
        case WS_ERROR_NOT_FOUND_RESULT:
33
            $fault = new soap_fault(
34
                'Server',
35
                '',
36
                'No result was found for this query'
37
            );
38
            break;
39
        case WS_ERROR_INVALID_INPUT:
40
            $fault = new soap_fault(
41
                'Server',
42
                '',
43
                'The input variables are invalid o are not correctly set'
44
            );
45
            break;
46
        case WS_ERROR_SETTING:
47
            $fault = new soap_fault(
48
                'Server',
49
                '',
50
                'Please check the configuration for this webservice'
51
            );
52
            break;
53
    }
54
55
    return $fault;
56
}
57
58
/**
59
 * @param array $params
60
 *
61
 * @return bool
62
 */
63
function WSHelperVerifyKey($params)
64
{
65
    global $_configuration, $debug;
66
    if (is_array($params)) {
67
        $secret_key = $params['secret_key'];
68
    } else {
69
        $secret_key = $params;
70
    }
71
    //error_log(print_r($params,1));
72
    $check_ip = false;
73
    $ip_matches = false;
74
    $ip = trim($_SERVER['REMOTE_ADDR']);
75
    // if we are behind a reverse proxy, assume it will send the
76
    // HTTP_X_FORWARDED_FOR header and use this IP instead
77
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
78
        list($ip1, $ip2) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
79
        $ip = trim($ip1);
80
    }
81
    if ($debug) {
82
        error_log("ip: $ip");
83
    }
84
    // Check if a file that limits access from webservices exists and contains
85
    // the restraining check
86
    if (is_file('webservice-auth-ip.conf.php')) {
87
        include 'webservice-auth-ip.conf.php';
88
        if ($debug) {
89
            error_log("webservice-auth-ip.conf.php file included");
90
        }
91
        if (!empty($ws_auth_ip)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ws_auth_ip seems to never exist and therefore empty should always be true.
Loading history...
92
            $check_ip = true;
93
            $ip_matches = api_check_ip_in_range($ip, $ws_auth_ip);
94
            if ($debug) {
95
                error_log("ip_matches: $ip_matches");
96
            }
97
        }
98
    }
99
100
    if ($debug) {
101
        error_log("checkip ".intval($check_ip));
102
    }
103
104
    if ($check_ip) {
105
        $security_key = $_configuration['security_key'];
106
    } else {
107
        $security_key = $ip.$_configuration['security_key'];
108
        //error_log($secret_key.'-'.$security_key);
109
    }
110
111
    $result = api_is_valid_secret_key($secret_key, $security_key);
112
    //error_log($secret_key.'-'.$security_key);
113
    if ($debug) {
114
        error_log('WSHelperVerifyKey result: '.intval($result));
115
    }
116
117
    return $result;
118
}
119
120
// Create the server instance
121
$server = new soap_server();
122
123
/** @var HookWSRegistration $hook */
124
$hook = HookWSRegistration::create();
125
if (!empty($hook)) {
126
    $hook->setEventData(['server' => $server]);
127
    $res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_PRE);
128
    if (!empty($res['server'])) {
129
        $server = $res['server'];
130
    }
131
}
132
133
$server->soap_defencoding = 'UTF-8';
134
135
// Initialize WSDL support
136
$server->configureWSDL('WSAccessUrl', 'urn:WSAccessUrl');
137
138
$server->wsdl->addComplexType(
139
    'portalItem',
140
    'complexType',
141
    'struct',
142
    'all',
143
    '',
144
    [
145
        'id' => ['name' => 'id', 'type' => 'xsd:string'],
146
        'url' => ['name' => 'url', 'type' => 'xsd:string'],
147
    ]
148
);
149
150
$server->wsdl->addComplexType(
151
    'portalList',
152
    'complexType',
153
    'array',
154
    '',
155
    'SOAP-ENC:Array',
156
    [],
157
    [
158
        [
159
            'ref' => 'SOAP-ENC:arrayType',
160
            'wsdl:arrayType' => 'tns:portalItem[]',
161
        ],
162
    ],
163
    'tns:portalItem'
164
);
165
166
$server->wsdl->addComplexType(
167
    'getPortals',
168
    'complexType',
169
    'struct',
170
    'all',
171
    '',
172
    [
173
        'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
174
    ]
175
);
176
177
// Register the method to expose
178
$server->register(
179
    'WSGetPortals', // method name
180
    ['getPortals' => 'tns:getPortals'], // input parameters
181
    ['return' => 'tns:portalList'], // output parameters
182
    'urn:WSAccessUrl', // namespace
183
    'urn:WSAccessUrl#WSGetPortals', // soapaction
184
    'rpc', // style
185
    'encoded', // use
186
    'This service adds a user to portal'               // documentation
187
);
188
189
// Define the method WSAddUserToPortal
190
function WSGetPortals($params)
191
{
192
    global $debug;
193
    if (!WSHelperVerifyKey($params['secret_key'])) {
194
        return return_error(WS_ERROR_SECRET_KEY);
195
    }
196
    $urlData = UrlManager::get_url_data();
197
198
    $return = [];
199
    foreach ($urlData as $data) {
200
        $return[] = [
201
            'id' => $data['id'],
202
            'url' => $data['url'],
203
        ];
204
    }
205
    if ($debug) {
206
        error_log(print_r($return, 1));
207
    }
208
209
    return $return;
210
}
211
212
$server->wsdl->addComplexType(
213
    'AddUserToPortal',
214
    'complexType',
215
    'struct',
216
    'all',
217
    '',
218
    [
219
        'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
220
        'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
221
        'portal_id' => ['name' => 'portal_id', 'type' => 'xsd:string'],
222
    ]
223
);
224
225
// Register the method to expose
226
$server->register(
227
    'WSAddUserToPortal', // method name
228
    ['addUserToPortal' => 'tns:AddUserToPortal'], // input parameters
229
    ['return' => 'xsd:string'], // output parameters
230
    'urn:WSAccessUrl', // namespace
231
    'urn:WSAccessUrl#WSAddUserToPortal', // soapaction
232
    'rpc', // style
233
    'encoded', // use
234
    'This service adds a user to portal'               // documentation
235
);
236
237
// Define the method WSAddUserToPortal
238
function WSAddUserToPortal($params)
239
{
240
    if (!WSHelperVerifyKey($params['secret_key'])) {
241
        return return_error(WS_ERROR_SECRET_KEY);
242
    }
243
244
    $userId = $params['user_id'];
245
    $portalId = $params['portal_id'];
246
247
    UrlManager::add_user_to_url($userId, $portalId);
248
249
    $result = UrlManager::relation_url_user_exist($userId, $portalId);
250
    if (!empty($result)) {
251
        return 1;
252
    }
253
254
    return 0;
255
}
256
257
// Register the method to expose
258
$server->register(
259
    'WSRemoveUserFromPortal', // method name
260
    ['removeUserFromPortal' => 'tns:AddUserToPortal'], // input parameters
261
    ['return' => 'xsd:string'], // output parameters
262
    'urn:WSAccessUrl', // namespace
263
    'urn:WSAccessUrl#WSRemoveUserFromPortal', // soapaction
264
    'rpc', // style
265
    'encoded', // use
266
    'This service remove a user from a portal'                  // documentation
267
);
268
269
// Define the method WSDeleteUserFromGroup
270
function WSRemoveUserFromPortal($params)
271
{
272
    if (!WSHelperVerifyKey($params['secret_key'])) {
273
        return return_error(WS_ERROR_SECRET_KEY);
274
    }
275
276
    $userId = $params['user_id'];
277
    $portalId = $params['portal_id'];
278
279
    UrlManager::delete_url_rel_user($userId, $portalId);
280
281
    $result = UrlManager::relation_url_user_exist($userId, $portalId);
282
    if (empty($result)) {
283
        return 1;
284
    }
285
286
    return 0;
287
}
288
289
$server->wsdl->addComplexType(
290
    'getPortalListFromUser',
291
    'complexType',
292
    'struct',
293
    'all',
294
    '',
295
    [
296
        'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
297
        'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
298
    ]
299
);
300
301
// Register the method to expose
302
$server->register(
303
    'WSGetPortalListFromUser', // method name
304
    ['getPortalListFromUser' => 'tns:getPortalListFromUser'], // input parameters
305
    ['return' => 'tns:portalList'], // output parameters
306
    'urn:WSAccessUrl', // namespace
307
    'urn:WSAccessUrl#WSGetPortalListFromUser', // soapaction
308
    'rpc', // style
309
    'encoded', // use
310
    'This service remove a user from a portal'                  // documentation
311
);
312
313
// Define the method WSDeleteUserFromGroup
314
function WSGetPortalListFromUser($params)
315
{
316
    if (!WSHelperVerifyKey($params['secret_key'])) {
317
        return return_error(WS_ERROR_SECRET_KEY);
318
    }
319
320
    $userId = $params['user_id'];
321
322
    $result = UrlManager::get_access_url_from_user($userId);
323
    if (!empty($result)) {
324
        foreach ($result as &$data) {
325
            $data['id'] = $data['access_url_id'];
326
        }
327
    }
328
329
    return $result;
330
}
331
332
// Course ws
333
$server->wsdl->addComplexType(
334
    'getPortalListFromCourse',
335
    'complexType',
336
    'struct',
337
    'all',
338
    '',
339
    [
340
        'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
341
        'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
342
        'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
343
    ]
344
);
345
346
// Register the method to expose
347
$server->register(
348
    'WSGetPortalListFromCourse', // method name
349
    ['getPortalListFromCourse' => 'tns:getPortalListFromCourse'], // input parameters
350
    ['return' => 'tns:portalList'], // output parameters
351
    'urn:WSAccessUrl', // namespace
352
    'urn:WSAccessUrl#getPortalListFromCourse', // soapaction
353
    'rpc', // style
354
    'encoded', // use
355
    'This service remove a user from a portal'                  // documentation
356
);
357
358
// Define the method WSDeleteUserFromGroup
359
function WSGetPortalListFromCourse($params)
360
{
361
    if (!WSHelperVerifyKey($params['secret_key'])) {
362
        return return_error(WS_ERROR_SECRET_KEY);
363
    }
364
365
    $courseInfo = CourseManager::getCourseInfoFromOriginalId(
366
        $params['original_course_id_value'],
367
        $params['original_course_id_name']
368
    );
369
370
    $courseId = $courseInfo['real_id'];
371
372
    $result = UrlManager::get_access_url_from_course($courseId);
373
374
    if (!empty($result)) {
375
        foreach ($result as &$data) {
376
            $data['id'] = $data['access_url_id'];
377
        }
378
    }
379
380
    return $result;
381
}
382
383
$server->wsdl->addComplexType(
384
    'addCourseToPortal',
385
    'complexType',
386
    'struct',
387
    'all',
388
    '',
389
    [
390
        'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
391
        'portal_id' => ['name' => 'portal_id', 'type' => 'xsd:string'],
392
        'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
393
        'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
394
    ]
395
);
396
397
// Register the method to expose
398
$server->register(
399
    'WSAddCourseToPortal', // method name
400
    ['addCourseToPortal' => 'tns:addCourseToPortal'], // input parameters
401
    ['return' => 'xsd:string'], // output parameters
402
    'urn:WSAccessUrl', // namespace
403
    'urn:WSAccessUrl#WSAddCourseToPortal', // soapaction
404
    'rpc', // style
405
    'encoded', // use
406
    'This service adds a course to portal'               // documentation
407
);
408
409
// Define the method WSAddUserToPortal
410
function WSAddCourseToPortal($params)
411
{
412
    if (!WSHelperVerifyKey($params['secret_key'])) {
413
        return return_error(WS_ERROR_SECRET_KEY);
414
    }
415
416
    $courseInfo = CourseManager::getCourseInfoFromOriginalId(
417
        $params['original_course_id_value'],
418
        $params['original_course_id_name']
419
    );
420
421
    $courseId = $courseInfo['real_id'];
422
    $portalId = $params['portal_id'];
423
424
    UrlManager::add_course_to_url($courseId, $portalId);
425
426
    $result = UrlManager::relation_url_course_exist($courseId, $portalId);
427
428
    return intval($result);
429
}
430
431
// Register the method to expose
432
$server->register(
433
    'WSRemoveCourseFromPortal', // method name
434
    ['removeCourseFromPortal' => 'tns:addCourseToPortal'], // input parameters
435
    ['return' => 'xsd:string'], // output parameters
436
    'urn:WSAccessUrl', // namespace
437
    'urn:WSAccessUrl#WSRemoveCourseFromPortal', // soapaction
438
    'rpc', // style
439
    'encoded', // use
440
    'This service remove a course from a portal'                  // documentation
441
);
442
443
// Define the method WSDeleteUserFromGroup
444
function WSRemoveCourseFromPortal($params)
445
{
446
    if (!WSHelperVerifyKey($params['secret_key'])) {
447
        return return_error(WS_ERROR_SECRET_KEY);
448
    }
449
450
    $courseInfo = CourseManager::getCourseInfoFromOriginalId(
451
        $params['original_course_id_value'],
452
        $params['original_course_id_name']
453
    );
454
455
    $courseId = $courseInfo['real_id'];
456
    $portalId = $params['portal_id'];
457
458
    UrlManager::delete_url_rel_course($courseId, $portalId);
459
    $result = UrlManager::relation_url_course_exist($courseId, $portalId);
460
461
    if (empty($result)) {
462
        return true;
463
    }
464
465
    return false;
466
}
467
468
/* Delete user from group Web Service end */
469
470
// Add more webservices through hooks from plugins
471
if (!empty($hook)) {
472
    $hook->setEventData(['server' => $server]);
473
    $res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_POST);
474
    if (!empty($res['server'])) {
475
        $server = $res['server'];
476
    }
477
}
478
479
// Use the request to (try to) invoke the service
480
$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents('php://input');
481
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
482
483
// If you send your data in utf8 then this value must be false.
484
$decodeUTF8 = api_get_setting('registration.soap.php.decode_utf8');
485
if ($decodeUTF8 === 'true') {
486
    $server->decode_utf8 = true;
487
} else {
488
    $server->decode_utf8 = false;
489
}
490
$server->service($HTTP_RAW_POST_DATA);
491