IMipPlugin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace Sabre\CalDAV\Schedule;
  3. use Sabre\DAV;
  4. use Sabre\VObject\ITip;
  5. /**
  6. * iMIP handler.
  7. *
  8. * This class is responsible for sending out iMIP messages. iMIP is the
  9. * email-based transport for iTIP. iTIP deals with scheduling operations for
  10. * iCalendar objects.
  11. *
  12. * If you want to customize the email that gets sent out, you can do so by
  13. * extending this class and overriding the sendMessage method.
  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 IMipPlugin extends DAV\ServerPlugin {
  20. /**
  21. * Email address used in From: header.
  22. *
  23. * @var string
  24. */
  25. protected $senderEmail;
  26. /**
  27. * ITipMessage
  28. *
  29. * @var ITip\Message
  30. */
  31. protected $itipMessage;
  32. /**
  33. * Creates the email handler.
  34. *
  35. * @param string $senderEmail. The 'senderEmail' is the email that shows up
  36. * in the 'From:' address. This should
  37. * generally be some kind of no-reply email
  38. * address you own.
  39. */
  40. function __construct($senderEmail) {
  41. $this->senderEmail = $senderEmail;
  42. }
  43. /*
  44. * This initializes the plugin.
  45. *
  46. * This function is called by Sabre\DAV\Server, after
  47. * addPlugin is called.
  48. *
  49. * This method should set up the required event subscriptions.
  50. *
  51. * @param DAV\Server $server
  52. * @return void
  53. */
  54. function initialize(DAV\Server $server) {
  55. $server->on('schedule', [$this, 'schedule'], 120);
  56. }
  57. /**
  58. * Returns a plugin name.
  59. *
  60. * Using this name other plugins will be able to access other plugins
  61. * using \Sabre\DAV\Server::getPlugin
  62. *
  63. * @return string
  64. */
  65. function getPluginName() {
  66. return 'imip';
  67. }
  68. /**
  69. * Event handler for the 'schedule' event.
  70. *
  71. * @param ITip\Message $iTipMessage
  72. * @return void
  73. */
  74. function schedule(ITip\Message $iTipMessage) {
  75. // Not sending any emails if the system considers the update
  76. // insignificant.
  77. if (!$iTipMessage->significantChange) {
  78. if (!$iTipMessage->scheduleStatus) {
  79. $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email';
  80. }
  81. return;
  82. }
  83. $summary = $iTipMessage->message->VEVENT->SUMMARY;
  84. if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto')
  85. return;
  86. if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto')
  87. return;
  88. $sender = substr($iTipMessage->sender, 7);
  89. $recipient = substr($iTipMessage->recipient, 7);
  90. if ($iTipMessage->senderName) {
  91. $sender = $iTipMessage->senderName . ' <' . $sender . '>';
  92. }
  93. if ($iTipMessage->recipientName) {
  94. $recipient = $iTipMessage->recipientName . ' <' . $recipient . '>';
  95. }
  96. $subject = 'SabreDAV iTIP message';
  97. switch (strtoupper($iTipMessage->method)) {
  98. case 'REPLY' :
  99. $subject = 'Re: ' . $summary;
  100. break;
  101. case 'REQUEST' :
  102. $subject = $summary;
  103. break;
  104. case 'CANCEL' :
  105. $subject = 'Cancelled: ' . $summary;
  106. break;
  107. }
  108. $headers = [
  109. 'Reply-To: ' . $sender,
  110. 'From: ' . $this->senderEmail,
  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. * @return void
  134. */
  135. protected function mail($to, $subject, $body, array $headers) {
  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. function getPluginInfo() {
  151. return [
  152. 'name' => $this->getPluginName(),
  153. 'description' => 'Email delivery (rfc6037) for CalDAV scheduling',
  154. 'link' => 'http://sabre.io/dav/scheduling/',
  155. ];
  156. }
  157. }