CustomPropertiesBackend.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\DAV;
  8. use Exception;
  9. use OCA\DAV\CalDAV\Calendar;
  10. use OCA\DAV\CalDAV\DefaultCalendarValidator;
  11. use OCA\DAV\Connector\Sabre\Directory;
  12. use OCA\DAV\Connector\Sabre\FilesPlugin;
  13. use OCP\DB\QueryBuilder\IQueryBuilder;
  14. use OCP\IDBConnection;
  15. use OCP\IUser;
  16. use Sabre\DAV\Exception as DavException;
  17. use Sabre\DAV\PropertyStorage\Backend\BackendInterface;
  18. use Sabre\DAV\PropFind;
  19. use Sabre\DAV\PropPatch;
  20. use Sabre\DAV\Server;
  21. use Sabre\DAV\Tree;
  22. use Sabre\DAV\Xml\Property\Complex;
  23. use Sabre\DAV\Xml\Property\Href;
  24. use Sabre\DAV\Xml\Property\LocalHref;
  25. use Sabre\Xml\ParseException;
  26. use Sabre\Xml\Service as XmlService;
  27. use function array_intersect;
  28. class CustomPropertiesBackend implements BackendInterface {
  29. /** @var string */
  30. private const TABLE_NAME = 'properties';
  31. /**
  32. * Value is stored as string.
  33. */
  34. public const PROPERTY_TYPE_STRING = 1;
  35. /**
  36. * Value is stored as XML fragment.
  37. */
  38. public const PROPERTY_TYPE_XML = 2;
  39. /**
  40. * Value is stored as a property object.
  41. */
  42. public const PROPERTY_TYPE_OBJECT = 3;
  43. /**
  44. * Value is stored as a {DAV:}href string.
  45. */
  46. public const PROPERTY_TYPE_HREF = 4;
  47. /**
  48. * Ignored properties
  49. *
  50. * @var string[]
  51. */
  52. private const IGNORED_PROPERTIES = [
  53. '{DAV:}getcontentlength',
  54. '{DAV:}getcontenttype',
  55. '{DAV:}getetag',
  56. '{DAV:}quota-used-bytes',
  57. '{DAV:}quota-available-bytes',
  58. '{http://owncloud.org/ns}permissions',
  59. '{http://owncloud.org/ns}downloadURL',
  60. '{http://owncloud.org/ns}dDC',
  61. '{http://owncloud.org/ns}size',
  62. '{http://nextcloud.org/ns}is-encrypted',
  63. // Currently, returning null from any propfind handler would still trigger the backend,
  64. // so we add all known Nextcloud custom properties in here to avoid that
  65. // text app
  66. '{http://nextcloud.org/ns}rich-workspace',
  67. '{http://nextcloud.org/ns}rich-workspace-file',
  68. // groupfolders
  69. '{http://nextcloud.org/ns}acl-enabled',
  70. '{http://nextcloud.org/ns}acl-can-manage',
  71. '{http://nextcloud.org/ns}acl-list',
  72. '{http://nextcloud.org/ns}inherited-acl-list',
  73. '{http://nextcloud.org/ns}group-folder-id',
  74. // files_lock
  75. '{http://nextcloud.org/ns}lock',
  76. '{http://nextcloud.org/ns}lock-owner-type',
  77. '{http://nextcloud.org/ns}lock-owner',
  78. '{http://nextcloud.org/ns}lock-owner-displayname',
  79. '{http://nextcloud.org/ns}lock-owner-editor',
  80. '{http://nextcloud.org/ns}lock-time',
  81. '{http://nextcloud.org/ns}lock-timeout',
  82. '{http://nextcloud.org/ns}lock-token',
  83. ];
  84. /**
  85. * Properties set by one user, readable by all others
  86. *
  87. * @var string[]
  88. */
  89. private const PUBLISHED_READ_ONLY_PROPERTIES = [
  90. '{urn:ietf:params:xml:ns:caldav}calendar-availability',
  91. '{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL',
  92. ];
  93. /**
  94. * Map of custom XML elements to parse when trying to deserialize an instance of
  95. * \Sabre\DAV\Xml\Property\Complex to find a more specialized PROPERTY_TYPE_*
  96. */
  97. private const COMPLEX_XML_ELEMENT_MAP = [
  98. '{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL' => Href::class,
  99. ];
  100. /**
  101. * @var Tree
  102. */
  103. private $tree;
  104. /**
  105. * @var IDBConnection
  106. */
  107. private $connection;
  108. /**
  109. * @var IUser
  110. */
  111. private $user;
  112. /**
  113. * Properties cache
  114. *
  115. * @var array
  116. */
  117. private $userCache = [];
  118. private Server $server;
  119. private XmlService $xmlService;
  120. private DefaultCalendarValidator $defaultCalendarValidator;
  121. /**
  122. * @param Tree $tree node tree
  123. * @param IDBConnection $connection database connection
  124. * @param IUser $user owner of the tree and properties
  125. */
  126. public function __construct(
  127. Server $server,
  128. Tree $tree,
  129. IDBConnection $connection,
  130. IUser $user,
  131. DefaultCalendarValidator $defaultCalendarValidator,
  132. ) {
  133. $this->server = $server;
  134. $this->tree = $tree;
  135. $this->connection = $connection;
  136. $this->user = $user;
  137. $this->xmlService = new XmlService();
  138. $this->xmlService->elementMap = array_merge(
  139. $this->xmlService->elementMap,
  140. self::COMPLEX_XML_ELEMENT_MAP,
  141. );
  142. $this->defaultCalendarValidator = $defaultCalendarValidator;
  143. }
  144. /**
  145. * Fetches properties for a path.
  146. *
  147. * @param string $path
  148. * @param PropFind $propFind
  149. * @return void
  150. */
  151. public function propFind($path, PropFind $propFind) {
  152. $requestedProps = $propFind->get404Properties();
  153. // these might appear
  154. $requestedProps = array_diff(
  155. $requestedProps,
  156. self::IGNORED_PROPERTIES,
  157. );
  158. $requestedProps = array_filter(
  159. $requestedProps,
  160. fn ($prop) => !str_starts_with($prop, FilesPlugin::FILE_METADATA_PREFIX),
  161. );
  162. // substr of calendars/ => path is inside the CalDAV component
  163. // two '/' => this a calendar (no calendar-home nor calendar object)
  164. if (str_starts_with($path, 'calendars/') && substr_count($path, '/') === 2) {
  165. $allRequestedProps = $propFind->getRequestedProperties();
  166. $customPropertiesForShares = [
  167. '{DAV:}displayname',
  168. '{urn:ietf:params:xml:ns:caldav}calendar-description',
  169. '{urn:ietf:params:xml:ns:caldav}calendar-timezone',
  170. '{http://apple.com/ns/ical/}calendar-order',
  171. '{http://apple.com/ns/ical/}calendar-color',
  172. '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp',
  173. ];
  174. foreach ($customPropertiesForShares as $customPropertyForShares) {
  175. if (in_array($customPropertyForShares, $allRequestedProps)) {
  176. $requestedProps[] = $customPropertyForShares;
  177. }
  178. }
  179. }
  180. // substr of addressbooks/ => path is inside the CardDAV component
  181. // three '/' => this a addressbook (no addressbook-home nor contact object)
  182. if (str_starts_with($path, 'addressbooks/') && substr_count($path, '/') === 3) {
  183. $allRequestedProps = $propFind->getRequestedProperties();
  184. $customPropertiesForShares = [
  185. '{DAV:}displayname',
  186. ];
  187. foreach ($customPropertiesForShares as $customPropertyForShares) {
  188. if (in_array($customPropertyForShares, $allRequestedProps, true)) {
  189. $requestedProps[] = $customPropertyForShares;
  190. }
  191. }
  192. }
  193. // substr of principals/users/ => path is a user principal
  194. // two '/' => this a principal collection (and not some child object)
  195. if (str_starts_with($path, 'principals/users/') && substr_count($path, '/') === 2) {
  196. $allRequestedProps = $propFind->getRequestedProperties();
  197. $customProperties = [
  198. '{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL',
  199. ];
  200. foreach ($customProperties as $customProperty) {
  201. if (in_array($customProperty, $allRequestedProps, true)) {
  202. $requestedProps[] = $customProperty;
  203. }
  204. }
  205. }
  206. if (empty($requestedProps)) {
  207. return;
  208. }
  209. $node = $this->tree->getNodeForPath($path);
  210. if ($node instanceof Directory && $propFind->getDepth() !== 0) {
  211. $this->cacheDirectory($path, $node);
  212. }
  213. // First fetch the published properties (set by another user), then get the ones set by
  214. // the current user. If both are set then the latter as priority.
  215. foreach ($this->getPublishedProperties($path, $requestedProps) as $propName => $propValue) {
  216. try {
  217. $this->validateProperty($path, $propName, $propValue);
  218. } catch (DavException $e) {
  219. continue;
  220. }
  221. $propFind->set($propName, $propValue);
  222. }
  223. foreach ($this->getUserProperties($path, $requestedProps) as $propName => $propValue) {
  224. try {
  225. $this->validateProperty($path, $propName, $propValue);
  226. } catch (DavException $e) {
  227. continue;
  228. }
  229. $propFind->set($propName, $propValue);
  230. }
  231. }
  232. /**
  233. * Updates properties for a path
  234. *
  235. * @param string $path
  236. * @param PropPatch $propPatch
  237. *
  238. * @return void
  239. */
  240. public function propPatch($path, PropPatch $propPatch) {
  241. $propPatch->handleRemaining(function ($changedProps) use ($path) {
  242. return $this->updateProperties($path, $changedProps);
  243. });
  244. }
  245. /**
  246. * This method is called after a node is deleted.
  247. *
  248. * @param string $path path of node for which to delete properties
  249. */
  250. public function delete($path) {
  251. $statement = $this->connection->prepare(
  252. 'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'
  253. );
  254. $statement->execute([$this->user->getUID(), $this->formatPath($path)]);
  255. $statement->closeCursor();
  256. unset($this->userCache[$path]);
  257. }
  258. /**
  259. * This method is called after a successful MOVE
  260. *
  261. * @param string $source
  262. * @param string $destination
  263. *
  264. * @return void
  265. */
  266. public function move($source, $destination) {
  267. $statement = $this->connection->prepare(
  268. 'UPDATE `*PREFIX*properties` SET `propertypath` = ?' .
  269. ' WHERE `userid` = ? AND `propertypath` = ?'
  270. );
  271. $statement->execute([$this->formatPath($destination), $this->user->getUID(), $this->formatPath($source)]);
  272. $statement->closeCursor();
  273. }
  274. /**
  275. * Validate the value of a property. Will throw if a value is invalid.
  276. *
  277. * @throws DavException The value of the property is invalid
  278. */
  279. private function validateProperty(string $path, string $propName, mixed $propValue): void {
  280. switch ($propName) {
  281. case '{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL':
  282. /** @var Href $propValue */
  283. $href = $propValue->getHref();
  284. if ($href === null) {
  285. throw new DavException('Href is empty');
  286. }
  287. // $path is the principal here as this prop is only set on principals
  288. $node = $this->tree->getNodeForPath($href);
  289. if (!($node instanceof Calendar) || $node->getOwner() !== $path) {
  290. throw new DavException('No such calendar');
  291. }
  292. $this->defaultCalendarValidator->validateScheduleDefaultCalendar($node);
  293. break;
  294. }
  295. }
  296. /**
  297. * @param string $path
  298. * @param string[] $requestedProperties
  299. *
  300. * @return array
  301. */
  302. private function getPublishedProperties(string $path, array $requestedProperties): array {
  303. $allowedProps = array_intersect(self::PUBLISHED_READ_ONLY_PROPERTIES, $requestedProperties);
  304. if (empty($allowedProps)) {
  305. return [];
  306. }
  307. $qb = $this->connection->getQueryBuilder();
  308. $qb->select('*')
  309. ->from(self::TABLE_NAME)
  310. ->where($qb->expr()->eq('propertypath', $qb->createNamedParameter($path)));
  311. $result = $qb->executeQuery();
  312. $props = [];
  313. while ($row = $result->fetch()) {
  314. $props[$row['propertyname']] = $this->decodeValueFromDatabase($row['propertyvalue'], $row['valuetype']);
  315. }
  316. $result->closeCursor();
  317. return $props;
  318. }
  319. /**
  320. * prefetch all user properties in a directory
  321. */
  322. private function cacheDirectory(string $path, Directory $node): void {
  323. $prefix = ltrim($path . '/', '/');
  324. $query = $this->connection->getQueryBuilder();
  325. $query->select('name', 'propertypath', 'propertyname', 'propertyvalue', 'valuetype')
  326. ->from('filecache', 'f')
  327. ->leftJoin('f', 'properties', 'p', $query->expr()->andX(
  328. $query->expr()->eq('propertypath', $query->func()->concat(
  329. $query->createNamedParameter($prefix),
  330. 'name'
  331. )),
  332. $query->expr()->eq('userid', $query->createNamedParameter($this->user->getUID()))
  333. ))
  334. ->where($query->expr()->eq('parent', $query->createNamedParameter($node->getInternalFileId(), IQueryBuilder::PARAM_INT)));
  335. $result = $query->executeQuery();
  336. $propsByPath = [];
  337. while ($row = $result->fetch()) {
  338. $childPath = $prefix . $row['name'];
  339. if (!isset($propsByPath[$childPath])) {
  340. $propsByPath[$childPath] = [];
  341. }
  342. if (isset($row['propertyname'])) {
  343. $propsByPath[$childPath][$row['propertyname']] = $this->decodeValueFromDatabase($row['propertyvalue'], $row['valuetype']);
  344. }
  345. }
  346. $this->userCache = array_merge($this->userCache, $propsByPath);
  347. }
  348. /**
  349. * Returns a list of properties for the given path and current user
  350. *
  351. * @param string $path
  352. * @param array $requestedProperties requested properties or empty array for "all"
  353. * @return array
  354. * @note The properties list is a list of propertynames the client
  355. * requested, encoded as xmlnamespace#tagName, for example:
  356. * http://www.example.org/namespace#author If the array is empty, all
  357. * properties should be returned
  358. */
  359. private function getUserProperties(string $path, array $requestedProperties) {
  360. if (isset($this->userCache[$path])) {
  361. return $this->userCache[$path];
  362. }
  363. // TODO: chunking if more than 1000 properties
  364. $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?';
  365. $whereValues = [$this->user->getUID(), $this->formatPath($path)];
  366. $whereTypes = [null, null];
  367. if (!empty($requestedProperties)) {
  368. // request only a subset
  369. $sql .= ' AND `propertyname` in (?)';
  370. $whereValues[] = $requestedProperties;
  371. $whereTypes[] = IQueryBuilder::PARAM_STR_ARRAY;
  372. }
  373. $result = $this->connection->executeQuery(
  374. $sql,
  375. $whereValues,
  376. $whereTypes
  377. );
  378. $props = [];
  379. while ($row = $result->fetch()) {
  380. $props[$row['propertyname']] = $this->decodeValueFromDatabase($row['propertyvalue'], $row['valuetype']);
  381. }
  382. $result->closeCursor();
  383. $this->userCache[$path] = $props;
  384. return $props;
  385. }
  386. /**
  387. * @throws Exception
  388. */
  389. private function updateProperties(string $path, array $properties): bool {
  390. // TODO: use "insert or update" strategy ?
  391. $existing = $this->getUserProperties($path, []);
  392. try {
  393. $this->connection->beginTransaction();
  394. foreach ($properties as $propertyName => $propertyValue) {
  395. // common parameters for all queries
  396. $dbParameters = [
  397. 'userid' => $this->user->getUID(),
  398. 'propertyPath' => $this->formatPath($path),
  399. 'propertyName' => $propertyName,
  400. ];
  401. // If it was null, we need to delete the property
  402. if (is_null($propertyValue)) {
  403. if (array_key_exists($propertyName, $existing)) {
  404. $deleteQuery = $deleteQuery ?? $this->createDeleteQuery();
  405. $deleteQuery
  406. ->setParameters($dbParameters)
  407. ->executeStatement();
  408. }
  409. } else {
  410. [$value, $valueType] = $this->encodeValueForDatabase(
  411. $path,
  412. $propertyName,
  413. $propertyValue,
  414. );
  415. $dbParameters['propertyValue'] = $value;
  416. $dbParameters['valueType'] = $valueType;
  417. if (!array_key_exists($propertyName, $existing)) {
  418. $insertQuery = $insertQuery ?? $this->createInsertQuery();
  419. $insertQuery
  420. ->setParameters($dbParameters)
  421. ->executeStatement();
  422. } else {
  423. $updateQuery = $updateQuery ?? $this->createUpdateQuery();
  424. $updateQuery
  425. ->setParameters($dbParameters)
  426. ->executeStatement();
  427. }
  428. }
  429. }
  430. $this->connection->commit();
  431. unset($this->userCache[$path]);
  432. } catch (Exception $e) {
  433. $this->connection->rollBack();
  434. throw $e;
  435. }
  436. return true;
  437. }
  438. /**
  439. * long paths are hashed to ensure they fit in the database
  440. *
  441. * @param string $path
  442. * @return string
  443. */
  444. private function formatPath(string $path): string {
  445. if (strlen($path) > 250) {
  446. return sha1($path);
  447. }
  448. return $path;
  449. }
  450. /**
  451. * @throws ParseException If parsing a \Sabre\DAV\Xml\Property\Complex value fails
  452. * @throws DavException If the property value is invalid
  453. */
  454. private function encodeValueForDatabase(string $path, string $name, mixed $value): array {
  455. // Try to parse a more specialized property type first
  456. if ($value instanceof Complex) {
  457. $xml = $this->xmlService->write($name, [$value], $this->server->getBaseUri());
  458. $value = $this->xmlService->parse($xml, $this->server->getBaseUri()) ?? $value;
  459. }
  460. if ($name === '{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL') {
  461. $value = $this->encodeDefaultCalendarUrl($value);
  462. }
  463. try {
  464. $this->validateProperty($path, $name, $value);
  465. } catch (DavException $e) {
  466. throw new DavException(
  467. "Property \"$name\" has an invalid value: " . $e->getMessage(),
  468. 0,
  469. $e,
  470. );
  471. }
  472. if (is_scalar($value)) {
  473. $valueType = self::PROPERTY_TYPE_STRING;
  474. } elseif ($value instanceof Complex) {
  475. $valueType = self::PROPERTY_TYPE_XML;
  476. $value = $value->getXml();
  477. } elseif ($value instanceof Href) {
  478. $valueType = self::PROPERTY_TYPE_HREF;
  479. $value = $value->getHref();
  480. } else {
  481. $valueType = self::PROPERTY_TYPE_OBJECT;
  482. $value = serialize($value);
  483. }
  484. return [$value, $valueType];
  485. }
  486. /**
  487. * @return mixed|Complex|string
  488. */
  489. private function decodeValueFromDatabase(string $value, int $valueType) {
  490. switch ($valueType) {
  491. case self::PROPERTY_TYPE_XML:
  492. return new Complex($value);
  493. case self::PROPERTY_TYPE_HREF:
  494. return new Href($value);
  495. case self::PROPERTY_TYPE_OBJECT:
  496. return unserialize($value);
  497. case self::PROPERTY_TYPE_STRING:
  498. default:
  499. return $value;
  500. }
  501. }
  502. private function encodeDefaultCalendarUrl(Href $value): Href {
  503. $href = $value->getHref();
  504. if ($href === null) {
  505. return $value;
  506. }
  507. if (!str_starts_with($href, '/')) {
  508. return $value;
  509. }
  510. try {
  511. // Build path relative to the dav base URI to be used later to find the node
  512. $value = new LocalHref($this->server->calculateUri($href) . '/');
  513. } catch (DavException\Forbidden) {
  514. // Not existing calendars will be handled later when the value is validated
  515. }
  516. return $value;
  517. }
  518. private function createDeleteQuery(): IQueryBuilder {
  519. $deleteQuery = $this->connection->getQueryBuilder();
  520. $deleteQuery->delete('properties')
  521. ->where($deleteQuery->expr()->eq('userid', $deleteQuery->createParameter('userid')))
  522. ->andWhere($deleteQuery->expr()->eq('propertypath', $deleteQuery->createParameter('propertyPath')))
  523. ->andWhere($deleteQuery->expr()->eq('propertyname', $deleteQuery->createParameter('propertyName')));
  524. return $deleteQuery;
  525. }
  526. private function createInsertQuery(): IQueryBuilder {
  527. $insertQuery = $this->connection->getQueryBuilder();
  528. $insertQuery->insert('properties')
  529. ->values([
  530. 'userid' => $insertQuery->createParameter('userid'),
  531. 'propertypath' => $insertQuery->createParameter('propertyPath'),
  532. 'propertyname' => $insertQuery->createParameter('propertyName'),
  533. 'propertyvalue' => $insertQuery->createParameter('propertyValue'),
  534. 'valuetype' => $insertQuery->createParameter('valueType'),
  535. ]);
  536. return $insertQuery;
  537. }
  538. private function createUpdateQuery(): IQueryBuilder {
  539. $updateQuery = $this->connection->getQueryBuilder();
  540. $updateQuery->update('properties')
  541. ->set('propertyvalue', $updateQuery->createParameter('propertyValue'))
  542. ->set('valuetype', $updateQuery->createParameter('valueType'))
  543. ->where($updateQuery->expr()->eq('userid', $updateQuery->createParameter('userid')))
  544. ->andWhere($updateQuery->expr()->eq('propertypath', $updateQuery->createParameter('propertyPath')))
  545. ->andWhere($updateQuery->expr()->eq('propertyname', $updateQuery->createParameter('propertyName')));
  546. return $updateQuery;
  547. }
  548. }