IMipPlugin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CalDAV\Schedule;
  4. use Sabre\DAV;
  5. use Sabre\VObject\ITip;
  6. /**
  7. * iMIP handler.
  8. *
  9. * This class is responsible for sending out iMIP messages. iMIP is the
  10. * email-based transport for iTIP. iTIP deals with scheduling operations for
  11. * iCalendar objects.
  12. *
  13. * If you want to customize the email that gets sent out, you can do so by
  14. * extending this class and overriding the sendMessage method.
  15. *
  16. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  17. * @author Evert Pot (http://evertpot.com/)
  18. * @license http://sabre.io/license/ Modified BSD License
  19. */
  20. class IMipPlugin extends DAV\ServerPlugin
  21. {
  22. /**
  23. * Email address used in From: header.
  24. *
  25. * @var string
  26. */
  27. protected $senderEmail;
  28. /**
  29. * Creates the email handler.
  30. *
  31. * @param string $senderEmail. The 'senderEmail' is the email that shows up
  32. * in the 'From:' address. This should
  33. * generally be some kind of no-reply email
  34. * address you own.
  35. */
  36. public function __construct($senderEmail)
  37. {
  38. $this->senderEmail = $senderEmail;
  39. }
  40. /*
  41. * This initializes the plugin.
  42. *
  43. * This function is called by Sabre\DAV\Server, after
  44. * addPlugin is called.
  45. *
  46. * This method should set up the required event subscriptions.
  47. *
  48. * @param DAV\Server $server
  49. * @return void
  50. */
  51. public function initialize(DAV\Server $server)
  52. {
  53. $server->on('schedule', [$this, 'schedule'], 120);
  54. }
  55. /**
  56. * Returns a plugin name.
  57. *
  58. * Using this name other plugins will be able to access other plugins
  59. * using \Sabre\DAV\Server::getPlugin
  60. *
  61. * @return string
  62. */
  63. public function getPluginName()
  64. {
  65. return 'imip';
  66. }
  67. /**
  68. * Event handler for the 'schedule' event.
  69. */
  70. public function schedule(ITip\Message $iTipMessage)
  71. {
  72. // Not sending any emails if the system considers the update
  73. // insignificant.
  74. if (!$iTipMessage->significantChange) {
  75. if (!$iTipMessage->scheduleStatus) {
  76. $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email';
  77. }
  78. return;
  79. }
  80. $summary = $iTipMessage->message->VEVENT->SUMMARY;
  81. if ('mailto' !== parse_url($iTipMessage->sender, PHP_URL_SCHEME)) {
  82. return;
  83. }
  84. if ('mailto' !== parse_url($iTipMessage->recipient, PHP_URL_SCHEME)) {
  85. return;
  86. }
  87. $sender = substr($iTipMessage->sender, 7);
  88. $recipient = substr($iTipMessage->recipient, 7);
  89. if ($iTipMessage->senderName) {
  90. $sender = $iTipMessage->senderName.' <'.$sender.'>';
  91. }
  92. if ($iTipMessage->recipientName && $iTipMessage->recipientName != $recipient) {
  93. $recipient = $iTipMessage->recipientName.' <'.$recipient.'>';
  94. }
  95. $subject = 'SabreDAV iTIP message';
  96. switch (strtoupper($iTipMessage->method)) {
  97. case 'REPLY':
  98. $subject = 'Re: '.$summary;
  99. break;
  100. case 'REQUEST':
  101. $subject = 'Invitation: '.$summary;
  102. break;
  103. case 'CANCEL':
  104. $subject = 'Cancelled: '.$summary;
  105. break;
  106. }
  107. $headers = [
  108. 'Reply-To: '.$sender,
  109. 'From: '.$iTipMessage->senderName.' <'.$this->senderEmail.'>',
  110. 'MIME-Version: 1.0',
  111. 'Content-Type: text/calendar; charset=UTF-8; method='.$iTipMessage->method,
  112. ];
  113. if (DAV\Server::$exposeVersion) {
  114. $headers[] = 'X-Sabre-Version: '.DAV\Version::VERSION;
  115. }
  116. $this->mail(
  117. $recipient,
  118. $subject,
  119. $iTipMessage->message->serialize(),
  120. $headers
  121. );
  122. $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip';
  123. }
  124. // @codeCoverageIgnoreStart
  125. // This is deemed untestable in a reasonable manner
  126. /**
  127. * This function is responsible for sending the actual email.
  128. *
  129. * @param string $to Recipient email address
  130. * @param string $subject Subject of the email
  131. * @param string $body iCalendar body
  132. * @param array $headers List of headers
  133. */
  134. protected function mail($to, $subject, $body, array $headers)
  135. {
  136. mail($to, $subject, $body, implode("\r\n", $headers));
  137. }
  138. // @codeCoverageIgnoreEnd
  139. /**
  140. * Returns a bunch of meta-data about the plugin.
  141. *
  142. * Providing this information is optional, and is mainly displayed by the
  143. * Browser plugin.
  144. *
  145. * The description key in the returned array may contain html and will not
  146. * be sanitized.
  147. *
  148. * @return array
  149. */
  150. public function getPluginInfo()
  151. {
  152. return [
  153. 'name' => $this->getPluginName(),
  154. 'description' => 'Email delivery (rfc6047) for CalDAV scheduling',
  155. 'link' => 'http://sabre.io/dav/scheduling/',
  156. ];
  157. }
  158. }