bulkfile = $bulk; if ($domain != null) $this -> setDomain($domain); if ($line_num != null) $this -> line_num = $line_num; else $this -> line_num = 1; $this -> seperator = $seperator; } private function setDomain($domain) { $this -> domain = $domain; $_SESSION["domain"] = $this -> domain; } public function get_CSV_delimitor() { $delimitor = array ( "comma" => ',', "semicolon" => ';', "colon" => ':', "tab" => "\t" ); return $delimitor; } // Validate the delimitor on CSV file public function checkForSeperator() { $fFile = fopen($this -> bulkfile["tmp_name"], 'r'); if ($fFile) $fFileline = fgetcsv($fFile,0,$this -> seperator); fclose ($fFile); // since we need compulsory two fields, the array length should be minimum two if (count($fFileline) < 2) return false; else return true; } public function check_for_CSV_file() { if (preg_match('/.+\.csv$/i', $this->bulkfile["name"])) return true; else return false; } /* To check for the duplication of Mail Box user name. The first row has to the column for username. */ public function checkDuplicateUname($fp) { $name = array(); $duplicateName = array(); $fHandle = fopen($fp,'r') or die("can't open file"); // to skip the heading (username, password) fgets($fHandle); while($csv_line = fgetcsv($fHandle,0,$this -> seperator)) { // to check whether username has been set if(isset($name[$csv_line[0]])) $duplicateName[$csv_line[0]] = $name[$csv_line[0]]; else $name[$csv_line[0]] = $csv_line[0]; } fclose($fHandle); return $duplicateName; } // function to read a CSV file, validate the fields and store the data in the session public function scanCsvfile() { global $PALANG; $errMsg = array(); $output = array(); $mailBox = array(); // To setup the file header $checkForSameUname = $this -> checkDuplicateUname($this -> bulkfile["tmp_name"]); $fp = fopen($this -> bulkfile["tmp_name"],'r') or die("can't open file"); $csvLine = $this -> getHeader($fp); if (!$csvLine) { return false; } $k = 0; $total = 0; $error = 0; while($csv_line = fgetcsv($fp,0,$this -> seperator)) { // Since we know the column for username and password, the validation should be out of for loop $csv_error_mailbox = $this -> validateCsvMailBox($csv_line[0],$k); if (isset($csv_error_mailbox)) { $errMsg = $this -> assignedError($csv_error_mailbox,$csv_line[0],$k); } else if (isset ($checkForSameUname[$csv_line[0]],$k)) { $errMsg = $this -> assignedError($PALANG['emailExistError'],$csv_line[0],$k+1); } // We can validate the password, but I think it would be OK to use the default password as it is given in CSV // as user should change their password. Moreover, It will be hectic for the administrator // to provide different password fulfilling the password requirement else if (!($this -> validateUnameOrPword($csv_line[0],$csv_line[1]))) { $errMsg = $this -> assignedError($PALANG['emailBlankError'],$csv_line[0],($k+1)); } else { foreach($csv_line as $i => $key) { // To store only the records required for the mail box if (isset($this -> fHeader[$i])) { $_SESSION["mailBox"][$k-$error][$this -> fHeader[$i]] = $key; } } } if (!empty($errMsg)) { $output [$k]['error'] = $errMsg[0]; $output [$k]['errorLine'] = $errMsg[1]; $output [$k]['errorMsg'] = $errMsg[2]; $error++; unset($errMsg); } // initialise the error value after assigning the error to output array $errMsg =''; $k++; } fclose($fp) or die("can't close file"); $output ['stats']['mailerror'] = $error; $output ['stats']['total'] = $k - $error; // To track the recods of email created // Email alies mail box is avaible without email (mail box), then it fail to create email if (isset($_SESSION["mailBox"])) { $_SESSION["mailboxTotal"] = count($_SESSION["mailBox"]); } return $output; } // For the email // This function checks for exisitance of blank username an password private function validateUnameOrPword($username,$password) { if (empty($username) || empty($password)) return false; else return true; } private function assignedError($errorMsg,$username,$lineNo) { //$error = "".$lineNo."".$username."".$errorMsg.""; $error = array(); $error[0] = $username; $error[1] = $lineNo; $error[2] = $errorMsg; return $error; } // The heading (header fields)follows strict pattern starting with the array index: // 0 - username (mandatory), // 1 - password (mandatory), // 2 - name (optional) // 3 - quota (optional) private function getHeader($fileCSV) { if ($fileCSV) $fheaderline = fgets($fileCSV); $header = explode($this -> seperator, $fheaderline); // Only the four important fields are taken into consideration if (count($header) > 4) $header = array_slice($header, 0,4); foreach ($header as $head => $row) { $headerRow[trim($row)] = trim($row); } $tblHeader = $this -> getColumn($headerRow); $set = array_diff($headerRow,$tblHeader); if(empty($set)) { foreach($tblHeader as $i => $key) { $this -> fHeader[ ] = $key; } return true; } else return false; } // Collecting the column names from database (mailBox table) private function getColumn($header) { $arrFields = array(); $tableName = 'mailbox'; $query = "SHOW COLUMNS FROM ".$tableName; $result = db_query ($query); while( $row = db_array($result['result']) ) { if(isset($header[trim($row[0])])) { $arrFields[trim( $row[0] )] = trim( $row[0] ); } } return $arrFields; } private function validateCsvMailBox($csv_mailBox,$line_num) { global $PALANG; if( $this -> validateProposedMailBox($csv_mailBox) > 0) return $PALANG['InvalidMailError']; if($this -> checkForExisitingMailBox($csv_mailBox)) return $PALANG['InvalidEmailExisit']; } // This function checks for the possible spaces in-between the email addrees in the uploaded CSV file // In the CSV file, we don't request user to add @ in user name as by default it gets assigned. // To DO: would like to implement check_email function but due to the flash error which specifically for single // mail box creation, it may not be competible. private function validateProposedMailBox($csv_mailBox) { return (strlen(preg_replace('|[^ ]|', '', $csv_mailBox))); } // check for exisiting mail box private function checkForExisitingMailBox($csv_mailBox) { $csv_mailBox = escape_string($csv_mailBox .'@'.$this -> domain); // Thanks it works $mh = new MailboxHandler($csv_mailBox); return $mh->view(); } // Function name: createCsvMailBox creates a mail box from the uploaded csv file. In order to skip the maximum execution time // It will run every before 5 seconds from maximum excution time and restart again until it completes creating the mail boxes. public function createCsvMailBox() { $startTime = microtime(true); $active = true; $mail = false; $quota = 0; $name =''; $MailArray =& $_SESSION["mailBox"]; foreach ($MailArray as $i => $mailbox) { if (isset ($mailbox['quota'])) $quota = $mailbox['quota']; if (isset ($mailbox['name'])) $name = $mailbox['name']; $address = escape_string($mailbox['username'] .'@'.$_SESSION["domain"]); $password = escape_string($mailbox['password']); $AddMailBoxHandler = new MailboxHandler($address); if (!$AddMailBoxHandler->add($password, $name, $quota, $active, $mail)) { // To store errors $_SESSION['errorMail'] = $AddMailBoxHandler->errormsg; } unset($_SESSION["mailBox"][$i]); $endTime = microtime(true); $execTime = $endTime - $startTime; //The maximum time to create email is 2.1 seconds in my case but the processing of other statement do takes few seconds. if ((int)($execTime) >= (int)(ini_get("max_execution_time") - 5)) { // chances that the all the mailbox may get created after attending the the execution time set above if(count ($_SESSION["mailBox"]) > 0) { // Display the message where the browser is redirected after maximum execution time (in function.inc.php file) informProgress('executing'); print ""; } } } } } ?>