summaryrefslogtreecommitdiff
path: root/lib/kolab_sync_data_calendar.php
blob: a0c828bed7db474f603b76dfad67fa6d7fa5e81e (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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php

/**
 +--------------------------------------------------------------------------+
 | Kolab Sync (ActiveSync for Kolab)                                        |
 |                                                                          |
 | Copyright (C) 2011-2012, 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/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 +--------------------------------------------------------------------------+
*/

/**
 * Calendar (Events) data class for Syncroton
 */
class kolab_sync_data_calendar extends kolab_sync_data implements Syncroton_Data_IDataCalendar
{
    /**
     * Mapping from ActiveSync Calendar namespace fields
     */
    protected $mapping = array(
        'allDayEvent'             => 'allday',
        //'attendees'               => 'attendees',
        'body'                    => 'description',
        //'bodyTruncated'           => 'bodytruncated',
        'busyStatus'              => 'free_busy',
        //'categories'              => 'categories',
        'dtStamp'                 => 'changed',
        'endTime'                 => 'end',
        //'exceptions'              => 'exceptions',
        'location'                => 'location',
        //'meetingStatus'           => 'meetingstatus',
        //'organizerEmail'          => 'organizeremail',
        //'organizerName'           => 'organizername',
        //'recurrence'              => 'recurrence',
        //'reminder'                => 'reminder',
        //'responseRequested'       => 'responserequested',
        //'responseType'          => 'responsetype',
        'sensitivity'             => 'sensitivity',
        'startTime'               => 'start',
        'subject'                 => 'title',
        //'timezone'                => 'timezone',
        'uID'                     => 'uid',
    );

    /**
     * Kolab object type
     *
     * @var string
     */
    protected $modelName = 'event';

    /**
     * Type of the default folder
     *
     * @var int
     */
    protected $defaultFolderType = Syncroton_Command_FolderSync::FOLDERTYPE_CALENDAR;

    /**
     * Default container for new entries
     *
     * @var string
     */
    protected $defaultFolder = 'Calendar';

    /**
     * Type of user created folders
     *
     * @var int
     */
    protected $folderType = Syncroton_Command_FolderSync::FOLDERTYPE_CALENDAR_USER_CREATED;

    /**
     * attendee status
     */
    const ATTENDEE_STATUS_UNKNOWN       = 0;
    const ATTENDEE_STATUS_TENTATIVE     = 2;
    const ATTENDEE_STATUS_ACCEPTED      = 3;
    const ATTENDEE_STATUS_DECLINED      = 4;
    const ATTENDEE_STATUS_NOTRESPONDED  = 5;

    /**
     * attendee types
     */
    const ATTENDEE_TYPE_REQUIRED = 1;
    const ATTENDEE_TYPE_OPTIONAL = 2;
    const ATTENDEE_TYPE_RESOURCE = 3;

    /**
     * busy status constants
     */
    const BUSY_STATUS_FREE        = 0;
    const BUSY_STATUS_TENTATIVE   = 1;
    const BUSY_STATUS_BUSY        = 2;
    const BUSY_STATUS_OUTOFOFFICE = 3;

    /**
     * Sensitivity values
     */
    const SENSITIVITY_NORMAL       = 0;
    const SENSITIVITY_PERSONAL     = 1;
    const SENSITIVITY_PRIVATE      = 2;
    const SENSITIVITY_CONFIDENTIAL = 3;

    /**
     * Mapping of attendee status
     *
     * @var array
     */
    protected $attendeeStatusMap = array(
        'UNKNOWN'      => self::ATTENDEE_STATUS_UNKNOWN,
        'TENTATIVE'    => self::ATTENDEE_STATUS_TENTATIVE,
        'ACCEPTED'     => self::ATTENDEE_STATUS_ACCEPTED,
        'DECLINED'     => self::ATTENDEE_STATUS_DECLINED,
        'DELEGATED'    => self::ATTENDEE_STATUS_UNKNOWN,
        'NEEDS-ACTION' => self::ATTENDEE_STATUS_UNKNOWN,
        //self::ATTENDEE_STATUS_NOTRESPONDED,
    );

    /**
     * Mapping of attendee type
     *
     * NOTE: recurrences need extra handling!
     * @var array
     */
    protected $attendeeTypeMap = array(
        'REQ-PARTICIPANT' => self::ATTENDEE_TYPE_REQUIRED,
        'OPT-PARTICIPANT' => self::ATTENDEE_TYPE_OPTIONAL,
//        'NON-PARTICIPANT' => self::ATTENDEE_TYPE_RESOURCE,
//        'CHAIR'           => self::ATTENDEE_TYPE_RESOURCE,
    );

    /**
     * Mapping of busy status
     *
     * @var array
     */
    protected $busyStatusMap = array(
        'free'        => self::BUSY_STATUS_FREE,
        'tentative'   => self::BUSY_STATUS_TENTATIVE,
        'busy'        => self::BUSY_STATUS_BUSY,
        'outofoffice' => self::BUSY_STATUS_OUTOFOFFICE,
    );

    /**
     * mapping of sensitivity
     *
     * @var array
     */
    protected $sensitivityMap = array(
        'public'       => self::SENSITIVITY_PERSONAL,
        'private'      => self::SENSITIVITY_PRIVATE,
        'confidential' => self::SENSITIVITY_CONFIDENTIAL,
    );


    /**
     * Appends contact data to xml element
     *
     * @param Syncroton_Model_SyncCollection $collection Collection data
     * @param string                         $serverId   Local entry identifier
     */
    public function getEntry(Syncroton_Model_SyncCollection $collection, $serverId)
    {
        $event  = is_array($serverId) ? $serverId : $this->getObject($collection->collectionId, $serverId);
        $config = $this->getFolderConfig($event['_mailbox']);
        $result = array();

        // Timezone
        // Kolab Format 3.0 and xCal does support timezone per-date, but ActiveSync allows
        // only one timezone per-event. We'll use timezone of the start date
        if ($event['start'] instanceof DateTime) {
            $timezone = $event['start']->getTimezone();

            if ($timezone && ($tz_name = $timezone->getName()) != 'UTC') {
                $tzc = kolab_sync_timezone_converter::getInstance();

                if ($tz_name = $tzc->encodeTimezone($tz_name)) {
                    $result['timezone'] = $tz_name;
                }
            }
        }

        // Calendar namespace fields
        foreach ($this->mapping as $key => $name) {
            $value = $this->getKolabDataItem($event, $name);

            switch ($name) {
            case 'changed':
            case 'end':
            case 'start':
                // For all-day events Kolab uses different times
                // At least Android doesn't display such event as all-day event
                if ($value) {
                    $date = clone $value;
                    if ($event['allday']) {
                        if ($name == 'start') {
                            $date->setTime(0, 0, 0);
                        }
                        else if ($name == 'end') {
                            $date->setTime(0, 0, 0);
                            $date->modify('+1 day');
                        }
                    }

                    $value = self::date_from_kolab($date);
                }

                break;

            case 'sensitivity':
                $value = intval($this->sensitivityMap[$value]);
                break;

            case 'free_busy':
                $value = $this->busyStatusMap[$value];
                break;

            case 'description':
                $value = $this->setBody($value);
                break;
            }

            if (empty($value) || is_array($value)) {
                continue;
            }

            $result[$key] = $value;
        }

        // Event reminder time
        if ($config['ALARMS'] && ($minutes = $this->from_kolab_alarm($event['alarms']))) {
            $result['reminder'] = $minutes;
        }

        // Categories, Roundcube Calendar plugin supports only one category at a time
        if (!empty($event['categories'])) {
            $result['categories'] = (array) $event['categories'];
        }

        // Organizer
        if (!empty($event['attendees'])) {
            foreach ($event['attendees'] as $idx => $attendee) {
                if ($attendee['role'] == 'ORGANIZER') {
                    $organizer = $attendee;
                    if ($name = $attendee['name']) {
                        $result['organizerName'] = $name;
                    }
                    if ($email = $attendee['email']) {
                        $result['organizerEmail'] = $email;
                    }

                    unset($event['attendees'][$idx]);
                    break;
                }
            }
        }

        // Attendees
        if (!empty($event['attendees'])) {
            $result['attendees'] = array();

            foreach ($event['attendees'] as $idx => $attendee) {
                $att = array();

                if ($attendee['name']) {
                    $att['name'] = $name;
                }
                if ($attendee['email']) {
                    $att['email'] = $email;
                }

                if ($this->asversion >= 12) {
                    $type   = isset($attendee['role'])   ? $this->attendeeTypeMap[$attendee['role']] : null;
                    $status = isset($attendee['status']) ? $this->attendeeStatusMap[$attende['status']] : null;

                    $att['attendeeType']   = $type ? $type : self::ATTENDEE_TYPE_REQUIRED;
                    $att['attendeeStatus'] = $status ? $status : self::ATTENDEE_STATUS_UNKNOWN;
                }

                $result['attendees'][] = new Syncroton_Model_EventAttendee($att);
            }
/*
            // set own status
            if (($ownAttendee = Calendar_Model_Attender::getOwnAttender($event->attendee)) !== null
                && ($busyType = array_search($ownAttendee->status, $this->_busyStatusMapping)) !== false
            ) {
                $result['BusyStatus'] = $busyType;
            }
*/
        }

        // Event meeting status
        $result['meetingStatus'] = intval(!empty($result['attendees']));

        // Recurrence
        $result['recurrence'] = $this->recurrence_from_kolab($event);

        // Recurrence exceptions
        $result['exceptions'] = $this->exceptions_from_kolab($event, $result['startTime']);

        return new Syncroton_Model_Event($result);
    }

    /**
     * convert contact from xml to libkolab array
     *
     * @param Syncroton_Model_IEntry $data     Contact to convert
     * @param string                 $folderid Folder identifier
     * @param array                  $entry    Existing entry
     *
     * @return array
     */
    public function toKolab(Syncroton_Model_IEntry $data, $folderid, $entry = null)
    {
        $foldername = $this->backend->folder_id2name($folderid, $this->device->deviceid);
        $event      = !empty($entry) ? $entry : array();
        $config     = $this->getFolderConfig($foldername);

        $event['allday'] = 0;

        // Timezone
        if (isset($data->timezone)) {
            $tzc      = kolab_sync_timezone_converter::getInstance();
            $expected = kolab_format::$timezone->getName();

            if (!empty($event['start']) && ($event['start'] instanceof DateTime)) {
                $expected = $event['start']->getTimezone()->getName();
            }

            $timezone = $tzc->getTimezone($data->timezone, $expected);
            try {
                $timezone = new DateTimeZone($timezone);
            }
            catch (Exception $e) {
                $timezone = null;
            }
        }
        if (empty($timezone)) {
            $timezone = new DateTimeZone('UTC');
        }

        // Calendar namespace fields
        foreach ($this->mapping as $key => $name) {
            $value = $data->$key;

            switch ($name) {
            case 'changed':
                $value = null;
                break;

            case 'end':
            case 'start':
                if ($timezone && $value) {
                    $value->setTimezone($timezone);
                }
                // In ActiveSync all-day event ends on 00:00:00 next day
                if ($value && $data->allDayEvent && $name == 'end') {
                    $value->modify('-1 second');
                }

                break;

            case 'sensitivity':
                $map   = array_flip($this->sensitivityMap);
                $value = $map[$value];
                break;

            case 'free_busy':
                $map   = array_flip($this->busyStatusMap);
                $value = $map[$value];
                break;

            case 'description':
                $value = $this->getBody($value, Syncroton_Model_EmailBody::TYPE_PLAINTEXT);
                // If description isn't specified keep old description
                if ($value === null) {
                    continue 2;
                }
                break;

            case 'uid':
                // If UID is too long, use auto-generated UID (#1034)
                // It's because UID is used as ServerId which cannot be longer than 64 chars
                if (strlen($value) > 64) {
                    $value = null;
                }
                break;
            }

            $this->setKolabDataItem($event, $name, $value);
        }

        // Try to fix allday events from Android
        // It doesn't set all-day flag but the period is a whole day
        if (!$event['allday'] && $event['end'] && $event['start']) {
            $interval = @date_diff($event['start'], $event['end']);
            if ($interval && $interval->format('%y%m%d%h%i%s') == '001000') {
                $event['allday'] = 1;
                $event['end']    = clone $event['start'];
            }
        }

        // Reminder
        // @TODO: should alarms be used when importing event from phone?
        if ($config['ALARMS']) {
            $event['alarms'] = $this->to_kolab_alarm($data->reminder, $event);
        }

        $event['attendees']  = array();
        $event['categories'] = array();

        // Categories
        if (isset($data->categories)) {
            foreach ($data->categories as $category) {
                $event['categories'][] = $category;
            }
        }

        // Organizer
        $name  = $data->organizerName;
        $email = $data->organizerEmail;
        if ($name || $email) {
            $event['attendees'][] = array(
                'role'  => 'ORGANIZER',
                'name'  => $name,
                'email' => $email,
            );
        }

        // Attendees
        if (isset($data->attendees)) {
            foreach ($data->attendees as $attendee) {
                $role = false;
                if (isset($attendee->attendeeType)) {
                    $role = array_search($attendee->attendeeType, $this->attendeeTypeMap);
                }
                if ($role === false) {
                    $role = array_search(self::ATTENDEE_TYPE_REQUIRED, $this->attendeeTypeMap);
                }

                // AttendeeStatus send only on repsonse (?)

                $event['attendees'][] = array(
                    'role'  => $role,
                    'name'  => $attendee->name,
                    'email' => $attendee->email,
                );
            }
        }

        // recurrence
        $event['recurrence'] = $this->recurrence_to_kolab($data->recurrence, $timezone);

        // recurrence exceptions
        $event['recurrence']['EXDATE'] = $this->exceptions_to_kolab($data->exceptions, $timezone);

        return $event;
    }

    /**
     * Set attendee status for meeting
     *
     * @param Syncroton_Model_MeetingResponse $request The meeting response
     *
     * @return string ID of new calendar entry
     */
    public function setAttendeeStatus(Syncroton_Model_MeetingResponse $request)
    {
        // @TODO: not implemented
        throw new Syncroton_Exception_Status_MeetingResponse(Syncroton_Exception_Status_MeetingMeeting::MEETING_ERROR);
    }

    /**
     * Returns filter query array according to specified ActiveSync FilterType
     *
     * @param int $filter_type  Filter type
     *
     * @param array  Filter query
     */
    protected function filter($filter_type = 0)
    {
        $filter = array();

        switch ($filter_type) {
        case Syncroton_Command_Sync::FILTER_2_WEEKS_BACK:
            $mod = '-2 weeks';
            break;
        case Syncroton_Command_Sync::FILTER_1_MONTH_BACK:
            $mod = '-1 month';
            break;
        case Syncroton_Command_Sync::FILTER_3_MONTHS_BACK:
            $mod = '-3 months';
            break;
        case Syncroton_Command_Sync::FILTER_6_MONTHS_BACK:
            $mod = '-6 months';
            break;
        }

        if (!empty($mod)) {
            $dt = new DateTime('now', new DateTimeZone('UTC'));
            $dt->modify($mod);
            $filter[] = array('dtend', '>', $dt);
        }

        return $filter;
    }

    /**
     * Converts libkolab alarms string into number of minutes
     */
    protected function from_kolab_alarm($value)
    {
        // e.g. '-15M:DISPLAY'
        // Ignore EMAIL alarms
        if (preg_match('/^-([0-9]+)([WDHMS]):(DISPLAY|AUDIO)$/', $value, $matches)) {
            $value = intval($matches[1]);

            switch ($matches[2]) {
            case 'S': $value = 1; break;
            case 'H': $value *= 60; break;
            case 'D': $value *= 24 * 60; break;
            case 'W': $value *= 7 * 24 * 60; break;
            }

            return $value;
        }
    }

    /**
     * Converts ActiveSync libkolab alarms string into number of minutes
     */
    protected function to_kolab_alarm($value, $event)
    {
        // Get alarm type from old event object if exists
        if (!empty($event['alarms']) && preg_match('/:(.*)$/', $event['alarms'], $matches)) {
            $type = $matches[1];
        }

        if ($value) {
            return sprintf('-%dM:%s', $value, $type ? $type : 'DISPLAY');
        }

        if ($type == 'DISPLAY' || $type == 'AUDIO') {
            return null;
        }

        return $event['alarms'];
    }

}