egroupware_official/addressbook/import/Import_from_VCard
2001-03-21 00:12:39 +00:00

462 lines
15 KiB
Plaintext

<?php
// This file defines a set of functions and an associative array.
// The key of the array corresponds to a header in the source
// import file and the value of the array item will be used in
// the creation of the output file.
//
// An exported Outlook file looks like this:
//
// Title<tab>First Name<tab>Middle Name<tab>Last Name<tab>...
// <tab>Patrick<tab><tab>Walsh<tab>...
//
// Where the first line explains each optional field. This is what
// will be looked up in the key.
//
// The array need not be in any order and any fields not defined will
// not be transferred. If the val='+', the value will be appended to
// the previous field and any text after the '+' will be appended
// before the value. For example, the following would add a comma and
// a space between LastName and FirstName and store it in FullName:
//
// array("LastName" => "FullName","FirstName" => "+, ");
//
// Also start with a '#' symbol and a comma separated list will be
// turned into a number of the same entries.
class import_conv
{
var $currentrecord = array(); //used for buffering to allow uid lines to go first
var $id;
var $type = 'vcard';
var $import = array(
"n" => "n",
"sound" => "sound",
"bday" => "bday",
"note" => "note",
"tz" => "tz",
"geo" => "geo",
"url" => "url",
"pubkey" => "pubkey",
"org" => "org",
"title" => "title",
"adr" => "adr",
"label" => "label",
"tel" => "tel",
"email" => "email"
);
var $grouping = array("A\.","B\.","C\.","D\.");
var $names = array(
"family" => "family",
"given" => "given",
"middle" => "middle",
"prefix" => "prefix",
"suffix" => "suffix"
);
var $adr_types = array(
"DOM" => "dom",
"INTL" => "intl",
"PARCEL" => "parcel",
"POSTAL" => "postal",
"WORK" => "one",
"HOME" => "two"
);
// Used to set preferred number field
var $tel_types = array(
"PREF" => "tel_prefer",
"WORK" => "tel_work",
"HOME" => "tel_home",
"VOICE" => "tel_voice",
"FAX" => "tel_fax",
"MSG" => "tel_msg",
"CELL" => "tel_cell",
"PAGER" => "tel_pager",
"BBS" => "tel_bbs",
"MODEM" => "tel_modem",
"CAR" => "tel_car",
"ISDN" => "tel_isdn",
"VIDEO" => "tel_video"
);
function import_start_file($buffer,$j="",$k="") {
$this->id = 0;
return $buffer;
}
function import_start_record($buffer) {
++$this->id;
$this->currentrecord = array();
return $buffer;
}
function import_new_attrib($buffer,$name,$value) {
$value = trim($value);
$value = ereg_replace("=0D=0A","\n",$value);
//echo '<br>'.$this->id.": ".$name.' => '.$value;
$this->currentrecord += array($name => $value);
return $buffer;
}
function import_end_record($buffer,$private="private") {
global $phpgw_info;
$buffer[$this->id]="";
while ( list($name, $value) = each($this->currentrecord)) {
$buffer[$this->id][$name] = $value;
//$buffer[$this->id]["private"] = $private;
//echo '<br>'.$name.' => '.$value;
}
return $buffer;
}
function import_end_file($buffer) {
global $phpgw,$phpgw_info;
$contacts = CreateObject("phpgwapi.contacts");
echo '<br>';
for ($i=1;$i<=count($buffer);$i++) {
while ( list($name,$value) = @each($buffer[$i]) ) {
$field = split(";",$name);
reset($this->grouping);
$field[0] = ereg_replace("A\.","",$field[0]);
$field[0] = ereg_replace("B\.","",$field[0]);
$field[0] = ereg_replace("C\.","",$field[0]);
$field[0] = ereg_replace("D\.","",$field[0]);
$values = split(";",$value);
if ($field[1]) {
//echo $field[0];
switch ($field[0]) {
case "LABEL":
$buffer[$i]["label"] = ereg_replace("=0D=0A","\n",$values[0]);
break;
case "NOTE":
$buffer[$i]["note"] = ereg_replace("=0D=0A","\n",$values[0]);
break;
case "ADR":
switch ($field[1]) {
case "INTL":
switch ($field[2]) {
case "WORK":
if ( !stristr($buffer[$i]["adr_one_type"],$field[1])) {
$buffer[$i]["adr_one_type"] .= "intl;";
}
if (!$buffer[$i]["adr_one_street"]) {
$buffer[$i]["address2"] = $values[1];
$buffer[$i]["adr_one_street"] = $values[2];
$buffer[$i]["adr_one_locality"] = $values[3];
$buffer[$i]["adr_one_region"] = $values[4];
$buffer[$i]["adr_one_postalcode"] = $values[5];
$buffer[$i]["adr_one_countryname"] = $values[6];
}
break;
case "HOME":
if ( !stristr($buffer[$i]["adr_two_type"],$field[1]) ) {
$buffer[$i]["adr_two_type"] .= "intl;";
}
if (!$buffer[$i]["adr_two_street"]) {
$buffer[$i]["adr_two_street"] = $values[2];
$buffer[$i]["adr_two_locality"] = $values[3];
$buffer[$i]["adr_two_region"] = $values[4];
$buffer[$i]["adr_two_postalcode"] = $values[5];
$buffer[$i]["adr_two_countryname"] = $values[6];
}
break;
default:
break;
}
break;
case "DOM":
switch ($field[2]) {
case "WORK":
if ( !stristr($buffer[$i]["adr_one_type"],$field[1])) {
$buffer[$i]["adr_one_type"] .= "dom;";
}
if (!$buffer[$i]["adr_one_street"]) {
$buffer[$i]["address2"] = $values[1];
$buffer[$i]["adr_one_street"] = $values[2];
$buffer[$i]["adr_one_locality"] = $values[3];
$buffer[$i]["adr_one_region"] = $values[4];
$buffer[$i]["adr_one_postalcode"] = $values[5];
$buffer[$i]["adr_one_countryname"] = $values[6];
}
break;
case "HOME":
if ( !stristr($buffer[$i]["adr_two_type"],$field[1]) ) {
$buffer[$i]["adr_two_type"] .= "dom;";
}
if (!$buffer[$i]["adr_two_street"]) {
$buffer[$i]["adr_two_street"] = $values[2];
$buffer[$i]["adr_two_locality"] = $values[3];
$buffer[$i]["adr_two_region"] = $values[4];
$buffer[$i]["adr_two_postalcode"] = $values[5];
$buffer[$i]["adr_two_countryname"] = $values[6];
}
break;
default:
break;
}
break;
case "PARCEL":
switch ($field[2]) {
case "WORK":
if ( !stristr($buffer[$i]["adr_one_type"],$field[1])) {
$buffer[$i]["adr_one_type"] .= "parcel;";
}
if (!$buffer[$i]["adr_one_street"]) {
$buffer[$i]["address2"] = $values[1];
$buffer[$i]["adr_one_street"] = $values[2];
$buffer[$i]["adr_one_locality"] = $values[3];
$buffer[$i]["adr_one_region"] = $values[4];
$buffer[$i]["adr_one_postalcode"] = $values[5];
$buffer[$i]["adr_one_countryname"] = $values[6];
}
break;
case "HOME":
if ( !stristr($buffer[$i]["adr_two_type"],$field[1]) ) {
$buffer[$i]["adr_two_type"] .= "parcel;";
}
if (!$buffer[$i]["adr_two_street"]) {
$buffer[$i]["adr_two_street"] = $values[2];
$buffer[$i]["adr_two_locality"] = $values[3];
$buffer[$i]["adr_two_region"] = $values[4];
$buffer[$i]["adr_two_postalcode"] = $values[5];
$buffer[$i]["adr_two_countryname"] = $values[6];
}
break;
default:
break;
}
break;
case "POSTAL":
switch ($field[2]) {
case "WORK":
if ( !stristr($buffer[$i]["adr_one_type"],$field[1])) {
$buffer[$i]["adr_one_type"] .= "postal;";
}
if (!$buffer[$i]["adr_one_street"]) {
$buffer[$i]["address2"] = $values[1];
$buffer[$i]["adr_one_street"] = $values[2];
$buffer[$i]["adr_one_locality"] = $values[3];
$buffer[$i]["adr_one_region"] = $values[4];
$buffer[$i]["adr_one_postalcode"] = $values[5];
$buffer[$i]["adr_one_countryname"] = $values[6];
}
break;
case "HOME":
if ( !stristr($buffer[$i]["adr_two_type"],$field[1]) ) {
$buffer[$i]["adr_two_type"] .= "postal;";
}
if (!$buffer[$i]["adr_two_street"]) {
$buffer[$i]["adr_two_street"] = $values[2];
$buffer[$i]["adr_two_locality"] = $values[3];
$buffer[$i]["adr_two_region"] = $values[4];
$buffer[$i]["adr_two_postalcode"] = $values[5];
$buffer[$i]["adr_two_countryname"] = $values[6];
}
break;
default:
break;
}
break;
case "WORK":
if (!$buffer[$i]["adr_one_street"]) {
$buffer[$i]["address2"] = $values[1];
$buffer[$i]["adr_one_street"] = $values[2];
$buffer[$i]["adr_one_locality"] = $values[3];
$buffer[$i]["adr_one_region"] = $values[4];
$buffer[$i]["adr_one_postalcode"] = $values[5];
$buffer[$i]["adr_one_countryname"] = $values[6];
}
break;
case "HOME":
$buffer[$i]["adr_two_street"] = $values[2];
$buffer[$i]["adr_two_locality"] = $values[3];
$buffer[$i]["adr_two_region"] = $values[4];
$buffer[$i]["adr_two_postalcode"] = $values[5];
$buffer[$i]["adr_two_countryname"] = $values[6];
break;
default:
if (!$buffer[$i]["adr_one_street"]) {
$buffer[$i]["address2"] = $values[1];
$buffer[$i]["adr_one_street"] = $values[2];
$buffer[$i]["adr_one_locality"] = $values[3];
$buffer[$i]["adr_one_region"] = $values[4];
$buffer[$i]["adr_one_postalcode"] = $values[5];
$buffer[$i]["adr_one_countryname"] = $values[6];
}
break;
}
break;
case "TEL":
switch ($field[1]) {
case "PREF":
//echo $field[2]." is preferred";
if ($field[2]) {
$buffer[$i]["tel_prefer"] .= strtolower($field[2]) . ";";
}
break;
case "WORK":
$buffer[$i]["tel_work"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "HOME":
$buffer[$i]["tel_home"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "VOICE":
$buffer[$i]["tel_voice"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "FAX":
$buffer[$i]["tel_fax"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "MSG":
$buffer[$i]["tel_msg"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "CELL":
$buffer[$i]["tel_cell"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "PAGER":
$buffer[$i]["tel_pager"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "BBS":
$buffer[$i]["tel_bbs"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "MODEM":
$buffer[$i]["tel_modem"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "CAR":
$buffer[$i]["tel_car"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "ISDN":
$buffer[$i]["tel_isdn"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
case "VIDEO":
$buffer[$i]["tel_video"] = $values[0];
if ($field[2] == "PREF") {
$buffer[$i]["tel_prefer"] .= strtolower($field[1]) . ";";
}
break;
default:
break;
}
break;
case "EMAIL":
switch ($field[1]) {
case "WORK":
$buffer[$i]["email"] = $values[0];
$buffer[$i]["email_type"] = $field[2];
break;
case "HOME":
$buffer[$i]["email_home"] = $values[0];
$buffer[$i]["email_home_type"] = $field[2];
break;
default:
if($buffer[$i]["email"]) {
$buffer[$i]["email_type"] = $field[2];
} elseif (!$buffer[$i]["email"]) {
$buffer[$i]["email"] = $values[0];
$buffer[$i]["email_type"] = $field[1];
}
break;
}
default:
break;
}
} else {
switch ($field[0]) {
case "N":
reset($this->names);
$j=0;
while(list($myname,$myval) = each($this->names) ) {
$namel = "n_".$myname;
$buffer[$i][$namel] = $values[$j];
$j++;
}
break;
case "FN":
$buffer[$i]["fn"] = $values[0];
break;
case "TITLE":
$buffer[$i]["title"] = $values[0];
break;
case "TZ":
$buffer[$i]["tz"] = $values[0];
break;
case "GEO":
$buffer[$i]["geo"] = $values[0];
break;
case "URL":
$buffer[$i]["url"] = $values[0];
break;
case "NOTE":
$buffer[$i]["note"] = ereg_replace("=0D=0A","\n",$values[0]);
break;
case "KEY":
$buffer[$i]["key"] = ereg_replace("=0D=0A","\n",$values[0]);
break;
case "LABEL":
$buffer[$i]["label"] = ereg_replace("=0D=0A","\n",$values[0]);
break;
case "BDAY": #1969-12-31
$tmp = split("-",$values[0]);
if ($tmp[0]) {
$buffer[$i]["bday"] = $tmp[1]."/".$tmp[2]."/".$tmp[0];
}
break;
}
}
}
$buffer[$i]["tel_prefer"] = substr($buffer[$i]["tel_prefer"],0,-1);
$buffer[$i]["adr_one_type"] = substr($buffer[$i]["adr_one_type"],0,-1);
$buffer[$i]["adr_two_type"] = substr($buffer[$i]["adr_two_type"],0,-1);
/*
echo '<br>';
reset($contacts->stock_contact_fields);
while (list($fname,$fvalue) = each($contacts->stock_contact_fields)) {
echo '<br>'.$i.': '.$fname.' => '.$buffer[$i][$fvalue];
}
echo '<br>';
*/
$contacts->add($phpgw_info["user"]["account_id"],$buffer[$i]);
}
$num = $i - 1;
return "Successfully imported $num records into your addressbook.";
}
}
?>