Convert routing to ownCloud fluid interface

This commit is contained in:
Bart Visscher 2012-07-23 18:58:52 +02:00
parent d0cae6a99a
commit 768b44b9b6
3 changed files with 178 additions and 108 deletions

View File

@ -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();
$router = new OC_Router();
$router->useCollection('ocs');
// 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')));
$router->create('config', '/config.{format}')
->defaults(array('format'=>''))
->action('OC_OCS', 'apiConfig')
->requirements(array('format'=>'xml|json'));
// PERSON
$routes->add('person_check',
new Route('/person/check.{format}',
array('format'=>'',
'action' => function ($parameters) {
$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);
}),
array('_method'=>'post',
'format'=>'xml|json')));
})
->requirements(array('format'=>'xml|json'));
// ACTIVITY
// activityget - GET ACTIVITY page,pagesize als urlparameter
$routes->add('activity_get',
new Route('/activity.{format}',
array('format'=>'',
'action' => function ($parameters) {
$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);
}),
array('format'=>'xml|json')));
})
->requirements(array('format'=>'xml|json'));
// activityput - POST ACTIVITY
$routes->add('activity_put',
new Route('/activity.{format}',
array('format'=>'',
'action' => function ($parameters) {
$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')));
})
->requirements(array('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) {
$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);
}),
array('format'=>'xml|json')));
})
->requirements(array('format'=>'xml|json'));
// set - POST DATA
$routes->add('privatedata_set',
new Route('/privatedata/setattribute/{app}/{key}.{format}',
array('format'=>'',
'action' => function ($parameters) {
$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);
}),
array('_method'=>'post',
'format'=>'xml|json')));
})
->requirements(array('format'=>'xml|json'));
// delete - POST DATA
$routes->add('privatedata_delete',
new Route('/privatedata/deleteattribute/{app}/{key}.{format}',
array('format'=>'',
'action' => function ($parameters) {
$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);
}),
array('_method'=>'post',
'format'=>'xml|json')));
$context = new RequestContext($_SERVER['REQUEST_URI'], $method);
$matcher = new UrlMatcher($routes, $context);
})
->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();

50
lib/route.php Normal file
View File

@ -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;
}
}

50
lib/router.php Normal file
View File

@ -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');
}
}
}