summaryrefslogtreecommitdiff
path: root/plugins/calendar/lib/calendar_itip.php
blob: dc4cfa0f3f89af040c577aa02e6437b60d1619d0 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php

/**
 * iTIP functions for the Calendar plugin
 *
 * Class providing functionality to manage iTIP invitations
 *
 * @version @package_version@
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 * @package @package_name@
 *
 * Copyright (C) 2011, 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/>.
 */
class calendar_itip
{
  private $rc;
  private $cal;
  private $event;
  private $itip_send = false;

  function __construct($cal)
  {
    $this->cal = $cal;
    $this->rc = $cal->rc;
    $this->sender = $this->rc->user->get_identity();

    $this->cal->add_hook('smtp_connect', array($this, 'smtp_connect_hook'));
  }

  /**
   * Send an iTip mail message
   *
   * @param array   Event object to send
   * @param string  iTip method (REQUEST|REPLY|CANCEL)
   * @param array   Hash array with recipient data (name, email)
   * @param string  Mail subject
   * @param string  Mail body text label
   * @param object  Mail_mime object with message data
   * @return boolean True on success, false on failure
   */
  public function send_itip_message($event, $method, $recipient, $subject, $bodytext, $message = null)
  {
    if (!$this->sender['name'])
      $this->sender['name'] = $this->sender['email'];
    
    if (!$message)
      $message = $this->compose_itip_message($event, $method);
    
    $mailto = rcube_idn_to_ascii($recipient['email']);
    
    $headers = $message->headers();
    $headers['To'] = format_email_recipient($mailto, $recipient['name']);
    $headers['Subject'] = $this->cal->gettext(array(
      'name' => $subject,
      'vars' => array('title' => $event['title'], 'name' => $this->sender['name'])
    ));
    
    // compose a list of all event attendees
    $attendees_list = array();
    foreach ((array)$event['attendees'] as $attendee) {
      $attendees_list[] = ($attendee['name'] && $attendee['email']) ?
        $attendee['name'] . ' <' . $attendee['email'] . '>' :
        ($attendee['name'] ? $attendee['name'] : $attendee['email']);
    }
    
    $mailbody = $this->cal->gettext(array(
      'name' => $bodytext,
      'vars' => array(
        'title' => $event['title'],
        'date' => $this->cal->lib->event_date_text($event, true),
        'attendees' => join(', ', $attendees_list),
        'sender' => $this->sender['name'],
        'organizer' => $this->sender['name'],
      )
    ));
    
    // append links for direct invitation replies
    if ($method == 'REQUEST' && ($token = $this->store_invitation($event, $recipient['email']))) {
      $mailbody .= "\n\n" . $this->cal->gettext(array(
        'name' => 'invitationattendlinks',
        'vars' => array('url' => $this->cal->get_url(array('action' => 'attend', 't' => $token))),
      ));
    }
    else if ($method == 'CANCEL') {
      $this->cancel_itip_invitation($event);
    }
    
    $message->headers($headers, true);
    $message->setTXTBody(rcube_mime::format_flowed($mailbody, 79));

    // finally send the message
    $this->itip_send = true;
    $sent = $this->rc->deliver_message($message, $headers['X-Sender'], $mailto, $smtp_error);
    $this->itip_send = false;

    return $sent;
  }

  /**
   * Plugin hook to alter SMTP authentication.
   * This is used if iTip messages are to be sent from an unauthenticated session
   */
  public function smtp_connect_hook($p)
  {
    // replace smtp auth settings if we're not in an authenticated session
    if ($this->itip_send && !$this->rc->user->ID) {
      foreach (array('smtp_server', 'smtp_user', 'smtp_pass') as $prop) {
        $p[$prop] = $this->rc->config->get("calendar_itip_$prop", $p[$prop]);
      }
    }

    return $p;
  }

  /**
   * Helper function to build a Mail_mime object to send an iTip message
   *
   * @param array   Event object to send
   * @param string  iTip method (REQUEST|REPLY|CANCEL)
   * @return object Mail_mime object with message data
   */
  public function compose_itip_message($event, $method)
  {
    $from = rcube_idn_to_ascii($this->sender['email']);
    $sender = format_email_recipient($from, $this->sender['name']);
    
    // 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', RCMAIL_CHARSET);
    $message->setParam('text_charset', RCMAIL_CHARSET . ";\r\n format=flowed");
    
    // compose common headers array
    $headers = array(
      'From' => $sender,
      'Date' => $this->rc->user_date(),
      'Message-ID' => $this->rc->gen_message_id(),
      'X-Sender' => $from,
    );
    if ($agent = $this->rc->config->get('useragent'))
      $headers['User-Agent'] = $agent;
    
    $message->headers($headers);
    
    // attach ics file for this event
    $ical = $this->cal->get_ical();
    $ics = $ical->export(array($event), $method);
    $message->addAttachment($ics, 'text/calendar', 'event.ics', false, '8bit', 'attachment', RCMAIL_CHARSET . "; method=" . $method);
    
    return $message;
  }


  /**
   * Find invitation record by token
   *
   * @param string Invitation token
   * @return mixed Invitation record as hash array or False if not found
   */
  public function get_invitation($token)
  {
    if ($parts = $this->decode_token($token)) {
      $result = $this->rc->db->query("SELECT * FROM itipinvitations WHERE token=?", $parts['base']);
      if ($result && ($rec = $this->rc->db->fetch_assoc($result))) {
        $rec['event'] = unserialize($rec['event']);
        $rec['attendee'] = $parts['attendee'];
        return $rec;
      }
    }
    
    return false;
  }

  /**
   * Update the attendee status of the given invitation record
   *
   * @param array Invitation record as fetched with calendar_itip::get_invitation()
   * @param string Attendee email address
   * @param string New attendee status
   */
  public function update_invitation($invitation, $email, $newstatus)
  {
    if (is_string($invitation))
      $invitation = $this->get_invitation($invitation);
    
    if ($invitation['token'] && $invitation['event']) {
      // update attendee record in event data
      foreach ($invitation['event']['attendees'] as $i => $attendee) {
        if ($attendee['role'] == 'ORGANIZER') {
          $organizer = $attendee;
        }
        else if ($attendee['email'] == $email) {
          // nothing to be done here
          if ($attendee['status'] == $newstatus)
            return true;
          
          $invitation['event']['attendees'][$i]['status'] = $newstatus;
          $this->sender = $attendee;
        }
      }
      $invitation['event']['changed'] = time();
      
      // send iTIP REPLY message to organizer
      if ($organizer) {
        $status = strtolower($newstatus);
        if ($this->send_itip_message($invitation['event'], 'REPLY', $organizer, 'itipsubject' . $status, 'itipmailbody' . $status))
          $this->rc->output->command('display_message', $this->cal->gettext(array('name' => 'sentresponseto', 'vars' => array('mailto' => $organizer['name'] ? $organizer['name'] : $organizer['email']))), 'confirmation');
        else
          $this->rc->output->command('display_message', $this->cal->gettext('itipresponseerror'), 'error');
      }
      
      // update record in DB
      $query = $this->rc->db->query(
        "UPDATE itipinvitations
         SET event=?
         WHERE token=?",
        self::serialize_event($invitation['event']),
        $invitation['token']
      );

      if ($this->rc->db->affected_rows($query))
        return true;
    }
    
    return false;
  }


  /**
   * Create iTIP invitation token for later replies via URL
   *
   * @param array Hash array with event properties
   * @param string Attendee email address
   * @return string Invitation token
   */
  public function store_invitation($event, $attendee)
  {
    static $stored = array();
    
    if (!$event['uid'] || !$attendee)
      return false;
      
    // generate token for this invitation
    $token = $this->generate_token($event, $attendee);
    $base = substr($token, 0, 40);
    
    // already stored this
    if ($stored[$base])
      return $token;

    // delete old entry
    $this->rc->db->query("DELETE FROM itipinvitations WHERE token=?", $base);

    $query = $this->rc->db->query(
      "INSERT INTO itipinvitations
       (token, event_uid, user_id, event, expires)
       VALUES(?, ?, ?, ?, ?)",
      $base,
      $event['uid'],
      $this->rc->user->ID,
      self::serialize_event($event),
      date('Y-m-d H:i:s', $event['end'] + 86400 * 2)
    );
    
    if ($this->rc->db->affected_rows($query)) {
      $stored[$base] = 1;
      return $token;
    }
    
    return false;
  }

  /**
   * Mark invitations for the given event as cancelled
   *
   * @param array Hash array with event properties
   */
  public function cancel_itip_invitation($event)
  {
    // flag invitation record as cancelled
    $this->rc->db->query(
      "UPDATE itipinvitations
       SET cancelled=1
       WHERE event_uid=? AND user_id=?",
       $event['uid'],
       $this->rc->user->ID
    );
  }

  /**
   * Generate an invitation request token for the given event and attendee
   *
   * @param array Event hash array
   * @param string Attendee email address
   */
  public function generate_token($event, $attendee)
  {
    $base = sha1($event['uid'] . ';' . $this->rc->user->ID);
    $mail = base64_encode($attendee);
    $hash = substr(md5($base . $mail . $this->rc->config->get('des_key')), 0, 6);
    
    return "$base.$mail.$hash";
  }

  /**
   * Decode the given iTIP request token and return its parts
   *
   * @param string Request token to decode
   * @return mixed Hash array with parts or False if invalid
   */
  public function decode_token($token)
  {
    list($base, $mail, $hash) = explode('.', $token);
    
    // validate and return parts
    if ($mail && $hash && $hash == substr(md5($base . $mail . $this->rc->config->get('des_key')), 0, 6)) {
      return array('base' => $base, 'attendee' => base64_decode($mail));
    }
    
    return false;
  }

  /**
   * Helper method to serialize the given event for storing in invitations table
   */
  private static function serialize_event($event)
  {
    $ev = $event;
    $ev['description'] = abbreviate_string($ev['description'], 100);
    unset($ev['attachments']);
    return serialize($ev);
  }

}