/** * Make sure MOBILE phone is returned as CELL (as specified in standard) */ function test_parse_three() { $vcard = new rcube_vcard(file_get_contents($this->_srcpath('johndoe.vcf')), null); $vcf = $vcard->export(); $this->assertRegExp('/TEL;CELL:\\+987654321/', $vcf, "Return CELL instead of MOBILE (import)"); $vcard = new rcube_vcard(); $vcard->set('phone', '+987654321', 'MOBILE'); $vcf = $vcard->export(); $this->assertRegExp('/TEL;TYPE=CELL:\\+987654321/', $vcf, "Return CELL instead of MOBILE (set)"); }
/** * Convert CSV data row to vCard */ protected function csv_to_vcard($data) { $contact = array(); foreach ($this->map as $idx => $name) { $value = $data[$idx]; if ($value !== null && $value !== '') { $contact[$name] = $value; } } if (empty($contact)) { return; } // Handle special values if (!empty($contact['birthday-d']) && !empty($contact['birthday-m']) && !empty($contact['birthday-y'])) { $contact['birthday'] = $contact['birthday-y'] . '-' . $contact['birthday-m'] . '-' . $contact['birthday-d']; } // categories/groups separator in vCard is ',' not ';' if (!empty($contact['groups'])) { $contact['groups'] = str_replace(';', ',', $contact['groups']); } // Empty dates, e.g. "0/0/00", "0000-00-00 00:00:00" foreach (array('birthday', 'anniversary') as $key) { if (!empty($contact[$key])) { $date = preg_replace('/[0[:^word:]]/', '', $contact[$key]); if (empty($date)) { unset($contact[$key]); } } } if (!empty($contact['gender']) && ($gender = strtolower($contact['gender']))) { if (!in_array($gender, array('male', 'female'))) { unset($contact['gender']); } } // Convert address(es) to rcube_vcard data foreach ($contact as $idx => $value) { $name = explode(':', $idx); if (in_array($name[0], array('street', 'locality', 'region', 'zipcode', 'country'))) { $contact['address:' . $name[1]][$name[0]] = $value; unset($contact[$idx]); } } // Create vcard object $vcard = new rcube_vcard(); foreach ($contact as $name => $value) { $name = explode(':', $name); $vcard->set($name[0], $value, $name[1]); } // add to the list $this->vcards[] = $vcard; }
/** * Adds a Name field to vcard if missing * * @param string $vcard vCard * @return string */ private function vcard_check($raw) { if ($raw !== null) { $vcard = new rcube_vcard(); $vcard->load($raw); $data = $vcard->get_assoc(); if (!isset($data['name'])) { if (isset($data['surname'])) { $data['name'] = $data['surname']; } else { if (isset($data['displayname'])) { $data['name'] = $data['displayname']; } else { if (isset($data['nickname'])) { $data['name'] = $data['nickname']; } else { if (isset($data['firstname'])) { $data['name'] = $data['firstname']; } else { if (isset($data['middlename'])) { $data['name'] = $data['middlename']; } else { if (is_array($data['email:home'])) { $data['name'] = $data['email:home'][0]; } else { if (is_array($data['email:work'])) { $data['name'] = $data['email:work'][0]; } else { if (is_array($data['email:other'])) { $data['name'] = $data['email:other'][0]; } else { $data['name'] = 'unknown'; } } } } } } } } $vcard->set('surname', '', false); $vcard->set('firstname', current(explode('@', $data['name'])), false); $raw = $vcard->export(); } } return $raw; }
private function convert_save_data($save_data, $record = array()) { $out = array(); $words = ''; // copy values into vcard object $vcard = new rcube_vcard($record['vcard'] ? $record['vcard'] : $save_data['vcard'], RCMAIL_CHARSET, false, $this->vcard_fieldmap); $vcard->reset(); foreach ($save_data as $key => $values) { list($field, $section) = explode(':', $key); $fulltext = in_array($field, $this->fulltext_cols); foreach ((array) $values as $value) { if (isset($value)) { $vcard->set($field, $value, $section); } if ($fulltext && is_array($value)) { $words .= ' ' . rcube_utils::normalize_string(join(" ", $value)); } else { if ($fulltext && strlen($value) >= 3) { $words .= ' ' . rcube_utils::normalize_string($value); } } } } $out['vcard'] = $vcard->export(false); foreach ($this->table_cols as $col) { $key = $col; if (!isset($save_data[$key])) { $key .= ':home'; } if (isset($save_data[$key])) { if (is_array($save_data[$key])) { $out[$col] = join(self::SEPARATOR, $save_data[$key]); } else { $out[$col] = $save_data[$key]; } } } // save all e-mails in database column $out['email'] = join(self::SEPARATOR, $vcard->email); // join words for fulltext search $out['words'] = join(" ", array_unique(explode(" ", $words))); return $out; }
/** * Get vcard data for specified contact */ private function get_contact_vcard($source, $cid, &$filename = null) { $rcmail = rcmail::get_instance(); $source = $rcmail->get_address_book($source); $contact = $source->get_record($cid, true); if ($contact) { $fieldmap = $source ? $source->vcard_map : null; if (empty($contact['vcard'])) { $vcard = new rcube_vcard('', RCUBE_CHARSET, false, $fieldmap); $vcard->reset(); foreach ($contact as $key => $values) { list($field, $section) = explode(':', $key); // avoid unwanted casting of DateTime objects to an array // (same as in rcube_contacts::convert_save_data()) if (is_object($values) && is_a($values, 'DateTime')) { $values = array($values); } foreach ((array) $values as $value) { if (is_array($value) || is_a($value, 'DateTime') || @strlen($value)) { $vcard->set($field, $value, strtoupper($section)); } } } $contact['vcard'] = $vcard->export(); } $name = rcube_addressbook::compose_list_name($contact); $filename = (self::parse_filename($name) ?: 'contact') . '.vcf'; // fix folding and end-of-line chars $vcard = preg_replace('/\\r|\\n\\s+/', '', $contact['vcard']); $vcard = preg_replace('/\\n/', rcube_vcard::$eol, $vcard); return rcube_vcard::rfc2425_fold($vcard) . rcube_vcard::$eol; } }
/** * Convert CSV data row to vCard */ protected function csv_to_vcard($data) { $contact = array(); foreach ($this->map as $idx => $name) { $value = $data[$idx]; if ($value !== null && $value !== '') { if (!empty($contact[$name])) { $contact[$name] = (array) $contact[$name]; $contact[$name][] = $value; } else { $contact[$name] = $value; } } } // Gmail format support foreach ($this->gmail_map as $idx => $item) { $type = preg_replace('/[^a-z]/', '', strtolower($data[$item['_idx']])); $key = $item['_key']; unset($item['_idx']); unset($item['_key']); foreach ($item as $item_key => $item_idx) { $value = $data[$item_idx]; if ($value !== null && $value !== '') { foreach (array($type, '*') as $_type) { if ($data_idx = $this->gmail_label_map[$key][$item_key][$_type]) { $value = explode(' ::: ', $value); if (!empty($contact[$data_idx])) { $contact[$data_idx] = array_merge((array) $contact[$data_idx], $value); } else { $contact[$data_idx] = $value; } break; } } } } } if (empty($contact)) { return; } // Handle special values if (!empty($contact['birthday-d']) && !empty($contact['birthday-m']) && !empty($contact['birthday-y'])) { $contact['birthday'] = $contact['birthday-y'] . '-' . $contact['birthday-m'] . '-' . $contact['birthday-d']; } if (!empty($contact['groups'])) { // categories/groups separator in vCard is ',' not ';' $contact['groups'] = str_replace(',', '', $contact['groups']); $contact['groups'] = str_replace(';', ',', $contact['groups']); if (!empty($this->gmail_map)) { // remove "* " added by GMail $contact['groups'] = str_replace('* ', '', $contact['groups']); // replace strange delimiter $contact['groups'] = str_replace(' ::: ', ',', $contact['groups']); } } // Empty dates, e.g. "0/0/00", "0000-00-00 00:00:00" foreach (array('birthday', 'anniversary') as $key) { if (!empty($contact[$key])) { $date = preg_replace('/[0[:^word:]]/', '', $contact[$key]); if (empty($date)) { unset($contact[$key]); } } } if (!empty($contact['gender']) && ($gender = strtolower($contact['gender']))) { if (!in_array($gender, array('male', 'female'))) { unset($contact['gender']); } } // Convert address(es) to rcube_vcard data foreach ($contact as $idx => $value) { $name = explode(':', $idx); if (in_array($name[0], array('street', 'locality', 'region', 'zipcode', 'country'))) { $contact['address:' . $name[1]][$name[0]] = $value; unset($contact[$idx]); } } // Create vcard object $vcard = new rcube_vcard(); foreach ($contact as $name => $value) { $name = explode(':', $name); if (is_array($value) && $name[0] != 'address') { foreach ((array) $value as $val) { $vcard->set($name[0], $val, $name[1]); } } else { $vcard->set($name[0], $value, $name[1]); } } // add to the list $this->vcards[] = $vcard; }
/** * Convert CSV data row to vCard */ protected function csv_to_vcard($data) { $contact = array(); foreach ($this->map as $idx => $name) { $value = $data[$idx]; if ($value !== null && $value !== '') { $contact[$name] = $value; } } if (empty($contact)) { return; } // Handle special values if (!empty($contact['birthday-d']) && !empty($contact['birthday-m']) && !empty($contact['birthday-y'])) { $contact['birthday'] = $contact['birthday-y'] . '-' . $contact['birthday-m'] . '-' . $contact['birthday-d']; } foreach (array('birthday', 'anniversary') as $key) { if (!empty($contact[$key]) && $contact[$key] == '0/0/00') { // @TODO: localization? unset($contact[$key]); } } if (!empty($contact['gender']) && ($gender = strtolower($contact['gender']))) { if (!in_array($gender, array('male', 'female'))) { unset($contact['gender']); } } // Convert address(es) to rcube_vcard data foreach ($contact as $idx => $value) { $name = explode(':', $idx); if (in_array($name[0], array('street', 'locality', 'region', 'zipcode', 'country'))) { $contact['address:' . $name[1]][$name[0]] = $value; unset($contact[$idx]); } } // Create vcard object $vcard = new rcube_vcard(); foreach ($contact as $name => $value) { $name = explode(':', $name); $vcard->set($name[0], $value, $name[1]); } // add to the list $this->vcards[] = $vcard; }