summaryrefslogtreecommitdiff
path: root/plugins/calendar/lib/calendar_ical.php
blob: 7563ba8bd969f73b1ef11af29e96d8046a454e1a (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<?php

/**
 * iCalendar functions for the Calendar plugin
 *
 * @version @package_version@
 * @author Lazlo Westerhof <hello@lazlo.me>
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 * @author Bogomil "Bogo" Shopov <shopov@kolabsys.com>
 *
 * Copyright (C) 2010, Lazlo Westerhof <hello@lazlo.me>
 * 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/>.
 */


/**
 * Class to parse and build vCalendar (iCalendar) files
 *
 * Uses the Horde:iCalendar class for parsing. To install:
 * > pear channel-discover pear.horde.org
 * > pear install horde/Horde_Icalendar
 *
 */
class calendar_ical
{
  const EOL = "\r\n";
  
  private $rc;
  private $cal;

  public $method;
  public $events = array();

  function __construct($cal)
  {
    $this->cal = $cal;
    $this->rc = $cal->rc;
  }

  /**
   * Import events from iCalendar format
   *
   * @param  string vCalendar input
   * @param  string Input charset (from envelope)
   * @return array List of events extracted from the input
   */
  public function import($vcal, $charset = RCMAIL_CHARSET)
  {
    $parser = $this->get_parser();
    $parser->parsevCalendar($vcal, 'VCALENDAR', $charset);
    $this->method = $parser->getAttributeDefault('METHOD', '');
    $this->events = $seen = array();
    if ($data = $parser->getComponents()) {
      foreach ($data as $comp) {
        if ($comp->getType() == 'vEvent') {
          $event = $this->_to_rcube_format($comp);
          if (!$seen[$event['uid']]++)
            $this->events[] = $event;
        }
      }
    }
    
    return $this->events;
  }

  /**
   * Read iCalendar events from a file
   *
   * @param string File path to read from
   * @return array List of events extracted from the file
   */
  public function import_from_file($filepath)
  {
    $this->events = $seen = array();
    $fp = fopen($filepath, 'r');

    // check file content first
    $begin = fread($fp, 1024);
    if (!preg_match('/BEGIN:VCALENDAR/i', $begin))
      return $this->events;

    $parser = $this->get_parser();
    $buffer = '';

    fseek($fp, 0);
    while (($line = fgets($fp, 2048)) !== false) {
      $buffer .= $line;
      if (preg_match('/END:VEVENT/i', $line)) {
        if (preg_match('/BEGIN:VCALENDAR/i', $buffer))
          $buffer .= self::EOL ."END:VCALENDAR";
        $parser->parsevCalendar($buffer, 'VCALENDAR', RCMAIL_CHARSET, false);
        $buffer = '';
      }
    }
    fclose($fp);

    if ($data = $parser->getComponents()) {
      foreach ($data as $comp) {
        if ($comp->getType() == 'vEvent') {
          $event = $this->_to_rcube_format($comp);
          if (!$seen[$event['uid']]++)
            $this->events[] = $event;
        }
      }
    }

    return $this->events;
  }

  /**
   * Load iCal parser from the Horde lib
   */
  public function get_parser()
  {
    if (!class_exists('Horde_iCalendar'))
      require_once($this->cal->home . '/lib/Horde_iCalendar.php');

    // set target charset for parsed events
    $GLOBALS['_HORDE_STRING_CHARSET'] = RCMAIL_CHARSET;

    return new Horde_iCalendar;
  }

  /**
   * Convert the given File_IMC_Parse_Vcalendar_Event object to the internal event format
   */
  private function _to_rcube_format($ve)
  {
    $event = array(
      'uid' => $ve->getAttributeDefault('UID'),
      'changed' => $ve->getAttributeDefault('DTSTAMP', 0),
      'title' => $ve->getAttributeDefault('SUMMARY'),
      'start' => $this->_date2time($ve->getAttribute('DTSTART')),
      'end' => $this->_date2time($ve->getAttribute('DTEND')),
      // set defaults
      'free_busy' => 'busy',
      'priority' => 0,
      'attendees' => array(),
    );

    // check for all-day dates
    if (is_array($ve->getAttribute('DTSTART')))
      $event['allday'] = true;

    if ($event['allday'])
      $event['end']->sub(new DateInterval('PT23H'));

    // assign current timezone to event start/end
    if (is_a($event['start'], 'DateTime'))
        $event['start']->setTimezone($this->cal->timezone);
    else
        unset($event['start']);

    if (is_a($event['end'], 'DateTime'))
        $event['end']->setTimezone($this->cal->timezone);
    else
        unset($event['end']);

    // map other attributes to internal fields
    $_attendees = array();
    foreach ($ve->getAllAttributes() as $attr) {
      switch ($attr['name']) {
        case 'ORGANIZER':
          $organizer = array(
            'name' => $attr['params']['CN'],
            'email' => preg_replace('/^mailto:/i', '', $attr['value']),
            'role' => 'ORGANIZER',
            'status' => 'ACCEPTED',
          );
          if (isset($_attendees[$organizer['email']])) {
            $i = $_attendees[$organizer['email']];
            $event['attendees'][$i]['role'] = $organizer['role'];
          }
          break;
        
        case 'ATTENDEE':
          $attendee = array(
            'name' => $attr['params']['CN'],
            'email' => preg_replace('/^mailto:/i', '', $attr['value']),
            'role' => $attr['params']['ROLE'] ? $attr['params']['ROLE'] : 'REQ-PARTICIPANT',
            'status' => $attr['params']['PARTSTAT'],
            'rsvp' => $attr['params']['RSVP'] == 'TRUE',
          );
          if ($organizer && $organizer['email'] == $attendee['email'])
            $attendee['role'] = 'ORGANIZER';
          
          $event['attendees'][] = $attendee;
          $_attendees[$attendee['email']] = count($event['attendees']) - 1;
          break;
          
        case 'TRANSP':
          $event['free_busy'] = $attr['value'] == 'TRANSPARENT' ? 'free' : 'busy';
          break;
        
        case 'STATUS':
          if ($attr['value'] == 'TENTATIVE')
            $event['free_busy'] = 'tentative';
          else if ($attr['value'] == 'CANCELLED')
            $event['cancelled'] = true;
          break;
        
        case 'PRIORITY':
          if (is_numeric($attr['value'])) {
            $event['priority'] = $attr['value'];
          }
          break;
        
        case 'RRULE':
          // parse recurrence rule attributes
          foreach (explode(';', $attr['value']) as $par) {
            list($k, $v) = explode('=', $par);
            $params[$k] = $v;
          }
          if ($params['UNTIL'])
            $params['UNTIL'] = date_create($params['UNTIL']);
          if (!$params['INTERVAL'])
            $params['INTERVAL'] = 1;
          
          $event['recurrence'] = $params;
          break;
        
        case 'EXDATE':
          break;
          
        case 'RECURRENCE-ID':
          $event['recurrence_id'] = $this->_date2time($attr['value']);
          break;
        
        case 'SEQUENCE':
          $event['sequence'] = intval($attr['value']);
          break;
        
        case 'DESCRIPTION':
        case 'LOCATION':
          $event[strtolower($attr['name'])] = $attr['value'];
          break;
        
        case 'CLASS':
        case 'X-CALENDARSERVER-ACCESS':
          $sensitivity_map = array('PUBLIC' => 0, 'PRIVATE' => 1, 'CONFIDENTIAL' => 2);
          $event['sensitivity'] = $sensitivity_map[$attr['value']];
          break;

        case 'X-MICROSOFT-CDO-BUSYSTATUS':
          if ($attr['value'] == 'OOF')
            $event['free_busy'] == 'outofoffice';
          else if (in_array($attr['value'], array('FREE', 'BUSY', 'TENTATIVE')))
            $event['free_busy'] = strtolower($attr['value']);
          break;

        case 'ATTACH':
          // decode inline attachment
          if (strtoupper($attr['params']['VALUE']) == 'BINARY' && !empty($attr['value'])) {
            $data = !strcasecmp($attr['params']['ENCODING'], 'BASE64') ? base64_decode($attr['value']) : $attr['value'];
            $mimetype = $attr['params']['FMTTYPE'] ? $attr['params']['FMTTYPE'] : rcube_mime::file_content_type($data, $attr['params']['X-LABEL'], 'application/octet-stream', true);
            $extensions = rcube_mime::get_mime_extensions($mimetype);
            $filename = $attr['params']['X-LABEL'] ? $attr['params']['X-LABEL'] : 'attachment' . count($event['attachments']) . '.' . $extensions[0];
            $event['attachments'][] = array(
              'mimetype' => $mimetype,
              'name' => $filename,
              'data' => $data,
              'size' => strlen($data),
            );
          }
          else if (!empty($attr['value']) && preg_match('!^[hftps]+://!', $attr['value'])) {
            // TODO: add support for displaying/managing link attachments in UI
            $event['links'][] = $attr['value'];
          }
          break;

        default:
          if (substr($attr['name'], 0, 2) == 'X-')
            $event['x-custom'][] = array($attr['name'], $attr['value']);
      }
    }

    // find alarms
    if ($valarm = $ve->findComponent('valarm')) {
      $action = 'DISPLAY';
      $trigger = null;
      
      foreach ($valarm->getAllAttributes() as $attr) {
        switch ($attr['name']) {
          case 'TRIGGER':
            if ($attr['params']['VALUE'] == 'DATE-TIME') {
              $trigger = '@' . $attr['value'];
            }
            else {
              $trigger = $attr['value'];
              $offset = abs($trigger);
              $unit = 'S';
              if ($offset % 86400 == 0) {
                $unit = 'D';
                $trigger = intval($trigger / 86400);
              }
              else if ($offset % 3600 == 0) {
                $unit = 'H';
                $trigger = intval($trigger / 3600);
              }
              else if ($offset % 60 == 0) {
                $unit = 'M';
                $trigger = intval($trigger / 60);
              }
            }
            break;

          case 'ACTION':
            $action = $attr['value'];
            break;
        }
      }
      if ($trigger)
        $event['alarms'] = $trigger . $unit . ':' . $action;
    }

    // add organizer to attendees list if not already present
    if ($organizer && !isset($_attendees[$organizer['email']]))
      array_unshift($event['attendees'], $organizer);

    // make sure the event has an UID
    if (!$event['uid'])
      $event['uid'] = $this->cal->generate_uid();
    
    return $event;
  }
  
  /**
   * Helper method to correctly interpret an all-day date value
   */
  private function _date2time($prop)
  {
    // create timestamp at 12:00 in user's timezone
    if (is_array($prop))
      return date_create(sprintf('%04d%02d%02dT120000', $prop['year'], $prop['month'], $prop['mday']), $this->cal->timezone);
    else if (is_numeric($prop))
      return date_create('@'.$prop);
    
    return $prop;
  }


  /**
   * Free resources by clearing member vars
   */
  public function reset()
  {
    $this->method = '';
    $this->events = array();
  }

  /**
   * Export events to iCalendar format
   *
   * @param  array   Events as array
   * @param  string  VCalendar method to advertise
   * @param  boolean Directly send data to stdout instead of returning
   * @param  callable Callback function to fetch attachment contents, false if no attachment export
   * @return string  Events in iCalendar format (http://tools.ietf.org/html/rfc5545)
   */
  public function export($events, $method = null, $write = false, $get_attachment = false)
  {
      $memory_limit = parse_bytes(ini_get('memory_limit'));
      
      $ical = "BEGIN:VCALENDAR" . self::EOL;
      $ical .= "VERSION:2.0" . self::EOL;
      $ical .= "PRODID:-//Roundcube Webmail " . RCMAIL_VERSION . "//NONSGML Calendar//EN" . self::EOL;
      $ical .= "CALSCALE:GREGORIAN" . self::EOL;
      
      if ($method)
        $ical .= "METHOD:" . strtoupper($method) . self::EOL;
        
      if ($write) {
        echo $ical;
        $ical = '';
      }
      
      foreach ($events as $event) {
        $vevent = "BEGIN:VEVENT" . self::EOL;
        $vevent .= "UID:" . self::escape($event['uid']) . self::EOL;
        $vevent .= $this->format_datetime("DTSTAMP", $event['changed'] ?: new DateTime(), false, true) . self::EOL;
        if ($event['sequence'])
            $vevent .= "SEQUENCE:" . intval($event['sequence']) . self::EOL;
        // correctly set all-day dates
        if ($event['allday']) {
          $event['end'] = clone $event['end'];
          $event['end']->add(new DateInterval('P1D'));  // ends the next day
          $vevent .= $this->format_datetime("DTSTART", $event['start'], true) . self::EOL;
          $vevent .= $this->format_datetime("DTEND",   $event['end'], true) . self::EOL;
        }
        else {
          $vevent .= $this->format_datetime("DTSTART", $event['start'], false) . self::EOL;
          $vevent .= $this->format_datetime("DTEND",   $event['end'], false) . self::EOL;
        }
        $vevent .= "SUMMARY:" . self::escape($event['title']) . self::EOL;
        $vevent .= "DESCRIPTION:" . self::escape($event['description']) . self::EOL;
        
        if (!empty($event['attendees'])){
          $vevent .= $this->_get_attendees($event['attendees']);
        }

        if (!empty($event['location'])) {
          $vevent .= "LOCATION:" . self::escape($event['location']) . self::EOL;
        }
        if ($event['recurrence']) {
          $vevent .= "RRULE:" . libcalendaring::to_rrule($event['recurrence'], self::EOL) . self::EOL;
        }
        if(!empty($event['categories'])) {
          $vevent .= "CATEGORIES:" . self::escape(strtoupper($event['categories'])) . self::EOL;
        }
        if ($event['sensitivity'] > 0) {
          $vevent .= "CLASS:" . ($event['sensitivity'] == 2 ? 'CONFIDENTIAL' : 'PRIVATE') . self::EOL;
        }
        if ($event['alarms']) {
          list($trigger, $action) = explode(':', $event['alarms']);
          $val = libcalendaring::parse_alaram_value($trigger);
          
          $vevent .= "BEGIN:VALARM\n";
          if ($val[1]) $vevent .= "TRIGGER:" . preg_replace('/^([-+])(.+)/', '\\1PT\\2', $trigger) . self::EOL;
          else         $vevent .= "TRIGGER;VALUE=DATE-TIME:" . gmdate('Ymd\THis\Z', $val[0]) . self::EOL;
          if ($action) $vevent .= "ACTION:" . self::escape(strtoupper($action)) . self::EOL;
          $vevent .= "END:VALARM\n";
        }
        
        $vevent .= "TRANSP:" . ($event['free_busy'] == 'free' ? 'TRANSPARENT' : 'OPAQUE') . self::EOL;
        
        if ($event['priority']) {
          $vevent .= "PRIORITY:" . $event['priority'] . self::EOL;
        }
        
        if ($event['cancelled'])
          $vevent .= "STATUS:CANCELLED" . self::EOL;
        else if ($event['free_busy'] == 'tentative')
          $vevent .= "STATUS:TENTATIVE" . self::EOL;
        
        foreach ((array)$event['x-custom'] as $prop)
          $vevent .= $prop[0] . ':' . self::escape($prop[1]) . self::EOL;
        
        // export attachments using the given callback function
        if (is_callable($get_attachment) && !empty($event['attachments'])) {
          foreach ((array)$event['attachments'] as $attach) {
            // check available memory and skip attachment export if we can't buffer it
            if ($memory_limit > 0 && ($memory_used = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024)
                  && $attach['size'] && $memory_used + $attach['size'] * 3 > $memory_limit) {
                continue;
            }
            // TODO: let the callback print the data directly to stdout (with b64 encoding)
            if ($data = call_user_func($get_attachment, $attach['id'], $event)) {
              $vevent .= sprintf('ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=%s;X-LABEL=%s:',
                self::escape($attach['mimetype']), self::escape($attach['name']));
              $vevent .= base64_encode($data) . self::EOL;
            }
            unset($data);  // attempt to free memory
          }
        }
        
        $vevent .= "END:VEVENT" . self::EOL;
        
        if ($write)
          echo rcube_vcard::rfc2425_fold($vevent);
        else
          $ical .= $vevent;
      }
      
      $ical .= "END:VCALENDAR" . self::EOL;
      
      if ($write) {
        echo $ical;
        return true;
      }

      // fold lines to 75 chars
      return rcube_vcard::rfc2425_fold($ical);
  }

  private function format_datetime($attr, $dt, $dateonly = false, $utc = false)
  {
    if (is_numeric($dt))
        $dt = new DateTime('@'.$dt);

    if ($utc)
      $dt->setTimezone(new DateTimeZone('UTC'));

    if ($dateonly) {
      return $attr . ';VALUE=DATE:' . $dt->format('Ymd');
    }
    else {
      // <ATTR>;TZID=Europe/Zurich:20120706T210000
      $tz = $dt->getTimezone();
      $tzname = $tz ? $tz->getName() : null;
      $tzid = $tzname && $tzname != 'UTC' && $tzname != '+00:00' ? ';TZID=' . self::escape($tzname) : '';
      return $attr . $tzid . ':' . $dt->format('Ymd\THis' . ($tzid ? '' : '\Z'));
    }
  }

  /**
   * Escape values according to RFC 2445 4.3.11
   */
  private function escape($str)
  {
    return strtr($str, array('\\' => '\\\\', "\n" => '\n', ';' => '\;', ',' => '\,'));
  }

  /**
  * Construct the orginizer of the event.
  * @param Array Attendees and roles
  *
  */
  private function _get_attendees($ats)
  {
    $organizer = "";
    $attendees = "";
    foreach ($ats as $at) {
      if ($at['role'] == "ORGANIZER") {
        if ($at['email']) {
          $organizer .= "ORGANIZER;";
          if (!empty($at['name']))
            $organizer .= 'CN="' . $at['name'] . '"';
          $organizer .= ":mailto:". $at['email'] . self::EOL;
        }
      }
      else if ($at['email']) {
        //I am an attendee 
        $attendees .= "ATTENDEE;ROLE=" . $at['role'] . ";PARTSTAT=" . $at['status'];
        if ($at['rsvp'])
          $attendees .= ";RSVP=TRUE";
        if (!empty($at['name']))
          $attendees .= ';CN="' . $at['name'] . '"';
        $attendees .= ":mailto:" . $at['email'] . self::EOL;
      }
    }

    return $organizer . $attendees;
  }

}