Horde_SyncMl_Device_sync4j::vcard2sif PHP Method

vcard2sif() public method

public vcard2sif ( $vcard )
    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>');
    }

Usage Example

Example #1
0
 /**
  * Converts the content from the backend to a format suitable for the
  * client device.
  *
  * Strips the uid (primary key) information as client and server might use
  * different ones.
  *
  * @param string $content      The content to convert
  * @param string $contentType  The content type of content as returned
  *                             from the backend
  * @param string $database     The server database URI.
  *
  * @return array  Three-element array with the converted content, the
  *                (possibly changed) new content type, and encoding type
  *                (like b64 as used by Funambol).
  */
 public function convertServer2Client($content, $contentType, $database)
 {
     $database = $GLOBALS['backend']->normalize($database);
     list($content, $contentType, $encodingType) = parent::convertServer2Client($content, $contentType, $database);
     if ($this->requestedContentType == $contentType) {
         if ($contentType == 'text/calendar' || $contentType == 'text/x-vcalendar') {
             $si = $GLOBALS['backend']->state->sourceURI;
             if (stristr($si, 'fol-') !== false) {
                 // The Funambol Outlook connector uses invalid STATUS
                 // values. Actually it maps MeetingStatus values of the
                 // Outlook event to the STATUS property, which is
                 // completely useless. So drop the STATUS altogether.
                 $content = preg_replace('/^STATUS:.*\\r?\\n/im', '', $content);
             }
         }
         return array($content, $contentType, $encodingType);
     }
     switch ($contentType) {
         case 'text/calendar':
         case 'text/x-vcalendar':
             switch ($database) {
                 case 'calendar':
                     $content = Horde_SyncMl_Device_sync4j::vevent2sif($content);
                     $content = base64_encode($content);
                     $contentType = 'text/x-s4j-sife';
                     break 2;
                 case 'tasks':
                     $content = Horde_SyncMl_Device_sync4j::vtodo2sif($content);
                     $content = base64_encode($content);
                     $contentType = 'text/x-s4j-sift';
                     break 2;
             }
             break;
         case 'text/x-vcard':
             $content = Horde_SyncMl_Device_sync4j::vcard2sif($content);
             $content = base64_encode($content);
             $contentType = 'text/x-s4j-sifc';
             break;
         case 'text/x-vnote':
         case 'text/plain':
             $content = Horde_SyncMl_Device_sync4j::vnote2sif($content);
             $content = base64_encode($content);
             $contentType = 'text/x-s4j-sifn';
             break;
     }
     $l = "\nOutput converted for client ({$contentType}):\n" . base64_decode($content) . "\n";
     $GLOBALS['backend']->logFile(Horde_SyncMl_Backend::LOGFILE_DATA, $l);
     return array($content, $contentType, 'b64');
 }