Static function to make a given email address rfc822 compliant.
static public getBareEmail ( string $address ) : string | ||
$address | string | An email address. |
return | string | The RFC822-formatted email address. |
public function vcard2sif($vcard) { $iCal = new Horde_Icalendar(); if (!$iCal->parsevCalendar($vcard)) { // @TODO: NEVER use die() in a library. die("There was an error importing the data."); } $components = $iCal->getComponents(); switch (count($components)) { case 0: // @TODO: NEVER use die() in a library. die("No data was found."); case 1: $content = $components[0]; break; default: // @TODO: NEVER use die() in a library. die("Multiple components found; only one is supported."); } // from here on, the code is taken from // Turba_Driver::toHash, v 1.65 2005/03/12 // and modified for the Sync4J attribute names. $attr = $content->getAllAttributes(); foreach ($attr as $item) { switch ($item['name']) { case 'FN': $hash['FileAs'] = $item['value']; break; case 'N': $name = $item['values']; $hash['LastName'] = $name[Horde_Icalendar_Vcard::N_FAMILY]; $hash['FirstName'] = $name[Horde_Icalendar_Vcard::N_GIVEN]; $hash['MiddleName'] = $name[Horde_Icalendar_Vcard::N_ADDL]; $hash['Title'] = $name[Horde_Icalendar_Vcard::N_PREFIX]; $hash['Suffix'] = $name[Horde_Icalendar_Vcard::N_SUFFIX]; break; case 'NICKNAME': $hash['NickName'] = $item['value']; break; // For vCard 3.0. // For vCard 3.0. case 'ADR': if (isset($item['params']['TYPE'])) { if (!is_array($item['params']['TYPE'])) { $item['params']['TYPE'] = array($item['params']['TYPE']); } } else { $item['params']['TYPE'] = array(); if (isset($item['params']['WORK'])) { $item['params']['TYPE'][] = 'WORK'; } if (isset($item['params']['HOME'])) { $item['params']['TYPE'][] = 'HOME'; } } $address = $item['values']; foreach ($item['params']['TYPE'] as $adr) { switch (Horde_String::upper($adr)) { case 'HOME': $prefix = 'HomeAddress'; break; case 'WORK': $prefix = 'BusinessAddress'; break; default: $prefix = 'HomeAddress'; } if ($prefix) { $hash[$prefix . 'Street'] = isset($address[Horde_Icalendar_Vcard::ADR_STREET]) ? $address[Horde_Icalendar_Vcard::ADR_STREET] : null; $hash[$prefix . 'City'] = isset($address[Horde_Icalendar_Vcard::ADR_LOCALITY]) ? $address[Horde_Icalendar_Vcard::ADR_LOCALITY] : null; $hash[$prefix . 'State'] = isset($address[Horde_Icalendar_Vcard::ADR_REGION]) ? $address[Horde_Icalendar_Vcard::ADR_REGION] : null; $hash[$prefix . 'PostalCode'] = isset($address[Horde_Icalendar_Vcard::ADR_POSTCODE]) ? $address[Horde_Icalendar_Vcard::ADR_POSTCODE] : null; $hash[$prefix . 'Country'] = isset($address[Horde_Icalendar_Vcard::ADR_COUNTRY]) ? $address[Horde_Icalendar_Vcard::ADR_COUNTRY] : null; $hash[$prefix . 'PostOfficeBox'] = isset($address[Horde_Icalendar_Vcard::ADR_POB]) ? $address[Horde_Icalendar_Vcard::ADR_POB] : null; } } break; case 'TEL': if (isset($item['params']['FAX'])) { if (isset($item['params']['WORK'])) { $hash['BusinessFaxNumber'] = $item['value']; } elseif (isset($item['params']['HOME'])) { $hash['HomeFaxNumber'] = $item['value']; } else { $hash['OtherFaxNumber'] = $item['value']; } } elseif (isset($item['params']['TYPE'])) { if (!is_array($item['params']['TYPE'])) { $item['params']['TYPE'] = array($item['params']['TYPE']); } // For vCard 3.0. foreach ($item['params']['TYPE'] as $tel) { if (Horde_String::upper($tel) == 'WORK') { $hash['BusinessTelephoneNumber'] = $item['value']; } elseif (Horde_String::upper($tel) == 'HOME') { $hash['HomeTelephoneNumber'] = $item['value']; } elseif (Horde_String::upper($tel) == 'CELL') { $hash['MobileTelephoneNumber'] = $item['value']; } elseif (Horde_String::upper($tel) == 'FAX') { $hash['BusinessFaxNumber'] = $item['value']; } } } else { if (isset($item['params']['HOME'])) { $hash['HomeTelephoneNumber'] = $item['value']; } elseif (isset($item['params']['WORK'])) { $hash['BusinessTelephoneNumber'] = $item['value']; } elseif (isset($item['params']['CELL'])) { $hash['MobileTelephoneNumber'] = $item['value']; } elseif (!isset($hash['HomeTelephoneNumber'])) { $hash['HomeTelephoneNumber'] = $item['value']; } } break; case 'EMAIL': $email_set = false; if (isset($item['params']['HOME']) && (!isset($hash['Email2Address']) || isset($item['params']['PREF']))) { $hash['Email2Address'] = Horde_Icalendar_Vcard::getBareEmail($item['value']); $email_set = true; } elseif (isset($item['params']['WORK']) && (!isset($hash['Email3Address']) || isset($item['params']['PREF']))) { $hash['Email3Address'] = Horde_Icalendar_Vcard::getBareEmail($item['value']); $email_set = true; } elseif (isset($item['params']['TYPE'])) { if (!is_array($item['params']['TYPE'])) { $item['params']['TYPE'] = array($item['params']['TYPE']); } if (in_array('HOME', $item['params']['TYPE']) && (!isset($hash['Email2Address']) || in_array('PREF', $item['params']['TYPE']))) { $hash['Email2Address'] = Horde_Icalendar_Vcard::getBareEmail($item['value']); $email_set = true; } elseif (in_array('WORK', $item['params']['TYPE']) && (!isset($hash['Email3Address']) || in_array('PREF', $item['params']['TYPE']))) { $hash['Email3Address'] = Horde_Icalendar_Vcard::getBareEmail($item['value']); $email_set = true; } } if (!$email_set && (!isset($hash['Email1Address']) || isset($item['params']['PREF']))) { $hash['Email1Address'] = Horde_Icalendar_Vcard::getBareEmail($item['value']); } break; case 'TITLE': $hash['JobTitle'] = $item['value']; break; case 'ORG': $values = preg_split('/(?<!\\\\);/', trim($item['value'])); $hash['CompanyName'] = $values[0]; $hash['Department'] = isset($values[1]) ? $values[1] : ''; break; case 'NOTE': $hash['Body'] = $item['value']; break; case 'URL': $hash['WebPage'] = $item['value']; break; case 'BDAY': if (is_array($item['value'])) { $hash['Birthday'] = sprintf('%04d-%02d-%02d', $item['value']['year'], $item['value']['month'], $item['value']['mday']); } break; case 'X-ANNIVERSARY': if (is_array($item['value'])) { $hash['Anniversary'] = sprintf('%04d-%02d-%02d', $item['value']['year'], $item['value']['month'], $item['value']['mday']); } break; case 'X-SPOUSE': $hash['Spouse'] = $item['value']; break; case 'X-CHILDREN': $hash['Children'] = $item['value']; break; case 'CATEGORIES': $hash['Categories'] = $item['value']; break; } } return Horde_SyncMl_Device_sync4j::array2sif($hash, '<?xml version="1.0"?><contact>', '</contact>'); }
/** * Convert an ActiveSync contact message into a hash suitable for * importing via self::add(). * * @param Horde_ActiveSync_Message_Contact $message The contact message * object. * * @return array A contact hash. */ public function fromASContact(Horde_ActiveSync_Message_Contact $message) { $hash = array(); foreach (self::$_asMap as $turbaField => $asField) { if (!$message->isGhosted($asField)) { try { $hash[$turbaField] = $message->{$asField}; } catch (InvalidArgumentException $e) { } } } /* Requires special handling */ try { if ($message->getProtocolVersion() >= Horde_ActiveSync::VERSION_TWELVE) { if (!empty($message->airsyncbasebody)) { $hash['notes'] = $message->airsyncbasebody->data; } } else { $hash['notes'] = $message->body; } } catch (InvalidArgumentException $e) { } // picture ($message->picture *should* already be base64 encdoed) if (!$message->isGhosted('picture')) { $hash['photo'] = base64_decode($message->picture); } /* Email addresses */ $hash['emails'] = array(); if (!$message->isGhosted('email1address')) { $e = Horde_Icalendar_Vcard::getBareEmail($message->email1address); $hash['emails'][] = $hash['email'] = $e ? $e : ''; } if (!$message->isGhosted('email2address')) { $e = Horde_Icalendar_Vcard::getBareEmail($message->email2address); $hash['emails'][] = $hash['homeEmail'] = $e ? $e : ''; } if (!$message->isGhosted('email3address')) { $e = Horde_Icalendar_Vcard::getBareEmail($message->email3address); $hash['emails'][] = $hash['workEmail'] = $e ? $e : ''; } $hash['emails'] = implode(',', $hash['emails']); /* Categories */ if (is_array($message->categories) && count($message->categories)) { $hash['__tags'] = $message->categories; } /* Children */ if (is_array($message->children) && count($message->children)) { // We use a comma as incoming delimiter as it's the most // common even though it might be used withing a name string. $hash['children'] = implode(', ', $message->children); } elseif (!$message->isGhosted('children')) { $hash['children'] = ''; } /* Birthday and Anniversary */ if (!empty($message->birthday)) { $bday = new Horde_Date($message->birthday); $bday->setTimezone(date_default_timezone_get()); $hash['birthday'] = $bday->format('Y-m-d'); } elseif (!$message->isGhosted('birthday')) { $hash['birthday'] = ''; } if (!empty($message->anniversary)) { $anniversary = new Horde_Date($message->anniversary); $anniversary->setTimezone(date_default_timezone_get()); $hash['anniversary'] = $anniversary->format('Y-m-d'); } elseif (!$message->isGhosted('anniversary')) { $hash['anniversary'] = ''; } /* Countries */ include 'Horde/Nls/Countries.php'; if (!empty($message->homecountry)) { if (!empty($this->map['homeCountryFree'])) { $hash['homeCountryFree'] = $message->homecountry; } else { $country = array_search($message->homecountry, $countries); if ($country === false) { $country = $message->homecountry; } $hash['homeCountry'] = $country; } } elseif (!$message->isGhosted('homecountry')) { $hash['homeCountry'] = ''; } if (!empty($message->businesscountry)) { if (!empty($this->map['workCountryFree'])) { $hash['workCountryFree'] = $message->businesscountry; } else { $country = array_search($message->businesscountry, $countries); if ($country === false) { $country = $message->businesscountry; } $hash['workCountry'] = $country; } } elseif (!$message->isGhosted('businesscountry')) { $hash['workCountry'] = ''; } if (!empty($message->othercountry)) { if (!empty($this->map['otherCountryFree'])) { $hash['otherCountryFree'] = $message->othercountry; } else { $country = array_search($message->othercountry, $countries); if ($country === false) { $country = $message->othercountry; } $hash['otherCountry'] = $country; } } elseif (!$message->isGhosted('othercountry')) { $hash['otherCountry'] = ''; } return $hash; }