1<?php
2
3namespace SabreForRainLoop\CalDAV\Schedule;
4
5use SabreForRainLoop\VObject;
6use SabreForRainLoop\DAV;
7
8/**
9 * iMIP handler.
10 *
11 * This class is responsible for sending out iMIP messages. iMIP is the
12 * email-based transport for iTIP. iTIP deals with scheduling operations for
13 * iCalendar objects.
14 *
15 * If you want to customize the email that gets sent out, you can do so by
16 * extending this class and overriding the sendMessage method.
17 *
18 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
19 * @author Evert Pot (http://evertpot.com/)
20 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
21 */
22class IMip {
23
24    /**
25     * Email address used in From: header.
26     *
27     * @var string
28     */
29    protected $senderEmail;
30
31    /**
32     * Creates the email handler.
33     *
34     * @param string $senderEmail. The 'senderEmail' is the email that shows up
35     *                             in the 'From:' address. This should
36     *                             generally be some kind of no-reply email
37     *                             address you own.
38     */
39    public function __construct($senderEmail) {
40
41        $this->senderEmail = $senderEmail;
42
43    }
44
45    /**
46     * Sends one or more iTip messages through email.
47     *
48     * @param string $originator Originator Email
49     * @param array $recipients Array of email addresses
50     * @param VObject\Component $vObject
51     * @param string $principal Principal Url of the originator
52     * @return void
53     */
54    public function sendMessage($originator, array $recipients, VObject\Component $vObject, $principal) {
55
56        foreach($recipients as $recipient) {
57
58            $to = $recipient;
59            $replyTo = $originator;
60            $subject = 'SabreDAV iTIP message';
61
62            switch(strtoupper($vObject->METHOD)) {
63                case 'REPLY' :
64                    $subject = 'Response for: ' . $vObject->VEVENT->SUMMARY;
65                    break;
66                case 'REQUEST' :
67                    $subject = 'Invitation for: ' .$vObject->VEVENT->SUMMARY;
68                    break;
69                case 'CANCEL' :
70                    $subject = 'Cancelled event: ' . $vObject->VEVENT->SUMMARY;
71                    break;
72            }
73
74            $headers = array();
75            $headers[] = 'Reply-To: ' . $replyTo;
76            $headers[] = 'From: ' . $this->senderEmail;
77            $headers[] = 'Content-Type: text/calendar; method=' . (string)$vObject->method . '; charset=utf-8';
78            if (DAV\Server::$exposeVersion) {
79                $headers[] = 'X-Sabre-Version: ' . DAV\Version::VERSION . '-' . DAV\Version::STABILITY;
80            }
81
82            $vcalBody = $vObject->serialize();
83
84            $this->mail($to, $subject, $vcalBody, $headers);
85
86        }
87
88    }
89
90    // @codeCoverageIgnoreStart
91    // This is deemed untestable in a reasonable manner
92
93    /**
94     * This function is reponsible for sending the actual email.
95     *
96     * @param string $to Recipient email address
97     * @param string $subject Subject of the email
98     * @param string $body iCalendar body
99     * @param array $headers List of headers
100     * @return void
101     */
102    protected function mail($to, $subject, $body, array $headers) {
103
104
105        mail($to, $subject, $body, implode("\r\n", $headers));
106
107    }
108
109    // @codeCoverageIgnoreEnd
110
111}
112