first part of the new new OCS CLOUD module. So far only quota is implemented.

The specification is developed here:
http://www.freedesktop.org/wiki/Specifications/open-collaboration-services-1.7#CLOUD
will be moved to
http://www.freedesktop.org/wiki/Specifications/open-collaboration-services
as soon as everything is done.
This commit is contained in:
Frank Karlitschek 2012-07-23 18:34:21 +02:00
parent bd63fddbf4
commit e7e1f234dd
2 changed files with 436 additions and 383 deletions

View File

@ -4,7 +4,9 @@
* ownCloud
*
* @author Frank Karlitschek
* @author Michael Gapczynski
* @copyright 2012 Frank Karlitschek frank@owncloud.org
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
@ -32,54 +34,48 @@ class OC_OCS {
/**
* reads input date from get/post/cookies and converts the date to a special data-type
*
* @param variable $key
* @param variable-type $type
* @param priority $getpriority
* @param default $default
* @return data
* @param string HTTP method to read the key from
* @param string Parameter to read
* @param string Variable type to format data
* @param mixed Default value to return if the key is not found
* @return mixed Data or if the key is not found and no default is set it will exit with a 400 Bad request
*/
public static function readData($key,$type='raw',$getpriority=false,$default='') {
if($getpriority) {
if(isset($_GET[$key])) {
$data=$_GET[$key];
} elseif(isset($_POST[$key])) {
$data=$_POST[$key];
public static function readData($method, $key, $type = 'raw', $default = null) {
if ($method == 'get') {
if (isset($_GET[$key])) {
$data = $_GET[$key];
} else if (isset($default)) {
return $default;
} else {
if($default=='') {
if(($type=='int') or ($type=='float')) $data=0; else $data='';
$data = false;
}
} else if ($method == 'post') {
if (isset($_POST[$key])) {
$data = $_POST[$key];
} else if (isset($default)) {
return $default;
} else {
$data=$default;
$data = false;
}
}
if ($data === false) {
echo self::generateXml('', 'fail', 400, 'Bad request. Please provide a valid '.$key);
exit();
} else {
if(isset($_POST[$key])) {
$data=$_POST[$key];
} elseif(isset($_GET[$key])) {
$data=$_GET[$key];
} elseif(isset($_COOKIE[$key])) {
$data=$_COOKIE[$key];
} else {
if($default=='') {
if(($type=='int') or ($type=='float')) $data=0; else $data='';
} else {
$data=$default;
// 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);
}
}
}
if($type=='raw') return($data);
elseif($type=='text') return(addslashes(strip_tags($data)));
elseif($type=='int') { $data = (int) $data; return($data); }
elseif($type=='float') { $data = (float) $data; return($data); }
elseif($type=='array') { $data = $data; return($data); }
}
/**
main function to handle the REST request
**/
public static function handle() {
// overwrite the 404 error page returncode
header("HTTP/1.0 200 OK");
@ -88,6 +84,7 @@ class OC_OCS {
$method='get';
}elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
$method='put';
parse_str(file_get_contents("php://input"),$put_vars);
}elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
$method='post';
}else{
@ -96,73 +93,92 @@ class OC_OCS {
}
// preprocess url
$url=$_SERVER['REQUEST_URI'];
$url = strtolower($_SERVER['REQUEST_URI']);
if(substr($url,(strlen($url)-1))<>'/') $url.='/';
$ex=explode('/',$url);
$paracount=count($ex);
$format = self::readData($method, 'format', 'text', '');
// eventhandler
// CONFIG
// apiconfig - GET - CONFIG
if(($method=='get') and (strtolower($ex[$paracount-3])=='v1.php') and (strtolower($ex[$paracount-2])=='config')){
$format=OC_OCS::readdata('format','text');
if(($method=='get') and ($ex[$paracount-3] == 'v1.php') and ($ex[$paracount-2] == 'config')){
OC_OCS::apiconfig($format);
// PERSON
// personcheck - POST - PERSON/CHECK
}elseif(($method=='post') and (strtolower($ex[$paracount-4])=='v1.php') and (strtolower($ex[$paracount-3])=='person') and (strtolower($ex[$paracount-2])=='check')){
$format=OC_OCS::readdata('format','text');
$login=OC_OCS::readdata('login','text');
$passwd=OC_OCS::readdata('password','text');
}elseif(($method=='post') and ($ex[$paracount-4] == 'v1.php') and ($ex[$paracount-3]=='person') and ($ex[$paracount-2] == 'check')){
$login = self::readData($method, 'login', 'text');
$passwd = self::readData($method, 'password', 'text');
OC_OCS::personcheck($format,$login,$passwd);
/*
} else if ($method == 'post' && $ex[$paracount - 4] == 'v1.php' && $ex[$paracount - 3] == 'person' && $ex[$paracount - 2] == 'add') {
if (OC_Group::inGroup(self::checkPassword(), 'admin')) {
$login = self::readData($method, 'login', 'text');
$password = self::readData($method, 'password', 'text');
try {
OC_User::createUser($login, $password);
echo self::generateXml($format, 'ok', 201, '');
} catch (Exception $exception) {
echo self::generateXml($format, 'fail', 400, $exception->getMessage());
}
} else {
echo self::generateXml($format, 'fail', 403, 'Permission denied');
}
*/
// ACTIVITY
// activityget - GET ACTIVITY page,pagesize als urlparameter
}elseif(($method=='get') and (strtolower($ex[$paracount-3])=='v1.php')and (strtolower($ex[$paracount-2])=='activity')){
$format=OC_OCS::readdata('format','text');
$page=OC_OCS::readdata('page','int');
$pagesize=OC_OCS::readdata('pagesize','int');
}elseif(($method=='get') and ($ex[$paracount-3] == 'v1.php') and ($ex[$paracount-2] == 'activity')){
$page = self::readData($method, 'page', 'int', 0);
$pagesize = self::readData($method, 'pagesize','int', 10);
if($pagesize<1 or $pagesize>100) $pagesize=10;
OC_OCS::activityget($format,$page,$pagesize);
// activityput - POST ACTIVITY
}elseif(($method=='post') and (strtolower($ex[$paracount-3])=='v1.php')and (strtolower($ex[$paracount-2])=='activity')){
$format=OC_OCS::readdata('format','text');
$message=OC_OCS::readdata('message','text');
}elseif(($method=='post') and ($ex[$paracount-3] == 'v1.php') and ($ex[$paracount-2] == 'activity')){
$message = self::readData($method, 'message', 'text');
OC_OCS::activityput($format,$message);
// PRIVATEDATA
// get - GET DATA
}elseif(($method=='get') and (strtolower($ex[$paracount-4])=='v1.php')and (strtolower($ex[$paracount-2])=='getattribute')){
$format=OC_OCS::readdata('format','text');
}elseif(($method=='get') and ($ex[$paracount-4] == 'v1.php') and ($ex[$paracount-2] == 'getattribute')){
OC_OCS::privateDataGet($format);
}elseif(($method=='get') and (strtolower($ex[$paracount-5])=='v1.php')and (strtolower($ex[$paracount-3])=='getattribute')){
$format=OC_OCS::readdata('format','text');
}elseif(($method=='get') and ($ex[$paracount-5] == 'v1.php') and ($ex[$paracount-3] == 'getattribute')){
$app=$ex[$paracount-2];
OC_OCS::privateDataGet($format, $app);
}elseif(($method=='get') and (strtolower($ex[$paracount-6])=='v1.php')and (strtolower($ex[$paracount-4])=='getattribute')){
$format=OC_OCS::readdata('format','text');
}elseif(($method=='get') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-4] == 'getattribute')){
$key=$ex[$paracount-2];
$app=$ex[$paracount-3];
OC_OCS::privateDataGet($format, $app,$key);
// set - POST DATA
}elseif(($method=='post') and (strtolower($ex[$paracount-6])=='v1.php')and (strtolower($ex[$paracount-4])=='setattribute')){
$format=OC_OCS::readdata('format','text');
}elseif(($method=='post') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-4] == 'setattribute')){
$key=$ex[$paracount-2];
$app=$ex[$paracount-3];
$value=OC_OCS::readdata('value','text');
$value = self::readData($method, 'value', 'text');
OC_OCS::privatedataset($format, $app, $key, $value);
// delete - POST DATA
}elseif(($method=='post') and (strtolower($ex[$paracount-6])=='v1.php')and (strtolower($ex[$paracount-4])=='deleteattribute')){
$format=OC_OCS::readdata('format','text');
}elseif(($method=='post') and ($ex[$paracount-6] =='v1.php') and ($ex[$paracount-4] == 'deleteattribute')){
$key=$ex[$paracount-2];
$app=$ex[$paracount-3];
OC_OCS::privatedatadelete($format, $app, $key);
// CLOUD
// quotaget - GET QUOTA parameter
}elseif(($method=='get') and ($ex[$paracount-5] == 'v1.php') and ($ex[$paracount-4]=='cloud') and ($ex[$paracount-3] == 'files') and ($ex[$paracount-2] == 'quota')){
OC_OCS::quotaget($format);
// add more calls here
// please document all the call in the draft spec
// http://www.freedesktop.org/wiki/Specifications/open-collaboration-services-1.7#CLOUD
}else{
$format=OC_OCS::readdata('format','text');
$txt='Invalid query, please check the syntax. API specifications are here: http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n";
$txt.=OC_OCS::getdebugoutput();
echo(OC_OCS::generatexml($format,'failed',999,$txt));
@ -239,7 +255,6 @@ class OC_OCS {
*/
private static function generateXml($format,$status,$statuscode,$message,$data=array(),$tag='',$tagattribute='',$dimension=-1,$itemscount='',$itemsperpage='') {
if($format=='json') {
$json=array();
$json['status']=$status;
$json['statuscode']=$statuscode;
@ -248,8 +263,6 @@ class OC_OCS {
$json['itemsperpage']=$itemsperpage;
$json['data']=$data;
return(json_encode($json));
}else{
$txt='';
$writer = xmlwriter_open_memory();
@ -342,7 +355,6 @@ class OC_OCS {
}else{
xmlwriter_write_element($writer,$key,$value);
}
}
}
@ -355,7 +367,10 @@ class OC_OCS {
* @return string xml/json
*/
private static function apiConfig($format) {
$xml['version']='1.5';
$user=OC_OCS::checkpassword(false);
$url=substr(OCP\Util::getServerHost().$_SERVER['SCRIPT_NAME'],0,-11).'';
$xml['version']='1.7';
$xml['website']='ownCloud';
$xml['host']=OCP\Util::getServerHost();
$xml['contact']='';
@ -412,7 +427,7 @@ class OC_OCS {
*/
private static function activityPut($format,$message) {
// not implemented in ownCloud
OC_OCS::checkpassword();
$user=OC_OCS::checkpassword();
echo(OC_OCS::generatexml($format,'ok',100,''));
}
@ -525,4 +540,40 @@ class OC_OCS {
public static function deleteData($user, $app, $key) {
return OC_Preferences::deleteKey($user,$app,$key);
}
// CLOUD API #############################################
/**
* get the quota of the current user
* @param string $format
* @return string xml/json
*/
private static function quotaGet($format) {
$user=OC_OCS::checkpassword();
// calculate the disc space
$user_dir = '/'.$user.'/files';
OC_Filesystem::init($user_dir);
$rootInfo=OC_FileCache::get('');
$sharedInfo=OC_FileCache::get('/Shared');
$used=$rootInfo['size']-$sharedInfo['size'];
$free=OC_Filesystem::free_space();
$total=$free+$used;
if($total==0) $total=1; // prevent division by zero
$relative=round(($used/$total)*10000)/100;
$xml=array();
$xml['quota']=$total;
$xml['free']=$free;
$xml['used']=$used;
$xml['relative']=$relative;
$txt=OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'cloud', 'full', 1, count($xml), 0);
echo($txt);
}
}

View File

@ -35,7 +35,9 @@ echo('
<termsofuse></termsofuse>
<register></register>
<services>
<activity ocsversion="1.5" />
<config ocsversion="1.7" />
<activity ocsversion="1.7" />
<cloud ocsversion="1.7" />
</services>
</provider>
</providers>