2012-07-28 17:40:11 -04:00
|
|
|
<?php
|
2015-10-05 14:54:56 -04:00
|
|
|
/**
|
2016-07-21 11:07:57 -04:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2015-10-05 14:54:56 -04:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-05-26 13:56:05 -04:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-03-31 04:49:10 -04:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2015-10-05 14:54:56 -04:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 13:56:05 -04:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-10-05 14:54:56 -04:00
|
|
|
* @author Michael Gapczynski <GapczynskiM@gmail.com>
|
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 11:07:57 -04:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-10-05 14:54:56 -04: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,
|
2019-12-03 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-10-05 14:54:56 -04:00
|
|
|
*
|
|
|
|
|
*/
|
2015-08-03 10:05:50 -04:00
|
|
|
use OCP\API;
|
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
|
|
2012-07-28 17:50:40 -04:00
|
|
|
class OC_API {
|
2012-08-02 20:02:31 -04:00
|
|
|
|
2012-07-28 17:50:40 -04:00
|
|
|
/**
|
2012-12-31 10:47:15 -05:00
|
|
|
* api actions
|
|
|
|
|
*/
|
2020-03-26 04:30:18 -04:00
|
|
|
protected static $actions = [];
|
2014-01-13 06:27:05 -05:00
|
|
|
|
2012-07-28 17:50:40 -04:00
|
|
|
/**
|
2012-12-31 10:47:15 -05:00
|
|
|
* respond to a call
|
2017-09-21 11:56:00 -04:00
|
|
|
* @param \OC\OCS\Result $result
|
2012-12-31 10:47:15 -05:00
|
|
|
* @param string $format the format xml|json
|
|
|
|
|
*/
|
2014-06-30 09:37:38 -04:00
|
|
|
public static function respond($result, $format='xml') {
|
2016-02-15 09:38:37 -05:00
|
|
|
$request = \OC::$server->getRequest();
|
|
|
|
|
|
2013-01-25 07:48:59 -05:00
|
|
|
// Send 401 headers if unauthorised
|
2020-04-10 08:19:56 -04:00
|
|
|
if ($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
|
2016-02-15 09:38:37 -05:00
|
|
|
// If request comes from JS return dummy auth request
|
2020-04-10 08:19:56 -04:00
|
|
|
if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
|
2016-02-15 09:38:37 -05:00
|
|
|
header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
|
|
|
|
|
} else {
|
|
|
|
|
header('WWW-Authenticate: Basic realm="Authorisation Required"');
|
|
|
|
|
}
|
2018-06-26 04:32:50 -04:00
|
|
|
http_response_code(401);
|
2013-01-25 07:48:59 -05:00
|
|
|
}
|
2015-08-03 10:05:50 -04:00
|
|
|
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($result->getHeaders() as $name => $value) {
|
2015-08-07 07:12:43 -04:00
|
|
|
header($name . ': ' . $value);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 18:01:56 -04:00
|
|
|
$meta = $result->getMeta();
|
|
|
|
|
$data = $result->getData();
|
2016-02-15 09:38:37 -05:00
|
|
|
if (self::isV2($request)) {
|
2015-08-03 10:05:50 -04:00
|
|
|
$statusCode = self::mapStatusCodes($result->getStatusCode());
|
|
|
|
|
if (!is_null($statusCode)) {
|
2015-08-05 18:01:56 -04:00
|
|
|
$meta['statuscode'] = $statusCode;
|
2018-06-26 04:32:50 -04:00
|
|
|
http_response_code($statusCode);
|
2015-08-03 10:05:50 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 15:03:11 -04:00
|
|
|
self::setContentType($format);
|
2015-08-05 18:01:56 -04:00
|
|
|
$body = self::renderResult($format, $meta, $data);
|
2015-08-03 15:03:11 -04:00
|
|
|
echo $body;
|
2012-07-28 17:50:40 -04:00
|
|
|
}
|
2012-08-01 13:48:51 -04:00
|
|
|
|
2014-02-06 10:30:58 -05:00
|
|
|
/**
|
|
|
|
|
* @param XMLWriter $writer
|
|
|
|
|
*/
|
2012-12-31 10:47:15 -05:00
|
|
|
private static function toXML($array, $writer) {
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($array as $k => $v) {
|
2013-01-25 07:48:59 -05:00
|
|
|
if ($k[0] === '@') {
|
|
|
|
|
$writer->writeAttribute(substr($k, 1), $v);
|
|
|
|
|
continue;
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif (is_numeric($k)) {
|
2012-08-02 11:48:09 -04:00
|
|
|
$k = 'element';
|
|
|
|
|
}
|
2020-04-10 08:19:56 -04:00
|
|
|
if (is_array($v)) {
|
2012-08-01 13:48:51 -04:00
|
|
|
$writer->startElement($k);
|
|
|
|
|
self::toXML($v, $writer);
|
|
|
|
|
$writer->endElement();
|
|
|
|
|
} else {
|
|
|
|
|
$writer->writeElement($k, $v);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-14 09:41:06 -04:00
|
|
|
}
|
2014-01-13 06:27:05 -05:00
|
|
|
|
2014-03-11 19:35:19 -04:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function requestedFormat() {
|
2020-03-26 04:30:18 -04:00
|
|
|
$formats = ['json', 'xml'];
|
2014-03-11 19:35:19 -04:00
|
|
|
|
|
|
|
|
$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 08:21:07 -05:00
|
|
|
* @param string $format
|
2014-03-11 19:35:19 -04:00
|
|
|
*/
|
2015-08-03 15:03:11 -04:00
|
|
|
public static function setContentType($format = null) {
|
|
|
|
|
$format = is_null($format) ? self::requestedFormat() : $format;
|
2014-03-11 19:35:19 -04: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 10:05:50 -04:00
|
|
|
/**
|
2015-10-12 17:39:16 -04:00
|
|
|
* @param \OCP\IRequest $request
|
|
|
|
|
* @return bool
|
2015-08-03 10:05:50 -04:00
|
|
|
*/
|
2015-10-12 17:39:16 -04:00
|
|
|
protected static function isV2(\OCP\IRequest $request) {
|
2015-08-03 10:05:50 -04:00
|
|
|
$script = $request->getScriptName();
|
2014-03-11 19:35:19 -04:00
|
|
|
|
2015-10-12 17:39:16 -04:00
|
|
|
return substr($script, -11) === '/ocs/v2.php';
|
2015-08-03 10:05:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param integer $sc
|
2015-08-05 05:23:29 -04:00
|
|
|
* @return int
|
2015-08-03 10:05:50 -04: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 11:49:44 -04:00
|
|
|
return Http::STATUS_BAD_REQUEST;
|
2015-08-03 10:05:50 -04:00
|
|
|
}
|
2015-08-03 15:03:11 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $format
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2015-08-05 18:01:56 -04:00
|
|
|
public static function renderResult($format, $meta, $data) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$response = [
|
|
|
|
|
'ocs' => [
|
2015-08-05 18:01:56 -04:00
|
|
|
'meta' => $meta,
|
|
|
|
|
'data' => $data,
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
|
|
|
|
];
|
2015-08-03 15:03:11 -04: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 15:03:41 -04:00
|
|
|
}
|