/**
  * Returns the actual SyncXXX object type.
  *
  * @param string            $folderid           id of the parent folder
  * @param string            $id                 id of the message
  * @param ContentParameters $contentparameters  parameters of the requested message (truncation, mimesupport etc)
  *
  * @access public
  * @return object/false     false if the message could not be retrieved
  */
 public function GetMessage($folderid, $id, $contentparameters)
 {
     ZLog::Write(LOGLEVEL_DEBUG, 'VCDir::GetMessage(' . $folderid . ', ' . $id . ', ..)');
     if ($folderid != "root") {
         return;
     }
     $types = array('dom' => 'type', 'intl' => 'type', 'postal' => 'type', 'parcel' => 'type', 'home' => 'type', 'work' => 'type', 'pref' => 'type', 'voice' => 'type', 'fax' => 'type', 'msg' => 'type', 'cell' => 'type', 'pager' => 'type', 'bbs' => 'type', 'modem' => 'type', 'car' => 'type', 'isdn' => 'type', 'video' => 'type', 'aol' => 'type', 'applelink' => 'type', 'attmail' => 'type', 'cis' => 'type', 'eworld' => 'type', 'internet' => 'type', 'ibmmail' => 'type', 'mcimail' => 'type', 'powershare' => 'type', 'prodigy' => 'type', 'tlx' => 'type', 'x400' => 'type', 'gif' => 'type', 'cgm' => 'type', 'wmf' => 'type', 'bmp' => 'type', 'met' => 'type', 'pmb' => 'type', 'dib' => 'type', 'pict' => 'type', 'tiff' => 'type', 'pdf' => 'type', 'ps' => 'type', 'jpeg' => 'type', 'qtime' => 'type', 'mpeg' => 'type', 'mpeg2' => 'type', 'avi' => 'type', 'wave' => 'type', 'aiff' => 'type', 'pcm' => 'type', 'x509' => 'type', 'pgp' => 'type', 'text' => 'value', 'inline' => 'value', 'url' => 'value', 'cid' => 'value', 'content-id' => 'value', '7bit' => 'encoding', '8bit' => 'encoding', 'quoted-printable' => 'encoding', 'base64' => 'encoding');
     // Parse the vcard
     $message = new SyncContact();
     $data = file_get_contents($this->getPath() . "/" . $id);
     $data = str_replace("", '', $data);
     $data = str_replace("\r\n", "\n", $data);
     $data = str_replace("\r", "\n", $data);
     $data = preg_replace('/(\\n)([ \\t])/i', '', $data);
     $lines = explode("\n", $data);
     $vcard = array();
     foreach ($lines as $line) {
         if (trim($line) == '') {
             continue;
         }
         $pos = strpos($line, ':');
         if ($pos === false) {
             continue;
         }
         $field = trim(substr($line, 0, $pos));
         $value = trim(substr($line, $pos + 1));
         $fieldparts = preg_split('/(?<!\\\\)(\\;)/i', $field, -1, PREG_SPLIT_NO_EMPTY);
         $type = strtolower(array_shift($fieldparts));
         $fieldvalue = array();
         foreach ($fieldparts as $fieldpart) {
             if (preg_match('/([^=]+)=(.+)/', $fieldpart, $matches)) {
                 if (!in_array(strtolower($matches[1]), array('value', 'type', 'encoding', 'language'))) {
                     continue;
                 }
                 if (isset($fieldvalue[strtolower($matches[1])]) && is_array($fieldvalue[strtolower($matches[1])])) {
                     $fieldvalue[strtolower($matches[1])] = array_merge($fieldvalue[strtolower($matches[1])], preg_split('/(?<!\\\\)(\\,)/i', $matches[2], -1, PREG_SPLIT_NO_EMPTY));
                 } else {
                     $fieldvalue[strtolower($matches[1])] = preg_split('/(?<!\\\\)(\\,)/i', $matches[2], -1, PREG_SPLIT_NO_EMPTY);
                 }
             } else {
                 if (!isset($types[strtolower($fieldpart)])) {
                     continue;
                 }
                 $fieldvalue[$types[strtolower($fieldpart)]][] = $fieldpart;
             }
         }
         //
         switch ($type) {
             case 'categories':
                 //case 'nickname':
                 $val = preg_split('/(?<!\\\\)(\\,)/i', $value);
                 $val = array_map("w2ui", $val);
                 break;
             default:
                 $val = preg_split('/(?<!\\\\)(\\;)/i', $value);
                 break;
         }
         if (isset($fieldvalue['encoding'][0])) {
             switch (strtolower($fieldvalue['encoding'][0])) {
                 case 'q':
                 case 'quoted-printable':
                     foreach ($val as $i => $v) {
                         $val[$i] = quoted_printable_decode($v);
                     }
                     break;
                 case 'b':
                 case 'base64':
                     foreach ($val as $i => $v) {
                         $val[$i] = base64_decode($v);
                     }
                     break;
             }
         } else {
             foreach ($val as $i => $v) {
                 $val[$i] = $this->unescape($v);
             }
         }
         $fieldvalue['val'] = $val;
         $vcard[$type][] = $fieldvalue;
     }
     if (isset($vcard['email'][0]['val'][0])) {
         $message->email1address = $vcard['email'][0]['val'][0];
     }
     if (isset($vcard['email'][1]['val'][0])) {
         $message->email2address = $vcard['email'][1]['val'][0];
     }
     if (isset($vcard['email'][2]['val'][0])) {
         $message->email3address = $vcard['email'][2]['val'][0];
     }
     if (isset($vcard['tel'])) {
         foreach ($vcard['tel'] as $tel) {
             if (!isset($tel['type'])) {
                 $tel['type'] = array();
             }
             if (in_array('car', $tel['type'])) {
                 $message->carphonenumber = $tel['val'][0];
             } elseif (in_array('pager', $tel['type'])) {
                 $message->pagernumber = $tel['val'][0];
             } elseif (in_array('cell', $tel['type'])) {
                 $message->mobilephonenumber = $tel['val'][0];
             } elseif (in_array('home', $tel['type'])) {
                 if (in_array('fax', $tel['type'])) {
                     $message->homefaxnumber = $tel['val'][0];
                 } elseif (empty($message->homephonenumber)) {
                     $message->homephonenumber = $tel['val'][0];
                 } else {
                     $message->home2phonenumber = $tel['val'][0];
                 }
             } elseif (in_array('work', $tel['type'])) {
                 if (in_array('fax', $tel['type'])) {
                     $message->businessfaxnumber = $tel['val'][0];
                 } elseif (empty($message->businessphonenumber)) {
                     $message->businessphonenumber = $tel['val'][0];
                 } else {
                     $message->business2phonenumber = $tel['val'][0];
                 }
             } elseif (empty($message->homephonenumber)) {
                 $message->homephonenumber = $tel['val'][0];
             } elseif (empty($message->home2phonenumber)) {
                 $message->home2phonenumber = $tel['val'][0];
             } else {
                 $message->radiophonenumber = $tel['val'][0];
             }
         }
     }
     //;;street;city;state;postalcode;country
     if (isset($vcard['adr'])) {
         foreach ($vcard['adr'] as $adr) {
             if (empty($adr['type'])) {
                 $a = 'other';
             } elseif (in_array('home', $adr['type'])) {
                 $a = 'home';
             } elseif (in_array('work', $adr['type'])) {
                 $a = 'business';
             } else {
                 $a = 'other';
             }
             if (!empty($adr['val'][2])) {
                 $b = $a . 'street';
                 $message->{$b} = w2ui($adr['val'][2]);
             }
             if (!empty($adr['val'][3])) {
                 $b = $a . 'city';
                 $message->{$b} = w2ui($adr['val'][3]);
             }
             if (!empty($adr['val'][4])) {
                 $b = $a . 'state';
                 $message->{$b} = w2ui($adr['val'][4]);
             }
             if (!empty($adr['val'][5])) {
                 $b = $a . 'postalcode';
                 $message->{$b} = w2ui($adr['val'][5]);
             }
             if (!empty($adr['val'][6])) {
                 $b = $a . 'country';
                 $message->{$b} = w2ui($adr['val'][6]);
             }
         }
     }
     if (!empty($vcard['fn'][0]['val'][0])) {
         $message->fileas = w2ui($vcard['fn'][0]['val'][0]);
     }
     if (!empty($vcard['n'][0]['val'][0])) {
         $message->lastname = w2ui($vcard['n'][0]['val'][0]);
     }
     if (!empty($vcard['n'][0]['val'][1])) {
         $message->firstname = w2ui($vcard['n'][0]['val'][1]);
     }
     if (!empty($vcard['n'][0]['val'][2])) {
         $message->middlename = w2ui($vcard['n'][0]['val'][2]);
     }
     if (!empty($vcard['n'][0]['val'][3])) {
         $message->title = w2ui($vcard['n'][0]['val'][3]);
     }
     if (!empty($vcard['n'][0]['val'][4])) {
         $message->suffix = w2ui($vcard['n'][0]['val'][4]);
     }
     if (!empty($vcard['bday'][0]['val'][0])) {
         $tz = date_default_timezone_get();
         date_default_timezone_set('UTC');
         $message->birthday = strtotime($vcard['bday'][0]['val'][0]);
         date_default_timezone_set($tz);
     }
     if (!empty($vcard['org'][0]['val'][0])) {
         $message->companyname = w2ui($vcard['org'][0]['val'][0]);
     }
     if (!empty($vcard['note'][0]['val'][0])) {
         $message->body = w2ui($vcard['note'][0]['val'][0]);
         $message->bodysize = strlen($vcard['note'][0]['val'][0]);
         $message->bodytruncated = 0;
     }
     if (!empty($vcard['role'][0]['val'][0])) {
         $message->jobtitle = w2ui($vcard['role'][0]['val'][0]);
     }
     //$vcard['title'][0]['val'][0]
     if (!empty($vcard['url'][0]['val'][0])) {
         $message->webpage = w2ui($vcard['url'][0]['val'][0]);
     }
     if (!empty($vcard['categories'][0]['val'])) {
         $message->categories = $vcard['categories'][0]['val'];
     }
     if (!empty($vcard['photo'][0]['val'][0])) {
         $message->picture = base64_encode($vcard['photo'][0]['val'][0]);
     }
     return $message;
 }
Example #2
0
/**
 * Read the correct message body 
 *
 * @param ressource $msg - the message
**/
function eml_ReadMessage($msg)
{
    global $protocolversion;
    $rtf = mapi_message_openproperty($msg, PR_RTF_COMPRESSED);
    if (!$rtf) {
        $body = mapi_message_openproperty($msg, PR_BODY);
        $content = "text/plain";
    } else {
        $rtf = preg_replace("/(\n.*)/m", "", mapi_decompressrtf($rtf));
        if (strpos($rtf, "\\fromtext") != false || !($protocolversion >= 2.5)) {
            $body = mapi_message_openproperty($msg, PR_BODY);
            $content = "text/plain";
        } else {
            $body = mapi_message_openproperty($msg, PR_HTML);
            $content = "text/html";
        }
    }
    if (mb_detect_encoding($body) != "UTF-8") {
        $body = w2ui($body);
    }
    return array('body' => $body, 'content' => $content);
}
 /**
  * Returns the actual SyncXXX object type.
  *
  * @param string            $folderid           id of the parent folder
  * @param string            $id                 id of the message
  * @param ContentParameters $contentparameters  parameters of the requested message (truncation, mimesupport etc)
  *
  * @access public
  * @return object/false     false if the message could not be retrieved
  */
 public function GetMessage($folderid, $id, $contentparameters)
 {
     ZLog::Write(LOGLEVEL_DEBUG, "VCDir::GetMessage({$folderid}, {$id}, ..)");
     if ($folderid !== 'root') {
         return;
     }
     $types = array('dom' => 'type', 'intl' => 'type', 'postal' => 'type', 'parcel' => 'type', 'home' => 'type', 'work' => 'type', 'pref' => 'type', 'voice' => 'type', 'fax' => 'type', 'msg' => 'type', 'cell' => 'type', 'pager' => 'type', 'bbs' => 'type', 'modem' => 'type', 'car' => 'type', 'isdn' => 'type', 'video' => 'type', 'aol' => 'type', 'applelink' => 'type', 'attmail' => 'type', 'cis' => 'type', 'eworld' => 'type', 'internet' => 'type', 'ibmmail' => 'type', 'mcimail' => 'type', 'powershare' => 'type', 'prodigy' => 'type', 'tlx' => 'type', 'x400' => 'type', 'gif' => 'type', 'cgm' => 'type', 'wmf' => 'type', 'bmp' => 'type', 'met' => 'type', 'pmb' => 'type', 'dib' => 'type', 'pict' => 'type', 'tiff' => 'type', 'pdf' => 'type', 'ps' => 'type', 'jpeg' => 'type', 'qtime' => 'type', 'mpeg' => 'type', 'mpeg2' => 'type', 'avi' => 'type', 'wave' => 'type', 'aiff' => 'type', 'pcm' => 'type', 'x509' => 'type', 'pgp' => 'type', 'text' => 'value', 'inline' => 'value', 'url' => 'value', 'cid' => 'value', 'content-id' => 'value', '7bit' => 'encoding', '8bit' => 'encoding', 'quoted-printable' => 'encoding', 'base64' => 'encoding');
     // Parse the vcard
     $message = new SyncContact();
     $query = "SELECT vcard, name, email, firstname, surname from contacts where user_id = '{$this->db_user_id}' and contact_id = '{$id}' and del = '0'";
     $result = mysql_query($query, $this->contactsdb) or print mysql_error($this->contactsdb);
     $results = 0;
     while ($result_rows = mysql_fetch_array($result)) {
         $data = $result_rows[0];
         $name = $result_rows[1];
         $email = $result_rows[2];
         $firstname = $result_rows[3];
         $surname = $result_rows[4];
         ++$results;
     }
     if ($results >= 1 and strlen($data) === 0) {
         $data = "BEGIN:VCARD\nVERSION:3.0\nN:{$surname};{$firstname};;;\nFN:{$name}\nEMAIL;TYPE=INTERNET:{$email}\nEND:VCARD";
     }
     $data = str_replace("", '', $data);
     $data = str_replace("\r\n", "\n", $data);
     $data = str_replace("\r", "\n", $data);
     $data = preg_replace('/(\\n)([ \\t])/i', '', $data);
     $lines = explode("\n", $data);
     $vcard = array();
     foreach ($lines as $line) {
         if (trim($line) === '') {
             continue;
         }
         $pos = strpos($line, ':');
         if ($pos === false) {
             continue;
         }
         $field = trim(substr($line, 0, $pos));
         $value = trim(substr($line, $pos + 1));
         $fieldparts = preg_split('/(?<!\\\\)(\\;)/i', $field, -1, PREG_SPLIT_NO_EMPTY);
         $type = strtolower(array_shift($fieldparts));
         $fieldvalue = array();
         foreach ($fieldparts as $fieldpart) {
             if (preg_match('/([^=]+)=(.+)/', $fieldpart, $matches)) {
                 if (!in_array(strtolower($matches[1]), array('value', 'type', 'encoding', 'language'))) {
                     continue;
                 }
                 if (isset($fieldvalue[strtolower($matches[1])]) and is_array($fieldvalue[strtolower($matches[1])])) {
                     $fieldvalue[strtolower($matches[1])] = array_merge($fieldvalue[strtolower($matches[1])], preg_split('/(?<!\\\\)(\\,)/i', $matches[2], -1, PREG_SPLIT_NO_EMPTY));
                 } else {
                     $fieldvalue[strtolower($matches[1])] = preg_split('/(?<!\\\\)(\\,)/i', $matches[2], -1, PREG_SPLIT_NO_EMPTY);
                 }
             } else {
                 if (!isset($types[strtolower($fieldpart)])) {
                     continue;
                 }
                 $fieldvalue[$types[strtolower($fieldpart)]][] = $fieldpart;
             }
         }
         //
         switch ($type) {
             case 'categories':
                 //case 'nickname':
                 $val = preg_split('/(?<!\\\\)(\\,)/i', $value);
                 $val = array_map('w2ui', $val);
                 break;
             default:
                 $val = preg_split('/(?<!\\\\)(\\;)/i', $value);
                 break;
         }
         if (isset($fieldvalue['encoding'][0])) {
             switch (strtolower($fieldvalue['encoding'][0])) {
                 case 'q':
                 case 'quoted-printable':
                     foreach ($val as $i => $v) {
                         $val[$i] = quoted_printable_decode($v);
                     }
                     break;
                 case 'b':
                 case 'base64':
                     foreach ($val as $i => $v) {
                         $val[$i] = base64_decode($v);
                     }
                     break;
             }
         } else {
             foreach ($val as $i => $v) {
                 $val[$i] = $this->unescape($v);
             }
         }
         $fieldvalue['val'] = $val;
         $vcard[$type][] = $fieldvalue;
     }
     if (isset($vcard['email'][0]['val'][0])) {
         $message->email1address = $vcard['email'][0]['val'][0];
     }
     if (isset($vcard['email'][1]['val'][0])) {
         $message->email2address = $vcard['email'][1]['val'][0];
     }
     if (isset($vcard['email'][2]['val'][0])) {
         $message->email3address = $vcard['email'][2]['val'][0];
     }
     // Calling array_map repeatedly throughout this clause is inefficient, but it works.
     // This code shouldn't get called all that often. Refactoring welcome.
     if (isset($vcard['tel'])) {
         foreach ($vcard['tel'] as $tel) {
             if (!isset($tel['type'])) {
                 $tel['type'] = array();
             }
             if (in_array('car', array_map('mb_strtolower', $tel['type']))) {
                 $message->carphonenumber = $tel['val'][0];
             } elseif (in_array('pager', array_map('mb_strtolower', $tel['type']))) {
                 $message->pagernumber = $tel['val'][0];
             } elseif (in_array('cell', array_map('mb_strtolower', $tel['type']))) {
                 $message->mobilephonenumber = $tel['val'][0];
             } elseif (in_array('home', array_map('mb_strtolower', $tel['type']))) {
                 if (in_array('fax', array_map('mb_strtolower', $tel['type']))) {
                     $message->homefaxnumber = $tel['val'][0];
                 } elseif (empty($message->homephonenumber)) {
                     $message->homephonenumber = $tel['val'][0];
                 } else {
                     $message->home2phonenumber = $tel['val'][0];
                 }
             } elseif (in_array('work', array_map('mb_strtolower', $tel['type']))) {
                 if (in_array('fax', array_map('mb_strtolower', $tel['type']))) {
                     $message->businessfaxnumber = $tel['val'][0];
                 } elseif (empty($message->businessphonenumber)) {
                     $message->businessphonenumber = $tel['val'][0];
                 } else {
                     $message->business2phonenumber = $tel['val'][0];
                 }
             } elseif (empty($message->homephonenumber)) {
                 $message->homephonenumber = $tel['val'][0];
             } elseif (empty($message->home2phonenumber)) {
                 $message->home2phonenumber = $tel['val'][0];
             } else {
                 $message->radiophonenumber = $tel['val'][0];
             }
         }
     }
     //;;street;city;state;postalcode;country
     if (isset($vcard['adr'])) {
         foreach ($vcard['adr'] as $adr) {
             if (empty($adr['type'])) {
                 $a = 'other';
             } elseif (in_array('home', $adr['type'])) {
                 $a = 'home';
             } elseif (in_array('work', $adr['type'])) {
                 $a = 'business';
             } else {
                 $a = 'other';
             }
             if (!empty($adr['val'][2])) {
                 $b = $a . 'street';
                 $message->{$b} = w2ui($adr['val'][2]);
             }
             if (!empty($adr['val'][3])) {
                 $b = $a . 'city';
                 $message->{$b} = w2ui($adr['val'][3]);
             }
             if (!empty($adr['val'][4])) {
                 $b = $a . 'state';
                 $message->{$b} = w2ui($adr['val'][4]);
             }
             if (!empty($adr['val'][5])) {
                 $b = $a . 'postalcode';
                 $message->{$b} = w2ui($adr['val'][5]);
             }
             if (!empty($adr['val'][6])) {
                 $b = $a . 'country';
                 $message->{$b} = w2ui($adr['val'][6]);
             }
         }
     }
     if (!empty($vcard['fn'][0]['val'][0])) {
         $message->fileas = w2ui($vcard['fn'][0]['val'][0]);
     }
     if (!empty($vcard['n'][0]['val'][0])) {
         $message->lastname = w2ui($vcard['n'][0]['val'][0]);
     }
     if (!empty($vcard['n'][0]['val'][1])) {
         $message->firstname = w2ui($vcard['n'][0]['val'][1]);
     }
     if (!empty($vcard['n'][0]['val'][2])) {
         $message->middlename = w2ui($vcard['n'][0]['val'][2]);
     }
     if (!empty($vcard['n'][0]['val'][3])) {
         $message->title = w2ui($vcard['n'][0]['val'][3]);
     }
     if (!empty($vcard['n'][0]['val'][4])) {
         $message->suffix = w2ui($vcard['n'][0]['val'][4]);
     }
     if (!empty($vcard['bday'][0]['val'][0])) {
         $tz = date_default_timezone_get();
         date_default_timezone_set('UTC');
         $message->birthday = strtotime($vcard['bday'][0]['val'][0]);
         date_default_timezone_set($tz);
     }
     if (!empty($vcard['org'][0]['val'][0])) {
         $message->companyname = w2ui($vcard['org'][0]['val'][0]);
     }
     if (!empty($vcard['note'][0]['val'][0])) {
         $message->body = w2ui($vcard['note'][0]['val'][0]);
         $message->bodysize = strlen($vcard['note'][0]['val'][0]);
         $message->bodytruncated = 0;
     }
     if (!empty($vcard['role'][0]['val'][0])) {
         $message->jobtitle = w2ui($vcard['role'][0]['val'][0]);
     }
     //$vcard['title'][0]['val'][0]
     if (!empty($vcard['url'][0]['val'][0])) {
         $message->webpage = w2ui($vcard['url'][0]['val'][0]);
     }
     if (!empty($vcard['categories'][0]['val'])) {
         $message->categories = $vcard['categories'][0]['val'];
     }
     if (!empty($vcard['photo'][0]['val'][0])) {
         $message->picture = base64_encode($vcard['photo'][0]['val'][0]);
     }
     return $message;
 }
Example #4
0
 function GetMessage($folderid, $id, $truncsize, $bodypreference = false, $optionbodypreference = false, $mimesupport = 0)
 {
     debugLog('GContacts::GetMessage(' . $folderid . ', ' . $this->_items[$id] . ', ..)');
     if ($folderid != "root") {
         return;
     }
     debugLog("GContacts::GetMessage: (fid: '{$folderid}'  id: '{$id}' )");
     // Parse the vcard
     $message = new SyncContact();
     try {
         // perform query and get feed of all results
         $query = new Zend_Gdata_Query(GCONTACTS_URL . '/' . $id);
         $entry = $this->service->getEntry($query);
     } catch (Zend_Gdata_App_Exception $e) {
         debugLog("GContacts::GetMessage - ERROR! (" . $e->getMessage() . ")");
         return false;
     }
     // parse feed and extract contact information
     // into simpler objects
     try {
         $doc = new DOMDocument();
         $doc->formatOutput = true;
         //$obj = new stdClass;
         //$obj->edit = $entry->getEditLink()->href;
         $xmldata = $entry->getXML();
         $doc->loadXML($xmldata);
         //filter out real contact id without other garbage
         preg_match("/[_a-z0-9]+\$/", $entry->id, $matches);
         $contactid = $matches[0];
         $fh = fopen(STATE_DIR . '/xml-get/' . (string) $entry->title . '_' . (string) $contactid . '.xml', 'w');
         fwrite($fh, $doc->saveXML());
         $xml = simplexml_load_string($xmldata);
         fclose($fh);
         //Prefix:
         //givenName:	 ok
         //Middle:
         //familyName:		ok
         //nameSuffix:	ok
         //last
         //first
         //middlename
         //title
         //suffix
         if (!empty($xml->name->fullName)) {
             debugLog('GContacts::GetMessage - fullName: ' . (string) $xml->name->fullName);
             $message->fileas = w2ui($xml->name->fullName);
         }
         //    if (!empty($xml->name->namePrefix)){
         //	debugLog('GContacts::GetMessage - namePrefix: '.(string) $xml->name->namePrefix);
         //	$message->middlename = w2ui($xml->name->namePrefix);
         //    }
         if (!empty($xml->name->givenName)) {
             debugLog('GContacts::GetMessage - givenName: ' . (string) $xml->name->givenName);
             $message->firstname = w2ui($xml->name->givenName);
         }
         //if (!empty($xml->name->????)){
         //	debugLog('GContacts::GetMessage - familyName: '.(string) $xml->name->????);
         //	$message->title = w2ui($xml->name->^^^^);
         //    }
         if (!empty($xml->name->familyName)) {
             debugLog('GContacts::GetMessage - familyName: ' . (string) $xml->name->familyName);
             $message->lastname = w2ui($xml->name->familyName);
         }
         if (!empty($xml->name->nameSuffix)) {
             debugLog('GContacts::GetMessage - nameSuffix: ' . (string) $xml->name->nameSuffix);
             $message->suffix = w2ui($xml->name->nameSuffix);
         }
         if (!empty($xml->organization->orgName)) {
             debugLog('GContacts::GetMessage - orgName: ' . (string) $xml->organization->orgName);
             $message->companyname = w2ui($xml->organization->orgName);
         }
         if (!empty($xml->organization->orgTitle)) {
             debugLog('GContacts::GetMessage - orgName: ' . (string) $xml->organization->orgTitle);
             $message->jobtitle = w2ui($xml->organization->orgTitle);
         }
         if (!empty($xml->nickname)) {
             debugLog('GContacts::GetMessage - Nickname: ' . (string) $xml->nickname);
             $message->nickname = w2ui($xml->nickname);
         }
         foreach ($xml->email as $e) {
             debugLog('GContacts::GetMessage - email: ' . (string) $e['address']);
             if (empty($message->email1address)) {
                 $message->email1address = w2ui($e['address']);
             } elseif (empty($message->email2address)) {
                 $message->email2address = w2ui($e['address']);
             } elseif (empty($message->email3address)) {
                 $message->email3address = w2ui($e['address']);
             } else {
                 debugLog('GContacts::GetMessage - LOST email address: ' . (string) $e['address']);
             }
         }
         foreach ($xml->im as $i) {
             debugLog('GContacts::GetMessage - im: ' . (string) $i['address']);
             if (empty($message->imaddress)) {
                 $message->imaddress = w2ui($i['address']);
             } elseif (empty($message->im2address)) {
                 $message->imaddress2 = w2ui($i['address']);
             } elseif (empty($message->imaddress3)) {
                 $message->imaddress3 = w2ui($i['address']);
             } else {
                 debugLog('GContacts::GetMessage - LOST im address: ' . (string) $i['address']);
             }
         }
         foreach ($xml->structuredPostalAddress as $p) {
             switch ($p['key']) {
                 case 'Children':
                     debugLog($p['key'] . ': ' . (string) $p['value']);
                     $message->children = w2ui($p['value']);
                     break;
                 case 'Spouse':
                     debugLog($p['key'] . ': ' . (string) $p['value']);
                     $message->spouse = w2ui($p['value']);
                     break;
             }
         }
         foreach ($xml->structuredPostalAddress as $p) {
             preg_match("/[_a-z0-9]+\$/", $p['rel'], $matches);
             $rel = $matches[0];
             switch ($rel) {
                 case 'home':
                     $a = 'home';
                     break;
                 case 'work':
                     break;
                 case 'office':
                     $a = 'business';
                     break;
                 case 'other':
                     $a = 'other';
                     break;
                 default:
                     debugLog('GContacts::GetMessage - structuredPostalAddress di tipo ' . $rel . ': non censito');
                     break;
             }
             if (!empty($p->street)) {
                 $b = $a . 'street';
                 debugLog($b . ': ' . (string) $p->street);
                 $message->{$b} = w2ui($p->street);
             }
             if (!empty($p->city)) {
                 $b = $a . 'city';
                 debugLog($b . ': ' . (string) $p->city);
                 $message->{$b} = w2ui($p->city);
             }
             if (!empty($p->postcode)) {
                 $b = $a . 'postalcode';
                 debugLog($b . ': ' . (string) $p->postcode);
                 $message->{$b} = w2ui($p->postcode);
             }
             if (!empty($p->region)) {
                 $b = $a . 'state';
                 debugLog($b . ': ' . (string) $p->region);
                 $message->{$b} = w2ui($p->region);
             }
             if (!empty($p->country)) {
                 $b = $a . 'country';
                 debugLog($b . ': ' . (string) $p->country);
                 $message->{$b} = w2ui($p->country);
             }
         }
         foreach ($xml->phoneNumber as $p) {
             preg_match("/[_a-z0-9]+\$/", $p['rel'], $matches);
             $rel = $matches[0];
             switch ($rel) {
                 case 'home':
                     if (empty($message->homephonenumber)) {
                         debugLog('GContacts::GetMessage - homephonenumber: ' . (string) $p);
                         $message->homephonenumber = w2ui($p);
                     } elseif (empty($message->home2phonenumber)) {
                         debugLog('GContacts::GetMessage - home2phonenumber: ' . (string) $p);
                         $message->home2phonenumber = w2ui($p);
                     } else {
                         debugLog('GContacts::GetMessage - LOST phone number: ' . (string) $p);
                     }
                     break;
                 case 'home_fax':
                     if (empty($message->homefaxnumber)) {
                         debugLog('GContacts::GetMessage - homefaxnumber: ' . (string) $p);
                         $message->homefaxnumber = w2ui($p);
                     } else {
                         debugLog('GContacts::GetMessage - LOST phone number: ' . (string) $p);
                     }
                     break;
                 case 'work_fax':
                     if (empty($message->businessfaxnumber)) {
                         debugLog('GContacts::GetMessage - businessfaxnumber: ' . (string) $p);
                         $message->businessfaxnumber = w2ui($p);
                     } else {
                         debugLog('GContacts::GetMessage - LOST phone number: ' . (string) $p);
                     }
                     break;
                 case 'mobile':
                     if (empty($message->mobilephonenumber)) {
                         debugLog('GContacts::GetMessage - mobilephonenumber: ' . (string) $p);
                         $message->mobilephonenumber = w2ui($p);
                     } elseif (empty($message->home2phonenumber)) {
                         debugLog('GContacts::GetMessage - home2phonenumber: ' . (string) $p);
                         $message->home2phonenumber = w2ui($p);
                     } elseif (empty($message->radiophonenumber)) {
                         debugLog('GContacts::GetMessage - radiophonenumber: ' . (string) $p);
                         $message->radiophonenumber = w2ui($p);
                     } else {
                         debugLog('GContacts::GetMessage - LOST phone number: ' . (string) $p);
                     }
                     break;
                 case 'work':
                     if (empty($message->businessphonenumber)) {
                         debugLog('GContacts::GetMessage - businessphonenumber: ' . (string) $p);
                         $message->businessphonenumber = w2ui($p);
                     } elseif (empty($message->business2phonenumber)) {
                         debugLog('GContacts::GetMessage - business2phonenumber: ' . (string) $p);
                         $message->business2phonenumber = w2ui($p);
                     } else {
                         debugLog('GContacts::GetMessage - LOST phone number: ' . (string) $p);
                     }
                     break;
                 case 'pager':
                     if (empty($message->pagernumber)) {
                         debugLog('GContacts::GetMessage - pagernumber: ' . (string) $p);
                         $message->pagernumber = w2ui($p);
                     } else {
                         debugLog('GContacts::GetMessage - LOST phone number: ' . (string) $p);
                     }
                     break;
                 case 'main':
                 case 'other':
                     if (empty($message->homephonenumber)) {
                         debugLog('GContacts::GetMessage - homephonenumber: ' . (string) $p);
                         $message->homephonenumber = w2ui($p);
                     } elseif (empty($message->home2phonenumber)) {
                         debugLog('GContacts::GetMessage - home2phonenumber: ' . (string) $p);
                         $message->home2phonenumber = w2ui($p);
                     } elseif (empty($message->businessphonenumber)) {
                         debugLog('GContacts::GetMessage - businessphonenumber: ' . (string) $p);
                         $message->businessphonenumber = w2ui($p);
                     } elseif (empty($message->business2phonenumber)) {
                         debugLog('GContacts::GetMessage - business2phonenumber: ' . (string) $p);
                         $message->business2phonenumber = w2ui($p);
                     } elseif (empty($message->carphonenumber)) {
                         debugLog('GContacts::GetMessage - carphonenumber: ' . (string) $p);
                         $message->carphonenumber = w2ui($p);
                     } else {
                         debugLog('GContacts::GetMessage - LOST phone number: ' . (string) $p);
                     }
                     break;
                 default:
                     debugLog('GContacts::GetMessage - LOST phone number: ' . (string) $p . ' phoneNumber di tipo ' . $rel . ': non censito');
                     break;
             }
         }
         if (!empty($xml->birthday['when'])) {
             debugLog('GContacts::GetMessage - birthday: ' . (string) $xml->birthday['when']);
             $tz = date_default_timezone_get();
             date_default_timezone_set('UTC');
             $message->birthday = strtotime($xml->birthday['when']);
             date_default_timezone_set($tz);
         }
         foreach ($xml->website as $w) {
             debugLog('GContacts::GetMessage - webpage: ' . (string) $w['href']);
             $message->webpage = w2ui($w['href']);
         }
         //$e["id"] = (string)$contactid;
         //$e["flags"] = "1";
         //$e["mod"] = strtotime((string)$entry->getUpdated());
         //$results[] = $e;
         //debugLog((string)$entry->getUpdated());
         if (!empty($entry->content)) {
             debugLog('GContacts::GetMessage - Note: ' . (string) $entry->content);
             if ($bodypreference === false) {
                 $message->body = w2ui($entry->content);
                 $message->bodysize = strlen($entry->content);
                 $message->bodytruncated = 0;
             } else {
                 if (isset($bodypreference[1]) && !isset($bodypreference[1]["TruncationSize"])) {
                     $bodypreference[1]["TruncationSize"] = 1024 * 1024;
                 }
                 if (isset($bodypreference[2]) && !isset($bodypreference[2]["TruncationSize"])) {
                     $bodypreference[2]["TruncationSize"] = 1024 * 1024;
                 }
                 if (isset($bodypreference[3]) && !isset($bodypreference[3]["TruncationSize"])) {
                     $bodypreference[3]["TruncationSize"] = 1024 * 1024;
                 }
                 if (isset($bodypreference[4]) && !isset($bodypreference[4]["TruncationSize"])) {
                     $bodypreference[4]["TruncationSize"] = 1024 * 1024;
                 }
                 $message->airsyncbasebody = new SyncAirSyncBaseBody();
                 debugLog("airsyncbasebody!");
                 $body = "";
                 $plain = $entry->content;
                 if (isset($bodypreference[2])) {
                     debugLog("HTML Body");
                     // Send HTML if requested and native type was html
                     $message->airsyncbasebody->type = 2;
                     $html = '<html>' . '<head>' . '<meta name="Generator" content="Z-Push">' . '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . '</head>' . '<body>' . str_replace("\n", "<BR>", str_replace("\r", "<BR>", str_replace("\r\n", "<BR>", w2u($plain)))) . '</body>' . '</html>';
                     if (isset($bodypreference[2]["TruncationSize"]) && strlen($html) > $bodypreference[2]["TruncationSize"]) {
                         $html = utf8_truncate($html, $bodypreference[2]["TruncationSize"]);
                         $message->airsyncbasebody->truncated = 1;
                     }
                     $message->airsyncbasebody->data = $html;
                     $message->airsyncbasebody->estimateddatasize = strlen($html);
                 } else {
                     // Send Plaintext as Fallback or if original body is plaintext
                     debugLog("Plaintext Body");
                     $plain = $entry->content;
                     $plain = w2u(str_replace("\n", "\r\n", str_replace("\r", "", $plain)));
                     $message->airsyncbasebody->type = 1;
                     if (isset($bodypreference[1]["TruncationSize"]) && strlen($plain) > $bodypreference[1]["TruncationSize"]) {
                         $plain = utf8_truncate($plain, $bodypreference[1]["TruncationSize"]);
                         $message->airsyncbasebody->truncated = 1;
                     }
                     $message->airsyncbasebody->estimateddatasize = strlen($plain);
                     $message->airsyncbasebody->data = $plain;
                 }
                 // In case we have nothing for the body, send at least a blank...
                 // dw2412 but only in case the body is not rtf!
                 if ($message->airsyncbasebody->type != 3 && (!isset($message->airsyncbasebody->data) || strlen($message->airsyncbasebody->data) == 0)) {
                     $message->airsyncbasebody->data = " ";
                 }
             }
         }
         if (!empty($vcard['categories'][0]['val'])) {
             $message->categories = $vcard['categories'][0]['val'];
         }
         if (!empty($vcard['photo'][0]['val'][0])) {
             $message->picture = base64_encode($vcard['photo'][0]['val'][0]);
         }
     } catch (Exception $e) {
         debugLog('Gcontacts::GetMessageList - Problem retrieving data (' . $e->exception() . ')');
         return false;
     }
     return $message;
     /*
     $types = array ('dom' => 'type', 'intl' => 'type', 'postal' => 'type', 'parcel' => 'type', 'home' => 'type', 'work' => 'type',
         'pref' => 'type', 'voice' => 'type', 'fax' => 'type', 'msg' => 'type', 'cell' => 'type', 'pager' => 'type',
         'bbs' => 'type', 'modem' => 'type', 'car' => 'type', 'isdn' => 'type', 'video' => 'type',
         'aol' => 'type', 'applelink' => 'type', 'attmail' => 'type', 'cis' => 'type', 'eworld' => 'type',
         'internet' => 'type', 'ibmmail' => 'type', 'mcimail' => 'type',
         'powershare' => 'type', 'prodigy' => 'type', 'tlx' => 'type', 'x400' => 'type',
         'gif' => 'type', 'cgm' => 'type', 'wmf' => 'type', 'bmp' => 'type', 'met' => 'type', 'pmb' => 'type', 'dib' => 'type',
         'pict' => 'type', 'tiff' => 'type', 'pdf' => 'type', 'ps' => 'type', 'jpeg' => 'type', 'qtime' => 'type',
         'mpeg' => 'type', 'mpeg2' => 'type', 'avi' => 'type',
         'wave' => 'type', 'aiff' => 'type', 'pcm' => 'type',
         'x509' => 'type', 'pgp' => 'type', 'text' => 'value', 'inline' => 'value', 'url' => 'value', 'cid' => 'value', 'content-id' => 'value',
         '7bit' => 'encoding', '8bit' => 'encoding', 'quoted-printable' => 'encoding', 'base64' => 'encoding',
     );
     
     
     // Parse the vcard
     $message = new SyncContact();
     
     $data = file_get_contents($this->_path . "/" . $this->_items[$id]);
     $data = str_replace("\x00", '', $data);
     $data = str_replace("\r\n", "\n", $data);
     $data = str_replace("\r", "\n", $data);
     $data = preg_replace('/(\n)([ \t])/i', '', $data);
     
     $lines = explode("\n", $data);
     
     $vcard = array();
     foreach($lines as $line) {
         if (trim($line) == '')
     	continue;
         $pos = strpos($line, ':');
         if ($pos === false)
     	continue;
     
         $field = trim(substr($line, 0, $pos));
         $value = trim(substr($line, $pos+1));
     
         $fieldparts = preg_split('/(?<!\\\\)(\;)/i', $field, -1, PREG_SPLIT_NO_EMPTY);
     
         $type = strtolower(array_shift($fieldparts));
     
         $fieldvalue = array();
     
         foreach ($fieldparts as $fieldpart) {
     	if(preg_match('/([^=]+)=(.+)/', $fieldpart, $matches)){
     	    if(!in_array(strtolower($matches[1]),array('value','type','encoding','language')))
     		continue;
     	    if(isset($fieldvalue[strtolower($matches[1])]) && is_array($fieldvalue[strtolower($matches[1])])){
     		$fieldvalue[strtolower($matches[1])] = array_merge($fieldvalue[strtolower($matches[1])], preg_split('/(?<!\\\\)(\,)/i', $matches[2], -1, PREG_SPLIT_NO_EMPTY));
     	    }else{
     		$fieldvalue[strtolower($matches[1])] = preg_split('/(?<!\\\\)(\,)/i', $matches[2], -1, PREG_SPLIT_NO_EMPTY);
     	    }
     	}else{
     	    if(!isset($types[strtolower($fieldpart)]))
     		continue;
     	    $fieldvalue[$types[strtolower($fieldpart)]][] = $fieldpart;
     	}
         }
         //
         switch ($type) {
     	case 'categories':
     	    //case 'nickname':
     	    $val = preg_split('/(?<!\\\\)(\,)/i', $value);
     	    $val = array_map("w2ui", $val);
     	    break;
     	default:
     	    $val = preg_split('/(?<!\\\\)(\;)/i', $value);
     	    break;
         }
         if(isset($fieldvalue['encoding'][0])){
     	switch(strtolower($fieldvalue['encoding'][0])){
     	    case 'q':
     	    case 'quoted-printable':
     		foreach($val as $i => $v){
     		    $val[$i] = quoted_printable_decode($v);
     		}
     		break;
     	    case 'b':
     	    case 'base64':
     		foreach($val as $i => $v){
     		    $val[$i] = base64_decode($v);
     		}
     		break;
     	}
         }else{
     	foreach($val as $i => $v){
     	    $val[$i] = $this->unescape($v);
     	}
         }
         $fieldvalue['val'] = $val;
         $vcard[$type][] = $fieldvalue;
     }
     */
     /*
     	if(isset($vcard['tel'])){
     	    foreach($vcard['tel'] as $tel) {
     		if(!isset($tel['type'])){
     		    $tel['type'] = array();
     		}
     		if(in_array('car', $tel['type'])){
     		    $message->carphonenumber = $tel['val'][0];
     		}elseif(in_array('pager', $tel['type'])){
     		    $message->pagernumber = $tel['val'][0];
     		}elseif(in_array('cell', $tel['type'])){
     		    $message->mobilephonenumber = $tel['val'][0];
     		}elseif(in_array('home', $tel['type'])){
     		    if(in_array('fax', $tel['type'])){
     			$message->homefaxnumber = $tel['val'][0];
     		    }elseif(empty($message->homephonenumber)){
     			$message->homephonenumber = $tel['val'][0];
     		    }else{
     			$message->home2phonenumber = $tel['val'][0];
     		    }
     		}elseif(in_array('work', $tel['type'])){
     		    if(in_array('fax', $tel['type'])){
     			$message->businessfaxnumber = $tel['val'][0];
     		    }elseif(empty($message->businessphonenumber)){
     			$message->businessphonenumber = $tel['val'][0];
     		    }else{
     			$message->business2phonenumber = $tel['val'][0];
     		    }
     		}elseif(empty($message->homephonenumber)){
     		    $message->homephonenumber = $tel['val'][0];
     		}elseif(empty($message->home2phonenumber)){
     		    $message->home2phonenumber = $tel['val'][0];
     		}else{
     		    $message->radiophonenumber = $tel['val'][0];
     		}
     	    }
     	}
     */
     if (!empty($vcard['categories'][0]['val'])) {
         $message->categories = $vcard['categories'][0]['val'];
     }
     if (!empty($vcard['photo'][0]['val'][0])) {
         $message->picture = base64_encode($vcard['photo'][0]['val'][0]);
     }
     return $message;
 }
Example #5
0
 function GetMessage($folderid, $id, $truncsize, $bodypreference = false, $optionbodypreference = false, $mimesupport = 0)
 {
     debugLog('VCDir::GetMessage(' . $folderid . ', ' . $this->_items[$id] . ', ..)');
     if ($folderid != "root") {
         return;
     }
     $types = array('dom' => 'type', 'intl' => 'type', 'postal' => 'type', 'parcel' => 'type', 'home' => 'type', 'work' => 'type', 'pref' => 'type', 'voice' => 'type', 'fax' => 'type', 'msg' => 'type', 'cell' => 'type', 'pager' => 'type', 'bbs' => 'type', 'modem' => 'type', 'car' => 'type', 'isdn' => 'type', 'video' => 'type', 'aol' => 'type', 'applelink' => 'type', 'attmail' => 'type', 'cis' => 'type', 'eworld' => 'type', 'internet' => 'type', 'ibmmail' => 'type', 'mcimail' => 'type', 'powershare' => 'type', 'prodigy' => 'type', 'tlx' => 'type', 'x400' => 'type', 'gif' => 'type', 'cgm' => 'type', 'wmf' => 'type', 'bmp' => 'type', 'met' => 'type', 'pmb' => 'type', 'dib' => 'type', 'pict' => 'type', 'tiff' => 'type', 'pdf' => 'type', 'ps' => 'type', 'jpeg' => 'type', 'qtime' => 'type', 'mpeg' => 'type', 'mpeg2' => 'type', 'avi' => 'type', 'wave' => 'type', 'aiff' => 'type', 'pcm' => 'type', 'x509' => 'type', 'pgp' => 'type', 'text' => 'value', 'inline' => 'value', 'url' => 'value', 'cid' => 'value', 'content-id' => 'value', '7bit' => 'encoding', '8bit' => 'encoding', 'quoted-printable' => 'encoding', 'base64' => 'encoding');
     // Parse the vcard
     $message = new SyncContact();
     $data = file_get_contents($this->_path . "/" . $this->_items[$id]);
     $data = str_replace("", '', $data);
     $data = str_replace("\r\n", "\n", $data);
     $data = str_replace("\r", "\n", $data);
     $data = preg_replace('/(\\n)([ \\t])/i', '', $data);
     $lines = explode("\n", $data);
     $vcard = array();
     foreach ($lines as $line) {
         if (trim($line) == '') {
             continue;
         }
         $pos = strpos($line, ':');
         if ($pos === false) {
             continue;
         }
         $field = trim(substr($line, 0, $pos));
         $value = trim(substr($line, $pos + 1));
         $fieldparts = preg_split('/(?<!\\\\)(\\;)/i', $field, -1, PREG_SPLIT_NO_EMPTY);
         $type = strtolower(array_shift($fieldparts));
         $fieldvalue = array();
         foreach ($fieldparts as $fieldpart) {
             if (preg_match('/([^=]+)=(.+)/', $fieldpart, $matches)) {
                 if (!in_array(strtolower($matches[1]), array('value', 'type', 'encoding', 'language'))) {
                     continue;
                 }
                 if (isset($fieldvalue[strtolower($matches[1])]) && is_array($fieldvalue[strtolower($matches[1])])) {
                     $fieldvalue[strtolower($matches[1])] = array_merge($fieldvalue[strtolower($matches[1])], preg_split('/(?<!\\\\)(\\,)/i', $matches[2], -1, PREG_SPLIT_NO_EMPTY));
                 } else {
                     $fieldvalue[strtolower($matches[1])] = preg_split('/(?<!\\\\)(\\,)/i', $matches[2], -1, PREG_SPLIT_NO_EMPTY);
                 }
             } else {
                 if (!isset($types[strtolower($fieldpart)])) {
                     continue;
                 }
                 $fieldvalue[$types[strtolower($fieldpart)]][] = $fieldpart;
             }
         }
         //
         switch ($type) {
             case 'categories':
                 //case 'nickname':
                 $val = preg_split('/(?<!\\\\)(\\,)/i', $value);
                 $val = array_map("w2ui", $val);
                 break;
             default:
                 $val = preg_split('/(?<!\\\\)(\\;)/i', $value);
                 break;
         }
         if (isset($fieldvalue['encoding'][0])) {
             switch (strtolower($fieldvalue['encoding'][0])) {
                 case 'q':
                 case 'quoted-printable':
                     foreach ($val as $i => $v) {
                         $val[$i] = quoted_printable_decode($v);
                     }
                     break;
                 case 'b':
                 case 'base64':
                     foreach ($val as $i => $v) {
                         $val[$i] = base64_decode($v);
                     }
                     break;
             }
         } else {
             foreach ($val as $i => $v) {
                 $val[$i] = $this->unescape($v);
             }
         }
         $fieldvalue['val'] = $val;
         $vcard[$type][] = $fieldvalue;
     }
     if (isset($vcard['email'][0]['val'][0])) {
         $message->email1address = $vcard['email'][0]['val'][0];
     }
     if (isset($vcard['email'][1]['val'][0])) {
         $message->email2address = $vcard['email'][1]['val'][0];
     }
     if (isset($vcard['email'][2]['val'][0])) {
         $message->email3address = $vcard['email'][2]['val'][0];
     }
     if (isset($vcard['tel'])) {
         foreach ($vcard['tel'] as $tel) {
             if (!isset($tel['type'])) {
                 $tel['type'] = array();
             }
             if (in_array('car', $tel['type'])) {
                 $message->carphonenumber = $tel['val'][0];
             } elseif (in_array('pager', $tel['type'])) {
                 $message->pagernumber = $tel['val'][0];
             } elseif (in_array('cell', $tel['type'])) {
                 $message->mobilephonenumber = $tel['val'][0];
             } elseif (in_array('home', $tel['type'])) {
                 if (in_array('fax', $tel['type'])) {
                     $message->homefaxnumber = $tel['val'][0];
                 } elseif (empty($message->homephonenumber)) {
                     $message->homephonenumber = $tel['val'][0];
                 } else {
                     $message->home2phonenumber = $tel['val'][0];
                 }
             } elseif (in_array('work', $tel['type'])) {
                 if (in_array('fax', $tel['type'])) {
                     $message->businessfaxnumber = $tel['val'][0];
                 } elseif (empty($message->businessphonenumber)) {
                     $message->businessphonenumber = $tel['val'][0];
                 } else {
                     $message->business2phonenumber = $tel['val'][0];
                 }
             } elseif (empty($message->homephonenumber)) {
                 $message->homephonenumber = $tel['val'][0];
             } elseif (empty($message->home2phonenumber)) {
                 $message->home2phonenumber = $tel['val'][0];
             } else {
                 $message->radiophonenumber = $tel['val'][0];
             }
         }
     }
     //;;street;city;state;postalcode;country
     if (isset($vcard['adr'])) {
         foreach ($vcard['adr'] as $adr) {
             if (empty($adr['type'])) {
                 $a = 'other';
             } elseif (in_array('home', $adr['type'])) {
                 $a = 'home';
             } elseif (in_array('work', $adr['type'])) {
                 $a = 'business';
             } else {
                 $a = 'other';
             }
             if (!empty($adr['val'][2])) {
                 $b = $a . 'street';
                 $message->{$b} = w2ui($adr['val'][2]);
             }
             if (!empty($adr['val'][3])) {
                 $b = $a . 'city';
                 $message->{$b} = w2ui($adr['val'][3]);
             }
             if (!empty($adr['val'][4])) {
                 $b = $a . 'state';
                 $message->{$b} = w2ui($adr['val'][4]);
             }
             if (!empty($adr['val'][5])) {
                 $b = $a . 'postalcode';
                 $message->{$b} = w2ui($adr['val'][5]);
             }
             if (!empty($adr['val'][6])) {
                 $b = $a . 'country';
                 $message->{$b} = w2ui($adr['val'][6]);
             }
         }
     }
     if (!empty($vcard['fn'][0]['val'][0])) {
         $message->fileas = w2ui($vcard['fn'][0]['val'][0]);
     }
     if (!empty($vcard['n'][0]['val'][0])) {
         $message->lastname = w2ui($vcard['n'][0]['val'][0]);
     }
     if (!empty($vcard['n'][0]['val'][1])) {
         $message->firstname = w2ui($vcard['n'][0]['val'][1]);
     }
     if (!empty($vcard['n'][0]['val'][2])) {
         $message->middlename = w2ui($vcard['n'][0]['val'][2]);
     }
     if (!empty($vcard['n'][0]['val'][3])) {
         $message->title = w2ui($vcard['n'][0]['val'][3]);
     }
     if (!empty($vcard['n'][0]['val'][4])) {
         $message->suffix = w2ui($vcard['n'][0]['val'][4]);
     }
     if (!empty($vcard['bday'][0]['val'][0])) {
         $tz = date_default_timezone_get();
         date_default_timezone_set('UTC');
         $message->birthday = strtotime($vcard['bday'][0]['val'][0]);
         date_default_timezone_set($tz);
     }
     if (!empty($vcard['org'][0]['val'][0])) {
         $message->companyname = w2ui($vcard['org'][0]['val'][0]);
     }
     if (!empty($vcard['note'][0]['val'][0])) {
         if ($bodypreference === false) {
             $message->body = w2ui($vcard['note'][0]['val'][0]);
             $message->bodysize = strlen($vcard['note'][0]['val'][0]);
             $message->bodytruncated = 0;
         } else {
             if (isset($bodypreference[1]) && !isset($bodypreference[1]["TruncationSize"])) {
                 $bodypreference[1]["TruncationSize"] = 1024 * 1024;
             }
             if (isset($bodypreference[2]) && !isset($bodypreference[2]["TruncationSize"])) {
                 $bodypreference[2]["TruncationSize"] = 1024 * 1024;
             }
             if (isset($bodypreference[3]) && !isset($bodypreference[3]["TruncationSize"])) {
                 $bodypreference[3]["TruncationSize"] = 1024 * 1024;
             }
             if (isset($bodypreference[4]) && !isset($bodypreference[4]["TruncationSize"])) {
                 $bodypreference[4]["TruncationSize"] = 1024 * 1024;
             }
             $message->airsyncbasebody = new SyncAirSyncBaseBody();
             debugLog("airsyncbasebody!");
             $body = "";
             $plain = $vcard['note'][0]['val'][0];
             if (isset($bodypreference[2])) {
                 debugLog("HTML Body");
                 // Send HTML if requested and native type was html
                 $message->airsyncbasebody->type = 2;
                 $html = '<html>' . '<head>' . '<meta name="Generator" content="Z-Push">' . '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . '</head>' . '<body>' . str_replace("\n", "<BR>", str_replace("\r", "<BR>", str_replace("\r\n", "<BR>", w2u($plain)))) . '</body>' . '</html>';
                 if (isset($bodypreference[2]["TruncationSize"]) && strlen($html) > $bodypreference[2]["TruncationSize"]) {
                     $html = utf8_truncate($html, $bodypreference[2]["TruncationSize"]);
                     $message->airsyncbasebody->truncated = 1;
                 }
                 $message->airsyncbasebody->data = $html;
                 $message->airsyncbasebody->estimateddatasize = strlen($html);
             } else {
                 // Send Plaintext as Fallback or if original body is plaintext
                 debugLog("Plaintext Body");
                 $plain = $vcard['note'][0]['val'][0];
                 $plain = w2u(str_replace("\n", "\r\n", str_replace("\r", "", $plain)));
                 $message->airsyncbasebody->type = 1;
                 if (isset($bodypreference[1]["TruncationSize"]) && strlen($plain) > $bodypreference[1]["TruncationSize"]) {
                     $plain = utf8_truncate($plain, $bodypreference[1]["TruncationSize"]);
                     $message->airsyncbasebody->truncated = 1;
                 }
                 $message->airsyncbasebody->estimateddatasize = strlen($plain);
                 $message->airsyncbasebody->data = $plain;
             }
             // In case we have nothing for the body, send at least a blank...
             // dw2412 but only in case the body is not rtf!
             if ($message->airsyncbasebody->type != 3 && (!isset($message->airsyncbasebody->data) || strlen($message->airsyncbasebody->data) == 0)) {
                 $message->airsyncbasebody->data = " ";
             }
         }
     }
     if (!empty($vcard['role'][0]['val'][0])) {
         $message->jobtitle = w2ui($vcard['role'][0]['val'][0]);
     }
     //$vcard['title'][0]['val'][0]
     if (!empty($vcard['url'][0]['val'][0])) {
         $message->webpage = w2ui($vcard['url'][0]['val'][0]);
     }
     if (!empty($vcard['categories'][0]['val'])) {
         $message->categories = $vcard['categories'][0]['val'];
     }
     if (!empty($vcard['photo'][0]['val'][0])) {
         $message->picture = base64_encode($vcard['photo'][0]['val'][0]);
     }
     return $message;
 }