summaryrefslogtreecommitdiff
path: root/lib/Kolab/CalDAV/IMip.php
blob: c08f37adc9fef17290c3e8c34abd02496d9fc363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

/**
 * Extended CalDAV IMip handler for the Kolab DAV server
 *
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2013, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

namespace Kolab\CalDAV;

use \rcube;
use \rcube_utils;
use \Mail_mime;

use Sabre\VObject;
use Sabre\CalDAV;
use Sabre\DAV;

/**
 * iMIP handler.
 *
 * This class is responsible for sending out iMIP messages. iMIP is the
 * email-based transport for iTIP. iTIP deals with scheduling operations for
 * iCalendar objects.
 *
 * If you want to customize the email that gets sent out, you can do so by
 * extending this class and overriding the sendMessage method.
 *
 * @copyright Copyright (C) 2007-2013 Rooftop Solutions. All rights reserved.
 * @author Evert Pot (http://www.rooftopsolutions.nl/)
 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
 */
class IMip extends CalDAV\Schedule\IMip
{
    public function __construct()
    {
        
    }
    
    /**
     * Sends one or more iTip messages through email.
     *
     * @param string $originator Originator Email
     * @param array $recipients Array of email addresses
     * @param VObject\Component $vObject
     * @param string $principal Principal Url of the originator
     * @return void
     */
    public function sendMessage($originator, array $recipients, VObject\Component $vObject, $principal)
    {
        $ics = $vObject->serialize();
        console(__METHOD__, $originator, $recipients, $principal, $ics);

        $rcube = rcube::get_instance();
        $sender = $rcube->user->get_identity();
        $sender_email = $sender['email'] ?: $rcube->get_user_email();
        $sender_name  = $sender['name']  ?: $rcube->get_user_name();

        foreach($recipients as $recipient) {
            $subject = 'KolabDAV iTIP message';
            switch (strtoupper($vObject->METHOD)) {
                case 'REPLY' :
                    $subject = 'Response for: ' . $vObject->VEVENT->SUMMARY;
                    break;
                case 'REQUEST' :
                    $subject = 'Invitation for: ' .$vObject->VEVENT->SUMMARY;
                    break;
                case 'CANCEL' :
                    $subject = 'Cancelled event: ' . $vObject->VEVENT->SUMMARY;
                    break;
            }

            $sender = rcube_utils::idn_to_ascii($sender_email);
            $from = format_email_recipient($sender, $sender_name);
            $mailto = rcube_utils::idn_to_ascii($recipient);

            // compose multipart message using PEAR:Mail_Mime
            $message = new Mail_mime("\r\n");
            $message->setParam('text_encoding', 'quoted-printable');
            $message->setParam('head_encoding', 'quoted-printable');
            $message->setParam('head_charset', RCUBE_CHARSET);
            $message->setParam('text_charset', RCUBE_CHARSET . ";\r\n format=flowed");

            // compose common headers array
            $headers = array(
                'To' => $mailto,
                'From' => $from,
                'Date' => date('r'),
                'Reply-To' => $originator,
                'Message-ID' => $rcube->gen_message_id(),
                'X-Sender' => $sender,
                'Subject' => $subject,
            );
            if ($agent = $rcube->config->get('useragent'))
                $headers['User-Agent'] = $agent;

            $message->headers($headers);
            $message->setContentType('text/calendar', array('method' => strval($vObject->method), 'charset' => RCUBE_CHARSET));
            $message->setTXTBody($ics);

            // send message through Roundcube's SMTP feature
            if (!$rcube->deliver_message($message, $sender, $mailto, $smtp_error)) {
                rcube::raise_error(array(
                    'code' => 600, 'type' => 'php',
                    'file' => __FILE__, 'line' => __LINE__,
                    'message' => "Failed to send iTIP message to " . $mailto),
                    true, false);
            }
        }
    }

    /**
     * This function is reponsible for sending the actual email.
     *
     * @param string $to Recipient email address
     * @param string $subject Subject of the email
     * @param string $body iCalendar body
     * @param array $headers List of headers
     * @return void
     */
    protected function mail($to, $subject, $body, array $headers)
    {
        //mail($to, $subject, $body, implode("\r\n", $headers));
    }

}