CustomPropertiesBackend.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 OCA\DAV\Connector\Sabre\Node;
  27. use OCP\IDBConnection;
  28. use OCP\IUser;
  29. use Sabre\DAV\PropertyStorage\Backend\BackendInterface;
  30. use Sabre\DAV\PropFind;
  31. use Sabre\DAV\PropPatch;
  32. use Sabre\DAV\Tree;
  33. class CustomPropertiesBackend implements BackendInterface {
  34. /**
  35. * Ignored properties
  36. *
  37. * @var array
  38. */
  39. private $ignoredProperties = [
  40. '{DAV:}getcontentlength',
  41. '{DAV:}getcontenttype',
  42. '{DAV:}getetag',
  43. '{DAV:}quota-used-bytes',
  44. '{DAV:}quota-available-bytes',
  45. '{http://owncloud.org/ns}permissions',
  46. '{http://owncloud.org/ns}downloadURL',
  47. '{http://owncloud.org/ns}dDC',
  48. '{http://owncloud.org/ns}size',
  49. '{http://nextcloud.org/ns}is-encrypted',
  50. ];
  51. /**
  52. * @var Tree
  53. */
  54. private $tree;
  55. /**
  56. * @var IDBConnection
  57. */
  58. private $connection;
  59. /**
  60. * @var IUser
  61. */
  62. private $user;
  63. /**
  64. * Properties cache
  65. *
  66. * @var array
  67. */
  68. private $cache = [];
  69. /**
  70. * @param Tree $tree node tree
  71. * @param IDBConnection $connection database connection
  72. * @param IUser $user owner of the tree and properties
  73. */
  74. public function __construct(
  75. Tree $tree,
  76. IDBConnection $connection,
  77. IUser $user) {
  78. $this->tree = $tree;
  79. $this->connection = $connection;
  80. $this->user = $user;
  81. }
  82. /**
  83. * Fetches properties for a path.
  84. *
  85. * @param string $path
  86. * @param PropFind $propFind
  87. * @return void
  88. */
  89. public function propFind($path, PropFind $propFind) {
  90. $requestedProps = $propFind->get404Properties();
  91. // these might appear
  92. $requestedProps = array_diff(
  93. $requestedProps,
  94. $this->ignoredProperties
  95. );
  96. // substr of calendars/ => path is inside the CalDAV component
  97. // two '/' => this a calendar (no calendar-home nor calendar object)
  98. if (substr($path, 0, 10) === 'calendars/' && substr_count($path, '/') === 2) {
  99. $allRequestedProps = $propFind->getRequestedProperties();
  100. $customPropertiesForShares = [
  101. '{DAV:}displayname',
  102. '{urn:ietf:params:xml:ns:caldav}calendar-description',
  103. '{urn:ietf:params:xml:ns:caldav}calendar-timezone',
  104. '{http://apple.com/ns/ical/}calendar-order',
  105. '{http://apple.com/ns/ical/}calendar-color',
  106. '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp',
  107. ];
  108. foreach ($customPropertiesForShares as $customPropertyForShares) {
  109. if (in_array($customPropertyForShares, $allRequestedProps)) {
  110. $requestedProps[] = $customPropertyForShares;
  111. }
  112. }
  113. }
  114. if (empty($requestedProps)) {
  115. return;
  116. }
  117. $props = $this->getProperties($path, $requestedProps);
  118. foreach ($props as $propName => $propValue) {
  119. $propFind->set($propName, $propValue);
  120. }
  121. }
  122. /**
  123. * Updates properties for a path
  124. *
  125. * @param string $path
  126. * @param PropPatch $propPatch
  127. *
  128. * @return void
  129. */
  130. public function propPatch($path, PropPatch $propPatch) {
  131. $propPatch->handleRemaining(function ($changedProps) use ($path) {
  132. return $this->updateProperties($path, $changedProps);
  133. });
  134. }
  135. /**
  136. * This method is called after a node is deleted.
  137. *
  138. * @param string $path path of node for which to delete properties
  139. */
  140. public function delete($path) {
  141. $statement = $this->connection->prepare(
  142. 'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
  143. );
  144. $statement->execute([$this->user->getUID(), $this->formatPath($path)]);
  145. $statement->closeCursor();
  146. unset($this->cache[$path]);
  147. }
  148. /**
  149. * This method is called after a successful MOVE
  150. *
  151. * @param string $source
  152. * @param string $destination
  153. *
  154. * @return void
  155. */
  156. public function move($source, $destination) {
  157. $statement = $this->connection->prepare(
  158. 'UPDATE `*PREFIX*properties` SET `propertypath` = ?' .
  159. ' WHERE `userid` = ? AND `propertypath` = ?'
  160. );
  161. $statement->execute([$this->formatPath($destination), $this->user->getUID(), $this->formatPath($source)]);
  162. $statement->closeCursor();
  163. }
  164. /**
  165. * Returns a list of properties for this nodes.;
  166. *
  167. * @param string $path
  168. * @param array $requestedProperties requested properties or empty array for "all"
  169. * @return array
  170. * @note The properties list is a list of propertynames the client
  171. * requested, encoded as xmlnamespace#tagName, for example:
  172. * http://www.example.org/namespace#author If the array is empty, all
  173. * properties should be returned
  174. */
  175. private function getProperties(string $path, array $requestedProperties) {
  176. if (isset($this->cache[$path])) {
  177. return $this->cache[$path];
  178. }
  179. // TODO: chunking if more than 1000 properties
  180. $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?';
  181. $whereValues = [$this->user->getUID(), $this->formatPath($path)];
  182. $whereTypes = [null, null];
  183. if (!empty($requestedProperties)) {
  184. // request only a subset
  185. $sql .= ' AND `propertyname` in (?)';
  186. $whereValues[] = $requestedProperties;
  187. $whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
  188. }
  189. $result = $this->connection->executeQuery(
  190. $sql,
  191. $whereValues,
  192. $whereTypes
  193. );
  194. $props = [];
  195. while ($row = $result->fetch()) {
  196. $props[$row['propertyname']] = $row['propertyvalue'];
  197. }
  198. $result->closeCursor();
  199. $this->cache[$path] = $props;
  200. return $props;
  201. }
  202. /**
  203. * Update properties
  204. *
  205. * @param string $path path for which to update properties
  206. * @param array $properties array of properties to update
  207. *
  208. * @return bool
  209. */
  210. private function updateProperties(string $path, array $properties) {
  211. $deleteStatement = 'DELETE FROM `*PREFIX*properties`' .
  212. ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
  213. $insertStatement = 'INSERT INTO `*PREFIX*properties`' .
  214. ' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)';
  215. $updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' .
  216. ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
  217. // TODO: use "insert or update" strategy ?
  218. $existing = $this->getProperties($path, []);
  219. $this->connection->beginTransaction();
  220. foreach ($properties as $propertyName => $propertyValue) {
  221. // If it was null, we need to delete the property
  222. if (is_null($propertyValue)) {
  223. if (array_key_exists($propertyName, $existing)) {
  224. $this->connection->executeUpdate($deleteStatement,
  225. [
  226. $this->user->getUID(),
  227. $this->formatPath($path),
  228. $propertyName,
  229. ]
  230. );
  231. }
  232. } else {
  233. if (!array_key_exists($propertyName, $existing)) {
  234. $this->connection->executeUpdate($insertStatement,
  235. [
  236. $this->user->getUID(),
  237. $this->formatPath($path),
  238. $propertyName,
  239. $propertyValue,
  240. ]
  241. );
  242. } else {
  243. $this->connection->executeUpdate($updateStatement,
  244. [
  245. $propertyValue,
  246. $this->user->getUID(),
  247. $this->formatPath($path),
  248. $propertyName,
  249. ]
  250. );
  251. }
  252. }
  253. }
  254. $this->connection->commit();
  255. unset($this->cache[$path]);
  256. return true;
  257. }
  258. /**
  259. * long paths are hashed to ensure they fit in the database
  260. *
  261. * @param string $path
  262. * @return string
  263. */
  264. private function formatPath(string $path): string {
  265. if (strlen($path) > 250) {
  266. return sha1($path);
  267. } else {
  268. return $path;
  269. }
  270. }
  271. }