aboutsummaryrefslogtreecommitdiff
path: root/services/api/rest_api.php
blob: 4cee374d620b9e4c1fd4f0113e90df2efb080e53 (plain)
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
<?php
/**
 * Rest endpoint.
 * The API REST endpoint.
 *
 * @package Elgg
 * @subpackage API
 */

/**
 *  Start the Elgg engine
 */
require_once("../../engine/start.php");
global $CONFIG;

// Register the error handler
error_reporting(E_ALL);
set_error_handler('_php_api_error_handler');

// Register a default exception handler
set_exception_handler('_php_api_exception_handler');

// Check to see if the api is available
if ((isset($CONFIG->disable_api)) && ($CONFIG->disable_api == true)) {
	throw new SecurityException(elgg_echo('SecurityException:APIAccessDenied'));
}

// plugins should return true to control what API and user authentication handlers are registered
if (elgg_trigger_plugin_hook('rest', 'init', null, false) == false) {
	// for testing from a web browser, you can use the session PAM
	// do not use for production sites!!
	//register_pam_handler('pam_auth_session');

	// user token can also be used for user authentication
	register_pam_handler('pam_auth_usertoken');

	// simple API key check
	register_pam_handler('api_auth_key', "sufficient", "api");
	// hmac
	register_pam_handler('api_auth_hmac', "sufficient", "api");
}

// Get parameter variables
$method = get_input('method');
$result = null;

// this will throw an exception if authentication fails
authenticate_method($method);

$result = execute_method($method);


if (!($result instanceof GenericResult)) {
	throw new APIException(elgg_echo('APIException:ApiResultUnknown'));
}

// Output the result
echo elgg_view_page($method, elgg_view("api/output", array("result" => $result)));