Skip to content

Instantly share code, notes, and snippets.

@nansenat16
Created December 23, 2011 10:21
Show Gist options
  • Save nansenat16/1513806 to your computer and use it in GitHub Desktop.
Save nansenat16/1513806 to your computer and use it in GitHub Desktop.
convert outlook express BIG5 csv to RoundCube support vCard file
<?php
//
// http://homepage.mac.com/phrogz/CSV2vCard_v2.html
// http://en.wikipedia.org/wiki/VCard
// Test CentOS 5.7 PHP 5.3.3
// 2011/12/23 by nansen.su
// 2012/02/16 support Thunderbird 10
$field=array(
array('名稱','prefix'),
array('電子郵件地址','email'),
array('住家所在街道','street_addr_home'),
array('住家所在縣/市','city_home'),
array('住家所在郵遞區號','zip_home'),
array('住家所在省/市','state_home'),
array('住家所在國家/地區','country_home'),
array('住家電話','tel_home'),
array('公司所在街道','street_addr_office'),
array('公司所在縣/市','city_office'),
array('公司所在郵遞區號','zip_office'),
array('公司所在省/市','state_office'),
array('公司所在國家/地區','country_office'),
array('公司電話','tel_office'),
array('公司','company'),
array('職稱','job_title'),
//
array('名字','last_name'),
array('姓氏','first_name'),
array('中間名','middle_name'),
array('暱稱','suffix'),
array('住家傳真','fax_home'),
array('行動電話','phone_cell'),
array('個人網頁','web_personal'),
array('公司網頁','web_office'),
array('公司傳真','fax_office'),
array('呼叫器','pager'),
//array('部門','office'),
//array('辦公室位置','office_loc'),
array('備註','note'),
//Thunderbird 10
array('名','last_name'),
array('姓','first_name'),
array('顯示名稱','prefix'),
array('主要 Email','email'),
array('商務電話','tel_office'),
array('傳真號碼','fax_office'),
array('呼叫器號碼','pager'),
array('手機號碼','phone_cell'),
array('住家住址','street_addr_home'),
//array('住家住址 2',''),
array('居住縣市','city_home'),
array('居住市鎮','state_home'),
array('住址郵遞區號','zip_home'),
array('居住國家','country_home'),
array('商務地址','street_addr_office'),
//array('商務地址 2',''),
array('商務縣市','city_office'),
array('商務省市','state_office'),
array('商務郵遞區號','zip_office'),
array('商務國家','country_office'),
array('網頁 1','web_office'),
array('註解','note'),
);
function vc($item){
$str="BEGIN:VCARD\nVERSION:3.0\n";
$str.='N:'.$item['last_name'].';'.$item['first_name'].';'.$item['middle_name'].';'.$item['prefix'].';'.$item['suffix']."\n";
$str.='FN:'.$item['first_name'].' '.$item['last_name'].' '.$item['prefix']."\n";
if(isset($item['email'])){
$str.='EMAIL;TYPE=INTERNET:'.$item['email']."\n";
}else{
$str.='EMAIL;TYPE=INTERNET:user@no.email.address'."\n";
}
if(isset($item['company'])){
$str.='ORG:'.$item['company']."\n";
}
if(isset($item['job_title'])){
$str.='TITLE:'.$item['job_title']."\n";
}
if(isset($item['tel_home'])){
$str.='TEL;TYPE=HOME,VOICE:'.$item['tel_home']."\n";
}
if(isset($item['tel_office'])){
$str.='TEL;TYPE=WORD,VOICE:'.$item['tel_office']."\n";
}
if(isset($item['fax_home'])){
$str.='TEL;TYPE=FAX,HOME:'.$item['fax_home']."\n";
}
if(isset($item['fax_office'])){
$str.='TEL;TYPE=FAX,WORK:'.$item['fax_office']."\n";
}
if(isset($item['phone_cell'])){
$str.='TEL;TYPE=CELL:'.$item['phone_cell']."\n";
}
if(isset($item['web_office'])){
$str.='URL;TYPE=WORK:'.$item['web_office']."\n";
}
if(isset($item['web_personal'])){
$str.='URL;TYPE=HOME:'.$item['web_personal']."\n";
}
if(isset($item['street_addr_home'])||
isset($item['city_home'])||
isset($item['zip_home'])||
isset($item['state_home'])||
isset($item['country_home'])){
$str.='ADR;TYPE=HOME:;;'.$item['street_addr_home'].';'.$item['city_home'].';'.$item['state_home'].';'.$item['zip_home'].';'.$item['country_home']."\n";
}
if(isset($item['street_addr_office'])||
isset($item['city_office'])||
isset($item['zip_office'])||
isset($item['state_office'])||
isset($item['country_office'])){
$str.='ADR;TYPE=WORK:;;'.$item['street_addr_office'].';'.$item['city_office'].';'.$item['state_office'].';'.$item['zip_office'].';'.$item['country_office']."\n";
}
if(isset($item['note'])){
$str.='NOTE:'.$item['note']."\n";
}
$str.="END:VCARD\n";
return $str;
}
setlocale(LC_ALL, 'zh_TW.UTF-8');
if(array_key_exists('upload',$_GET)){
$tmp=$_FILES['csv']['tmp_name'];
$f=fopen($tmp,'r');
$tmp=fread($f,filesize($tmp));
fclose($f);
$tmp_utf8=iconv('big5','utf8',$tmp);
$tmp_file=tempnam('/tmp','csv2vcf');
$new=fopen($tmp_file,'w');
fwrite($new,$tmp_utf8);
fclose($new);
$f=fopen($tmp_file,'r');
$csv=array();
while(($data=fgetcsv($f))!==false){
$csv[]=$data;
}
fclose($f);
unlink($tmp_file);
$map=array();
for($n=0;$n<count($csv[0]);$n++){
//echo $csv[0][$n]."\n";
for($m=0;$m<count($field);$m++){
if($field[$m][0]==$csv[0][$n]){
$map[$n]=$field[$m][1];
}
}
}
$data=array();
$keep=array('last_name','first_name','middle_name','suffix','prefix');
for($n=1;$n<count($csv);$n++){
$tmp_data=array();
//print_r($csv[$n]);
for($m=0;$m<count($csv[$n]);$m++){
if(isset($map[$m])){
if(in_array($map[$m],$keep)||$csv[$n][$m]!=''){
$tmp_data[$map[$m]]=$csv[$n][$m];
}
}
}
$data[]=$tmp_data;
}
//print_r($map);
//print_r($data);
//exit();
$str_vc='';
for($n=0;$n<count($data);$n++){
$str_vc.=vc($data[$n]);
}
header('Content-type:application/vcf-file');
header('Content-Disposition: attachment; filename='.date('Ymd-His').'.vcf');
echo $str_vc;
exit();
}else{
?>
<html>
<head>
<title>CSV to VCF</title>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
</head>
<body>
<h1>CSV to VCF Tool</h1>
<form action="?upload" method="post" enctype="multipart/form-data">
<input name="csv" type="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment