Invite.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CalDAV\Xml\Notification;
  4. use Sabre\CalDAV;
  5. use Sabre\CalDAV\SharingPlugin as SharingPlugin;
  6. use Sabre\DAV;
  7. use Sabre\Xml\Writer;
  8. /**
  9. * This class represents the cs:invite-notification notification element.
  10. *
  11. * This element is defined here:
  12. * http://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk/doc/Extensions/caldav-sharing.txt
  13. *
  14. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  15. * @author Evert Pot (http://evertpot.com/)
  16. * @license http://sabre.io/license/ Modified BSD License
  17. */
  18. class Invite implements NotificationInterface
  19. {
  20. /**
  21. * A unique id for the message.
  22. *
  23. * @var string
  24. */
  25. protected $id;
  26. /**
  27. * Timestamp of the notification.
  28. *
  29. * @var \DateTime
  30. */
  31. protected $dtStamp;
  32. /**
  33. * A url to the recipient of the notification. This can be an email
  34. * address (mailto:), or a principal url.
  35. *
  36. * @var string
  37. */
  38. protected $href;
  39. /**
  40. * The type of message, see the SharingPlugin::STATUS_* constants.
  41. *
  42. * @var int
  43. */
  44. protected $type;
  45. /**
  46. * True if access to a calendar is read-only.
  47. *
  48. * @var bool
  49. */
  50. protected $readOnly;
  51. /**
  52. * A url to the shared calendar.
  53. *
  54. * @var string
  55. */
  56. protected $hostUrl;
  57. /**
  58. * Url to the sharer of the calendar.
  59. *
  60. * @var string
  61. */
  62. protected $organizer;
  63. /**
  64. * The name of the sharer.
  65. *
  66. * @var string
  67. */
  68. protected $commonName;
  69. /**
  70. * The name of the sharer.
  71. *
  72. * @var string
  73. */
  74. protected $firstName;
  75. /**
  76. * The name of the sharer.
  77. *
  78. * @var string
  79. */
  80. protected $lastName;
  81. /**
  82. * A description of the share request.
  83. *
  84. * @var string
  85. */
  86. protected $summary;
  87. /**
  88. * The Etag for the notification.
  89. *
  90. * @var string
  91. */
  92. protected $etag;
  93. /**
  94. * The list of supported components.
  95. *
  96. * @var CalDAV\Xml\Property\SupportedCalendarComponentSet
  97. */
  98. protected $supportedComponents;
  99. /**
  100. * Creates the Invite notification.
  101. *
  102. * This constructor receives an array with the following elements:
  103. *
  104. * * id - A unique id
  105. * * etag - The etag
  106. * * dtStamp - A DateTime object with a timestamp for the notification.
  107. * * type - The type of notification, see SharingPlugin::STATUS_*
  108. * constants for details.
  109. * * readOnly - This must be set to true, if this is an invite for
  110. * read-only access to a calendar.
  111. * * hostUrl - A url to the shared calendar.
  112. * * organizer - Url to the sharer principal.
  113. * * commonName - The real name of the sharer (optional).
  114. * * firstName - The first name of the sharer (optional).
  115. * * lastName - The last name of the sharer (optional).
  116. * * summary - Description of the share, can be the same as the
  117. * calendar, but may also be modified (optional).
  118. * * supportedComponents - An instance of
  119. * Sabre\CalDAV\Property\SupportedCalendarComponentSet.
  120. * This allows the client to determine which components
  121. * will be supported in the shared calendar. This is
  122. * also optional.
  123. *
  124. * @param array $values All the options
  125. */
  126. public function __construct(array $values)
  127. {
  128. $required = [
  129. 'id',
  130. 'etag',
  131. 'href',
  132. 'dtStamp',
  133. 'type',
  134. 'readOnly',
  135. 'hostUrl',
  136. 'organizer',
  137. ];
  138. foreach ($required as $item) {
  139. if (!isset($values[$item])) {
  140. throw new \InvalidArgumentException($item.' is a required constructor option');
  141. }
  142. }
  143. foreach ($values as $key => $value) {
  144. if (!property_exists($this, $key)) {
  145. throw new \InvalidArgumentException('Unknown option: '.$key);
  146. }
  147. $this->$key = $value;
  148. }
  149. }
  150. /**
  151. * The xmlSerialize method is called during xml writing.
  152. *
  153. * Use the $writer argument to write its own xml serialization.
  154. *
  155. * An important note: do _not_ create a parent element. Any element
  156. * implementing XmlSerializable should only ever write what's considered
  157. * its 'inner xml'.
  158. *
  159. * The parent of the current element is responsible for writing a
  160. * containing element.
  161. *
  162. * This allows serializers to be re-used for different element names.
  163. *
  164. * If you are opening new elements, you must also close them again.
  165. */
  166. public function xmlSerialize(Writer $writer)
  167. {
  168. $writer->writeElement('{'.CalDAV\Plugin::NS_CALENDARSERVER.'}invite-notification');
  169. }
  170. /**
  171. * This method serializes the entire notification, as it is used in the
  172. * response body.
  173. */
  174. public function xmlSerializeFull(Writer $writer)
  175. {
  176. $cs = '{'.CalDAV\Plugin::NS_CALENDARSERVER.'}';
  177. $this->dtStamp->setTimezone(new \DateTimeZone('GMT'));
  178. $writer->writeElement($cs.'dtstamp', $this->dtStamp->format('Ymd\\THis\\Z'));
  179. $writer->startElement($cs.'invite-notification');
  180. $writer->writeElement($cs.'uid', $this->id);
  181. $writer->writeElement('{DAV:}href', $this->href);
  182. switch ($this->type) {
  183. case DAV\Sharing\Plugin::INVITE_ACCEPTED:
  184. $writer->writeElement($cs.'invite-accepted');
  185. break;
  186. case DAV\Sharing\Plugin::INVITE_NORESPONSE:
  187. $writer->writeElement($cs.'invite-noresponse');
  188. break;
  189. }
  190. $writer->writeElement($cs.'hosturl', [
  191. '{DAV:}href' => $writer->contextUri.$this->hostUrl,
  192. ]);
  193. if ($this->summary) {
  194. $writer->writeElement($cs.'summary', $this->summary);
  195. }
  196. $writer->startElement($cs.'access');
  197. if ($this->readOnly) {
  198. $writer->writeElement($cs.'read');
  199. } else {
  200. $writer->writeElement($cs.'read-write');
  201. }
  202. $writer->endElement(); // access
  203. $writer->startElement($cs.'organizer');
  204. // If the organizer contains a 'mailto:' part, it means it should be
  205. // treated as absolute.
  206. if ('mailto:' === strtolower(substr($this->organizer, 0, 7))) {
  207. $writer->writeElement('{DAV:}href', $this->organizer);
  208. } else {
  209. $writer->writeElement('{DAV:}href', $writer->contextUri.$this->organizer);
  210. }
  211. if ($this->commonName) {
  212. $writer->writeElement($cs.'common-name', $this->commonName);
  213. }
  214. if ($this->firstName) {
  215. $writer->writeElement($cs.'first-name', $this->firstName);
  216. }
  217. if ($this->lastName) {
  218. $writer->writeElement($cs.'last-name', $this->lastName);
  219. }
  220. $writer->endElement(); // organizer
  221. if ($this->commonName) {
  222. $writer->writeElement($cs.'organizer-cn', $this->commonName);
  223. }
  224. if ($this->firstName) {
  225. $writer->writeElement($cs.'organizer-first', $this->firstName);
  226. }
  227. if ($this->lastName) {
  228. $writer->writeElement($cs.'organizer-last', $this->lastName);
  229. }
  230. if ($this->supportedComponents) {
  231. $writer->writeElement('{'.CalDAV\Plugin::NS_CALDAV.'}supported-calendar-component-set', $this->supportedComponents);
  232. }
  233. $writer->endElement(); // invite-notification
  234. }
  235. /**
  236. * Returns a unique id for this notification.
  237. *
  238. * This is just the base url. This should generally be some kind of unique
  239. * id.
  240. *
  241. * @return string
  242. */
  243. public function getId()
  244. {
  245. return $this->id;
  246. }
  247. /**
  248. * Returns the ETag for this notification.
  249. *
  250. * The ETag must be surrounded by literal double-quotes.
  251. *
  252. * @return string
  253. */
  254. public function getETag()
  255. {
  256. return $this->etag;
  257. }
  258. }