function buildCompanyData($position, $checked, $fields) {
		$contact_data = array();
		if (isset($checked['first_name']) && $checked['first_name']) $contact_data['first_name'] = array_var($fields, $position['first_name']);
		if (isset($checked['email']) && $checked['email']) $contact_data['email'] = array_var($fields, $position['email']);
		if (isset($checked['homepage']) && $checked['homepage']) $contact_data['homepage'] = array_var($fields, $position['homepage']);
		if (isset($checked['address']) && $checked['address']) $contact_data['address'] = array_var($fields, $position['address']);
		if (isset($checked['address2']) && $checked['address2']) $contact_data['address2'] = array_var($fields, $position['address2']);
		if (isset($checked['city']) && $checked['city']) $contact_data['city'] = array_var($fields, $position['city']);
		if (isset($checked['state']) && $checked['state']) $contact_data['state'] = array_var($fields, $position['state']);
		if (isset($checked['zipcode']) && $checked['zipcode']) $contact_data['zipcode'] = array_var($fields, $position['zipcode']);
		if (isset($checked['country']) && $checked['country']) $contact_data['country'] = CountryCodes::getCountryCodeByName(array_var($fields, $position['country']));
		if (isset($checked['phone_number']) && $checked['phone_number']) $contact_data['phone_number'] = array_var($fields, $position['phone_number']);
		if (isset($checked['fax_number']) && $checked['fax_number']) $contact_data['fax_number'] = array_var($fields, $position['fax_number']);
		if (isset($checked['notes']) && $checked['notes']) $contact_data['notes'] = array_var($fields, $position['notes']);
		$contact_data['timezone'] = logged_user()->getTimezone();
		
		return $contact_data;
	}
 private function read_vcard_file($filename, $only_first_record = false)
 {
     $handle = fopen($filename, 'rb');
     if (!$handle) {
         flash_error(lang('file not exists'));
         ajx_current("empty");
         return;
     }
     // parse VCard blocks
     $in_block = true;
     $results = array();
     while (($line = fgets($handle)) !== false) {
         if (preg_match('/^BEGIN:VCARD/', $line)) {
             // START OF CONTACT
             $in_block = true;
             $block_data = array();
         } else {
             if (preg_match('/^END:VCARD/', $line)) {
                 // END OF CONTACT
                 $in_block = false;
                 if (isset($photo_data)) {
                     if (isset($photo_data)) {
                         $filename = ROOT . "/tmp/" . rand() . ".{$photo_type}";
                         $f_handle = fopen($filename, "wb");
                         fwrite($f_handle, base64_decode($photo_data));
                         fclose($f_handle);
                         $block_data['photo_tmp_filename'] = $filename;
                     }
                 }
                 unset($photo_data);
                 unset($photo_enc);
                 unset($photo_type);
                 $results[] = $block_data;
                 if ($only_first_record && count($results) > 0) {
                     return $results;
                 }
             } else {
                 if (preg_match('/^N(:|;charset=[-a-zA-Z0-9.]+:)([^;]*);([^;]*)/i', $line, $matches)) {
                     // NAME
                     $block_data["firstname"] = trim($matches[count($matches) - 1]);
                     $block_data["lastname"] = trim($matches[count($matches) - 2]);
                 } else {
                     if (preg_match('/^ORG(:|;charset=[-a-zA-Z0-9.]+:)([^;]*)/i', $line, $matches)) {
                         // ORGANIZATION
                         $block_data["company_name"] = str_replace(array("\r\n", "\n", "\r", "\t", '\\r\\n', '\\n', '\\r', '\\t'), " ", trim($matches[2]));
                     } else {
                         if (preg_match('/^NOTE(:|;charset=[-a-zA-Z0-9.]+:)([^;]*)/i', $line, $matches)) {
                             // NOTES
                             $block_data["notes"] = trim($matches[count($matches) - 1]);
                         } else {
                             if (preg_match('/EMAIL;type=(PREF,)?INTERNET(,PREF)?(;type=(HOME|WORK))?(;type=PREF)?:([-a-zA-Z0-9_.]+@[-a-zA-Z0-9.]+)/i', $line, $matches)) {
                                 // EMAIL
                                 $email = trim($matches[count($matches) - 1]);
                                 if (!isset($block_data["email"])) {
                                     $block_data["email"] = $email;
                                 } else {
                                     if (!isset($block_data["email2"])) {
                                         $block_data["email2"] = $email;
                                     } else {
                                         if (!isset($block_data["email3"])) {
                                             $block_data["email3"] = $email;
                                         }
                                     }
                                 }
                             } else {
                                 if (preg_match('/URL(;type=(HOME|WORK))?.*?:(.+)/i', $line, $matches)) {
                                     // WEB URL
                                     $url = trim($matches[3]);
                                     $url = str_replace(array("\r\n", "\n", "\r", "\t", '\\r\\n', '\\n', '\\r', '\\t'), " ", $url);
                                     if ($matches[2] == "HOME") {
                                         $block_data['h_web_page'] = $url;
                                     } else {
                                         if ($matches[2] == "WORK") {
                                             $block_data['w_web_page'] = $url;
                                         } else {
                                             $block_data['o_web_page'] = $url;
                                         }
                                     }
                                 } else {
                                     if (preg_match('/TEL(;type=(HOME|WORK|CELL|FAX)[,A-Z]*)?.*:(.+)/i', $line, $matches)) {
                                         // PHONE
                                         $phone = trim($matches[3]);
                                         $phone = str_replace(array("\r\n", "\n", "\r", "\t", '\\r\\n', '\\n', '\\r', '\\t'), " ", $phone);
                                         if ($matches[2] == "HOME") {
                                             $block_data["h_phone_number"] = $phone;
                                         } else {
                                             if ($matches[2] == "CELL") {
                                                 $block_data["h_mobile_number"] = $phone;
                                             } else {
                                                 if ($matches[2] == "WORK") {
                                                     $block_data["w_phone_number"] = $phone;
                                                 } else {
                                                     if ($matches[2] == "FAX") {
                                                         $block_data["w_fax_number"] = $phone;
                                                     } else {
                                                         $block_data["o_phone_number"] = $phone;
                                                     }
                                                 }
                                             }
                                         }
                                     } else {
                                         if (preg_match('/ADR;type=(HOME|WORK|[A-Z0-9]*)[,A-Z]*(:|;charset=[-a-zA-Z0-9.]+:|;type=pref:);;([^;]*);([^;]*);([^;]*);([^;]*);([^;]*)/i', $line, $matches)) {
                                             // ADDRESS
                                             // $matches is
                                             // [1] <-- street
                                             // [2] <-- city
                                             // [3] <-- state
                                             // [4] <-- zip
                                             // [5] <-- country
                                             $addr = array_slice($matches, count($matches) - 5);
                                             foreach ($addr as $k => $v) {
                                                 $addr[$k] = str_replace(array("\r\n", "\n", "\r", "\t", '\\r\\n', '\\n', '\\r', '\\t'), " ", trim($v));
                                             }
                                             if ($matches[1] == "HOME") {
                                                 $block_data["h_address"] = $addr[0];
                                                 $block_data["h_city"] = $addr[1];
                                                 $block_data["h_state"] = $addr[2];
                                                 $block_data["h_zipcode"] = $addr[3];
                                                 $block_data["h_country"] = CountryCodes::getCountryCodeByName($addr[4]);
                                             } else {
                                                 if ($matches[1] == "WORK") {
                                                     $block_data["w_address"] = $addr[0];
                                                     $block_data["w_city"] = $addr[1];
                                                     $block_data["w_state"] = $addr[2];
                                                     $block_data["w_zipcode"] = $addr[3];
                                                     $block_data["w_country"] = CountryCodes::getCountryCodeByName($addr[4]);
                                                 } else {
                                                     $block_data["o_address"] = $addr[0];
                                                     $block_data["o_city"] = $addr[1];
                                                     $block_data["o_state"] = $addr[2];
                                                     $block_data["o_zipcode"] = $addr[3];
                                                     $block_data["o_country"] = CountryCodes::getCountryCodeByName($addr[4]);
                                                 }
                                             }
                                         } else {
                                             if (preg_match('/^BDAY[;value=date]*:([0-9]+)-([0-9]+)-([0-9]+)/i', $line, $matches)) {
                                                 // BIRTHDAY
                                                 // $matches[1]  <-- year     $matches[2]  <-- month    $matches[3]  <-- day
                                                 $block_data["o_birthday"] = $matches[1] . '-' . $matches[2] . '-' . $matches[3] . "00:00:00";
                                             } else {
                                                 if (preg_match('/TITLE(:|;charset=[-a-zA-Z0-9.]+:)(.*)/i', $line, $matches)) {
                                                     // JOB TITLE
                                                     $block_data["job_title"] = str_replace(array("\r\n", "\n", "\r", "\t", '\\r\\n', '\\n', '\\r', '\\t'), " ", trim($matches[2]));
                                                 } else {
                                                     if (preg_match('/PHOTO(;ENCODING=(b|BASE64)?(;TYPE=([-a-zA-Z.]+))|;VALUE=uri):(.*)/i', $line, $matches)) {
                                                         foreach ($matches as $k => $v) {
                                                             if (str_starts_with(strtoupper($v), ";ENCODING")) {
                                                                 $enc_idx = $k + 1;
                                                             }
                                                             if (str_starts_with(strtoupper($v), ";TYPE")) {
                                                                 $type_idx = $k + 1;
                                                             }
                                                             if (str_starts_with(strtoupper($v), ";VALUE=uri")) {
                                                                 $uri_idx = $k + 1;
                                                             }
                                                         }
                                                         if (isset($enc_idx) && isset($type_idx)) {
                                                             $photo_enc = $matches[$enc_idx];
                                                             $photo_type = $matches[$type_idx];
                                                             $photo_data = str_replace(array("\r\n", "\n", "\r", "\t"), "", trim($matches[count($matches) - 1]));
                                                         } else {
                                                             if (isset($uri_idx)) {
                                                                 $uri = trim($matches[count($matches) - 1]);
                                                                 $photo_type = substr($uri, strrpos($uri, "."));
                                                                 $data = file_get_contents(urldecode($uri));
                                                                 $filename = ROOT . "/tmp/" . rand() . ".{$photo_type}";
                                                                 $f_handle = fopen($filename, "wb");
                                                                 fwrite($f_handle, $data);
                                                                 fclose($f_handle);
                                                                 $block_data['photo_tmp_filename'] = $filename;
                                                             }
                                                         }
                                                     } else {
                                                         if (isset($photo_data) && isset($enc_idx) && isset($type_idx)) {
                                                             $photo_data .= str_replace(array("\r\n", "\n", "\r", "\t"), "", trim($line));
                                                         }
                                                         // unknown / ignored VCard field
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         unset($matches);
     }
     fclose($handle);
     return $results;
 }