Sincronizzare la rubrica di Thunderbird con i contatti di Google
Avete un account Google e volete sempre tenere sincronizzati i contatti presenti in esso con quelli della vostra rubrica di Thunderbird ?
Ecco un modo per andare ad aggiornare i contatti, usando come base proprio la rubrica di Thunderbird.
Tool necessari:
- add-on Thundersync
- script php di conversione
Per prima cosa dovete installare l'addon Thundersync e configurarlo in modo che il formato esportazione sia "vCard v2.1 (*.vcf, single file)", ricordandovi anche di indicare il nome del file dove dovrà essere salvata l'esportazione della rubrica.
Fatto questo dovete selezionare la vostra rubrica e avviare il processo di esportazione (verrà generato un file nella posizione da voi indicata in fase di configurazione dell'addon).
Una volta pronto il file accedete via web ai contatti del vostro account Google ed esportateli in formato csv, copiando il file scaricato nella stessa directory dove è presente quanto generato da Thundersync.
Eseguiti i passi sopra indicati, create nella directory sopra citata e create il file tb2google.php , al cui interno dovete inserire il seguente codice:
<?php$file = __DIR__ . '/' . 'google.csv';// <-- nome file con i contatti google $google = trim(file_get_contents($file)); $google = str_replace("\r","\n",$google); $google = str_replace("\n\n","\n",$google); $google = explode("\n",$google); $file = __DIR__ . '/' . 'rubrica.vcf'; // <-- nome file della rubrica $rubrica = trim(file_get_contents($file)); $rubrica = str_replace("\r","\n",$rubrica); $rubrica = str_replace("\n\n","\n",$rubrica); $rubrica = explode("\n",$rubrica); $google_header = $google[0]; $google_header = preg_replace("#[^a-zA-Z0-9\ \-_,.\@]#","",$google_header); $google_header = explode(',',$google_header); $google_line = explode(',',',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'); // print_r($google_header); function getPosition($field) { $field = strtolower($field); static $cache = array(); global $google_header; if (isset($cache[$field])) { return $cache[$field]; } foreach ($google_header as $k=>$x) { if (strtolower($x)==$field) { $cache[$field] = $k; return $k; } } return -1; } function telType($tel,$emailArray) { $tel = str_replace('+39','',$tel); $tel = trim($tel); $tel = str_replace(' ','',$tel); if (substr($tel,0,1)=='3') { return 'Mobile'; } return 'Home'; } function arrTelUnique($tel) { $ret = array(); foreach ($tel as $t) { // echo $t."\n"; $t = str_replace(' ','',trim($t)); if (substr($t,0,1)!='+') { $t = '+39'.$t; } $ret[] = $t; } $ret = array_values(array_unique($ret)); return $ret; } $email = $tel = $teltype = array(); $gname = getPosition('Name'); $gemail[0] = getPosition('E-mail 1 - Value'); $gemail[1] = getPosition('E-mail 2 - Value'); $gemail[2] = getPosition('E-mail 3 - Value'); $gtel[0] = getPosition('Phone 1 - Value'); $gtel[1] = getPosition('Phone 2 - Value'); $gtel[2] = getPosition('Phone 3 - Value'); $gteltype[0] = getPosition('Phone 1 - Type'); $gteltype[1] = getPosition('Phone 2 - Type'); $gteltype[2] = getPosition('Phone 3 - Type'); $gazienda = getPosition('Organization 1 - Name'); $gia_presenti_email = array(); $gia_presenti_tel = array(); /* $tmp = array(); $tmp[] = $google[0]; $tmp[] = $google[60]; $google = $tmp; // */ foreach ($google as $k=>$dati) { $dati = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $dati); if ($k == 0 || $dati == '') { $google[$k] = $dati; continue; } $dati = explode(',',$dati); $google[$k] = $dati; $_email1 = strval(@$dati[$gemail[0]]); $_email2 = strval(@$dati[$gemail[1]]); $_email3 = strval(@$dati[$gemail[2]]); $_email1 = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $_email1); $_email2 = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $_email2); $_email3 = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $_email3); $_tel1 = strval(@$dati[$gtel[0]]); $_tel2 = strval(@$dati[$gtel[1]]); $_tel3 = strval(@$dati[$gtel[2]]); $_tel1 = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $_tel1); $_tel2 = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $_tel2); $_tel3 = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $_tel3); if ($_email1!='') { $gia_presenti_email[trim(strtolower($_email1))] = $k; } if ($_email2!='') { $gia_presenti_email[trim(strtolower($_email2))] = $k; } if ($_email3!='') { $gia_presenti_email[trim(strtolower($_email3))] = $k; } if ($_tel1!='') { $gia_presenti_tel[$_tel1] = $k; $gia_presenti_tel[trim(str_replace(" ","",$_tel1))] = $k; } if ($_tel2!='') { $gia_presenti_tel[$_tel2] = $k; $gia_presenti_tel[trim(str_replace(" ","",$_tel2))] = $k; } if ($_tel3!='') { $gia_presenti_tel[$_tel3] = $k; $gia_presenti_tel[trim(str_replace(" ","",$_tel3))] = $k; } } foreach ($rubrica as $riga) { $riga = trim($riga); if ($riga == '') { continue; } if (preg_match('#^BEGIN:VCARD#',$riga)) { $tel = array(); $email = array(); $nome = ''; $org = ''; } if (preg_match('#^TEL;#',$riga)) { $tel[] = preg_replace('#^.*:#','',$riga); $tel[] = str_replace(' ','',preg_replace('#^.*:#','',$riga)); } if (preg_match('#^EMAIL;#',$riga)) { $email[] = strtolower(preg_replace('#^.*:#','',$riga)); } if (preg_match('#^FN;#',$riga)) { $nome = preg_replace('#^.*:#','',$riga); } if (preg_match('#^ORG;#',$riga)) { $org = preg_replace('#^.*:#','',$riga); $org = preg_replace('#;.*$#','',$org); } if (preg_match('#^END:VCARD#',$riga)) { $index = -1; foreach ($email as $_m) { $_m = trim(strtolower($_m)); if (isset($gia_presenti_email[$_m])) { $index = $gia_presenti_email[$_m]; break; } } if ($index < 0) { foreach ($tel as $_t) { $_t = trim(strtolower($_t)); if (isset($gia_presenti_tel[$_t])) { $index = $gia_presenti_tel[$_t]; break; } } } if ($index >= 0) { } else { $index = count($google); $google[$index] = $google_line; } $tel = arrTelUnique($tel); $email = array_unique($email); foreach ($tel as $k=>$telefono) { if (!isset($gtel[$k])) { break; } $google[intval($index)][intval($gtel[$k])] = $telefono; $google[intval($index)][intval($gteltype[$k])] = telType($telefono,$email); } foreach ($email as $k=>$emailx) { $google[intval($index)][intval($gemail[$k])] = $emailx; } $google[intval($index)][intval($gazienda)] = $org; $google[intval($index)][intval($gname)] = $nome; } } foreach ($google as $k=>$s) { if (is_array($s)) { foreach ($s as $key=>$val) { $val = str_replace(","," ",$val); $val = str_replace("'"," ",$val); $val = str_replace("\""," ",$val); $s[$key] = $val; } $s = implode(",",$s); $google[$k] = $s; } } $google = implode("\n",$google); $google = str_replace("\r","\n",$google); $google = str_replace("\n\n","\n",$google); file_put_contents( __DIR__ . '/' . 'google-aggiornato.csv', $google ); ?>
Una volta pronto il file php avviate php tb2google.php e questo andrà a creare il file google-aggiornato.csv, che potrà essere importato nei contatti di Google (ricordatevi di effettuare alla fine di tutta la procedura di "trova duplicati e fondi contatti" sul sito).