PrivateData.php 3.8 KB

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