diff options
author | Bart Visscher <bartv@thisnet.nl> | 2012-07-23 18:58:52 +0200 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2012-07-23 18:58:52 +0200 |
commit | 768b44b9b685a07af6030e484ab6322ba44b5b7e (patch) | |
tree | 23df0049a675a65447643ca7ef3a18a21841aa7f /lib | |
parent | d0cae6a99a332af79b2506205aa25aad4313d912 (diff) | |
download | nextcloud-server-768b44b9b685a07af6030e484ab6322ba44b5b7e.tar.gz nextcloud-server-768b44b9b685a07af6030e484ab6322ba44b5b7e.zip |
Convert routing to ownCloud fluid interface
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ocs.php | 192 | ||||
-rw-r--r-- | lib/route.php | 50 | ||||
-rw-r--r-- | lib/router.php | 50 |
3 files changed, 181 insertions, 111 deletions
diff --git a/lib/ocs.php b/lib/ocs.php index 7e84d0e5441..27007076661 100644 --- a/lib/ocs.php +++ b/lib/ocs.php @@ -21,12 +21,6 @@ * */ -use Symfony\Component\Routing\Matcher\UrlMatcher; -use Symfony\Component\Routing\RequestContext; -use Symfony\Component\Routing\RouteCollection; -use Symfony\Component\Routing\Route; -use Symfony\Component\Routing\Exception\ResourceNotFoundException; - /** * Class to handle open collaboration services API requests * @@ -87,114 +81,89 @@ class OC_OCS { // 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'; - }elseif($_SERVER['REQUEST_METHOD'] == 'POST') { - $method='post'; - }else{ - echo('internal server error: method not supported'); - exit(); - } - - $routes = new RouteCollection(); - // CONFIG - $routes->add('config', - new Route('/config.{format}', - array('format'=>'', - 'action' => function ($parameters) { - OC_OCS::apiconfig($parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text')); - }), - array('format'=>'xml|json'))); - - // PERSON - $routes->add('person_check', - new Route('/person/check.{format}', - array('format'=>'', - 'action' => function ($parameters) { - $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); - $login=OC_OCS::readdata('login','text'); - $passwd=OC_OCS::readdata('password','text'); - OC_OCS::personcheck($format,$login,$passwd); - }), - array('_method'=>'post', - 'format'=>'xml|json'))); - - // ACTIVITY - // activityget - GET ACTIVITY page,pagesize als urlparameter - $routes->add('activity_get', - new Route('/activity.{format}', - array('format'=>'', - 'action' => function ($parameters) { - $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); - $page=OC_OCS::readdata('page','int'); - $pagesize=OC_OCS::readdata('pagesize','int'); - if($pagesize<1 or $pagesize>100) $pagesize=10; - OC_OCS::activityget($format,$page,$pagesize); - }), - array('format'=>'xml|json'))); - // activityput - POST ACTIVITY - $routes->add('activity_put', - new Route('/activity.{format}', - array('format'=>'', - 'action' => function ($parameters) { - $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); + $router = new OC_Router(); + $router->useCollection('ocs'); + // CONFIG + $router->create('config', '/config.{format}') + ->defaults(array('format'=>'')) + ->action('OC_OCS', 'apiConfig') + ->requirements(array('format'=>'xml|json')); + + // PERSON + $router->create('person_check', '/person/check.{format}') + ->post() + ->defaults(array('format'=>'')) + ->action(function ($parameters) { + $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); + $login=OC_OCS::readdata('login','text'); + $passwd=OC_OCS::readdata('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'=>'')) + ->action(function ($parameters) { + $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); + $page=OC_OCS::readdata('page','int'); + $pagesize=OC_OCS::readdata('pagesize','int'); + 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'=>'')) + ->action(function ($parameters) { + $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); $message=OC_OCS::readdata('message','text'); OC_OCS::activityput($format,$message); - }), - array('_method'=>'post', - 'format'=>'xml|json'))); - // PRIVATEDATA - // get - GET DATA - $routes->add('privatedata_get', - new Route('/privatedata/getattribute/{app}/{key}.{format}', - array('app' => '', - 'key' => '', - 'format' => '', - 'action' => function ($parameters) { - $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); - $app = addslashes(strip_tags($parameters['app'])); - $key = addslashes(strip_tags($parameters['key'])); - OC_OCS::privateDataGet($format, $app, $key); - }), - array('format'=>'xml|json'))); - // set - POST DATA - $routes->add('privatedata_set', - new Route('/privatedata/setattribute/{app}/{key}.{format}', - array('format'=>'', - 'action' => function ($parameters) { - $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); - $app = addslashes(strip_tags($parameters['app'])); - $key = addslashes(strip_tags($parameters['key'])); - $value=OC_OCS::readdata('value','text'); - OC_OCS::privateDataSet($format, $app, $key, $value); - }), - array('_method'=>'post', - 'format'=>'xml|json'))); - // delete - POST DATA - $routes->add('privatedata_delete', - new Route('/privatedata/deleteattribute/{app}/{key}.{format}', - array('format'=>'', - 'action' => function ($parameters) { - $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); - $app = addslashes(strip_tags($parameters['app'])); - $key = addslashes(strip_tags($parameters['key'])); - OC_OCS::privateDataDelete($format, $app, $key); - }), - array('_method'=>'post', - 'format'=>'xml|json'))); - - $context = new RequestContext($_SERVER['REQUEST_URI'], $method); - - $matcher = new UrlMatcher($routes, $context); + }) + ->requirements(array('format'=>'xml|json')); + + // PRIVATEDATA + // get - GET DATA + $router->create('privatedata_get', + '/privatedata/getattribute/{app}/{key}.{format}') + ->defaults(array('app' => '', 'key' => '', 'format' => '')) + ->action(function ($parameters) { + $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); + $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'=>'')) + ->action(function ($parameters) { + $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); + $app = addslashes(strip_tags($parameters['app'])); + $key = addslashes(strip_tags($parameters['key'])); + $value=OC_OCS::readdata('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'=>'')) + ->action(function ($parameters) { + $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); + $app = addslashes(strip_tags($parameters['app'])); + $key = addslashes(strip_tags($parameters['key'])); + OC_OCS::privateDataDelete($format, $app, $key); + }) + ->requirements(array('format'=>'xml|json')); try { - $parameters = $matcher->match($_SERVER['PATH_INFO']); - $action = $parameters['action']; - unset($parameters['action']); - call_user_func($action, $parameters); + $router->match($_SERVER['PATH_INFO']); } catch (ResourceNotFoundException $e) { $format=OC_OCS::readdata('format','text'); $txt='Invalid query, please check the syntax. API specifications are here: http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n"; @@ -388,7 +357,8 @@ class OC_OCS { * @param string $format * @return string xml/json */ - private static function apiConfig($format) { + public static function apiConfig($parameters) { + $format = $parameters['format'] ? $parameters['format'] : OC_OCS::readdata('format','text'); $xml['version']='1.5'; $xml['website']='ownCloud'; $xml['host']=OCP\Util::getServerHost(); diff --git a/lib/route.php b/lib/route.php new file mode 100644 index 00000000000..4344c977113 --- /dev/null +++ b/lib/route.php @@ -0,0 +1,50 @@ +<?php +/** + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +use Symfony\Component\Routing\Route; + +class OC_Route extends Route { + public function method($method) { + $this->setRequirement('_method', $method); + return $this; + } + + public function post() { + $this->method('post'); + return $this; + } + + public function defaults($defaults) { + $action = $this->getDefault('action'); + $this->setDefaults($defaults); + if (isset($defaults['action'])) { + $action = $defaults['action']; + } + $this->action($action); + return $this; + } + + public function requirements($requirements) { + $method = $this->getRequirement('_method'); + $this->setRequirements($requirements); + if (isset($requirements['_method'])) { + $method = $requirements['_method']; + } + $this->method($method); + return $this; + } + + public function action($class, $function = null) { + $action = array($class, $function); + if (is_null($function)) { + $action = $class; + } + $this->setDefault('action', $action); + return $this; + } +} diff --git a/lib/router.php b/lib/router.php new file mode 100644 index 00000000000..f037ecdfef4 --- /dev/null +++ b/lib/router.php @@ -0,0 +1,50 @@ +<?php +/** + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +use Symfony\Component\Routing\Matcher\UrlMatcher; +use Symfony\Component\Routing\RequestContext; +use Symfony\Component\Routing\RouteCollection; +//use Symfony\Component\Routing\Route; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; + +class OC_Router { + protected $collections = array(); + protected $collection = null; + + public function useCollection($name) { + if (!isset($this->collections[$name])) { + $this->collections[$name] = new RouteCollection(); + } + $this->collection = $this->collections[$name]; + } + + public function create($name, $pattern, array $defaults = array(), array $requirements = array()) { + $route = new OC_Route($pattern, $defaults, $requirements); + $this->collection->add($name, $route); + return $route; + } + + public function match($url) { + $context = new RequestContext($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']); + $matcher = new UrlMatcher($this->collection, $context); + $parameters = $matcher->match($url); + if (isset($parameters['action'])) { + $action = $parameters['action']; + if (!is_callable($action)) { + var_dump($action); + throw new Exception('not a callable action'); + } + unset($parameters['action']); + call_user_func($action, $parameters); + } elseif (isset($parameters['file'])) { + include ($parameters['file']); + } else { + throw new Exception('no action available'); + } + } +} |