Working LDIF import

This commit is contained in:
Miles Lott 2001-03-11 17:34:58 +00:00
parent 19d2b3c139
commit b5cd836f7f
6 changed files with 142 additions and 16 deletions

View File

@ -25,6 +25,7 @@
class import_conv
{
var $type = '';
var $basedn;
var $contactsdn;

View File

@ -25,6 +25,7 @@
class import_conv
{
var $type = '';
var $currentrecord; //used for buffering to allow uid lines to go first
var $import = array(

View File

@ -0,0 +1,99 @@
<?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.
//
// 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 = '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 '<br>';
for ($i=1;$i<=count($buffer);$i++) {
while ( list($name,$value) = @each($buffer[$i]) ) {
//echo '<br>'.$i.': '.$name.' => '.$value;
$entry[$i][$name] = $value;
}
//echo '<br>';
$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 '<br>'.$this->id.': '.$name.' => '.$value;
}
return $buffer;
}
function import_new_attrib($buffer,$name,$value) {
//$value = str_replace("\n","<BR>",$value);
$value = str_replace("\r","",$value);
//echo '<br>'.$name.' => '.$value;
$this->currentrecord += array($name => $value);
return $buffer;
}
}
?>

View File

@ -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",

View File

@ -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 '<br>'.$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);
}
}

View File

@ -14,7 +14,10 @@
<OL>
<LI>In Outlook, select your Contacts folder, select <b>Import
and Export...</b> from the <b>File</b>
menu and export your contacts into a comma separated text file.<P></LI>
menu and export your contacts into a comma separated text file.
<P>Or, in Netscape, open the Addressbook and select <b>Export</b> from the <b>File</b> menu.
The file exported will be in LDIF format.<P>
</LI>
<LI>Enter the path to the exported file here:
<INPUT NAME="tsvfile" SIZE=48 TYPE="file" VALUE="{tsvfilename}"><P></LI>
<LI>Select the type of conversion (Import types will perform an actual import. Debug will display output in browser or via a download.):<BR>
@ -23,8 +26,6 @@
{conv}
</SELECT><P></LI>
<LI><INPUT NAME="download" TYPE="checkbox" VALUE="{debug}" CHECKED>Debug output in browser (Uncheck to download output.)</LI>
<LI>Use this basedn (LDAP)<BR><INPUT NAME="basedn" TYPE="text" VALUE="{basedn}" SIZE="48"></LI>
<LI>Use this context for storing Contacts (LDAP)<BR><INPUT NAME="context" TYPE="text" VALUE="{context}" SIZE="48"></LI>
<LI><INPUT NAME="convert" TYPE="submit" VALUE="{download}"></LI>
</OL>
</FORM></TD>