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
*/
2012-08-11 02:04:43 +04:00
use Symfony\Component\Routing\Exception\ResourceNotFoundException ;
use Symfony\Component\Routing\Exception\MethodNotAllowedException ;
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
/**
* reads input date from get / post / cookies and converts the date to a special data - type
*
* @ 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 ( $method , $key , $type = 'raw' , $default = null ) {
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 );
}
}
/**
main function to handle the REST request
**/
public static function handle () {
// overwrite the 404 error page returncode
header ( " HTTP/1.0 200 OK " );
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-23 20:34:21 +04:00
} elseif ( $_SERVER [ 'REQUEST_METHOD' ] == 'POST' ) {
$method = 'post' ;
} else {
echo ( 'internal server error: method not supported' );
exit ();
}
$format = self :: readData ( $method , 'format' , 'text' , '' );
2012-07-24 20:35:09 +04:00
$router = new OC_Router ();
2012-08-11 02:04:43 +04:00
$router -> useCollection ( 'root' );
2012-07-23 20:34:21 +04:00
// CONFIG
2012-07-24 20:35:09 +04:00
$router -> create ( 'config' , '/config.{format}' )
-> defaults ( array ( 'format' => $format ))
-> action ( 'OC_OCS' , 'apiConfig' )
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-23 20:34:21 +04:00
// PERSON
2012-07-24 20:35:09 +04:00
$router -> create ( 'person_check' , '/person/check.{format}' )
-> post ()
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$login = OC_OCS :: readData ( 'post' , 'login' , 'text' );
$passwd = OC_OCS :: readData ( 'post' , 'password' , 'text' );
2012-10-27 19:45:09 +04:00
OC_OCS :: personCheck ( $format , $login , $passwd );
})
2012-07-24 20:35:09 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-23 20:34:21 +04:00
// ACTIVITY
// activityget - GET ACTIVITY page,pagesize als urlparameter
2012-07-24 20:35:09 +04:00
$router -> create ( 'activity_get' , '/activity.{format}' )
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$page = OC_OCS :: readData ( 'get' , 'page' , 'int' , 0 );
$pagesize = OC_OCS :: readData ( 'get' , 'pagesize' , 'int' , 10 );
if ( $pagesize < 1 or $pagesize > 100 ) $pagesize = 10 ;
OC_OCS :: activityGet ( $format , $page , $pagesize );
2012-10-27 19:45:09 +04:00
})
2012-07-24 20:35:09 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-23 20:34:21 +04:00
// activityput - POST ACTIVITY
2012-07-24 20:35:09 +04:00
$router -> create ( 'activity_put' , '/activity.{format}' )
-> post ()
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$message = OC_OCS :: readData ( 'post' , 'message' , 'text' );
2012-10-27 19:45:09 +04:00
OC_OCS :: activityPut ( $format , $message );
})
2012-07-24 20:35:09 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-23 20:34:21 +04:00
// PRIVATEDATA
// get - GET DATA
2012-07-24 20:35:09 +04:00
$router -> create ( 'privatedata_get' ,
'/privatedata/getattribute/{app}/{key}.{format}' )
-> defaults ( array ( 'app' => '' , 'key' => '' , 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$app = addslashes ( strip_tags ( $parameters [ 'app' ]));
$key = addslashes ( strip_tags ( $parameters [ 'key' ]));
OC_OCS :: privateDataGet ( $format , $app , $key );
2012-10-27 19:45:09 +04:00
})
2012-07-24 20:35:09 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-23 20:34:21 +04:00
// set - POST DATA
2012-07-24 20:35:09 +04:00
$router -> create ( 'privatedata_set' ,
'/privatedata/setattribute/{app}/{key}.{format}' )
-> post ()
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$app = addslashes ( strip_tags ( $parameters [ 'app' ]));
$key = addslashes ( strip_tags ( $parameters [ 'key' ]));
$value = OC_OCS :: readData ( 'post' , 'value' , 'text' );
OC_OCS :: privateDataSet ( $format , $app , $key , $value );
2012-10-27 19:45:09 +04:00
})
2012-07-24 20:35:09 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-23 20:34:21 +04:00
// delete - POST DATA
2012-07-24 20:35:09 +04:00
$router -> create ( 'privatedata_delete' ,
'/privatedata/deleteattribute/{app}/{key}.{format}' )
-> post ()
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$app = addslashes ( strip_tags ( $parameters [ 'app' ]));
$key = addslashes ( strip_tags ( $parameters [ 'key' ]));
OC_OCS :: privateDataDelete ( $format , $app , $key );
2012-10-27 19:45:09 +04:00
})
2012-07-24 20:35:09 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-23 20:34:21 +04:00
// CLOUD
2012-08-29 10:38:33 +04:00
// systemWebApps
2012-07-25 19:59:50 +04:00
$router -> create ( 'system_webapps' ,
'/cloud/system/webapps.{format}' )
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
OC_OCS :: systemwebapps ( $format );
2012-10-27 19:45:09 +04:00
})
2012-07-25 19:59:50 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-24 17:18:52 +04:00
2012-08-29 10:38:33 +04:00
// quotaget
2012-07-24 20:35:09 +04:00
$router -> create ( 'quota_get' ,
'/cloud/user/{user}.{format}' )
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$user = $parameters [ 'user' ];
OC_OCS :: quotaGet ( $format , $user );
2012-10-27 19:45:09 +04:00
})
2012-07-24 20:35:09 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-08-29 10:38:33 +04:00
// quotaset
2012-07-24 20:35:09 +04:00
$router -> create ( 'quota_set' ,
'/cloud/user/{user}.{format}' )
-> post ()
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$user = $parameters [ 'user' ];
$quota = self :: readData ( 'post' , 'quota' , 'int' );
OC_OCS :: quotaSet ( $format , $user , $quota );
2012-10-27 19:45:09 +04:00
})
2012-07-24 20:35:09 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-23 20:34:21 +04:00
2012-08-29 10:38:33 +04:00
// keygetpublic
2012-07-25 19:59:50 +04:00
$router -> create ( 'keygetpublic' ,
'/cloud/user/{user}/publickey.{format}' )
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$user = $parameters [ 'user' ];
2012-10-27 19:45:09 +04:00
OC_OCS :: publicKeyGet ( $format , $user );
})
2012-07-25 19:59:50 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-24 17:18:52 +04:00
2012-08-29 10:38:33 +04:00
// keygetprivate
2012-07-25 19:59:50 +04:00
$router -> create ( 'keygetpublic' ,
'/cloud/user/{user}/privatekey.{format}' )
-> defaults ( array ( 'format' => $format ))
-> action ( function ( $parameters ) {
$format = $parameters [ 'format' ];
$user = $parameters [ 'user' ];
2012-10-27 19:45:09 +04:00
OC_OCS :: privateKeyGet ( $format , $user );
})
2012-07-25 19:59:50 +04:00
-> requirements ( array ( 'format' => 'xml|json' ));
2012-07-24 17:18:52 +04:00
2012-07-23 20:34:21 +04:00
// 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
2012-07-23 21:43:28 +04:00
// TODO:
// users
// groups
// bookmarks
// sharing
// versioning
// news (rss)
2012-07-24 20:35:09 +04:00
try {
$router -> match ( $_SERVER [ 'PATH_INFO' ]);
} catch ( ResourceNotFoundException $e ) {
2012-10-27 19:45:09 +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-23 20:34:21 +04:00
$txt .= OC_OCS :: getdebugoutput ();
2012-09-10 15:59:08 +04:00
echo ( OC_OCS :: generatexml ( $format , 'failed' , 999 , $txt ));
2012-08-11 02:04:43 +04:00
} catch ( MethodNotAllowedException $e ) {
OC_Response :: setStatus ( 405 );
2012-07-23 20:34:21 +04:00
}
exit ();
}
/**
* generated some debug information to make it easier to find faild API calls
* @ return debug data string
*/
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 );
}
/**
* checks if the user is authenticated
* checks the IP whitlist , apikeys and login / password combination
* if $forceuser is true and the authentication failed it returns an 401 http response .
* if $forceuser is false and authentification fails it returns an empty username string
* @ param bool $forceuser
* @ return username string
*/
private static function checkPassword ( $forceuser = true ) {
//valid user account ?
if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) $authuser = $_SERVER [ 'PHP_AUTH_USER' ]; else $authuser = '' ;
if ( isset ( $_SERVER [ 'PHP_AUTH_PW' ])) $authpw = $_SERVER [ 'PHP_AUTH_PW' ]; else $authpw = '' ;
if ( empty ( $authuser )) {
2012-09-07 17:22:01 +04:00
if ( $forceuser ) {
2012-07-23 20:34:21 +04:00
header ( 'WWW-Authenticate: Basic realm="your valid user account or api key"' );
header ( 'HTTP/1.0 401 Unauthorized' );
exit ;
} else {
$identifieduser = '' ;
}
} else {
2012-09-10 15:59:08 +04:00
if ( ! OC_User :: login ( $authuser , $authpw )) {
2012-09-07 17:22:01 +04:00
if ( $forceuser ) {
2012-07-23 20:34:21 +04:00
header ( 'WWW-Authenticate: Basic realm="your valid user account or api key"' );
header ( 'HTTP/1.0 401 Unauthorized' );
exit ;
} else {
$identifieduser = '' ;
}
} else {
$identifieduser = $authuser ;
}
}
return ( $identifieduser );
}
/**
* 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 $itemscount
* @ param int $itemsperpage
* @ return string xml / json
*/
2012-10-27 19:45:09 +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 );
}
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 );
}
}
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
}
}
}
/**
* return the config data of this server
* @ param string $format
* @ return string xml / json
*/
2012-07-24 20:35:09 +04:00
public static function apiConfig ( $parameters ) {
$format = $parameters [ 'format' ];
2012-07-23 20:34:21 +04:00
$user = OC_OCS :: checkpassword ( false );
2012-09-10 15:59:08 +04:00
$url = substr ( OCP\Util :: getServerHost () . $_SERVER [ 'SCRIPT_NAME' ], 0 , - 11 ) . '' ;
2012-07-23 20:34:21 +04:00
$xml [ 'version' ] = '1.7' ;
$xml [ 'website' ] = 'ownCloud' ;
$xml [ 'host' ] = OCP\Util :: getServerHost ();
$xml [ 'contact' ] = '' ;
$xml [ 'ssl' ] = 'false' ;
2012-09-10 15:59:08 +04:00
echo ( OC_OCS :: generatexml ( $format , 'ok' , 100 , '' , $xml , 'config' , '' , 1 ));
2012-07-23 20:34:21 +04:00
}
/**
* check if the provided login / apikey / password is valid
* @ param string $format
* @ param string $login
* @ param string $passwd
* @ return string xml / json
*/
2012-10-27 19:45:09 +04:00
private static function personCheck ( $format , $login , $passwd ) {
2012-09-07 17:22:01 +04:00
if ( $login <> '' ) {
2012-09-10 15:59:08 +04:00
if ( OC_User :: login ( $login , $passwd )) {
2012-07-23 20:34:21 +04:00
$xml [ 'person' ][ 'personid' ] = $login ;
2012-09-10 15:59:08 +04:00
echo ( OC_OCS :: generatexml ( $format , 'ok' , 100 , '' , $xml , 'person' , 'check' , 2 ));
2012-07-23 20:34:21 +04:00
} else {
2012-09-10 15:59:08 +04:00
echo ( OC_OCS :: generatexml ( $format , 'failed' , 102 , 'login not valid' ));
2012-07-23 20:34:21 +04:00
}
} else {
2012-09-10 15:59:08 +04:00
echo ( OC_OCS :: generatexml ( $format , 'failed' , 101 , 'please specify all mandatory fields' ));
2012-07-23 20:34:21 +04:00
}
}
// ACTIVITY API #############################################
/**
* get my activities
* @ param string $format
* @ param string $page
* @ param string $pagesize
* @ return string xml / json
*/
2012-09-10 15:59:08 +04:00
private static function activityGet ( $format , $page , $pagesize ) {
2012-07-23 20:34:21 +04:00
$user = OC_OCS :: checkpassword ();
//TODO
2012-10-27 19:45:09 +04:00
$txt = OC_OCS :: generatexml ( $format , 'ok' , 100 , '' , $xml , 'activity' , 'full' , 2 , $totalcount , $pagesize );
2012-07-23 20:34:21 +04:00
echo ( $txt );
}
/**
* submit a activity
* @ param string $format
* @ param string $message
* @ return string xml / json
*/
2012-10-27 19:45:09 +04:00
private static function activityPut ( $format , $message ) {
2012-07-23 20:34:21 +04:00
// not implemented in ownCloud
$user = OC_OCS :: checkpassword ();
2012-09-10 15:59:08 +04:00
echo ( OC_OCS :: generatexml ( $format , 'ok' , 100 , '' ));
2012-07-23 20:34:21 +04:00
}
// PRIVATEDATA API #############################################
/**
* get private data and create the xml for ocs
* @ param string $format
* @ param string $app
* @ param string $key
* @ return string xml / json
*/
2012-09-10 15:59:08 +04:00
private static function privateDataGet ( $format , $app = " " , $key = " " ) {
2012-07-23 20:34:21 +04:00
$user = OC_OCS :: checkpassword ();
2012-09-10 15:59:08 +04:00
$result = OC_OCS :: getData ( $user , $app , $key );
2012-07-23 20:34:21 +04:00
$xml = array ();
foreach ( $result as $i => $log ) {
$xml [ $i ][ 'key' ] = $log [ 'key' ];
$xml [ $i ][ 'app' ] = $log [ 'app' ];
$xml [ $i ][ 'value' ] = $log [ 'value' ];
}
$txt = OC_OCS :: generatexml ( $format , 'ok' , 100 , '' , $xml , 'privatedata' , 'full' , 2 , count ( $xml ), 0 ); //TODO: replace 'privatedata' with 'attribute' once a new libattice has been released that works with it
echo ( $txt );
}
/**
* set private data referenced by $key to $value and generate the xml for ocs
* @ param string $format
* @ param string $app
* @ param string $key
* @ param string $value
* @ return string xml / json
*/
2011-01-23 05:23:03 +03:00
private static function privateDataSet ( $format , $app , $key , $value ) {
2011-02-06 03:22:48 +03:00
$user = OC_OCS :: checkpassword ();
2012-09-10 15:59:08 +04:00
if ( OC_OCS :: setData ( $user , $app , $key , $value )) {
echo ( OC_OCS :: generatexml ( $format , 'ok' , 100 , '' ));
2011-02-06 03:22:48 +03:00
}
}
/**
* delete private data referenced by $key and generate the xml for ocs
* @ param string $format
* @ param string $app
* @ param string $key
* @ return string xml / json
*/
private static function privateDataDelete ( $format , $app , $key ) {
2012-09-07 17:22:01 +04:00
if ( $key == " " or $app == " " ) {
2011-02-06 03:22:48 +03:00
return ; //key and app are NOT optional here
}
$user = OC_OCS :: checkpassword ();
2012-09-10 15:59:08 +04:00
if ( OC_OCS :: deleteData ( $user , $app , $key )) {
echo ( OC_OCS :: generatexml ( $format , 'ok' , 100 , '' ));
2011-02-06 03:22:48 +03:00
}
}
2012-08-29 10:38:33 +04:00
2011-02-06 03:22:48 +03:00
/**
* get private data
* @ param string $user
* @ param string $app
* @ param string $key
2011-03-16 20:18:45 +03:00
* @ param bool $like use LIKE instead of = when comparing keys
2011-02-06 03:22:48 +03:00
* @ return array
*/
2012-09-10 15:59:08 +04:00
public static function getData ( $user , $app = " " , $key = " " ) {
2012-09-07 17:22:01 +04:00
if ( $app ) {
2011-07-28 22:23:58 +04:00
$apps = array ( $app );
} else {
2011-07-29 23:36:03 +04:00
$apps = OC_Preferences :: getApps ( $user );
2011-07-28 22:23:58 +04:00
}
2012-09-07 17:22:01 +04:00
if ( $key ) {
2011-07-28 22:23:58 +04:00
$keys = array ( $key );
2011-02-06 03:22:48 +03:00
} else {
2012-09-07 17:22:01 +04:00
foreach ( $apps as $app ) {
2012-09-10 15:59:08 +04:00
$keys = OC_Preferences :: getKeys ( $user , $app );
2011-07-28 22:23:58 +04:00
}
}
$result = array ();
2012-09-07 17:22:01 +04:00
foreach ( $apps as $app ) {
foreach ( $keys as $key ) {
2012-09-10 15:59:08 +04:00
$value = OC_Preferences :: getValue ( $user , $app , $key );
$result [] = array ( 'app' => $app , 'key' => $key , 'value' => $value );
2011-02-06 03:22:48 +03:00
}
}
return $result ;
}
/**
* set private data referenced by $key to $value
* @ param string $user
* @ param string $app
* @ param string $key
* @ param string $value
* @ return bool
*/
public static function setData ( $user , $app , $key , $value ) {
2012-09-10 15:59:08 +04:00
return OC_Preferences :: setValue ( $user , $app , $key , $value );
2011-01-23 05:23:03 +03:00
}
2010-04-14 18:58:52 +04:00
2011-02-06 03:22:48 +03:00
/**
* delete private data referenced by $key
* @ param string $user
* @ param string $app
* @ param string $key
* @ return string xml / json
*/
public static function deleteData ( $user , $app , $key ) {
2012-09-10 15:59:08 +04:00
return OC_Preferences :: deleteKey ( $user , $app , $key );
2011-03-16 20:18:45 +03:00
}
2012-07-23 20:34:21 +04:00
// CLOUD API #############################################
2012-07-24 17:18:52 +04:00
/**
* get a list of installed web apps
* @ param string $format
* @ return string xml / json
*/
private static function systemWebApps ( $format ) {
$login = OC_OCS :: checkpassword ();
$apps = OC_App :: getEnabledApps ();
$values = array ();
foreach ( $apps as $app ) {
$info = OC_App :: getAppInfo ( $app );
if ( isset ( $info [ 'standalone' ])) {
2012-10-27 19:45:09 +04:00
$newvalue = array ( 'name' => $info [ 'name' ], 'url' => OC_Helper :: linkToAbsolute ( $app , '' ), 'icon' => '' );
2012-07-24 17:18:52 +04:00
$values [] = $newvalue ;
}
}
$txt = OC_OCS :: generatexml ( $format , 'ok' , 100 , '' , $values , 'cloud' , '' , 2 , 0 , 0 );
echo ( $txt );
}
2012-07-23 20:34:21 +04:00
/**
2012-07-23 21:43:28 +04:00
* get the quota of a user
2012-07-23 20:34:21 +04:00
* @ param string $format
2012-07-23 21:43:28 +04:00
* @ param string $user
2012-07-23 20:34:21 +04:00
* @ return string xml / json
*/
2012-10-27 19:45:09 +04:00
private static function quotaGet ( $format , $user ) {
2012-07-23 21:43:28 +04:00
$login = OC_OCS :: checkpassword ();
if ( OC_Group :: inGroup ( $login , 'admin' ) or ( $login == $user )) {
2012-09-07 17:22:01 +04:00
if ( OC_User :: userExists ( $user )) {
2012-07-23 21:43:28 +04:00
// 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 ;
2012-07-24 17:18:52 +04:00
$txt = OC_OCS :: generatexml ( $format , 'ok' , 100 , '' , $xml , 'cloud' , '' , 1 , 0 , 0 );
2012-07-23 21:43:28 +04:00
echo ( $txt );
} else {
2012-07-24 17:18:52 +04:00
echo self :: generateXml ( '' , 'fail' , 300 , 'User does not exist' );
2012-07-23 21:43:28 +04:00
}
} else {
echo self :: generateXml ( '' , 'fail' , 300 , 'You don´ t have permission to access this ressource.' );
}
2012-07-23 20:34:21 +04:00
}
2012-07-23 21:43:28 +04:00
/**
* set the quota of a user
* @ param string $format
* @ param string $user
* @ param string $quota
* @ return string xml / json
*/
2012-10-27 19:45:09 +04:00
private static function quotaSet ( $format , $user , $quota ) {
2012-07-23 21:43:28 +04:00
$login = OC_OCS :: checkpassword ();
if ( OC_Group :: inGroup ( $login , 'admin' )) {
// todo
// not yet implemented
2012-07-24 17:18:52 +04:00
// add logic here
2012-07-23 21:43:28 +04:00
error_log ( 'OCS call: user:' . $user . ' quota:' . $quota );
$xml = array ();
2012-07-24 17:18:52 +04:00
$txt = OC_OCS :: generatexml ( $format , 'ok' , 100 , '' , $xml , 'cloud' , '' , 1 , 0 , 0 );
2012-07-23 21:43:28 +04:00
echo ( $txt );
} else {
echo self :: generateXml ( '' , 'fail' , 300 , 'You don´ t have permission to access this ressource.' );
}
}
2012-07-23 20:34:21 +04:00
2012-07-24 17:18:52 +04:00
/**
* get the public key of a user
* @ param string $format
* @ param string $user
* @ return string xml / json
*/
2012-10-27 19:45:09 +04:00
private static function publicKeyGet ( $format , $user ) {
2012-07-24 17:18:52 +04:00
$login = OC_OCS :: checkpassword ();
2012-09-07 17:22:01 +04:00
if ( OC_User :: userExists ( $user )) {
2012-07-24 17:18:52 +04:00
// calculate the disc space
$txt = 'this is the public key of ' . $user ;
echo ( $txt );
} else {
echo self :: generateXml ( '' , 'fail' , 300 , 'User does not exist' );
}
}
/**
* get the private key of a user
* @ param string $format
* @ param string $user
* @ return string xml / json
*/
2012-10-27 19:45:09 +04:00
private static function privateKeyGet ( $format , $user ) {
2012-07-24 17:18:52 +04:00
$login = OC_OCS :: checkpassword ();
if ( OC_Group :: inGroup ( $login , 'admin' ) or ( $login == $user )) {
2012-09-07 17:22:01 +04:00
if ( OC_User :: userExists ( $user )) {
2012-07-24 17:18:52 +04:00
// calculate the disc space
$txt = 'this is the private key of ' . $user ;
echo ( $txt );
} else {
echo self :: generateXml ( '' , 'fail' , 300 , 'User does not exist' );
}
} else {
echo self :: generateXml ( '' , 'fail' , 300 , 'You don´ t have permission to access this ressource.' );
}
}
2012-07-23 20:34:21 +04:00
2011-02-06 03:22:48 +03:00
}