Inbox.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Sabre\CalDAV\Schedule;
  3. use Sabre\DAV;
  4. use Sabre\CalDAV;
  5. use Sabre\DAVACL;
  6. use Sabre\CalDAV\Backend;
  7. use Sabre\VObject;
  8. /**
  9. * The CalDAV scheduling inbox
  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. class Inbox extends DAV\Collection implements IInbox {
  16. /**
  17. * CalDAV backend
  18. *
  19. * @var Backend\BackendInterface
  20. */
  21. protected $caldavBackend;
  22. /**
  23. * The principal Uri
  24. *
  25. * @var string
  26. */
  27. protected $principalUri;
  28. /**
  29. * Constructor
  30. *
  31. * @param Backend\SchedulingSupport $caldavBackend
  32. * @param string $principalUri
  33. */
  34. function __construct(Backend\SchedulingSupport $caldavBackend, $principalUri) {
  35. $this->caldavBackend = $caldavBackend;
  36. $this->principalUri = $principalUri;
  37. }
  38. /**
  39. * Returns the name of the node.
  40. *
  41. * This is used to generate the url.
  42. *
  43. * @return string
  44. */
  45. function getName() {
  46. return 'inbox';
  47. }
  48. /**
  49. * Returns an array with all the child nodes
  50. *
  51. * @return \Sabre\DAV\INode[]
  52. */
  53. function getChildren() {
  54. $objs = $this->caldavBackend->getSchedulingObjects($this->principalUri);
  55. $children = [];
  56. foreach ($objs as $obj) {
  57. //$obj['acl'] = $this->getACL();
  58. $obj['principaluri'] = $this->principalUri;
  59. $children[] = new SchedulingObject($this->caldavBackend, $obj);
  60. }
  61. return $children;
  62. }
  63. /**
  64. * Creates a new file in the directory
  65. *
  66. * Data will either be supplied as a stream resource, or in certain cases
  67. * as a string. Keep in mind that you may have to support either.
  68. *
  69. * After succesful creation of the file, you may choose to return the ETag
  70. * of the new file here.
  71. *
  72. * The returned ETag must be surrounded by double-quotes (The quotes should
  73. * be part of the actual string).
  74. *
  75. * If you cannot accurately determine the ETag, you should not return it.
  76. * If you don't store the file exactly as-is (you're transforming it
  77. * somehow) you should also not return an ETag.
  78. *
  79. * This means that if a subsequent GET to this new file does not exactly
  80. * return the same contents of what was submitted here, you are strongly
  81. * recommended to omit the ETag.
  82. *
  83. * @param string $name Name of the file
  84. * @param resource|string $data Initial payload
  85. * @return null|string
  86. */
  87. function createFile($name, $data = null) {
  88. $this->caldavBackend->createSchedulingObject($this->principalUri, $name, $data);
  89. }
  90. /**
  91. * Returns the owner principal
  92. *
  93. * This must be a url to a principal, or null if there's no owner
  94. *
  95. * @return string|null
  96. */
  97. function getOwner() {
  98. return $this->principalUri;
  99. }
  100. /**
  101. * Returns a group principal
  102. *
  103. * This must be a url to a principal, or null if there's no owner
  104. *
  105. * @return string|null
  106. */
  107. function getGroup() {
  108. return null;
  109. }
  110. /**
  111. * Returns a list of ACE's for this node.
  112. *
  113. * Each ACE has the following properties:
  114. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  115. * currently the only supported privileges
  116. * * 'principal', a url to the principal who owns the node
  117. * * 'protected' (optional), indicating that this ACE is not allowed to
  118. * be updated.
  119. *
  120. * @return array
  121. */
  122. function getACL() {
  123. return [
  124. [
  125. 'privilege' => '{DAV:}read',
  126. 'principal' => '{DAV:}authenticated',
  127. 'protected' => true,
  128. ],
  129. [
  130. 'privilege' => '{DAV:}write-properties',
  131. 'principal' => $this->getOwner(),
  132. 'protected' => true,
  133. ],
  134. [
  135. 'privilege' => '{DAV:}unbind',
  136. 'principal' => $this->getOwner(),
  137. 'protected' => true,
  138. ],
  139. [
  140. 'privilege' => '{DAV:}unbind',
  141. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  142. 'protected' => true,
  143. ],
  144. [
  145. 'privilege' => '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-deliver-invite',
  146. 'principal' => '{DAV:}authenticated',
  147. 'protected' => true,
  148. ],
  149. [
  150. 'privilege' => '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-deliver-reply',
  151. 'principal' => '{DAV:}authenticated',
  152. 'protected' => true,
  153. ],
  154. ];
  155. }
  156. /**
  157. * Updates the ACL
  158. *
  159. * This method will receive a list of new ACE's.
  160. *
  161. * @param array $acl
  162. * @return void
  163. */
  164. function setACL(array $acl) {
  165. throw new DAV\Exception\MethodNotAllowed('You\'re not allowed to update the ACL');
  166. }
  167. /**
  168. * Returns the list of supported privileges for this node.
  169. *
  170. * The returned data structure is a list of nested privileges.
  171. * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
  172. * standard structure.
  173. *
  174. * If null is returned from this method, the default privilege set is used,
  175. * which is fine for most common usecases.
  176. *
  177. * @return array|null
  178. */
  179. function getSupportedPrivilegeSet() {
  180. $ns = '{' . CalDAV\Plugin::NS_CALDAV . '}';
  181. $default = DAVACL\Plugin::getDefaultSupportedPrivilegeSet();
  182. $default['aggregates'][] = [
  183. 'privilege' => $ns . 'schedule-deliver',
  184. 'aggregates' => [
  185. ['privilege' => $ns . 'schedule-deliver-invite'],
  186. ['privilege' => $ns . 'schedule-deliver-reply'],
  187. ],
  188. ];
  189. return $default;
  190. }
  191. /**
  192. * Performs a calendar-query on the contents of this calendar.
  193. *
  194. * The calendar-query is defined in RFC4791 : CalDAV. Using the
  195. * calendar-query it is possible for a client to request a specific set of
  196. * object, based on contents of iCalendar properties, date-ranges and
  197. * iCalendar component types (VTODO, VEVENT).
  198. *
  199. * This method should just return a list of (relative) urls that match this
  200. * query.
  201. *
  202. * The list of filters are specified as an array. The exact array is
  203. * documented by \Sabre\CalDAV\CalendarQueryParser.
  204. *
  205. * @param array $filters
  206. * @return array
  207. */
  208. function calendarQuery(array $filters) {
  209. $result = [];
  210. $validator = new CalDAV\CalendarQueryValidator();
  211. $objects = $this->caldavBackend->getSchedulingObjects($this->principalUri);
  212. foreach ($objects as $object) {
  213. $vObject = VObject\Reader::read($object['calendardata']);
  214. if ($validator->validate($vObject, $filters)) {
  215. $result[] = $object['uri'];
  216. }
  217. // Destroy circular references to PHP will GC the object.
  218. $vObject->destroy();
  219. }
  220. return $result;
  221. }
  222. }