2010-04-14 18:58:52 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
2010-05-17 01:13:42 +04:00
|
|
|
* @author Frank Karlitschek
|
2012-07-23 20:34:21 +04:00
|
|
|
* @author Michael Gapczynski
|
2012-05-26 21:14:24 +04:00
|
|
|
* @copyright 2012 Frank Karlitschek frank@owncloud.org
|
2012-07-23 20:34:21 +04:00
|
|
|
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com
|
2010-05-17 01:13:42 +04:00
|
|
|
*
|
2010-04-14 18:58:52 +04:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
2010-05-17 01:13:42 +04:00
|
|
|
* License as published by the Free Software Foundation; either
|
2010-04-14 18:58:52 +04:00
|
|
|
* version 3 of the License, or any later version.
|
2010-05-17 01:13:42 +04:00
|
|
|
*
|
2010-04-14 18:58:52 +04:00
|
|
|
* 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.
|
2010-05-17 01:13:42 +04:00
|
|
|
*
|
2011-02-09 17:50:27 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public
|
2010-04-14 18:58:52 +04:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2010-05-17 01:13:42 +04:00
|
|
|
*
|
2010-04-14 18:58:52 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to handle open collaboration services API requests
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class OC_OCS {
|
|
|
|
|
2012-07-23 20:34:21 +04:00
|
|
|
/**
|
2014-09-30 15:42:08 +04:00
|
|
|
* reads input data from get/post and converts the date to a special data-type
|
2012-07-23 20:34:21 +04:00
|
|
|
*
|
2014-02-08 14:47:55 +04:00
|
|
|
* @param string $method HTTP method to read the key from
|
|
|
|
* @param string $key Parameter to read
|
|
|
|
* @param string $type Variable type to format data
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $default Default value to return if the key is not found
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return string Data or if the key is not found and no default is set it will exit with a 400 Bad request
|
2012-07-23 20:34:21 +04:00
|
|
|
*/
|
|
|
|
public static function readData($method, $key, $type = 'raw', $default = null) {
|
2014-04-21 17:44:54 +04:00
|
|
|
$data = false;
|
2012-07-23 20:34:21 +04:00
|
|
|
if ($method == 'get') {
|
|
|
|
if (isset($_GET[$key])) {
|
|
|
|
$data = $_GET[$key];
|
|
|
|
} else if (isset($default)) {
|
|
|
|
return $default;
|
|
|
|
} else {
|
|
|
|
$data = false;
|
|
|
|
}
|
|
|
|
} else if ($method == 'post') {
|
|
|
|
if (isset($_POST[$key])) {
|
|
|
|
$data = $_POST[$key];
|
|
|
|
} else if (isset($default)) {
|
|
|
|
return $default;
|
|
|
|
} else {
|
|
|
|
$data = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($data === false) {
|
|
|
|
echo self::generateXml('', 'fail', 400, 'Bad request. Please provide a valid '.$key);
|
|
|
|
exit();
|
|
|
|
} else {
|
|
|
|
// NOTE: Is the raw type necessary? It might be a little risky without sanitization
|
|
|
|
if ($type == 'raw') return $data;
|
|
|
|
elseif ($type == 'text') return OC_Util::sanitizeHTML($data);
|
|
|
|
elseif ($type == 'int') return (int) $data;
|
|
|
|
elseif ($type == 'float') return (float) $data;
|
|
|
|
elseif ($type == 'array') return OC_Util::sanitizeHTML($data);
|
|
|
|
else return OC_Util::sanitizeHTML($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 23:03:41 +04:00
|
|
|
public static function notFound() {
|
|
|
|
if($_SERVER['REQUEST_METHOD'] == 'GET') {
|
|
|
|
$method='get';
|
|
|
|
}elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
|
|
|
|
$method='put';
|
2012-09-10 15:59:08 +04:00
|
|
|
parse_str(file_get_contents("php://input"), $put_vars);
|
2012-07-30 23:03:41 +04:00
|
|
|
}elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
|
|
$method='post';
|
|
|
|
}else{
|
|
|
|
echo('internal server error: method not supported');
|
|
|
|
exit();
|
|
|
|
}
|
2012-07-23 20:34:21 +04:00
|
|
|
|
2012-07-30 23:03:41 +04:00
|
|
|
$format = self::readData($method, 'format', 'text', '');
|
2013-02-11 20:44:02 +04:00
|
|
|
$txt='Invalid query, please check the syntax. API specifications are here:'
|
|
|
|
.' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n";
|
2012-07-30 23:03:41 +04:00
|
|
|
$txt.=OC_OCS::getDebugOutput();
|
2013-02-09 20:35:47 +04:00
|
|
|
echo(OC_OCS::generateXml($format, 'failed', 999, $txt));
|
2012-07-23 20:34:21 +04:00
|
|
|
|
2012-07-30 23:03:41 +04:00
|
|
|
}
|
|
|
|
|
2012-07-23 20:34:21 +04:00
|
|
|
/**
|
2014-09-30 15:42:08 +04:00
|
|
|
* generated some debug information to make it easier to find failed API calls
|
|
|
|
* @return string data
|
2012-07-23 20:34:21 +04:00
|
|
|
*/
|
|
|
|
private static function getDebugOutput() {
|
|
|
|
$txt='';
|
|
|
|
$txt.="debug output:\n";
|
|
|
|
if(isset($_SERVER['REQUEST_METHOD'])) $txt.='http request method: '.$_SERVER['REQUEST_METHOD']."\n";
|
|
|
|
if(isset($_SERVER['REQUEST_URI'])) $txt.='http request uri: '.$_SERVER['REQUEST_URI']."\n";
|
|
|
|
if(isset($_GET)) foreach($_GET as $key=>$value) $txt.='get parameter: '.$key.'->'.$value."\n";
|
|
|
|
if(isset($_POST)) foreach($_POST as $key=>$value) $txt.='post parameter: '.$key.'->'.$value."\n";
|
|
|
|
return($txt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-21 17:44:54 +04:00
|
|
|
* generates the xml or json response for the API call from an multidimenional data array.
|
|
|
|
* @param string $format
|
|
|
|
* @param string $status
|
|
|
|
* @param string $statuscode
|
|
|
|
* @param string $message
|
|
|
|
* @param array $data
|
|
|
|
* @param string $tag
|
|
|
|
* @param string $tagattribute
|
|
|
|
* @param int $dimension
|
|
|
|
* @param int|string $itemscount
|
|
|
|
* @param int|string $itemsperpage
|
|
|
|
* @return string xml/json
|
|
|
|
*/
|
2013-02-11 20:44:02 +04:00
|
|
|
private static function generateXml($format, $status, $statuscode,
|
|
|
|
$message, $data=array(), $tag='', $tagattribute='', $dimension=-1, $itemscount='', $itemsperpage='') {
|
2012-07-23 20:34:21 +04:00
|
|
|
if($format=='json') {
|
|
|
|
$json=array();
|
|
|
|
$json['status']=$status;
|
|
|
|
$json['statuscode']=$statuscode;
|
|
|
|
$json['message']=$message;
|
|
|
|
$json['totalitems']=$itemscount;
|
|
|
|
$json['itemsperpage']=$itemsperpage;
|
|
|
|
$json['data']=$data;
|
|
|
|
return(json_encode($json));
|
|
|
|
}else{
|
|
|
|
$txt='';
|
|
|
|
$writer = xmlwriter_open_memory();
|
|
|
|
xmlwriter_set_indent( $writer, 2 );
|
|
|
|
xmlwriter_start_document($writer );
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_start_element($writer, 'ocs');
|
|
|
|
xmlwriter_start_element($writer, 'meta');
|
|
|
|
xmlwriter_write_element($writer, 'status', $status);
|
|
|
|
xmlwriter_write_element($writer, 'statuscode', $statuscode);
|
|
|
|
xmlwriter_write_element($writer, 'message', $message);
|
2012-10-27 19:45:09 +04:00
|
|
|
if($itemscount<>'') xmlwriter_write_element($writer, 'totalitems', $itemscount);
|
2012-09-10 15:59:08 +04:00
|
|
|
if(!empty($itemsperpage)) xmlwriter_write_element($writer, 'itemsperpage', $itemsperpage);
|
2012-07-23 20:34:21 +04:00
|
|
|
xmlwriter_end_element($writer);
|
|
|
|
if($dimension=='0') {
|
|
|
|
// 0 dimensions
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_write_element($writer, 'data', $data);
|
2012-07-23 20:34:21 +04:00
|
|
|
|
|
|
|
}elseif($dimension=='1') {
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_start_element($writer, 'data');
|
2012-07-23 20:34:21 +04:00
|
|
|
foreach($data as $key=>$entry) {
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_write_element($writer, $key, $entry);
|
2012-07-23 20:34:21 +04:00
|
|
|
}
|
|
|
|
xmlwriter_end_element($writer);
|
|
|
|
|
|
|
|
}elseif($dimension=='2') {
|
2012-10-27 19:45:09 +04:00
|
|
|
xmlwriter_start_element($writer, 'data');
|
2012-07-23 20:34:21 +04:00
|
|
|
foreach($data as $entry) {
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_start_element($writer, $tag);
|
|
|
|
if(!empty($tagattribute)) {
|
|
|
|
xmlwriter_write_attribute($writer, 'details', $tagattribute);
|
|
|
|
}
|
|
|
|
foreach($entry as $key=>$value) {
|
|
|
|
if(is_array($value)) {
|
|
|
|
foreach($value as $k=>$v) {
|
|
|
|
xmlwriter_write_element($writer, $k, $v);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xmlwriter_write_element($writer, $key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xmlwriter_end_element($writer);
|
2013-02-09 20:27:57 +04:00
|
|
|
}
|
2012-07-23 20:34:21 +04:00
|
|
|
xmlwriter_end_element($writer);
|
|
|
|
|
|
|
|
}elseif($dimension=='3') {
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_start_element($writer, 'data');
|
2012-07-23 20:34:21 +04:00
|
|
|
foreach($data as $entrykey=>$entry) {
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_start_element($writer, $tag);
|
|
|
|
if(!empty($tagattribute)) {
|
|
|
|
xmlwriter_write_attribute($writer, 'details', $tagattribute);
|
|
|
|
}
|
|
|
|
foreach($entry as $key=>$value) {
|
|
|
|
if(is_array($value)) {
|
|
|
|
xmlwriter_start_element($writer, $entrykey);
|
|
|
|
foreach($value as $k=>$v) {
|
|
|
|
xmlwriter_write_element($writer, $k, $v);
|
|
|
|
}
|
|
|
|
xmlwriter_end_element($writer);
|
|
|
|
} else {
|
|
|
|
xmlwriter_write_element($writer, $key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xmlwriter_end_element($writer);
|
2012-07-23 20:34:21 +04:00
|
|
|
}
|
|
|
|
xmlwriter_end_element($writer);
|
|
|
|
}elseif($dimension=='dynamic') {
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_start_element($writer, 'data');
|
|
|
|
OC_OCS::toxml($writer, $data, 'comment');
|
2012-07-23 20:34:21 +04:00
|
|
|
xmlwriter_end_element($writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlwriter_end_element($writer);
|
|
|
|
|
|
|
|
xmlwriter_end_document( $writer );
|
|
|
|
$txt.=xmlwriter_output_memory( $writer );
|
|
|
|
unset($writer);
|
|
|
|
return($txt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param resource $writer
|
|
|
|
* @param array $data
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param string $node
|
|
|
|
*/
|
2012-10-27 19:45:09 +04:00
|
|
|
public static function toXml($writer, $data, $node) {
|
2012-07-23 20:34:21 +04:00
|
|
|
foreach($data as $key => $value) {
|
|
|
|
if (is_numeric($key)) {
|
|
|
|
$key = $node;
|
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
if (is_array($value)) {
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_start_element($writer, $key);
|
2012-10-27 19:45:09 +04:00
|
|
|
OC_OCS::toxml($writer, $value, $node);
|
2012-07-23 20:34:21 +04:00
|
|
|
xmlwriter_end_element($writer);
|
|
|
|
}else{
|
2012-09-10 15:59:08 +04:00
|
|
|
xmlwriter_write_element($writer, $key, $value);
|
2012-07-23 20:34:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-06 03:22:48 +03:00
|
|
|
}
|