ocs.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @author Michael Gapczynski
  7. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  8. * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  25. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  26. /**
  27. * Class to handle open collaboration services API requests
  28. *
  29. */
  30. class OC_OCS {
  31. /**
  32. * reads input date from get/post/cookies and converts the date to a special data-type
  33. *
  34. * @param string HTTP method to read the key from
  35. * @param string Parameter to read
  36. * @param string Variable type to format data
  37. * @param mixed Default value to return if the key is not found
  38. * @return mixed Data or if the key is not found and no default is set it will exit with a 400 Bad request
  39. */
  40. public static function readData($method, $key, $type = 'raw', $default = null) {
  41. if ($method == 'get') {
  42. if (isset($_GET[$key])) {
  43. $data = $_GET[$key];
  44. } else if (isset($default)) {
  45. return $default;
  46. } else {
  47. $data = false;
  48. }
  49. } else if ($method == 'post') {
  50. if (isset($_POST[$key])) {
  51. $data = $_POST[$key];
  52. } else if (isset($default)) {
  53. return $default;
  54. } else {
  55. $data = false;
  56. }
  57. }
  58. if ($data === false) {
  59. echo self::generateXml('', 'fail', 400, 'Bad request. Please provide a valid '.$key);
  60. exit();
  61. } else {
  62. // NOTE: Is the raw type necessary? It might be a little risky without sanitization
  63. if ($type == 'raw') return $data;
  64. elseif ($type == 'text') return OC_Util::sanitizeHTML($data);
  65. elseif ($type == 'int') return (int) $data;
  66. elseif ($type == 'float') return (float) $data;
  67. elseif ($type == 'array') return OC_Util::sanitizeHTML($data);
  68. else return OC_Util::sanitizeHTML($data);
  69. }
  70. }
  71. /**
  72. main function to handle the REST request
  73. **/
  74. public static function handle() {
  75. // overwrite the 404 error page returncode
  76. header("HTTP/1.0 200 OK");
  77. if($_SERVER['REQUEST_METHOD'] == 'GET') {
  78. $method='get';
  79. }elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
  80. $method='put';
  81. parse_str(file_get_contents("php://input"), $put_vars);
  82. }elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
  83. $method='post';
  84. }else{
  85. echo('internal server error: method not supported');
  86. exit();
  87. }
  88. $format = self::readData($method, 'format', 'text', '');
  89. $router = new OC_Router();
  90. $router->useCollection('root');
  91. // CONFIG
  92. $router->create('config', '/config.{format}')
  93. ->defaults(array('format' => $format))
  94. ->action('OC_OCS', 'apiConfig')
  95. ->requirements(array('format'=>'xml|json'));
  96. // PERSON
  97. $router->create('person_check', '/person/check.{format}')
  98. ->post()
  99. ->defaults(array('format' => $format))
  100. ->action(function ($parameters) {
  101. $format = $parameters['format'];
  102. $login = OC_OCS::readData('post', 'login', 'text');
  103. $passwd = OC_OCS::readData('post', 'password', 'text');
  104. OC_OCS::personCheck($format, $login, $passwd);
  105. })
  106. ->requirements(array('format'=>'xml|json'));
  107. // ACTIVITY
  108. // activityget - GET ACTIVITY page,pagesize als urlparameter
  109. $router->create('activity_get', '/activity.{format}')
  110. ->defaults(array('format' => $format))
  111. ->action(function ($parameters) {
  112. $format = $parameters['format'];
  113. $page = OC_OCS::readData('get', 'page', 'int', 0);
  114. $pagesize = OC_OCS::readData('get', 'pagesize', 'int', 10);
  115. if($pagesize<1 or $pagesize>100) $pagesize=10;
  116. OC_OCS::activityGet($format, $page, $pagesize);
  117. })
  118. ->requirements(array('format'=>'xml|json'));
  119. // activityput - POST ACTIVITY
  120. $router->create('activity_put', '/activity.{format}')
  121. ->post()
  122. ->defaults(array('format' => $format))
  123. ->action(function ($parameters) {
  124. $format = $parameters['format'];
  125. $message = OC_OCS::readData('post', 'message', 'text');
  126. OC_OCS::activityPut($format, $message);
  127. })
  128. ->requirements(array('format'=>'xml|json'));
  129. // PRIVATEDATA
  130. // get - GET DATA
  131. $router->create('privatedata_get',
  132. '/privatedata/getattribute/{app}/{key}.{format}')
  133. ->defaults(array('app' => '', 'key' => '', 'format' => $format))
  134. ->action(function ($parameters) {
  135. $format = $parameters['format'];
  136. $app = addslashes(strip_tags($parameters['app']));
  137. $key = addslashes(strip_tags($parameters['key']));
  138. OC_OCS::privateDataGet($format, $app, $key);
  139. })
  140. ->requirements(array('format'=>'xml|json'));
  141. // set - POST DATA
  142. $router->create('privatedata_set',
  143. '/privatedata/setattribute/{app}/{key}.{format}')
  144. ->post()
  145. ->defaults(array('format' => $format))
  146. ->action(function ($parameters) {
  147. $format = $parameters['format'];
  148. $app = addslashes(strip_tags($parameters['app']));
  149. $key = addslashes(strip_tags($parameters['key']));
  150. $value=OC_OCS::readData('post', 'value', 'text');
  151. OC_OCS::privateDataSet($format, $app, $key, $value);
  152. })
  153. ->requirements(array('format'=>'xml|json'));
  154. // delete - POST DATA
  155. $router->create('privatedata_delete',
  156. '/privatedata/deleteattribute/{app}/{key}.{format}')
  157. ->post()
  158. ->defaults(array('format' => $format))
  159. ->action(function ($parameters) {
  160. $format = $parameters['format'];
  161. $app = addslashes(strip_tags($parameters['app']));
  162. $key = addslashes(strip_tags($parameters['key']));
  163. OC_OCS::privateDataDelete($format, $app, $key);
  164. })
  165. ->requirements(array('format'=>'xml|json'));
  166. // CLOUD
  167. // systemWebApps
  168. $router->create('system_webapps',
  169. '/cloud/system/webapps.{format}')
  170. ->defaults(array('format' => $format))
  171. ->action(function ($parameters) {
  172. $format = $parameters['format'];
  173. OC_OCS::systemwebapps($format);
  174. })
  175. ->requirements(array('format'=>'xml|json'));
  176. // quotaget
  177. $router->create('quota_get',
  178. '/cloud/user/{user}.{format}')
  179. ->defaults(array('format' => $format))
  180. ->action(function ($parameters) {
  181. $format = $parameters['format'];
  182. $user = $parameters['user'];
  183. OC_OCS::quotaGet($format, $user);
  184. })
  185. ->requirements(array('format'=>'xml|json'));
  186. // quotaset
  187. $router->create('quota_set',
  188. '/cloud/user/{user}.{format}')
  189. ->post()
  190. ->defaults(array('format' => $format))
  191. ->action(function ($parameters) {
  192. $format = $parameters['format'];
  193. $user = $parameters['user'];
  194. $quota = self::readData('post', 'quota', 'int');
  195. OC_OCS::quotaSet($format, $user, $quota);
  196. })
  197. ->requirements(array('format'=>'xml|json'));
  198. // keygetpublic
  199. $router->create('keygetpublic',
  200. '/cloud/user/{user}/publickey.{format}')
  201. ->defaults(array('format' => $format))
  202. ->action(function ($parameters) {
  203. $format = $parameters['format'];
  204. $user = $parameters['user'];
  205. OC_OCS::publicKeyGet($format, $user);
  206. })
  207. ->requirements(array('format'=>'xml|json'));
  208. // keygetprivate
  209. $router->create('keygetpublic',
  210. '/cloud/user/{user}/privatekey.{format}')
  211. ->defaults(array('format' => $format))
  212. ->action(function ($parameters) {
  213. $format = $parameters['format'];
  214. $user = $parameters['user'];
  215. OC_OCS::privateKeyGet($format, $user);
  216. })
  217. ->requirements(array('format'=>'xml|json'));
  218. // add more calls here
  219. // please document all the call in the draft spec
  220. // http://www.freedesktop.org/wiki/Specifications/open-collaboration-services-1.7#CLOUD
  221. // TODO:
  222. // users
  223. // groups
  224. // bookmarks
  225. // sharing
  226. // versioning
  227. // news (rss)
  228. try {
  229. $router->match($_SERVER['PATH_INFO']);
  230. } catch (ResourceNotFoundException $e) {
  231. $txt='Invalid query, please check the syntax. '
  232. .'API specifications are here: '
  233. .'http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.'
  234. .'DEBUG OUTPUT:'."\n";
  235. $txt.=OC_OCS::getdebugoutput();
  236. echo(OC_OCS::generatexml($format, 'failed', 999, $txt));
  237. } catch (MethodNotAllowedException $e) {
  238. OC_Response::setStatus(405);
  239. }
  240. exit();
  241. }
  242. /**
  243. * generated some debug information to make it easier to find faild API calls
  244. * @return debug data string
  245. */
  246. private static function getDebugOutput() {
  247. $txt='';
  248. $txt.="debug output:\n";
  249. if(isset($_SERVER['REQUEST_METHOD'])) $txt.='http request method: '.$_SERVER['REQUEST_METHOD']."\n";
  250. if(isset($_SERVER['REQUEST_URI'])) $txt.='http request uri: '.$_SERVER['REQUEST_URI']."\n";
  251. if(isset($_GET)) foreach($_GET as $key=>$value) $txt.='get parameter: '.$key.'->'.$value."\n";
  252. if(isset($_POST)) foreach($_POST as $key=>$value) $txt.='post parameter: '.$key.'->'.$value."\n";
  253. return($txt);
  254. }
  255. /**
  256. * checks if the user is authenticated
  257. * checks the IP whitlist, apikeys and login/password combination
  258. * if $forceuser is true and the authentication failed it returns an 401 http response.
  259. * if $forceuser is false and authentification fails it returns an empty username string
  260. * @param bool $forceuser
  261. * @return username string
  262. */
  263. private static function checkPassword($forceuser=true) {
  264. //valid user account ?
  265. if(isset($_SERVER['PHP_AUTH_USER'])) $authuser=$_SERVER['PHP_AUTH_USER']; else $authuser='';
  266. if(isset($_SERVER['PHP_AUTH_PW'])) $authpw=$_SERVER['PHP_AUTH_PW']; else $authpw='';
  267. if(empty($authuser)) {
  268. if($forceuser) {
  269. header('WWW-Authenticate: Basic realm="your valid user account or api key"');
  270. header('HTTP/1.0 401 Unauthorized');
  271. exit;
  272. }else{
  273. $identifieduser='';
  274. }
  275. }else{
  276. if(!OC_User::login($authuser, $authpw)) {
  277. if($forceuser) {
  278. header('WWW-Authenticate: Basic realm="your valid user account or api key"');
  279. header('HTTP/1.0 401 Unauthorized');
  280. exit;
  281. }else{
  282. $identifieduser='';
  283. }
  284. }else{
  285. $identifieduser=$authuser;
  286. }
  287. }
  288. return($identifieduser);
  289. }
  290. /**
  291. * generates the xml or json response for the API call from an multidimenional data array.
  292. * @param string $format
  293. * @param string $status
  294. * @param string $statuscode
  295. * @param string $message
  296. * @param array $data
  297. * @param string $tag
  298. * @param string $tagattribute
  299. * @param int $dimension
  300. * @param int $itemscount
  301. * @param int $itemsperpage
  302. * @return string xml/json
  303. */
  304. private static function generateXml($format, $status, $statuscode, $message, $data=array(), $tag='', $tagattribute='', $dimension=-1, $itemscount='', $itemsperpage='') {
  305. if($format=='json') {
  306. $json=array();
  307. $json['status']=$status;
  308. $json['statuscode']=$statuscode;
  309. $json['message']=$message;
  310. $json['totalitems']=$itemscount;
  311. $json['itemsperpage']=$itemsperpage;
  312. $json['data']=$data;
  313. return(json_encode($json));
  314. }else{
  315. $txt='';
  316. $writer = xmlwriter_open_memory();
  317. xmlwriter_set_indent( $writer, 2 );
  318. xmlwriter_start_document($writer );
  319. xmlwriter_start_element($writer, 'ocs');
  320. xmlwriter_start_element($writer, 'meta');
  321. xmlwriter_write_element($writer, 'status', $status);
  322. xmlwriter_write_element($writer, 'statuscode', $statuscode);
  323. xmlwriter_write_element($writer, 'message', $message);
  324. if($itemscount<>'') xmlwriter_write_element($writer, 'totalitems', $itemscount);
  325. if(!empty($itemsperpage)) xmlwriter_write_element($writer, 'itemsperpage', $itemsperpage);
  326. xmlwriter_end_element($writer);
  327. if($dimension=='0') {
  328. // 0 dimensions
  329. xmlwriter_write_element($writer, 'data', $data);
  330. }elseif($dimension=='1') {
  331. xmlwriter_start_element($writer, 'data');
  332. foreach($data as $key=>$entry) {
  333. xmlwriter_write_element($writer, $key, $entry);
  334. }
  335. xmlwriter_end_element($writer);
  336. }elseif($dimension=='2') {
  337. xmlwriter_start_element($writer, 'data');
  338. foreach($data as $entry) {
  339. xmlwriter_start_element($writer, $tag);
  340. if(!empty($tagattribute)) {
  341. xmlwriter_write_attribute($writer, 'details', $tagattribute);
  342. }
  343. foreach($entry as $key=>$value) {
  344. if(is_array($value)) {
  345. foreach($value as $k=>$v) {
  346. xmlwriter_write_element($writer, $k, $v);
  347. }
  348. } else {
  349. xmlwriter_write_element($writer, $key, $value);
  350. }
  351. }
  352. xmlwriter_end_element($writer);
  353. }
  354. xmlwriter_end_element($writer);
  355. }elseif($dimension=='3') {
  356. xmlwriter_start_element($writer, 'data');
  357. foreach($data as $entrykey=>$entry) {
  358. xmlwriter_start_element($writer, $tag);
  359. if(!empty($tagattribute)) {
  360. xmlwriter_write_attribute($writer, 'details', $tagattribute);
  361. }
  362. foreach($entry as $key=>$value) {
  363. if(is_array($value)) {
  364. xmlwriter_start_element($writer, $entrykey);
  365. foreach($value as $k=>$v) {
  366. xmlwriter_write_element($writer, $k, $v);
  367. }
  368. xmlwriter_end_element($writer);
  369. } else {
  370. xmlwriter_write_element($writer, $key, $value);
  371. }
  372. }
  373. xmlwriter_end_element($writer);
  374. }
  375. xmlwriter_end_element($writer);
  376. }elseif($dimension=='dynamic') {
  377. xmlwriter_start_element($writer, 'data');
  378. OC_OCS::toxml($writer, $data, 'comment');
  379. xmlwriter_end_element($writer);
  380. }
  381. xmlwriter_end_element($writer);
  382. xmlwriter_end_document( $writer );
  383. $txt.=xmlwriter_output_memory( $writer );
  384. unset($writer);
  385. return($txt);
  386. }
  387. }
  388. public static function toXml($writer, $data, $node) {
  389. foreach($data as $key => $value) {
  390. if (is_numeric($key)) {
  391. $key = $node;
  392. }
  393. if (is_array($value)) {
  394. xmlwriter_start_element($writer, $key);
  395. OC_OCS::toxml($writer, $value, $node);
  396. xmlwriter_end_element($writer);
  397. }else{
  398. xmlwriter_write_element($writer, $key, $value);
  399. }
  400. }
  401. }
  402. /**
  403. * return the config data of this server
  404. * @param string $format
  405. * @return string xml/json
  406. */
  407. public static function apiConfig($parameters) {
  408. $format = $parameters['format'];
  409. $user=OC_OCS::checkpassword(false);
  410. $url=substr(OCP\Util::getServerHost().$_SERVER['SCRIPT_NAME'], 0, -11).'';
  411. $xml['version']='1.7';
  412. $xml['website']='ownCloud';
  413. $xml['host']=OCP\Util::getServerHost();
  414. $xml['contact']='';
  415. $xml['ssl']='false';
  416. echo(OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'config', '', 1));
  417. }
  418. /**
  419. * check if the provided login/apikey/password is valid
  420. * @param string $format
  421. * @param string $login
  422. * @param string $passwd
  423. * @return string xml/json
  424. */
  425. private static function personCheck($format, $login, $passwd) {
  426. if($login<>'') {
  427. if(OC_User::login($login, $passwd)) {
  428. $xml['person']['personid']=$login;
  429. echo(OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'person', 'check', 2));
  430. }else{
  431. echo(OC_OCS::generatexml($format, 'failed', 102, 'login not valid'));
  432. }
  433. }else{
  434. echo(OC_OCS::generatexml($format, 'failed', 101, 'please specify all mandatory fields'));
  435. }
  436. }
  437. // ACTIVITY API #############################################
  438. /**
  439. * get my activities
  440. * @param string $format
  441. * @param string $page
  442. * @param string $pagesize
  443. * @return string xml/json
  444. */
  445. private static function activityGet($format, $page, $pagesize) {
  446. $user=OC_OCS::checkpassword();
  447. //TODO
  448. $txt=OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'activity', 'full', 2, $totalcount, $pagesize);
  449. echo($txt);
  450. }
  451. /**
  452. * submit a activity
  453. * @param string $format
  454. * @param string $message
  455. * @return string xml/json
  456. */
  457. private static function activityPut($format, $message) {
  458. // not implemented in ownCloud
  459. $user=OC_OCS::checkpassword();
  460. echo(OC_OCS::generatexml($format, 'ok', 100, ''));
  461. }
  462. // PRIVATEDATA API #############################################
  463. /**
  464. * get private data and create the xml for ocs
  465. * @param string $format
  466. * @param string $app
  467. * @param string $key
  468. * @return string xml/json
  469. */
  470. private static function privateDataGet($format, $app="", $key="") {
  471. $user=OC_OCS::checkpassword();
  472. $result=OC_OCS::getData($user, $app, $key);
  473. $xml=array();
  474. foreach($result as $i=>$log) {
  475. $xml[$i]['key']=$log['key'];
  476. $xml[$i]['app']=$log['app'];
  477. $xml[$i]['value']=$log['value'];
  478. }
  479. $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
  480. echo($txt);
  481. }
  482. /**
  483. * set private data referenced by $key to $value and generate the xml for ocs
  484. * @param string $format
  485. * @param string $app
  486. * @param string $key
  487. * @param string $value
  488. * @return string xml/json
  489. */
  490. private static function privateDataSet($format, $app, $key, $value) {
  491. $user=OC_OCS::checkpassword();
  492. if(OC_OCS::setData($user, $app, $key, $value)) {
  493. echo(OC_OCS::generatexml($format, 'ok', 100, ''));
  494. }
  495. }
  496. /**
  497. * delete private data referenced by $key and generate the xml for ocs
  498. * @param string $format
  499. * @param string $app
  500. * @param string $key
  501. * @return string xml/json
  502. */
  503. private static function privateDataDelete($format, $app, $key) {
  504. if($key=="" or $app=="") {
  505. return; //key and app are NOT optional here
  506. }
  507. $user=OC_OCS::checkpassword();
  508. if(OC_OCS::deleteData($user, $app, $key)) {
  509. echo(OC_OCS::generatexml($format, 'ok', 100, ''));
  510. }
  511. }
  512. /**
  513. * get private data
  514. * @param string $user
  515. * @param string $app
  516. * @param string $key
  517. * @param bool $like use LIKE instead of = when comparing keys
  518. * @return array
  519. */
  520. public static function getData($user, $app="", $key="") {
  521. if($app) {
  522. $apps=array($app);
  523. }else{
  524. $apps=OC_Preferences::getApps($user);
  525. }
  526. if($key) {
  527. $keys=array($key);
  528. }else{
  529. foreach($apps as $app) {
  530. $keys=OC_Preferences::getKeys($user, $app);
  531. }
  532. }
  533. $result=array();
  534. foreach($apps as $app) {
  535. foreach($keys as $key) {
  536. $value=OC_Preferences::getValue($user, $app, $key);
  537. $result[]=array('app'=>$app, 'key'=>$key, 'value'=>$value);
  538. }
  539. }
  540. return $result;
  541. }
  542. /**
  543. * set private data referenced by $key to $value
  544. * @param string $user
  545. * @param string $app
  546. * @param string $key
  547. * @param string $value
  548. * @return bool
  549. */
  550. public static function setData($user, $app, $key, $value) {
  551. return OC_Preferences::setValue($user, $app, $key, $value);
  552. }
  553. /**
  554. * delete private data referenced by $key
  555. * @param string $user
  556. * @param string $app
  557. * @param string $key
  558. * @return string xml/json
  559. */
  560. public static function deleteData($user, $app, $key) {
  561. return OC_Preferences::deleteKey($user, $app, $key);
  562. }
  563. // CLOUD API #############################################
  564. /**
  565. * get a list of installed web apps
  566. * @param string $format
  567. * @return string xml/json
  568. */
  569. private static function systemWebApps($format) {
  570. $login=OC_OCS::checkpassword();
  571. $apps=OC_App::getEnabledApps();
  572. $values=array();
  573. foreach($apps as $app) {
  574. $info=OC_App::getAppInfo($app);
  575. if(isset($info['standalone'])) {
  576. $newvalue=array('name'=>$info['name'], 'url'=>OC_Helper::linkToAbsolute($app, ''), 'icon'=>'');
  577. $values[]=$newvalue;
  578. }
  579. }
  580. $txt=OC_OCS::generatexml($format, 'ok', 100, '', $values, 'cloud', '', 2, 0, 0);
  581. echo($txt);
  582. }
  583. /**
  584. * get the quota of a user
  585. * @param string $format
  586. * @param string $user
  587. * @return string xml/json
  588. */
  589. private static function quotaGet($format, $user) {
  590. $login=OC_OCS::checkpassword();
  591. if(OC_Group::inGroup($login, 'admin') or ($login==$user)) {
  592. if(OC_User::userExists($user)) {
  593. // calculate the disc space
  594. $user_dir = '/'.$user.'/files';
  595. OC_Filesystem::init($user_dir);
  596. $rootInfo=OC_FileCache::get('');
  597. $sharedInfo=OC_FileCache::get('/Shared');
  598. $used=$rootInfo['size']-$sharedInfo['size'];
  599. $free=OC_Filesystem::free_space();
  600. $total=$free+$used;
  601. if($total==0) $total=1; // prevent division by zero
  602. $relative=round(($used/$total)*10000)/100;
  603. $xml=array();
  604. $xml['quota']=$total;
  605. $xml['free']=$free;
  606. $xml['used']=$used;
  607. $xml['relative']=$relative;
  608. $txt=OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'cloud', '', 1, 0, 0);
  609. echo($txt);
  610. }else{
  611. echo self::generateXml('', 'fail', 300, 'User does not exist');
  612. }
  613. }else{
  614. echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
  615. }
  616. }
  617. /**
  618. * set the quota of a user
  619. * @param string $format
  620. * @param string $user
  621. * @param string $quota
  622. * @return string xml/json
  623. */
  624. private static function quotaSet($format, $user, $quota) {
  625. $login=OC_OCS::checkpassword();
  626. if(OC_Group::inGroup($login, 'admin')) {
  627. // todo
  628. // not yet implemented
  629. // add logic here
  630. error_log('OCS call: user:'.$user.' quota:'.$quota);
  631. $xml=array();
  632. $txt=OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'cloud', '', 1, 0, 0);
  633. echo($txt);
  634. }else{
  635. echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
  636. }
  637. }
  638. /**
  639. * get the public key of a user
  640. * @param string $format
  641. * @param string $user
  642. * @return string xml/json
  643. */
  644. private static function publicKeyGet($format, $user) {
  645. $login=OC_OCS::checkpassword();
  646. if(OC_User::userExists($user)) {
  647. // calculate the disc space
  648. $txt='this is the public key of '.$user;
  649. echo($txt);
  650. }else{
  651. echo self::generateXml('', 'fail', 300, 'User does not exist');
  652. }
  653. }
  654. /**
  655. * get the private key of a user
  656. * @param string $format
  657. * @param string $user
  658. * @return string xml/json
  659. */
  660. private static function privateKeyGet($format, $user) {
  661. $login=OC_OCS::checkpassword();
  662. if(OC_Group::inGroup($login, 'admin') or ($login==$user)) {
  663. if(OC_User::userExists($user)) {
  664. // calculate the disc space
  665. $txt='this is the private key of '.$user;
  666. echo($txt);
  667. }else{
  668. echo self::generateXml('', 'fail', 300, 'User does not exist');
  669. }
  670. }else{
  671. echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
  672. }
  673. }
  674. }