/rainloop/v/0.0.0/app/libraries/SabreForRainLoop/CalDAV/Backend/AbstractBackend.php

https://gitlab.com/wuhang2003/rainloop-webmail · PHP · 155 lines · 29 code · 21 blank · 105 comment · 2 complexity · 60b8f4482d8546291e9936c021638a45 MD5 · raw file

  1. <?php
  2. namespace SabreForRainLoop\CalDAV\Backend;
  3. use SabreForRainLoop\VObject;
  4. use SabreForRainLoop\CalDAV;
  5. /**
  6. * Abstract Calendaring backend. Extend this class to create your own backends.
  7. *
  8. * Checkout the BackendInterface for all the methods that must be implemented.
  9. *
  10. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  11. * @author Evert Pot (http://evertpot.com/)
  12. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  13. */
  14. abstract class AbstractBackend implements BackendInterface {
  15. /**
  16. * Updates properties for a calendar.
  17. *
  18. * The mutations array uses the propertyName in clark-notation as key,
  19. * and the array value for the property value. In the case a property
  20. * should be deleted, the property value will be null.
  21. *
  22. * This method must be atomic. If one property cannot be changed, the
  23. * entire operation must fail.
  24. *
  25. * If the operation was successful, true can be returned.
  26. * If the operation failed, false can be returned.
  27. *
  28. * Deletion of a non-existent property is always successful.
  29. *
  30. * Lastly, it is optional to return detailed information about any
  31. * failures. In this case an array should be returned with the following
  32. * structure:
  33. *
  34. * array(
  35. * 403 => array(
  36. * '{DAV:}displayname' => null,
  37. * ),
  38. * 424 => array(
  39. * '{DAV:}owner' => null,
  40. * )
  41. * )
  42. *
  43. * In this example it was forbidden to update {DAV:}displayname.
  44. * (403 Forbidden), which in turn also caused {DAV:}owner to fail
  45. * (424 Failed Dependency) because the request needs to be atomic.
  46. *
  47. * @param mixed $calendarId
  48. * @param array $mutations
  49. * @return bool|array
  50. */
  51. public function updateCalendar($calendarId, array $mutations) {
  52. return false;
  53. }
  54. /**
  55. * Performs a calendar-query on the contents of this calendar.
  56. *
  57. * The calendar-query is defined in RFC4791 : CalDAV. Using the
  58. * calendar-query it is possible for a client to request a specific set of
  59. * object, based on contents of iCalendar properties, date-ranges and
  60. * iCalendar component types (VTODO, VEVENT).
  61. *
  62. * This method should just return a list of (relative) urls that match this
  63. * query.
  64. *
  65. * The list of filters are specified as an array. The exact array is
  66. * documented by \SabreForRainLoop\CalDAV\CalendarQueryParser.
  67. *
  68. * Note that it is extremely likely that getCalendarObject for every path
  69. * returned from this method will be called almost immediately after. You
  70. * may want to anticipate this to speed up these requests.
  71. *
  72. * This method provides a default implementation, which parses *all* the
  73. * iCalendar objects in the specified calendar.
  74. *
  75. * This default may well be good enough for personal use, and calendars
  76. * that aren't very large. But if you anticipate high usage, big calendars
  77. * or high loads, you are strongly adviced to optimize certain paths.
  78. *
  79. * The best way to do so is override this method and to optimize
  80. * specifically for 'common filters'.
  81. *
  82. * Requests that are extremely common are:
  83. * * requests for just VEVENTS
  84. * * requests for just VTODO
  85. * * requests with a time-range-filter on either VEVENT or VTODO.
  86. *
  87. * ..and combinations of these requests. It may not be worth it to try to
  88. * handle every possible situation and just rely on the (relatively
  89. * easy to use) CalendarQueryValidator to handle the rest.
  90. *
  91. * Note that especially time-range-filters may be difficult to parse. A
  92. * time-range filter specified on a VEVENT must for instance also handle
  93. * recurrence rules correctly.
  94. * A good example of how to interprete all these filters can also simply
  95. * be found in \SabreForRainLoop\CalDAV\CalendarQueryFilter. This class is as correct
  96. * as possible, so it gives you a good idea on what type of stuff you need
  97. * to think of.
  98. *
  99. * @param mixed $calendarId
  100. * @param array $filters
  101. * @return array
  102. */
  103. public function calendarQuery($calendarId, array $filters) {
  104. $result = array();
  105. $objects = $this->getCalendarObjects($calendarId);
  106. $validator = new \SabreForRainLoop\CalDAV\CalendarQueryValidator();
  107. foreach($objects as $object) {
  108. if ($this->validateFilterForObject($object, $filters)) {
  109. $result[] = $object['uri'];
  110. }
  111. }
  112. return $result;
  113. }
  114. /**
  115. * This method validates if a filters (as passed to calendarQuery) matches
  116. * the given object.
  117. *
  118. * @param array $object
  119. * @param array $filters
  120. * @return bool
  121. */
  122. protected function validateFilterForObject(array $object, array $filters) {
  123. // Unfortunately, setting the 'calendardata' here is optional. If
  124. // it was excluded, we actually need another call to get this as
  125. // well.
  126. if (!isset($object['calendardata'])) {
  127. $object = $this->getCalendarObject($object['calendarid'], $object['uri']);
  128. }
  129. $data = is_resource($object['calendardata'])?stream_get_contents($object['calendardata']):$object['calendardata'];
  130. $vObject = VObject\Reader::read($data);
  131. $validator = new CalDAV\CalendarQueryValidator();
  132. return $validator->validate($vObject, $filters);
  133. }
  134. }