/** * */ function importUsers($task) { $csv = UserImport::getPath2Csv(); if (empty($csv)) { echo '<form enctype="multipart/form-data" ' . 'action="index2.php" method="POST" ' . 'name="adminForm"><input type="hidden" ' . 'name="MAX_FILE_SIZE" value="3000000"/>' . '<input name="csvfile" type="file" ' . 'title="CSV File to import"/><input type="hidden" ' . 'name="option" value="com_importusers" />' . '<input type="hidden" name="task" value="' . $task . '" />' . '<input type="submit" value="Upload+Process"/></form>'; } else { echo "Using csv uploaded before: {$csv}" . "<br/>"; echo '<form ' . 'action="index2.php" method="POST" ' . 'name="adminForm"><input type="hidden" ' . 'name="option" value="com_importusers" />' . '<input type="hidden" name="task" value="' . $task . '" />'; } }
function import($mode) { //keeps all output till end of processing $num_records = 0; global $From, $subject, $path_to_csv; if ($mode == REAL_IMPORT) { echo "Mode: Actual import<br/>"; } else { echo "Mode: Test import<br/>"; } global $old_id, $new_id; // Set up connection to Mambo database global $connection; if ($connection == null) { $connection = mysql_connect(UserImport::getHost(), UserImport::getDbUser(), UserImport::getDbPass()) or die('Not connected to Mambo database: ' . mysql_error()); } echo "Connected to Mambo Database at: " . UserImport::getHost() . "." . UserImport::getDb(); mysql_select_db(UserImport::getDb(), $connection) or die('Can\'t select Mambo database ' . $mambo_database_name . ': ' . mysql_error()); global $mos_users; $mos_users = UserImport::getdbPrefix() . "users"; global $mos_core_acl_aro; $mos_core_acl_aro = UserImport::getdbPrefix() . "core_acl_aro"; global $mos_core_acl_groups_aro_map; $mos_core_acl_groups_aro_map = UserImport::getdbPrefix() . "core_acl_groups_aro_map"; global $mos_iu_csvfiles; $mos_iu_csvfiles = UserImport::getdbPrefix() . "iu_csvfiles"; // Create new starting "id" number for mos_users table by // getting the current highest id number and adding 1 $current_id = mysql_query("SELECT MAX(id) FROM {$mos_users}", $connection); while ($row = mysql_fetch_array($current_id)) { $old_id = $row[0]; $new_id = $old_id + 1; echo "<p>Current highest 'id' in " . UserImport::getdbPrefix() . "users is: " . $row[0] . " <br>The new 'id' numbers will start at: " . $new_id . "<br>"; } //get the highest csv file id $current_file_id = mysql_query("SELECT MAX(id) FROM {$mos_iu_csvfiles}", $connection); //Store the current csvfile if (UserImport::getPath2Csv() == null) { $path_to_csv = mysql_real_escape_string($path_to_csv, $connection); mysql_query("INSERT INTO {$mos_iu_csvfiles} (id, name, active)VALUES ('{$current_file_id}', '{$path_to_csv}', '1')", $connection) or die("<br> Mos_iu_csvfiles not updated. Error is: " . mysql_error()); } // open the .csv file $handle = fopen(UserImport::getPath2Csv(), "r"); while ($data = fgetcsv($handle, 1000, UserImport::getDelimiter())) { // Here we assign some values for the SQL statements // We have to escape the name strings so the SQL doesn't throw errors when it sees // names with embedded quotes like "O'Brien" $username = mysql_escape_string($data[0]); $name = mysql_escape_string($data[1]); $email = $data[2]; $registerdate = date("Y-m-d H:i:s"); /* creates a new password for each user so it can be included in the email*/ $clearpassword = $username . date("si"); $password = md5($clearpassword); $myRows = mysql_query("SELECT COUNT(*) from {$mos_users} WHERE username = '******'", $connection) or die("<br> Error getting Count. Error is: " . mysql_error()); $dupCountRow = mysql_fetch_row($myRows); switch ($mode) { case TEST_IMPORT: $myRows = mysql_query("SELECT COUNT(*) from {$mos_users} WHERE username = '******'", $connection) or die("<br> Error getting Count. Error is: " . mysql_error()); $dupCountRow = mysql_fetch_row($myRows); // DUP Check if ($dupCountRow[0] > 0) { echo "User Already exists : {$username}<br>"; } else { echo "<br> In testing mode: User " . $name . " would have been added to Mambo CMS"; $num_records++; } break; case REAL_IMPORT: // - first check if user exist $myRows = mysql_query("SELECT COUNT(*) from {$mos_users} WHERE username = '******'", $connection) or die("<br> Error getting Count. Error is: " . mysql_error()); $dupCountRow = mysql_fetch_row($myRows); // DUP Check if ($dupCountRow[0] > 0) { echo "User Already exists : {$username}<br>"; } else { // mysql_query("INSERT ..."); // This is where the existing script INSERT stmts and such go, don't forget to close the IF mysql_query("INSERT INTO {$mos_users} (id, name, username, email, password , usertype, block, sendEmail, gid, registerDate, lastvisitDate)\r\n\t\t\t\t\t\t\t\tVALUES ('{$new_id}', '{$name}', '{$username}', '{$email}', '{$password}', 'registered', '0', '0', '18', '{$registerdate}', '0000-00-00 00:00:00')", $connection) or die("<br> Mos_users not updated. Error is: " . mysql_error()); mysql_query("INSERT INTO {$mos_core_acl_aro} (aro_id , section_value , value , order_value , name , hidden) \r\n\t\t\t\t\t\t\t\tVALUES ('{$new_id}', 'users', '{$new_id}', '0', '{$name}', '0')", $connection) or die("<br> Mos_core_acl_aro not updated. Error is: " . mysql_error()); mysql_query("INSERT INTO {$mos_core_acl_groups_aro_map} (group_id , section_value , aro_id) \r\n\t\t\t\t\t\t\t\tVALUES ('18', '', '{$new_id}')", $connection) or die("<br> Mos_core_acl_groups_aro_map not updated. Error is: " . mysql_error()); echo "<br> User " . $name . " added to Mambo CMS"; $num_records++; //TODO: this needs to be changed to mambo api mail calls // Beginning of code to send confirmation email /* recipient */ $to = "{$email}"; /* subject */ //$subject = "Your account in our website"; //This is the email - Insert what you want to say below - Title tags is the subject field on the email// /* message */ $message = importScreens::notification(); $message = str_replace('{NAME}', $name, $message); $message = str_replace('{USERNAME}', $username, $message); $message = str_replace('{PASSWORD}', $clearpassword, $message); /* To send HTML mail, you can set the Content-type header. */ $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; /* additional headers - Enter who the email is from and email address*/ $headers .= "To: " . $name . " <" . $email . ">\r\n"; $headers .= "From:" . $From . "\r\n"; /* and now mail it */ mail($to, $subject, $message, $headers); // End of of code to send a confirmation email } // end else - username didn't exist in database so we wrote a new record break; default: echo "<br> \$mode variable not set properly: User " . $name . " would have been added to Mambo CMS"; } // end switch ($mode) // increment id counter $new_id++; } // end while ($data = fgetcsv ($handle, 1000, $csv_delimiter)) fclose($handle); if ($mode == REAL_IMPORT) { mysql_query("UPDATE {$mos_iu_csvfiles} SET active='0' \r\n\t\t\t\t\t\t\t\tWHERE active='1'", $connection) or die("<br> Mos_iu_csvfiles not updated. Error is: " . mysql_error()); //echo "Database csv name updated <br/>"; //$num_records = $new_id - $old_id - 1; echo "<br/>" . $num_records . " users added to the Mambo Database"; } else { echo "<br/>" . $num_records . " users would have been added to the Mambo Database"; } mysql_close($connection); echo "<br/><br/>"; echo "<br> Processing complete at: " . "{$registerdate}"; importScreens::init('new'); }
public function getInstance($record, CsvImportProfile $profile) { $instance = parent::getInstance($record, $profile); $e = $instance->isExistingRecord(); if ($this->allowOnly == self::CREATE && $e == true) { throw new Exception('Record exists'); } if ($this->allowOnly == self::UPDATE && $e == false) { throw new Exception('Record not found'); } return $instance; }