2012-07-28 21:40:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-07-28 21:50:40 +00: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 11:47:05 +00:00
|
|
|
|
2012-07-28 21:50:40 +00:00
|
|
|
class OC_API {
|
2012-08-03 00:02:31 +00:00
|
|
|
|
2012-09-13 09:41:20 +00:00
|
|
|
/**
|
|
|
|
* API authentication levels
|
|
|
|
*/
|
|
|
|
const GUEST_AUTH = 0;
|
|
|
|
const USER_AUTH = 1;
|
|
|
|
const SUBADMIN_AUTH = 2;
|
|
|
|
const ADMIN_AUTH = 3;
|
|
|
|
|
2012-08-03 00:02:31 +00:00
|
|
|
private static $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* initialises the OAuth store and server
|
|
|
|
*/
|
|
|
|
private static function init() {
|
2012-08-03 11:47:05 +00:00
|
|
|
self::$server = new OC_OAuth_Server(new OC_OAuth_Store());
|
2012-08-03 00:02:31 +00:00
|
|
|
}
|
2012-07-28 21:50:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* api actions
|
|
|
|
*/
|
2012-07-30 10:56:21 +00:00
|
|
|
protected static $actions = array();
|
2012-07-28 21:50:40 +00: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
|
2012-07-30 12:56:01 +00:00
|
|
|
* @param string $app the id of the app registering the call
|
2012-09-13 09:41:20 +00:00
|
|
|
* @param int $authlevel the level of authentication required for the call
|
|
|
|
* @param array $defaults
|
|
|
|
* @param array $requirements
|
2012-07-28 21:50:40 +00:00
|
|
|
*/
|
2012-09-13 09:41:20 +00:00
|
|
|
public static function register($method, $url, $action, $app,
|
|
|
|
$authlevel = OC_API::USER_AUTH,
|
2012-07-31 21:26:15 +00:00
|
|
|
$defaults = array(),
|
|
|
|
$requirements = array()){
|
2012-07-28 21:50:40 +00:00
|
|
|
$name = strtolower($method).$url;
|
2012-07-30 18:52:47 +00:00
|
|
|
$name = str_replace(array('/', '{', '}'), '_', $name);
|
2012-07-28 21:50:40 +00:00
|
|
|
if(!isset(self::$actions[$name])){
|
2012-08-02 19:51:31 +00:00
|
|
|
OC::getRouter()->useCollection('ocs');
|
2012-08-02 15:47:38 +00:00
|
|
|
OC::getRouter()->create($name, $url.'.{_format}')
|
2012-07-31 20:34:35 +00:00
|
|
|
->method($method)
|
2012-07-31 21:26:15 +00:00
|
|
|
->defaults(array('_format' => 'xml') + $defaults)
|
|
|
|
->requirements(array('_format' => 'xml|json') + $requirements)
|
2012-07-28 21:50:40 +00:00
|
|
|
->action('OC_API', 'call');
|
|
|
|
self::$actions[$name] = array();
|
|
|
|
}
|
2012-09-13 09:41:20 +00:00
|
|
|
self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authlevel);
|
2012-07-28 21:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handles an api call
|
|
|
|
* @param array $parameters
|
|
|
|
*/
|
2012-07-30 10:56:21 +00:00
|
|
|
public static function call($parameters){
|
2012-07-30 19:03:41 +00:00
|
|
|
$name = $parameters['_route'];
|
2012-07-28 21:50:40 +00:00
|
|
|
// Loop through registered actions
|
|
|
|
foreach(self::$actions[$name] as $action){
|
2012-07-30 13:39:06 +00:00
|
|
|
$app = $action['app'];
|
2012-09-13 09:41:20 +00:00
|
|
|
// Authorsie this call
|
2012-09-13 15:18:38 +00:00
|
|
|
if(self::isAuthorised($action)){
|
2012-08-03 11:56:11 +00:00
|
|
|
if(is_callable($action['action'])){
|
|
|
|
$responses[] = array('app' => $app, 'response' => call_user_func($action['action'], $parameters));
|
|
|
|
} else {
|
|
|
|
$responses[] = array('app' => $app, 'response' => 501);
|
|
|
|
}
|
2012-09-13 09:41:20 +00:00
|
|
|
} else {
|
|
|
|
$responses[] = array('app' => $app, 'response' => 401);
|
|
|
|
}
|
2012-08-03 11:56:11 +00:00
|
|
|
|
2012-07-28 21:50:40 +00:00
|
|
|
}
|
2012-07-30 13:39:06 +00:00
|
|
|
// Merge the responses
|
|
|
|
$response = self::mergeResponses($responses);
|
2012-07-28 21:50:40 +00:00
|
|
|
// Send the response
|
|
|
|
if(isset($parameters['_format'])){
|
|
|
|
self::respond($response, $parameters['_format']);
|
|
|
|
} else {
|
|
|
|
self::respond($response);
|
|
|
|
}
|
2012-09-13 15:18:38 +00:00
|
|
|
// logout the user to be stateless
|
2012-08-01 13:39:05 +00:00
|
|
|
OC_User::logout();
|
2012-07-28 21:50:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 09:41:20 +00:00
|
|
|
/**
|
|
|
|
* authenticate the api call
|
|
|
|
* @param array $action the action details as supplied to OC_API::register()
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-13 15:18:38 +00:00
|
|
|
private static function isAuthorised($action){
|
2012-09-13 09:41:20 +00:00
|
|
|
$level = $action['authlevel'];
|
|
|
|
switch($level){
|
|
|
|
case OC_API::GUEST_AUTH:
|
|
|
|
// Anyone can access
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case OC_API::USER_AUTH:
|
|
|
|
// User required
|
2012-09-13 15:18:38 +00:00
|
|
|
return self::loginUser();
|
2012-09-13 09:41:20 +00:00
|
|
|
break;
|
|
|
|
case OC_API::SUBADMIN_AUTH:
|
|
|
|
// Check for subadmin
|
2012-09-13 15:18:38 +00:00
|
|
|
$user = self::loginUser();
|
|
|
|
if(!$user){
|
|
|
|
return false;
|
|
|
|
} else {
|
2012-09-14 13:41:06 +00:00
|
|
|
$subadmin = OC_SubAdmin::isSubAdmin($user);
|
|
|
|
$admin = OC_Group::inGroup($user, 'admin');
|
|
|
|
if($subadmin || $admin){
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-13 15:18:38 +00:00
|
|
|
}
|
2012-09-13 09:41:20 +00:00
|
|
|
break;
|
|
|
|
case OC_API::ADMIN_AUTH:
|
|
|
|
// Check for admin
|
2012-09-13 15:18:38 +00:00
|
|
|
$user = self::loginUser();
|
|
|
|
if(!$user){
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return OC_Group::inGroup($user, 'admin');
|
|
|
|
}
|
2012-09-13 09:41:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// oops looks like invalid level supplied
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-13 15:18:38 +00:00
|
|
|
* http basic auth
|
|
|
|
* @return string|false (username, or false on failure)
|
2012-09-13 09:41:20 +00:00
|
|
|
*/
|
2012-09-13 15:18:38 +00:00
|
|
|
private static function loginUser(){
|
|
|
|
$authuser = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
|
|
|
|
$authpw = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
|
|
|
|
return OC_User::login($authuser, $authpw) ? $authuser : false;
|
2012-09-13 09:41:20 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 13:39:06 +00:00
|
|
|
/**
|
|
|
|
* intelligently merges the different responses
|
|
|
|
* @param array $responses
|
|
|
|
* @return array the final merged response
|
|
|
|
*/
|
|
|
|
private static function mergeResponses($responses){
|
2012-09-04 13:50:56 +00:00
|
|
|
$finalresponse = array(
|
|
|
|
'meta' => array(
|
|
|
|
'statuscode' => '',
|
|
|
|
),
|
|
|
|
'data' => array(),
|
|
|
|
);
|
2012-07-30 13:39:06 +00:00
|
|
|
$numresponses = count($responses);
|
|
|
|
|
|
|
|
foreach($responses as $response){
|
2012-09-04 13:50:56 +00:00
|
|
|
if(is_int($response['response']) && empty($finalresponse['meta']['statuscode'])){
|
|
|
|
$finalresponse['meta']['statuscode'] = $response['response'];
|
2012-07-30 13:39:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-08-03 15:20:01 +00:00
|
|
|
if(is_array($response['response'])){
|
2012-07-30 13:39:06 +00:00
|
|
|
// Shipped apps win
|
|
|
|
if(OC_App::isShipped($response['app'])){
|
2012-09-04 13:50:56 +00:00
|
|
|
$finalresponse['data'] = array_merge_recursive($finalresponse['data'], $response['response']);
|
2012-07-30 13:39:06 +00:00
|
|
|
} else {
|
2012-09-04 13:50:56 +00:00
|
|
|
$finalresponse['data'] = array_merge_recursive($response['response'], $finalresponse['data']);
|
2012-07-30 13:39:06 +00:00
|
|
|
}
|
2012-09-04 13:50:56 +00:00
|
|
|
$finalresponse['meta']['statuscode'] = 100;
|
2012-07-30 13:39:06 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-04 13:50:56 +00:00
|
|
|
//Some tidying up
|
|
|
|
if($finalresponse['meta']['statuscode']=='100'){
|
|
|
|
$finalresponse['meta']['status'] = 'ok';
|
|
|
|
} else {
|
|
|
|
$finalresponse['meta']['status'] = 'failure';
|
|
|
|
}
|
|
|
|
if(empty($finalresponse['data'])){
|
|
|
|
unset($finalresponse['data']);
|
|
|
|
}
|
|
|
|
return array('ocs' => $finalresponse);
|
2012-07-30 13:39:06 +00:00
|
|
|
}
|
|
|
|
|
2012-07-28 21:50:40 +00:00
|
|
|
/**
|
|
|
|
* respond to a call
|
|
|
|
* @param int|array $response the response
|
|
|
|
* @param string $format the format xml|json
|
|
|
|
*/
|
2012-07-30 19:03:41 +00:00
|
|
|
private static function respond($response, $format='json'){
|
|
|
|
if ($format == 'json') {
|
2012-08-01 17:48:51 +00: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 19:03:41 +00:00
|
|
|
} else {
|
|
|
|
var_dump($format, $response);
|
|
|
|
}
|
2012-07-28 21:50:40 +00:00
|
|
|
}
|
2012-08-01 17:48:51 +00:00
|
|
|
|
|
|
|
private static function toXML($array, $writer){
|
|
|
|
foreach($array as $k => $v) {
|
2012-08-02 15:48:09 +00:00
|
|
|
if (is_numeric($k)) {
|
|
|
|
$k = 'element';
|
|
|
|
}
|
2012-08-01 17:48:51 +00:00
|
|
|
if (is_array($v)) {
|
|
|
|
$writer->startElement($k);
|
|
|
|
self::toXML($v, $writer);
|
|
|
|
$writer->endElement();
|
|
|
|
} else {
|
|
|
|
$writer->writeElement($k, $v);
|
|
|
|
}
|
|
|
|
}
|
2012-09-14 13:41:06 +00:00
|
|
|
}
|
2012-08-01 13:37:00 +00:00
|
|
|
|
2012-07-30 19:03:41 +00:00
|
|
|
}
|