/** * @return OC_Router */ public static function getRouter() { if (!isset(OC::$router)) { OC::$router = new OC_Router(); OC::$router->loadRoutes(); } return OC::$router; }
/** main function to handle the REST request **/ public static function handle() { // overwrite the 404 error page returncode header("HTTP/1.0 200 OK"); if ($_SERVER['REQUEST_METHOD'] == 'GET') { $method = 'get'; } elseif ($_SERVER['REQUEST_METHOD'] == 'PUT') { $method = 'put'; parse_str(file_get_contents("php://input"), $put_vars); } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { $method = 'post'; } else { echo 'internal server error: method not supported'; exit; } $format = self::readData($method, 'format', 'text', ''); $router = new OC_Router(); $router->useCollection('root'); // CONFIG $router->create('config', '/config.{format}')->defaults(array('format' => $format))->action('OC_OCS', 'apiConfig')->requirements(array('format' => 'xml|json')); // PERSON $router->create('person_check', '/person/check.{format}')->post()->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; $login = OC_OCS::readData('post', 'login', 'text'); $passwd = OC_OCS::readData('post', 'password', 'text'); OC_OCS::personCheck($format, $login, $passwd); })->requirements(array('format' => 'xml|json')); // ACTIVITY // activityget - GET ACTIVITY page,pagesize als urlparameter $router->create('activity_get', '/activity.{format}')->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; $page = OC_OCS::readData('get', 'page', 'int', 0); $pagesize = OC_OCS::readData('get', 'pagesize', 'int', 10); if ($pagesize < 1 or $pagesize > 100) { $pagesize = 10; } OC_OCS::activityGet($format, $page, $pagesize); })->requirements(array('format' => 'xml|json')); // activityput - POST ACTIVITY $router->create('activity_put', '/activity.{format}')->post()->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; $message = OC_OCS::readData('post', 'message', 'text'); OC_OCS::activityPut($format, $message); })->requirements(array('format' => 'xml|json')); // PRIVATEDATA // get - GET DATA $router->create('privatedata_get', '/privatedata/getattribute/{app}/{key}.{format}')->defaults(array('app' => '', 'key' => '', 'format' => $format))->action(function ($parameters) { $format = $parameters['format']; $app = addslashes(strip_tags($parameters['app'])); $key = addslashes(strip_tags($parameters['key'])); OC_OCS::privateDataGet($format, $app, $key); })->requirements(array('format' => 'xml|json')); // set - POST DATA $router->create('privatedata_set', '/privatedata/setattribute/{app}/{key}.{format}')->post()->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; $app = addslashes(strip_tags($parameters['app'])); $key = addslashes(strip_tags($parameters['key'])); $value = OC_OCS::readData('post', 'value', 'text'); OC_OCS::privateDataSet($format, $app, $key, $value); })->requirements(array('format' => 'xml|json')); // delete - POST DATA $router->create('privatedata_delete', '/privatedata/deleteattribute/{app}/{key}.{format}')->post()->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; $app = addslashes(strip_tags($parameters['app'])); $key = addslashes(strip_tags($parameters['key'])); OC_OCS::privateDataDelete($format, $app, $key); })->requirements(array('format' => 'xml|json')); // CLOUD // systemWebApps $router->create('system_webapps', '/cloud/system/webapps.{format}')->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; OC_OCS::systemwebapps($format); })->requirements(array('format' => 'xml|json')); // quotaget $router->create('quota_get', '/cloud/user/{user}.{format}')->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; $user = $parameters['user']; OC_OCS::quotaGet($format, $user); })->requirements(array('format' => 'xml|json')); // quotaset $router->create('quota_set', '/cloud/user/{user}.{format}')->post()->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; $user = $parameters['user']; $quota = self::readData('post', 'quota', 'int'); OC_OCS::quotaSet($format, $user, $quota); })->requirements(array('format' => 'xml|json')); // keygetpublic $router->create('keygetpublic', '/cloud/user/{user}/publickey.{format}')->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; $user = $parameters['user']; OC_OCS::publicKeyGet($format, $user); })->requirements(array('format' => 'xml|json')); // keygetprivate $router->create('keygetpublic', '/cloud/user/{user}/privatekey.{format}')->defaults(array('format' => $format))->action(function ($parameters) { $format = $parameters['format']; $user = $parameters['user']; OC_OCS::privateKeyGet($format, $user); })->requirements(array('format' => 'xml|json')); // add more calls here // please document all the call in the draft spec // http://www.freedesktop.org/wiki/Specifications/open-collaboration-services-1.7#CLOUD // TODO: // users // groups // bookmarks // sharing // versioning // news (rss) try { $router->match($_SERVER['PATH_INFO']); } catch (ResourceNotFoundException $e) { $txt = 'Invalid query, please check the syntax. ' . 'API specifications are here: ' . 'http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.' . 'DEBUG OUTPUT:' . "\n"; $txt .= OC_OCS::getdebugoutput(); echo OC_OCS::generatexml($format, 'failed', 999, $txt); } catch (MethodNotAllowedException $e) { OC_Response::setStatus(405); } exit; }