get the cvs-import working with register globals off and the new links

This commit is contained in:
Ralf Becker 2003-06-29 23:03:58 +00:00
parent cb9f3526c9
commit 4711f99fd7
2 changed files with 144 additions and 107 deletions

View File

@ -15,20 +15,19 @@
$GLOBALS['phpgw_info']['flags'] = array( $GLOBALS['phpgw_info']['flags'] = array(
'currentapp' => 'infolog', 'currentapp' => 'infolog',
'noheader' => True, 'noheader' => True,
'nonavbar' => True, 'enable_contacts_class' => True,
'enable_contacts_class' => True
); );
include("../header.inc.php"); include('../header.inc.php');
if (!isset($GLOBALS['phpgw_info']['user']['apps']['admin']) || if (!isset($GLOBALS['phpgw_info']['user']['apps']['admin']) ||
!$GLOBALS['phpgw_info']['user']['apps']['admin']) // no admin !$GLOBALS['phpgw_info']['user']['apps']['admin']) // no admin
{ {
Header('Location: ' . $GLOBALS['phpgw']->link('/home.php')); $GLOBALS['phpgw']->redirect_link('/home.php');
$GLOBALS['phpgw']->common->phpgw_exit();
} }
$GLOBALS['phpgw_info']['flags']['app_header'] = lang('InfoLog - Import CSV-File');
$GLOBALS['phpgw']->common->phpgw_header(); $GLOBALS['phpgw']->common->phpgw_header();
$infolog = createobject('infolog.uiinfolog'); $boinfolog = createobject('infolog.boinfolog');
$GLOBALS['phpgw']->template->set_file(array('import_t' => 'csv_import.tpl')); $GLOBALS['phpgw']->template->set_file(array('import_t' => 'csv_import.tpl'));
$GLOBALS['phpgw']->template->set_block('import_t','filename'); $GLOBALS['phpgw']->template->set_block('import_t','filename');
@ -42,12 +41,16 @@
// $GLOBALS['phpgw']->template->set_var("navbar_bg",$GLOBALS['phpgw_info']["theme"]["navbar_bg"]); // $GLOBALS['phpgw']->template->set_var("navbar_bg",$GLOBALS['phpgw_info']["theme"]["navbar_bg"]);
// $GLOBALS['phpgw']->template->set_var("navbar_text",$GLOBALS['phpgw_info']["theme"]["navbar_text"]); // $GLOBALS['phpgw']->template->set_var("navbar_text",$GLOBALS['phpgw_info']["theme"]["navbar_text"]);
if ($action == 'download' && (!$fieldsep || !$csvfile || !($fp=fopen($csvfile,"r")))) { // pull some vars from _POST
//
$action = $_POST['action'];
$csvfile = isset($_POST['csvfile']) ? $_POST['csvfile'] : $_FILES['csvfile']['tmp_name'];
if ($action == 'download' && (!$_POST['fieldsep'] || !$csvfile || !($fp=fopen($csvfile,"r"))))
{
$action = ''; $action = '';
} }
$GLOBALS['phpgw']->template->set_var("action_url",$GLOBALS['phpgw']->link("/infolog/csv_import.php")); $GLOBALS['phpgw']->template->set_var("action_url",$GLOBALS['phpgw']->link("/infolog/csv_import.php"));
$GLOBALS['phpgw']->template->set_var( $infolog->setStyleSheet( ));
$GLOBALS['phpgw']->template->set_var("lang_info_action",lang("InfoLog - Import CSV-File"));
$PSep = '||'; // Pattern-Separator, separats the pattern-replacement-pairs in trans $PSep = '||'; // Pattern-Separator, separats the pattern-replacement-pairs in trans
$ASep = '|>'; // Assignment-Separator, separats pattern and replacesment $ASep = '|>'; // Assignment-Separator, separats pattern and replacesment
@ -55,20 +58,27 @@
$CPre = '|['; $CPreReg = '\|\['; // |{csv-fieldname} is expanded to the value of the csv-field $CPre = '|['; $CPreReg = '\|\['; // |{csv-fieldname} is expanded to the value of the csv-field
$CPos = ']'; $CPosReg = '\]'; // if used together with @ (replacement is eval-ed) value gets autom. quoted $CPos = ']'; $CPosReg = '\]'; // if used together with @ (replacement is eval-ed) value gets autom. quoted
function dump_array( $arr ) { function dump_array( $arr )
while (list($key,$val) = each($arr)) {
foreach($arr as $key => $val)
{
$ret .= ($ret ? ',' : '(') . "'$key' => '$val'"; $ret .= ($ret ? ',' : '(') . "'$key' => '$val'";
}
return $ret.')'; return $ret.')';
} }
function index( $value,$arr ) { function index( $value,$arr )
while (list ($key,$val) = each($arr)) {
foreach($arr as $key => $val)
{
if ($value == $val) if ($value == $val)
return $key; return $key;
}
return False; return False;
} }
function addr_id( $n_family,$n_given,$org_name ) { // find in Addressbook, at least n_family AND (n_given OR org_name) have to match function addr_id( $n_family,$n_given,$org_name )
{ // find in Addressbook, at least n_family AND (n_given OR org_name) have to match
$contacts = createobject('phpgwapi.contacts'); $contacts = createobject('phpgwapi.contacts');
$addrs = $contacts->read( 0,0,array('id'),'',"n_family=$n_family,n_given=$n_given,org_name=$org_name" ); $addrs = $contacts->read( 0,0,array('id'),'',"n_family=$n_family,n_given=$n_given,org_name=$org_name" );
@ -94,7 +104,7 @@ function cat_id($cats)
$cats = split('[,;]',$cats); $cats = split('[,;]',$cats);
while (list($k,$cat) = each($cats)) foreach($cats as $k => $cat)
{ {
if (isset($cat2id[$cat])) if (isset($cat2id[$cat]))
{ {
@ -126,11 +136,12 @@ function cat_id($cats)
return $id_str; return $id_str;
} }
switch ($action) { switch ($action)
{
case '': // Start, ask Filename case '': // Start, ask Filename
$GLOBALS['phpgw']->template->set_var('lang_csvfile',lang('CSV-Filename')); $GLOBALS['phpgw']->template->set_var('lang_csvfile',lang('CSV-Filename'));
$GLOBALS['phpgw']->template->set_var('lang_fieldsep',lang('Fieldseparator')); $GLOBALS['phpgw']->template->set_var('lang_fieldsep',lang('Fieldseparator'));
$GLOBALS['phpgw']->template->set_var('fieldsep',$fieldsep ? $fieldsep : ','); $GLOBALS['phpgw']->template->set_var('fieldsep',$_POST['fieldsep'] ? $_POST['fieldsep'] : ',');
$GLOBALS['phpgw']->template->set_var('submit',lang('Download')); $GLOBALS['phpgw']->template->set_var('submit',lang('Download'));
$GLOBALS['phpgw']->template->set_var('csvfile',$csvfile); $GLOBALS['phpgw']->template->set_var('csvfile',$csvfile);
$GLOBALS['phpgw']->template->set_var('enctype','ENCTYPE="multipart/form-data"'); $GLOBALS['phpgw']->template->set_var('enctype','ENCTYPE="multipart/form-data"');
@ -153,9 +164,10 @@ function cat_id($cats)
$GLOBALS['phpgw']->template->set_var('lang_debug',lang('Test Import (show importable records <u>only</u> in browser)')); $GLOBALS['phpgw']->template->set_var('lang_debug',lang('Test Import (show importable records <u>only</u> in browser)'));
$GLOBALS['phpgw']->template->parse('rows','fheader'); $GLOBALS['phpgw']->template->parse('rows','fheader');
$hiddenvars .= '<input type="hidden" name="action" value="import">'."\n". $hiddenvars .= '<input type="hidden" name="action" value="import">'."\n".
'<input type="hidden" name="fieldsep" value="'.$fieldsep."\">\n"; '<input type="hidden" name="fieldsep" value="'.$_POST['fieldsep']."\">\n";
$info_names = array( 'type' => 'Type: task,phone,note,confirm,reject,email,fax', $info_names = array(
'type' => 'Type: task,phone,note,confirm,reject,email,fax',
'from' => 'From: text(64) free text if no Addressbook-entry assigned', 'from' => 'From: text(64) free text if no Addressbook-entry assigned',
'addr' => 'Addr: text(64) phone-nr/email-address', 'addr' => 'Addr: text(64) phone-nr/email-address',
'subject' => 'Subject: text(64)', 'subject' => 'Subject: text(64)',
@ -168,12 +180,13 @@ function cat_id($cats)
'startdate' => 'Start Date: DateTime', 'startdate' => 'Start Date: DateTime',
'enddate' => 'End Date: DateTime', 'enddate' => 'End Date: DateTime',
'pri' => 'Priority: urgent,high,normal,low', 'pri' => 'Priority: urgent,high,normal,low',
'time' => 'Time: int(11) time used in min', //'time' => 'Time: int(11) time used in min',
'bill_cat' => 'Billing Cathegory: int(11)', //'bill_cat' => 'Billing Cathegory: int(11)',
'status' => 'Status: offer,ongoing,call,will-call,done,billed', 'status' => 'Status: offer,ongoing,call,will-call,done,billed',
'confirm' => 'Confirmation: not,accept,finish,both when to confirm', 'confirm' => 'Confirmation: not,accept,finish,both when to confirm',
'cat_id' => 'Categorie id(s), to set use @cat_id(Cat1,Cat2)', 'cat_id' => 'Categorie id(s), to set use @cat_id(Cat1,Cat2)',
'addr_id' => 'Addressbook id, to set use @addr_id(nlast,nfirst,org)' ); 'addr_id' => 'Addressbook id, to set use @addr_id(nlast,nfirst,org)'
);
// the next line is used in the help-text too // the next line is used in the help-text too
$mktime_lotus = "${PSep}0?([0-9]+)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*).*$ASep@mktime(${VPre}4,${VPre}5,${VPre}6,${VPre}2,${VPre}3,${VPre}1)"; $mktime_lotus = "${PSep}0?([0-9]+)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*).*$ASep@mktime(${VPre}4,${VPre}5,${VPre}6,${VPre}2,${VPre}3,${VPre}1)";
@ -192,21 +205,26 @@ function cat_id($cats)
'no CSV 2' => "subject${PSep}@substr(${CPre}Notiz$CPos,0,60).' ...'" ); 'no CSV 2' => "subject${PSep}@substr(${CPre}Notiz$CPos,0,60).' ...'" );
*/ */
$info_name_options = "<option value=\"\">none\n"; $info_name_options = "<option value=\"\">none\n";
while (list($field,$name) = each($info_names)) { foreach($info_names as $field => $name)
{
$info_name_options .= "<option value=\"$field\">".$GLOBALS['phpgw']->strip_html($name)."\n"; $info_name_options .= "<option value=\"$field\">".$GLOBALS['phpgw']->strip_html($name)."\n";
} }
$csv_fields = fgetcsv($fp,8000,$fieldsep); $csv_fields = fgetcsv($fp,8000,$_POST['fieldsep']);
$csv_fields[] = 'no CSV 1'; // eg. for static assignments $csv_fields[] = 'no CSV 1'; // eg. for static assignments
$csv_fields[] = 'no CSV 2'; $csv_fields[] = 'no CSV 2';
$csv_fields[] = 'no CSV 3'; $csv_fields[] = 'no CSV 3';
while (list($csv_idx,$csv_field) = each($csv_fields)) { foreach($csv_fields as $csv_idx => $csv_field)
{
$GLOBALS['phpgw']->template->set_var('csv_field',$csv_field); $GLOBALS['phpgw']->template->set_var('csv_field',$csv_field);
$GLOBALS['phpgw']->template->set_var('csv_idx',$csv_idx); $GLOBALS['phpgw']->template->set_var('csv_idx',$csv_idx);
if ($def = $defaults[$csv_field]) { if ($def = $defaults[$csv_field])
{
list( $info,$trans ) = explode($PSep,$def,2); list( $info,$trans ) = explode($PSep,$def,2);
$GLOBALS['phpgw']->template->set_var('trans',$trans); $GLOBALS['phpgw']->template->set_var('trans',$trans);
$GLOBALS['phpgw']->template->set_var('info_fields',str_replace('="'.$info.'">','="'.$info.'" selected>',$info_name_options)); $GLOBALS['phpgw']->template->set_var('info_fields',str_replace('="'.$info.'">','="'.$info.'" selected>',$info_name_options));
} else { }
else
{
$GLOBALS['phpgw']->template->set_var('trans',''); $GLOBALS['phpgw']->template->set_var('trans','');
$GLOBALS['phpgw']->template->set_var('info_fields',$info_name_options); $GLOBALS['phpgw']->template->set_var('info_fields',$info_name_options);
} }
@ -221,7 +239,7 @@ function cat_id($cats)
$old = $csvfile; $csvfile = $GLOBALS['phpgw_info']['server']['temp_dir'].'/info_log_import_'.basename($csvfile); $old = $csvfile; $csvfile = $GLOBALS['phpgw_info']['server']['temp_dir'].'/info_log_import_'.basename($csvfile);
rename($old,$csvfile); rename($old,$csvfile);
$hiddenvars .= '<input type="hidden" name="csvfile" value="'.$csvfile.'">'; $hiddenvars .= '<input type="hidden" name="csvfile" value="'.$csvfile.'">';
$help_on_trans = "<a name='help'><b>How to use Translation's</b><p>". $help_on_trans = "<a name=\"help\"></a><b>How to use Translation's</b><p>".
"Translations enable you to change / adapt the content of each CSV field for your needs. <br>". "Translations enable you to change / adapt the content of each CSV field for your needs. <br>".
"General syntax is: <b>pattern1 ${ASep} replacement1 ${PSep} ... ${PSep} patternN ${ASep} replacementN</b><br>". "General syntax is: <b>pattern1 ${ASep} replacement1 ${PSep} ... ${PSep} patternN ${ASep} replacementN</b><br>".
"If the pattern-part of a pair is ommited it will match everything ('^.*$'), which is only ". "If the pattern-part of a pair is ommited it will match everything ('^.*$'), which is only ".
@ -257,21 +275,22 @@ function cat_id($cats)
break; break;
case 'import': case 'import':
$fp=fopen($csvfile,"r"); set_time_limit(0);
$csv_fields = fgetcsv($fp,8000,$fieldsep); $fp=fopen($_POST['csvfile'],'r');
$csv_fields = fgetcsv($fp,8000,$_POST['fieldsep']);
$csv_fields[] = 'no CSV 1'; // eg. for static assignments $csv_fields[] = 'no CSV 1'; // eg. for static assignments
$csv_fields[] = 'no CSV 2'; $csv_fields[] = 'no CSV 2';
$csv_fields[] = 'no CSV 3'; $csv_fields[] = 'no CSV 3';
$info_fields = array_diff($info_fields,array( '' )); // throw away empty / not assigned entrys $info_fields = array_diff($_POST['info_fields'],array( '' )); // throw away empty / not assigned entrys
$defaults = array(); $defaults = array();
while (list($csv_idx,$info) = each($info_fields)) foreach($info_fields as $csv_idx => $info)
{ // convert $trans[$csv_idx] into array of pattern => value { // convert $trans[$csv_idx] into array of pattern => value
$defaults[$csv_fields[$csv_idx]] = $info; $defaults[$csv_fields[$csv_idx]] = $info;
if ($trans[$csv_idx]) if ($_POST['trans'][$csv_idx])
{ {
$defaults[$csv_fields[$csv_idx]] .= $PSep.addslashes($trans[$csv_idx]); $defaults[$csv_fields[$csv_idx]] .= $PSep.addslashes($_POST['trans'][$csv_idx]);
} }
} }
@ -281,47 +300,59 @@ function cat_id($cats)
$log = "<table border=1>\n\t<tr><td>#</td>\n"; $log = "<table border=1>\n\t<tr><td>#</td>\n";
reset($info_fields); foreach($info_fields as $csv_idx => $info)
while (list($csv_idx,$info) = each($info_fields)) { // convert $trans[$csv_idx] into array of pattern => value { // convert $trans[$csv_idx] into array of pattern => value
// if (!$debug) echo "<p>$csv_idx: ".$csv_fields[$csv_idx].": $info".($trans[$csv_idx] ? ': '.$trans[$csv_idx] : '')."</p>"; // if (!$debug) echo "<p>$csv_idx: ".$csv_fields[$csv_idx].": $info".($trans[$csv_idx] ? ': '.$trans[$csv_idx] : '')."</p>";
$pat_reps = explode($PSep,stripslashes($trans[$csv_idx])); $pat_reps = explode($PSep,stripslashes($_POST['trans'][$csv_idx]));
$replaces = ''; $values = ''; $replaces = ''; $values = '';
if ($pat_reps[0] != '') { if ($pat_reps[0] != '')
while (list($k,$pat_rep) = each($pat_reps)) { {
foreach($pat_reps as $k => $pat_rep)
{
list($pattern,$replace) = explode($ASep,$pat_rep,2); list($pattern,$replace) = explode($ASep,$pat_rep,2);
if ($replace == '') { $replace = $pattern; $pattern = '^.*$'; } if ($replace == '')
{
$replace = $pattern; $pattern = '^.*$';
}
$values[$pattern] = $replace; // replace two with only one, added by the form $values[$pattern] = $replace; // replace two with only one, added by the form
$replaces .= ($replaces != '' ? $PSep : '') . $pattern . $ASep . $replace; $replaces .= ($replaces != '' ? $PSep : '') . $pattern . $ASep . $replace;
} }
$trans[$csv_idx] = $values; $trans[$csv_idx] = $values;
} else } /*else
unset( $trans[$csv_idx] ); unset( $trans[$csv_idx] );*/
$log .= "\t\t<td><b>$info</b></td>\n"; $log .= "\t\t<td><b>$info</b></td>\n";
} }
if ($start < 1) $start = 1; $start = $_POST['start'] < 1 ? 1 : $_POST['start'];
for ($i = 1; $i < $start && fgetcsv($fp,8000,$fieldsep); ++$i) ; // overread lines before our start-record for ($i = 1; $i < $start && fgetcsv($fp,8000,$_POST['fieldsep']); ++$i) ; // overread lines before our start-record
for ($anz = 0; $anz < $max && ($fields = fgetcsv($fp,8000,$fieldsep)); ++$anz) { for ($anz = 0; $anz < $_POST['max'] && ($fields = fgetcsv($fp,8000,$_POST['fieldsep'])); ++$anz)
{
$log .= "\t</tr><tr><td>".($start+$anz)."</td>\n"; $log .= "\t</tr><tr><td>".($start+$anz)."</td>\n";
reset($info_fields); $values = array(); $values = array();
while (list($csv_idx,$info) = each($info_fields)) { foreach($info_fields as $csv_idx => $info)
{
//echo "<p>$csv: $info".($trans[$csv] ? ': '.$trans[$csv] : '')."</p>"; //echo "<p>$csv: $info".($trans[$csv] ? ': '.$trans[$csv] : '')."</p>";
$val = $fields[$csv_idx]; $val = $fields[$csv_idx];
if (isset($trans[$csv_idx])) { if (isset($trans[$csv_idx]))
{
$trans_csv = $trans[$csv_idx]; $trans_csv = $trans[$csv_idx];
while (list($pattern,$replace) = each($trans_csv)) { while (list($pattern,$replace) = each($trans_csv))
if (ereg((string) $pattern,$val)) { {
if (ereg((string) $pattern,$val))
{
// echo "<p>csv_idx='$csv_idx',info='$info',trans_csv=".dump_array($trans_csv).",ereg_replace('$pattern','$replace','$val') = "; // echo "<p>csv_idx='$csv_idx',info='$info',trans_csv=".dump_array($trans_csv).",ereg_replace('$pattern','$replace','$val') = ";
$val = ereg_replace((string) $pattern,str_replace($VPre,'\\',$replace),(string) $val); $val = ereg_replace((string) $pattern,str_replace($VPre,'\\',$replace),(string) $val);
// echo "'$val'</p>"; // echo "'$val'</p>";
$reg = $CPreReg.'([a-zA-Z_0-9]+)'.$CPosReg; $reg = $CPreReg.'([a-zA-Z_0-9]+)'.$CPosReg;
while (ereg($reg,$val,$vars)) { // expand all CSV fields while (ereg($reg,$val,$vars))
{ // expand all CSV fields
$val = str_replace($CPre.$vars[1].$CPos,$val[0] == '@' ? "'".addslashes($fields[index($vars[1],$csv_fields)])."'" : $fields[index($vars[1],$csv_fields)],$val); $val = str_replace($CPre.$vars[1].$CPos,$val[0] == '@' ? "'".addslashes($fields[index($vars[1],$csv_fields)])."'" : $fields[index($vars[1],$csv_fields)],$val);
} }
if ($val[0] == '@') { if ($val[0] == '@')
{
// removing the $ to close security hole of showing vars, which contain eg. passwords // removing the $ to close security hole of showing vars, which contain eg. passwords
$val = 'return '.substr(str_replace('$','',$val),1).';'; $val = 'return '.substr(str_replace('$','',$val),1).';';
// echo "<p>eval('$val')="; // echo "<p>eval('$val')=";
@ -339,21 +370,29 @@ function cat_id($cats)
} }
if (!isset($values['datecreated'])) $values['datecreated'] = $values['startdate']; if (!isset($values['datecreated'])) $values['datecreated'] = $values['startdate'];
if (!$debug) { if (!$_POST['debug'])
$infolog->bo->write($values); {
$id = $boinfolog->write($values);
if ($id && $values['addr_id'])
{
$boinfolog->write(array(
'info_id' => $id,
'info_link_id' => $boinfolog->link->link('infolog',$id,'addressbook',$values['addr_id'])
));
}
} }
} }
$log .= "\t</tr>\n</table>\n"; $log .= "\t</tr>\n</table>\n";
$GLOBALS['phpgw']->template->set_var('anz_imported',$debug ? lang( '%1 records read (not yet imported, you may go back and uncheck Test Import)', $GLOBALS['phpgw']->template->set_var('anz_imported',$_POST['debug'] ?
$anz,'<a href="javascript:history.back()">','</a>' ) : lang( '%1 records read (not yet imported, you may go back and uncheck Test Import)',$anz,'<a href="javascript:history.back()">','</a>' ) :
lang( '%1 records imported',$anz )); lang( '%1 records imported',$anz ));
$GLOBALS['phpgw']->template->set_var('log',$log); $GLOBALS['phpgw']->template->set_var('log',$log);
$GLOBALS['phpgw']->template->parse('rows','imported'); $GLOBALS['phpgw']->template->parse('rows','imported');
break; break;
} }
$GLOBALS['phpgw']->template->set_var('hiddenvars',$hiddenvars); $GLOBALS['phpgw']->template->set_var('hiddenvars',$hiddenvars);
$GLOBALS['phpgw']->template->fp('phpgw_body','import'); $GLOBALS['phpgw']->template->pfp('phpgw_body','import');
$GLOBALS['phpgw']->common->phpgw_footer(); $GLOBALS['phpgw']->common->phpgw_footer();
?> ?>

View File

@ -53,8 +53,6 @@
<!-- END imported --> <!-- END imported -->
<!-- BEGIN import --> <!-- BEGIN import -->
{info_css}<p class=action>{lang_info_action}<br>
<hr noshade width="98%" align="center" size="1">
<center> <center>
<FORM {enctype} action="{action_url}" method="post"> <FORM {enctype} action="{action_url}" method="post">
<TABLE> <TABLE>