Broker.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. <?php
  2. namespace Sabre\VObject\ITip;
  3. use Sabre\VObject\Component\VCalendar;
  4. use Sabre\VObject\DateTimeParser;
  5. use Sabre\VObject\Reader;
  6. use Sabre\VObject\Recur\EventIterator;
  7. /**
  8. * The ITip\Broker class is a utility class that helps with processing
  9. * so-called iTip messages.
  10. *
  11. * iTip is defined in rfc5546, stands for iCalendar Transport-Independent
  12. * Interoperability Protocol, and describes the underlying mechanism for
  13. * using iCalendar for scheduling for for example through email (also known as
  14. * IMip) and CalDAV Scheduling.
  15. *
  16. * This class helps by:
  17. *
  18. * 1. Creating individual invites based on an iCalendar event for each
  19. * attendee.
  20. * 2. Generating invite updates based on an iCalendar update. This may result
  21. * in new invites, updates and cancellations for attendees, if that list
  22. * changed.
  23. * 3. On the receiving end, it can create a local iCalendar event based on
  24. * a received invite.
  25. * 4. It can also process an invite update on a local event, ensuring that any
  26. * overridden properties from attendees are retained.
  27. * 5. It can create a accepted or declined iTip reply based on an invite.
  28. * 6. It can process a reply from an invite and update an events attendee
  29. * status based on a reply.
  30. *
  31. * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
  32. * @author Evert Pot (http://evertpot.com/)
  33. * @license http://sabre.io/license/ Modified BSD License
  34. */
  35. class Broker {
  36. /**
  37. * This setting determines whether the rules for the SCHEDULE-AGENT
  38. * parameter should be followed.
  39. *
  40. * This is a parameter defined on ATTENDEE properties, introduced by RFC
  41. * 6638. This parameter allows a caldav client to tell the server 'Don't do
  42. * any scheduling operations'.
  43. *
  44. * If this setting is turned on, any attendees with SCHEDULE-AGENT set to
  45. * CLIENT will be ignored. This is the desired behavior for a CalDAV
  46. * server, but if you're writing an iTip application that doesn't deal with
  47. * CalDAV, you may want to ignore this parameter.
  48. *
  49. * @var bool
  50. */
  51. public $scheduleAgentServerRules = true;
  52. /**
  53. * The broker will try during 'parseEvent' figure out whether the change
  54. * was significant.
  55. *
  56. * It uses a few different ways to do this. One of these ways is seeing if
  57. * certain properties changed values. This list of specified here.
  58. *
  59. * This list is taken from:
  60. * * http://tools.ietf.org/html/rfc5546#section-2.1.4
  61. *
  62. * @var string[]
  63. */
  64. public $significantChangeProperties = array(
  65. 'DTSTART',
  66. 'DTEND',
  67. 'DURATION',
  68. 'DUE',
  69. 'RRULE',
  70. 'RDATE',
  71. 'EXDATE',
  72. 'STATUS',
  73. );
  74. /**
  75. * This method is used to process an incoming itip message.
  76. *
  77. * Examples:
  78. *
  79. * 1. A user is an attendee to an event. The organizer sends an updated
  80. * meeting using a new iTip message with METHOD:REQUEST. This function
  81. * will process the message and update the attendee's event accordingly.
  82. *
  83. * 2. The organizer cancelled the event using METHOD:CANCEL. We will update
  84. * the users event to state STATUS:CANCELLED.
  85. *
  86. * 3. An attendee sent a reply to an invite using METHOD:REPLY. We can
  87. * update the organizers event to update the ATTENDEE with its correct
  88. * PARTSTAT.
  89. *
  90. * The $existingObject is updated in-place. If there is no existing object
  91. * (because it's a new invite for example) a new object will be created.
  92. *
  93. * If an existing object does not exist, and the method was CANCEL or
  94. * REPLY, the message effectively gets ignored, and no 'existingObject'
  95. * will be created.
  96. *
  97. * The updated $existingObject is also returned from this function.
  98. *
  99. * If the iTip message was not supported, we will always return false.
  100. *
  101. * @param Message $itipMessage
  102. * @param VCalendar $existingObject
  103. * @return VCalendar|null
  104. */
  105. public function processMessage(Message $itipMessage, VCalendar $existingObject = null) {
  106. // We only support events at the moment.
  107. if ($itipMessage->component !== 'VEVENT') {
  108. return false;
  109. }
  110. switch($itipMessage->method) {
  111. case 'REQUEST' :
  112. return $this->processMessageRequest($itipMessage, $existingObject);
  113. case 'CANCEL' :
  114. return $this->processMessageCancel($itipMessage, $existingObject);
  115. case 'REPLY' :
  116. return $this->processMessageReply($itipMessage, $existingObject);
  117. default :
  118. // Unsupported iTip message
  119. return null;
  120. }
  121. return $existingObject;
  122. }
  123. /**
  124. * This function parses a VCALENDAR object and figure out if any messages
  125. * need to be sent.
  126. *
  127. * A VCALENDAR object will be created from the perspective of either an
  128. * attendee, or an organizer. You must pass a string identifying the
  129. * current user, so we can figure out who in the list of attendees or the
  130. * organizer we are sending this message on behalf of.
  131. *
  132. * It's possible to specify the current user as an array, in case the user
  133. * has more than one identifying href (such as multiple emails).
  134. *
  135. * It $oldCalendar is specified, it is assumed that the operation is
  136. * updating an existing event, which means that we need to look at the
  137. * differences between events, and potentially send old attendees
  138. * cancellations, and current attendees updates.
  139. *
  140. * If $calendar is null, but $oldCalendar is specified, we treat the
  141. * operation as if the user has deleted an event. If the user was an
  142. * organizer, this means that we need to send cancellation notices to
  143. * people. If the user was an attendee, we need to make sure that the
  144. * organizer gets the 'declined' message.
  145. *
  146. * @param VCalendar|string $calendar
  147. * @param string|array $userHref
  148. * @param VCalendar|string $oldCalendar
  149. * @return array
  150. */
  151. public function parseEvent($calendar = null, $userHref, $oldCalendar = null) {
  152. if ($oldCalendar) {
  153. if (is_string($oldCalendar)) {
  154. $oldCalendar = Reader::read($oldCalendar);
  155. }
  156. if (!isset($oldCalendar->VEVENT)) {
  157. // We only support events at the moment
  158. return array();
  159. }
  160. $oldEventInfo = $this->parseEventInfo($oldCalendar);
  161. } else {
  162. $oldEventInfo = array(
  163. 'organizer' => null,
  164. 'significantChangeHash' => '',
  165. 'attendees' => array(),
  166. );
  167. }
  168. $userHref = (array)$userHref;
  169. if (!is_null($calendar)) {
  170. if (is_string($calendar)) {
  171. $calendar = Reader::read($calendar);
  172. }
  173. if (!isset($calendar->VEVENT)) {
  174. // We only support events at the moment
  175. return array();
  176. }
  177. $eventInfo = $this->parseEventInfo($calendar);
  178. if (!$eventInfo['attendees'] && !$oldEventInfo['attendees']) {
  179. // If there were no attendees on either side of the equation,
  180. // we don't need to do anything.
  181. return array();
  182. }
  183. if (!$eventInfo['organizer'] && !$oldEventInfo['organizer']) {
  184. // There was no organizer before or after the change.
  185. return array();
  186. }
  187. $baseCalendar = $calendar;
  188. // If the new object didn't have an organizer, the organizer
  189. // changed the object from a scheduling object to a non-scheduling
  190. // object. We just copy the info from the old object.
  191. if (!$eventInfo['organizer'] && $oldEventInfo['organizer']) {
  192. $eventInfo['organizer'] = $oldEventInfo['organizer'];
  193. $eventInfo['organizerName'] = $oldEventInfo['organizerName'];
  194. }
  195. } else {
  196. // The calendar object got deleted, we need to process this as a
  197. // cancellation / decline.
  198. if (!$oldCalendar) {
  199. // No old and no new calendar, there's no thing to do.
  200. return array();
  201. }
  202. $eventInfo = $oldEventInfo;
  203. if (in_array($eventInfo['organizer'], $userHref)) {
  204. // This is an organizer deleting the event.
  205. $eventInfo['attendees'] = array();
  206. // Increasing the sequence, but only if the organizer deleted
  207. // the event.
  208. $eventInfo['sequence']++;
  209. } else {
  210. // This is an attendee deleting the event.
  211. foreach($eventInfo['attendees'] as $key=>$attendee) {
  212. if (in_array($attendee['href'], $userHref)) {
  213. $eventInfo['attendees'][$key]['instances'] = array('master' =>
  214. array('id'=>'master', 'partstat' => 'DECLINED')
  215. );
  216. }
  217. }
  218. }
  219. $baseCalendar = $oldCalendar;
  220. }
  221. if (in_array($eventInfo['organizer'], $userHref)) {
  222. return $this->parseEventForOrganizer($baseCalendar, $eventInfo, $oldEventInfo);
  223. } elseif ($oldCalendar) {
  224. // We need to figure out if the user is an attendee, but we're only
  225. // doing so if there's an oldCalendar, because we only want to
  226. // process updates, not creation of new events.
  227. foreach($eventInfo['attendees'] as $attendee) {
  228. if (in_array($attendee['href'], $userHref)) {
  229. return $this->parseEventForAttendee($baseCalendar, $eventInfo, $oldEventInfo, $attendee['href']);
  230. }
  231. }
  232. }
  233. return array();
  234. }
  235. /**
  236. * Processes incoming REQUEST messages.
  237. *
  238. * This is message from an organizer, and is either a new event
  239. * invite, or an update to an existing one.
  240. *
  241. *
  242. * @param Message $itipMessage
  243. * @param VCalendar $existingObject
  244. * @return VCalendar|null
  245. */
  246. protected function processMessageRequest(Message $itipMessage, VCalendar $existingObject = null) {
  247. if (!$existingObject) {
  248. // This is a new invite, and we're just going to copy over
  249. // all the components from the invite.
  250. $existingObject = new VCalendar();
  251. foreach($itipMessage->message->getComponents() as $component) {
  252. $existingObject->add(clone $component);
  253. }
  254. } else {
  255. // We need to update an existing object with all the new
  256. // information. We can just remove all existing components
  257. // and create new ones.
  258. foreach($existingObject->getComponents() as $component) {
  259. $existingObject->remove($component);
  260. }
  261. foreach($itipMessage->message->getComponents() as $component) {
  262. $existingObject->add(clone $component);
  263. }
  264. }
  265. return $existingObject;
  266. }
  267. /**
  268. * Processes incoming CANCEL messages.
  269. *
  270. * This is a message from an organizer, and means that either an
  271. * attendee got removed from an event, or an event got cancelled
  272. * altogether.
  273. *
  274. * @param Message $itipMessage
  275. * @param VCalendar $existingObject
  276. * @return VCalendar|null
  277. */
  278. protected function processMessageCancel(Message $itipMessage, VCalendar $existingObject = null) {
  279. if (!$existingObject) {
  280. // The event didn't exist in the first place, so we're just
  281. // ignoring this message.
  282. } else {
  283. foreach($existingObject->VEVENT as $vevent) {
  284. $vevent->STATUS = 'CANCELLED';
  285. $vevent->SEQUENCE = $itipMessage->sequence;
  286. }
  287. }
  288. return $existingObject;
  289. }
  290. /**
  291. * Processes incoming REPLY messages.
  292. *
  293. * The message is a reply. This is for example an attendee telling
  294. * an organizer he accepted the invite, or declined it.
  295. *
  296. * @param Message $itipMessage
  297. * @param VCalendar $existingObject
  298. * @return VCalendar|null
  299. */
  300. protected function processMessageReply(Message $itipMessage, VCalendar $existingObject = null) {
  301. // A reply can only be processed based on an existing object.
  302. // If the object is not available, the reply is ignored.
  303. if (!$existingObject) {
  304. return null;
  305. }
  306. $instances = array();
  307. $requestStatus = '2.0;Success';
  308. // Finding all the instances the attendee replied to.
  309. foreach($itipMessage->message->VEVENT as $vevent) {
  310. $recurId = isset($vevent->{'RECURRENCE-ID'})?$vevent->{'RECURRENCE-ID'}->getValue():'master';
  311. $attendee = $vevent->ATTENDEE;
  312. $instances[$recurId] = $attendee['PARTSTAT']->getValue();
  313. if (isset($vevent->{'REQUEST-STATUS'})) {
  314. $requestStatus = $vevent->{'REQUEST-STATUS'}->getValue();
  315. }
  316. }
  317. // Now we need to loop through the original organizer event, to find
  318. // all the instances where we have a reply for.
  319. $masterObject = null;
  320. foreach($existingObject->VEVENT as $vevent) {
  321. $recurId = isset($vevent->{'RECURRENCE-ID'})?$vevent->{'RECURRENCE-ID'}->getValue():'master';
  322. if ($recurId==='master') {
  323. $masterObject = $vevent;
  324. }
  325. if (isset($instances[$recurId])) {
  326. $attendeeFound = false;
  327. if (isset($vevent->ATTENDEE)) {
  328. foreach($vevent->ATTENDEE as $attendee) {
  329. if ($attendee->getValue() === $itipMessage->sender) {
  330. $attendeeFound = true;
  331. $attendee['PARTSTAT'] = $instances[$recurId];
  332. $attendee['SCHEDULE-STATUS'] = $requestStatus;
  333. // Un-setting the RSVP status, because we now know
  334. // that the attende already replied.
  335. unset($attendee['RSVP']);
  336. break;
  337. }
  338. }
  339. }
  340. if (!$attendeeFound) {
  341. // Adding a new attendee. The iTip documentation calls this
  342. // a party crasher.
  343. $attendee = $vevent->add('ATTENDEE', $itipMessage->sender, array(
  344. 'PARTSTAT' => $instances[$recurId]
  345. ));
  346. if ($itipMessage->senderName) $attendee['CN'] = $itipMessage->senderName;
  347. }
  348. unset($instances[$recurId]);
  349. }
  350. }
  351. if(!$masterObject) {
  352. // No master object, we can't add new instances.
  353. return null;
  354. }
  355. // If we got replies to instances that did not exist in the
  356. // original list, it means that new exceptions must be created.
  357. foreach($instances as $recurId=>$partstat) {
  358. $recurrenceIterator = new EventIterator($existingObject, $itipMessage->uid);
  359. $found = false;
  360. $iterations = 1000;
  361. do {
  362. $newObject = $recurrenceIterator->getEventObject();
  363. $recurrenceIterator->next();
  364. if (isset($newObject->{'RECURRENCE-ID'}) && $newObject->{'RECURRENCE-ID'}->getValue()===$recurId) {
  365. $found = true;
  366. }
  367. $iterations--;
  368. } while($recurrenceIterator->valid() && !$found && $iterations);
  369. // Invalid recurrence id. Skipping this object.
  370. if (!$found) continue;
  371. unset(
  372. $newObject->RRULE,
  373. $newObject->EXDATE,
  374. $newObject->RDATE
  375. );
  376. $attendeeFound = false;
  377. if (isset($newObject->ATTENDEE)) {
  378. foreach($newObject->ATTENDEE as $attendee) {
  379. if ($attendee->getValue() === $itipMessage->sender) {
  380. $attendeeFound = true;
  381. $attendee['PARTSTAT'] = $partstat;
  382. break;
  383. }
  384. }
  385. }
  386. if (!$attendeeFound) {
  387. // Adding a new attendee
  388. $attendee = $newObject->add('ATTENDEE', $itipMessage->sender, array(
  389. 'PARTSTAT' => $partstat
  390. ));
  391. if ($itipMessage->senderName) {
  392. $attendee['CN'] = $itipMessage->senderName;
  393. }
  394. }
  395. $existingObject->add($newObject);
  396. }
  397. return $existingObject;
  398. }
  399. /**
  400. * This method is used in cases where an event got updated, and we
  401. * potentially need to send emails to attendees to let them know of updates
  402. * in the events.
  403. *
  404. * We will detect which attendees got added, which got removed and create
  405. * specific messages for these situations.
  406. *
  407. * @param VCalendar $calendar
  408. * @param array $eventInfo
  409. * @param array $oldEventInfo
  410. * @return array
  411. */
  412. protected function parseEventForOrganizer(VCalendar $calendar, array $eventInfo, array $oldEventInfo) {
  413. // Merging attendee lists.
  414. $attendees = array();
  415. foreach($oldEventInfo['attendees'] as $attendee) {
  416. $attendees[$attendee['href']] = array(
  417. 'href' => $attendee['href'],
  418. 'oldInstances' => $attendee['instances'],
  419. 'newInstances' => array(),
  420. 'name' => $attendee['name'],
  421. 'forceSend' => null,
  422. );
  423. }
  424. foreach($eventInfo['attendees'] as $attendee) {
  425. if (isset($attendees[$attendee['href']])) {
  426. $attendees[$attendee['href']]['name'] = $attendee['name'];
  427. $attendees[$attendee['href']]['newInstances'] = $attendee['instances'];
  428. $attendees[$attendee['href']]['forceSend'] = $attendee['forceSend'];
  429. } else {
  430. $attendees[$attendee['href']] = array(
  431. 'href' => $attendee['href'],
  432. 'oldInstances' => array(),
  433. 'newInstances' => $attendee['instances'],
  434. 'name' => $attendee['name'],
  435. 'forceSend' => $attendee['forceSend'],
  436. );
  437. }
  438. }
  439. $messages = array();
  440. foreach($attendees as $attendee) {
  441. // An organizer can also be an attendee. We should not generate any
  442. // messages for those.
  443. if ($attendee['href']===$eventInfo['organizer']) {
  444. continue;
  445. }
  446. $message = new Message();
  447. $message->uid = $eventInfo['uid'];
  448. $message->component = 'VEVENT';
  449. $message->sequence = $eventInfo['sequence'];
  450. $message->sender = $eventInfo['organizer'];
  451. $message->senderName = $eventInfo['organizerName'];
  452. $message->recipient = $attendee['href'];
  453. $message->recipientName = $attendee['name'];
  454. if (!$attendee['newInstances']) {
  455. // If there are no instances the attendee is a part of, it
  456. // means the attendee was removed and we need to send him a
  457. // CANCEL.
  458. $message->method = 'CANCEL';
  459. // Creating the new iCalendar body.
  460. $icalMsg = new VCalendar();
  461. $icalMsg->METHOD = $message->method;
  462. $event = $icalMsg->add('VEVENT', array(
  463. 'UID' => $message->uid,
  464. 'SEQUENCE' => $message->sequence,
  465. ));
  466. if (isset($calendar->VEVENT->SUMMARY)) {
  467. $event->add('SUMMARY', $calendar->VEVENT->SUMMARY->getValue());
  468. }
  469. $event->add(clone $calendar->VEVENT->DTSTART);
  470. $org = $event->add('ORGANIZER', $eventInfo['organizer']);
  471. if ($eventInfo['organizerName']) $org['CN'] = $eventInfo['organizerName'];
  472. $event->add('ATTENDEE', $attendee['href'], array(
  473. 'CN' => $attendee['name'],
  474. ));
  475. $message->significantChange = true;
  476. } else {
  477. // The attendee gets the updated event body
  478. $message->method = 'REQUEST';
  479. // Creating the new iCalendar body.
  480. $icalMsg = new VCalendar();
  481. $icalMsg->METHOD = $message->method;
  482. foreach($calendar->select('VTIMEZONE') as $timezone) {
  483. $icalMsg->add(clone $timezone);
  484. }
  485. // We need to find out that this change is significant. If it's
  486. // not, systems may opt to not send messages.
  487. //
  488. // We do this based on the 'significantChangeHash' which is
  489. // some value that changes if there's a certain set of
  490. // properties changed in the event, or simply if there's a
  491. // difference in instances that the attendee is invited to.
  492. $message->significantChange =
  493. $attendee['forceSend'] === 'REQUEST' ||
  494. array_keys($attendee['oldInstances']) != array_keys($attendee['newInstances']) ||
  495. $oldEventInfo['significantChangeHash']!==$eventInfo['significantChangeHash'];
  496. foreach($attendee['newInstances'] as $instanceId => $instanceInfo) {
  497. $currentEvent = clone $eventInfo['instances'][$instanceId];
  498. if ($instanceId === 'master') {
  499. // We need to find a list of events that the attendee
  500. // is not a part of to add to the list of exceptions.
  501. $exceptions = array();
  502. foreach($eventInfo['instances'] as $instanceId=>$vevent) {
  503. if (!isset($attendee['newInstances'][$instanceId])) {
  504. $exceptions[] = $instanceId;
  505. }
  506. }
  507. // If there were exceptions, we need to add it to an
  508. // existing EXDATE property, if it exists.
  509. if ($exceptions) {
  510. if (isset($currentEvent->EXDATE)) {
  511. $currentEvent->EXDATE->setParts(array_merge(
  512. $currentEvent->EXDATE->getParts(),
  513. $exceptions
  514. ));
  515. } else {
  516. $currentEvent->EXDATE = $exceptions;
  517. }
  518. }
  519. // Cleaning up any scheduling information that
  520. // shouldn't be sent along.
  521. unset($currentEvent->ORGANIZER['SCHEDULE-FORCE-SEND']);
  522. unset($currentEvent->ORGANIZER['SCHEDULE-STATUS']);
  523. foreach($currentEvent->ATTENDEE as $attendee) {
  524. unset($attendee['SCHEDULE-FORCE-SEND']);
  525. unset($attendee['SCHEDULE-STATUS']);
  526. // We're adding PARTSTAT=NEEDS-ACTION to ensure that
  527. // iOS shows an "Inbox Item"
  528. if (!isset($attendee['PARTSTAT'])) {
  529. $attendee['PARTSTAT'] = 'NEEDS-ACTION';
  530. }
  531. }
  532. }
  533. $icalMsg->add($currentEvent);
  534. }
  535. }
  536. $message->message = $icalMsg;
  537. $messages[] = $message;
  538. }
  539. return $messages;
  540. }
  541. /**
  542. * Parse an event update for an attendee.
  543. *
  544. * This function figures out if we need to send a reply to an organizer.
  545. *
  546. * @param VCalendar $calendar
  547. * @param array $eventInfo
  548. * @param array $oldEventInfo
  549. * @param string $attendee
  550. * @return Message[]
  551. */
  552. protected function parseEventForAttendee(VCalendar $calendar, array $eventInfo, array $oldEventInfo, $attendee) {
  553. if ($this->scheduleAgentServerRules && $eventInfo['organizerScheduleAgent']==='CLIENT') {
  554. return array();
  555. }
  556. // Don't bother generating messages for events that have already been
  557. // cancelled.
  558. if ($eventInfo['status']==='CANCELLED') {
  559. return array();
  560. }
  561. $instances = array();
  562. foreach($oldEventInfo['attendees'][$attendee]['instances'] as $instance) {
  563. $instances[$instance['id']] = array(
  564. 'id' => $instance['id'],
  565. 'oldstatus' => $instance['partstat'],
  566. 'newstatus' => null,
  567. );
  568. }
  569. foreach($eventInfo['attendees'][$attendee]['instances'] as $instance) {
  570. if (isset($instances[$instance['id']])) {
  571. $instances[$instance['id']]['newstatus'] = $instance['partstat'];
  572. } else {
  573. $instances[$instance['id']] = array(
  574. 'id' => $instance['id'],
  575. 'oldstatus' => null,
  576. 'newstatus' => $instance['partstat'],
  577. );
  578. }
  579. }
  580. // We need to also look for differences in EXDATE. If there are new
  581. // items in EXDATE, it means that an attendee deleted instances of an
  582. // event, which means we need to send DECLINED specifically for those
  583. // instances.
  584. // We only need to do that though, if the master event is not declined.
  585. if ($instances['master']['newstatus'] !== 'DECLINED') {
  586. foreach($eventInfo['exdate'] as $exDate) {
  587. if (!in_array($exDate, $oldEventInfo['exdate'])) {
  588. if (isset($instances[$exDate])) {
  589. $instances[$exDate]['newstatus'] = 'DECLINED';
  590. } else {
  591. $instances[$exDate] = array(
  592. 'id' => $exDate,
  593. 'oldstatus' => null,
  594. 'newstatus' => 'DECLINED',
  595. );
  596. }
  597. }
  598. }
  599. }
  600. // Gathering a few extra properties for each instance.
  601. foreach($instances as $recurId=>$instanceInfo) {
  602. if (isset($eventInfo['instances'][$recurId])) {
  603. $instances[$recurId]['dtstart'] = clone $eventInfo['instances'][$recurId]->DTSTART;
  604. } else {
  605. $instances[$recurId]['dtstart'] = $recurId;
  606. }
  607. }
  608. $message = new Message();
  609. $message->uid = $eventInfo['uid'];
  610. $message->method = 'REPLY';
  611. $message->component = 'VEVENT';
  612. $message->sequence = $eventInfo['sequence'];
  613. $message->sender = $attendee;
  614. $message->senderName = $eventInfo['attendees'][$attendee]['name'];
  615. $message->recipient = $eventInfo['organizer'];
  616. $message->recipientName = $eventInfo['organizerName'];
  617. $icalMsg = new VCalendar();
  618. $icalMsg->METHOD = 'REPLY';
  619. $hasReply = false;
  620. foreach($instances as $instance) {
  621. if ($instance['oldstatus']==$instance['newstatus'] && $eventInfo['organizerForceSend'] !== 'REPLY') {
  622. // Skip
  623. continue;
  624. }
  625. $event = $icalMsg->add('VEVENT', array(
  626. 'UID' => $message->uid,
  627. 'SEQUENCE' => $message->sequence,
  628. ));
  629. $summary = isset($calendar->VEVENT->SUMMARY)?$calendar->VEVENT->SUMMARY->getValue():'';
  630. // Adding properties from the correct source instance
  631. if (isset($eventInfo['instances'][$instance['id']])) {
  632. $instanceObj = $eventInfo['instances'][$instance['id']];
  633. $event->add(clone $instanceObj->DTSTART);
  634. if (isset($instanceObj->SUMMARY)) {
  635. $event->add('SUMMARY', $instanceObj->SUMMARY->getValue());
  636. } elseif ($summary) {
  637. $event->add('SUMMARY', $summary);
  638. }
  639. } else {
  640. // This branch of the code is reached, when a reply is
  641. // generated for an instance of a recurring event, through the
  642. // fact that the instance has disappeared by showing up in
  643. // EXDATE
  644. $dt = DateTimeParser::parse($instance['id'], $eventInfo['timezone']);
  645. // Treat is as a DATE field
  646. if (strlen($instance['id']) <= 8) {
  647. $recur = $event->add('DTSTART', $dt, array('VALUE' => 'DATE'));
  648. } else {
  649. $recur = $event->add('DTSTART', $dt);
  650. }
  651. if ($summary) {
  652. $event->add('SUMMARY', $summary);
  653. }
  654. }
  655. if ($instance['id'] !== 'master') {
  656. $dt = DateTimeParser::parse($instance['id'], $eventInfo['timezone']);
  657. // Treat is as a DATE field
  658. if (strlen($instance['id']) <= 8) {
  659. $recur = $event->add('RECURRENCE-ID', $dt, array('VALUE' => 'DATE'));
  660. } else {
  661. $recur = $event->add('RECURRENCE-ID', $dt);
  662. }
  663. }
  664. $organizer = $event->add('ORGANIZER', $message->recipient);
  665. if ($message->recipientName) {
  666. $organizer['CN'] = $message->recipientName;
  667. }
  668. $attendee = $event->add('ATTENDEE', $message->sender, array(
  669. 'PARTSTAT' => $instance['newstatus']
  670. ));
  671. if ($message->senderName) {
  672. $attendee['CN'] = $message->senderName;
  673. }
  674. $hasReply = true;
  675. }
  676. if ($hasReply) {
  677. $message->message = $icalMsg;
  678. return array($message);
  679. } else {
  680. return array();
  681. }
  682. }
  683. /**
  684. * Returns attendee information and information about instances of an
  685. * event.
  686. *
  687. * Returns an array with the following keys:
  688. *
  689. * 1. uid
  690. * 2. organizer
  691. * 3. organizerName
  692. * 4. attendees
  693. * 5. instances
  694. *
  695. * @param VCalendar $calendar
  696. * @return array
  697. */
  698. protected function parseEventInfo(VCalendar $calendar = null) {
  699. $uid = null;
  700. $organizer = null;
  701. $organizerName = null;
  702. $organizerForceSend = null;
  703. $sequence = null;
  704. $timezone = null;
  705. $status = null;
  706. $organizerScheduleAgent = 'SERVER';
  707. $significantChangeHash = '';
  708. // Now we need to collect a list of attendees, and which instances they
  709. // are a part of.
  710. $attendees = array();
  711. $instances = array();
  712. $exdate = array();
  713. foreach($calendar->VEVENT as $vevent) {
  714. if (is_null($uid)) {
  715. $uid = $vevent->UID->getValue();
  716. } else {
  717. if ($uid !== $vevent->UID->getValue()) {
  718. throw new ITipException('If a calendar contained more than one event, they must have the same UID.');
  719. }
  720. }
  721. if (!isset($vevent->DTSTART)) {
  722. throw new ITipException('An event MUST have a DTSTART property.');
  723. }
  724. if (isset($vevent->ORGANIZER)) {
  725. if (is_null($organizer)) {
  726. $organizer = $vevent->ORGANIZER->getNormalizedValue();
  727. $organizerName = isset($vevent->ORGANIZER['CN'])?$vevent->ORGANIZER['CN']:null;
  728. } else {
  729. if ($organizer !== $vevent->ORGANIZER->getNormalizedValue()) {
  730. throw new SameOrganizerForAllComponentsException('Every instance of the event must have the same organizer.');
  731. }
  732. }
  733. $organizerForceSend =
  734. isset($vevent->ORGANIZER['SCHEDULE-FORCE-SEND']) ?
  735. strtoupper($vevent->ORGANIZER['SCHEDULE-FORCE-SEND']) :
  736. null;
  737. $organizerScheduleAgent =
  738. isset($vevent->ORGANIZER['SCHEDULE-AGENT']) ?
  739. strtoupper((string)$vevent->ORGANIZER['SCHEDULE-AGENT']) :
  740. 'SERVER';
  741. }
  742. if (is_null($sequence) && isset($vevent->SEQUENCE)) {
  743. $sequence = $vevent->SEQUENCE->getValue();
  744. }
  745. if (isset($vevent->EXDATE)) {
  746. $exdate = $vevent->EXDATE->getParts();
  747. }
  748. if (isset($vevent->STATUS)) {
  749. $status = strtoupper($vevent->STATUS->getValue());
  750. }
  751. $recurId = isset($vevent->{'RECURRENCE-ID'})?$vevent->{'RECURRENCE-ID'}->getValue():'master';
  752. if ($recurId==='master') {
  753. $timezone = $vevent->DTSTART->getDateTime()->getTimeZone();
  754. }
  755. if(isset($vevent->ATTENDEE)) {
  756. foreach($vevent->ATTENDEE as $attendee) {
  757. if ($this->scheduleAgentServerRules &&
  758. isset($attendee['SCHEDULE-AGENT']) &&
  759. strtoupper($attendee['SCHEDULE-AGENT']->getValue()) === 'CLIENT'
  760. ) {
  761. continue;
  762. }
  763. $partStat =
  764. isset($attendee['PARTSTAT']) ?
  765. strtoupper($attendee['PARTSTAT']) :
  766. 'NEEDS-ACTION';
  767. $forceSend =
  768. isset($attendee['SCHEDULE-FORCE-SEND']) ?
  769. strtoupper($attendee['SCHEDULE-FORCE-SEND']) :
  770. null;
  771. if (isset($attendees[$attendee->getNormalizedValue()])) {
  772. $attendees[$attendee->getNormalizedValue()]['instances'][$recurId] = array(
  773. 'id' => $recurId,
  774. 'partstat' => $partStat,
  775. 'force-send' => $forceSend,
  776. );
  777. } else {
  778. $attendees[$attendee->getNormalizedValue()] = array(
  779. 'href' => $attendee->getNormalizedValue(),
  780. 'instances' => array(
  781. $recurId => array(
  782. 'id' => $recurId,
  783. 'partstat' => $partStat,
  784. ),
  785. ),
  786. 'name' => isset($attendee['CN'])?(string)$attendee['CN']:null,
  787. 'forceSend' => $forceSend,
  788. );
  789. }
  790. }
  791. $instances[$recurId] = $vevent;
  792. }
  793. foreach($this->significantChangeProperties as $prop) {
  794. if (isset($vevent->$prop)) {
  795. $significantChangeHash.=$prop.':';
  796. foreach($vevent->select($prop) as $val) {
  797. $significantChangeHash.= $val->getValue().';';
  798. }
  799. }
  800. }
  801. }
  802. $significantChangeHash = md5($significantChangeHash);
  803. return compact(
  804. 'uid',
  805. 'organizer',
  806. 'organizerName',
  807. 'organizerScheduleAgent',
  808. 'organizerForceSend',
  809. 'instances',
  810. 'attendees',
  811. 'sequence',
  812. 'exdate',
  813. 'timezone',
  814. 'significantChangeHash',
  815. 'status'
  816. );
  817. }
  818. }