nextcloud/lib/api.php

170 lines
4.5 KiB
PHP
Raw Normal View History

<?php
/**
2012-07-29 01:50:40 +04:00
* ownCloud
*
* @author Tom Needham
* @author Michael Gapczynski
* @author Bart Visscher
* @copyright 2012 Tom Needham tom@owncloud.com
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com
* @copyright 2012 Bart Visscher bartv@thisnet.nl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
2012-08-03 15:47:05 +04:00
2012-07-29 01:50:40 +04:00
class OC_API {
private static $server;
/**
* initialises the OAuth store and server
*/
private static function init() {
2012-08-03 15:47:05 +04:00
self::$server = new OC_OAuth_Server(new OC_OAuth_Store());
}
2012-07-29 01:50:40 +04:00
/**
* api actions
*/
protected static $actions = array();
2012-07-29 01:50:40 +04:00
/**
* registers an api call
* @param string $method the http method
* @param string $url the url to match
* @param callable $action the function to run
* @param string $app the id of the app registering the call
2012-07-29 01:50:40 +04:00
*/
2012-08-01 01:26:15 +04:00
public static function register($method, $url, $action, $app,
$defaults = array(),
$requirements = array()){
2012-07-29 01:50:40 +04:00
$name = strtolower($method).$url;
2012-07-30 22:52:47 +04:00
$name = str_replace(array('/', '{', '}'), '_', $name);
2012-07-29 01:50:40 +04:00
if(!isset(self::$actions[$name])){
OC::getRouter()->useCollection('ocs');
OC::getRouter()->create($name, $url.'.{_format}')
->method($method)
2012-08-01 01:26:15 +04:00
->defaults(array('_format' => 'xml') + $defaults)
->requirements(array('_format' => 'xml|json') + $requirements)
2012-07-29 01:50:40 +04:00
->action('OC_API', 'call');
self::$actions[$name] = array();
}
self::$actions[$name][] = array('app' => $app, 'action' => $action);
2012-07-29 01:50:40 +04:00
}
/**
* handles an api call
* @param array $parameters
*/
public static function call($parameters){
2012-07-30 23:03:41 +04:00
$name = $parameters['_route'];
2012-07-29 01:50:40 +04:00
// Loop through registered actions
foreach(self::$actions[$name] as $action){
2012-07-30 17:39:06 +04:00
$app = $action['app'];
if(is_callable($action['action'])){
2012-07-30 17:39:06 +04:00
$responses[] = array('app' => $app, 'response' => call_user_func($action['action'], $parameters));
2012-07-29 01:50:40 +04:00
} else {
2012-07-30 17:39:06 +04:00
$responses[] = array('app' => $app, 'response' => 501);
2012-07-29 01:50:40 +04:00
}
}
2012-07-30 17:39:06 +04:00
// Merge the responses
$response = self::mergeResponses($responses);
2012-07-29 01:50:40 +04:00
// Send the response
if(isset($parameters['_format'])){
self::respond($response, $parameters['_format']);
} else {
self::respond($response);
}
// logout the user to be stateles
OC_User::logout();
2012-07-29 01:50:40 +04:00
}
2012-07-30 17:39:06 +04:00
/**
* intelligently merges the different responses
* @param array $responses
* @return array the final merged response
*/
private static function mergeResponses($responses){
$finalresponse = array();
$numresponses = count($responses);
foreach($responses as $response){
if(is_int($response) && empty($finalresponse)){
$finalresponse = $response;
continue;
}
if(is_array($response)){
// Shipped apps win
if(OC_App::isShipped($response['app'])){
2012-07-30 20:04:09 +04:00
$finalresponse = array_merge_recursive($finalresponse, $response);
2012-07-30 17:39:06 +04:00
} else {
2012-07-30 20:04:09 +04:00
$finalresponse = array_merge_recursive($response, $finalresponse);
2012-07-30 17:39:06 +04:00
}
}
}
return $finalresponse;
}
2012-07-29 01:50:40 +04:00
/**
* respond to a call
* @param int|array $response the response
* @param string $format the format xml|json
*/
2012-07-30 23:03:41 +04:00
private static function respond($response, $format='json'){
if ($format == 'json') {
2012-08-01 21:48:51 +04:00
OC_JSON::encodedPrint($response);
} else if ($format == 'xml') {
header('Content-type: text/xml; charset=UTF-8');
$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent( true );
$writer->startDocument();
self::toXML($response, $writer);
$writer->endDocument();
echo $writer->outputMemory(true);
2012-07-30 23:03:41 +04:00
} else {
var_dump($format, $response);
}
2012-07-29 01:50:40 +04:00
}
2012-08-01 21:48:51 +04:00
private static function toXML($array, $writer){
foreach($array as $k => $v) {
2012-08-02 19:48:09 +04:00
if (is_numeric($k)) {
$k = 'element';
}
2012-08-01 21:48:51 +04:00
if (is_array($v)) {
$writer->startElement($k);
self::toXML($v, $writer);
$writer->endElement();
} else {
$writer->writeElement($k, $v);
}
}
}
2012-07-29 01:50:40 +04:00
2012-08-01 17:37:00 +04:00
/**
* check if the user is authenticated
*/
public static function checkLoggedIn(){
// Check OAuth
if(!OC_OAuthServer::isAuthorised()){
2012-08-01 17:37:00 +04:00
OC_Response::setStatus(401);
die();
}
}
2012-07-30 23:03:41 +04:00
}