. */ /** * @defgroup mail_capture automatic archiving of the e-mails */ /** * Mail capture is a Maarch module which allows you to archive * e-mails and attachments in Maarch with imap/pop3 protocol.
* We use the php_imap library.
* This Maarch module proposes a batch allowing the storing of e-mails.
* The module prepares the e-mails and their attachments for the Maarch AutoImport.
* @file * @author Loic Vinet * @author Laurent Giovannoni * @date $date$ * @version $Revision$ * @ingroup mail_capture * @brief Extraction of mails and attachements and archiving in Maarch */ //error mode and function //error_reporting(E_ERROR); //set_error_handler(errorHandler); // global var of the program /** * Path to the log file */ $_ENV['log'] = ""; /** * User exit of the program, contains 1 if any problem appears */ $_ENV['ErrorLevel'] = 0; /** * Connection object to database 1 */ $_ENV['db'] = ""; /** * Connection object to database 2 */ $_ENV['db2'] = ""; /** * Array of e-mails config */ $_ENV['emails'] = array(); /** * Current email login */ $_ENV['email_login'] = ''; /** * Creation of the log file */ function loginCreation() { if(!is_dir(dirname($_SERVER["PHP_SELF"]).DIRECTORY_SEPARATOR."log" .DIRECTORY_SEPARATOR)) { mkdir(dirname($_SERVER["PHP_SELF"]).DIRECTORY_SEPARATOR."log" .DIRECTORY_SEPARATOR."",0777); } if(!is_dir(dirname($_SERVER["PHP_SELF"]).DIRECTORY_SEPARATOR."log" .DIRECTORY_SEPARATOR."".date("Y")."_".date("m")."_".date("d") ."".DIRECTORY_SEPARATOR."")) { mkdir(dirname($_SERVER["PHP_SELF"]).DIRECTORY_SEPARATOR."log" .DIRECTORY_SEPARATOR."".date("Y")."_".date("m")."_".date("d") ."".DIRECTORY_SEPARATOR."",0777); } if(!is_dir(dirname($_ENV["tmp_directory"]).DIRECTORY_SEPARATOR)) { mkdir(dirname($_ENV["tmp_directory"]).DIRECTORY_SEPARATOR."",0777); } $folderlogname = dirname($_SERVER["PHP_SELF"]).DIRECTORY_SEPARATOR ."log".DIRECTORY_SEPARATOR."".date("Y")."_".date("m")."_" .date("d")."".DIRECTORY_SEPARATOR.""; $_ENV['log'] = $folderlogname . "mail_capture_" . date("H")."_" .date("i")."_".date("s") . ".log"; writeLog("Application start with : ".$_SERVER['SCRIPT_FILENAME']); } /** * Write on the log file * @param $eventInfo string text which is written in the log file */ function writeLog($eventInfo) { $log_file_opened = fopen($_ENV['log'], "a"); fwrite($log_file_opened, $eventInfo . "\r\n"); fclose($log_file_opened); } function emptydir($dirname) { $dhdl = opendir($dirname); while ($filename = readdir($dhdl)) { if($filename == "." || $filename == "..") continue; unlink($dirname . DIRECTORY_SEPARATOR . $filename); } } /** * Managing of errors * @param $errno integer number of the error * @param $errstr string text of the error * @param $errfile string file concerned with the error * @param $errline integer line of the error * @param $errcontext string context of the error */ function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) { $string = $errstr; $search1="'Bad message number'"; $search2="'disposition'"; preg_match($search1,$string,$out); $count=count($out[0]); if($count == 1) { $find1 = true; } preg_match($search2,$string,$out); $count=count($out[0]); if($count == 1) { $find2 = true; } if(!$find1 && !$find2) { writeLog("[ERROR] from line ".$errline." : ". $errstr." [ERROR]"); $_ENV['ErrorLevel'] = 1; } } // begin date_default_timezone_set('Europe/Paris'); if($argc != 2 ) { echo "You must specify the configuration file." . $argc; exit; } $conf = $argv[1]; $xmlconfig = simplexml_load_file($conf); foreach($xmlconfig->parameters as $parameters) { $_ENV["script_directory"] = $parameters->SCRIPT_DIRECTORY; $_ENV["tmp_directory"] = $parameters->TMP_DIRECTORY; $_ENV["preprocess_script"] = $parameters->PREPROCESS_SCRIPT; $_ENV["preprocess_xml_filepath"] = $parameters->PREPROCESS_XML_FILEPATH; $_ENV["postprocess_script"] = $parameters->POSTPROCESS_SCRIPT; $_ENV["postprocess_xml_filepath"] = $parameters->POSTPROCESS_XML_FILEPATH; $_ENV["debug_extract_files"] = $parameters->DEBUG_EXTRACT_FILES; $_ENV["delete_mail"] = $parameters->DELETE_MAIL; } $i=0; require_once("class_mailcapture.php"); if(DIRECTORY_SEPARATOR == "/") { $_ENV['osname'] = "UNIX"; } else { $_ENV['osname'] = "WINDOWS"; } loginCreation(); writeLog("Launch of mail capture"); writeLog("Loading the xml config file"); if($_ENV["preprocess_script"] <> "") { $returnTab = array(); //Execution du script de sortie writeLog("Launch of mail preprocess_script"); writelog("Execution of ".$_ENV["preprocess_script"]); include($_ENV["preprocess_script"]); } writeLog("Load Maarch Mail Capture core engine..."); //Ouverture de la class imap $mailcapture = new mailcapture(); //Connexion au comptes IMAP $mailboxes = array(); foreach($xmlconfig->mailbox as $mailbox) { $_ENV['email_login'] = (string) $mailbox->login; $id = (string) $mailbox->mailbox; $mailboxes[$id] = $mailcapture->captureMessages( (string) $mailbox->mailbox, (string) $mailbox->login, (string) $mailbox->password, (string) $mailbox->from_regex ); } if($_ENV["postprocess_script"] <> "") { $returnTab = array(); //Execution du script de sortie writelog("Execution of ".$_ENV["postprocess_script"]); include($_ENV["postprocess_script"]); } else { //Si aucun script utilisé en sortie (??), on affiche le tableau print_r($values); } writeLog("Return execution code : ".$_ENV['ErrorLevel']); writeLog("End of application !"); exit($_ENV['ErrorLevel']);