From b5cd836f7f4c735ee6a57902940875fcb8509cd4 Mon Sep 17 00:00:00 2001 From: Miles Lott Date: Sun, 11 Mar 2001 17:34:58 +0000 Subject: [PATCH] Working LDIF import --- addressbook/conv/Debug LDAP | 1 + addressbook/conv/Debug SQL | 1 + addressbook/conv/Import from Netscape | 99 +++++++++++++++++++ ...ort to Addressbook => Import from Outlook} | 1 + addressbook/import.php | 49 ++++++--- addressbook/templates/default/import.tpl | 7 +- 6 files changed, 142 insertions(+), 16 deletions(-) create mode 100644 addressbook/conv/Import from Netscape rename addressbook/conv/{Import to Addressbook => Import from Outlook} (99%) diff --git a/addressbook/conv/Debug LDAP b/addressbook/conv/Debug LDAP index da163df541..11d344be6f 100644 --- a/addressbook/conv/Debug LDAP +++ b/addressbook/conv/Debug LDAP @@ -25,6 +25,7 @@ class import_conv { + var $type = ''; var $basedn; var $contactsdn; diff --git a/addressbook/conv/Debug SQL b/addressbook/conv/Debug SQL index 1f03bad42f..b633b19f78 100644 --- a/addressbook/conv/Debug SQL +++ b/addressbook/conv/Debug SQL @@ -25,6 +25,7 @@ class import_conv { + var $type = ''; var $currentrecord; //used for buffering to allow uid lines to go first var $import = array( diff --git a/addressbook/conv/Import from Netscape b/addressbook/conv/Import from Netscape new file mode 100644 index 0000000000..d177b9db40 --- /dev/null +++ b/addressbook/conv/Import from Netscape @@ -0,0 +1,99 @@ + "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 = 'ldif'; + + var $import = array( + "title" => "title", + "givenname" => "n_given", + "sn" => "n_family", + "cn" => "fn", + "o" => "org_name", + "ou" => "org_unit", + "streetaddress" => "adr_street", + "locality" => "adr_locality", + "st" => "adr_region", + "postalcode" => "adr_postalcode", + "countryname" => "adr_countryname", + "telephonenumber" => "a_tel", + "homephone" => "b_tel", + "facsimiletelephonenumber" => "c_tel", + "xmozillaanyphone" => "ophone", + "cellphone" => "mphone", + "description" => "note", + "pagerphone" => "pager", + "mail" => "d_email", + "homeurl" => "url", + "xmozillauseconferenceserver" => "", + "xmozillanickname" => "", + "xmozillausehtmlmail" => "", + "modifytimestamp" => "", + "objectclass" => "" + ); + + function import_start_file($buffer,$j="",$k="") { + return $buffer; + } + + function import_end_file($buffer) { + global $phpgw,$phpgw_info; + + $contacts = CreateObject("phpgwapi.contacts"); + echo '
'; + for ($i=1;$i<=count($buffer);$i++) { + while ( list($name,$value) = @each($buffer[$i]) ) { + //echo '
'.$i.': '.$name.' => '.$value; + $entry[$i][$name] = $value; + } + //echo '
'; + $contacts->add($phpgw_info["user"]["account_id"],$entry[$i]); + } + $num = $i - 1; + return "Successfully imported $num records into your addressbook."; + } + + function import_start_record($buffer) { + $top=array(); + ++$this->id; + $this->currentrecord = $top; + 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; + //echo '
'.$this->id.': '.$name.' => '.$value; + } + return $buffer; + } + + function import_new_attrib($buffer,$name,$value) { + //$value = str_replace("\n","
",$value); + $value = str_replace("\r","",$value); + //echo '
'.$name.' => '.$value; + $this->currentrecord += array($name => $value); + + return $buffer; + } + } +?> diff --git a/addressbook/conv/Import to Addressbook b/addressbook/conv/Import from Outlook similarity index 99% rename from addressbook/conv/Import to Addressbook rename to addressbook/conv/Import from Outlook index c43e558010..244c90689e 100644 --- a/addressbook/conv/Import to Addressbook +++ b/addressbook/conv/Import from Outlook @@ -27,6 +27,7 @@ { var $currentrecord = array(); //used for buffering to allow uid lines to go first var $id; + var $type = 'csv'; var $import = array( "Title" => "title", diff --git a/addressbook/import.php b/addressbook/import.php index 173f9e5c7b..4fa1524961 100644 --- a/addressbook/import.php +++ b/addressbook/import.php @@ -53,11 +53,12 @@ $t->set_var("cancel_url",$phpgw->link("/addressbook/index.php")); $t->set_var("navbar_bg",$phpgw_info["theme"]["navbar_bg"]); $t->set_var("navbar_text",$phpgw_info["theme"]["navbar_text"]); - $t->set_var("import_text",lang("Import from Outlook")); + $t->set_var("import_text",lang("Import from Outlook (CSV) or Netscape (LDIF)")); $t->set_var("action_url",$phpgw->link("/addressbook/import.php")); $t->set_var("tsvfilename",""); $t->set_var("conv",$conv); $t->set_var("debug",lang("Debug output in browser")); + $t->set_var("filetype",lang("LDIF")); $t->set_var("basedn",$basedn); $t->set_var("context",$context); $t->set_var("download",lang("Submit")); @@ -73,20 +74,42 @@ $o = new import_conv; $buffer = $o->import_start_file($buffer,$basedn,$context); $fp=fopen($tsvfile,"r"); - while ($data = fgetcsv($fp,8000,",")) { - $num = count($data); - $row++; - if ($row == 1) { - $header = $data; - } else { - $buffer = $o->import_start_record($buffer); - for ($c=0; $c<$num; $c++ ) { - //Send name/value pairs along with the buffer - if ($o->import[$header[$c]]!="" && $data[$c]!="") { - $buffer = $o->import_new_attrib($buffer, $o->import[$header[$c]],$data[$c]); + if ($o->type != 'ldif') { + while ($data = fgetcsv($fp,8000,",")) { + $num = count($data); + $row++; + if ($row == 1) { + $header = $data; + } else { + $buffer = $o->import_start_record($buffer); + for ($c=0; $c<$num; $c++ ) { + //Send name/value pairs along with the buffer + if ($o->import[$header[$c]]!="" && $data[$c]!="") { + $buffer = $o->import_new_attrib($buffer, $o->import[$header[$c]],$data[$c]); + } } + $buffer = $o->import_end_record($buffer,$private); + } + } + } else { + while ($data = fgets($fp,8000)) { + list($name,$value) = split(':', $data); + if (substr($name,0,2) == 'dn') { + $buffer = $o->import_start_record($buffer); + } + if ($name && $value) { + $test = split(',mail=',$value); + if ($test[1]) { + $name = "mail"; + $value = $test[1]; + } + //echo '
'.$j.': '.$name.' => '.$value; + if ($o->import[$name] != "" && $value != "") { + $buffer = $o->import_new_attrib($buffer, $o->import[$name],$value); + } + } else { + $buffer = $o->import_end_record($buffer,$private); } - $buffer = $o->import_end_record($buffer,$private); } } diff --git a/addressbook/templates/default/import.tpl b/addressbook/templates/default/import.tpl index e461c87399..64e2b38258 100644 --- a/addressbook/templates/default/import.tpl +++ b/addressbook/templates/default/import.tpl @@ -14,7 +14,10 @@
  1. In Outlook, select your Contacts folder, select Import and Export... from the File - menu and export your contacts into a comma separated text file.

  2. + menu and export your contacts into a comma separated text file. +

    Or, in Netscape, open the Addressbook and select Export from the File menu. + The file exported will be in LDIF format.

    +

  3. Enter the path to the exported file here:

  4. Select the type of conversion (Import types will perform an actual import. Debug will display output in browser or via a download.):
    @@ -23,8 +26,6 @@ {conv}

  5. Debug output in browser (Uncheck to download output.)
  6. -
  7. Use this basedn (LDAP)
  8. -
  9. Use this context for storing Contacts (LDAP)