* Licensed under the GNU GPL. For full terms see the file COPYING.
*
* Import csv files for address book
* This takes a comma delimited file uploaded from addressbook.php
* and allows the user to rearrange the field order to better
* fit the address book. A subset of data is manipulated to save time.
* @version $Id$
* @package sm-plugins
* @subpackage abook_import_export
*/
/** SquirrelMail init */
if (file_exists('../../include/init.php')) {
/* sm 1.5.2+*/
/* main init script */
include_once('../../include/init.php');
} else {
/* sm 1.4.0+ */
/** @ignore */
define('SM_PATH', '../../');
/* main init script */
include_once(SM_PATH . 'include/validate.php');
}
/* load address book functions */
include_once(SM_PATH . 'functions/addressbook.php');
/* load sqm_baseuri() (sm 1.4.0-1.4.5,1.5.0) function */
include_once(SM_PATH . 'functions/display_messages.php');
/* load own functions */
include_once(SM_PATH . 'plugins/abook_import_export/functions.php');
// Local Variables
$errorstring = '';
$finish = '';
$csvmax = 0;
$key = 0;
$x = 0;
$row = 0;
$cols = 0;
$colspan = 0;
$c = 0;
$error = 0;
$reorg = array();
$selrow = '';
// FIXME: not sure if global declarations are needed
global $color, $squirrelmail_language, $default_charset;
// Make sure that $default_charset is set to correct value
set_my_charset();
if (! sqGetGlobalVar('finish',$finish,SQ_POST)) {
// Stage 1. Process uploaded file
displayPageHeader($color, "None");
// initialize address book. don't display errors. don't init remote backends
// object is used by form. do it now in order to avoid domain switching.
$abook = addressbook_init(false, true);
// switch domain
bindtextdomain('abook_import_export',SM_PATH . 'locale');
textdomain('abook_import_export');
if (function_exists('bind_textdomain_codeset')) {
if ($squirrelmail_language == 'ja_JP') {
bind_textdomain_codeset ('abook_import_export', 'EUC-JP');
} else {
bind_textdomain_codeset ('abook_import_export', $default_charset );
}
}
if(isset($_FILES['smusercsv'])) {
/* ======= 1st case - Handle the first POST file upload ====== */
// Check to make sure the user actually put a file in the upload file box.
$smusercsv = $_FILES['smusercsv'];
if ($smusercsv['tmp_name'] == '' || $smusercsv['size'] == 0) {
// Detect PHP 4.2.0+ upload error codes (http://www.php.net/features.file-upload.errors)
$upload_error = _("Please select a file for uploading.");
if (isset($smusercsv['error']) && $smusercsv['error']!=0 ) {
switch($smusercsv['error']) {
case 1:
$upload_error = _("The uploaded file exceeds PHP upload_max_filesize limits.");
break;
case 2:
$upload_error = _("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML.");
break;
case 3:
$upload_error = _("The uploaded file was only partially uploaded.");
break;
case 4:
$upload_error = _("No file was uploaded.");
break;
case 6:
$upload_error = _("Missing a temporary directory.");
break;
case 7:
$upload_error = _("Failed to write file to disk.");
break;
case 8:
// File upload stopped by extension. 'security library' is more user friendly.
$upload_error = _("File upload stopped by security library.");
break;
default:
$upload_error = _("Unknown upload error.");
break;
}
}
$error_msg = html_tag('p',$upload_error)
.html_tag('p',sprintf(_("Return to main %sAddress Book%s page."),
'',
''), 'center');
aie_error_box($error_msg,_("Upload error"),true);
} elseif ( $smusercsv['size'] > $aie_csv_maxsize ) {
// i18n: %s displays 'somenumber B', 'somenumber KB' or 'somenumber MB'.
$error_msg = sprintf(_("Imported CSV file is too big. Contact your system administrator, if you want to import files, that are bigger than %s."),aie_display_size($aie_csv_maxsize));
aie_error_box($error_msg,'',true);
}
/**
* Remove old csvdata from session and set initial $csvdata and $csvorder values.
* $csvdata stores imported data. $csvorder is used by aie_CSVProcess() to detect
* order of imported fields. variable is accessed through references in order
* to avoid globalization of the variable. $error can store fatal processing errors.
*/
sqsession_unregister('csvdata');
$csvdata = array();
$csvorder = array();
$error = '';
// find unused file name in attachment directory.
$temp_file = md5($smusercsv['tmp_name']);
// handle misconfigured $attachment_dir
if (preg_match('/^[a-z]\:\\\\/i',$attachment_dir)) {
// windows full path x:\something. relative path issues are ignored, because they will
// require mods in all SquirrelMail scripts.
if (substr($attachment_dir,-1,1)!='\\') {
$attachment_dir.= '\\';
}
} elseif (substr($attachment_dir,-1,1)!='/') {
$attachment_dir.= '/';
}
while (file_exists($attachment_dir . $temp_file)) {
// calculate new md5sum until we find place to store data
$temp_file = md5($temp_file);
}
// don't open uploaded file directly. Move it to SM temp directory before using it.
if (@move_uploaded_file($smusercsv['tmp_name'],$attachment_dir . $temp_file)) {
$csvfile = fopen($attachment_dir . $temp_file,"r");
if (!$csvfile) {
echo '
'
.'
' ._("Error, could not open address file.") .' |
'. _("The entries that have not imported yet are still marked below with a checkbox. You can retry importing them to your personal addressbook, by changing the fields accordingly.") .'
'; } echo ''; } else { /** * $csvdata is empty. User tried to import empty file or $error contains fatal * processing error message. */ if (empty($error)) $error = _("Nothing to import"); $error .= '' . _("Return to Address Book") . '
'; aie_error_box($error); } if(strlen($errorstring)) { echo _("The following rows have errors") . ':' . _("Retry Importing the addresses that failed") . '
'; } else { echo '' . _("Click on the link below to verify your work.") . '
' .'