aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/handlers/service_handler.php20
-rw-r--r--engine/lib/api.php102
-rw-r--r--htaccess_dist2
3 files changed, 109 insertions, 15 deletions
diff --git a/engine/handlers/service_handler.php b/engine/handlers/service_handler.php
new file mode 100644
index 000000000..51ec2a689
--- /dev/null
+++ b/engine/handlers/service_handler.php
@@ -0,0 +1,20 @@
+<?php
+/**
+ * Elgg web services handler.
+ *
+ * @package Elgg
+ * @subpackage Core
+ * @author Curverider Ltd
+ * @link http://elgg.org/
+ */
+
+
+// Load Elgg engine
+define('externalpage',true);
+require_once("../start.php");
+
+// Get input
+$handler = get_input('handler');
+$request = get_input('request');
+
+service_handler($handler, $request); \ No newline at end of file
diff --git a/engine/lib/api.php b/engine/lib/api.php
index 92d68475b..91c3743a3 100644
--- a/engine/lib/api.php
+++ b/engine/lib/api.php
@@ -1349,31 +1349,103 @@ function __php_api_exception_handler($exception) {
page_draw($exception->getMessage(), elgg_view("api/output", array("result" => $result)));
}
-// Initialisation /////////////////////////////////////////////////////////////
+
+// Services handler ///////////////////////////////////////////
/**
- * Register a page handler for the various API endpoints.
- *
- * @param array $page
+ * Services handler - turns request over to the registered handler
+ *
+ * @param string $handler
+ * @param array $request
*/
-function api_endpoint_handler($page) {
+function service_handler($handler, $request) {
global $CONFIG;
+
+ // setup the input parameters since this comes through rewrite rule
+ $query = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?')+1);
+ if (isset($query)) {
+ parse_str($query, $query_arr);
+ if (is_array($query_arr)) {
+ foreach($query_arr as $name => $val) {
+ set_input($name, $val);
+ }
+ }
+ }
- // Which view
- if ($page[1]) {
- elgg_set_viewtype($page[1]);
+ set_context('api');
+
+ $request = explode('/',$request);
+
+ // after the handler, the first identifier is response format
+ // ex) http://example.org/services/api/rest/xml/?method=test
+ $reponse_format = $request[0];
+ // Which view - xml, json, ...
+ if ($reponse_format) {
+ elgg_set_viewtype($reponse_format);
+ } else {
+ // default to xml
+ elgg_set_viewtype("xml");
+ }
+
+ if (!isset($CONFIG->servicehandler) || empty($handler)) {
+ // no handlers set or bad url
+ header("HTTP/1.0 404 Not Found");
+ exit;
+ } else if (isset($CONFIG->servicehandler[$handler]) && is_callable($CONFIG->servicehandler[$handler])) {
+ $function = $CONFIG->servicehandler[$handler];
+ $function($page, $handler);
+ } else {
+ // no handler for this web service
+ header("HTTP/1.0 404 Not Found");
+ exit;
}
+}
- // Which endpoint
- if ($page[0]) {
- switch ($page[0]) {
- case 'rest' :
- default : include($CONFIG->path . "services/api/rest.php");
- }
+/**
+ * Registers a web services handler
+ *
+ * @param string $handler web services type
+ * @param string $function Your function name
+ * @return true|false Depending on success
+ */
+function register_service_handler($handler, $function) {
+ global $CONFIG;
+ if (!isset($CONFIG->servicehandler)) {
+ $CONFIG->servicehandler = array();
+ }
+ if (is_callable($function)) {
+ $CONFIG->servicehandler[$handler] = $function;
+ return true;
}
+
+ return false;
}
/**
+ * Remove a web service
+ * To replace a web service handler, register the desired handler over the old on
+ * with register_service_handler().
+ *
+ * @param string $handler web services type
+ */
+function unregister_service_handler($handler) {
+ global $CONFIG;
+ if (isset($CONFIG->servicehandler) && isset($CONFIG->servicehandler[$handler])) {
+ unset($CONFIG->servicehandler[$handler]);
+ }
+}
+
+// REST handler //////////////////////////////////////////////////////////////
+
+function rest_handler() {
+ global $CONFIG;
+
+ require $CONFIG->path . "services/api/rest.php";
+}
+
+// Initialisation /////////////////////////////////////////////////////////////
+
+/**
* Unit tests for API
*/
function api_unit_test($hook, $type, $value, $params) {
@@ -1388,7 +1460,7 @@ function api_unit_test($hook, $type, $value, $params) {
*/
function api_init() {
// Register a page handler, so we can have nice URLs
- register_page_handler('api','api_endpoint_handler');
+ register_service_handler('rest','rest_handler');
register_plugin_hook('unit_test', 'system', 'api_unit_test');
diff --git a/htaccess_dist b/htaccess_dist
index a01d8bc08..b8ba81114 100644
--- a/htaccess_dist
+++ b/htaccess_dist
@@ -118,6 +118,8 @@ RewriteEngine on
RewriteRule ^action\/([A-Za-z0-9\_\-\/]+)$ engine/handlers/action_handler.php?action=$1
+RewriteRule ^services\/api\/([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/service_handler.php?handler=$1&request=$2
+
RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)$ services/export/handler.php?view=$1&guid=$2
RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)\/$ services/export/handler.php?view=$1&guid=$2
RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)\/([A-Za-z]+)\/([A-Za-z0-9\_]+)\/$ services/export/handler.php?view=$1&guid=$2&type=$3&idname=$4