egroupware_official/addressbook/import.php

230 lines
6.5 KiB
PHP
Raw Normal View History

<?php
/**************************************************************************\
* phpGroupWare - addressbook *
* http://www.phpgroupware.org *
* Written by Joseph Engo <jengo@phpgroupware.org> *
* -------------------------------------------- *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
\**************************************************************************/
/* $Id$ */
2001-03-11 07:58:20 +01:00
$phpgw_info["flags"]["currentapp"] = "addressbook";
$phpgw_info["flags"]["enable_contacts_class"] = True;
$phpgw_info["flags"]["enable_browser_class"] = True;
2001-03-11 07:58:20 +01:00
include("../header.inc.php");
2001-03-13 03:07:45 +01:00
//$sep = $phpgw_info["server"]["dir_separator"];
$sep = SEP;
2001-03-11 07:58:20 +01:00
// Construct a default basedn for Contacts if using LDAP
$tmpbasedn = split(",",$phpgw_info["server"]["ldap_context"]);
array_shift($tmpbasedn);
for ($i=0;$i<count($tmpbasedn);$i++) {
if($i==0) {
$basedn = $tmpbasedn[$i];
} else {
$basedn = $basedn.",".$tmpbasedn[$i];
}
}
$context = $phpgw_info["server"]["ldap_contact_context"];
if (!$convert)
{
$t = new Template(PHPGW_APP_TPL);
2001-03-11 07:58:20 +01:00
$t->set_file(array("import" => "import.tpl"));
2001-03-20 17:39:28 +01:00
$dir_handle=opendir($phpgw_info["server"]["app_root"].$sep."import");
2001-03-11 07:58:20 +01:00
$i=0; $myfilearray="";
while ($file = readdir($dir_handle))
{
2001-03-20 17:39:28 +01:00
//echo "<!-- ".is_file($phpgw_info["server"]["app_root"].$sep."import".$sep.$file)." -->";
if ((substr($file, 0, 1) != ".") && is_file($phpgw_info["server"]["app_root"].$sep."import".$sep.$file) )
{
2001-03-11 07:58:20 +01:00
$myfilearray[$i] = $file;
$i++;
}
}
closedir($dir_handle);
sort($myfilearray);
for ($i=0;$i<count($myfilearray);$i++)
{
$fname = ereg_replace('_',' ',$myfilearray[$i]);
$conv .= '<OPTION VALUE="'.$myfilearray[$i].'">'.$fname.'</OPTION>';
2001-03-11 07:58:20 +01:00
}
2001-03-11 07:58:20 +01:00
$t->set_var("lang_cancel",lang("Cancel"));
2001-03-27 19:17:46 +02:00
$t->set_var("lang_cat",lang("Select Category"));
2001-03-29 13:02:14 +02:00
$t->set_var("cancel_url",$phpgw->link("/addressbook/index.php"));
2001-03-11 07:58:20 +01:00
$t->set_var("navbar_bg",$phpgw_info["theme"]["navbar_bg"]);
$t->set_var("navbar_text",$phpgw_info["theme"]["navbar_text"]);
2001-03-27 19:17:46 +02:00
$t->set_var("import_text",lang("Import from LDIF, CSV, or VCard"));
2001-03-29 13:02:14 +02:00
$t->set_var("action_url",$phpgw->link("/addressbook/import.php"));
$t->set_var("cat_link",cat_option($cat_id,True,False));
2001-03-11 07:58:20 +01:00
$t->set_var("tsvfilename","");
$t->set_var("conv",$conv);
$t->set_var("debug",lang("Debug output in browser"));
2001-03-11 18:34:58 +01:00
$t->set_var("filetype",lang("LDIF"));
2001-03-11 07:58:20 +01:00
$t->set_var("basedn",$basedn);
$t->set_var("context",$context);
$t->set_var("download",lang("Submit"));
2001-03-29 13:02:14 +02:00
$t->set_var("start",$start);
$t->set_var("sort",$sort);
$t->set_var("order",$order);
$t->set_var("filter",$filter);
$t->set_var("query",$query);
$t->set_var("cat_id",$cat_id);
2001-03-11 07:58:20 +01:00
$t->pparse("out","import");
$phpgw->common->phpgw_footer();
}
else
{
2001-03-20 17:39:28 +01:00
include ($phpgw_info["server"]["app_root"].$sep."import".$sep.$conv_type);
2001-03-11 07:58:20 +01:00
if ($private=="") { $private="public"; }
$row=0;
$buffer=array();
2001-03-20 23:55:03 +01:00
$this = new import_conv;
$buffer = $this->import_start_file($buffer,$basedn,$context);
2001-03-11 07:58:20 +01:00
$fp=fopen($tsvfile,"r");
if ($this->type == 'csv')
{
while ($data = fgetcsv($fp,8000,","))
{
2001-03-11 18:34:58 +01:00
$num = count($data);
$row++;
if ($row == 1)
{
2001-03-11 18:34:58 +01:00
$header = $data;
}
else
{
2001-03-20 23:55:03 +01:00
$buffer = $this->import_start_record($buffer);
for ($c=0; $c<$num; $c++ )
{
2001-03-11 18:34:58 +01:00
//Send name/value pairs along with the buffer
if ($this->import[$header[$c]]!="" && $data[$c]!="")
{
2001-03-20 23:55:03 +01:00
$buffer = $this->import_new_attrib($buffer, $this->import[$header[$c]],$data[$c]);
2001-03-11 18:34:58 +01:00
}
}
2001-03-20 23:55:03 +01:00
$buffer = $this->import_end_record($buffer,$private);
2001-03-11 18:34:58 +01:00
}
}
}
elseif ($this->type == 'ldif')
{
while ($data = fgets($fp,8000))
{
$url = "";
list($name,$value,$extra) = split(':', $data);
if (substr($name,0,2) == 'dn')
{
2001-03-20 23:55:03 +01:00
$buffer = $this->import_start_record($buffer);
2001-03-11 18:34:58 +01:00
}
$test = trim($value);
if ($name && !empty($test) && $extra)
{
// Probable url string
$url = $test;
$value = $extra;
}
elseif ($name && empty($test) && $extra)
{
// Probable multiline encoding
$newval = base64_decode(trim($extra));
$value = $newval;
//echo $name.':'.$value;
}
if ($name && $value)
{
2001-03-11 18:34:58 +01:00
$test = split(',mail=',$value);
if ($test[1])
{
2001-03-11 18:34:58 +01:00
$name = "mail";
$value = $test[1];
}
if ($url)
{
2001-03-12 03:05:20 +01:00
$name = "homeurl";
$value = $url. ':' . $value;
2001-03-12 03:05:20 +01:00
}
2001-03-11 18:34:58 +01:00
//echo '<br>'.$j.': '.$name.' => '.$value;
if ($this->import[$name] != "" && $value != "")
{
2001-03-20 23:55:03 +01:00
$buffer = $this->import_new_attrib($buffer, $this->import[$name],$value);
2001-03-11 07:58:20 +01:00
}
}
else
{
2001-03-20 23:55:03 +01:00
$buffer = $this->import_end_record($buffer,$private);
2001-03-11 07:58:20 +01:00
}
}
}
else
{
while ($data = fgets($fp,8000))
{
2001-03-21 01:14:04 +01:00
list($name,$value,$extra) = split(':', $data);
if (strtolower(substr($name,0,5)) == 'begin')
{
2001-03-20 23:55:03 +01:00
$buffer = $this->import_start_record($buffer);
}
if (substr($value,0,5) == "http")
{
2001-03-21 01:14:04 +01:00
$value = $value . ":".$extra;
}
if ($name && $value)
{
2001-03-20 23:55:03 +01:00
reset($this->import);
while ( list($fname,$fvalue) = each($this->import) )
{
if ( strstr(strtolower($name), $this->import[$fname]) )
{
2001-03-20 23:55:03 +01:00
$buffer = $this->import_new_attrib($buffer,$name,$value);
}
}
}
else
{
2001-03-27 19:17:46 +02:00
$buffer = $this->import_end_record($buffer);
2001-03-20 19:14:46 +01:00
}
}
2001-03-11 07:58:20 +01:00
}
2001-03-11 07:58:20 +01:00
fclose($fp);
2001-03-27 19:17:46 +02:00
$buffer = $this->import_end_file($buffer,$private,$cat_id);
2001-03-20 19:14:46 +01:00
if ($download == "")
{
if($conv_type=="Debug LDAP" || $conv_type=="Debug SQL" )
{
2001-04-14 04:42:50 +02:00
// filename, default application/octet-stream, length of file, default nocache True
$phpgw->browser->content_header($tsvfilename,'',strlen($buffer));
2001-03-11 07:58:20 +01:00
echo $buffer;
}
else
{
2001-03-11 07:58:20 +01:00
echo "<pre>$buffer</pre>";
2001-03-28 03:59:08 +02:00
echo '<a href="'.$phpgw->link("/addressbook/index.php",
2001-03-29 17:39:10 +02:00
"sort=$sort&order=$order&filter=$filter&start=$start&query=$query&cat_id=$cat_id")
2001-03-28 03:59:08 +02:00
. '">'.lang("OK").'</a>';
2001-03-11 07:58:20 +01:00
$phpgw->common->phpgw_footer();
}
}
else
{
2001-03-11 07:58:20 +01:00
echo "<pre>$buffer</pre>";
2001-03-28 03:59:08 +02:00
echo '<a href="'.$phpgw->link("/addressbook/index.php",
2001-03-29 17:39:10 +02:00
"sort=$sort&order=$order&filter=$filter&start=$start&query=$query&cat_id=$cat_id")
2001-03-28 03:59:08 +02:00
. '">'.lang("OK").'</a>';
2001-03-11 07:58:20 +01:00
$phpgw->common->phpgw_footer();
}
}
?>