nextcloud/lib/private/legacy/OC_API.php

190 lines
4.8 KiB
PHP
Raw Normal View History

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