Subscription.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CalDAV\Subscriptions;
  4. use Sabre\CalDAV\Backend\SubscriptionSupport;
  5. use Sabre\DAV\Collection;
  6. use Sabre\DAV\PropPatch;
  7. use Sabre\DAV\Xml\Property\Href;
  8. use Sabre\DAVACL\ACLTrait;
  9. use Sabre\DAVACL\IACL;
  10. /**
  11. * Subscription Node.
  12. *
  13. * This node represents a subscription.
  14. *
  15. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  16. * @author Evert Pot (http://evertpot.com/)
  17. * @license http://sabre.io/license/ Modified BSD License
  18. */
  19. class Subscription extends Collection implements ISubscription, IACL
  20. {
  21. use ACLTrait;
  22. /**
  23. * caldavBackend.
  24. *
  25. * @var SubscriptionSupport
  26. */
  27. protected $caldavBackend;
  28. /**
  29. * subscriptionInfo.
  30. *
  31. * @var array
  32. */
  33. protected $subscriptionInfo;
  34. /**
  35. * Constructor.
  36. */
  37. public function __construct(SubscriptionSupport $caldavBackend, array $subscriptionInfo)
  38. {
  39. $this->caldavBackend = $caldavBackend;
  40. $this->subscriptionInfo = $subscriptionInfo;
  41. $required = [
  42. 'id',
  43. 'uri',
  44. 'principaluri',
  45. 'source',
  46. ];
  47. foreach ($required as $r) {
  48. if (!isset($subscriptionInfo[$r])) {
  49. throw new \InvalidArgumentException('The '.$r.' field is required when creating a subscription node');
  50. }
  51. }
  52. }
  53. /**
  54. * Returns the name of the node.
  55. *
  56. * This is used to generate the url.
  57. *
  58. * @return string
  59. */
  60. public function getName()
  61. {
  62. return $this->subscriptionInfo['uri'];
  63. }
  64. /**
  65. * Returns the last modification time.
  66. *
  67. * @return int|null
  68. */
  69. public function getLastModified()
  70. {
  71. if (isset($this->subscriptionInfo['lastmodified'])) {
  72. return $this->subscriptionInfo['lastmodified'];
  73. }
  74. }
  75. /**
  76. * Deletes the current node.
  77. */
  78. public function delete()
  79. {
  80. $this->caldavBackend->deleteSubscription(
  81. $this->subscriptionInfo['id']
  82. );
  83. }
  84. /**
  85. * Returns an array with all the child nodes.
  86. *
  87. * @return \Sabre\DAV\INode[]
  88. */
  89. public function getChildren()
  90. {
  91. return [];
  92. }
  93. /**
  94. * Updates properties on this node.
  95. *
  96. * This method received a PropPatch object, which contains all the
  97. * information about the update.
  98. *
  99. * To update specific properties, call the 'handle' method on this object.
  100. * Read the PropPatch documentation for more information.
  101. */
  102. public function propPatch(PropPatch $propPatch)
  103. {
  104. return $this->caldavBackend->updateSubscription(
  105. $this->subscriptionInfo['id'],
  106. $propPatch
  107. );
  108. }
  109. /**
  110. * Returns a list of properties for this nodes.
  111. *
  112. * The properties list is a list of propertynames the client requested,
  113. * encoded in clark-notation {xmlnamespace}tagname.
  114. *
  115. * If the array is empty, it means 'all properties' were requested.
  116. *
  117. * Note that it's fine to liberally give properties back, instead of
  118. * conforming to the list of requested properties.
  119. * The Server class will filter out the extra.
  120. *
  121. * @param array $properties
  122. *
  123. * @return array
  124. */
  125. public function getProperties($properties)
  126. {
  127. $r = [];
  128. foreach ($properties as $prop) {
  129. switch ($prop) {
  130. case '{http://calendarserver.org/ns/}source':
  131. $r[$prop] = new Href($this->subscriptionInfo['source']);
  132. break;
  133. default:
  134. if (array_key_exists($prop, $this->subscriptionInfo)) {
  135. $r[$prop] = $this->subscriptionInfo[$prop];
  136. }
  137. break;
  138. }
  139. }
  140. return $r;
  141. }
  142. /**
  143. * Returns the owner principal.
  144. *
  145. * This must be a url to a principal, or null if there's no owner
  146. *
  147. * @return string|null
  148. */
  149. public function getOwner()
  150. {
  151. return $this->subscriptionInfo['principaluri'];
  152. }
  153. /**
  154. * Returns a list of ACE's for this node.
  155. *
  156. * Each ACE has the following properties:
  157. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  158. * currently the only supported privileges
  159. * * 'principal', a url to the principal who owns the node
  160. * * 'protected' (optional), indicating that this ACE is not allowed to
  161. * be updated.
  162. *
  163. * @return array
  164. */
  165. public function getACL()
  166. {
  167. return [
  168. [
  169. 'privilege' => '{DAV:}all',
  170. 'principal' => $this->getOwner(),
  171. 'protected' => true,
  172. ],
  173. [
  174. 'privilege' => '{DAV:}all',
  175. 'principal' => $this->getOwner().'/calendar-proxy-write',
  176. 'protected' => true,
  177. ],
  178. [
  179. 'privilege' => '{DAV:}read',
  180. 'principal' => $this->getOwner().'/calendar-proxy-read',
  181. 'protected' => true,
  182. ],
  183. ];
  184. }
  185. }