privatedata.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Frank Karlitschek <frank@owncloud.org>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Tom Needham <tom@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. class OC_OCS_Privatedata {
  27. /**
  28. * read keys
  29. * test: curl http://login:passwd@oc/core/ocs/v1.php/privatedata/getattribute/testy/123
  30. * test: curl http://login:passwd@oc/core/ocs/v1.php/privatedata/getattribute/testy
  31. * @param array $parameters The OCS parameter
  32. * @return \OC_OCS_Result
  33. */
  34. public static function get($parameters) {
  35. $user = OC_User::getUser();
  36. $app = addslashes(strip_tags($parameters['app']));
  37. $key = isset($parameters['key']) ? addslashes(strip_tags($parameters['key'])) : null;
  38. if(empty($key)) {
  39. $query = \OCP\DB::prepare('SELECT `key`, `app`, `value` FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? ');
  40. $result = $query->execute(array($user, $app));
  41. } else {
  42. $query = \OCP\DB::prepare('SELECT `key`, `app`, `value` FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? AND `key` = ? ');
  43. $result = $query->execute(array($user, $app, $key));
  44. }
  45. $xml = array();
  46. while ($row = $result->fetchRow()) {
  47. $data=array();
  48. $data['key']=$row['key'];
  49. $data['app']=$row['app'];
  50. $data['value']=$row['value'];
  51. $xml[] = $data;
  52. }
  53. return new OC_OCS_Result($xml);
  54. }
  55. /**
  56. * set a key
  57. * test: curl http://login:passwd@oc/core/ocs/v1.php/privatedata/setattribute/testy/123 --data "value=foobar"
  58. * @param array $parameters The OCS parameter
  59. * @return \OC_OCS_Result
  60. */
  61. public static function set($parameters) {
  62. $user = OC_User::getUser();
  63. $app = addslashes(strip_tags($parameters['app']));
  64. $key = addslashes(strip_tags($parameters['key']));
  65. $value = (string)$_POST['value'];
  66. // update in DB
  67. $query = \OCP\DB::prepare('UPDATE `*PREFIX*privatedata` SET `value` = ? WHERE `user` = ? AND `app` = ? AND `key` = ?');
  68. $numRows = $query->execute(array($value, $user, $app, $key));
  69. if ($numRows === false || $numRows === 0) {
  70. // store in DB
  71. $query = \OCP\DB::prepare('INSERT INTO `*PREFIX*privatedata` (`user`, `app`, `key`, `value`)' . ' VALUES(?, ?, ?, ?)');
  72. $query->execute(array($user, $app, $key, $value));
  73. }
  74. return new OC_OCS_Result(null, 100);
  75. }
  76. /**
  77. * delete a key
  78. * test: curl http://login:passwd@oc/core/ocs/v1.php/privatedata/deleteattribute/testy/123 --data "post=1"
  79. * @param array $parameters The OCS parameter
  80. * @return \OC_OCS_Result
  81. */
  82. public static function delete($parameters) {
  83. $user = OC_User::getUser();
  84. if (!isset($parameters['app']) or !isset($parameters['key'])) {
  85. //key and app are NOT optional here
  86. return new OC_OCS_Result(null, 101);
  87. }
  88. $app = addslashes(strip_tags($parameters['app']));
  89. $key = addslashes(strip_tags($parameters['key']));
  90. // delete in DB
  91. $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? AND `key` = ? ');
  92. $query->execute(array($user, $app, $key ));
  93. return new OC_OCS_Result(null, 100);
  94. }
  95. }