AbstractBackend.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CalDAV\Backend;
  4. use Sabre\CalDAV;
  5. use Sabre\VObject;
  6. /**
  7. * Abstract Calendaring backend. Extend this class to create your own backends.
  8. *
  9. * Checkout the BackendInterface for all the methods that must be implemented.
  10. *
  11. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  12. * @author Evert Pot (http://evertpot.com/)
  13. * @license http://sabre.io/license/ Modified BSD License
  14. */
  15. abstract class AbstractBackend implements BackendInterface
  16. {
  17. /**
  18. * Updates properties for a calendar.
  19. *
  20. * The list of mutations is stored in a Sabre\DAV\PropPatch object.
  21. * To do the actual updates, you must tell this object which properties
  22. * you're going to process with the handle() method.
  23. *
  24. * Calling the handle method is like telling the PropPatch object "I
  25. * promise I can handle updating this property".
  26. *
  27. * Read the PropPatch documentation for more info and examples.
  28. *
  29. * @param mixed $calendarId
  30. */
  31. public function updateCalendar($calendarId, \Sabre\DAV\PropPatch $propPatch)
  32. {
  33. }
  34. /**
  35. * Returns a list of calendar objects.
  36. *
  37. * This method should work identical to getCalendarObject, but instead
  38. * return all the calendar objects in the list as an array.
  39. *
  40. * If the backend supports this, it may allow for some speed-ups.
  41. *
  42. * @param mixed $calendarId
  43. *
  44. * @return array
  45. */
  46. public function getMultipleCalendarObjects($calendarId, array $uris)
  47. {
  48. return array_map(function ($uri) use ($calendarId) {
  49. return $this->getCalendarObject($calendarId, $uri);
  50. }, $uris);
  51. }
  52. /**
  53. * Performs a calendar-query on the contents of this calendar.
  54. *
  55. * The calendar-query is defined in RFC4791 : CalDAV. Using the
  56. * calendar-query it is possible for a client to request a specific set of
  57. * object, based on contents of iCalendar properties, date-ranges and
  58. * iCalendar component types (VTODO, VEVENT).
  59. *
  60. * This method should just return a list of (relative) urls that match this
  61. * query.
  62. *
  63. * The list of filters are specified as an array. The exact array is
  64. * documented by \Sabre\CalDAV\CalendarQueryParser.
  65. *
  66. * Note that it is extremely likely that getCalendarObject for every path
  67. * returned from this method will be called almost immediately after. You
  68. * may want to anticipate this to speed up these requests.
  69. *
  70. * This method provides a default implementation, which parses *all* the
  71. * iCalendar objects in the specified calendar.
  72. *
  73. * This default may well be good enough for personal use, and calendars
  74. * that aren't very large. But if you anticipate high usage, big calendars
  75. * or high loads, you are strongly advised to optimize certain paths.
  76. *
  77. * The best way to do so is override this method and to optimize
  78. * specifically for 'common filters'.
  79. *
  80. * Requests that are extremely common are:
  81. * * requests for just VEVENTS
  82. * * requests for just VTODO
  83. * * requests with a time-range-filter on either VEVENT or VTODO.
  84. *
  85. * ..and combinations of these requests. It may not be worth it to try to
  86. * handle every possible situation and just rely on the (relatively
  87. * easy to use) CalendarQueryValidator to handle the rest.
  88. *
  89. * Note that especially time-range-filters may be difficult to parse. A
  90. * time-range filter specified on a VEVENT must for instance also handle
  91. * recurrence rules correctly.
  92. * A good example of how to interpret all these filters can also simply
  93. * be found in \Sabre\CalDAV\CalendarQueryFilter. This class is as correct
  94. * as possible, so it gives you a good idea on what type of stuff you need
  95. * to think of.
  96. *
  97. * @param mixed $calendarId
  98. *
  99. * @return array
  100. */
  101. public function calendarQuery($calendarId, array $filters)
  102. {
  103. $result = [];
  104. $objects = $this->getCalendarObjects($calendarId);
  105. foreach ($objects as $object) {
  106. if ($this->validateFilterForObject($object, $filters)) {
  107. $result[] = $object['uri'];
  108. }
  109. }
  110. return $result;
  111. }
  112. /**
  113. * This method validates if a filter (as passed to calendarQuery) matches
  114. * the given object.
  115. *
  116. * @return bool
  117. */
  118. protected function validateFilterForObject(array $object, array $filters)
  119. {
  120. // Unfortunately, setting the 'calendardata' here is optional. If
  121. // it was excluded, we actually need another call to get this as
  122. // well.
  123. if (!isset($object['calendardata'])) {
  124. $object = $this->getCalendarObject($object['calendarid'], $object['uri']);
  125. }
  126. $vObject = VObject\Reader::read($object['calendardata']);
  127. $validator = new CalDAV\CalendarQueryValidator();
  128. $result = $validator->validate($vObject, $filters);
  129. // Destroy circular references so PHP will GC the object.
  130. $vObject->destroy();
  131. return $result;
  132. }
  133. /**
  134. * Searches through all of a users calendars and calendar objects to find
  135. * an object with a specific UID.
  136. *
  137. * This method should return the path to this object, relative to the
  138. * calendar home, so this path usually only contains two parts:
  139. *
  140. * calendarpath/objectpath.ics
  141. *
  142. * If the uid is not found, return null.
  143. *
  144. * This method should only consider * objects that the principal owns, so
  145. * any calendars owned by other principals that also appear in this
  146. * collection should be ignored.
  147. *
  148. * @param string $principalUri
  149. * @param string $uid
  150. *
  151. * @return string|null
  152. */
  153. public function getCalendarObjectByUID($principalUri, $uid)
  154. {
  155. // Note: this is a super slow naive implementation of this method. You
  156. // are highly recommended to optimize it, if your backend allows it.
  157. foreach ($this->getCalendarsForUser($principalUri) as $calendar) {
  158. // We must ignore calendars owned by other principals.
  159. if ($calendar['principaluri'] !== $principalUri) {
  160. continue;
  161. }
  162. // Ignore calendars that are shared.
  163. if (isset($calendar['{http://sabredav.org/ns}owner-principal']) && $calendar['{http://sabredav.org/ns}owner-principal'] !== $principalUri) {
  164. continue;
  165. }
  166. $results = $this->calendarQuery(
  167. $calendar['id'],
  168. [
  169. 'name' => 'VCALENDAR',
  170. 'prop-filters' => [],
  171. 'comp-filters' => [
  172. [
  173. 'name' => 'VEVENT',
  174. 'is-not-defined' => false,
  175. 'time-range' => null,
  176. 'comp-filters' => [],
  177. 'prop-filters' => [
  178. [
  179. 'name' => 'UID',
  180. 'is-not-defined' => false,
  181. 'time-range' => null,
  182. 'text-match' => [
  183. 'value' => $uid,
  184. 'negate-condition' => false,
  185. 'collation' => 'i;octet',
  186. ],
  187. 'param-filters' => [],
  188. ],
  189. ],
  190. ],
  191. ],
  192. ]
  193. );
  194. if ($results) {
  195. // We have a match
  196. return $calendar['uri'].'/'.$results[0];
  197. }
  198. }
  199. }
  200. }