diff --git a/lib/filter/mapistore.php b/lib/filter/mapistore.php index dbac3fe..42d0150 100644 --- a/lib/filter/mapistore.php +++ b/lib/filter/mapistore.php @@ -1,410 +1,352 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_filter_mapistore extends kolab_api_filter { protected $input; protected $attrs_filter; /** * Modify request path * * @param array (Exploded) request path */ public function path(&$path) { // handle differences between OpenChange API and Kolab API // here we do only very basic modifications, just to be able // to select apprioprate api action class if ($path[0] == 'calendars') { $path[0] = 'events'; } } /** * Executed before every api action * * @param kolab_api_input Request */ public function input(&$input) { $this->input = $input; $this->common_action = !in_array($input->action, array('folders', 'info')); // handle differences between OpenChange API and Kolab API switch ($input->action) { case 'folders': // in OpenChange folders/1/folders means get all folders if ($input->method == 'GET' && $input->path[0] === '1' && $input->path[1] == 'folders') { $input->path = array(); $type = 'folder'; } // in OpenChange folders/0/folders means get the hierarchy of the NON IPM Subtree // we should ignore/send empty request else if ($input->method == 'GET' && $input->path[0] === '0' && $input->path[1] == 'folders') { // @TODO throw new kolab_api_exception(kolab_api_exception::NOT_FOUND); } else if ($input->path[1] == 'messages') { $input->path[1] = 'objects'; if ($input->args['properties']) { $type = $input->api->backend->folder_type($input->path[0]); list($type, ) = explode('.', $type); } } else if ($input->path[1] == 'deletemessages') { $input->path[1] = 'deleteobjects'; } // properties filter, map MAPI attribute names to Kolab attributes if ($type && $input->args['properties']) { $this->attrs_filter = explode(',', $this->input->args['properties']); $properties = $this->attributes_filter($this->attrs_filter, $type); $input->args['properties'] = implode(',', $properties); } break; case 'notes': // Notes do not have attachments in Exchange if ($input->path[1] === 'attachments' || count($this->path) > 2) { throw new kolab_api_exception(kolab_api_exception::NOT_FOUND); } break; } // convert / to // or /// if ($this->common_action && ($uid = $input->path[0])) { list($folder, $msg, $attach) = self::uid_decode($uid); $path = array($folder, $msg); if ($attach) { $path[] = $attach; } array_splice($input->path, 0, 1, $path); } // convert parent_id into path on object create request if ($input->method == 'POST' && $this->common_action && !count($input->path)) { $data = $input->input(null, true); if ($data['parent_id']) { $input->path[0] = $data['parent_id']; } else { throw new kolab_api_exception(kolab_api_exception::INVALID_REQUEST); } } // convert parent_id into path on object update request else if ($input->method == 'PUT' && $folder && count($input->path) == 2) { $data = $input->input(null, true); if ($data['parent_id'] && $data['parent_id'] != $folder) { $this->parent_change_handler($data); } } } /** * Executed when parsing request body * * @param string Request data * @param string Expected object type * @param string Original object data (set on update requests) */ public function input_body(&$data, $type = null, $original_object = null) { $input = $this->input; // handle differences between OpenChange API and Kolab API // Note: input->path is already modified by input() and path() above switch ($input->action) { case 'folders': // folders//deletemessages input if ($input->path[1] == 'deleteobjects') { // Kolab API expects just a list of identifiers, I.e.: // [{"id": "1"}, {"id": "2"}] => ["1", "2"] foreach ((array) $data as $idx => $element) { $data[$idx] = $element['id']; } } break; } switch ($type) { case 'attachment': case 'event': case 'note': case 'task': case 'contact': case 'mail': case 'folder': $model = $this->get_model_class($type); $data = $model->input($data, $original_object); break; } } /** * Apply filter on output data * * @param array Result data * @param string Object type * @param array Context (folder_uid, object_uid, object) * @param array Optional attributes filter */ public function output(&$result, $type, $context = null, $attrs_filter = array()) { // handle differences between OpenChange API and Kolab API $model = $this->get_model_class($type); if (!empty($this->attrs_filter)) { $attrs_filter = array_combine($this->attrs_filter, $this->attrs_filter); } else if (!empty($attrs_filter)) { $attrs_filter = $this->attributes_filter($attrs_filter, $type, true); $attrs_filter = array_combine($attrs_filter, $attrs_filter); } foreach ($result as $idx => $data) { if ($filtered = $model->output($data, $context)) { // apply properties filter (again) if (!empty($attrs_filter)) { $filtered = array_intersect_key($filtered, $attrs_filter); } $result[$idx] = $filtered; } else { unset($result[$idx]); $unset = true; } } if ($unset) { $result = array_values($result); } // cleanup unset($_SESSION['uploads']['MAPIATTACH']); } /** * Executed for response headers * * @param array Response headers */ public function headers(&$headers) { // handle differences between OpenChange API and Kolab API foreach ($headers as $name => $value) { switch ($name) { case 'X-Count': $headers['X-mapistore-rowcount'] = $value; unset($headers[$name]); break; } } } /** * Executed for empty response status * * @param int Status code */ public function send_status(&$status) { // handle differences between OpenChange API and Kolab API if ($this->input->method == 'PUT' && !in_array($input->action, array('info'))) { // Mapistore expects 204 on object updates // however, we'd like to send modified UID of the object sometimes // $status = kolab_api_output::STATUS_EMPTY; } } /** * Converts kolab identifiers describind the object into * MAPI identifier that can be easily used in URL. * * @param string Folder UID * @param string Object UID * @param string Optional attachment identifier * * @return string Object identifier */ public static function uid_encode($folder_uid, $msg_uid, $attach_id = null) { $result = array($folder_uid, $msg_uid); if ($attach_id) { $result[] = $attach_id; } $result = array_map(array('kolab_api_filter_mapistore', 'uid_encode_item'), $result); return implode('.', $result); } /** * Converts back the MAPI identifier into kolab folder/object/attachment IDs * * @param string Object identifier * * @return array Object identifiers */ public static function uid_decode($uid) { $result = explode('.', $uid); $result = array_map(array('kolab_api_filter_mapistore', 'uid_decode_item'), $result); return $result; } /** * Encodes UID element */ protected static function uid_encode_item($str) { $fn = function($match) { return '_' . ord($match[1]); }; $str = preg_replace_callback('/([^0-9a-zA-Z-])/', $fn, $str); return $str; } /** * Decodes UID element */ protected static function uid_decode_item($str) { $fn = function($match) { return chr($match[1]); }; $str = preg_replace_callback('/_([0-9]{2})/', $fn, $str); return $str; } /** * Filter property names */ protected function attributes_filter($attrs, $type = null, $reverse = false) { $model = $this->get_model_class($type); return $model->attributes_filter($attrs, $reverse); } /** * Return instance of model class object */ protected function get_model_class($type) { $class = "kolab_api_filter_mapistore_$type"; return new $class($this); } - /** - * Convert DateTime object to MAPI date format - */ - public function date_php2mapi($date, $utc = true, $time = null) - { - // convert string to DateTime - if (!is_object($date) && !empty($date)) { - // convert date to datetime on 00:00:00 - if (preg_match('/^([0-9]{4})-?([0-9]{2})-?([0-9]{2})$/', $date, $m)) { - $date = $m[1] . '-' . $m[2] . '-' . $m[3] . 'T00:00:00+00:00'; - } - - $date = new DateTime($date); - } - - if (!is_object($date)) { - return; - } - - if ($utc) { - $date->setTimezone(new DateTimeZone('UTC')); - } - - if (!empty($time)) { - $date->setTime((int) $time['hour'], (int) $time['minute'], (int) $time['second']); - } - - // MAPI PTypTime is 64-bit integer representing the number - // of 100-nanosecond intervals since January 1, 1601. - // Mapistore format for this type is a float number - - // seconds since 1601-01-01 00:00:00 - $seconds = floatval($date->format('U')) + 11644473600; -/* - if ($microseconds = intval($date->format('u'))) { - $seconds += $microseconds/1000000; - } -*/ - return $seconds; - } - - /** - * Convert date-time from MAPI format to DateTime - */ - public function date_mapi2php($date) - { - $seconds = floatval(sprintf('%.0f', $date)); - - // assumes we're working with dates after 1970-01-01 - $dt = new DateTime('@' . intval($seconds - 11644473600)); -/* - if ($microseconds = intval(($date - $seconds) * 1000000)) { - $dt = new DateTime($dt->format('Y-m-d H:i:s') . '.' . $microseconds, $dt->getTimezone()); - } -*/ - return $dt; - } - /** * Handles object parent modification (move) */ protected function parent_change_handler($data) { $folder = $this->input->path[0]; $uid = $this->input->path[1]; $target = $data['parent_id']; $api = kolab_api::get_instance(); // move the object $api->backend->objects_move($folder, $target, array($uid)); // replace folder uid in input arguments $this->input->path[0] = $target; // exit if the rest of input is empty if (count($data) < 2) { $api->output->send_status(kolab_api_output::STATUS_EMPTY); } } } diff --git a/lib/filter/mapistore/common.php b/lib/filter/mapistore/common.php index 9777e9a..46cc3d7 100644 --- a/lib/filter/mapistore/common.php +++ b/lib/filter/mapistore/common.php @@ -1,808 +1,919 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_filter_mapistore_common { // Common properties [MS-OXCMSG] protected static $common_map = array( // 'PidTagAccess' => '', // 'PidTagAccessLevel' => '', // 0 - read-only, 1 - modify // 'PidTagChangeKey' => '', 'PidTagCreationTime' => 'creation-date', // PtypTime, UTC 'PidTagLastModificationTime' => 'last-modification-date', // PtypTime, UTC // 'PidTagLastModifierName' => '', // 'PidTagObjectType' => '', // @TODO // 'PidTagHasAttachments' => '', // @TODO // 'PidTagRecordKey' => '', // 'PidTagSearchKey' => '', 'PidNameKeywords' => 'categories', ); protected $recipient_track_status_map = array( 'TENTATIVE' => 0x00000002, 'ACCEPTED' => 0x00000003, 'DECLINED' => 0x00000004, ); protected $recipient_type_map = array( 'NON-PARTICIPANT' => 0x00000004, 'OPT-PARTICIPANT' => 0x00000002, 'REQ-PARTICIPANT' => 0x00000001, 'CHAIR' => 0x00000001, ); /** * Mapping of weekdays */ protected static $recurrence_day_map = array( 'SU' => 0x00000000, 'MO' => 0x00000001, 'TU' => 0x00000002, 'WE' => 0x00000003, 'TH' => 0x00000004, 'FR' => 0x00000005, 'SA' => 0x00000006, 'BYDAY-SU' => 0x00000001, 'BYDAY-MO' => 0x00000002, 'BYDAY-TU' => 0x00000004, 'BYDAY-WE' => 0x00000008, 'BYDAY-TH' => 0x00000010, 'BYDAY-FR' => 0x00000020, 'BYDAY-SA' => 0x00000040, ); /** * Extracts data from kolab data array */ public static function get_kolab_value($data, $name) { $name_items = explode('.', $name); $count = count($name_items); $value = $data[$name_items[0]]; // special handling of x-custom properties if ($name_items[0] === 'x-custom') { foreach ((array) $value as $custom) { if ($custom['identifier'] === $name_items[1]) { return $custom['value']; } } return null; } for ($i = 1; $i < $count; $i++) { if (!is_array($value)) { return null; } list($key, $num) = explode(':', $name_items[$i]); $value = $value[$key]; if ($num !== null && $value !== null) { $value = is_array($value) ? $value[$num] : null; } } return $value; } /** * Sets specified kolab data item */ public static function set_kolab_value(&$data, $name, $value) { $name_items = explode('.', $name); $count = count($name_items); $element = &$data; // x-custom properties if ($name_items[0] === 'x-custom') { // this is supposed to be converted later by parse_common_props() $data[$name] = $value; return; } if ($count > 1) { for ($i = 0; $i < $count - 1; $i++) { $key = $name_items[$i]; if (!array_key_exists($key, $element)) { $element[$key] = array(); } $element = &$element[$key]; } } $element[$name_items[$count - 1]] = $value; } /** * Parse common properties in object data (convert into MAPI format) */ protected static function parse_common_props(&$result, $data, $context = array()) { if (empty($context)) { // @TODO: throw exception? return; } if ($data['uid'] && $context['folder_uid']) { $result['id'] = kolab_api_filter_mapistore::uid_encode($context['folder_uid'], $data['uid']); } if ($context['folder_uid']) { $result['parent_id'] = $context['folder_uid']; } foreach (self::$common_map as $mapi_idx => $kolab_idx) { if (!isset($result[$mapi_idx]) && ($value = $data[$kolab_idx]) !== null) { switch ($mapi_idx) { case 'PidTagCreationTime': case 'PidTagLastModificationTime': - $result[$mapi_idx] = kolab_api_filter_mapistore::date_php2mapi($value, true); + $result[$mapi_idx] = self::date_php2mapi($value, true); break; case 'PidNameKeywords': $result[$mapi_idx] = self::parse_categories((array) $value); break; } } } } /** * Convert common properties into kolab format */ protected static function convert_common_props(&$result, $data, $original) { // @TODO: id, parent_id? foreach (self::$common_map as $mapi_idx => $kolab_idx) { if (array_key_exists($mapi_idx, $data) && !array_key_exists($kolab_idx, $result)) { $value = $data[$mapi_idx]; switch ($mapi_idx) { case 'PidTagCreationTime': case 'PidTagLastModificationTime': if ($value) { - $dt = kolab_api_filter_mapistore::date_mapi2php($value); + $dt = self::date_mapi2php($value); $result[$kolab_idx] = $dt->format('Y-m-d\TH:i:s\Z'); } break; default: if ($value) { $result[$kolab_idx] = $value; } break; } } } // Handle x-custom fields foreach ((array) $result as $key => $value) { if (strpos($key, 'x-custom.') === 0) { unset($result[$key]); $key = substr($key, 9); foreach ((array) $original['x-custom'] as $idx => $custom) { if ($custom['identifier'] == $key) { if ($value) { $original['x-custom'][$idx]['value'] = $value; } else { unset($original['x-custom'][$idx]); } $x_custom_update = true; continue 2; } } if ($value) { $original['x-custom'][] = array( 'identifier' => $key, 'value' => $value, ); } $x_custom_update = true; } } if ($x_custom_update) { $result['x-custom'] = array_values($original['x-custom']); } } /** * Filter property names with mapping (kolab <> MAPI) * * @param array $attrs Property names * @param bool $reverse Reverse mapping * * @return array Property names */ public function attributes_filter($attrs, $reverse = false) { $map = array_merge(self::$common_map, $this->map()); $result = array(); // add some special common attributes $map['PidTagMessageClass'] = 'PidTagMessageClass'; $map['collection'] = 'collection'; $map['id'] = 'uid'; foreach ($attrs as $attr) { if ($reverse) { if ($name = array_search($attr, $map)) { $result[] = $name; } } else if ($name = $map[$attr]) { $result[] = $name; } } return $result; } /** * Return properties map */ protected function map() { return array(); } /** * Parse categories according to [MS-OXCICAL 2.1.3.1.1.20.3] * * @param array Categories * * @return array Categories */ public static function parse_categories($categories) { if (!is_array($categories)) { return; } $result = array(); foreach ($categories as $idx => $val) { $val = preg_replace('/(\x3B|\x2C|\x06\x1B|\xFE\x54|\xFF\x1B)/', '', $val); $val = preg_replace('/\s+/', ' ', $val); $val = trim($val); $len = mb_strlen($val); if ($len) { if ($len > 255) { $val = mb_substr($val, 0, 255); } $result[mb_strtolower($val)] = $val; } } return array_values($result); } /** * Convert Kolab 'attendee' specification into MAPI recipient * and add it to the result */ public function attendee_to_recipient($attendee, &$result, $is_organizer = false) { $email = $attendee['cal-address']; $params = (array) $attendee['parameters']; // parse mailto string if (strpos($email, 'mailto:') === 0) { $email = urldecode(substr($email, 7)); } $emails = rcube_mime::decode_address_list($email, 1); if (!empty($email)) { $email = $emails[key($emails)]; $recipient = array( 'PidTagAddressType' => 'SMTP', 'PidTagDisplayName' => $params['cn'] ?: $email['name'], 'PidTagDisplayType' => 0, 'PidTagEmailAddress' => $email['mailto'], ); if ($is_organizer) { $recipient['PidTagRecipientFlags'] = 0x00000003; $recipient['PidTagRecipientType'] = 0x00000001; } else { $recipient['PidTagRecipientFlags'] = 0x00000001; $recipient['PidTagRecipientTrackStatus'] = (int) $this->recipient_track_status_map[$params['partstat']]; $recipient['PidTagRecipientType'] = $this->to_recipient_type($params['cutype'], $params['role']); } $recipient['PidTagRecipientDisplayName'] = $recipient['PidTagDisplayName']; $result['recipients'][] = $recipient; if (strtoupper($params['rsvp']) == 'TRUE') { $result['PidTagReplyRequested'] = true; $result['PidTagResponseRequested'] = true; } } } /** * Convert MAPI recipient into Kolab attendee */ public function recipient_to_attendee($recipient, &$result) { if ($email = $recipient['PidTagEmailAddress']) { $mailto = 'mailto:' . rawurlencode($email); $attendee = array( 'cal-address' => $mailto, 'parameters' => array( 'cn' => $recipient['PidTagDisplayName'] ?: $recipient['PidTagRecipientDisplayName'], ), ); if ($recipient['PidTagRecipientFlags'] == 0x00000003) { $result['organizer'] = $attendee; } else { switch ($recipient['PidTagRecipientType']) { case 0x00000004: $role = 'NON-PARTICIPANT'; break; case 0x00000003: $cutype = 'RESOURCE'; break; case 0x00000002: $role = 'OPT-PARTICIPANT'; break; case 0x00000001: $role = 'REQ-PARTICIPANT'; break; } $map = array_flip($this->recipient_track_status_map); $partstat = $map[$recipient['PidTagRecipientTrackStatus']] ?: 'NEEDS-ACTION'; // @TODO: rsvp? $attendee['parameters']['cutype'] = $cutype; $attendee['parameters']['role'] = $role; $attendee['parameters']['partstat'] = $partstat; $result['attendee'][] = $attendee; } } } /** * Convert Kolab recurrence specification into MAPI properties * * @param array $data Kolab object * @param array $type Object data (MAPI format) * @param string $type Object type (event, task) * * @return object MAPI recurrence in binary format */ public static function recurrence_from_kolab($data, $object = array(), $type = 'event') { - if (empty($data['rrule']) || empty($data['rrule']['recur'])) { + if ((empty($data['rrule']) || empty($data['rrule']['recur'])) + && (empty($data['rdate']) || empty($data['rdate']['date'])) + ) { return null; } - $rule = $data['rrule']['recur']; - $startdate = kolab_api_input_json::to_datetime($data['dtstart']); - $result = array( - 'Period' => $rule['interval'] ? $rule['interval'] : 1, - 'FirstDOW' => self::day2bitmask($rule['wkst'] ?: 'MO'), - 'OccurrenceCount' => 0x0000000A, - 'EndDate' => 0x5AE980DF, - 'CalendarType' => kolab_api_filter_mapistore_structure_recurrencepattern::CALENDARTYPE_DEFAULT, - // DeletedInstanceDates - // ModifiedInstanceDates - ); - // Get event/task start date for FirstDateTime calculations - if ($startdate) { - $mapi_dt = kolab_api_filter_mapistore::date_php2mapi($startdate, true); - $result['StartDate'] = intval($mapi_dt / 10000000 / 60); + if ($dtstart = kolab_api_input_json::to_datetime($data['dtstart'])) { + // StartDate: Set to the date portion of DTSTART, in the time zone specified + // by PidLidTimeZoneStruct. This date is stored in minutes after + // midnight Jan 1, 1601. Note that this value MUST always be + // evenly divisible by 1440. + // EndDate: Set to the start date of the last instance of a recurrence, in the + // time zone specified by PidLidTimeZoneStruct. This date is + // stored in minutes after midnight January 1, 1601. If the + // recurrence is infinite, set EndDate to 0x5AE980DF. Note that + // this value MUST always be evenly divisible by 1440, except for + // the special value 0x5AE980DF. + + $startdate = clone $dtstart; + $startdate->setTime(0, 0, 0); + $startdate = self::date_php2mapi($startdate, true); + $startdate = intval($startdate / 60); + + if ($mod = ($startdate % 1440)) { + $startdate -= $mod; + } + + // @TODO: get first occurrence of the event using libcalendaring_recurrence class ? } else { rcube::raise_error(array( 'line' => __LINE__, 'file' => __FILE__, 'message' => "Found recurring $type without start date, skipping recurrence", ), true, false); return; } -// $startdate->setTime(0, 0, 0); - - // @TODO: - // StartDate: Set to the date portion of DTSTART, in the time zone specified - // by PidLidTimeZoneStruct. This date is stored in minutes after - // midnight Jan 1, 1601. Note that this value MUST always be - // evenly divisible by 1440. - // EndDate: Set to the start date of the last instance of a recurrence, in the - // time zone specified by PidLidTimeZoneStruct. This date is - // stored in minutes after midnight January 1, 1601. If the - // recurrence is infinite, set EndDate to 0x5AE980DF. Note that - // this value MUST always be evenly divisible by 1440, except for - // the special value 0x5AE980DF. - - // @TODO: get first occurrence of the event using libcalendaring_recurrence class ? + $rule = (array) ($data['rrule'] ? $data['rrule']['recur'] : null); + $result = array( + 'Period' => $rule && $rule['interval'] ? $rule['interval'] : 1, + 'FirstDOW' => self::day2bitmask($rule['wkst'] ?: 'MO'), + 'OccurrenceCount' => 0x0000000A, + 'StartDate' => $startdate, + 'EndDate' => 0x5AE980DF, + 'FirstDateTime' => $startdate, + 'CalendarType' => kolab_api_filter_mapistore_structure_recurrencepattern::CALENDARTYPE_DEFAULT, + 'ModifiedInstanceDates' => array(), + 'DeletedInstanceDates' => array(), + ); switch ($rule['freq']) { case 'DAILY': $result['RecurFrequency'] = kolab_api_filter_mapistore_structure_recurrencepattern::RECURFREQUENCY_DAILY; $result['PatternType'] = kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_DAY; $result['Period'] *= 1440; break; case 'WEEKLY': // if BYDAY does not exist use day from DTSTART if (empty($rule['byday'])) { $rule['byday'] = strtoupper($startdate->format('S')); } $result['RecurFrequency'] = kolab_api_filter_mapistore_structure_recurrencepattern::RECURFREQUENCY_WEEKLY; $result['PatternType'] = kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_WEEK; $result['PatternTypeSpecific'] = self::day2bitmask($rule['byday'], 'BYDAY-'); break; case 'MONTHLY': $result['RecurFrequency'] = kolab_api_filter_mapistore_structure_recurrencepattern::RECURFREQUENCY_MONTHLY; if (!empty($rule['bymonthday'])) { // MAPI doesn't support multi-valued month days $month_day = min(explode(',', $rule['bymonthday'])); $result['PatternType'] = kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_MONTH; $result['PatternTypeSpecific'] = $month_day == -1 ? 0x0000001F : $month_day; } else { $result['PatternType'] = kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_MONTHNTH; $result['PatternTypeSpecific'][] = self::day2bitmask($rule['byday'], 'BYDAY-'); if (!empty($rule['bysetpos'])) { $result['PatternTypeSpecific'][] = $rule['bysetpos'] == -1 ? 0x00000005 : $rule['bysetpos']; } } break; case 'YEARLY': $result['RecurFrequency'] = kolab_api_filter_mapistore_structure_recurrencepattern::RECURFREQUENCY_YEARLY; $result['Period'] *= 12; // MAPI doesn't support multi-valued months if ($rule['bymonth']) { // @TODO: set $startdate } if (!empty($rule['bymonthday'])) { // MAPI doesn't support multi-valued month days $month_day = min(explode(',', $rule['bymonthday'])); $result['PatternType'] = kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_MONTHNTH; $result['PatternTypeSpecific'] = array(0, $month_day == -1 ? 0x0000001F : $month_day); } else { $result['PatternType'] = kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_MONTHNTH; $result['PatternTypeSpecific'][] = self::day2bitmask($rule['byday'], 'BYDAY-'); if (!empty($rule['bysetpos'])) { $result['PatternTypeSpecific'][] = $rule['bysetpos'] == -1 ? 0x00000005 : $rule['bysetpos']; } } break; } - if (!empty($rule['until'])) { - $result['EndDate'] = intval(kolab_api_filter_mapistore::date_php2mapi($rule['until']) / 10000000 / 60); + $exception_info = array(); + $extended_exception = array(); + + // Custom occurrences (RDATE) + if (!empty($data['rdate'])) { + foreach ((array) $data['rdate']['date'] as $dt) { + try { + $dt = new DateTime($dt, $dtstart->getTimezone()); + $dt->setTime(0, 0, 0); + $dt = self::date_php2minutes($dt); + + $result['ModifiedInstanceDates'][] = $dt; + $result['DeletedInstanceDates'][] = $dt; + + $exception_info[] = new kolab_api_filter_mapistore_structure_exceptioninfo(array( + 'StartDateTime' => $dt, + 'EndDateTime' => $dt + $object['PidLidAppointmentDuration'], + 'OriginalStartDate' => $dt, + 'OverrideFlags' => 0, + )); + + $extended_exception[] = kolab_api_filter_mapistore_structure_extendedexception::get_empty(); + } + catch (Exception $e) { + } + } + + $result['EndType'] = kolab_api_filter_mapistore_structure_recurrencepattern::ENDTYPE_NOCC; + $result['OccurenceCount'] = count($result['ModifiedInstanceDates']); + + // @FIXME: Kolab format says there can be RDATE and/or RRULE + // MAPI specification says there must be RRULE if RDATE is specified + if (!$result['RecurFrequency']) { + $result['RecurFrequency'] = 0; + $result['PatternType'] = 0; + } + } + + if ($rule && !empty($rule['until'])) { + $result['EndDate'] = intval(self::date_php2mapi($rule['until']) / 10000000 / 60); // @TODO: calculate OccurrenceCount? $result['EndType'] = kolab_api_filter_mapistore_structure_recurrencepattern::ENDTYPE_AFTER; } - else if (!empty($rule['count'])) { + else if ($rule && !empty($rule['count'])) { $result['EndType'] = kolab_api_filter_mapistore_structure_recurrencepattern::ENDTYPE_NOCC; $result['OccurrenceCount'] = $rule['count']; // @TODO: set EndDate } - else { + else if (!isset($result['EndType'])) { $result['EndType'] = kolab_api_filter_mapistore_structure_recurrencepattern::ENDTYPE_NEVER; } - $result['FirstDateTime'] = self::date_php2minutes($startdate); - - // Deleted instances - if (!empty($data['exdate']) && $startdate) { + // Deleted instances (EXDATE) + if (!empty($data['exdate'])) { if (!empty($data['exdate']['date'])) { $exceptions = (array) $data['exdate']['date']; } else if (!empty($data['exdate']['date-time'])) { $exceptions = (array) $data['exdate']['date-time']; } else { $exceptions = array(); } // convert date(-time)s to numbers foreach ($exceptions as $idx => $dt) { try { - $dt = new DateTime($dt, $startdate->getTimezone()); + $dt = new DateTime($dt, $dtstart->getTimezone()); $dt->setTime(0, 0, 0); - $exceptions[$idx] = self::date_php2minutes($dt); + $result['DeletedInstanceDates'][] = self::date_php2minutes($dt); } catch (Exception $e) { } } + } - // [MS-OXCICAL] 2.1.3.1.1.20.13: Sort and make exceptions valid - sort($exceptions); - $exceptions = array_values(array_unique(array_filter($exceptions))); - - $result['DeletedInstanceDates'] = $exceptions; + // [MS-OXCICAL] 2.1.3.1.1.20.13: Sort and make exceptions valid + foreach (array('DeletedInstanceDates', 'ModifiedInstanceDates') as $key) { + if (!empty($result[$key])) { + sort($result[$key]); + $result[$key] = array_values(array_unique(array_filter($result[$key]))); + } } $result = new kolab_api_filter_mapistore_structure_recurrencepattern($result); if ($type == 'task') { return $result->output(true); } // @TODO: exceptions $byhour = $rule['byhour'] ? min(explode(',', $rule['byhour'])) : 0; $byminute = $rule['byminute'] ? min(explode(',', $rule['byminute'])) : 0; $offset = 60 * intval($byhour) + intval($byminute); $arp = array( 'RecurrencePattern' => $result, 'StartTimeOffset' => $offset, 'EndTimeOffset' => $offset + $object['PidLidAppointmentDuration'], - // ExceptionInfo - // ExtendedExceptions + 'ExceptionInfo' => $exception_info, + 'ExtendedException' => $extended_exception, ); $result = new kolab_api_filter_mapistore_structure_appointmentrecurrencepattern($arp); return $result->output(true); } /** * Convert MAPI recurrence into Kolab (MS-OXICAL: 2.1.3.2.2) * * @param string $rule MAPI binary representation of recurrence rule * @param array $object Kolab object * @param string $type Object type (task, event) */ public static function recurrence_to_kolab($rule, &$object, $type = 'event') { if (empty($rule)) { return array(); } // parse binary (Appointment)RecurrencePattern if ($type == 'event') { $arp = new kolab_api_filter_mapistore_structure_appointmentrecurrencepattern(); $arp->input($rule, true); $rp = $arp->RecurrencePattern; } else { $rp = new kolab_api_filter_mapistore_structure_recurrencepattern(); $rp->input($rule, true); } $result = array( 'interval' => $rp->Period, ); switch ($rp->PatternType) { case kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_DAY: $result['freq'] = 'DAILY'; $result['interval'] /= 1440; if ($arp) { $result['byhour'] = floor($arp->StartTimeOffset / 60); $result['byminute'] = $arp->StartTimeOffset - $result['byhour'] * 60; } break; case kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_WEEK: $result['freq'] = 'WEEKLY'; $result['byday'] = self::bitmask2day($rp->PatternTypeSpecific); if ($rp->Period >= 1) { $result['wkst'] = self::bitmask2day($rp->FirstDOW); } break; default: // monthly/yearly $evenly_divisible = $rp->Period % 12 == 0; switch ($rp->PatternType) { case kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_MONTH: case kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_MONTHEND: $result['freq'] = $evenly_divisible ? 'YEARLY' : 'MONTHLY'; break; case kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_MONTHNTH: case kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_HJMONTHNTH: $result['freq'] = $evenly_divisible ? 'YEARLY-NTH' : 'MONTHLY-NTH'; break; default: // not-supported return; } if ($result['freq'] = 'MONTHLY') { $rule['bymonthday'] = intval($rp->PatternTypeSpecific == 0x0000001F ? -1 : $rp->PatternTypeSpecific); } else if ($result['freq'] = 'MONTHLY-NTH') { $result['freq'] = 'MONTHLY'; $result['byday'] = self::bitmask2day($rp->PatternTypeSpecific[0]); if ($rp->PatternTypeSpecific[1]) { $result['bysetpos'] = intval($rp->PatternTypeSpecific[1] == 0x00000005 ? -1 : $rp->PatternTypeSpecific[1]); } } else if ($result['freq'] = 'YEARLY') { $result['interval'] /= 12; $rule['bymonthday'] = intval($rp->PatternTypeSpecific == 0x0000001F ? -1 : $rp->PatternTypeSpecific); $rule['bymonth'] = 0;// @TODO: month from FirstDateTime } else if ($result['freq'] = 'YEARLY-NTH') { $result['freq'] = 'YEARLY'; $result['interval'] /= 12; $result['byday'] = self::bitmask2day($rp->PatternTypeSpecific[0]); $result['bymonth'] = 0;// @TODO: month from FirstDateTime if ($rp->PatternTypeSpecific[1]) { $result['bysetpos'] = intval($rp->PatternTypeSpecific[1] == 0x00000005 ? -1 : $rp->PatternTypeSpecific[1]); } } } if ($rp->EndType == kolab_api_filter_mapistore_structure_recurrencepattern::ENDTYPE_AFTER) { // @TODO: set UNTIL to EndDate + StartTimeOffset, or the midnight of EndDate } else if ($rp->EndType == kolab_api_filter_mapistore_structure_recurrencepattern::ENDTYPE_NOCC) { $result['count'] = $rp->OccurrenceCount; } if ($result['interval'] == 1) { unset($result['interval']); } - // Deleted exceptions - $object['exdate'] = array(); - foreach ((array) $rp->DeletedInstanceDates as $date) { - if ($dt = self::date_minutes2php($date)) { - $object['exdate']['date'][] = $dt->format('Y-m-d'); - } - } - $object['rrule']['recur'] = $result; - } - - /** - * Converts string of days (TU,TH) to bitmask used by MAPI - * - * @param string $days - * - * @return int - */ - protected static function day2bitmask($days, $prefix = '') - { - $days = explode(',', $days); - $result = 0; + $object['exdate'] = array(); + $object['rdate'] = array(); - foreach ($days as $day) { - $result = $result + self::$recurrence_day_map[$prefix.$day]; - } +// $exception_info = (array) $rp->ExceptionInfo; +// $extended_exception = (array) $rp->ExtendedException; + $modified_dates = (array) $rp->ModifiedInstanceDates; + $deleted_dates = (array) $rp->DeletedInstanceDates; - return $result; - } + // Deleted/Modified exceptions (EXDATE/RDATE) + foreach ($deleted_dates as $date) { + $idx = in_array($date, $modified_dates) ? 'rdate' : 'exdate'; + $dt = self::date_minutes2php($date); - /** - * Convert bitmask used by MAPI to string of days (TU,TH) - * - * @param int $days - * - * @return string - */ - protected static function bitmask2day($days) - { - $days_arr = array(); - - foreach (self::$recurrence_day_map as $day => $bit) { - if (($days & $bit) === $bit) { - $days_arr[] = preg_replace('/^BYDAY-/', '', $day); + if ($dt) { + $object[$idx]['date'][] = $dt->format('Y-m-d'); } } - - $result = implode(',', $days_arr); - - return $result; } /** * Returns number of minutes between midnight 1601-01-01 * and specified UTC DateTime */ - protected static function date_php2minutes($date) + public static function date_php2minutes($date) { $start = new DateTime('1601-01-01 00:00:00 +00:00'); // make sure the specified date is in UTC $date->setTimezone(new DateTimeZone('UTC')); return (int) round(($date->getTimestamp() - $start->getTimestamp()) / 60); } /** * Convert number of minutes between midnight 1601-01-01 * and specified UTC DateTime into PHP DateTime * * @return DateTime|bool DateTime object or False on failure */ - protected static function date_minutes2php($minutes) + public static function date_minutes2php($minutes) { $datetime = new DateTime('1601-01-01 00:00:00', new DateTimeZone('UTC')); $interval = new DateInterval(sprintf('PT%dM', $minutes)); return $datetime->add($interval); } + /** + * Convert DateTime object to MAPI date format + */ + public static function date_php2mapi($date, $utc = true, $time = null) + { + // convert string to DateTime + if (!is_object($date) && !empty($date)) { + // convert date to datetime on 00:00:00 + if (preg_match('/^([0-9]{4})-?([0-9]{2})-?([0-9]{2})$/', $date, $m)) { + $date = $m[1] . '-' . $m[2] . '-' . $m[3] . 'T00:00:00+00:00'; + } + + $date = new DateTime($date); + } + + if (!is_object($date)) { + return; + } + + if ($utc) { + $date->setTimezone(new DateTimeZone('UTC')); + } + + if (!empty($time)) { + $date->setTime((int) $time['hour'], (int) $time['minute'], (int) $time['second']); + } + + // MAPI PTypTime is 64-bit integer representing the number + // of 100-nanosecond intervals since January 1, 1601. + // Mapistore format for this type is a float number + + // seconds since 1601-01-01 00:00:00 + $seconds = floatval($date->format('U')) + 11644473600; +/* + if ($microseconds = intval($date->format('u'))) { + $seconds += $microseconds/1000000; + } +*/ + return $seconds; + } + + /** + * Convert date-time from MAPI format to DateTime + */ + public static function date_mapi2php($date) + { + $seconds = floatval(sprintf('%.0f', $date)); + + // assumes we're working with dates after 1970-01-01 + $dt = new DateTime('@' . intval($seconds - 11644473600)); +/* + if ($microseconds = intval(($date - $seconds) * 1000000)) { + $dt = new DateTime($dt->format('Y-m-d H:i:s') . '.' . $microseconds, $dt->getTimezone()); + } +*/ + return $dt; + } + /** * Setting PidTagRecipientType according to [MS-OXCICAL 2.1.3.1.1.20.2] */ protected function to_recipient_type($cutype, $role) { if ($cutype && in_array($cutype, array('RESOURCE', 'ROOM'))) { return 0x00000003; } if ($role && ($type = $this->recipient_type_map[$role])) { return $type; } return 0x00000001; } + + /** + * Converts string of days (TU,TH) to bitmask used by MAPI + * + * @param string $days + * + * @return int + */ + protected static function day2bitmask($days, $prefix = '') + { + $days = explode(',', $days); + $result = 0; + + foreach ($days as $day) { + $result = $result + self::$recurrence_day_map[$prefix.$day]; + } + + return $result; + } + + /** + * Convert bitmask used by MAPI to string of days (TU,TH) + * + * @param int $days + * + * @return string + */ + protected static function bitmask2day($days) + { + $days_arr = array(); + + foreach (self::$recurrence_day_map as $day => $bit) { + if (($days & $bit) === $bit) { + $days_arr[] = preg_replace('/^BYDAY-/', '', $day); + } + } + + $result = implode(',', $days_arr); + + return $result; + } } diff --git a/lib/filter/mapistore/contact.php b/lib/filter/mapistore/contact.php index 14152e6..0e58721 100644 --- a/lib/filter/mapistore/contact.php +++ b/lib/filter/mapistore/contact.php @@ -1,580 +1,580 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_filter_mapistore_contact extends kolab_api_filter_mapistore_common { protected $map = array( // contact name properties [MS-OXOCNTC] 'PidTagNickname' => 'nickname', 'PidTagGeneration' => 'n.suffix', 'PidTagDisplayNamePrefix' => 'n.prefix', 'PidTagSurname' => 'n.surname', 'PidTagMiddleName' => 'n.additional', 'PidTagGivenName' => 'n.given', 'PidTagInitials' => 'x-custom.MAPI:PidTagInitials', 'PidTagDisplayName' => 'fn', 'PidLidYomiFirstName' => '', 'PidLidYomiLastName' => '', 'PidLidFileUnder' => '', 'PidLidFileUnderId' => '', // PtypInteger32 'PidLidFileUnderList' => '', // PtypMultipleInteger32 // electronic and phisical address properties 'PidTagPrimaryFaxNumber' => 'x-custom.MAPI:PidTagPrimaryFaxNumber', 'PidTagBusinessFaxNumber' => 'x-custom.MAPI:PidTagBusinessFaxNumber', 'PidTagHomeFaxNumber' => '', 'PidTagHomeAddressStreet' => '', 'PidTagHomeAddressCity' => '', 'PidTagHomeAddressStateOrProvince' => '', 'PidTagHomeAddressPostalCode' => '', 'PidTagHomeAddressCountry' => '', 'PidLidHomeAddressCountryCode' => '', 'PidTagHomeAddressPostOfficeBox' => '', 'PidLidHomeAddress' => '', // @TODO: ? 'PidLidWorkAddressStreet' => '', 'PidLidWorkAddressCity' => '', 'PidLidWorkAddressState' => '', 'PidLidWorkAddressPostalCode' => '', 'PidLidWorkAddressCountry' => '', 'PidLidWorkAddressCountryCode' => '', 'PidLidWorkAddressPostOfficeBox' => '', 'PidLidWorkAddress' => '', // @TODO: ? 'PidTagOtherAddressStreet' => '', 'PidTagOtherAddressCity' => '', 'PidTagOtherAddressStateOrProvince' => '', 'PidTagOtherAddressPostalCode' => '', 'PidTagOtherAddressCountry' => '', 'PidLidOtherAddressCountryCode' => '', 'PidTagOtherAddressPostOfficeBox' => '', 'PidLidOtherAddress' => '', // @TODO: ? 'PidTagStreetAddress' => '', // @TODO: ? 'PidTagLocality' => '', // @TODO: ? 'PidTagStateOrProvince' => '', // @TODO: ? 'PidTagPostalCode' => '', // @TODO: ? 'PidTagCountry' => '', // @TODO: ? 'PidLidAddressCountryCode' => '', // @TODO: ? 'PidTagPostOfficeBox' => '', // @TODO: ? 'PidTagPostalAddress' => '', // @TODO: ? 'PidTagPagerTelephoneNumber' => '', 'PidTagCallbackTelephoneNumber' => '', 'PidTagBusinessTelephoneNumber' => '', 'PidTagHomeTelephoneNumber' => '', 'PidTagPrimaryTelephoneNumber' => '', 'PidTagBusiness2TelephoneNumber' => '', 'PidTagMobileTelephoneNumber' => '', 'PidTagRadioTelephoneNumber' => '', 'PidTagCarTelephoneNumber' => '', 'PidTagOtherTelephoneNumber' => '', 'PidTagAssistantTelephoneNumber' => '', 'PidTagHome2TelephoneNumber' => 'x-custom.MAPI:PidTagHome2TelephoneNumber', 'PidTagTelecommunicationsDeviceForDeafTelephoneNumber' => 'x-custom.MAPI:PidTagTelecommunicationsDeviceForDeafTelephoneNumber', 'PidTagCompanyMainTelephoneNumber' => 'x-custom.MAPI:PidTagCompanyMainTelephoneNumber', 'PidTagTelexNumber' => '', 'PidTagIsdnNumber' => '', 'PidTagBirthday' => 'bday', // PtypTime, UTC 'PidLidBirthdayLocal' => '', // PtypTime 'PidTagWeddingAnniversary' => 'anniversary', // PtypTime, UTC 'PidLidWeddingAnniversaryLocal' => '', // PtypTime // professional properties 'PidTagTitle' => 'title', 'PidTagCompanyName' => '', 'PidLidYomiCompanyName' => '', 'PidTagDepartmentName' => '', 'PidTagOfficeLocation' => 'x-custom.MAPI:PidTagOfficeLocation', 'PidTagManagerName' => '', 'PidTagAssistant' => '', 'PidTagProfession' => 'group.role', 'PidLidHasPicture' => '', // PtypBoolean, more about photo attachments in MS-OXOCNTC // other properties 'PidTagHobbies' => 'x-custom.MAPI:PidTagHobbies', 'PidTagSpouseName' => '', 'PidTagLanguage' => 'lang', 'PidTagLocation' => 'x-custom.MAPI:PidTagLocation', 'PidLidInstantMessagingAddress' => 'impp', 'PidTagOrganizationalIdNumber' => 'x-custom.MAPI:PidTagOrganizationalIdNumber', 'PidTagCustomerId' => 'x-custom.MAPI:PidTagCustomerId', 'PidTagGovernmentIdNumber' => 'x-custom.MAPI:PidTagGovernmentIdNumber', 'PidTagPersonalHomePage' => 'url', 'PidTagBusinessHomePage' => 'x-custom.MAPI:PidTagBussinessHomePage', 'PidTagFtpSite' => 'x-custom.MAPI:PidTagFtpSite', 'PidLidFreeBusyLocation' => 'fburl', 'PidTagChildrenNames' => '', // PtypMultipleString 'PidTagGender' => 'gender', 'PidTagUserX509Certificate' => 'key', // PtypMultipleBinary 'PidTagMessageClass' => '', // IPM.Contact, IPM.DistList 'PidTagBody' => 'note', 'PidTagLastModificationTime' => 'rev', /* // distribution lists [MS-OXOCNTC] 'PidLidDistributionListName' => '', // PtypString = PidTagDisplayName 'PidLidDistributionListMembers' => '', // PtypMultipleBinary 'PidLidDistributionListOneOffMembers' => '', // PtypMultipleBinary 'PidLidDistributionListChecksum' => '', // PtypInteger32 'PidLidDistributionListStream' => '', // PtypBinary */ ); protected $gender_map = array( 0 => '', 1 => 'F', 2 => 'M', ); protected $phone_map = array( 'PidTagPagerTelephoneNumber' => 'pager', 'PidTagBusinessTelephoneNumber' => 'work', 'PidTagHomeTelephoneNumber' => 'home', 'PidTagMobileTelephoneNumber' => 'cell', 'PidTagCarTelephoneNumber' => 'x-car', 'PidTagOtherTelephoneNumber' => 'textphone', 'PidTagBusinessFaxNumber' => 'faxwork', 'PidTagHomeFaxNumber' => 'faxhome', ); protected $email_map = array( 'PidLidEmail1EmailAddress' => 'home', 'PidLidEmail2EmailAddress' => 'work', 'PidLidEmail3EmailAddress' => 'other', ); /** * Convert Kolab to MAPI * * @param array Data * @param array Context (folder_uid, object_uid, object) * * @return array Data */ public function output($data, $context = null) { $result = array( // @TODO: IPM.DistList for groups 'PidTagMessageClass' => 'IPM.Contact', // mapistore REST API specific properties 'collection' => 'contacts', ); foreach ($this->map as $mapi_idx => $kolab_idx) { if (empty($kolab_idx)) { continue; } $value = $this->get_kolab_value($data, $kolab_idx); if ($value === null) { continue; } switch ($mapi_idx) { case 'PidTagGender': $value = (int) array_search($value, $this->gender_map); break; case 'PidTagPersonalHomePage': case 'PidLidInstantMessagingAddress': if (is_array($value)) { $value = $value[0]; } break; case 'PidTagBirthday': case 'PidTagWeddingAnniversary': case 'PidTagLastModificationTime': - $value = kolab_api_filter_mapistore::date_php2mapi($value, false); + $value = $this->date_php2mapi($value, false); break; case 'PidTagUserX509Certificate': foreach ((array) $value as $val) { if ($val && preg_match('|^data:application/pkcs7-mime;base64,|i', $val, $m)) { $result[$mapi_idx] = substr($val, strlen($m[0])); continue 3; } } $value = null; break; case 'PidTagTitle': if (is_array($value)) { $value = $value[0]; } break; } if ($value === null) { continue; } $result[$mapi_idx] = $value; } // contact photo attachment [MS-OXVCARD 2.1.3.2.4] if (!empty($data['photo'])) { // @TODO: check if photo is one of .bmp, .gif, .jpeg, .png // Photo in MAPI is handled as attachment // Set PidTagAttachmentContactPhoto=true on attachment object $result['PidLidHasPicture'] = true; } // Organization/Department $organization = $data['group']['org']; if (is_array($organization)) { $result['PidTagCompanyName'] = $organization[0]; $result['PidTagDepartmentName'] = $organization[1]; } else if ($organization !== null) { $result['PidTagCompanyName'] = $organization; } // Manager/Assistant $related = $data['group']['related']; if ($related && $related['parameters']) { $related = array($related); } foreach ((array) $related as $rel) { $type = $rel['parameters']['type']; if ($type == 'x-manager') { $result['PidTagManagerName'] = $rel['text']; } else if ($type == 'x-assistant') { $result['PidTagAssistant'] = $rel['text']; } } // Children, Spouse foreach ((array) $data['related'] as $rel) { $type = $rel['parameters']['type']; if ($type == 'child') { $result['PidTagChildrensNames'][] = $rel['text']; } else if ($type == 'spouse') { $result['PidTagSpouseName'] = $rel['text']; } } // Emails $email_map = array_flip($this->email_map); foreach ((array) $data['email'] as $email) { $type = is_array($email) ? $email['parameters']['type'] : 'other'; $key = $email_map[$type] ?: $email_map['other']; $result[$key] = is_array($email) ? $email['text'] : $email; } // Phone(s) $phone_map = array_flip($this->phone_map); $phones = $data['tel']; if ($phones && $phones['parameters']) { $phones = array($phones); } foreach ((array) $phones as $phone) { $type = implode('', (array)$phone['parameters']['type']); if ($phone['text'] && ($idx = $phone_map[$type])) { $result[$idx] = $phone['text']; } } // Addresses(s) $addresses = $data['adr']; if ($addresses && $addresses['parameters']) { $addresses = array($addresses); } foreach ((array) $addresses as $addr) { $type = $addr['parameters']['type']; $address = null; if ($type == 'home') { $address = array( 'PidTagHomeAddressStreet' => $addr['street'], 'PidTagHomeAddressCity' => $addr['locality'], 'PidTagHomeAddressStateOrProvince' => $addr['region'], 'PidTagHomeAddressPostalCode' => $addr['code'], 'PidTagHomeAddressCountry' => $addr['country'], 'PidTagHomeAddressPostOfficeBox' => $addr['pobox'], ); } else if ($type == 'work') { $address = array( 'PidLidWorkAddressStreet' => $addr['street'], 'PidLidWorkAddressCity' => $addr['locality'], 'PidLidWorkAddressState' => $addr['region'], 'PidLidWorkAddressPostalCode' => $addr['code'], 'PidLidWorkAddressCountry' => $addr['country'], 'PidLidWorkAddressPostOfficeBox' => $addr['pobox'], ); } if (!empty($address)) { $result = array_merge($result, array_filter($address)); } } $other_adr_map = array( 'street' => 'PidTagOtherAddressStreet', 'locality' => 'PidTagOtherAddressCity', 'region' => 'PidTagOtherAddressStateOrProvince', 'code' => 'PidTagOtherAddressPostalCode', 'country' => 'PidTagOtherAddressCountry', 'pobox' => 'PidTagOtherAddressPostOfficeBox', ); foreach ((array) $data['group']['adr'] as $idx => $value) { if ($value && ($key = $other_adr_map[$idx])) { $result[$key] = $value; } } $this->parse_common_props($result, $data, $context); return $result; } /** * Convert from MAPI to Kolab * * @param array Data * @param array Data of the object that is being updated * * @return array Data */ public function input($data, $object = null) { $result = array(); foreach ($this->map as $mapi_idx => $kolab_idx) { if (empty($kolab_idx)) { continue; } if (!array_key_exists($mapi_idx, $data)) { continue; } $value = $data[$mapi_idx]; switch ($mapi_idx) { case 'PidTagBirthday': case 'PidTagWeddingAnniversary': if ($value) { - $value = kolab_api_filter_mapistore::date_mapi2php($value); + $value = $this->date_mapi2php($value); $value = $value->format('Y-m-d'); } break; case 'PidTagLastModificationTime': if ($value) { - $value = kolab_api_filter_mapistore::date_mapi2php($value); + $value = $this->date_mapi2php($value); $value = $value->format('Y-m-d\TH:i:s\Z'); } break; case 'PidTagGender': $value = $this->gender_map[(int)$value]; break; case 'PidTagUserX509Certificate': if (!empty($value)) { $value = array('data:application/pkcs7-mime;base64,' . $value); } break; case 'PidTagPersonalHomePage': case 'PidLidInstantMessagingAddress': if (!empty($value)) { $value = array($value); } break; } $this->set_kolab_value($result, $kolab_idx, $value); } // MS-OXVCARD 2.1.3.2.1 if (!empty($data['PidTagNormalizedSubject']) && empty($data['PidTagDisplayName'])) { $result['fn'] = $data['PidTagNormalizedSubject']; } // Organization/Department if ($data['PidTagCompanyName']) { $result['group']['org'][] = $data['PidTagCompanyName']; } if (!empty($data['PidTagDepartmentName'])) { $result['group']['org'][] = $data['PidTagDepartmentName']; } // Manager if ($data['PidTagManagerName']) { $result['group']['related'][] = array( 'parameters' => array('type' => 'x-manager'), 'text' => $data['PidTagManagerName'], ); } // Assistant if ($data['PidTagAssistant']) { $result['group']['related'][] = array( 'parameters' => array('type' => 'x-assistant'), 'text' => $data['PidTagAssistant'], ); } // Spouse if ($data['PidTagSpouseName']) { $result['related'][] = array( 'parameters' => array('type' => 'spouse'), 'text' => $data['PidTagSpouseName'], ); } // Children foreach ((array) $data['PidTagChildrensNames'] as $child) { $result['related'][] = array( 'parameters' => array('type' => 'child'), 'text' => $child, ); } // Emails foreach ($this->email_map as $mapi_idx => $type) { if ($email = $data[$mapi_idx]) { $result['email'][] = array( 'parameters' => array('type' => $type), 'text' => $email, ); } } // Phone(s) foreach ($this->phone_map as $mapi_idx => $type) { if (array_key_exists($mapi_idx, $data)) { // first remove the old phone... if (!empty($object['tel'])) { foreach ($object['tel'] as $idx => $phone) { $pt = implode('', (array) $phone['parameters']['type']); if ($pt == $type) { unset($object['tel'][$idx]); } } } if ($tel = $data[$mapi_idx]) { if (preg_match('/^fax(work|home)$/', $type, $m)) { $type = array('fax', $m[1]); } // and add it to the list $result['tel'][] = array( 'parameters' => array('type' => $type), 'text' => $tel, ); } } } if (!empty($object['tel'])) { $result['tel'] = array_merge((array) $result['tel'], (array) $object['tel']); } // Home address $address = array(); $adr_map = array( 'PidTagHomeAddressStreet' => 'street', 'PidTagHomeAddressCity' => 'locality', 'PidTagHomeAddressStateOrProvince' => 'region', 'PidTagHomeAddressPostalCode' => 'code', 'PidTagHomeAddressCountry' => 'country', 'PidTagHomeAddressPostOfficeBox' => 'pobox', ); foreach ($adr_map as $mapi_idx => $idx) { if ($adr = $data[$mapi_idx]) { $address[$idx] = $adr; } } if (!empty($address)) { $type = array('parameters' => array('type' => 'home')); $result['adr'][] = array_merge($address, $type); } // Work address $address = array(); $adr_map = array( 'PidLidWorkAddressStreet' => 'street', 'PidLidWorkAddressCity' => 'locality', 'PidLidWorkAddressState' => 'region', 'PidLidWorkAddressPostalCode' => 'code', 'PidLidWorkAddressCountry' => 'country', 'PidLidWorkAddressPostOfficeBox' => 'pobox', ); foreach ($adr_map as $mapi_idx => $idx) { if ($adr = $data[$mapi_idx]) { $address[$idx] = $adr; } } if (!empty($address)) { $type = array('parameters' => array('type' => 'work')); $result['adr'][] = array_merge($address, $type); } // Office address $address = array(); $adr_map = array( 'PidTagOtherAddressStreet' => 'street', 'PidTagOtherAddressCity' => 'locality', 'PidTagOtherAddressStateOrProvince' => 'region', 'PidTagOtherAddressPostalCode' => 'code', 'PidTagOtherAddressCountry' => 'country', 'PidTagOtherAddressPostOfficeBox' => 'pobox', ); foreach ($adr_map as $mapi_idx => $idx) { if ($adr = $data[$mapi_idx]) { $address[$idx] = $adr; } } if (!empty($address)) { $result['group']['adr'] = array_merge($address, $type); } $this->convert_common_props($result, $data, $object); return $result; } /** * Returns the attributes names mapping */ public function map() { $map = array_filter($this->map); return $map; } } diff --git a/lib/filter/mapistore/event.php b/lib/filter/mapistore/event.php index 5ed2128..b9e73a4 100644 --- a/lib/filter/mapistore/event.php +++ b/lib/filter/mapistore/event.php @@ -1,378 +1,378 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_filter_mapistore_event extends kolab_api_filter_mapistore_common { protected $map = array( // common properties [MS-OXOCAL] 'PidLidAppointmentSequence' => 'sequence', // PtypInteger32 'PidLidBusyStatus' => '', // PtypInteger32, @TODO: X-MICROSOFT-CDO-BUSYSTATUS 'PidLidAppointmentAuxiliaryFlags' => '', // PtypInteger32 'PidLidLocation' => 'location', // PtypString 'PidLidAppointmentStartWhole' => 'dtstart', // PtypTime, UTC 'PidLidAppointmentEndWhole' => 'dtend', // PtypTime, UTC 'PidLidAppointmentDuration' => '', // PtypInteger32, optional 'PidLidAppointmentSubType' => '', // PtypBoolean 'PidLidAppointmentStateFlags' => '', // PtypInteger32 'PidLidResponseStatus' => '', // PtypInteger32 'PidLidRecurring' => '', // PtypBoolean 'PidLidIsRecurring' => '', // PtypBoolean 'PidLidClipStart' => '', // PtypTime 'PidLidClipEnd' => '', // PtypTime 'PidLidAllAttendeesString' => '', // PtypString 'PidLidToAttendeesString' => '', // PtypString 'PidLidCCAttendeesString' => '', // PtypString 'PidLidNonSendableTo' => '', // PtypString 'PidLidNonSendableCc' => '', // PtypString 'PidLidNonSendableBcc' => '', // PtypString 'PidLidNonSendToTrackStatus' => '', // PtypMultipleInteger32 'PidLidNonSendCcTrackStatus' => '', // PtypMultipleInteger32 'PidLidNonSendBccTrackStatus' => '', // PtypMultipleInteger32 'PidLidAppointmentUnsendableRecipients' => '', // PtypBinary, optional 'PidLidAppointmentNotAllowPropose' => '', // PtypBoolean, @TODO: X-MICROSOFT-CDO-DISALLOW-COUNTER 'PidLidGlobalObjectId' => '', // PtypBinary 'PidLidCleanGlobalObjectId' => '', // PtypBinary 'PidTagOwnerAppointmentId' => '', // PtypInteger32, @TODO: X-MICROSOFT-CDO-OWNERAPPTID 'PidTagStartDate' => '', // PtypTime 'PidTagEndDate' => '', // PtypTime 'PidLidCommonStart' => '', // PtypTime 'PidLidCommonEnd' => '', // PtypTime 'PidLidOwnerCriticalChange' => '', // PtypTime, @TODO: X-MICROSOFT-CDO-CRITICAL-CHANGE 'PidLidIsException' => '', // PtypBoolean 'PidTagResponseRequested' => '', // PtypBoolean 'PidTagReplyRequested' => '', // PtypBoolean 'PidLidTimeZoneStruct' => '', // PtypBinary 'PidLidTimeZoneDescription' => '', // PtypString 'PidLidAppointmentTimeZoneDefinitionRecur' => '', // PtypBinary 'PidLidAppointmentTimeZoneDefinitionStartDisplay' => '', // PtypBinary 'PidLidAppointmentTimeZoneDefinitionEndDisplay' => '', // PtypBinary 'PidLidAppointmentRecur' => '', // PtypBinary 'PidLidRecurrenceType' => '', // PtypInteger32 'PidLidRecurrencePattern' => '', // PtypString 'PidLidLinkedTaskItems' => '', // PtypMultipleBinary 'PidLidMeetingWorkspaceUrl' => '', // PtypString 'PidTagIconIndex' => '', // PtypInteger32 'PidLidAppointmentColor' => '', // PtypInteger32 'PidLidAppointmentReplyTime' => '', // @TODO: X-MICROSOFT-CDO-REPLYTIME 'PidLidIntendedBusyStatus' => '', // @TODO: X-MICROSOFT-CDO-INTENDEDSTATUS // calendar object properties [MS-OXOCAL] 'PidTagMessageClass' => '', 'PidLidSideEffects' => '', // PtypInteger32 'PidLidFExceptionAttendees' => '', // PtypBoolean 'PidLidClientIntent' => '', // PtypInteger32 // common props [MS-OXCMSG] 'PidTagSubject' => 'summary', 'PidTagBody' => 'description', 'PidTagHtml' => '', // @TODO: (?) 'PidTagNativeBody' => '', 'PidTagBodyHtml' => '', 'PidTagRtfCompressed' => '', 'PidTagInternetCodepage' => '', 'PidTagContentId' => '', 'PidTagBodyContentLocation' => '', 'PidTagImportance' => 'priority', 'PidTagSensitivity' => 'class', 'PidLidPrivate' => '', 'PidTagCreationTime' => 'created', 'PidTagLastModificationTime' => 'dtstamp', // reminder properties [MS-OXORMDR] 'PidLidReminderSet' => '', // PtypBoolean 'PidLidReminderSignalTime' => '', // PtypTime 'PidLidReminderDelta' => '', // PtypInteger32 'PidLidReminderTime' => '', // PtypTime 'PidLidReminderOverride' => '', // PtypBoolean 'PidLidReminderPlaySound' => '', // PtypBoolean 'PidLidReminderFileParameter' => '', // PtypString 'PidTagReplyTime' => '', // PtypTime 'PidLidReminderType' => '', // PtypInteger32 ); /** * Message importance for PidTagImportance as defined in [MS-OXCMSG] */ protected $importance = array( 0 => 0x00000000, 1 => 0x00000002, 2 => 0x00000002, 3 => 0x00000002, 4 => 0x00000002, 5 => 0x00000001, 6 => 0x00000000, 7 => 0x00000000, 8 => 0x00000000, 9 => 0x00000000, ); /** * Message sesnitivity for PidTagSensitivity as defined in [MS-OXCMSG] */ protected $sensitivity = array( 'public' => 0x00000000, 'personal' => 0x00000001, 'private' => 0x00000002, 'confidential' => 0x00000003, ); /** * Convert Kolab to MAPI * * @param array Data * @param array Context (folder_uid, object_uid, object) * * @return array Data */ public function output($data, $context = null) { $result = array( 'PidTagMessageClass' => 'IPM.Appointment', // mapistore REST API specific properties 'collection' => 'calendars', ); foreach ($this->map as $mapi_idx => $kolab_idx) { if (empty($kolab_idx)) { continue; } $value = $this->get_kolab_value($data, $kolab_idx); if ($value === null) { continue; } switch ($mapi_idx) { case 'PidTagSensitivity': $value = (int) $this->sensitivity[strtolower($value)]; break; case 'PidTagCreationTime': case 'PidTagLastModificationTime': - $value = kolab_api_filter_mapistore::date_php2mapi($value, true); + $value = $this->date_php2mapi($value, true); break; case 'PidTagImportance': $value = (int) $this->importance[(int) $value]; break; case 'PidLidAppointmentStartWhole': case 'PidLidAppointmentEndWhole': $dt = kolab_api_input_json::to_datetime($value); - $value = kolab_api_filter_mapistore::date_php2mapi($dt, true); + $value = $this->date_php2mapi($dt, true); // PidLidAppointmentTimeZoneDefinitionStartDisplay // PidLidAppointmentTimeZoneDefinitionEndDisplay // this is all-day event if ($dt->_dateonly) { $result['PidLidAppointmentSubType'] = 0x00000001; } break; } $result[$mapi_idx] = $value; } // Organizer if (!empty($data['organizer'])) { $this->attendee_to_recipient($data['organizer'], $result, true); } // Attendees [MS-OXCICAL 2.1.3.1.1.20.2] foreach ((array) $data['attendee'] as $attendee) { $this->attendee_to_recipient($attendee, $result); } // Alarms (MAPI supports only one) foreach ((array) $data['valarm'] as $alarm) { if ($alarm['properties'] && $alarm['properties']['action'] == 'DISPLAY' && ($duration = $alarm['properties']['trigger']['duration']) && ($delta = self::reminder_duration_to_delta($duration)) ) { $result['PidLidReminderDelta'] = $delta; $result['PidLidReminderSet'] = true; // PidLidReminderTime // PidLidReminderSignalTime break; } } // @TODO: PidLidAppointmentDuration // @TODO: exceptions, resources // Recurrence if ($rule = $this->recurrence_from_kolab($data, $result)) { $result['PidLidAppointmentRecur'] = $rule; } $this->parse_common_props($result, $data, $context); return $result; } /** * Convert from MAPI to Kolab * * @param array Data * @param array Data of the object that is being updated * * @return array Data */ public function input($data, $object = null) { $result = array(); foreach ($this->map as $mapi_idx => $kolab_idx) { if (empty($kolab_idx)) { continue; } if (!array_key_exists($mapi_idx, $data)) { continue; } $value = $data[$mapi_idx]; switch ($mapi_idx) { case 'PidTagImportance': $map = array( 0x00000002 => 1, 0x00000001 => 5, 0x00000000 => 9, ); $value = (int) $map[(int) $value]; break; case 'PidTagSensitivity': $map = array_flip($this->sensitivity); $value = $map[$value]; break; case 'PidTagCreationTime': case 'PidTagLastModificationTime': if ($value) { - $value = kolab_api_filter_mapistore::date_mapi2php($value); + $value = $this->date_mapi2php($value); $value = $value->format('Y-m-d\TH:i:s\Z'); } break; case 'PidLidAppointmentStartWhole': case 'PidLidAppointmentEndWhole': if ($value) { - $value = kolab_api_filter_mapistore::date_mapi2php($value); + $value = $this->date_mapi2php($value); $format = $data['PidLidAppointmentSubType'] ? 'Y-m-d' : 'Y-m-d\TH:i:s\Z'; $value = $value->format($format); } break; } $result[$kolab_idx] = $value; } // Alarms (MAPI supports only one, DISPLAY) if ($data['PidLidReminderSet'] && ($delta = $data['PidLidReminderDelta'])) { $duration = self::reminder_delta_to_duration($delta); $alarm = array( 'action' => 'DISPLAY', 'trigger' => array('duration' => $duration), 'description' => 'Reminder', ); $result['valarm'] = array(array('properties' => $alarm)); } else if (array_key_exists('PidLidReminderSet', $data) || array_key_exists('PidLidReminderDelta', $data)) { $result['valarm'] = array(); } // Recurrence if (array_key_exists('PidLidAppointmentRecur', $data)) { $this->recurrence_to_kolab($data['PidLidAppointmentRecur'], $result, 'event'); } if (array_key_exists('recipients', $data)) { $result['attendee'] = array(); $result['organizer'] = array(); foreach ((array) $data['recipients'] as $recipient) { $this->recipient_to_attendee($recipient, $result); } } // @TODO: PidLidAppointmentDuration (?) // @TODO: exception, resources $this->convert_common_props($result, $data, $object); return $result; } /** * Returns the attributes names mapping */ public function map() { $map = array_filter($this->map); // @TODO: add properties that are not in the map $map['PidLidAppointmentRecur'] = 'rrule'; return $map; } /** * Convert PidLidReminderDelta value into xCal duration */ protected static function reminder_delta_to_duration($delta) { if ($delta == 0x5AE980E1) { $delta = 15; } $delta = (int) $delta; return "-PT{$delta}M"; } /** * Convert Kolab alarm duration into PidLidReminderDelta */ protected static function reminder_duration_to_delta($duration) { if ($duration && preg_match('/^-[PT]*([0-9]+)([WDHMS])$/', $duration, $matches)) { $value = intval($matches[1]); switch ($matches[2]) { case 'S': $value = intval(round($value/60)); break; case 'H': $value *= 60; break; case 'D': $value *= 24 * 60; break; case 'W': $value *= 7 * 24 * 60; break; } return $value; } } } diff --git a/lib/filter/mapistore/folder.php b/lib/filter/mapistore/folder.php index 10816aa..0cacf3e 100644 --- a/lib/filter/mapistore/folder.php +++ b/lib/filter/mapistore/folder.php @@ -1,167 +1,167 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_filter_mapistore_folder extends kolab_api_filter_mapistore_common { protected $map = array( - // read-only properties + // [MS-OXCFOLD] read-only properties 'PidTagAccess' => '', 'PidTagChangeKey' => '', 'PidTagCreationTime' => '', // PtypTime, @TODO: store in folder annotation? 'PidTagLastodificationTime' => '', // PtypTime 'PidTagContentCount' => '', // PtypInteger32 'PidTagContentUnreadCount' => '', // PtypInteger32 'PidTagDeletedOn' => '', // PtypTime // 'PidTagAddressbookEntryId' => '', // PtypBinary 'PidTagFolderId' => '', // PtypInteger64 - 'PidTagHierarchyChangeNumber' => '', // PtypInteger32, number of subfolders + 'PidTagHierarchyChangeNumber' => '', // PtypInteger32, 'PidTagMessageSize' => '', // PtypInteger32, size of all messages 'PidTagMessageSizeExtended' => '', // PtypInteger64 'PidTagSubfolders' => '', // PtypBoolean 'PidTagLocalCommitTime' => '', // PtypTime, last change time in UTC 'PidTagLocalCommitTimeMax' => '', // PtypTime 'PidTagDeletedCountTotal' => '', // PtypInteger32 // read-write properties - 'PidTagAttributeHidden' => '', // Ptypboolean + 'PidTagAttributeHidden' => '', // PtypBoolean 'PidTagComment' => '', // PtypString, @TODO: store in folder annotation? 'PidTagContainerClass' => 'type', // PtypString, IPF.* 'PidTagContainerHierarchy' => '', // PtypObject 'PidTagDisplayName' => 'name', // PtypString 'PidTagFolderAssociatedContents' => '', // PtypObject 'PidTagFolderType' => '', // PtypInteger32, 0 - namespace roots, 1 - other, 2 - virtual/search folders 'PidTagRights' => '', // PtypInteger32 'PidTagAccessControlListData' => '', // PtypBinary, see [MS-OXCPERM] ); protected $type_map = array( '' => 'IPF.Note', 'mail' => 'IPF.Note', 'task' => 'IPF.Task', 'note' => 'IPF.StickyNote', 'event' => 'IPF.Appointment', 'journal' => 'IPF.Journal', 'contact' => 'IPF.Contact', ); /** * Convert Kolab to MAPI * * @param array Data * @param array Context (folder_uid, object_uid, object) * * @return array Data */ public function output($data, $context = null) { list($type, ) = explode('.', $data['type']); $type = $this->type_map[(string)$type]; // skip folders of unsupported type if (empty($type)) { return; } // skip folders that are not subfolders of the specified folder, // in list-mode MAPI always requests for one-level of the hierarchy (?) if ($api->input->path[1] == 'folders') { $api = kolab_api::get_instance(); $parent = !empty($api->input->path) ? $api->input->path[0] : ''; if ($data['parent'] != $parent) { return; } } $result = array( // mapistore properties 'id' => $data['uid'], // MAPI properties 'PidTagFolderType' => 1, 'PidTagDisplayName' => $data['name'], 'PidTagContainerClass' => $type, ); if ($data['parent']) { $result['parent_id'] = $data['parent']; } $result = array_filter($result, function($v) { return $v !== null; }); return $result; } /** * Convert from MAPI to Kolab * * @param array Data * @param array Data of the object that is being updated * * @return array Data */ public function input($data, $object = null) { $result = array(); // mapistore properties if ($data['id']) { $result['uid'] = $data['id']; } if ($data['parent_id']) { $result['parent'] = $data['parent_id']; } // MAPI properties if ($data['PidTagDisplayName']) { $result['name'] = $data['PidTagDisplayName']; } if ($data['PidTagContainerClass']) { // @TODO: what if folder is already a *.default or *.sentitems, etc. // we should keep the subtype intact $map = array_flip($this->type_map); $result['type'] = $map[$data['PidTagContainerClass']]; } return $result; } /** * Returns the attributes names mapping */ public function map() { $map = array_filter($this->map); $map['parent_id'] = 'parent'; $map['PidTagContainerClass'] = 'type'; $map['PidTagFolderType'] = 'PidTagFolderType'; return $map; } } diff --git a/lib/filter/mapistore/structure/appointmentrecurrencepattern.php b/lib/filter/mapistore/structure/appointmentrecurrencepattern.php index 1a4a5eb..c5193b0 100644 --- a/lib/filter/mapistore/structure/appointmentrecurrencepattern.php +++ b/lib/filter/mapistore/structure/appointmentrecurrencepattern.php @@ -1,69 +1,69 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ /** * AppointmentRecurrencePattern structure definition according to MS-OXOCAL 2.2.1.44.5 */ class kolab_api_filter_mapistore_structure_appointmentrecurrencepattern extends kolab_api_filter_mapistore_structure { protected $structure = array( 'RecurrencePattern' => array('type' => 'kolab_api_filter_mapistore_structure_recurrencepattern'), 'ReaderVersion' => array('type' => 'ULONG', 'default' => 0x00003006), 'WriterVersion' => array('type' => 'ULONG', 'default' => 0x00003009), 'StartTimeOffset' => array('type' => 'ULONG'), 'EndTimeOffset' => array('type' => 'ULONG'), 'ExceptionCount' => array('type' => 'WORD'), 'ExceptionInfo' => array('type' => '[kolab_api_filter_mapistore_structure_exceptioninfo]', 'counter' => 'ExceptionCount'), 'ReservedBlock1Size' => array('type' => 'ULONG', 'default' => 0), 'ReservedBlock1' => array('type' => 'STRING', 'counter' => 'ReservedBlock1Size'), 'ExtendedException' => array('type' => '[kolab_api_filter_mapistore_structure_extendedexception]', 'counter' => 'ExceptionCount'), 'ReservedBlock2Size' => array('type' => 'ULONG', 'default' => 0), 'ReservedBlock2' => array('type' => 'STRING', 'counter' => 'ReservedBlock2Size'), ); /** * Convert internal structure into binary string * * @param bool $base64 Enables base64 encoding of the output * * @return string Binary representation of the structure */ public function output($base64 = false) { - if (count($this->data['ExceptionInfo']) != count($this->ExtendedException)) { + if (count($this->data['ExceptionInfo']) != count($this->data['ExtendedException'])) { throw new kolab_api_exception(kolab_api_exception::SERVER_ERROR, array( 'line' => __LINE__, 'file' => __FILE__, 'message' => 'ExceptionInfo and ExtendedException need to be of the same size' )); } $this->data['ExceptionCount'] = count($this->data['ExceptionInfo']); $this->data['ReservedBlock1Size'] = strlen($this->data['ReservedBlock1']); $this->data['ReservedBlock2Size'] = strlen($this->data['ReservedBlock2']); return parent::output($base64); } } diff --git a/lib/filter/mapistore/structure/changehighlight.php b/lib/filter/mapistore/structure/changehighlight.php index bbd45c6..7a3afef 100644 --- a/lib/filter/mapistore/structure/changehighlight.php +++ b/lib/filter/mapistore/structure/changehighlight.php @@ -1,73 +1,73 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ /** * ChangeHighlight structure definition according to MS-OXOCAL 2.2.1.44.3 */ class kolab_api_filter_mapistore_structure_changehighlight extends kolab_api_filter_mapistore_structure { protected $structure = array( 'ChangeHighlightSize' => array('type' => 'ULONG'), - 'ChangeHighlightValue' => array('type' => 'ULONG'), + 'ChangeHighlightValue' => array('type' => 'ULONG', 'default' => 0), 'Reserved' => array('type' => 'STRING'), ); /** * Convert binary input into internal structure * * @param string $input Binary representation of the structure * @param bool $base64 Set to TRUE if the input is base64-encoded * * @return int Number of bytes read from the binary input */ public function input($input, $base64 = false) { if ($base64) { $input = base64_decode($input); } // Read size $unpack = unpack('V', substr($input, 0, 4)); $value = $unpack[1]; $this->structure['Reserved']['length'] = $value - 4; return parent::input($input, false); } /** * Convert internal structure into binary string * * @param bool $base64 Enables base64 encoding of the output * * @return string Binary representation of the structure */ public function output($base64 = false) { $this->data['ChangeHighlightSize'] = strlen($this->data['Reserved']) + 4; return parent::output($base64); } } diff --git a/lib/filter/mapistore/structure/extendedexception.php b/lib/filter/mapistore/structure/extendedexception.php index 292f175..63a0d23 100644 --- a/lib/filter/mapistore/structure/extendedexception.php +++ b/lib/filter/mapistore/structure/extendedexception.php @@ -1,130 +1,142 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ /** * ExtendedException structure definition according to MS-OXOCAL 2.2.1.44.4 */ class kolab_api_filter_mapistore_structure_extendedexception extends kolab_api_filter_mapistore_structure { protected $parent; protected $structure = array( 'ChangeHighlight' => array('type' => 'kolab_api_filter_mapistore_structure_changehighlight'), 'ReservedBlockEE1Size' => array('type' => 'ULONG', 'default' => 0), 'ReservedBlockEE1' => array('type' => 'STRING', 'counter' => 'ReservedBlockEE1Size'), 'StartDateTime' => array('type' => 'ULONG'), 'EndDateTime' => array('type' => 'ULONG'), 'OriginalStartDate' => array('type' => 'ULONG'), 'WideCharSubjectLength' => array('type' => 'WORD',), 'WideCharSubject' => array('type' => 'WSTRING', 'counter' => 'WideCharSubjectLength'), 'WideCharLocationLength' => array('type' => 'WORD'), 'WideCharLocation' => array('type' => 'WSTRING', 'counter' => 'WideCharLocationLength'), 'ReservedBlockEE2Size' => array('type' => 'ULONG', 'default' => 0), 'ReservedBlockEE2' => array('type' => 'STRING', 'counter' => 'ReservedBlockEE2Size'), ); /** * Convert binary input into internal structure * * @param string $input Binary representation of the structure * @param bool $base64 Set to TRUE if the input is base64-encoded * @param object $parent Parent structure * @param int $index Index in the parent property array * * @return int Number of bytes read from the binary input */ public function input($input, $base64 = false, $parent = null, $index = null) { if ($base64) { $input = base64_decode($input); } // read OverrideFlags from matching ExceptionInfo if (empty($parent) || $index === null || !array_key_exists($index, (array) $parent->ExceptionInfo)) { throw new kolab_api_exception(kolab_api_exception::SERVER_ERROR, array( 'line' => __LINE__, 'file' => __FILE__, 'message' => 'Missing ExceptionInfo structure for ' . get_class($this) )); } $flags = $parent->ExceptionInfo[$index]->OverrideFlags; if (!($flags & kolab_api_filter_mapistore_structure_exceptioninfo::OVERRIDEFLAGS_ARO_SUBJECT)) { $no_subject = true; $this->structure['WideCharSubject']['type'] = 'EMPTY'; $this->structure['WideCharSubjectLength']['type'] = 'EMPTY'; } if (!($flags & kolab_api_filter_mapistore_structure_exceptioninfo::OVERRIDEFLAGS_ARO_LOCATION)) { $no_location = true; $this->structure['WideCharLocation']['type'] = 'EMPTY'; $this->structure['WideCharLocationLength']['type'] = 'EMPTY'; } if ($no_subject && $no_location) { $this->structure['StartDateTime']['type'] = 'EMPTY'; $this->structure['EndDateTime']['type'] = 'EMPTY'; $this->structure['OriginalStartDate']['type'] = 'EMPTY'; } return parent::input($input, false); } /** * Convert internal structure into binary string * * @param bool $base64 Enables base64 encoding of the output * * @return string Binary representation of the structure */ public function output($base64 = false) { if ($this->data['WideCharSubject'] !== null) { $got_subject = true; $this->data['WideCharSubjectLength'] = mb_strlen($this->data['WideCharSubject']); } else { $this->structure['WideCharSubjectLength']['type'] = 'EMPTY'; $this->structure['WideCharSubject']['type'] = 'EMPTY'; } if ($this->data['WideCharLocation'] !== null) { $got_location = true; $this->data['WideCharLocationLength'] = mb_strlen($this->data['WideCharLocation']); } else { $this->structure['WideCharLocationLength']['type'] = 'EMPTY'; $this->structure['WideCharLocation']['type'] = 'EMPTY'; } if (!$got_subject && !$got_location) { $this->structure['StartDateTime']['type'] = 'EMPTY'; $this->structure['EndDateTime']['type'] = 'EMPTY'; $this->structure['OriginalStartDate']['type'] = 'EMPTY'; } return parent::output($base64); } + + /** + * Returns instance of this class with default values set + * + * @return kolab_api_filter_mapistore_structure_extendedexception Object instance + */ + public static function get_empty() + { + return new self(array( + 'ChangeHighlight' => new kolab_api_filter_mapistore_structure_changehighlight(), + )); + } } diff --git a/lib/filter/mapistore/task.php b/lib/filter/mapistore/task.php index f3fb709..09dfec8 100644 --- a/lib/filter/mapistore/task.php +++ b/lib/filter/mapistore/task.php @@ -1,290 +1,287 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_filter_mapistore_task extends kolab_api_filter_mapistore_common { protected $map = array( // task specific props [MS-OXOTASK] 'PidTagProcessed' => '', // PtypBoolean 'PidLidTaskMode' => '', // ignored 'PidLidTaskStatus' => '', // PtypInteger32 'PidLidPercentComplete' => 'percent-complete', // PtypFloating64 'PidLidTaskStartDate' => 'dtstart', // PtypTime 'PidLidTaskDueDate' => 'due', // PtypTime 'PidLidTaskResetReminder' => '', // @TODO // PtypBoolean 'PidLidTaskAccepted' => '', // @TODO // PtypBoolean 'PidLidTaskDeadOccurrence' => '', // @TODO // PtypBoolean 'PidLidTaskDateCompleted' => 'x-custom.MAPI:PidLidTaskDateCompleted', // PtypTime 'PidLidTaskLastUpdate' => '', // PtypTime 'PidLidTaskActualEffort' => 'x-custom.MAPI:PidLidTaskActualEffort', // PtypInteger32 'PidLidTaskEstimatedEffort' => 'x-custom.MAPI:PidLidTaskEstimatedEffort', // PtypInteger32 'PidLidTaskVersion' => '', // PtypInteger32 'PidLidTaskState' => '', // PtypInteger32 'PidLidTaskRecurrence' => '', // PtypBinary 'PidLidTaskAssigners' => '', // PtypBinary 'PidLidTaskStatusOnComplete' => '', // PtypBoolean 'PidLidTaskHistory' => '', // @TODO: ? // PtypInteger32 'PidLidTaskUpdates' => '', // PtypBoolean 'PidLidTaskComplete' => '', // PtypBoolean 'PidLidTaskFCreator' => '', // PtypBoolean 'PidLidTaskOwner' => '', // @TODO // PtypString 'PidLidTaskMultipleRecipients' => '', // PtypBoolean 'PidLidTaskAssigner' => '', // PtypString 'PidLidTaskLastUser' => '', // PtypString 'PidLidTaskOrdinal' => '', // PtypInteger32 'PidLidTaskLastDelegate' => '', // PtypString 'PidLidTaskFRecurring' => '', // PtypBoolean 'PidLidTaskOwnership' => '', // @TODO // PtypInteger32 'PidLidTaskAcceptanceState' => '', // PtypInteger32 'PidLidTaskFFixOffline' => '', // PtypBoolean 'PidLidTaskGlobalId' => '', // @TODO // PtypBinary 'PidLidTaskCustomFlags' => '', // ignored 'PidLidTaskRole' => '', // ignored 'PidLidTaskNoCompute' => '', // ignored 'PidLidTeamTask' => '', // ignored // common props [MS-OXCMSG] 'PidTagSubject' => 'summary', 'PidTagBody' => 'description', 'PidTagHtml' => '', // @TODO: (?) 'PidTagNativeBody' => '', 'PidTagBodyHtml' => '', 'PidTagRtfCompressed' => '', 'PidTagInternetCodepage' => '', 'PidTagMessageClass' => '', 'PidLidCommonStart' => 'dtstart', 'PidLidCommonEnd' => 'due', 'PidTagIconIndex' => '', // @TODO 'PidTagCreationTime' => 'created', // PtypTime, UTC 'PidTagLastModificationTime' => 'dtstamp', // PtypTime, UTC ); /** * Values for PidLidTaskStatus property */ protected $status_map = array( 'none' => 0x00000000, // PidLidPercentComplete = 0 'in-progress' => 0x00000001, // PidLidPercentComplete > 0 and PidLidPercentComplete < 1 'complete' => 0x00000002, // PidLidPercentComplete = 1 'waiting' => 0x00000003, 'deferred' => 0x00000004, ); /** * Values for PidLidTaskHistory property */ protected $history_map = array( 'none' => 0x00000000, 'accepted' => 0x00000001, 'rejected' => 0x00000002, 'changed' => 0x00000003, 'due-changed' => 0x00000004, 'assigned' => 0x00000005, ); /** * Convert Kolab to MAPI * * @param array Data * @param array Context (folder_uid, object_uid, object) * * @return array Data */ public function output($data, $context = null) { $result = array( 'PidTagMessageClass' => 'IPM.Task', // mapistore REST API specific properties 'collection' => 'tasks', ); foreach ($this->map as $mapi_idx => $kolab_idx) { if (empty($kolab_idx)) { continue; } $value = $this->get_kolab_value($data, $kolab_idx); if ($value === null) { continue; } switch ($mapi_idx) { case 'PidLidPercentComplete': $value /= 100; break; case 'PidLidTaskStartDate': case 'PidLidTaskDueDate': - $value = kolab_api_filter_mapistore::date_php2mapi($value, false, array('hour' => 0)); + $value = $this->date_php2mapi($value, false, array('hour' => 0)); break; case 'PidLidCommonStart': case 'PidLidCommonEnd': - $value = kolab_api_filter_mapistore::date_php2mapi($value, true); - break; - // case 'PidLidTaskLastUpdate': case 'PidTagCreationTime': case 'PidTagLastModificationTime': - $value = kolab_api_filter_mapistore::date_php2mapi($value, true); + $value = $this->date_php2mapi($value, true); break; case 'PidLidTaskActualEffort': case 'PidLidTaskEstimatedEffort': $value = (int) $value; break; } if ($value === null) { continue; } $result[$mapi_idx] = $value; } // set status $percent = $result['PidLidPercentComplete']; if ($precent == 1) { $result['PidLidTaskStatus'] = $this->status_map['complete']; // PidLidTaskDateCompleted (?) } else if ($precent > 0) { $result['PidLidTaskStatus'] = $this->status_map['in-progress']; } else { $result['PidLidTaskStatus'] = $this->status_map['none']; } // Organizer if (!empty($data['organizer'])) { $this->attendee_to_recipient($data['organizer'], $result, true); } // Attendees [MS-OXCICAL 2.1.3.1.1.20.2] foreach ((array) $data['attendee'] as $attendee) { $this->attendee_to_recipient($attendee, $result); } // Recurrence if ($rule = $this->recurrence_from_kolab($data, $result)) { $result['PidLidTaskRecurrence'] = $rule; $result['PidLidTaskFRecurring'] = true; } $this->parse_common_props($result, $data, $context); return $result; } /** * Convert from MAPI to Kolab * * @param array Data * @param array Data of the object that is being updated * * @return array Data */ public function input($data, $object = null) { $result = array(); foreach ($this->map as $mapi_idx => $kolab_idx) { if (empty($kolab_idx)) { continue; } if (!array_key_exists($mapi_idx, $data)) { continue; } $value = $data[$mapi_idx]; switch ($mapi_idx) { case 'PidLidPercentComplete': $value = intval($value * 100); break; case 'PidLidTaskStartDate': case 'PidLidTaskDueDate': if (intval($value) !== 0x5AE980E0) { - $value = kolab_api_filter_mapistore::date_mapi2php($value); + $value = $this->date_mapi2php($value); $value = $value->format('Y-m-d'); } break; case 'PidLidCommonStart': case 'PidLidCommonEnd': -// $value = kolab_api_filter_mapistore::date_mapi2php($value, true); +// $value = $this->date_mapi2php($value, true); break; case 'PidTagCreationTime': case 'PidTagLastModificationTime': if ($value) { - $value = kolab_api_filter_mapistore::date_mapi2php($value); + $value = $this->date_mapi2php($value); $value = $value->format('Y-m-d\TH:i:s\Z'); } break; } $result[$kolab_idx] = $value; } if ($data['PidLidTaskComplete']) { $result['status'] = 'COMPLETED'; } // Recurrence if (array_key_exists('PidLidTaskRecurrence', $data)) { $this->recurrence_to_kolab($data['PidLidTaskRecurrence'], $result, 'task'); } if (array_key_exists('recipients', $data)) { $result['attendee'] = array(); $result['organizer'] = array(); foreach ((array) $data['recipients'] as $recipient) { $this->recipient_to_attendee($recipient, $result); } } $this->convert_common_props($result, $data, $object); return $result; } /** * Returns the attributes names mapping */ public function map() { $map = array_filter($this->map); $map['PidLidTaskRecurrence'] = 'rrule'; return $map; } } diff --git a/lib/input/json.php b/lib/input/json.php index 614a2e3..9f01206 100644 --- a/lib/input/json.php +++ b/lib/input/json.php @@ -1,217 +1,248 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_input_json extends kolab_api_input { /** * Get request data (JSON) * * @param string Expected object type * @param bool Disable filters application * @param array Original object data (set on update requests) * * @return array Request data */ public function input($type = null, $disable_filters = false, $original = null) { if ($this->input_body === null) { $data = file_get_contents('php://input'); $data = trim($data); $data = json_decode($data, true); $this->input_body = $data; } if (!$disable_filters) { if ($this->filter) { if (!empty($original)) { // convert object data into API format $data = $this->api->get_object_data($original, $type); } $this->filter->input_body($this->input_body, $type, $data); } // convert input to internal kolab_storage format if ($type) { $class = "kolab_api_input_json_$type"; $model = new $class; $model->input($this->input_body, $original); } } return $this->input_body; } /** * Convert xCard/xCal date and date-time into internal DateTime * * @param array|string Date or Date-Time * * @return DateTime */ public static function to_datetime($input) { if (empty($input)) { return; } if (is_array($input)) { if ($input['date-time']) { if ($input['parameters']['tzid']) { $tzid = str_replace('/kolab.org/', '', $input['parameters']['tzid']); } else { $tzid = 'UTC'; } $datetime = $input['date-time']; try { $timezone = new DateTimeZone($tzid); } catch (Exception $e) {} } else if ($input['timestamp']) { $datetime = $input['timestamp']; } else if ($input['date']) { $datetime = $input['date']; $is_date = true; } else { return; } } else { $datetime = $input; $is_date = preg_match('/^[0-9]{4}-?[0-9]{2}-?[0-9]{2}$/', $input); } try { $dt = new DateTime($datetime, $timezone ?: new DateTimeZone('UTC')); } catch (Exception $e) { return; } if ($is_date) { $dt->_dateonly = true; $dt->setTime(0, 0, 0); } return $dt; } /** * Add x-custom fields to the result */ public static function add_x_custom($data, &$result) { if (array_key_exists('x-custom', (array) $data)) { $value = (array) $data['x-custom']; foreach ((array) $value as $idx => $v) { if ($v['identifier'] && $v['value'] !== null) { $value[$idx] = array($v['identifier'], $v['value']); } else { unset($value[$idx]); } } $result['x-custom'] = $value; } } /** * Parse mailto URI, e.g. attendee/cal-address property * * @param string $uri Mailto: uri * @param string $params Element parameters * * @return string E-mail address */ public static function parse_mailto_uri($uri, &$params = array()) { if (strpos($uri, 'mailto:') === 0) { $uri = substr($uri, 7); $uri = rawurldecode($uri); $emails = rcube_mime::decode_address_list($uri, 1, true, null, false); $email = $emails[1]; if (!empty($email['mailto'])) { if (empty($params['cn']) && !empty($email['name'])) { $params['cn'] = $email['name']; } return $email['mailto']; } } } /** * Parse attendees property input * * @param array $attendees Attendees list * * @return array Attendees list in kolab_format_xcal format */ public static function parse_attendees($attendees) { foreach ((array) $attendees as $idx => $attendee) { $params = $attendee['parameters']; $email = kolab_api_input_json::parse_mailto_uri($attendee['cal-address'], $params); foreach (array('to', 'from') as $val) { foreach ((array) $params['delegated-' . $val] as $del) { if ($del_email = kolab_api_input_json::parse_mailto_uri($del, $params)) { $delegated[$val][] = $del_email; } } } if ($email) { $attendees[$idx] = array_filter(array( 'email' => $email, 'name' => $params['cn'], 'status' => $params['partstat'], 'role' => $params['role'], 'rsvp' => (bool) $params['rsvp'] || strtoupper($params['rsvp']) === 'TRUE', 'cutype' => $params['cutype'], 'dir' => $params['dir'], 'delegated-to' => $delegated['to'], 'delegated-from' => $delegated['from'], )); } else { unset($attendees[$idx]); } } return $attendees; } + + /** + * Handle recurrence rule input + * + * @param array $data Input data + * @param array $result Result data + */ + public static function parse_recurrence($data, &$result) + { + // Recurrence: deleted exceptions (EXDATE) + if (array_key_exists('exdate', $data)) { + $result['recurrence']['EXDATE'] = array(); + if (!empty($data['exdate']['date'])) { + $result['recurrence']['EXDATE'] = (array) $data['exdate']['date']; + } + else if (!empty($data['exdate']['date-time'])) { + $result['recurrence']['EXDATE'] = (array) $data['exdate']['date-time']; + } + } + + // Recurrence (RDATE) + if (array_key_exists('rdate', $data)) { + $result['recurrence']['RDATE'] = array(); + if (!empty($data['rdate']['date'])) { + $result['recurrence']['RDATE'] = (array) $data['rdate']['date']; + } + else if (!empty($data['exdate']['date-time'])) { + $result['recurrence']['RDATE'] = (array) $data['rdate']['date-time']; + } + } + } } diff --git a/lib/input/json/event.php b/lib/input/json/event.php index 70a0e61..66f5f1f 100644 --- a/lib/input/json/event.php +++ b/lib/input/json/event.php @@ -1,139 +1,130 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_input_json_event { // map xml/json attributes into internal (kolab_format) protected $field_map = array( 'description' => 'description', 'title' => 'summary', 'sensitivity' => 'class', 'sequence' => 'sequence', 'categories' => 'categories', 'created' => 'created', 'changed' => 'dtstamp', 'attendees' => 'attendee', 'organizer' => 'organizer', 'recurrence' => 'rrule', 'start' => 'dtstart', 'end' => 'dtend', 'valarms' => 'valarms', 'location' => 'location', 'priority' => 'priority', 'status' => 'status', 'url' => 'url', ); /** * Convert event input array into an array that can * be handled by kolab_storage_folder::save() * * @param array Request body * @param array Original object data (on update) */ public function input(&$data, $original = null) { if (empty($data) || !is_array($data)) { throw new kolab_api_exception(kolab_api_exception::INVALID_REQUEST); } // require at least 'dtstart' property for new objects if (empty($original) && empty($data['dtstart'])) { throw new kolab_api_exception(kolab_api_exception::INVALID_REQUEST); } foreach ($this->field_map as $kolab => $api) { if (!array_key_exists($api, $data)) { continue; } $value = $data[$api]; switch ($kolab) { case 'sensitivity': if ($value) { $value = strtolower($value); } break; case 'url': if (is_array($value)) { $value = $value[0]; } break; case 'created': case 'changed': case 'start': case 'end': $value = kolab_api_input_json::to_datetime($value); break; case 'attendees': $value = kolab_api_input_json::parse_attendees($value); break; case 'organizer': if (!empty($value)) { $value = kolab_api_input_json::parse_attendees(array($value)); $value = $value[0]; } break; } $result[$kolab] = $value; } // @TODO: recurrence // @TODO: exceptions // @TOOD: alarms - // Recurrence: deleted exceptions (EXDATE) - if (array_key_exists('exdate', $data)) { - $result['recurrence']['EXDATE'] = array(); - if (!empty($data['exdate']['date'])) { - $result['recurrence']['EXDATE'] = (array) $data['exdate']['date']; - } - else if (!empty($data['exdate']['date-time'])) { - $result['recurrence']['EXDATE'] = (array) $data['exdate']['date-time']; - } - } + kolab_api_input_json::parse_recurrence($data, $result); // x-custom fields kolab_api_input_json::add_x_custom($data, $result); // @TODO: should we require event summary/title? if (empty($result)) { throw new kolab_api_exception(kolab_api_exception::INVALID_REQUEST); } if (!empty($original)) { $result = array_merge($original, $result); } $data = $result; } } diff --git a/lib/input/json/task.php b/lib/input/json/task.php index bc45401..6e45361 100644 --- a/lib/input/json/task.php +++ b/lib/input/json/task.php @@ -1,123 +1,125 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_input_json_task { // map xml/json attributes into internal (kolab_format) protected $field_map = array( 'description' => 'description', 'title' => 'summary', 'sensitivity' => 'class', 'sequence' => 'sequence', 'categories' => 'categories', 'created' => 'created', 'changed' => 'dtstamp', 'complete' => 'percent-complete', 'status' => 'status', 'start' => 'dtstart', 'due' => 'due', 'parent_id' => 'related-to', 'location' => 'location', 'priority' => 'priority', 'url' => 'url', 'attendees' => 'attendee', 'organizer' => 'organizer', + 'recurrence' => 'rrule', ); /** * Convert task input array into an array that can * be handled by kolab_storage_folder::save() * * @param array Request body * @param array Original object data (on update) */ public function input(&$data, $original = null) { if (empty($data) || !is_array($data)) { throw new kolab_api_exception(kolab_api_exception::INVALID_REQUEST); } foreach ($this->field_map as $kolab => $api) { if (!array_key_exists($api, $data)) { continue; } $value = $data[$api]; switch ($kolab) { case 'sensitivity': if ($value) { $value = strtolower($value); } break; case 'parent_id': // kolab_format_task supports only one parent if (is_array($value)) { $value = $value[0]; } break; case 'created': case 'changed': case 'start': case 'due': $value = kolab_api_input_json::to_datetime($value); break; case 'attendees': $value = kolab_api_input_json::parse_attendees($value); break; case 'organizer': if (!empty($value)) { $value = kolab_api_input_json::parse_attendees(array($value)); $value = $value[0]; } break; } $result[$kolab] = $value; } // @TOOD: categories - // @TODO: recurrence // @TOOD: alarms + kolab_api_input_json::parse_recurrence($data, $result); + // x-custom fields kolab_api_input_json::add_x_custom($data, $result); if (empty($result)) { throw new kolab_api_exception(kolab_api_exception::INVALID_REQUEST); } if (!empty($original)) { $result = array_merge($original, $result); } $data = $result; } } diff --git a/lib/output/json.php b/lib/output/json.php index f4f0d95..f4c2544 100644 --- a/lib/output/json.php +++ b/lib/output/json.php @@ -1,271 +1,271 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_output_json extends kolab_api_output { /** * Send successful response * * @param mixed Response data * @param string Data type * @param array Context (folder_uid, object_uid, object) * @param array Optional attributes filter */ public function send($data, $type, $context = null, $attrs_filter = array()) { // Set output type $this->headers(array('Content-Type' => "application/json; charset=utf-8")); list($type, $mode) = explode('-', $type); if ($mode != 'list') { $data = array($data); } $class = "kolab_api_output_json_$type"; $model = new $class($this); $result = array(); $debug = $this->api->config->get('kolab_api_debug'); foreach ($data as $idx => $item) { if ($element = $model->element($item, $attrs_filter)) { $result[] = $element; } else { unset($data[$idx]); } } // apply output filter if ($this->api->filter) { $this->api->filter->output($result, $type, $context, $attrs_filter); } // generate JSON output $opts = $debug && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0; $result = json_encode($result, $opts); if ($mode != 'list') { $result = trim($result, '[]'); } if ($debug) { rcube::console($result); } $this->send_status(kolab_api_output::STATUS_OK, false); // send JSON output echo $result; exit; } /** * Convert object data into JSON API format * * @param array Object data * @param string Object type * * @return array Object data in JSON API format */ public function convert($data, $type) { $class = "kolab_api_output_json_$type"; $model = new $class($this); return $model->element($data); } /** * Convert (part of) kolab_format object into an array * * @param array Kolab object * @param string Object type * @param string Data element name * @param array Optional list of return properties * * @return array Object data */ public function object_to_array($object, $type, $element, $properties = array(), $array_elements = array()) { // load old object to preserve data we don't understand/process if (is_object($object['_formatobj'])) { $format = $object['_formatobj']; } // create new kolab_format instance if (!$format) { $format = kolab_format::factory($type, kolab_storage::$version); if (PEAR::isError($format)) { return; } $format->set($object); } $xml = $format->write(kolab_storage::$version); if (empty($xml) || !$format->is_valid() || !$format->uid) { return; } // The simplest way of "normalizing object properties // is to use its XML representation $doc = new DOMDocument(); // LIBXML_NOBLANKS is required for xml_to_array() below $doc->loadXML($xml, LIBXML_NOBLANKS); $node = $doc->getElementsByTagName($element)->item(0); $node = $this->xml_to_array($node); $node = array_filter($node); unset($node['prodid']); // faked 'categories' property (we need this for unit-tests // @TODO: find a better way if (!empty($object['categories'])) { $node['categories'] = $object['categories']; } if (!empty($properties)) { $node = array_intersect_key($node, array_combine($properties, $properties)); } // force some elements to be arrays if (!empty($array_elements)) { self::parse_array_result($node, $array_elements); } return $node; } /** * Convert XML element into an array * This is intended to use with Kolab XML format * * @param DOMElement XML element * * @return mixed Conversion result */ public function xml_to_array($node) { $children = $node->childNodes; if (!$children->length) { return; } if ($children->length == 1) { if ($node->firstChild->nodeType == XML_TEXT_NODE || !$node->firstChild->childNodes->length ) { return (string) $node->textContent; } if ($node->firstChild->nodeType == XML_ELEMENT_NODE && $node->firstChild->childNodes->length == 1 && $node->firstChild->firstChild->nodeType == XML_TEXT_NODE ) { switch ($node->firstChild->nodeName) { case 'integer': return (int) $node->textContent; case 'boolean': return strtoupper($node->textContent) == 'TRUE'; case 'date-time': case 'timestamp': case 'date': case 'text': case 'uri': case 'sex': return (string) $node->textContent; } } } $result = array(); foreach ($children as $child) { $value = $child->nodeType == XML_TEXT_NODE ? $child->nodeValue : $this->xml_to_array($child); if (!isset($result[$child->nodeName])) { $result[$child->nodeName] = $value; } else { if (!is_array($result[$child->nodeName]) || !isset($result[$child->nodeName][0])) { $result[$child->nodeName] = array($result[$child->nodeName]); } $result[$child->nodeName][] = $value; } } if (is_array($result['text']) && count($result) == 1) { $result = $result['text']; } return $result; } public static function parse_array_result(&$data, $array_elements = array()) { foreach ($array_elements as $key) { $items = explode('/', $key); if (count($items) > 1 && !empty($data[$items[0]])) { $key = array_shift($items); self::parse_array_result($data[$key], array(implode('/', $items))); } else if (!empty($data[$key]) && (!is_array($data[$key]) || !array_key_exists(0, $data[$key]))) { $data[$key] = array($data[$key]); } } } /** * Makes sure exdate/rdate output is consistent/unified */ - public static function parse_recur_dates(&$data) + public static function parse_recurrence(&$data) { foreach (array('exdate', 'rdate') as $key) { if ($data[$key]) { if (is_string($data[$key])) { $idx = strlen($data[$key]) > 10 ? 'date-time' : 'date'; $data[$key] = array($idx => array($data[$key])); } else if (array_key_exists('date', $data[$key]) && !is_array($data[$key]['date'])) { $data[$key]['date'] = (array) $data[$key]['date']; } else if (array_key_exists('date-time', $data[$key]) && !is_array($data[$key]['date-time'])) { $data[$key]['date-time'] = (array) $data[$key]['date-time']; } } } } } diff --git a/lib/output/json/event.php b/lib/output/json/event.php index 45770fc..c73fad9 100644 --- a/lib/output/json/event.php +++ b/lib/output/json/event.php @@ -1,84 +1,84 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_output_json_event { protected $output; protected $array_elements = array( 'attach', 'attendee', 'categories', 'x-custom', 'valarm', ); /** * Object constructor * * @param kolab_api_output Output object */ public function __construct($output) { $this->output = $output; } /** * Convert data into an array * * @param array Data * @param array Optional attributes filter * * @return array Data */ public function element($data, $attrs_filter = array()) { // partial data if (is_array($data) && count($data) == 1) { return $data; } $result = $this->output->object_to_array($data, 'event', 'vevent'); if (!empty($attrs_filter)) { $result['properties'] = array_intersect_key($result['properties'], array_combine($attrs_filter, $attrs_filter)); } // add 'components' to the result if (!empty($result['components'])) { $result['properties'] += (array) $result['components']; } $result = $result['properties']; kolab_api_output_json::parse_array_result($result, $this->array_elements); // make sure exdate/rdate format is unified - kolab_api_output_json::parse_recur_dates($result); + kolab_api_output_json::parse_recurrence($result); return $result; } } diff --git a/lib/output/json/task.php b/lib/output/json/task.php index ff95fcc..1460114 100644 --- a/lib/output/json/task.php +++ b/lib/output/json/task.php @@ -1,85 +1,85 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class kolab_api_output_json_task { protected $output; protected $array_elements = array( 'attach', 'attendee', 'related-to', 'x-custom', 'categories', 'valarm', ); /** * Object constructor * * @param kolab_api_output Output object */ public function __construct($output) { $this->output = $output; } /** * Convert data into an array * * @param array Data * @param array Optional attributes filter * * @return array Data */ public function element($data, $attrs_filter = array()) { // partial data if (is_array($data) && count($data) == 1) { $attrs_filter = array(key($data)); } $result = $this->output->object_to_array($data, 'task', 'vtodo'); if (!empty($attrs_filter)) { $result['properties'] = array_intersect_key($result['properties'], array_combine($attrs_filter, $attrs_filter)); } // add 'components' to the result if (!empty($result['components'])) { $result['properties'] += (array) $result['components']; } $result = $result['properties']; kolab_api_output_json::parse_array_result($result, $this->array_elements); // make sure exdate/rdate format is unified - kolab_api_output_json::parse_recur_dates($result); + kolab_api_output_json::parse_recurrence($result); return $result; } } diff --git a/tests/Mapistore/Events.php b/tests/Mapistore/Events.php index 59e8eac..eb23f57 100644 --- a/tests/Mapistore/Events.php +++ b/tests/Mapistore/Events.php @@ -1,251 +1,251 @@ get('folders/' . kolab_api_tests::folder_uid('Calendar') . '/messages'); $code = self::$api->response_code(); $body = self::$api->response_body(); $body = json_decode($body, true); $this->assertEquals(200, $code); $this->assertSame(kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100'), $body[0]['id']); $this->assertSame('Summary', $body[0]['PidTagSubject']); $this->assertSame('Description', $body[0]['PidTagBody']); $this->assertSame('calendars', $body[0]['collection']); $this->assertSame('IPM.Appointment', $body[0]['PidTagMessageClass']); $this->assertSame(kolab_api_tests::mapi_uid('Calendar', true, '101-101-101-101'), $body[1]['id']); $this->assertSame(0, $body[1]['PidTagSensitivity']); $this->assertSame('calendars', $body[1]['collection']); $this->assertSame('IPM.Appointment', $body[1]['PidTagMessageClass']); } /** * Test event existence */ function test_event_exists() { self::$api->head('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100')); $code = self::$api->response_code(); $body = self::$api->response_body(); $this->assertEquals(200, $code); $this->assertSame('', $body); // and non-existing event self::$api->head('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '12345')); $code = self::$api->response_code(); $body = self::$api->response_body(); $this->assertEquals(404, $code); $this->assertSame('', $body); } /** * Test event info */ function test_event_info() { self::$api->get('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100')); $code = self::$api->response_code(); $body = self::$api->response_body(); $body = json_decode($body, true); $this->assertEquals(200, $code); $this->assertSame(kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100'), $body['id']); $this->assertSame('Summary', $body['PidTagSubject']); $this->assertSame('calendars', $body['collection']); $this->assertSame('IPM.Appointment', $body['PidTagMessageClass']); } /** * Test event create */ function test_event_create() { $post = json_encode(array( 'parent_id' => kolab_api_tests::folder_uid('Calendar'), 'PidTagSubject' => 'Test summary', - 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore::date_php2mapi('2015-01-01'), + 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore_common::date_php2mapi('2015-01-01'), )); self::$api->post('calendars', array(), $post); $code = self::$api->response_code(); $body = self::$api->response_body(); $body = json_decode($body, true); $this->assertEquals(200, $code); $this->assertCount(1, $body); $this->assertTrue(!empty($body['id'])); // folder does not exists $post = json_encode(array( 'parent_id' => md5('non-existing'), 'PidTagSubject' => 'Test summary', - 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore::date_php2mapi('2015-01-01'), + 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore_common::date_php2mapi('2015-01-01'), )); self::$api->post('calendars', array(), $post); $code = self::$api->response_code(); $this->assertEquals(404, $code); // invalid object data $post = json_encode(array( 'parent_id' => kolab_api_tests::folder_uid('Calendar'), 'test' => 'Test summary 2', )); self::$api->post('calendars', array(), $post); $code = self::$api->response_code(); $this->assertEquals(422, $code); } /** * Test event update */ function test_event_update() { // @TODO: test modification of all supported properties $post = json_encode(array( 'PidTagSubject' => 'Modified subject (1)', - 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore::date_php2mapi('2015-01-01'), + 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore_common::date_php2mapi('2015-01-01'), )); self::$api->put('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100'), array(), $post); $code = self::$api->response_code(); $body = self::$api->response_body(); $this->assertEquals(200, $code); self::$api->get('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100')); $body = self::$api->response_body(); $body = json_decode($body, true); $this->assertSame('Modified subject (1)', $body['PidTagSubject']); } /** * Test counting event attachments */ function test_count_attachments() { self::$api->head('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100') . '/attachments'); $code = self::$api->response_code(); $body = self::$api->response_body(); $count = self::$api->response_header('X-mapistore-rowcount'); $this->assertEquals(200, $code); $this->assertSame('', $body); $this->assertSame(1, (int) $count); self::$api->head('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '101-101-101-101') . '/attachments'); $code = self::$api->response_code(); $body = self::$api->response_body(); $count = self::$api->response_header('X-mapistore-rowcount'); $this->assertEquals(200, $code); $this->assertSame('', $body); $this->assertSame(0, (int) $count); } /** * Test listing event attachments */ function test_list_attachments() { self::$api->get('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100') . '/attachments'); $code = self::$api->response_code(); $body = self::$api->response_body(); $body = json_decode($body, true); $this->assertEquals(200, $code); $this->assertCount(1, $body); $this->assertSame(kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100', '3'), $body[0]['id']); $this->assertSame('image/jpeg', $body[0]['PidTagAttachMimeTag']); $this->assertSame('photo-mini.jpg', $body[0]['PidTagDisplayName']); $this->assertSame(793, $body[0]['PidTagAttachSize']); } /** * Test event delete */ function test_event_delete() { // delete existing event self::$api->delete('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '101-101-101-101')); $code = self::$api->response_code(); $body = self::$api->response_body(); $this->assertEquals(204, $code); $this->assertSame('', $body); // and non-existing event self::$api->delete('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '12345')); $code = self::$api->response_code(); $body = self::$api->response_body(); $this->assertEquals(404, $code); $this->assertSame('', $body); } /** * Test event update with moving to another folder */ function test_event_update_and_move() { // test event moving to another folder (by parent_id change) $post = json_encode(array( 'PidTagSubject' => 'Modified subject (2)', 'parent_id' => kolab_api_tests::folder_uid('Calendar/Personal Calendar'), )); self::$api->put('calendars/' . kolab_api_tests::mapi_uid('Calendar', true, '100-100-100-100'), array(), $post); $code = self::$api->response_code(); $body = self::$api->response_body(); $this->assertEquals(200, $code); self::$api->get('calendars/' . kolab_api_tests::mapi_uid('Calendar/Personal Calendar', true, '100-100-100-100')); $body = self::$api->response_body(); $body = json_decode($body, true); $this->assertSame('Modified subject (2)', $body['PidTagSubject']); } } diff --git a/tests/Unit/Filter/Mapistore.php b/tests/Unit/Filter/Mapistore.php index 730f93a..5b167bc 100644 --- a/tests/Unit/Filter/Mapistore.php +++ b/tests/Unit/Filter/Mapistore.php @@ -1,82 +1,34 @@ assertSame('folder.msg', $uid); $uid = kolab_api_filter_mapistore::uid_encode('folder', 'msg', 'attach'); $this->assertSame('folder.msg.attach', $uid); $uid = kolab_api_filter_mapistore::uid_encode('f-ol.der', 'm-s.g', 'att.a-ch'); $this->assertSame('f-ol_46der.m-s_46g.att_46a-ch', $uid); } /** * Test uid_decode method */ function test_uid_decode() { $uid = kolab_api_filter_mapistore::uid_decode('f-ol_46der.m-s_46g.att_46a-ch'); $this->assertSame(array('f-ol.der', 'm-s.g', 'att.a-ch'), $uid); } - - /** - * Test date_php2mapi method - */ - function test_date_php2mapi() - { - $date = kolab_api_filter_mapistore::date_php2mapi('2014-01-01T00:00:00+00:00'); - $this->assertSame(13033008000.0, $date); - - $date = kolab_api_filter_mapistore::date_php2mapi('2014-01-01'); - $this->assertSame(13033008000.0, $date); - - $date = kolab_api_filter_mapistore::date_php2mapi('1970-01-01T00:00:00Z'); - $this->assertSame(11644473600.0, $date); - - $date = kolab_api_filter_mapistore::date_php2mapi('1601-01-01T00:00:00Z'); - $this->assertSame(0.0, $date); - - $date = new DateTime('1601-01-01T00:00:00Z'); - $date = kolab_api_filter_mapistore::date_php2mapi($date); - $this->assertSame(0.0, $date); -/* - $date = new DateTime('1970-01-01 00:00:00.1000 +0000'); - $date = kolab_api_filter_mapistore::date_php2mapi($date); - $this->assertSame(11644473600.1, $date); -*/ - $date = kolab_api_filter_mapistore::date_php2mapi(''); - $this->assertSame(null, $date); - } - - /** - * Test date_mapi2php method - */ - function test_date_mapi2php() - { - $format = 'c'; - $data = array( - 13033008000 => '2014-01-01T00:00:00+00:00', - 11644473600 => '1970-01-01T00:00:00+00:00', -// 11644473600.00001 => '1970-01-01T00:00:00.10+00:00', - 0 => '1601-01-01T00:00:00+00:00', - ); - - foreach ($data as $mapi => $exp) { - $date = kolab_api_filter_mapistore::date_mapi2php($mapi); - $this->assertSame($exp, $date->format($format)); - } - } } diff --git a/tests/Unit/Filter/Mapistore/Common.php b/tests/Unit/Filter/Mapistore/Common.php index a448231..e0bf086 100644 --- a/tests/Unit/Filter/Mapistore/Common.php +++ b/tests/Unit/Filter/Mapistore/Common.php @@ -1,239 +1,318 @@ array( 'n2' => 'test2', ), 'n3' => 'test3', 'x-custom' => array( array('identifier' => 'i', value => 'val_i'), ), ); $value = kolab_api_filter_mapistore_common::get_kolab_value($data, 'n1.n2'); $this->assertSame('test2', $value); $value = kolab_api_filter_mapistore_common::get_kolab_value($data, 'n3'); $this->assertSame('test3', $value); $value = kolab_api_filter_mapistore_common::get_kolab_value($data, 'n30'); $this->assertSame(null, $value); $value = kolab_api_filter_mapistore_common::get_kolab_value($data, 'x-custom.i'); $this->assertSame('val_i', $value); } /** * Test set_kolab_value method */ function test_set_kolab_value() { $data = array(); kolab_api_filter_mapistore_common::set_kolab_value($data, 'n1.n2', 'test'); $this->assertSame('test', $data['n1']['n2']); kolab_api_filter_mapistore_common::set_kolab_value($data, 'n1', 'test'); $this->assertSame('test', $data['n1']); kolab_api_filter_mapistore_common::set_kolab_value($data, 'x-custom.i', 'test1'); $this->assertSame('test1', $data['x-custom.i']); } /** * Test attributes_filter method */ function test_attributes_filter() { $api = new kolab_api_filter_mapistore_common; $input = array( 'creation-date', 'uid', 'unknown', ); $expected = array( 'PidTagCreationTime', 'id', ); $result = $api->attributes_filter($input, true); $this->assertSame($expected, $result); $input = $expected; $expected = array( 'creation-date', 'uid', ); $result = $api->attributes_filter($input); $this->assertSame($expected, $result); $result = $api->attributes_filter(array()); $this->assertSame(array(), $result); } /** * Test parse_categories method */ function test_parse_categories() { $categories = array( "test\x3Btest", "test\x2Ctest", "a\x06\x1Ba", "b\xFE\x54b", "c\xFF\x1Bc", "test ", " test", ); $expected = array( "testtest", "aa", "bb", "cc", "test", ); $result = kolab_api_filter_mapistore_common::parse_categories($categories); $this->assertSame($expected, $result); } /** * Test recurrence_to_kolab */ function test_recurrence_to_kolab() { // empty result kolab_api_filter_mapistore_event::recurrence_to_kolab('', $result = array()); $this->assertSame(array(), $result); // build complete AppointmentRecurrencePattern structure $structure = new kolab_api_filter_mapistore_structure_appointmentrecurrencepattern; $exceptioninfo = new kolab_api_filter_mapistore_structure_exceptioninfo; $recurrencepattern = new kolab_api_filter_mapistore_structure_recurrencepattern; $extendedexception = new kolab_api_filter_mapistore_structure_extendedexception; $highlight = new kolab_api_filter_mapistore_structure_changehighlight; $highlight->ChangeHighlightValue = 4; $extendedexception->ChangeHighlight = $highlight; $extendedexception->StartDateTime = 0x0CBC9934; $extendedexception->EndDateTime = 0x0CBC9952; $extendedexception->OriginalStartDate = 0x0CBC98F8; $extendedexception->WideCharSubject = 'Simple Recurrence with exceptions'; $extendedexception->WideCharLocation = '34/4141'; $recurrencepattern->RecurFrequency = 0x200b; $recurrencepattern->PatternType = 1; $recurrencepattern->CalendarType = 0; $recurrencepattern->FirstDateTime = 0x000021C0; $recurrencepattern->Period = 1; $recurrencepattern->SlidingFlag = 0; $recurrencepattern->PatternTypeSpecific = 0x00000032; $recurrencepattern->EndType = 0x00002022; $recurrencepattern->OccurrenceCount = 12; $recurrencepattern->FirstDOW = 0; - $recurrencepattern->DeletedInstanceDates = array(217742400, 218268000); - $recurrencepattern->ModifiedInstanceDates = array(0x0CBC96A0); + $recurrencepattern->DeletedInstanceDates = array(217742400, 218268000, 217787040); + $recurrencepattern->ModifiedInstanceDates = array(217787040); $recurrencepattern->StartDate = 213655680; $recurrencepattern->EndDate = 0x0CBCAD20; $exceptioninfo->StartDateTime = 0x0CBC9934; $exceptioninfo->EndDateTime = 0x0CBC9952; $exceptioninfo->OriginalStartDate = 0x0CBC98F8; $exceptioninfo->Subject = 'Simple Recurrence with exceptions'; $exceptioninfo->Location = '34/4141'; $structure->StartTimeOffset = 600; $structure->EndTimeOffset = 630; $structure->ExceptionInfo = array($exceptioninfo); $structure->RecurrencePattern = $recurrencepattern; $structure->ExtendedException = array($extendedexception); $rule = $structure->output(true); kolab_api_filter_mapistore_event::recurrence_to_kolab($rule, $result); $this->assertSame('WEEKLY', $result['rrule']['recur']['freq']); $this->assertSame('SU', $result['rrule']['recur']['wkst']); $this->assertSame('SU,TU,MO,TH,FR', $result['rrule']['recur']['byday']); $this->assertSame(12, $result['rrule']['recur']['count']); $this->assertSame('2015-01-01', $result['exdate']['date'][0]); $this->assertSame('2016-01-01', $result['exdate']['date'][1]); + $this->assertSame('2015-02-01', $result['rdate']['date'][0]); } /** * Test recurrence_from_kolab */ function test_recurrence_from_kolab() { $data = array( 'dtstart' => '2015-01-01T00:00:00Z', 'rrule' => array( 'recur' => array( 'freq' => 'MONTHLY', 'bymonthday' => 5, 'count' => 10, 'interval' => 2, ), ), 'exdate' => array( 'date' => array( '2015-01-01', '2016-01-01', ), ), + 'rdate' => array( + 'date' => array( + '2015-02-01', + ), + ), ); $result = kolab_api_filter_mapistore_event::recurrence_from_kolab($data, $event = array()); $arp = new kolab_api_filter_mapistore_structure_appointmentrecurrencepattern; $arp->input($result, true); $this->assertSame(kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_MONTH, $arp->RecurrencePattern->PatternType); $this->assertSame(kolab_api_filter_mapistore_structure_recurrencepattern::RECURFREQUENCY_MONTHLY, $arp->RecurrencePattern->RecurFrequency); + + // @TODO: test mode recurrence exception details $this->assertSame(5, $arp->RecurrencePattern->PatternTypeSpecific); $this->assertSame(10, $arp->RecurrencePattern->OccurrenceCount); $this->assertSame(2, $arp->RecurrencePattern->Period); - $this->assertSame(2, $arp->RecurrencePattern->DeletedInstanceCount); - $this->assertCount(2, $arp->RecurrencePattern->DeletedInstanceDates); + $this->assertSame(3, $arp->RecurrencePattern->DeletedInstanceCount); + $this->assertCount(3, $arp->RecurrencePattern->DeletedInstanceDates); + $this->assertSame(1, $arp->RecurrencePattern->ModifiedInstanceCount); + $this->assertCount(1, $arp->RecurrencePattern->ModifiedInstanceDates); + $this->assertSame(1, $arp->ExceptionCount); + $this->assertCount(1, $arp->ExceptionInfo); + $this->assertCount(1, $arp->ExtendedException); // test $type=task $data = array( 'dtstart' => '2015-01-01T00:00:00Z', 'rrule' => array( 'recur' => array( 'freq' => 'YEARLY', 'bymonth' => 5, 'bymonthday' => 1, 'count' => 10, ), ), ); $result = kolab_api_filter_mapistore_event::recurrence_from_kolab($data, $task = array(), 'task'); $rp = new kolab_api_filter_mapistore_structure_recurrencepattern; $rp->input($result, true); $this->assertSame(kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_MONTHNTH, $rp->PatternType); $this->assertSame(kolab_api_filter_mapistore_structure_recurrencepattern::RECURFREQUENCY_YEARLY, $rp->RecurFrequency); $this->assertSame(1, $rp->PatternTypeSpecific[1]); $this->assertSame(10, $rp->OccurrenceCount); $this->assertSame(12, $rp->Period); // @TODO: test other $rp properties } + + /** + * Test date_php2mapi method + */ + function test_date_php2mapi() + { + $date = kolab_api_filter_mapistore_common::date_php2mapi('2014-01-01T00:00:00+00:00'); + $this->assertSame(13033008000.0, $date); + + $date = kolab_api_filter_mapistore_common::date_php2mapi('2014-01-01'); + $this->assertSame(13033008000.0, $date); + + $date = kolab_api_filter_mapistore_common::date_php2mapi('1970-01-01T00:00:00Z'); + $this->assertSame(11644473600.0, $date); + + $date = kolab_api_filter_mapistore_common::date_php2mapi('1601-01-01T00:00:00Z'); + $this->assertSame(0.0, $date); + + $date = new DateTime('1601-01-01T00:00:00Z'); + $date = kolab_api_filter_mapistore_common::date_php2mapi($date); + $this->assertSame(0.0, $date); +/* + $date = new DateTime('1970-01-01 00:00:00.1000 +0000'); + $date = kolab_api_filter_mapistore::date_php2mapi($date); + $this->assertSame(11644473600.1, $date); +*/ + $date = kolab_api_filter_mapistore_common::date_php2mapi(''); + $this->assertSame(null, $date); + } + + /** + * Test date_mapi2php method + */ + function test_date_mapi2php() + { + $format = 'c'; + $data = array( + 13033008000 => '2014-01-01T00:00:00+00:00', + 11644473600 => '1970-01-01T00:00:00+00:00', +// 11644473600.00001 => '1970-01-01T00:00:00.10+00:00', + 0 => '1601-01-01T00:00:00+00:00', + ); + + foreach ($data as $mapi => $exp) { + $date = kolab_api_filter_mapistore_common::date_mapi2php($mapi); + $this->assertSame($exp, $date->format($format)); + } + } + + /** + * Test input date_minutes2php + */ + function test_date_minutes2php() + { + // @TODO + $this->markTestIncomplete('TODO'); + } + + /** + * Test input date_php2minutes + */ + function test_date_php2minutes() + { + // @TODO + $this->markTestIncomplete('TODO'); + } } diff --git a/tests/Unit/Filter/Mapistore/Contact.php b/tests/Unit/Filter/Mapistore/Contact.php index 09d509c..afb68bb 100644 --- a/tests/Unit/Filter/Mapistore/Contact.php +++ b/tests/Unit/Filter/Mapistore/Contact.php @@ -1,407 +1,407 @@ output($data, $context); $this->assertSame(kolab_api_tests::mapi_uid('Contacts', false, 'a-b-c-d'), $result['id']); $this->assertSame(kolab_api_tests::folder_uid('Contacts', false), $result['parent_id']); -// $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('20150421T145607Z'), $result['PidTagLastModificationTime']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('20150330', false), $result['PidTagBirthday']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('20150301', false), $result['PidTagWeddingAnniversary']); +// $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('20150421T145607Z'), $result['PidTagLastModificationTime']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('20150330', false), $result['PidTagBirthday']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('20150301', false), $result['PidTagWeddingAnniversary']); $this->assertSame('displname', $result['PidTagDisplayName']); $this->assertSame('last', $result['PidTagSurname']); $this->assertSame('test', $result['PidTagGivenName']); $this->assertSame('middlename', $result['PidTagMiddleName']); $this->assertSame('prefx', $result['PidTagDisplayNamePrefix']); $this->assertSame('suff', $result['PidTagGeneration']); $this->assertSame('dsfsdfsdfsdf sdfsdfsdf sdfsdfsfd', $result['PidTagBody']); $this->assertSame('free-busy url', $result['PidLidFreeBusyLocation']); $this->assertSame('title', $result['PidTagTitle']); $this->assertSame('Org', $result['PidTagCompanyName']); $this->assertSame('dept', $result['PidTagDepartmentName']); $this->assertSame('profeion', $result['PidTagProfession']); $this->assertSame('manager name', $result['PidTagManagerName']); $this->assertSame('assist', $result['PidTagAssistant']); $this->assertSame('website', $result['PidTagPersonalHomePage']); $this->assertSame('office street', $result['PidTagOtherAddressStreet']); $this->assertSame('office city', $result['PidTagOtherAddressCity']); $this->assertSame('office state', $result['PidTagOtherAddressStateOrProvince']); $this->assertSame('office zip', $result['PidTagOtherAddressPostalCode']); $this->assertSame('office country', $result['PidTagOtherAddressCountry']); // $this->assertSame('office pobox', $result['PidTagOtherAddressPostOfficeBox']); $this->assertSame('home street', $result['PidTagHomeAddressStreet']); $this->assertSame('home city', $result['PidTagHomeAddressCity']); $this->assertSame('home state', $result['PidTagHomeAddressStateOrProvince']); $this->assertSame('home zip', $result['PidTagHomeAddressPostalCode']); $this->assertSame('home country', $result['PidTagHomeAddressCountry']); // $this->assertSame('home pobox', $result['PidTagHomeAddressPostOfficeBox']); $this->assertSame('work street', $result['PidLidWorkAddressStreet']); $this->assertSame('work city', $result['PidLidWorkAddressCity']); $this->assertSame('work state', $result['PidLidWorkAddressState']); $this->assertSame('work zip', $result['PidLidWorkAddressPostalCode']); $this->assertSame('work country', $result['PidLidWorkAddressCountry']); // $this->assertSame('work pobox', $result['PidLidWorkAddressPostOfficeBox']); $this->assertSame('nick', $result['PidTagNickname']); $this->assertSame(2, $result['PidTagGender']); $this->assertSame('spouse', $result['PidTagSpouseName']); $this->assertSame(array('children', 'children2'), $result['PidTagChildrensNames']); $this->assertSame('home phone', $result['PidTagHomeTelephoneNumber']); $this->assertSame('work phone', $result['PidTagBusinessTelephoneNumber']); $this->assertSame('home fax', $result['PidTagHomeFaxNumber']); $this->assertSame('work fax', $result['PidTagBusinessFaxNumber']); $this->assertSame('mobile', $result['PidTagMobileTelephoneNumber']); $this->assertSame('pager', $result['PidTagPagerTelephoneNumber']); $this->assertSame('car phone', $result['PidTagCarTelephoneNumber']); $this->assertSame('other phone', $result['PidTagOtherTelephoneNumber']); $this->assertSame('im gg', $result['PidLidInstantMessagingAddress']); $this->assertSame('test@mail.ru', $result['PidLidEmail1EmailAddress']); $this->assertSame('work@email.pl', $result['PidLidEmail2EmailAddress']); $this->assertSame('other@email.pl', $result['PidLidEmail3EmailAddress']); $this->assertRegExp('/^cy9.*/', $result['PidTagUserX509Certificate']); // $this->assertRegExp('|^data:application/pgp-keys;base64,|', $result['key'][0]); // $this->assertRegExp('|^data:image/jpeg;base64,|', $result['photo']); // $this->assertSame('individual', $result['kind']); } /** * Test input method */ function test_input() { $api = new kolab_api_filter_mapistore_contact; $data = array( 'id' => kolab_api_tests::mapi_uid('Contacts', false, 'a-b-c-d'), 'parent_id' => kolab_api_tests::folder_uid('Contacts', false), -// 'PidTagLastModificationTime' => kolab_api_filter_mapistore::date_php2mapi('20150421T145607Z'), - 'PidTagBirthday' => kolab_api_filter_mapistore::date_php2mapi('20150330', true), - 'PidTagWeddingAnniversary' => kolab_api_filter_mapistore::date_php2mapi('20150301', true), +// 'PidTagLastModificationTime' => kolab_api_filter_mapistore_common::date_php2mapi('20150421T145607Z'), + 'PidTagBirthday' => kolab_api_filter_mapistore_common::date_php2mapi('20150330', true), + 'PidTagWeddingAnniversary' => kolab_api_filter_mapistore_common::date_php2mapi('20150301', true), 'PidTagDisplayName' => 'displname', 'PidTagSurname' => 'last', 'PidTagGivenName' => 'test', 'PidTagMiddleName' => 'middlename', 'PidTagDisplayNamePrefix' => 'prefx', 'PidTagGeneration' => 'suff', 'PidTagBody' => 'dsfsdfsdfsdf sdfsdfsdf sdfsdfsfd', 'PidLidFreeBusyLocation' => 'free-busy url', 'PidTagTitle' => 'title', 'PidTagCompanyName' => 'Org', 'PidTagDepartmentName' => 'dept', 'PidTagProfession' => 'profeion', 'PidTagManagerName' => 'manager name', 'PidTagAssistant' => 'assist', 'PidTagPersonalHomePage' => 'website', 'PidTagOtherAddressStreet' => 'office street', 'PidTagOtherAddressCity' => 'office city', 'PidTagOtherAddressStateOrProvince' => 'office state', 'PidTagOtherAddressPostalCode' => 'office zip', 'PidTagOtherAddressCountry' => 'office country', // 'PidTagOtherAddressPostOfficeBox' => 'office pobox', 'PidTagHomeAddressStreet' => 'home street', 'PidTagHomeAddressCity' => 'home city', 'PidTagHomeAddressStateOrProvince' => 'home state', 'PidTagHomeAddressPostalCode' => 'home zip', 'PidTagHomeAddressCountry' => 'home country', // 'PidTagHomeAddressPostOfficeBox' => 'home pobox', 'PidLidWorkAddressStreet' => 'work street', 'PidLidWorkAddressCity' => 'work city', 'PidLidWorkAddressState' => 'work state', 'PidLidWorkAddressPostalCode' => 'work zip', 'PidLidWorkAddressCountry' => 'work country', // 'PidLidWorkAddressPostOfficeBox' => 'work pobox', 'PidTagNickname' => 'nick', 'PidTagGender' => 2, 'PidTagSpouseName' => 'spouse', 'PidTagChildrensNames' => array('children', 'children2'), 'PidTagHomeTelephoneNumber' => 'home phone', 'PidTagBusinessTelephoneNumber' => 'work phone', 'PidTagHomeFaxNumber' => 'home fax', 'PidTagBusinessFaxNumber' => 'work fax', 'PidTagMobileTelephoneNumber' => 'mobile', 'PidTagPagerTelephoneNumber' => 'pager', 'PidTagCarTelephoneNumber' => 'car phone', 'PidTagOtherTelephoneNumber' => 'other phone', 'PidLidInstantMessagingAddress' => 'im gg', 'PidLidEmail1EmailAddress' => 'test@mail.ru', 'PidLidEmail2EmailAddress' => 'work@email.pl', 'PidLidEmail3EmailAddress' => 'other@email.pl', 'PidTagUserX509Certificate' => '1234567890', 'PidTagInitials' => 'initials', ); $result = $api->input($data); // $this->assertSame('a-b-c-d', $result['uid']); // $this->assertSame('20150420T141533Z', $result['rev']); // $this->assertSame('individual', $result['kind']); $this->assertSame('displname', $result['fn']); $this->assertSame('last', $result['n']['surname']); $this->assertSame('test', $result['n']['given']); $this->assertSame('middlename', $result['n']['additional']); $this->assertSame('prefx', $result['n']['prefix']); $this->assertSame('suff', $result['n']['suffix']); $this->assertSame('dsfsdfsdfsdf sdfsdfsdf sdfsdfsfd', $result['note']); $this->assertSame('free-busy url', $result['fburl']); $this->assertSame('title', $result['title']); $this->assertSame('Org', $result['group']['org'][0]); $this->assertSame('dept', $result['group']['org'][1]); $this->assertSame('profeion', $result['group']['role']); $this->assertSame('x-manager', $result['group']['related'][0]['parameters']['type']); $this->assertSame('manager name', $result['group']['related'][0]['text']); $this->assertSame('x-assistant', $result['group']['related'][1]['parameters']['type']); $this->assertSame('assist', $result['group']['related'][1]['text']); // $this->assertSame('', $result['group']['adr']['pobox']); $this->assertSame('office street', $result['group']['adr']['street']); $this->assertSame('office city', $result['group']['adr']['locality']); $this->assertSame('office state', $result['group']['adr']['region']); $this->assertSame('office zip', $result['group']['adr']['code']); $this->assertSame('office country', $result['group']['adr']['country']); $this->assertSame(array('website'), $result['url']); $this->assertSame('home', $result['adr'][0]['parameters']['type']); $this->assertSame('home street', $result['adr'][0]['street']); $this->assertSame('home city', $result['adr'][0]['locality']); $this->assertSame('home state', $result['adr'][0]['region']); $this->assertSame('home zip', $result['adr'][0]['code']); $this->assertSame('home country', $result['adr'][0]['country']); $this->assertSame('work', $result['adr'][1]['parameters']['type']); $this->assertSame('work street', $result['adr'][1]['street']); $this->assertSame('work city', $result['adr'][1]['locality']); $this->assertSame('work state', $result['adr'][1]['region']); $this->assertSame('work zip', $result['adr'][1]['code']); $this->assertSame('work country', $result['adr'][1]['country']); $this->assertSame('nick', $result['nickname']); $this->assertSame('spouse', $result['related'][0]['parameters']['type']); $this->assertSame('spouse', $result['related'][0]['text']); $this->assertSame('child', $result['related'][1]['parameters']['type']); $this->assertSame('children', $result['related'][1]['text']); $this->assertSame('child', $result['related'][2]['parameters']['type']); $this->assertSame('children2', $result['related'][2]['text']); $this->assertSame('2015-03-30', $result['bday']); // ? $this->assertSame('2015-03-01', $result['anniversary']); // ? $this->assertSame('M', $result['gender']); $this->assertSame(array('im gg'), $result['impp']); $this->assertSame('home', $result['email'][0]['parameters']['type']); $this->assertSame('test@mail.ru', $result['email'][0]['text']); $this->assertSame('work', $result['email'][1]['parameters']['type']); $this->assertSame('work@email.pl', $result['email'][1]['text']); $this->assertSame('other', $result['email'][2]['parameters']['type']); $this->assertSame('other@email.pl', $result['email'][2]['text']); $this->assertRegExp('|^data:application/pkcs7-mime;base64,|', $result['key'][0]); // $this->assertRegExp('|^data:application/pgp-keys;base64,|', $result['key'][1]); // $this->assertRegExp('|^data:image/jpeg;base64,|', $result['photo']); $this->assertSame('MAPI:PidTagInitials', $result['x-custom'][0]['identifier']); $this->assertSame('initials', $result['x-custom'][0]['value']); $phones = array( 'home' => 'home phone', 'work' => 'work phone', 'faxhome' => 'home fax', 'faxwork' => 'work fax', 'cell' => 'mobile', 'pager' => 'pager', 'x-car' => 'car phone', 'textphone' => 'other phone', ); foreach ($result['tel'] as $tel) { $type = implode('', (array)$tel['parameters']['type']); $text = $tel['text']; if (!empty($phones[$type]) && $phones[$type] == $text) { unset($phones[$type]); } } $this->assertCount(8, $result['tel']); $this->assertCount(0, $phones); self::$original = $result; } /** * Test input method with merge */ function test_input2() { $api = new kolab_api_filter_mapistore_contact; $data = array( 'id' => kolab_api_tests::mapi_uid('Contacts', false, 'a-b-c-d'), 'parent_id' => kolab_api_tests::folder_uid('Contacts', false), -// 'PidTagLastModificationTime' => kolab_api_filter_mapistore::date_php2mapi('20150421T145607Z'), - 'PidTagBirthday' => kolab_api_filter_mapistore::date_php2mapi('20150430', true), - 'PidTagWeddingAnniversary' => kolab_api_filter_mapistore::date_php2mapi('20150401', true), +// 'PidTagLastModificationTime' => kolab_api_filter_mapistore_common::date_php2mapi('20150421T145607Z'), + 'PidTagBirthday' => kolab_api_filter_mapistore_common::date_php2mapi('20150430', true), + 'PidTagWeddingAnniversary' => kolab_api_filter_mapistore_common::date_php2mapi('20150401', true), 'PidTagDisplayName' => 'displname1', 'PidTagSurname' => 'last1', 'PidTagGivenName' => 'test1', 'PidTagMiddleName' => 'middlename1', 'PidTagDisplayNamePrefix' => 'prefx1', 'PidTagGeneration' => 'suff1', 'PidTagBody' => 'body1', 'PidLidFreeBusyLocation' => 'free-busy url1', 'PidTagTitle' => 'title1', 'PidTagCompanyName' => 'Org1', 'PidTagDepartmentName' => 'dept1', 'PidTagProfession' => 'profeion1', 'PidTagManagerName' => 'manager name1', 'PidTagAssistant' => 'assist1', 'PidTagPersonalHomePage' => 'website1', 'PidTagOtherAddressStreet' => 'office street1', 'PidTagOtherAddressCity' => 'office city1', 'PidTagOtherAddressStateOrProvince' => 'office state1', 'PidTagOtherAddressPostalCode' => 'office zip1', 'PidTagOtherAddressCountry' => 'office country1', 'PidTagHomeAddressStreet' => 'home street1', 'PidTagHomeAddressCity' => 'home city1', 'PidTagHomeAddressStateOrProvince' => 'home state1', 'PidTagHomeAddressPostalCode' => 'home zip1', 'PidTagHomeAddressCountry' => 'home country1', 'PidLidWorkAddressStreet' => 'work street1', 'PidLidWorkAddressCity' => 'work city1', 'PidLidWorkAddressState' => 'work state1', 'PidLidWorkAddressPostalCode' => 'work zip1', 'PidLidWorkAddressCountry' => 'work country1', 'PidTagNickname' => 'nick1', 'PidTagGender' => 1, 'PidTagSpouseName' => 'spouse1', 'PidTagChildrensNames' => array('children10', 'children20'), 'PidTagHomeTelephoneNumber' => 'home phone1', 'PidTagBusinessTelephoneNumber' => null, 'PidTagHomeFaxNumber' => 'home fax1', 'PidTagBusinessFaxNumber' => 'work fax1', 'PidTagMobileTelephoneNumber' => 'mobile1', 'PidTagPagerTelephoneNumber' => 'pager1', 'PidTagOtherTelephoneNumber' => 'other phone1', 'PidLidInstantMessagingAddress' => 'im gg1', 'PidLidEmail1EmailAddress' => 'test@mail.ru', 'PidLidEmail2EmailAddress' => 'work@email.pl', 'PidTagUserX509Certificate' => '12345678901', 'PidTagInitials' => 'initials1', 'PidNameKeywords' => array('work1'), ); $result = $api->input($data, self::$original); // $this->assertSame('a-b-c-d', $result['uid']); // $this->assertSame('20150420T141533Z', $result['rev']); // $this->assertSame('individual', $result['kind']); $this->assertSame('displname1', $result['fn']); $this->assertSame('last1', $result['n']['surname']); $this->assertSame('test1', $result['n']['given']); $this->assertSame('middlename1', $result['n']['additional']); $this->assertSame('prefx1', $result['n']['prefix']); $this->assertSame('suff1', $result['n']['suffix']); $this->assertSame('body1', $result['note']); $this->assertSame('free-busy url1', $result['fburl']); $this->assertSame('title1', $result['title']); $this->assertSame('Org1', $result['group']['org'][0]); $this->assertSame('dept1', $result['group']['org'][1]); $this->assertSame('profeion1', $result['group']['role']); $this->assertSame('x-manager', $result['group']['related'][0]['parameters']['type']); $this->assertSame('manager name1', $result['group']['related'][0]['text']); $this->assertSame('x-assistant', $result['group']['related'][1]['parameters']['type']); $this->assertSame('assist1', $result['group']['related'][1]['text']); $this->assertSame('office street1', $result['group']['adr']['street']); $this->assertSame('office city1', $result['group']['adr']['locality']); $this->assertSame('office state1', $result['group']['adr']['region']); $this->assertSame('office zip1', $result['group']['adr']['code']); $this->assertSame('office country1', $result['group']['adr']['country']); $this->assertSame(array('website1'), $result['url']); $this->assertSame('home', $result['adr'][0]['parameters']['type']); $this->assertSame('home street1', $result['adr'][0]['street']); $this->assertSame('home city1', $result['adr'][0]['locality']); $this->assertSame('home state1', $result['adr'][0]['region']); $this->assertSame('home zip1', $result['adr'][0]['code']); $this->assertSame('home country1', $result['adr'][0]['country']); $this->assertSame('work', $result['adr'][1]['parameters']['type']); $this->assertSame('work street1', $result['adr'][1]['street']); $this->assertSame('work city1', $result['adr'][1]['locality']); $this->assertSame('work state1', $result['adr'][1]['region']); $this->assertSame('work zip1', $result['adr'][1]['code']); $this->assertSame('work country1', $result['adr'][1]['country']); $this->assertSame('nick1', $result['nickname']); $this->assertSame('spouse', $result['related'][0]['parameters']['type']); $this->assertSame('spouse1', $result['related'][0]['text']); $this->assertSame('child', $result['related'][1]['parameters']['type']); $this->assertSame('children10', $result['related'][1]['text']); $this->assertSame('child', $result['related'][2]['parameters']['type']); $this->assertSame('children20', $result['related'][2]['text']); $this->assertSame('2015-04-30', $result['bday']); // ? $this->assertSame('2015-04-01', $result['anniversary']); // ? $this->assertSame('F', $result['gender']); $this->assertSame(array('im gg1'), $result['impp']); $this->assertSame('home', $result['email'][0]['parameters']['type']); $this->assertSame('test@mail.ru', $result['email'][0]['text']); $this->assertSame('work', $result['email'][1]['parameters']['type']); $this->assertSame('work@email.pl', $result['email'][1]['text']); $this->assertSame(null, $result['email'][2]); $this->assertRegExp('|^data:application/pkcs7-mime;base64,|', $result['key'][0]); // $this->assertRegExp('|^data:application/pgp-keys;base64,|', $result['key'][1]); // $this->assertRegExp('|^data:image/jpeg;base64,|', $result['photo']); $this->assertSame('MAPI:PidTagInitials', $result['x-custom'][0]['identifier']); $this->assertSame('initials1', $result['x-custom'][0]['value']); $this->assertSame(array('work1'), $result['categories']); $phones = array( 'home' => 'home phone1', 'faxhome' => 'home fax1', 'faxwork' => 'work fax1', 'cell' => 'mobile1', 'pager' => 'pager1', 'x-car' => 'car phone', 'textphone' => 'other phone1', ); foreach ($result['tel'] as $tel) { $type = implode('', (array)$tel['parameters']['type']); $text = $tel['text']; if (!empty($phones[$type]) && $phones[$type] == $text) { unset($phones[$type]); } } $this->assertCount(7, $result['tel']); $this->assertCount(0, $phones); // @TODO: updating some deep items (e.g. adr); } /** * Test map method */ function test_map() { $api = new kolab_api_filter_mapistore_contact; $map = $api->map(); $this->assertInternalType('array', $map); $this->assertTrue(!empty($map)); } } diff --git a/tests/Unit/Filter/Mapistore/Event.php b/tests/Unit/Filter/Mapistore/Event.php index e9bdee7..a67a5f0 100644 --- a/tests/Unit/Filter/Mapistore/Event.php +++ b/tests/Unit/Filter/Mapistore/Event.php @@ -1,206 +1,222 @@ output($data, $context); $this->assertSame(kolab_api_tests::mapi_uid('Calendar', false, '100-100-100-100'), $result['id']); $this->assertSame('calendars', $result['collection']); $this->assertSame('IPM.Appointment', $result['PidTagMessageClass']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-05-14T13:03:33Z'), $result['PidTagCreationTime']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-05-14T13:50:18Z'), $result['PidTagLastModificationTime']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T13:03:33Z'), $result['PidTagCreationTime']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T13:50:18Z'), $result['PidTagLastModificationTime']); $this->assertSame(2, $result['PidLidAppointmentSequence']); $this->assertSame(3, $result['PidTagSensitivity']); $this->assertSame('Work', $result['PidNameKeywords'][0]); /* $this->assertSame('/kolab.org/Europe/Berlin', $result['dtstart']['parameters']['tzid']); $this->assertSame('2015-05-15T10:00:00', $result['dtstart']['date-time']); $this->assertSame('/kolab.org/Europe/Berlin', $result['dtend']['parameters']['tzid']); $this->assertSame('2015-05-15T10:30:00', $result['dtend']['date-time']); $this->assertSame('https://some.url', $result['url']); */ $this->assertSame('Summary', $result['PidTagSubject']); $this->assertSame('Description', $result['PidTagBody']); $this->assertSame(2, $result['PidTagImportance']); $this->assertSame('Location', $result['PidLidLocation']); $this->assertSame('German, Mark', $result['recipients'][0]['PidTagDisplayName']); $this->assertSame('mark.german@example.org', $result['recipients'][0]['PidTagEmailAddress']); $this->assertSame(1, $result['recipients'][0]['PidTagRecipientType']); $this->assertSame(3, $result['recipients'][0]['PidTagRecipientFlags']); $this->assertSame('Manager, Jane', $result['recipients'][1]['PidTagDisplayName']); $this->assertSame(1, $result['recipients'][1]['PidTagRecipientType']); $this->assertSame('jane.manager@example.org', $result['recipients'][1]['PidTagEmailAddress']); $this->assertSame(0, $result['recipients'][1]['PidTagRecipientTrackStatus']); $this->assertSame(1, $result['recipients'][1]['PidTagRecipientFlags']); $this->assertSame(15, $result['PidLidReminderDelta']); $this->assertSame(true, $result['PidLidReminderSet']); $data = kolab_api_tests::get_data('101-101-101-101', 'Calendar', 'event', 'json', $context); $result = $api->output($data, $context); $this->assertSame(kolab_api_tests::mapi_uid('Calendar', false, '101-101-101-101'), $result['id']); $this->assertSame('calendars', $result['collection']); $this->assertSame('IPM.Appointment', $result['PidTagMessageClass']); $this->assertSame(0, $result['PidTagSensitivity']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-05-15T00:00:00Z'), $result['PidLidAppointmentStartWhole']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-05-15T00:00:00Z'), $result['PidLidAppointmentEndWhole']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-05-15T00:00:00Z'), $result['PidLidAppointmentStartWhole']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-05-15T00:00:00Z'), $result['PidLidAppointmentEndWhole']); $this->assertSame(1, $result['PidLidAppointmentSubType']); - // recurrence + // EXDATE $arp = new kolab_api_filter_mapistore_structure_appointmentrecurrencepattern; $arp->input($result['PidLidAppointmentRecur'], true); $this->assertSame(1, $arp->RecurrencePattern->Period); $this->assertSame(0x200B, $arp->RecurrencePattern->RecurFrequency); $this->assertSame(1, $arp->RecurrencePattern->PatternType); $this->assertSame(2, $arp->RecurrencePattern->DeletedInstanceCount); $this->assertCount(2, $arp->RecurrencePattern->DeletedInstanceDates); + + // RDATE + $data = kolab_api_tests::get_data('102-102-102-102', 'Calendar', 'event', 'json', $context); + $result = $api->output($data, $context); + + // recurrence + $arp = new kolab_api_filter_mapistore_structure_appointmentrecurrencepattern; + $arp->input($result['PidLidAppointmentRecur'], true); + + $this->assertSame(2, $arp->RecurrencePattern->DeletedInstanceCount); + $this->assertCount(2, $arp->RecurrencePattern->DeletedInstanceDates); + $this->assertSame(2, $arp->RecurrencePattern->ModifiedInstanceCount); + $this->assertCount(2, $arp->RecurrencePattern->ModifiedInstanceDates); + $this->assertSame(2, $arp->ExceptionCount); + $this->assertCount(2, $arp->ExceptionInfo); + $this->assertCount(2, $arp->ExtendedException); } /** * Test input method */ function test_input() { $api = new kolab_api_filter_mapistore_event; $data = array( - 'PidTagCreationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-05-14T13:03:33Z'), - 'PidTagLastModificationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-05-14T13:50:18Z'), + 'PidTagCreationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T13:03:33Z'), + 'PidTagLastModificationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T13:50:18Z'), 'PidLidAppointmentSequence' => 10, 'PidTagSensitivity' => 3, 'PidNameKeywords' => array('work'), 'PidTagSubject' => 'subject', 'PidTagBody' => 'body', 'PidTagImportance' => 2, 'PidLidLocation' => 'location', - 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore::date_php2mapi('2015-05-14T13:03:33Z'), - 'PidLidAppointmentEndWhole' => kolab_api_filter_mapistore::date_php2mapi('2015-05-14T16:00:00Z'), + 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T13:03:33Z'), + 'PidLidAppointmentEndWhole' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T16:00:00Z'), 'PidLidReminderDelta' => 15, 'PidLidReminderSet' => true, 'recipients' => array( array( 'PidTagDisplayName' => 'German, Mark', 'PidTagEmailAddress' => 'mark.german@example.org', 'PidTagRecipientType' => 1, 'PidTagRecipientFlags' => 3, ), array( 'PidTagDisplayName' => 'Manager, Jane', 'PidTagEmailAddress' => 'manager@example.org', 'PidTagRecipientType' => 1, 'PidTagRecipientTrackStatus' => 2, ), ), ); $result = $api->input($data); $this->assertSame('subject', $result['summary']); $this->assertSame('body', $result['description']); $this->assertSame(10, $result['sequence']); $this->assertSame('confidential', $result['class']); $this->assertSame(array('work'), $result['categories']); $this->assertSame('location', $result['location']); $this->assertSame(1, $result['priority']); $this->assertSame('2015-05-14T13:03:33Z', $result['created']); $this->assertSame('2015-05-14T13:50:18Z', $result['dtstamp']); $this->assertSame('2015-05-14T13:03:33Z', $result['dtstart']); $this->assertSame('2015-05-14T16:00:00Z', $result['dtend']); $this->assertSame('DISPLAY', $result['valarm'][0]['properties']['action']); $this->assertSame('Reminder', $result['valarm'][0]['properties']['description']); $this->assertSame('-PT15M', $result['valarm'][0]['properties']['trigger']['duration']); $this->assertSame('Manager, Jane', $result['attendee'][0]['parameters']['cn']); $this->assertSame('TENTATIVE', $result['attendee'][0]['parameters']['partstat']); $this->assertSame('REQ-PARTICIPANT', $result['attendee'][0]['parameters']['role']); // $this->assertSame(true, $result['attendee'][0]['parameters']['rsvp']); $this->assertSame('mailto:manager%40example.org', $result['attendee'][0]['cal-address']); $this->assertSame('German, Mark', $result['organizer']['parameters']['cn']); $this->assertSame('mailto:mark.german%40example.org', $result['organizer']['cal-address']); self::$original = $result; $data = array( // all-day event - 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore::date_php2mapi('2015-05-14T00:00:00Z'), - 'PidLidAppointmentEndWhole' => kolab_api_filter_mapistore::date_php2mapi('2015-05-14T00:00:00Z'), + 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T00:00:00Z'), + 'PidLidAppointmentEndWhole' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T00:00:00Z'), 'PidLidAppointmentSubType' => 1, 'PidLidReminderSet' => false, // @TODO: recurrence, exceptions, alarms ); $result = $api->input($data); $this->assertSame('2015-05-14', $result['dtstart']); $this->assertSame('2015-05-14', $result['dtend']); $this->assertSame(array(), $result['valarm']); } /** * Test input method with merge */ function test_input2() { $api = new kolab_api_filter_mapistore_event; $data = array( -// 'PidTagCreationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-05-14T13:03:33Z'), -// 'PidTagLastModificationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-05-14T13:50:18Z'), +// 'PidTagCreationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T13:03:33Z'), +// 'PidTagLastModificationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-14T13:50:18Z'), 'PidLidAppointmentSequence' => 20, 'PidTagSensitivity' => 2, 'PidNameKeywords' => array('work1'), 'PidTagSubject' => 'subject1', 'PidTagBody' => 'body1', 'PidTagImportance' => 1, 'PidLidLocation' => 'location1', - 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore::date_php2mapi('2015-05-15T13:03:33Z'), - 'PidLidAppointmentEndWhole' => kolab_api_filter_mapistore::date_php2mapi('2015-05-15T16:00:00Z'), + 'PidLidAppointmentStartWhole' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-15T13:03:33Z'), + 'PidLidAppointmentEndWhole' => kolab_api_filter_mapistore_common::date_php2mapi('2015-05-15T16:00:00Z'), 'PidLidReminderDelta' => 25, 'PidLidReminderSet' => true, ); $result = $api->input($data, self::$original); $this->assertSame('subject1', $result['summary']); $this->assertSame('body1', $result['description']); $this->assertSame(20, $result['sequence']); $this->assertSame('private', $result['class']); $this->assertSame(array('work1'), $result['categories']); $this->assertSame('location1', $result['location']); $this->assertSame(5, $result['priority']); // $this->assertSame('2015-05-14T13:03:33Z', $result['created']); // $this->assertSame('2015-05-14T13:50:18Z', $result['dtstamp']); $this->assertSame('2015-05-15T13:03:33Z', $result['dtstart']); $this->assertSame('2015-05-15T16:00:00Z', $result['dtend']); $this->assertSame('DISPLAY', $result['valarm'][0]['properties']['action']); $this->assertSame('Reminder', $result['valarm'][0]['properties']['description']); $this->assertSame('-PT25M', $result['valarm'][0]['properties']['trigger']['duration']); // @TODO: recurrence, exceptions, attendees } /** * Test map method */ function test_map() { $api = new kolab_api_filter_mapistore_event; $map = $api->map(); $this->assertInternalType('array', $map); $this->assertTrue(!empty($map)); } } diff --git a/tests/Unit/Filter/Mapistore/Note.php b/tests/Unit/Filter/Mapistore/Note.php index 0bb69f0..70c9394 100644 --- a/tests/Unit/Filter/Mapistore/Note.php +++ b/tests/Unit/Filter/Mapistore/Note.php @@ -1,132 +1,130 @@ output($data, $context); $this->assertSame(kolab_api_tests::mapi_uid('Notes', false, '1-1-1-1'), $result['id']); $this->assertSame(kolab_api_tests::folder_uid('Notes', false), $result['parent_id']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-01-20T11:44:59Z'), $result['PidTagCreationTime']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-01-22T11:30:17Z'), $result['PidTagLastModificationTime']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-01-20T11:44:59Z'), $result['PidTagCreationTime']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-01-22T11:30:17Z'), $result['PidTagLastModificationTime']); // $this->assertSame('PUBLIC', $result['classification']); $this->assertSame('test', $result['PidTagSubject']); $this->assertRegexp('//', $result['PidTagBody']); $this->assertSame(100, $result['PidLidNoteX']); $this->assertSame(200, $result['PidLidNoteY']); } /** * Test input method */ function test_input() { $api = new kolab_api_filter_mapistore_note; $data = array( 'id' => kolab_api_tests::mapi_uid('Notes', false, '1-1-1-1'), 'parent_id' => kolab_api_tests::folder_uid('Notes', false), - 'PidTagCreationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-01-20T11:44:59Z'), - 'PidTagLastModificationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-01-22T11:30:17Z'), + 'PidTagCreationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-01-20T11:44:59Z'), + 'PidTagLastModificationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-01-22T11:30:17Z'), 'PidTagSubject' => 'subject', 'PidTagBody' => 'body', 'PidLidNoteColor' => 1, 'PidLidNoteHeight' => 100, 'PidLidNoteWidth' => 200, 'PidLidNoteX' => 300, 'PidLidNoteY' => 400, 'PidNameKeywords' => array('work1'), ); $result = $api->input($data); // $this->assertSame(kolab_api_tests::mapi_uid('Notes', false, '1-1-1-1'), $result['uid']); // $this->assertSame(kolab_api_tests::folder_uid('Notes', false), $result['parent']); $this->assertSame('2015-01-20T11:44:59Z', $result['creation-date']); $this->assertSame('2015-01-22T11:30:17Z', $result['last-modification-date']); $this->assertSame('subject', $result['summary']); $this->assertSame('body', $result['description']); $this->assertSame('MAPI:PidLidNoteColor', $result['x-custom'][0]['identifier']); $this->assertSame(1, $result['x-custom'][0]['value']); $this->assertSame('MAPI:PidLidNoteHeight', $result['x-custom'][1]['identifier']); $this->assertSame(100, $result['x-custom'][1]['value']); $this->assertSame('MAPI:PidLidNoteWidth', $result['x-custom'][2]['identifier']); $this->assertSame(200, $result['x-custom'][2]['value']); $this->assertSame('MAPI:PidLidNoteX', $result['x-custom'][3]['identifier']); $this->assertSame(300, $result['x-custom'][3]['value']); $this->assertSame('MAPI:PidLidNoteY', $result['x-custom'][4]['identifier']); $this->assertSame(400, $result['x-custom'][4]['value']); $this->assertSame(array('work1'), $result['categories']); self::$original = $result; } /** * Test input method with merge */ function test_input2() { $api = new kolab_api_filter_mapistore_note; $data = array( 'id' => kolab_api_tests::mapi_uid('Notes', false, '1-1-1-1'), 'parent_id' => kolab_api_tests::folder_uid('Notes', false), -// 'PidTagCreationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-01-20T11:44:59Z'), -// 'PidTagLastModificationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-01-22T11:30:17Z'), 'PidTagSubject' => 'subject1', 'PidTagBody' => 'body1', 'PidLidNoteX' => 250, 'PidLidNoteColor' => null, ); $result = $api->input($data, self::$original); // $this->assertSame('2015-01-20T11:44:59Z', $result['creation-date']); // $this->assertSame('2015-01-22T11:30:17Z', $result['last-modification-date']); $this->assertSame('subject1', $result['summary']); $this->assertSame('body1', $result['description']); $this->assertSame('MAPI:PidLidNoteHeight', $result['x-custom'][0]['identifier']); $this->assertSame(100, $result['x-custom'][0]['value']); $this->assertSame('MAPI:PidLidNoteWidth', $result['x-custom'][1]['identifier']); $this->assertSame(200, $result['x-custom'][1]['value']); $this->assertSame('MAPI:PidLidNoteX', $result['x-custom'][2]['identifier']); $this->assertSame(250, $result['x-custom'][2]['value']); $this->assertSame('MAPI:PidLidNoteY', $result['x-custom'][3]['identifier']); $this->assertSame(400, $result['x-custom'][3]['value']); $this->assertCount(4, $result['x-custom']); // test unsetting values $api = new kolab_api_filter_mapistore_note; $data = array( 'PidTagSubject' => '', ); $result = $api->input($data, self::$original); $this->assertSame('', $result['summary']); } /** * Test map method */ function test_map() { $api = new kolab_api_filter_mapistore_note; $map = $api->map(); $this->assertInternalType('array', $map); $this->assertTrue(!empty($map)); } } diff --git a/tests/Unit/Filter/Mapistore/Task.php b/tests/Unit/Filter/Mapistore/Task.php index 1da578f..f3de37c 100644 --- a/tests/Unit/Filter/Mapistore/Task.php +++ b/tests/Unit/Filter/Mapistore/Task.php @@ -1,182 +1,182 @@ output($data, $context); $this->assertSame(kolab_api_tests::mapi_uid('Tasks', false, '10-10-10-10'), $result['id']); $this->assertSame(kolab_api_tests::folder_uid('Tasks', false), $result['parent_id']); $this->assertSame('IPM.Task', $result['PidTagMessageClass']); $this->assertSame('tasks', $result['collection']); $this->assertSame('task title', $result['PidTagSubject']); $this->assertSame("task description\nsecond line", $result['PidTagBody']); $this->assertSame(0.56, $result['PidLidPercentComplete']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-04-20T14:22:18Z', true), $result['PidTagLastModificationTime']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-04-20T14:22:18Z', true), $result['PidTagCreationTime']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-04-20T14:22:18Z', true), $result['PidTagLastModificationTime']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-04-20T14:22:18Z', true), $result['PidTagCreationTime']); $this->assertSame(8, $result['PidLidTaskActualEffort']); $data = kolab_api_tests::get_data('20-20-20-20', 'Tasks', 'task', 'json', $context); $result = $api->output($data, $context); $this->assertSame(kolab_api_tests::mapi_uid('Tasks', false, '20-20-20-20'), $result['id']); $this->assertSame(kolab_api_tests::folder_uid('Tasks', false), $result['parent_id']); $this->assertSame('IPM.Task', $result['PidTagMessageClass']); $this->assertSame('tasks', $result['collection']); $this->assertSame('task', $result['PidTagSubject']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-04-20', true), $result['PidLidTaskStartDate']); - $this->assertSame(kolab_api_filter_mapistore::date_php2mapi('2015-04-27', true), $result['PidLidTaskDueDate']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-04-20', true), $result['PidLidTaskStartDate']); + $this->assertSame(kolab_api_filter_mapistore_common::date_php2mapi('2015-04-27', true), $result['PidLidTaskDueDate']); // organizer/attendees $this->assertSame('German, Mark', $result['recipients'][0]['PidTagDisplayName']); $this->assertSame('mark.german@example.org', $result['recipients'][0]['PidTagEmailAddress']); $this->assertSame(1, $result['recipients'][0]['PidTagRecipientType']); $this->assertSame('Manager, Jane', $result['recipients'][1]['PidTagDisplayName']); $this->assertSame(1, $result['recipients'][1]['PidTagRecipientType']); $this->assertSame('jane.manager@example.org', $result['recipients'][1]['PidTagEmailAddress']); // recurrence $rp = new kolab_api_filter_mapistore_structure_recurrencepattern; $rp->input($result['PidLidTaskRecurrence'], true); $this->assertSame(true, $result['PidLidTaskFRecurring']); $this->assertSame(kolab_api_filter_mapistore_structure_recurrencepattern::PATTERNTYPE_DAY, $rp->PatternType); $this->assertSame(kolab_api_filter_mapistore_structure_recurrencepattern::RECURFREQUENCY_DAILY, $rp->RecurFrequency); } /** * Test input method */ function test_input() { $api = new kolab_api_filter_mapistore_task; $data = array( 'id' => kolab_api_tests::mapi_uid('Tasks', false, '10-10-10-10'), 'parent_id' => kolab_api_tests::folder_uid('Tasks', false), - 'PidTagCreationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-01-20T11:44:59Z'), - 'PidTagLastModificationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-01-22T11:30:17Z'), + 'PidTagCreationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-01-20T11:44:59Z'), + 'PidTagLastModificationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-01-22T11:30:17Z'), 'PidTagMessageClass' => 'IPM.Task', 'PidTagSubject' => 'subject', 'PidLidPercentComplete' => 0.56, 'PidTagBody' => 'body', - 'PidLidTaskStartDate' => kolab_api_filter_mapistore::date_php2mapi('2015-04-20', true), - 'PidLidTaskDueDate' => kolab_api_filter_mapistore::date_php2mapi('2015-04-27', true), + 'PidLidTaskStartDate' => kolab_api_filter_mapistore_common::date_php2mapi('2015-04-20', true), + 'PidLidTaskDueDate' => kolab_api_filter_mapistore_common::date_php2mapi('2015-04-27', true), 'PidLidTaskActualEffort' => 16, 'PidLidTaskEstimatedEffort' => 20, 'PidNameKeywords' => array('work1'), 'recipients' => array( array( 'PidTagDisplayName' => 'German, Mark', 'PidTagEmailAddress' => 'mark.german@example.org', 'PidTagRecipientType' => 1, 'PidTagRecipientFlags' => 3, ), array( 'PidTagDisplayName' => 'Manager, Jane', 'PidTagEmailAddress' => 'manager@example.org', 'PidTagRecipientType' => 1, 'PidTagRecipientTrackStatus' => 2, ), ), ); $result = $api->input($data); self::$original = $result; $this->assertSame('subject', $result['summary']); $this->assertSame('body', $result['description']); $this->assertSame(56, $result['percent-complete']); $this->assertSame('2015-01-20T11:44:59Z', $result['created']); $this->assertSame('2015-01-22T11:30:17Z', $result['dtstamp']); $this->assertSame('2015-04-20', $result['dtstart']); $this->assertSame('2015-04-27', $result['due']); $this->assertSame('MAPI:PidLidTaskActualEffort', $result['x-custom'][0]['identifier']); $this->assertSame(16, $result['x-custom'][0]['value']); $this->assertSame('MAPI:PidLidTaskEstimatedEffort', $result['x-custom'][1]['identifier']); $this->assertSame(20, $result['x-custom'][1]['value']); $this->assertSame(array('work1'), $result['categories']); $this->assertSame('Manager, Jane', $result['attendee'][0]['parameters']['cn']); $this->assertSame('TENTATIVE', $result['attendee'][0]['parameters']['partstat']); $this->assertSame('REQ-PARTICIPANT', $result['attendee'][0]['parameters']['role']); // $this->assertSame(true, $result['attendee'][0]['parameters']['rsvp']); $this->assertSame('mailto:manager%40example.org', $result['attendee'][0]['cal-address']); $this->assertSame('German, Mark', $result['organizer']['parameters']['cn']); $this->assertSame('mailto:mark.german%40example.org', $result['organizer']['cal-address']); $data = array( 'PidLidTaskComplete' => true, - 'PidLidTaskDateCompleted' => kolab_api_filter_mapistore::date_php2mapi('2015-04-20', true), + 'PidLidTaskDateCompleted' => kolab_api_filter_mapistore_common::date_php2mapi('2015-04-20', true), 'PidLidTaskActualEffort' => 100, 'PidLidTaskEstimatedEffort' => 100, // @TODO: recurrence ); $result = $api->input($data); $this->assertSame('COMPLETED', $result['status']); $this->assertSame('MAPI:PidLidTaskDateCompleted', $result['x-custom'][0]['identifier']); $this->assertSame(13073961600.0, $result['x-custom'][0]['value']); } /** * Test input method with merge */ function test_input2() { $api = new kolab_api_filter_mapistore_task; $data = array( - 'PidTagCreationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-01-20T12:44:59Z'), - 'PidTagLastModificationTime' => kolab_api_filter_mapistore::date_php2mapi('2015-01-22T12:30:17Z'), + 'PidTagCreationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-01-20T12:44:59Z'), + 'PidTagLastModificationTime' => kolab_api_filter_mapistore_common::date_php2mapi('2015-01-22T12:30:17Z'), // 'PidTagMessageClass' => 'IPM.Task', 'PidTagSubject' => 'subject1', 'PidLidPercentComplete' => 0.66, 'PidTagBody' => 'body1', - 'PidLidTaskStartDate' => kolab_api_filter_mapistore::date_php2mapi('2015-04-21', true), - 'PidLidTaskDueDate' => kolab_api_filter_mapistore::date_php2mapi('2015-04-28', true), + 'PidLidTaskStartDate' => kolab_api_filter_mapistore_common::date_php2mapi('2015-04-21', true), + 'PidLidTaskDueDate' => kolab_api_filter_mapistore_common::date_php2mapi('2015-04-28', true), 'PidLidTaskActualEffort' => 21, 'PidLidTaskEstimatedEffort' => null, ); $result = $api->input($data, self::$original); self::$original = $result; $this->assertSame('subject1', $result['summary']); $this->assertSame('body1', $result['description']); $this->assertSame(66, $result['percent-complete']); $this->assertSame('2015-01-20T12:44:59Z', $result['created']); $this->assertSame('2015-01-22T12:30:17Z', $result['dtstamp']); $this->assertSame('2015-04-21', $result['dtstart']); $this->assertSame('2015-04-28', $result['due']); $this->assertSame('MAPI:PidLidTaskActualEffort', $result['x-custom'][0]['identifier']); $this->assertSame(21, $result['x-custom'][0]['value']); $this->assertCount(1, $result['x-custom']); } /** * Test map method */ function test_map() { $api = new kolab_api_filter_mapistore_task; $map = $api->map(); $this->assertInternalType('array', $map); $this->assertTrue(!empty($map)); } } diff --git a/tests/Unit/Input/Json.php b/tests/Unit/Input/Json.php index 5ecc32b..c1559c1 100644 --- a/tests/Unit/Input/Json.php +++ b/tests/Unit/Input/Json.php @@ -1,116 +1,125 @@ markTestIncomplete('TODO'); } /** * Test to_datetime method */ function test_to_datetime() { $result = kolab_api_input_json::to_datetime(null); $this->assertNull($result); $date = '2014-01-01'; $result = kolab_api_input_json::to_datetime($date); $this->assertInstanceOf('DateTime', $result); $this->assertSame('2014-01-01T00:00:00+00:00', $result->format('c')); $this->assertTrue($result->_dateonly); $date = array('date' => '2014-01-01'); $result = kolab_api_input_json::to_datetime($date); $this->assertInstanceOf('DateTime', $result); $this->assertSame('2014-01-01T00:00:00+00:00', $result->format('c')); $this->assertTrue($result->_dateonly); $date = '2015-04-20T14:22:18Z'; $result = kolab_api_input_json::to_datetime($date); $this->assertInstanceOf('DateTime', $result); $this->assertSame('2015-04-20T14:22:18+00:00', $result->format('c')); $this->assertFalse((bool) $result->_dateonly); $date = array('date-time' => '2015-04-21T00:00:00Z'); $result = kolab_api_input_json::to_datetime($date); $this->assertInstanceOf('DateTime', $result); $this->assertSame('2015-04-21T00:00:00+00:00', $result->format('c')); $this->assertFalse((bool) $result->_dateonly); $date = array( 'date-time' => '2015-04-21T00:00:00', 'parameters' => array( 'tzid' => '/kolab.org/Europe/Zurich', ), ); $result = kolab_api_input_json::to_datetime($date); $this->assertInstanceOf('DateTime', $result); $this->assertSame('2015-04-21T00:00:00+02:00', $result->format('c')); $this->assertFalse((bool) $result->_dateonly); $this->assertSame('Europe/Zurich', $result->getTimezone()->getName()); } /** * Test add_x_custom */ function test_add_x_custom() { kolab_api_input_json::add_x_custom($data, $result); $this->assertNull($result); $data = array('x-custom' => null); kolab_api_input_json::add_x_custom($data, $result); $this->assertSame(array(), $result['x-custom']); $data = array('x-custom' => array()); kolab_api_input_json::add_x_custom($data, $result); $this->assertSame(array(), $result['x-custom']); $data = array('x-custom' => array( array('identifier' => 'i', 'value' => 'v'), )); kolab_api_input_json::add_x_custom($data, $result); $this->assertSame('i', $result['x-custom'][0][0]); $this->assertSame('v', $result['x-custom'][0][1]); } /** * Test input parse_mailto_uri */ function test_parse_mailto_uri() { // @TODO $this->markTestIncomplete('TODO'); } /** * Test input parse_attendees */ function test_parse_attendees() { // @TODO $this->markTestIncomplete('TODO'); } + + /** + * Test input parse_recurrence + */ + function test_parse_recurrence() + { + // @TODO + $this->markTestIncomplete('TODO'); + } } diff --git a/tests/Unit/Input/Json/Event.php b/tests/Unit/Input/Json/Event.php index 93fde62..a8a4f2c 100644 --- a/tests/Unit/Input/Json/Event.php +++ b/tests/Unit/Input/Json/Event.php @@ -1,166 +1,175 @@ input($data); } /** * Test expected exception in input method * * @expectedException kolab_api_exception * @expectedExceptionCode 422 */ function test_input_exception2() { $input = new kolab_api_input_json_event; $data = 'test'; $input->input($data); } /** * Test expected exception in input method * * @expectedException kolab_api_exception * @expectedExceptionCode 422 */ function test_input_exception3() { $input = new kolab_api_input_json_event; $data = array('test' => 'test'); // 'dtstamp' field is required $input->input($data); } /** * Test input method (convert JSON to internal format) */ function test_input() { $input = new kolab_api_input_json_event; $data = array( 'description' => 'description', 'summary' => 'summary', 'sequence' => 10, 'class' => 'PUBLIC', 'categories' => array('test'), 'created' => '2015-04-20T14:22:18Z', 'dtstamp' => '2015-04-21T00:00:00Z', 'status' => 'NEEDS-ACTION', 'dtstart' => '2014-01-01', 'dtend' => '2014-02-01', 'location' => null, 'priority' => 1, 'url' => 'url', 'attendee' => array( array( 'parameters' => array( 'cn' => 'Manager, Jane', 'partstat' => 'NEEDS-ACTION', 'role' => 'REQ-PARTICIPANT', 'rsvp' => true, ), 'cal-address' => 'mailto:%3Cjane.manager%40example.org%3E', ), ), 'organizer' => array( 'parameters' => array( 'cn' => 'Organizer', ), 'cal-address' => 'mailto:organizer%40example.org', ), 'exdate' => array( 'date' => array( '2015-06-05', '2015-06-12', ), ), + 'rdate' => array( + 'date' => array( + '2015-06-15', + '2015-06-22', + ), + ), ); $input->input($data); $this->assertSame('description', $data['description']); $this->assertSame('summary', $data['title']); $this->assertSame('public', $data['sensitivity']); $this->assertSame(10, $data['sequence']); $this->assertSame(array('test'), $data['categories']); $this->assertSame(null, $data['location']); $this->assertSame(1, $data['priority']); $this->assertSame('url', $data['url']); $this->assertSame(kolab_api_input_json::to_datetime('2015-04-20T14:22:18Z')->format('c'), $data['created']->format('c')); $this->assertSame(kolab_api_input_json::to_datetime('2015-04-21T00:00:00Z')->format('c'), $data['changed']->format('c')); $this->assertSame(kolab_api_input_json::to_datetime('2014-01-01')->format('c'), $data['start']->format('c')); $this->assertSame(kolab_api_input_json::to_datetime('2014-02-01')->format('c'), $data['end']->format('c')); $this->assertSame('Manager, Jane', $data['attendees'][0]['name']); $this->assertSame('NEEDS-ACTION', $data['attendees'][0]['status']); $this->assertSame('REQ-PARTICIPANT', $data['attendees'][0]['role']); $this->assertSame(true, $data['attendees'][0]['rsvp']); $this->assertSame('jane.manager@example.org', $data['attendees'][0]['email']); $this->assertSame('Organizer', $data['organizer']['name']); $this->assertSame('organizer@example.org', $data['organizer']['email']); $this->assertSame('2015-06-05', $data['recurrence']['EXDATE'][0]); $this->assertSame('2015-06-12', $data['recurrence']['EXDATE'][1]); + $this->assertSame('2015-06-15', $data['recurrence']['RDATE'][0]); + $this->assertSame('2015-06-22', $data['recurrence']['RDATE'][1]); + self::$original = $data; } /** * Test input method with merging */ function test_input2() { $input = new kolab_api_input_json_event; $data = array( 'description' => 'description1', 'summary' => 'summary1', 'sequence' => 20, 'class' => 'PRIVATE', 'categories' => array('test1'), // 'created' => '2015-04-20T14:22:18Z', // 'dtstamp' => '2015-04-21T00:00:00Z', // 'status' => 'IN-PROCESS', 'dtstart' => '2014-01-11', 'dtend' => '2014-02-11', 'location' => 'location1', 'priority' => 2, 'url' => 'url1', ); $input->input($data, self::$original); $this->assertSame('description1', $data['description']); $this->assertSame('summary1', $data['title']); $this->assertSame('private', $data['sensitivity']); $this->assertSame(20, $data['sequence']); $this->assertSame(array('test1'), $data['categories']); $this->assertSame('location1', $data['location']); $this->assertSame(2, $data['priority']); $this->assertSame('url1', $data['url']); // $this->assertSame(kolab_api_input_json::to_datetime('2015-04-20T14:22:18Z')->format('c'), $data['created']->format('c')); // $this->assertSame(kolab_api_input_json::to_datetime('2015-04-21T00:00:00Z')->format('c'), $data['changed']->format('c')); $this->assertSame(kolab_api_input_json::to_datetime('2014-01-11')->format('c'), $data['start']->format('c')); $this->assertSame(kolab_api_input_json::to_datetime('2014-02-11')->format('c'), $data['end']->format('c')); } } diff --git a/tests/Unit/Output/Json.php b/tests/Unit/Output/Json.php index 942a0e4..811993f 100644 --- a/tests/Unit/Output/Json.php +++ b/tests/Unit/Output/Json.php @@ -1,34 +1,42 @@ markTestIncomplete('TODO'); } /** * Test xml_to_array method */ function test_xml_to_array() { $this->markTestIncomplete('TODO'); } /** * Test parse_array_result method */ function test_parse_array_result() { $this->markTestIncomplete('TODO'); } + + /** + * Test parse_recurrence + */ + function test_parse_recurrence() + { + $this->markTestIncomplete('TODO'); + } } diff --git a/tests/Unit/Output/Json/Event.php b/tests/Unit/Output/Json/Event.php index 723f550..34ff68c 100644 --- a/tests/Unit/Output/Json/Event.php +++ b/tests/Unit/Output/Json/Event.php @@ -1,62 +1,69 @@ element($object); $this->assertSame('100-100-100-100', $result['uid']); $this->assertSame('2015-05-14T13:03:33Z', $result['created']); $this->assertSame('2015-05-14T13:50:18Z', $result['dtstamp']); $this->assertSame(2, $result['sequence']); $this->assertSame('CONFIDENTIAL', $result['class']); $this->assertSame('Work', $result['categories'][0]); $this->assertSame('/kolab.org/Europe/Berlin', $result['dtstart']['parameters']['tzid']); $this->assertSame('2015-05-15T10:00:00', $result['dtstart']['date-time']); $this->assertSame('/kolab.org/Europe/Berlin', $result['dtend']['parameters']['tzid']); $this->assertSame('2015-05-15T10:30:00', $result['dtend']['date-time']); $this->assertSame('Summary', $result['summary']); $this->assertSame('Description', $result['description']); $this->assertSame(1, $result['priority']); $this->assertSame('Location', $result['location']); $this->assertSame('German, Mark', $result['organizer']['parameters']['cn']); $this->assertSame('mailto:%3Cmark.german%40example.org%3E', $result['organizer']['cal-address']); $this->assertSame('https://some.url', $result['url']); $this->assertSame('Manager, Jane', $result['attendee'][0]['parameters']['cn']); $this->assertSame('NEEDS-ACTION', $result['attendee'][0]['parameters']['partstat']); $this->assertSame('REQ-PARTICIPANT', $result['attendee'][0]['parameters']['role']); $this->assertSame(true, $result['attendee'][0]['parameters']['rsvp']); $this->assertSame('mailto:%3Cjane.manager%40example.org%3E', $result['attendee'][0]['cal-address']); $this->assertSame('image/jpeg', $result['attach'][0]['parameters']['fmttype']); $this->assertSame('photo-mini.jpg', $result['attach'][0]['parameters']['x-label']); $this->assertSame('cid:photo-mini.1431611291.28810.jpg', $result['attach'][0]['uri']); $this->assertSame('DISPLAY', $result['valarm'][0]['properties']['action']); $this->assertSame('Summary', $result['valarm'][0]['properties']['description']); $this->assertSame('START', $result['valarm'][0]['properties']['trigger']['parameters']['related']); $this->assertSame('-PT15M', $result['valarm'][0]['properties']['trigger']['duration']); $object = kolab_api_tests::get_data('101-101-101-101', 'Calendar', 'event', null, $context); $result = $output->element($object); $this->assertSame('101-101-101-101', $result['uid']); $this->assertSame('PUBLIC', $result['class']); $this->assertSame('2015-05-15', $result['dtstart']); $this->assertSame('2015-05-15', $result['dtend']); $this->assertSame('WEEKLY', $result['rrule']['recur']['freq']); $this->assertSame('MO', $result['rrule']['recur']['byday']); $this->assertSame('2015-06-05', $result['exdate']['date'][0]); $this->assertSame('2015-06-12', $result['exdate']['date'][1]); + + $object = kolab_api_tests::get_data('102-102-102-102', 'Calendar', 'event', null, $context); + $result = $output->element($object); + + $this->assertSame('102-102-102-102', $result['uid']); + $this->assertSame('2015-06-25', $result['rdate']['date'][0]); + $this->assertSame('2015-06-28', $result['rdate']['date'][1]); } } diff --git a/tests/data/event/102-102-102-102 b/tests/data/event/102-102-102-102 new file mode 100644 index 0000000..1e2c9e2 --- /dev/null +++ b/tests/data/event/102-102-102-102 @@ -0,0 +1,148 @@ +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="=_e9a7b998f314d7e1dc719a6613e55723" +From: mark.german@example.org +To: mark.german@example.org +Date: Thu, 25 Jun 2015 10:26:58 +0200 +X-Kolab-Type: application/x-vnd.kolab.event +X-Kolab-Mime-Version: 3.0 +Subject: 102-102-102-102 +User-Agent: Kolab 3.1/Roundcube 1.2-git + +--=_e9a7b998f314d7e1dc719a6613e55723 +Content-Transfer-Encoding: quoted-printable +Content-Type: text/plain; charset=ISO-8859-1 + +This is a Kolab Groupware object. To view this object you will need an emai= +l client that understands the Kolab Groupware format. For a list of such em= +ail clients please visit http://www.kolab.org/ + + +--=_e9a7b998f314d7e1dc719a6613e55723 +Content-Transfer-Encoding: 8bit +Content-Type: application/calendar+xml; charset=UTF-8; + name=kolab.xml +Content-Disposition: attachment; + filename=kolab.xml; + size=4169 + + + + + + + + Roundcube-libkolab-1.1 Libkolabxml-1.1 + + + 2.0 + + + 3.1.0 + + + + + + + 102-102-102-102 + + + 2015-01-21T07:46:50Z + + + 2015-06-25T08:26:58Z + + + 1 + + + PUBLIC + + + + + /kolab.org/Europe/Berlin + + + 2015-01-21T06:00:00 + + + + + /kolab.org/Europe/Berlin + + + 2015-01-21T06:30:00 + + + + + /kolab.org/Europe/Berlin + + + 2015-06-25 + 2015-06-28 + + + by date + + + recurring by date with a resource + + + + + German, Mark + + + mailto:%3Cmark.german%40example.org%3E + + + + + Audi A4 + + + NEEDS-ACTION + + + REQ-PARTICIPANT + + + true + + + RESOURCE + + + mailto:%3Cresource-car-audia4%40example.org%3E + + + + + + + DISPLAY + + + by date + + + + + START + + + -PT15M + + + + + + + + + + +--=_e9a7b998f314d7e1dc719a6613e55723--