custompropertiesbackend.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. * @author Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\Connector\Sabre;
  24. use OCP\IDBConnection;
  25. use OCP\IUser;
  26. use Sabre\DAV\PropertyStorage\Backend\BackendInterface;
  27. use Sabre\DAV\PropFind;
  28. use Sabre\DAV\PropPatch;
  29. use Sabre\DAV\Tree;
  30. use Sabre\DAV\Exception\NotFound;
  31. use Sabre\DAV\Exception\ServiceUnavailable;
  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. '{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. );
  50. /**
  51. * @var Tree
  52. */
  53. private $tree;
  54. /**
  55. * @var IDBConnection
  56. */
  57. private $connection;
  58. /**
  59. * @var IUser
  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. try {
  90. $node = $this->tree->getNodeForPath($path);
  91. if (!($node instanceof Node)) {
  92. return;
  93. }
  94. } catch (ServiceUnavailable $e) {
  95. // might happen for unavailable mount points, skip
  96. return;
  97. } catch (NotFound $e) {
  98. // in some rare (buggy) cases the node might not be found,
  99. // we catch the exception to prevent breaking the whole list with a 404
  100. // (soft fail)
  101. \OC::$server->getLogger()->warning(
  102. 'Could not get node for path: \"' . $path . '\" : ' . $e->getMessage(),
  103. array('app' => 'files')
  104. );
  105. return;
  106. }
  107. $requestedProps = $propFind->get404Properties();
  108. // these might appear
  109. $requestedProps = array_diff(
  110. $requestedProps,
  111. $this->ignoredProperties
  112. );
  113. if (empty($requestedProps)) {
  114. return;
  115. }
  116. if ($node instanceof Directory
  117. && $propFind->getDepth() !== 0
  118. ) {
  119. // note: pre-fetching only supported for depth <= 1
  120. $this->loadChildrenProperties($node, $requestedProps);
  121. }
  122. $props = $this->getProperties($node, $requestedProps);
  123. foreach ($props as $propName => $propValue) {
  124. $propFind->set($propName, $propValue);
  125. }
  126. }
  127. /**
  128. * Updates properties for a path
  129. *
  130. * @param string $path
  131. * @param PropPatch $propPatch
  132. *
  133. * @return void
  134. */
  135. public function propPatch($path, PropPatch $propPatch) {
  136. $node = $this->tree->getNodeForPath($path);
  137. if (!($node instanceof Node)) {
  138. return;
  139. }
  140. $propPatch->handleRemaining(function($changedProps) use ($node) {
  141. return $this->updateProperties($node, $changedProps);
  142. });
  143. }
  144. /**
  145. * This method is called after a node is deleted.
  146. *
  147. * @param string $path path of node for which to delete properties
  148. */
  149. public function delete($path) {
  150. $statement = $this->connection->prepare(
  151. 'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
  152. );
  153. $statement->execute(array($this->user, '/' . $path));
  154. $statement->closeCursor();
  155. unset($this->cache[$path]);
  156. }
  157. /**
  158. * This method is called after a successful MOVE
  159. *
  160. * @param string $source
  161. * @param string $destination
  162. *
  163. * @return void
  164. */
  165. public function move($source, $destination) {
  166. $statement = $this->connection->prepare(
  167. 'UPDATE `*PREFIX*properties` SET `propertypath` = ?' .
  168. ' WHERE `userid` = ? AND `propertypath` = ?'
  169. );
  170. $statement->execute(array('/' . $destination, $this->user, '/' . $source));
  171. $statement->closeCursor();
  172. }
  173. /**
  174. * Returns a list of properties for this nodes.;
  175. * @param Node $node
  176. * @param array $requestedProperties requested properties or empty array for "all"
  177. * @return array
  178. * @note The properties list is a list of propertynames the client
  179. * requested, encoded as xmlnamespace#tagName, for example:
  180. * http://www.example.org/namespace#author If the array is empty, all
  181. * properties should be returned
  182. */
  183. private function getProperties(Node $node, array $requestedProperties) {
  184. $path = $node->getPath();
  185. if (isset($this->cache[$path])) {
  186. return $this->cache[$path];
  187. }
  188. // TODO: chunking if more than 1000 properties
  189. $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?';
  190. $whereValues = array($this->user, $path);
  191. $whereTypes = array(null, null);
  192. if (!empty($requestedProperties)) {
  193. // request only a subset
  194. $sql .= ' AND `propertyname` in (?)';
  195. $whereValues[] = $requestedProperties;
  196. $whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
  197. }
  198. $result = $this->connection->executeQuery(
  199. $sql,
  200. $whereValues,
  201. $whereTypes
  202. );
  203. $props = [];
  204. while ($row = $result->fetch()) {
  205. $props[$row['propertyname']] = $row['propertyvalue'];
  206. }
  207. $result->closeCursor();
  208. $this->cache[$path] = $props;
  209. return $props;
  210. }
  211. /**
  212. * Update properties
  213. *
  214. * @param Node $node node for which to update properties
  215. * @param array $properties array of properties to update
  216. *
  217. * @return bool
  218. */
  219. private function updateProperties($node, $properties) {
  220. $path = $node->getPath();
  221. $deleteStatement = 'DELETE FROM `*PREFIX*properties`' .
  222. ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
  223. $insertStatement = 'INSERT INTO `*PREFIX*properties`' .
  224. ' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)';
  225. $updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' .
  226. ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?';
  227. // TODO: use "insert or update" strategy ?
  228. $existing = $this->getProperties($node, array());
  229. $this->connection->beginTransaction();
  230. foreach ($properties as $propertyName => $propertyValue) {
  231. // If it was null, we need to delete the property
  232. if (is_null($propertyValue)) {
  233. if (array_key_exists($propertyName, $existing)) {
  234. $this->connection->executeUpdate($deleteStatement,
  235. array(
  236. $this->user,
  237. $path,
  238. $propertyName
  239. )
  240. );
  241. }
  242. } else {
  243. if (!array_key_exists($propertyName, $existing)) {
  244. $this->connection->executeUpdate($insertStatement,
  245. array(
  246. $this->user,
  247. $path,
  248. $propertyName,
  249. $propertyValue
  250. )
  251. );
  252. } else {
  253. $this->connection->executeUpdate($updateStatement,
  254. array(
  255. $propertyValue,
  256. $this->user,
  257. $path,
  258. $propertyName
  259. )
  260. );
  261. }
  262. }
  263. }
  264. $this->connection->commit();
  265. unset($this->cache[$path]);
  266. return true;
  267. }
  268. /**
  269. * Bulk load properties for directory children
  270. *
  271. * @param Directory $node
  272. * @param array $requestedProperties requested properties
  273. *
  274. * @return void
  275. */
  276. private function loadChildrenProperties(Directory $node, $requestedProperties) {
  277. $path = $node->getPath();
  278. if (isset($this->cache[$path])) {
  279. // we already loaded them at some point
  280. return;
  281. }
  282. $childNodes = $node->getChildren();
  283. // pre-fill cache
  284. foreach ($childNodes as $childNode) {
  285. $this->cache[$childNode->getPath()] = [];
  286. }
  287. $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` LIKE ?';
  288. $sql .= ' AND `propertyname` in (?) ORDER BY `propertypath`, `propertyname`';
  289. $result = $this->connection->executeQuery(
  290. $sql,
  291. array($this->user, rtrim($path, '/') . '/%', $requestedProperties),
  292. array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
  293. );
  294. $oldPath = null;
  295. $props = [];
  296. while ($row = $result->fetch()) {
  297. $path = $row['propertypath'];
  298. if ($oldPath !== $path) {
  299. // save previously gathered props
  300. $this->cache[$oldPath] = $props;
  301. $oldPath = $path;
  302. // prepare props for next path
  303. $props = [];
  304. }
  305. $props[$row['propertyname']] = $row['propertyvalue'];
  306. }
  307. if (!is_null($oldPath)) {
  308. // save props from last run
  309. $this->cache[$oldPath] = $props;
  310. }
  311. $result->closeCursor();
  312. }
  313. }