/**
  * Encode a contact as vCard version 3.0.
  * @param Contact $contact
  * @param string $type 'WORK' (default) or 'HOME' is the default type for addresses and phonennumbers
  * @return string vCard
  * @global $CONFIG_TAB_ROOT, $country
  */
 function vCardExport($contact, $defaultType = 'WORK')
 {
     global $CONFIG_TAB_ROOT, $country;
     /* SEE: http://vcardmaker.wackomenace.co.uk/ (incorrectly escapes ':'!!), http://tools.ietf.org/html/rfc2426
      * Please post improvements to: http://sourceforge.net/tracker/?group_id=172286&atid=861164 as attachment!!
      * Or to the developer forums at: http://sourceforge.net/forum/forum.php?forum_id=590644
      */
     $type = $defaultType;
     $output = '';
     if ($contact->contact['nickname']) {
         $output .= 'NICKNAME:' . $contact->contact['nickname'] . "\n";
     }
     //if($contact->contact['birthday'] != '0000-00-00') $output .= 'BDAY:' . ?? . "\n";
     $vg = $contact->getValueGroup('addresses');
     foreach ($vg as $adr) {
         $lbl = strtolower($adr['type']);
         if (substr($lbl, 0, 4) == 'home' || substr($lbl, 0, 7) == 'private') {
             // \TODO TRANSLATION
             $type = 'HOME';
         } else {
             $type = $defaultType;
         }
         // reset
         $output .= 'ADR;TYPE=DOM,' . $type . ',POSTAL:;' . $adr['line2'] . ';' . $adr['line1'] . ';' . $adr['city'] . ';' . $adr['state'] . ';' . $adr['zip'] . ';' . $country[$adr['country']] . "\n";
         // \TODO modifier ,PREF before : for primary
         if ($adr['phone1']) {
             $output .= 'TEL;TYPE=' . $type . ',VOICE:' . $adr['phone1'] . "\n";
         }
         if ($adr['phone2']) {
             $output .= 'TEL;TYPE=' . $type . ',VOICE:' . $adr['phone2'] . "\n";
         }
     }
     $type = $defaultType;
     // reset
     /* tel-type     = "HOME" / "WORK" / "PREF" / "VOICE" / "FAX" / "MSG"
        / "CELL" / "PAGER" / "BBS" / "MODEM" / "CAR" / "ISDN"
        / "VIDEO" / "PCS" / iana-token / x-name */
     $vg = $contact->getValueGroup('phone');
     foreach ($vg as $v) {
         if ($v['visibility'] != 'visible') {
             continue;
         }
         $t = $type;
         $n = $v['value'];
         $number = $n;
         $service = $v['label'];
         if (substr($service, 0, 4) == 'sips') {
             $t = 'PAGER';
             $number = ContactImportExport::vCardEscape("sips:{$n}");
         } else {
             // See: http://www.voip-info.org/wiki/view/SIP+URI
             if (substr($service, 0, 3) == 'sip' || substr($service, 0, 4) == 'voip') {
                 $t = 'PAGER';
                 // there is no SIP defined in vCard
                 $number = ContactImportExport::vCardEscape("sip:{$n}");
             } else {
                 if (substr($service, 0, 3) == 'fax') {
                     $t = 'FAX';
                 } else {
                     if (substr($service, 0, 4) == 'cell' || substr($service, 0, 6) == 'mobile') {
                         $t = 'CELL';
                     } else {
                         if (substr($service, 0, 5) == 'video') {
                             $t = 'VIDEO';
                         }
                     }
                 }
             }
         }
         $output .= 'TEL;TYPE=' . $t . ':' . ContactImportExport::vCardEscape($number) . "\n";
     }
     $department = '';
     $prefixes = ';' . $contact->contact['namePrefix'];
     $suffixes = ';' . $contact->contact['nameSuffix'];
     // CORRECT vCARD version does not work with M$: invisible postfix title
     //$suffixes = ',' . $contact->contact['nameSuffix']; // OUTLOOK version (this joins $prefixes and $postfixes)
     $vg = $contact->getValueGroup('other');
     foreach ($vg as $v) {
         if ($v['visibility'] != 'visible') {
             continue;
         }
         $n = ContactImportExport::vCardEscape($v['value']);
         $l = strtolower($v['label']);
         if ($l == 'job title' || $l == 'occupation') {
             $output .= 'TITLE:' . $n . "\n";
         } else {
             if ($l == 'function' || $l == 'role') {
                 $output .= 'ROLE:' . $n . "\n";
             } else {
                 if ($l == 'department') {
                     $department = ";{$n}";
                 } else {
                     if ($l == 'academic title') {
                         if ($n == 'BS' || $n == 'BA' || $n == 'MS' || $n == 'MA' || $n == 'MD' || $n == 'MBA' || $n == 'PhD') {
                             $suffixes .= strlen($prefixes) <= 1 ? $n : ",{$n}";
                         } else {
                             $prefixes .= strlen($prefixes) <= 1 ? $n : ",{$n}";
                         }
                     }
                 }
             }
         }
     }
     $output .= 'ORG:' . $contact->groups(null, false, 'groupname', false) . $department . "\n";
     $vg = $contact->getValueGroup('email');
     foreach ($vg as $v) {
         if ($v['visibility'] == 'visible') {
             $output .= 'EMAIL;TYPE=INTERNET,' . $type . ':' . $v['value'] . "\n";
         }
     }
     // could have ,PRIM modifier before :
     $vg = $contact->getValueGroup('www');
     foreach ($vg as $v) {
         if ($v['visibility'] == 'visible') {
             $output .= 'URL:' . ContactImportExport::vCardEscape($v['value']) . "\n";
         }
     }
     // URI pointing to this TAB entry
     $output .= 'URL:' . ContactImportExport::vCardEscape($CONFIG_TAB_ROOT . 'contact/contact.php?id=' . $_GET['id']) . "\n";
     // Attach picture base64
     if (!empty($contact->contact['pictureData'])) {
         $output .= 'PHOTO;ENCODING=BASE64;TYPE=JPEG:' . base64_encode($contact->contact['pictureData']) . "\n";
     }
     // Attach picture URL
     if (!empty($contact->contact['pictureURL'])) {
         $output .= 'PHOTO;VALUE=URL:' . ContactImportExport::vCardEscape($contact->contact['pictureURL']) . "\n";
     }
     $output .= "END:VCARD\n";
     $output .= "\n";
     $head = "BEGIN:VCARD\nVERSION:3.0\n";
     $head .= 'FN:' . $contact->contact['namePrefix'] . ' ' . $contact->contact['firstname'] . ' ' . $contact->contact['lastname'] . ' ' . $contact->contact['nameSuffix'] . "\n";
     $head .= 'N:' . $contact->contact['lastname'] . ';' . $contact->contact['firstname'] . ';' . $contact->contact['middlename'] . $prefixes . $suffixes . ";\n";
     return mb_convert_encoding($head . $output, 'ISO-8859-1');
 }