Inbox.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CalDAV\Schedule;
  4. use Sabre\CalDAV;
  5. use Sabre\CalDAV\Backend;
  6. use Sabre\DAV;
  7. use Sabre\DAVACL;
  8. use Sabre\VObject;
  9. /**
  10. * The CalDAV scheduling inbox.
  11. *
  12. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  13. * @author Evert Pot (http://evertpot.com/)
  14. * @license http://sabre.io/license/ Modified BSD License
  15. */
  16. class Inbox extends DAV\Collection implements IInbox
  17. {
  18. use DAVACL\ACLTrait;
  19. /**
  20. * CalDAV backend.
  21. *
  22. * @var Backend\BackendInterface
  23. */
  24. protected $caldavBackend;
  25. /**
  26. * The principal Uri.
  27. *
  28. * @var string
  29. */
  30. protected $principalUri;
  31. /**
  32. * Constructor.
  33. *
  34. * @param string $principalUri
  35. */
  36. public function __construct(Backend\SchedulingSupport $caldavBackend, $principalUri)
  37. {
  38. $this->caldavBackend = $caldavBackend;
  39. $this->principalUri = $principalUri;
  40. }
  41. /**
  42. * Returns the name of the node.
  43. *
  44. * This is used to generate the url.
  45. *
  46. * @return string
  47. */
  48. public function getName()
  49. {
  50. return 'inbox';
  51. }
  52. /**
  53. * Returns an array with all the child nodes.
  54. *
  55. * @return \Sabre\DAV\INode[]
  56. */
  57. public function getChildren()
  58. {
  59. $objs = $this->caldavBackend->getSchedulingObjects($this->principalUri);
  60. $children = [];
  61. foreach ($objs as $obj) {
  62. //$obj['acl'] = $this->getACL();
  63. $obj['principaluri'] = $this->principalUri;
  64. $children[] = new SchedulingObject($this->caldavBackend, $obj);
  65. }
  66. return $children;
  67. }
  68. /**
  69. * Creates a new file in the directory.
  70. *
  71. * Data will either be supplied as a stream resource, or in certain cases
  72. * as a string. Keep in mind that you may have to support either.
  73. *
  74. * After successful creation of the file, you may choose to return the ETag
  75. * of the new file here.
  76. *
  77. * The returned ETag must be surrounded by double-quotes (The quotes should
  78. * be part of the actual string).
  79. *
  80. * If you cannot accurately determine the ETag, you should not return it.
  81. * If you don't store the file exactly as-is (you're transforming it
  82. * somehow) you should also not return an ETag.
  83. *
  84. * This means that if a subsequent GET to this new file does not exactly
  85. * return the same contents of what was submitted here, you are strongly
  86. * recommended to omit the ETag.
  87. *
  88. * @param string $name Name of the file
  89. * @param resource|string $data Initial payload
  90. *
  91. * @return string|null
  92. */
  93. public function createFile($name, $data = null)
  94. {
  95. $this->caldavBackend->createSchedulingObject($this->principalUri, $name, $data);
  96. }
  97. /**
  98. * Returns the owner principal.
  99. *
  100. * This must be a url to a principal, or null if there's no owner
  101. *
  102. * @return string|null
  103. */
  104. public function getOwner()
  105. {
  106. return $this->principalUri;
  107. }
  108. /**
  109. * Returns a list of ACE's for this node.
  110. *
  111. * Each ACE has the following properties:
  112. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  113. * currently the only supported privileges
  114. * * 'principal', a url to the principal who owns the node
  115. * * 'protected' (optional), indicating that this ACE is not allowed to
  116. * be updated.
  117. *
  118. * @return array
  119. */
  120. public function getACL()
  121. {
  122. return [
  123. [
  124. 'privilege' => '{DAV:}read',
  125. 'principal' => '{DAV:}authenticated',
  126. 'protected' => true,
  127. ],
  128. [
  129. 'privilege' => '{DAV:}write-properties',
  130. 'principal' => $this->getOwner(),
  131. 'protected' => true,
  132. ],
  133. [
  134. 'privilege' => '{DAV:}unbind',
  135. 'principal' => $this->getOwner(),
  136. 'protected' => true,
  137. ],
  138. [
  139. 'privilege' => '{DAV:}unbind',
  140. 'principal' => $this->getOwner().'/calendar-proxy-write',
  141. 'protected' => true,
  142. ],
  143. [
  144. 'privilege' => '{'.CalDAV\Plugin::NS_CALDAV.'}schedule-deliver',
  145. 'principal' => '{DAV:}authenticated',
  146. 'protected' => true,
  147. ],
  148. ];
  149. }
  150. /**
  151. * Performs a calendar-query on the contents of this calendar.
  152. *
  153. * The calendar-query is defined in RFC4791 : CalDAV. Using the
  154. * calendar-query it is possible for a client to request a specific set of
  155. * object, based on contents of iCalendar properties, date-ranges and
  156. * iCalendar component types (VTODO, VEVENT).
  157. *
  158. * This method should just return a list of (relative) urls that match this
  159. * query.
  160. *
  161. * The list of filters are specified as an array. The exact array is
  162. * documented by \Sabre\CalDAV\CalendarQueryParser.
  163. *
  164. * @return array
  165. */
  166. public function calendarQuery(array $filters)
  167. {
  168. $result = [];
  169. $validator = new CalDAV\CalendarQueryValidator();
  170. $objects = $this->caldavBackend->getSchedulingObjects($this->principalUri);
  171. foreach ($objects as $object) {
  172. $vObject = VObject\Reader::read($object['calendardata']);
  173. if ($validator->validate($vObject, $filters)) {
  174. $result[] = $object['uri'];
  175. }
  176. // Destroy circular references to PHP will GC the object.
  177. $vObject->destroy();
  178. }
  179. return $result;
  180. }
  181. }