2012-07-28 21:40:11 +00:00
|
|
|
<?php
|
2015-10-05 18:54:56 +00:00
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Michael Gapczynski <GapczynskiM@gmail.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Tom Needham <tom@owncloud.com>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2015-08-03 14:05:50 +00:00
|
|
|
use OCP\API;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
|
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
|
|
|
|
*/
|
2015-04-18 07:17:58 +00:00
|
|
|
|
|
|
|
/** @deprecated Use \OCP\API::GUEST_AUTH instead */
|
2012-09-13 09:41:20 +00:00
|
|
|
const GUEST_AUTH = 0;
|
2015-04-18 07:17:58 +00:00
|
|
|
|
|
|
|
/** @deprecated Use \OCP\API::USER_AUTH instead */
|
2012-09-13 09:41:20 +00:00
|
|
|
const USER_AUTH = 1;
|
2015-04-18 07:17:58 +00:00
|
|
|
|
|
|
|
/** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */
|
2012-09-13 09:41:20 +00:00
|
|
|
const SUBADMIN_AUTH = 2;
|
2015-04-18 07:17:58 +00:00
|
|
|
|
|
|
|
/** @deprecated Use \OCP\API::ADMIN_AUTH instead */
|
2012-09-13 09:41:20 +00:00
|
|
|
const ADMIN_AUTH = 3;
|
2014-01-13 11:27:05 +00:00
|
|
|
|
2013-01-25 12:48:59 +00:00
|
|
|
/**
|
|
|
|
* API Response Codes
|
|
|
|
*/
|
2015-04-18 07:17:58 +00:00
|
|
|
|
|
|
|
/** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */
|
2013-01-25 12:48:59 +00:00
|
|
|
const RESPOND_UNAUTHORISED = 997;
|
2015-04-18 07:17:58 +00:00
|
|
|
|
|
|
|
/** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */
|
2013-01-25 12:48:59 +00:00
|
|
|
const RESPOND_SERVER_ERROR = 996;
|
2015-04-18 07:17:58 +00:00
|
|
|
|
|
|
|
/** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */
|
2013-01-25 12:48:59 +00:00
|
|
|
const RESPOND_NOT_FOUND = 998;
|
2015-04-18 07:17:58 +00:00
|
|
|
|
|
|
|
/** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */
|
2013-01-25 12:48:59 +00:00
|
|
|
const RESPOND_UNKNOWN_ERROR = 999;
|
2014-01-13 11:27:05 +00:00
|
|
|
|
2012-07-28 21:50:40 +00:00
|
|
|
/**
|
2012-12-31 15:47:15 +00:00
|
|
|
* api actions
|
|
|
|
*/
|
2012-07-30 10:56:21 +00:00
|
|
|
protected static $actions = array();
|
2013-10-21 16:58:46 +00:00
|
|
|
private static $logoutRequired = false;
|
2014-12-17 19:22:51 +00:00
|
|
|
private static $isLoggedIn = false;
|
2014-01-13 11:27:05 +00:00
|
|
|
|
2012-07-28 21:50:40 +00:00
|
|
|
/**
|
2012-12-31 15:47:15 +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
|
|
|
|
* @param string $app the id of the app registering the call
|
|
|
|
* @param int $authLevel the level of authentication required for the call
|
|
|
|
* @param array $defaults
|
|
|
|
* @param array $requirements
|
|
|
|
*/
|
2014-01-13 11:27:05 +00:00
|
|
|
public static function register($method, $url, $action, $app,
|
2015-08-03 14:05:50 +00:00
|
|
|
$authLevel = API::USER_AUTH,
|
2012-07-31 21:26:15 +00:00
|
|
|
$defaults = array(),
|
2012-12-31 15:47:15 +00:00
|
|
|
$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-12-31 15:47:15 +00:00
|
|
|
if(!isset(self::$actions[$name])) {
|
2014-07-01 14:45:00 +00:00
|
|
|
$oldCollection = OC::$server->getRouter()->getCurrentCollection();
|
2014-03-10 13:06:47 +00:00
|
|
|
OC::$server->getRouter()->useCollection('ocs');
|
|
|
|
OC::$server->getRouter()->create($name, $url)
|
2012-07-31 20:34:35 +00:00
|
|
|
->method($method)
|
2013-06-28 15:25:10 +00:00
|
|
|
->defaults($defaults)
|
|
|
|
->requirements($requirements)
|
2012-07-28 21:50:40 +00:00
|
|
|
->action('OC_API', 'call');
|
|
|
|
self::$actions[$name] = array();
|
2014-07-01 14:45:00 +00:00
|
|
|
OC::$server->getRouter()->useCollection($oldCollection);
|
2012-07-28 21:50:40 +00:00
|
|
|
}
|
2013-01-25 12:48:59 +00:00
|
|
|
self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel);
|
2012-07-28 21:50:40 +00:00
|
|
|
}
|
2014-01-13 11:27:05 +00:00
|
|
|
|
2012-07-28 21:50:40 +00:00
|
|
|
/**
|
2012-12-31 15:47:15 +00:00
|
|
|
* handles an api call
|
|
|
|
* @param array $parameters
|
|
|
|
*/
|
|
|
|
public static function call($parameters) {
|
2015-02-27 12:05:57 +00:00
|
|
|
$request = \OC::$server->getRequest();
|
2015-03-01 10:46:39 +00:00
|
|
|
$method = $request->getMethod();
|
2015-02-27 12:05:57 +00:00
|
|
|
|
2012-09-17 12:08:17 +00:00
|
|
|
// Prepare the request variables
|
2015-03-01 10:46:39 +00:00
|
|
|
if($method === 'PUT') {
|
2015-02-27 12:05:57 +00:00
|
|
|
$parameters['_put'] = $request->getParams();
|
2015-03-01 10:46:39 +00:00
|
|
|
} else if($method === 'DELETE') {
|
2015-02-27 12:05:57 +00:00
|
|
|
$parameters['_delete'] = $request->getParams();
|
2012-09-17 12:08:17 +00:00
|
|
|
}
|
2012-07-30 19:03:41 +00:00
|
|
|
$name = $parameters['_route'];
|
2013-01-25 12:48:59 +00:00
|
|
|
// Foreach registered action
|
|
|
|
$responses = array();
|
2017-08-01 16:57:00 +00:00
|
|
|
$appManager = \OC::$server->getAppManager();
|
2013-01-25 12:48:59 +00:00
|
|
|
foreach(self::$actions[$name] as $action) {
|
|
|
|
// Check authentication and availability
|
2013-04-27 16:45:23 +00:00
|
|
|
if(!self::isAuthorised($action)) {
|
2013-01-25 12:48:59 +00:00
|
|
|
$responses[] = array(
|
|
|
|
'app' => $action['app'],
|
2017-09-21 15:56:00 +00:00
|
|
|
'response' => new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'),
|
2017-08-01 16:57:00 +00:00
|
|
|
'shipped' => $appManager->isShipped($action['app']),
|
2013-01-25 12:48:59 +00:00
|
|
|
);
|
|
|
|
continue;
|
2013-01-14 19:30:39 +00:00
|
|
|
}
|
2013-01-25 12:48:59 +00:00
|
|
|
if(!is_callable($action['action'])) {
|
|
|
|
$responses[] = array(
|
|
|
|
'app' => $action['app'],
|
2017-09-21 15:56:00 +00:00
|
|
|
'response' => new \OC\OCS\Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'),
|
2017-08-01 16:57:00 +00:00
|
|
|
'shipped' => $appManager->isShipped($action['app']),
|
2013-01-25 12:48:59 +00:00
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Run the action
|
|
|
|
$responses[] = array(
|
|
|
|
'app' => $action['app'],
|
|
|
|
'response' => call_user_func($action['action'], $parameters),
|
2017-08-01 16:57:00 +00:00
|
|
|
'shipped' => $appManager->isShipped($action['app']),
|
2013-01-25 12:48:59 +00:00
|
|
|
);
|
2012-12-12 21:04:23 +00:00
|
|
|
}
|
2013-01-25 12:48:59 +00:00
|
|
|
$response = self::mergeResponses($responses);
|
2014-03-11 23:35:19 +00:00
|
|
|
$format = self::requestedFormat();
|
2013-10-21 16:58:46 +00:00
|
|
|
if (self::$logoutRequired) {
|
2016-04-18 10:14:07 +00:00
|
|
|
\OC::$server->getUserSession()->logout();
|
2013-10-21 16:58:46 +00:00
|
|
|
}
|
2013-05-02 12:51:53 +00:00
|
|
|
|
|
|
|
self::respond($response, $format);
|
2012-07-28 21:50:40 +00:00
|
|
|
}
|
2014-01-13 11:27:05 +00:00
|
|
|
|
2013-01-25 12:48:59 +00:00
|
|
|
/**
|
|
|
|
* merge the returned result objects into one response
|
|
|
|
* @param array $responses
|
2017-09-21 15:56:00 +00:00
|
|
|
* @return \OC\OCS\Result
|
2013-01-25 12:48:59 +00:00
|
|
|
*/
|
2013-11-13 22:46:24 +00:00
|
|
|
public static function mergeResponses($responses) {
|
2014-03-28 10:25:55 +00:00
|
|
|
// Sort into shipped and third-party
|
2013-01-25 12:48:59 +00:00
|
|
|
$shipped = array(
|
2013-02-09 12:50:19 +00:00
|
|
|
'succeeded' => array(),
|
2013-01-25 12:48:59 +00:00
|
|
|
'failed' => array(),
|
|
|
|
);
|
|
|
|
$thirdparty = array(
|
|
|
|
'succeeded' => array(),
|
|
|
|
'failed' => array(),
|
|
|
|
);
|
2013-01-14 19:30:39 +00:00
|
|
|
|
2013-01-25 12:48:59 +00:00
|
|
|
foreach($responses as $response) {
|
2013-11-13 22:46:24 +00:00
|
|
|
if($response['shipped'] || ($response['app'] === 'core')) {
|
2013-01-25 12:48:59 +00:00
|
|
|
if($response['response']->succeeded()) {
|
2013-11-13 22:46:24 +00:00
|
|
|
$shipped['succeeded'][$response['app']] = $response;
|
2013-01-25 12:48:59 +00:00
|
|
|
} else {
|
2013-11-13 22:46:24 +00:00
|
|
|
$shipped['failed'][$response['app']] = $response;
|
2013-01-25 12:48:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if($response['response']->succeeded()) {
|
2013-11-13 22:46:24 +00:00
|
|
|
$thirdparty['succeeded'][$response['app']] = $response;
|
2013-01-25 12:48:59 +00:00
|
|
|
} else {
|
2013-11-13 22:46:24 +00:00
|
|
|
$thirdparty['failed'][$response['app']] = $response;
|
2013-01-25 12:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-02 12:36:16 +00:00
|
|
|
|
2013-01-25 12:48:59 +00:00
|
|
|
// Remove any error responses if there is one shipped response that succeeded
|
2013-11-14 01:14:37 +00:00
|
|
|
if(!empty($shipped['failed'])) {
|
2013-01-25 12:48:59 +00:00
|
|
|
// Which shipped response do we use if they all failed?
|
|
|
|
// They may have failed for different reasons (different status codes)
|
2014-03-28 10:25:55 +00:00
|
|
|
// Which response code should we return?
|
2015-04-18 08:30:02 +00:00
|
|
|
// Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR
|
2013-11-14 01:19:46 +00:00
|
|
|
// Merge failed responses if more than one
|
|
|
|
$data = array();
|
|
|
|
foreach($shipped['failed'] as $failure) {
|
|
|
|
$data = array_merge_recursive($data, $failure['response']->getData());
|
|
|
|
}
|
|
|
|
$picked = reset($shipped['failed']);
|
|
|
|
$code = $picked['response']->getStatusCode();
|
2014-01-13 11:27:05 +00:00
|
|
|
$meta = $picked['response']->getMeta();
|
2015-08-10 19:33:50 +00:00
|
|
|
$headers = $picked['response']->getHeaders();
|
2017-09-21 15:56:00 +00:00
|
|
|
$response = new \OC\OCS\Result($data, $code, $meta['message'], $headers);
|
2013-11-14 01:19:46 +00:00
|
|
|
return $response;
|
2013-11-14 01:14:37 +00:00
|
|
|
} elseif(!empty($shipped['succeeded'])) {
|
|
|
|
$responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
|
2013-05-02 12:36:16 +00:00
|
|
|
} elseif(!empty($thirdparty['failed'])) {
|
2013-11-14 01:21:54 +00:00
|
|
|
// Merge failed responses if more than one
|
|
|
|
$data = array();
|
|
|
|
foreach($thirdparty['failed'] as $failure) {
|
|
|
|
$data = array_merge_recursive($data, $failure['response']->getData());
|
|
|
|
}
|
|
|
|
$picked = reset($thirdparty['failed']);
|
|
|
|
$code = $picked['response']->getStatusCode();
|
2014-01-13 11:27:05 +00:00
|
|
|
$meta = $picked['response']->getMeta();
|
2015-08-10 19:33:50 +00:00
|
|
|
$headers = $picked['response']->getHeaders();
|
2017-09-21 15:56:00 +00:00
|
|
|
$response = new \OC\OCS\Result($data, $code, $meta['message'], $headers);
|
2013-11-14 01:21:54 +00:00
|
|
|
return $response;
|
2013-05-02 12:36:16 +00:00
|
|
|
} else {
|
2013-11-13 00:45:49 +00:00
|
|
|
$responses = $thirdparty['succeeded'];
|
2013-01-25 12:48:59 +00:00
|
|
|
}
|
|
|
|
// Merge the successful responses
|
2015-08-10 19:33:50 +00:00
|
|
|
$data = [];
|
|
|
|
$codes = [];
|
|
|
|
$header = [];
|
2013-05-02 12:36:16 +00:00
|
|
|
|
2014-04-21 13:44:54 +00:00
|
|
|
foreach($responses as $response) {
|
2013-11-13 22:46:24 +00:00
|
|
|
if($response['shipped']) {
|
|
|
|
$data = array_merge_recursive($response['response']->getData(), $data);
|
2013-01-25 12:48:59 +00:00
|
|
|
} else {
|
2013-11-13 22:46:24 +00:00
|
|
|
$data = array_merge_recursive($data, $response['response']->getData());
|
2013-01-25 12:48:59 +00:00
|
|
|
}
|
2015-08-10 19:33:50 +00:00
|
|
|
$header = array_merge_recursive($header, $response['response']->getHeaders());
|
|
|
|
$codes[] = ['code' => $response['response']->getStatusCode(),
|
|
|
|
'meta' => $response['response']->getMeta()];
|
2013-01-25 12:48:59 +00:00
|
|
|
}
|
2013-11-13 00:45:49 +00:00
|
|
|
|
|
|
|
// Use any non 100 status codes
|
|
|
|
$statusCode = 100;
|
2014-01-13 11:27:05 +00:00
|
|
|
$statusMessage = null;
|
2013-11-13 00:45:49 +00:00
|
|
|
foreach($codes as $code) {
|
2014-01-13 11:27:05 +00:00
|
|
|
if($code['code'] != 100) {
|
|
|
|
$statusCode = $code['code'];
|
|
|
|
$statusMessage = $code['meta']['message'];
|
2013-11-13 00:45:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 15:56:00 +00:00
|
|
|
return new \OC\OCS\Result($data, $statusCode, $statusMessage, $header);
|
2013-01-25 12:48:59 +00:00
|
|
|
}
|
2014-01-13 11:27:05 +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-12-31 15:47:15 +00:00
|
|
|
private static function isAuthorised($action) {
|
2012-09-13 09:41:20 +00:00
|
|
|
$level = $action['authlevel'];
|
2012-12-31 15:47:15 +00:00
|
|
|
switch($level) {
|
2015-08-03 14:05:50 +00:00
|
|
|
case API::GUEST_AUTH:
|
2012-09-13 09:41:20 +00:00
|
|
|
// Anyone can access
|
|
|
|
return true;
|
2015-08-03 14:05:50 +00:00
|
|
|
case API::USER_AUTH:
|
2012-09-13 09:41:20 +00:00
|
|
|
// User required
|
2012-09-13 15:18:38 +00:00
|
|
|
return self::loginUser();
|
2015-08-03 14:05:50 +00:00
|
|
|
case API::SUBADMIN_AUTH:
|
2012-09-13 09:41:20 +00:00
|
|
|
// Check for subadmin
|
2012-09-13 15:18:38 +00:00
|
|
|
$user = self::loginUser();
|
2012-12-31 15:47:15 +00:00
|
|
|
if(!$user) {
|
2012-09-13 15:18:38 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2015-10-27 13:09:45 +00:00
|
|
|
$userObject = \OC::$server->getUserSession()->getUser();
|
|
|
|
if($userObject === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
|
2013-02-09 12:00:44 +00:00
|
|
|
$admin = OC_User::isAdminUser($user);
|
2015-10-27 13:09:45 +00:00
|
|
|
if($isSubAdmin || $admin) {
|
2012-09-14 13:41:06 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-13 15:18:38 +00:00
|
|
|
}
|
2015-08-03 14:05:50 +00:00
|
|
|
case API::ADMIN_AUTH:
|
2012-09-13 09:41:20 +00:00
|
|
|
// Check for admin
|
2012-09-13 15:18:38 +00:00
|
|
|
$user = self::loginUser();
|
2012-12-31 15:47:15 +00:00
|
|
|
if(!$user) {
|
2012-09-13 15:18:38 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2013-02-09 12:00:44 +00:00
|
|
|
return OC_User::isAdminUser($user);
|
2012-09-13 15:18:38 +00:00
|
|
|
}
|
2012-09-13 09:41:20 +00:00
|
|
|
default:
|
|
|
|
// oops looks like invalid level supplied
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-13 11:27:05 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 09:41:20 +00:00
|
|
|
/**
|
2012-09-13 15:18:38 +00:00
|
|
|
* http basic auth
|
2016-02-17 15:13:37 +00:00
|
|
|
* @return string|false (username, or false on failure)
|
2012-09-13 09:41:20 +00:00
|
|
|
*/
|
2014-12-17 19:22:51 +00:00
|
|
|
private static function loginUser() {
|
|
|
|
if(self::$isLoggedIn === true) {
|
|
|
|
return \OC_User::getUser();
|
|
|
|
}
|
2014-03-07 19:00:34 +00:00
|
|
|
|
2014-03-07 19:06:06 +00:00
|
|
|
// reuse existing login
|
2016-04-27 07:38:30 +00:00
|
|
|
$loggedIn = \OC::$server->getUserSession()->isLoggedIn();
|
2014-03-28 10:25:55 +00:00
|
|
|
if ($loggedIn === true) {
|
2016-08-24 08:42:07 +00:00
|
|
|
if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
|
2016-06-01 09:19:49 +00:00
|
|
|
// Do not allow access to OCS until the 2FA challenge was solved successfully
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-28 10:25:55 +00:00
|
|
|
$ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false;
|
|
|
|
if ($ocsApiRequest) {
|
2014-03-07 19:00:34 +00:00
|
|
|
|
2014-03-28 10:25:55 +00:00
|
|
|
// initialize the user's filesystem
|
2016-06-07 11:41:44 +00:00
|
|
|
\OC_Util::setupFS(\OC_User::getUser());
|
2014-12-17 19:22:51 +00:00
|
|
|
self::$isLoggedIn = true;
|
2014-03-07 19:00:34 +00:00
|
|
|
|
2014-03-28 10:25:55 +00:00
|
|
|
return OC_User::getUser();
|
|
|
|
}
|
|
|
|
return false;
|
2014-03-07 19:06:06 +00:00
|
|
|
}
|
2014-03-07 19:00:34 +00:00
|
|
|
|
2014-03-28 10:25:55 +00:00
|
|
|
// basic auth - because OC_User::login will create a new session we shall only try to login
|
|
|
|
// if user and pass are set
|
2016-04-27 07:38:30 +00:00
|
|
|
$userSession = \OC::$server->getUserSession();
|
2016-04-29 12:00:07 +00:00
|
|
|
$request = \OC::$server->getRequest();
|
2016-04-27 07:38:30 +00:00
|
|
|
try {
|
2017-01-20 09:46:29 +00:00
|
|
|
if ($userSession->tryTokenLogin($request)
|
|
|
|
|| $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) {
|
|
|
|
self::$logoutRequired = true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2016-04-20 14:27:16 +00:00
|
|
|
}
|
2016-04-27 07:38:30 +00:00
|
|
|
// initialize the user's filesystem
|
2016-06-07 11:41:44 +00:00
|
|
|
\OC_Util::setupFS(\OC_User::getUser());
|
2016-04-27 07:38:30 +00:00
|
|
|
self::$isLoggedIn = true;
|
2016-03-07 10:17:33 +00:00
|
|
|
|
2016-04-27 07:38:30 +00:00
|
|
|
return \OC_User::getUser();
|
2017-01-20 09:46:29 +00:00
|
|
|
} catch (\OC\User\LoginException $e) {
|
|
|
|
return false;
|
2013-10-21 16:58:46 +00:00
|
|
|
}
|
2012-09-13 09:41:20 +00:00
|
|
|
}
|
2014-01-13 11:27:05 +00:00
|
|
|
|
2012-07-28 21:50:40 +00:00
|
|
|
/**
|
2012-12-31 15:47:15 +00:00
|
|
|
* respond to a call
|
2017-09-21 15:56:00 +00:00
|
|
|
* @param \OC\OCS\Result $result
|
2012-12-31 15:47:15 +00:00
|
|
|
* @param string $format the format xml|json
|
|
|
|
*/
|
2014-06-30 13:37:38 +00:00
|
|
|
public static function respond($result, $format='xml') {
|
2016-02-15 14:38:37 +00:00
|
|
|
$request = \OC::$server->getRequest();
|
|
|
|
|
2013-01-25 12:48:59 +00:00
|
|
|
// Send 401 headers if unauthorised
|
2015-08-03 14:05:50 +00:00
|
|
|
if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
|
2016-02-15 14:38:37 +00:00
|
|
|
// If request comes from JS return dummy auth request
|
|
|
|
if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
|
|
|
|
header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
|
|
|
|
} else {
|
|
|
|
header('WWW-Authenticate: Basic realm="Authorisation Required"');
|
|
|
|
}
|
2013-01-25 12:48:59 +00:00
|
|
|
header('HTTP/1.0 401 Unauthorized');
|
|
|
|
}
|
2015-08-03 14:05:50 +00:00
|
|
|
|
2015-08-07 11:12:43 +00:00
|
|
|
foreach($result->getHeaders() as $name => $value) {
|
|
|
|
header($name . ': ' . $value);
|
|
|
|
}
|
|
|
|
|
2015-08-05 22:01:56 +00:00
|
|
|
$meta = $result->getMeta();
|
|
|
|
$data = $result->getData();
|
2016-02-15 14:38:37 +00:00
|
|
|
if (self::isV2($request)) {
|
2015-08-03 14:05:50 +00:00
|
|
|
$statusCode = self::mapStatusCodes($result->getStatusCode());
|
|
|
|
if (!is_null($statusCode)) {
|
2015-08-05 22:01:56 +00:00
|
|
|
$meta['statuscode'] = $statusCode;
|
2015-08-03 14:05:50 +00:00
|
|
|
OC_Response::setStatus($statusCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 19:03:11 +00:00
|
|
|
self::setContentType($format);
|
2015-08-05 22:01:56 +00:00
|
|
|
$body = self::renderResult($format, $meta, $data);
|
2015-08-03 19:03:11 +00:00
|
|
|
echo $body;
|
2012-07-28 21:50:40 +00:00
|
|
|
}
|
2012-08-01 17:48:51 +00:00
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param XMLWriter $writer
|
|
|
|
*/
|
2012-12-31 15:47:15 +00:00
|
|
|
private static function toXML($array, $writer) {
|
2012-08-01 17:48:51 +00:00
|
|
|
foreach($array as $k => $v) {
|
2013-01-25 12:48:59 +00:00
|
|
|
if ($k[0] === '@') {
|
|
|
|
$writer->writeAttribute(substr($k, 1), $v);
|
|
|
|
continue;
|
|
|
|
} else if (is_numeric($k)) {
|
2012-08-02 15:48:09 +00:00
|
|
|
$k = 'element';
|
|
|
|
}
|
2013-01-25 12:48:59 +00:00
|
|
|
if(is_array($v)) {
|
2012-08-01 17:48:51 +00:00
|
|
|
$writer->startElement($k);
|
|
|
|
self::toXML($v, $writer);
|
|
|
|
$writer->endElement();
|
|
|
|
} else {
|
|
|
|
$writer->writeElement($k, $v);
|
|
|
|
}
|
|
|
|
}
|
2012-09-14 13:41:06 +00:00
|
|
|
}
|
2014-01-13 11:27:05 +00:00
|
|
|
|
2014-03-11 23:35:19 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function requestedFormat() {
|
|
|
|
$formats = array('json', 'xml');
|
|
|
|
|
|
|
|
$format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
|
|
|
|
return $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on the requested format the response content type is set
|
2016-02-17 13:21:07 +00:00
|
|
|
* @param string $format
|
2014-03-11 23:35:19 +00:00
|
|
|
*/
|
2015-08-03 19:03:11 +00:00
|
|
|
public static function setContentType($format = null) {
|
|
|
|
$format = is_null($format) ? self::requestedFormat() : $format;
|
2014-03-11 23:35:19 +00:00
|
|
|
if ($format === 'xml') {
|
|
|
|
header('Content-type: text/xml; charset=UTF-8');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($format === 'json') {
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
header('Content-Type: application/octet-stream; charset=utf-8');
|
|
|
|
}
|
|
|
|
|
2015-08-03 14:05:50 +00:00
|
|
|
/**
|
2015-10-12 21:39:16 +00:00
|
|
|
* @param \OCP\IRequest $request
|
|
|
|
* @return bool
|
2015-08-03 14:05:50 +00:00
|
|
|
*/
|
2015-10-12 21:39:16 +00:00
|
|
|
protected static function isV2(\OCP\IRequest $request) {
|
2015-08-03 14:05:50 +00:00
|
|
|
$script = $request->getScriptName();
|
2014-03-11 23:35:19 +00:00
|
|
|
|
2015-10-12 21:39:16 +00:00
|
|
|
return substr($script, -11) === '/ocs/v2.php';
|
2015-08-03 14:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param integer $sc
|
2015-08-05 09:23:29 +00:00
|
|
|
* @return int
|
2015-08-03 14:05:50 +00:00
|
|
|
*/
|
|
|
|
public static function mapStatusCodes($sc) {
|
|
|
|
switch ($sc) {
|
|
|
|
case API::RESPOND_NOT_FOUND:
|
|
|
|
return Http::STATUS_NOT_FOUND;
|
|
|
|
case API::RESPOND_SERVER_ERROR:
|
|
|
|
return Http::STATUS_INTERNAL_SERVER_ERROR;
|
|
|
|
case API::RESPOND_UNKNOWN_ERROR:
|
|
|
|
return Http::STATUS_INTERNAL_SERVER_ERROR;
|
|
|
|
case API::RESPOND_UNAUTHORISED:
|
|
|
|
// already handled for v1
|
|
|
|
return null;
|
|
|
|
case 100:
|
|
|
|
return Http::STATUS_OK;
|
|
|
|
}
|
|
|
|
// any 2xx, 4xx and 5xx will be used as is
|
|
|
|
if ($sc >= 200 && $sc < 600) {
|
|
|
|
return $sc;
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:49:44 +00:00
|
|
|
return Http::STATUS_BAD_REQUEST;
|
2015-08-03 14:05:50 +00:00
|
|
|
}
|
2015-08-03 19:03:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $format
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-05 22:01:56 +00:00
|
|
|
public static function renderResult($format, $meta, $data) {
|
2015-08-03 19:03:11 +00:00
|
|
|
$response = array(
|
|
|
|
'ocs' => array(
|
2015-08-05 22:01:56 +00:00
|
|
|
'meta' => $meta,
|
|
|
|
'data' => $data,
|
2015-08-03 19:03:11 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
if ($format == 'json') {
|
|
|
|
return OC_JSON::encode($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
$writer = new XMLWriter();
|
|
|
|
$writer->openMemory();
|
|
|
|
$writer->setIndent(true);
|
|
|
|
$writer->startDocument();
|
|
|
|
self::toXML($response, $writer);
|
|
|
|
$writer->endDocument();
|
|
|
|
return $writer->outputMemory(true);
|
|
|
|
}
|
2012-07-30 19:03:41 +00:00
|
|
|
}
|