$ListField)
{
$ins.="&$ImpFieldID=$ListField";
$Do[$ListField]=$ImpFieldID;
}
mysql_query("UPDATE " . $TABLEPREFIX . "imports SET FieldLinks='".addslashes($ins)."' WHERE ImportID='".addslashes($ImportID)."'");
$import_result = mysql_query("SELECT * FROM " . $TABLEPREFIX . "imports WHERE ImportID='" . addslashes($ImportID)."'");
$IMPORT = mysql_fetch_assoc($import_result);
if (!SAFE_MODE) {
$IMPORT_DIR = $ROOTDIR.'temp/import_' . $ImportID;
} else {
$IMPORT_DIR = $ROOTDIR . 'temp/import';
}
/*
This splits up the file to be imported into 100 line chunks so we can do a popup window and have it refresh.
This should ease people who are looking to import thousands of users at a time.
*/
if (!is_dir($IMPORT_DIR)) {
if (!SAFE_MODE) {
if (!mkdir($IMPORT_DIR, 0755)) {
$OUTPUT .= MakeErrorBox("An error occurred", "Unable to create directory " . str_replace($ROOTDIR, '', $IMPORT_DIR));
}
} else {
$OUTPUT .= MakeErrorBox("An error occurred", "Please create the '" . $ROOTDIR . "temp/import' directory and CHMOD it to 777");
}
}
$num_files = 0;
if (is_dir($IMPORT_DIR)) {
$original_import_file = file($ROOTDIR.'temp/' . $ImportID);
$size = sizeof($original_import_file);
$num_files = 1;
$start_pos = 0;
$header = array();
if ($IMPORT['Headers'] == 1) {
$header = array_slice($original_import_file, 0, 1);
}
while($start_pos < $size) {
$file_contents = array_slice($original_import_file, $start_pos, $IMPORT_PER_FILE);
$handle = fopen($IMPORT_DIR.'/import_file' . $num_files, 'w');
// dont include the header in the first file, it's already there.
if ($start_pos > 0) fputs($handle, implode("", $header));
fputs($handle, implode("", $file_contents));
fclose($handle);
$start_pos += $IMPORT_PER_FILE;
$num_files++;
}
}
$popup_link = "import?ImportID=$ImportID&Action=DoImport&StartTime=$SYSTEMTIME&TotalFiles=$num_files";
$add_to_url = array('ConfStatus', 'Status', 'AbFormat', 'OverrideDate');
foreach($add_to_url as $k) {
$popup_link .= "&" . $k . "=" . $$k;
}
$link_fields = urlencode(serialize($_POST['LinkField']));
$popup_link .= "&LinkField=" . $link_fields;
$link = MakeAdminLink($popup_link);
$OUTPUT .= 'Click OK to start importing your users ...
';
$OUTPUT .= '
| Import File Details | |
| ';
$FORM_ITEMS[$FORM_NOT_REQUIRED_ITEM . "Contains Headers"]="checkbox|headers:1:Yes, contains headers :checked";
$HELP_ITEMS['headers']['Title'] = "Contains Headers";
$HELP_ITEMS['headers']['Content'] = "Does the first line of your import file contain headers? If so, each header should be separated with a field separator, such as: email, name, sex.";
$FORM_ITEMS[$FORM_REQUIRED_ITEM . "Field Separator"]="textfield|fielddelim:10:10:,";
$HELP_ITEMS['fielddelim']['Title'] = "Field Separator";
$HELP_ITEMS['fielddelim']['Content'] = "What is the character used in your import file that separates the contents of each new field in a record?";
$FORM_ITEMS[$FORM_REQUIRED_ITEM . "Record Separator"]="textfield|recorddelim:10:10:NEWLINE";
$HELP_ITEMS['recorddelim']['Title'] = "Record Separator";
$HELP_ITEMS['recorddelim']['Content'] = "What is the character used in your import file that separates one record from the next?";
$FORM_ITEMS[$FORM_REQUIRED_ITEM . "Import File"]="file|TextFileName";
$HELP_ITEMS['TextFileName']['Title'] = "Import File";
$HELP_ITEMS['TextFileName']['Content'] = "Choose a file to upload that contains the subscriber details that you want to import. This should be a plain text file.";
$import_files = list_files($ROOTDIR.'temp/');
$filelist = '0->Select a file from the server;';
if (!empty($import_files)) {
$found_import_files = false;
foreach($import_files as $import_file) {
if (!preg_match('/^import/', $import_file)) continue;
$found_import_files = true;
$filelist .= $import_file . '->' . $import_file . ';';
}
$filelist = substr($filelist, 0, -1);
}
$extra_check = '';
if ($found_import_files) {
$FORM_ITEMS[$FORM_REQUIRED_ITEM . "Import Server File"]="select|ServerFile:1:" . $filelist;
$HELP_ITEMS['ServerFile']['Title'] = "Import File";
$HELP_ITEMS['ServerFile']['Content'] = "Choose a file on the server that contains the subscriber details that you want to import. This should be a plain text file. The file should be named so the start is \'import\' - eg \'import_list.txt\'"; $extra_check = ' && (f.ServerFile.selectedIndex == -1 || f.ServerFile.selectedIndex == 0) '; } $FORM_ITEMS["-1"]="submit|Next »:1-import?Action=ChooseDatasource&ListID=$ListID"; //make the form $FORM=new AdminForm; $FORM->title="TextFileImport"; $FORM->items=$FORM_ITEMS; $FORM->action=MakeAdminLink("import?Action=UploadText"); $FORM->EXTRA='ENCTYPE="multipart/form-data"'; $FORM->MakeForm("Import Details"); $FORM->output = "Complete the form below and then click on the \"Next\" button to proceed. For a tutorial on how to import subscribers, click here. " . $FORM->output; $OUTPUT.=MakeBox("Import Subscribers (Step 3 of 5)",$FORM->output); $OUTPUT .= ' '; $OUTPUT .= ' '; break; case 2: // database import $query = "SELECT * FROM " . $TABLEPREFIX . "import_mappings WHERE AdminID='" . addslashes($CURRENTADMIN['AdminID'])."' AND ImportType='db'"; $map_result = mysql_query($query); $default_database_host = $default_database_user = $default_database_pass = $default_database_name = ''; $map_list = ''; $jscript = ' '; $list = mysql_fetch_assoc(mysql_query("SELECT * FROM " . $TABLEPREFIX . "lists WHERE ListID = '".addslashes($ListID)."' ORDER BY ListName ASC")); $list = stripslashes_array($list); if($list['Formats'] == 3) { $format = ''; } else if($list['Formats'] == 1) { $format = ''; } else { $format = ''; } $form = ' '; $form .= $jscript; $intro_text = 'Complete the form below and then click on the "Next" button to proceed importing data from a database'; $OUTPUT.=MakeBox("Import Subscribers (Step 3 of 5)", $intro_text . $form); break; case 3: // email account $query = "SELECT * FROM " . $TABLEPREFIX . "import_mappings WHERE AdminID='" . addslashes($CURRENTADMIN['AdminID'])."' AND ImportType='email'"; $map_result = mysql_query($query); $default_email_host = $default_email_user = $default_email_pass = ''; $default_email_port = '110'; $map_list = ''; $jscript = ' '; $list = mysql_fetch_assoc(mysql_query("SELECT * FROM " . $TABLEPREFIX . "lists WHERE ListID = '".addslashes($ListID)."' ORDER BY ListName ASC")); $list = stripslashes_array($list); if($list['Formats'] == 3) { $format = ''; } else if($list['Formats'] == 1) { $format = ''; } else { $format = ''; } $form = ' '; $form .= $jscript; $intro_text = 'Complete the form below and then click on the "Next" button to proceed. '; $OUTPUT.=MakeBox("Import Subscribers (Step 3 of 5)", $intro_text . $form); break; } } if ($Action == 'CheckEmail') { $view = (isset($_GET['View'])) ? $_GET['View'] : ''; switch($view) { case 'CheckConnection': list($status, $result) = ConnectEmail($_POST['EmailHost'], $_POST['EmailUser'], $_POST['EmailPass'], $_POST['EmailPort']); if (!$status) { $OUTPUT .= MakeErrorBox("Unable to connect", "Unable to connect to email server: " . $result); } else { if (isset($_POST['SaveSettings']) && $_POST['SaveSettings'] == 1) { $email_mapping = array( 'hostname' => $_POST['EmailHost'], 'username' => $_POST['EmailUser'], 'password' => $_POST['EmailPass'], 'port' => $_POST['EmailPort'] ); if ($_POST['MapID'] <= 0) { $query = "INSERT INTO " . $TABLEPREFIX . "import_mappings(ListID, AdminID, ImportMap, ImportType) VALUES ('" . addslashes($_POST['ListID']) . "', '" . addslashes($CURRENTADMIN['AdminID']) . "', '" . addslashes(serialize($email_mapping)) . "', 'email')"; } else { $query = "UPDATE " . $TABLEPREFIX . "import_mappings SET ImportMap='" . addslashes(serialize($email_mapping)) . "' WHERE MapID='" . addslashes($_POST['MapID'])."'"; $MapID = $_POST['MapID']; } mysql_query($query); if ($_POST['MapID'] <= 0) $MapID = mysql_insert_id(); } imap_close($result); $popup_link = 'import?ImportID=' . $ImportID . '&Action=DoEmailImport&ListID=' . $ListID . '&EmailHost=' . $_POST['EmailHost'] . '&EmailUser=' . $_POST['EmailUser'] . '&EmailPass=' . $_POST['EmailPass'] . '&EmailPort=' . $_POST['EmailPort'] . '&Status=' . $_POST['Status'] . '&Confirmed=' . $_POST['Confirmed'] . '&Format=' . $_POST['Format']; $OUTPUT .= MakeSuccessBox("Connected to Email Server Successfully", "Connected to email server successfully. Click OK to start importing email addresses ", MakeAdminLink($popup_link), "", "", "import$ImportID~width=300,height=400,left=400,top=300"); } break; } } if ($Action == 'DatabaseMappings') { switch($_GET['View']) { case 'CheckConnection': list($status, $database_result) = MySQLConnect($_POST['DatabaseHost'], $_POST['DatabaseUser'], $_POST['DatabasePass'], $_POST['DatabaseName']); if (!$status) { $OUTPUT .= MakeErrorBox("Unable to connect", "Unable to connect to the MySQL server: " . $database_result); } if ($status) { $db_tables = FetchTables($_POST['DatabaseName']); mysql_close($database_result); // we need to re-establish the old database connection. we can't just use mysql_connect(host, user, pass, new_connection=1) since that came in at php 4.2 :( // so here we go! MySQLConnect($DBHOST, $DBUSER, $DBPASS, $DBNAME); $show_fields = true; $tablenames = array_keys($db_tables); if (empty($tablenames)) { $show_fields = false; $OUTPUT .= MakeErrorBox("Empty Database", "Sorry, that database is empty. Please try again."); } if ($show_fields) { $core_fields = array( 'Email' => 'Email', 'Format' => 'Newsletter Format', 'Confirmed' => 'Confirmation Status', 'Status' => 'Active/Inactive Status' ); $custom_fields = FetchCustomFields(); $db_map = array(); $OverWrite = (isset($_POST['OverWrite'])) ? 1 : 0; if ($_POST['MapID'] > 0) $db_map = LoadMap($_POST['MapID']); $table_list = ''; $Format = (isset($_POST['Format'])) ? $_POST['Format'] : 1; $Confirmed = (isset($_POST['Confirmed'])) ? $_POST['Confirmed'] : 0; $form = ' '; $intro_text = 'Select which database fields you wish to insert into the subscriber fields.'; $OUTPUT.=MakeBox("Import Subscribers (Step 4 of 5)", $intro_text . $form); } // end if show_fields (ie the database isn't empty). } // end if ($status) break; case 'MapPaths': list($status, $database_result) = MySQLConnect($_POST['DatabaseHost'], $_POST['DatabaseUser'], $_POST['DatabasePass'], $_POST['DatabaseName']); $db_tables = FetchTables($_POST['DatabaseName']); mysql_close($database_result); // we need to re-establish the old database connection. we can't just use mysql_connect(host, user, pass, new_connection=1) since that came in at php 4.2 :( // so here we go! MySQLConnect($DBHOST, $DBUSER, $DBPASS, $DBNAME); $email_found = false; $database_mapping = array( 'hostname' => $_POST['DatabaseHost'], 'username' => $_POST['DatabaseUser'], 'password' => $_POST['DatabasePass'], 'database' => $_POST['DatabaseName'], 'tablemapping' => array() ); foreach($_POST as $key => $table) { if (!preg_match('%^map_%', $key) || preg_match('%_option$%', $key)) continue; $mapval = $key.'_option'; if (isset($_POST[$mapval]) && $_POST[$mapval] > 0) { $mapkey = preg_replace('%^map_%', '', $key); if ($mapkey == 'Email') $email_found = true; if (is_numeric($mapkey)) { $database_mapping['tablemapping']['CustomFields'][$mapkey] = array($_POST[$key] => $db_tables[$table][(int)($_POST[$mapval]-1)]); } else { $database_mapping['tablemapping'][$mapkey] = array($_POST[$key] => $db_tables[$table][(int)($_POST[$mapval]-1)]); } } } $errormsgs = array(); if (!$email_found) { $errormsgs[] = ' You need to at least map the Email Address field.'; $OUTPUT .= MakeErrorBox("Missing data", "Please try again." . implode(' ', $errormsgs)); } else { if (isset($_POST['SaveSettings'])) { if ($_POST['MapID'] <= 0) { $query = "INSERT INTO " . $TABLEPREFIX . "import_mappings(ListID, AdminID, ImportMap, ImportType) VALUES ('" . addslashes($_POST['ListID']) . "', '" . addslashes($CURRENTADMIN['AdminID']) . "', '" . addslashes(serialize($database_mapping)) . "', 'db')"; } else { $query = "UPDATE " . $TABLEPREFIX . "import_mappings SET ImportMap='" . addslashes(serialize($database_mapping)) . "' WHERE MapID='" . addslashes($_POST['MapID'])."'"; } mysql_query($query); $MapID = ($_POST['MapID'] <= 0) ? mysql_insert_id() : $_POST['MapID']; } else { $MapID = $_POST['MapID']; } foreach(array('OverWrite', 'Confirmed') as $area) { $$area = (isset($_POST[$area])) ? $_POST[$area] : 0; } $Format = (isset($_POST['Format'])) ? $_POST['Format'] : 1; $popup_link = "import?Action=DoDatabaseImport&ImportID=$ImportID&MapID=$MapID&ListID=$ListID&OverWrite=$OverWrite&Status=$Status&Format=$Format&Confirmed=$Confirmed&ImportMap=" . urlencode(serialize($database_mapping)) . "&DatabaseHost=" . $_POST['DatabaseHost'] . "&DatabaseUser=" . $_POST['DatabaseUser'] . "&DatabasePass=" . $_POST['DatabasePass'] . "&DatabaseName=" . $_POST['DatabaseName']; $OUTPUT .= MakeSuccessBox("Connected to Database successfully", "Database mapped successfully. Click OK to start importing.", MakeAdminLink($popup_link), "", "", "import$ImportID~width=300,height=400,left=400,top=300"); } break; } } if ($Action == 'DoDatabaseImport') { $OUTPUT .= ''; $POPOUTPUT=1; include('includes/do_import_database.inc.php'); } if ($Action == 'DoEmailImport') { $OUTPUT .= ''; $POPOUTPUT=1; include('includes/do_import_email.inc.php'); } if(!$Action && !$ListID) { //select ListID form! $lists=mysql_query("SELECT * FROM " . $TABLEPREFIX . "lists ORDER BY ListName ASC"); while($l=mysql_fetch_assoc($lists)) { $l = stripslashes_array($l); if(AllowList($l['ListID'])) { if ($CURRENTADMIN['DisplaySummaries']) { $numSubs = (int)mysql_result(mysql_query("SELECT COUNT(*) from " . $TABLEPREFIX . "members where ListID=" . addslashes($l['ListID'])), 0, 0); if($numSubs == 1) $subs = "1 subscriber"; else $subs = number_format($numSubs, 0) . " subscribers"; $subs = " (" . $subs . ")"; } else { $subs = ""; } $alllists.=$l["ListID"]."->".str_replace(';', '$$SEMI$$', str_replace(':', '$$COLON$$', $l["ListName"]))."$subs;"; } } if ($alllists == '') { $OUTPUT = MakeBox("Import Subscribers",'No mailing lists have been created. Please click on the "Create Mailing List" button below to create one. '); } else { $FORM_ITEMS[$FORM_REQUIRED_ITEM . "Mailing List"]="select|ListID:5:$alllists"; $FORM_ITEMS["-1"]="submit|Next »:1-members"; $FORM=new AdminForm; $FORM->title="SelectList"; $FORM->items=$FORM_ITEMS; $FORM->action=MakeAdminLink("import?Action=ChooseDatasource"); $FORM->MakeForm("Import Details"); $FORM->output = "Before you can import subscribers, please choose a mailing list to work with." . $FORM->output; $OUTPUT.=MakeBox("Import Subscribers (Step 1 of 5)",$FORM->output); $OUTPUT .= ' '; } } ?> | |