]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
introducing a new web services handler
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Tue, 10 Nov 2009 01:50:19 +0000 (01:50 +0000)
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Tue, 10 Nov 2009 01:50:19 +0000 (01:50 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@3651 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/handlers/service_handler.php [new file with mode: 0644]
engine/lib/api.php
htaccess_dist

diff --git a/engine/handlers/service_handler.php b/engine/handlers/service_handler.php
new file mode 100644 (file)
index 0000000..51ec2a6
--- /dev/null
@@ -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
index 92d68475b150abf732ea5e4c6cabef038faf6ddb..91c3743a32b4ed8ce981ca433185d5b701e827ad 100644 (file)
@@ -1349,30 +1349,102 @@ 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 
  */
@@ -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');
        
index a01d8bc08392d7b64c168ae4045e32962233d5c7..b8ba8111490e29e2d4d739941e736378f1226e88 100644 (file)
@@ -118,6 +118,8 @@ RewriteEngine on
 \r
 RewriteRule ^action\/([A-Za-z0-9\_\-\/]+)$ engine/handlers/action_handler.php?action=$1\r
 \r
+RewriteRule ^services\/api\/([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/service_handler.php?handler=$1&request=$2\r
+\r
 RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)$ services/export/handler.php?view=$1&guid=$2\r
 RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)\/$ services/export/handler.php?view=$1&guid=$2\r
 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\r