CustomPropertiesBackend.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017, Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\DAV;
  26. use OCP\IDBConnection;
  27. use OCP\IUser;
  28. use Sabre\DAV\PropertyStorage\Backend\BackendInterface;
  29. use Sabre\DAV\PropFind;
  30. use Sabre\DAV\PropPatch;
  31. use Sabre\DAV\Tree;
  32. class CustomPropertiesBackend implements BackendInterface {
  33. /**
  34. * Ignored properties
  35. *
  36. * @var array
  37. */
  38. private $ignoredProperties = array(
  39. '{DAV:}getcontentlength',
  40. '{DAV:}getcontenttype',
  41. '{DAV:}getetag',
  42. '{DAV:}quota-used-bytes',
  43. '{DAV:}quota-available-bytes',
  44. '{http://owncloud.org/ns}permissions',
  45. '{http://owncloud.org/ns}downloadURL',
  46. '{http://owncloud.org/ns}dDC',
  47. '{http://owncloud.org/ns}size',
  48. '{http://nextcloud.org/ns}is-encrypted',
  49. );
  50. /**
  51. * @var Tree
  52. */
  53. private $tree;
  54. /**
  55. * @var IDBConnection
  56. */
  57. private $connection;
  58. /**
  59. * @var string
  60. */
  61. private $user;
  62. /**
  63. * Properties cache
  64. *
  65. * @var array
  66. */
  67. private $cache = [];
  68. /**
  69. * @param Tree $tree node tree
  70. * @param IDBConnection $connection database connection
  71. * @param IUser $user owner of the tree and properties
  72. */
  73. public function __construct(
  74. Tree $tree,
  75. IDBConnection $connection,
  76. IUser $user) {
  77. $this->tree = $tree;
  78. $this->connection = $connection;
  79. $this->user = $user->getUID();
  80. }
  81. /**
  82. * Fetches properties for a path.
  83. *
  84. * @param string $path
  85. * @param PropFind $propFind
  86. * @return void
  87. */
  88. public function propFind($path, PropFind $propFind) {
  89. $requestedProps = $propFind->get404Properties();
  90. // these might appear
  91. $requestedProps = array_diff(
  92. $requestedProps,
  93. $this->ignoredProperties
  94. );
  95. // substr of calendars/ => path is inside the CalDAV component
  96. // two '/' => this a calendar (no calendar-home nor calendar object)
  97. if (substr($path, 0, 10) === 'calendars/' && substr_count($path, '/') === 2) {
  98. $allRequestedProps = $propFind->getRequestedProperties();
  99. $customPropertiesForShares = [
  100. '{DAV:}displayname',
  101. '{urn:ietf:params:xml:ns:caldav}calendar-description',
  102. '{urn:ietf:params:xml:ns:caldav}calendar-timezone',
  103. '{http://apple.com/ns/ical/}calendar-order',
  104. '{http://apple.com/ns/ical/}calendar-color',
  105. '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp',
  106. ];
  107. foreach ($customPropertiesForShares as $customPropertyForShares) {
  108. if (in_array($customPropertyForShares, $allRequestedProps)) {
  109. $requestedProps[] = $customPropertyForShares;
  110. }
  111. }
  112. }
  113. if (empty($requestedProps)) {
  114. return;
  115. }
  116. $props = $this->getProperties($path, $requestedProps);
  117. foreach ($props as $propName => $propValue) {
  118. $propFind->set($propName, $propValue);
  119. }
  120. }
  121. /**
  122. * Updates properties for a path
  123. *
  124. * @param string $path
  125. * @param PropPatch $propPatch
  126. *
  127. * @return void
  128. */
  129. public function propPatch($path, PropPatch $propPatch) {
  130. $propPatch->handleRemaining(function($changedProps) use ($path) {
  131. return $this->updateProperties($path, $changedProps);
  132. });
  133. }
  134. /**
  135. * This method is called after a node is deleted.
  136. *
  137. * @param string $path path of node for which to delete properties
  138. */
  139. public function delete($path) {
  140. $statement = $this->connection->prepare(
  141. 'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
  142. );
  143. $statement->execute(array($this->user, $path));
  144. $statement->closeCursor();
  145. unset($this->cache[$path]);
  146. }
  147. /**
  148. * This method is called after a successful MOVE
  149. *
  150. * @param string $source
  151. * @param string $destination
  152. *
  153. * @return void
  154. */
  155. public function move($source, $destination) {
  156. $statement = $this->connection->prepare(
  157. 'UPDATE `*PREFIX*properties` SET `propertypath` = ?' .
  158. ' WHERE `userid` = ? AND `propertypath` = ?'
  159. );
  160. $statement->execute(array($destination, $this->user, $source));
  161. $statement->closeCursor();
  162. }
  163. /**
  164. * Returns a list of properties for this nodes.;
  165. * @param string $path
  166. * @param array $requestedProperties requested properties or empty array for "all"
  167. * @return array
  168. * @note The properties list is a list of propertynames the client
  169. * requested, encoded as xmlnamespace#tagName, for example:
  170. * http://www.example.org/namespace#author If the array is empty, all
  171. * properties should be returned
  172. */
  173. private function getProperties($path, array $requestedProperties) {
  174. if (isset($this->cache[$path])) {
  175. return $this->cache[$path];
  176. }
  177. // TODO: chunking if more than 1000 properties
  178. $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?';
  179. $whereValues = array($this->user, $path);
  180. $whereTypes = array(null, null);
  181. if (!empty($requestedProperties)) {
  182. // request only a subset
  183. $sql .= ' AND `propertyname` in (?)';
  184. $whereValues[] = $requestedProperties;
  185. $whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
  186. }
  187. $result = $this->connection->executeQuery(
  188. $sql,
  189. $whereValues,
  190. $whereTypes
  191. );
  192. $props = [];
  193. while ($row = $result->fetch()) {
  194. $props[$row['propertyname']] = $row['propertyvalue'];
  195. }
  196. $result->closeCursor();
  197. $this->cache[$path] = $props;
  198. return $props;
  199. }
  200. /**
  201. * Update properties
  202. *
  203. * @param string $path node for which to update properties
  204. * @param array $properties array of properties to update
  205. *
  206. * @return bool
  207. */
  208. private function updateProperties($path, $properties) {
  209. $deleteStatement = 'DELETE FROM `*PREFIX*properties`' .
  210. ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
  211. $insertStatement = 'INSERT INTO `*PREFIX*properties`' .
  212. ' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)';
  213. $updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' .
  214. ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
  215. // TODO: use "insert or update" strategy ?
  216. $existing = $this->getProperties($path, array());
  217. $this->connection->beginTransaction();
  218. foreach ($properties as $propertyName => $propertyValue) {
  219. // If it was null, we need to delete the property
  220. if (is_null($propertyValue)) {
  221. if (array_key_exists($propertyName, $existing)) {
  222. $this->connection->executeUpdate($deleteStatement,
  223. array(
  224. $this->user,
  225. $path,
  226. $propertyName
  227. )
  228. );
  229. }
  230. } else {
  231. if (!array_key_exists($propertyName, $existing)) {
  232. $this->connection->executeUpdate($insertStatement,
  233. array(
  234. $this->user,
  235. $path,
  236. $propertyName,
  237. $propertyValue
  238. )
  239. );
  240. } else {
  241. $this->connection->executeUpdate($updateStatement,
  242. array(
  243. $propertyValue,
  244. $this->user,
  245. $path,
  246. $propertyName
  247. )
  248. );
  249. }
  250. }
  251. }
  252. $this->connection->commit();
  253. unset($this->cache[$path]);
  254. return true;
  255. }
  256. }