From 223bd339cd5e9bf899d9f007ddb9c3e08b8ee36c Mon Sep 17 00:00:00 2001 From: Alex Orluc <alex.orluc@maarch.org> Date: Mon, 12 Dec 2016 14:40:55 +0000 Subject: [PATCH] FIX #4966 refactor logs --- sendmail/trunk/batch/batch_tools.php | 49 +++++++++-------- sendmail/trunk/batch/load_process_emails.php | 6 ++- sendmail/trunk/batch/process_emails.php | 55 ++++++++++++-------- 3 files changed, 63 insertions(+), 47 deletions(-) diff --git a/sendmail/trunk/batch/batch_tools.php b/sendmail/trunk/batch/batch_tools.php index 8ba799b4070..f91372d974b 100644 --- a/sendmail/trunk/batch/batch_tools.php +++ b/sendmail/trunk/batch/batch_tools.php @@ -1,7 +1,7 @@ <?php /* - * Copyright 2008-2011 Maarch + * Copyright 2008-2015 Maarch * * This file is part of Maarch Framework. * @@ -39,12 +39,16 @@ */ function Bt_doQuery($dbConn, $queryTxt, $param=array(), $transaction=false) { - $stmt = $dbConn->query($queryTxt, $param, true); - //$stmt = $dbConn->query($queryTxt); + if (count($param) > 0) { + $stmt = $dbConn->query($queryTxt, $param); + } else { + $stmt = $dbConn->query($queryTxt); + } + if (!$stmt) { if ($transaction) { $GLOBALS['logger']->write('ROLLBACK', 'INFO'); - $dbConn->query('ROLLBACK', true); + $dbConn->query('ROLLBACK'); } Bt_exitBatch( 104, 'SQL Query error:' . $queryTxt @@ -81,13 +85,12 @@ function Bt_exitBatch($returnCode, $message='') fclose($semaphore); } $GLOBALS['logger']->write($message, 'ERROR', $returnCode); - Bt_logInDataBase($GLOBALS['totalProcessedResources'], 1, 'return code:' - . $returnCode . ', ' . $message); + Bt_logInDataBase($GLOBALS['totalProcessedResources'], 1, $message.' (return code: '. $returnCode.')'); } elseif ($message <> '') { $GLOBALS['logger']->write($message, 'INFO', $returnCode); - Bt_logInDataBase($GLOBALS['totalProcessedResources'], 0, 'return code:' - . $returnCode . ', ' . $message); + Bt_logInDataBase($GLOBALS['totalProcessedResources'], 0, $message.' (return code: '. $returnCode.')'); } + Bt_updateWorkBatch(); exit($returnCode); } @@ -99,12 +102,10 @@ function Bt_exitBatch($returnCode, $message='') */ function Bt_logInDataBase($totalProcessed=0, $totalErrors=0, $info='') { - $query = "insert into history_batch (module_name, batch_id, event_date, " - . "total_processed, total_errors, info) values('" - . $GLOBALS['batchName'] . "', " . $GLOBALS['wb'] . ", " - . $GLOBALS['db']->current_datetime() . ", " . $totalProcessed . ", " . $totalErrors . ", '" - . $GLOBALS['func']->protect_string_db(substr(str_replace('\\', '\\\\', str_replace("'", "`", $info)), 0, 999)) . "')"; - $stmt = Bt_doQuery($GLOBALS['db'], $query); + $query = "INSERT INTO history_batch (module_name, batch_id, event_date, " + . "total_processed, total_errors, info) values(?, ?, CURRENT_TIMESTAMP, ?, ?, ?)"; + $arrayPDO = array($GLOBALS['batchName'], $GLOBALS['wb'], $totalProcessed, $totalErrors, substr(str_replace('\\', '\\\\', str_replace("'", "`", $info)), 0, 999)); + $GLOBALS['db']->query($query, $arrayPDO); } /** @@ -114,16 +115,15 @@ function Bt_logInDataBase($totalProcessed=0, $totalErrors=0, $info='') */ function Bt_getWorkBatch() { - $req = "select param_value_int from parameters where id = " - . "'". $GLOBALS['batchName'] . "_id'"; - $stmt = $GLOBALS['db']->query($req); - while ($reqResult = $stmt->fetch(PDO::FETCH_ASSOC)) { - $GLOBALS['wb'] = $reqResult[0] + 1; + $req = "SELECT param_value_int FROM parameters WHERE id = ? "; + $stmt = $GLOBALS['db']->query($req, array($GLOBALS['batchName']."_id")); + + while ($reqResult = $stmt->fetchObject()) { + $GLOBALS['wb'] = $reqResult->param_value_int + 1; } if ($GLOBALS['wb'] == '') { - $req = "insert into parameters(id, param_value_int) values " - . "('" . $GLOBALS['batchName'] . "_id', 1)"; - $stmt = $GLOBALS['db']->query($req); + $req = "INSERT INTO parameters(id, param_value_int) VALUES (?, 1)"; + $GLOBALS['db']->query($req, array($GLOBALS['batchName']."_id")); $GLOBALS['wb'] = 1; } } @@ -135,9 +135,8 @@ function Bt_getWorkBatch() */ function Bt_updateWorkBatch() { - $req = "update parameters set param_value_int = " . $GLOBALS['wb'] . " " - . "where id = '" . $GLOBALS['batchName'] . "_id'"; - $GLOBALS['db']->query($req); + $req = "UPDATE parameters SET param_value_int = ? WHERE id = ?"; + $GLOBALS['db']->query($req, array($GLOBALS['wb'], $GLOBALS['batchName']."_id")); } /** diff --git a/sendmail/trunk/batch/load_process_emails.php b/sendmail/trunk/batch/load_process_emails.php index a7c84c7c407..82e1bac8dfb 100644 --- a/sendmail/trunk/batch/load_process_emails.php +++ b/sendmail/trunk/batch/load_process_emails.php @@ -62,7 +62,7 @@ $log4PhpEnabled = false; // Open Logger $GLOBALS['logger'] = new Logger4Php(); -$GLOBALS['logger']->set_threshold_level('DEBUG'); +$GLOBALS['logger']->set_threshold_level('INFO'); $logFile = 'logs' . DIRECTORY_SEPARATOR . date('Y-m-d_H-i-s') . '.log'; @@ -120,6 +120,7 @@ if (!file_exists($GLOBALS['configFile'])) { 'Configuration file ' . $GLOBALS['configFile'] . ' does not exist', 'ERROR', 102 ); + echo "\nConfiguration file " . $GLOBALS['configFile'] . " does not exist ! \nThe batch cannot be launched !\n\n"; exit(102); } @@ -159,8 +160,11 @@ if ((string) $log4phpParams->enabled == 'true') { $GLOBALS['logger']->set_log4PhpBusinessCode((string) $log4phpParams->Log4PhpBusinessCode); $GLOBALS['logger']->set_log4PhpConfigPath((string) $log4phpParams->Log4PhpConfigPath); $GLOBALS['logger']->set_log4PhpBatchName($GLOBALS['batchName']); +}else{ + echo "\n/!\ WARNING /!\ LOG4PHP is disabled ! Informations of batch process will not show !\n\n"; } + // Mailer $mailerParams = $xmlconfig->MAILER; $path_to_mailer = (string)$mailerParams->path_to_mailer; diff --git a/sendmail/trunk/batch/process_emails.php b/sendmail/trunk/batch/process_emails.php index 93304261b32..618bdbdb2be 100644 --- a/sendmail/trunk/batch/process_emails.php +++ b/sendmail/trunk/batch/process_emails.php @@ -32,6 +32,8 @@ while ($state <> 'END') { $GLOBALS['emails'][] = $emailRecordset; } $state = 'SEND_AN_EMAIL'; + $err = 0; + $errTxt = ''; break; /**********************************************************************/ @@ -60,17 +62,19 @@ while ($state <> 'END') { //echo 'userInfo : ' . $userInfo['mail'] . '==' . ' sender_email : ' . $email->sender_email . PHP_EOL; if ($userInfo['mail'] == $email->sender_email) { if (!empty($mailfrom_generic)) { - $GLOBALS['logger']->write("Sending e-mail from : " - . '"' . $userInfo['firstname'].' ' .$userInfo['lastname'] - . '" <'.$mailfrom_generic.'>', 'INFO'); + + + $GLOBALS['logger']->write('process e-mail '.($currentEmail+1)."/".$totalEmailsToProcess.' (FROM => '.$userInfo['firstname'].' '.$userInfo['lastname'].' <'.$mailfrom_generic.'>'.', TO => '.$email->to_list.', SUBJECT => '.$email->email_object.', CC =>'.$email->cc_list.', CCI => '.$email->cci_list.') ...', 'INFO'); + $GLOBALS['mailer']->setFrom($userInfo['firstname'].' ' . $userInfo['lastname'].' <'.$mailfrom_generic.'> '); + $email->email_body = 'Courriel envoyé par : ' . $userInfo['firstname'].' ' . $userInfo['lastname'] . ' ' . $email->sender_email . ' ' . '.<br/><br/>' . $email->email_body; } else { - $GLOBALS['logger']->write("Sending e-mail from : " - . '"' . $userInfo['firstname'].' ' .$userInfo['lastname'] - . '" <'.$email->sender_email.'>', 'INFO'); + + $GLOBALS['logger']->write('process e-mail '.($currentEmail+1)."/".$totalEmailsToProcess.' (FROM => '.$userInfo['firstname'].' '.$userInfo['lastname'].' <'.$email->sender_email.'>'.', TO => '.$email->to_list.', SUBJECT => '.$email->email_object.', CC =>'.$email->cc_list.', CCI => '.$email->cci_list.') ...', 'INFO'); + $GLOBALS['mailer']->setFrom($userInfo['firstname'].' ' . $userInfo['lastname'].' <'.$email->sender_email.'> '); } @@ -80,16 +84,13 @@ while ($state <> 'END') { if (!empty($mailfrom_generic)) { $mailsEntities = $sendmail_tools->getAttachedEntitiesMails(); $entityShortLabel = substr($mailsEntities[$email->sender_email], 0, strrpos($mailsEntities[$email->sender_email], "(")); - $GLOBALS['logger']->write("Sending e-mail from : " . $entityShortLabel - . ' <' . $mailfrom_generic . '>', 'INFO'); $GLOBALS['mailer']->setFrom($entityShortLabel . ' <' . $mailfrom_generic. '> '); $email->email_body = 'Courriel envoyé par : ' . $entityShortLabel . ' ' . $sendmail_tools->explodeSenderEmail($email->sender_email) . ' ' . '.<br/><br/>' . $email->email_body; } else { $mailsEntities = $sendmail_tools->getAttachedEntitiesMails(); $entityShortLabel = substr($mailsEntities[$email->sender_email], 0, strrpos($mailsEntities[$email->sender_email], "(")); - $GLOBALS['logger']->write("Sending e-mail from : " . $entityShortLabel - . ' <' . $sendmail_tools->explodeSenderEmail($email->sender_email) . '>', 'INFO'); + $GLOBALS['mailer']->setFrom($entityShortLabel . ' <' . $sendmail_tools->explodeSenderEmail($email->sender_email) . '> '); } $GLOBALS['mailer']->setReplyTo($sendmail_tools->explodeSenderEmail($email->sender_email)); @@ -97,7 +98,6 @@ while ($state <> 'END') { //echo $email->email_body . PHP_EOL;exit; - $GLOBALS['logger']->write("Sending e-mail to : " . $email->to_list, 'INFO'); if (!empty($email->cc_list))$GLOBALS['logger']->write("Copy e-mail to : " . $email->cc_list, 'INFO'); if (!empty($email->cci_list))$GLOBALS['logger']->write("Copy invisible e-mail to : " . $email->cci_list, 'INFO'); @@ -119,7 +119,6 @@ while ($state <> 'END') { $GLOBALS['mailer']->setBcc($email->cci_list); } //--> Set subject - $GLOBALS['logger']->write("Subject : " . $email->email_object, 'INFO'); $GLOBALS['mailer']->setSubject($email->email_object); //--> Set body: Is Html/raw text ? if ($email->is_html == 'Y') { @@ -139,7 +138,7 @@ while ($state <> 'END') { //--> Set attachments //Res master if ($email->is_res_master_attached == 'Y') { - $GLOBALS['logger']->write("Set attachment on res master : " . $email->res_id, 'INFO'); + $GLOBALS['logger']->write("set attachment on res master : " . $email->res_id, 'INFO'); //Get file from docserver $resFile = $sendmail_tools->getResource($GLOBALS['collections'], $email->coll_id, $email->res_id); @@ -147,7 +146,7 @@ while ($state <> 'END') { if(is_file($resFile['file_path'])) { //Filename $resFilename = $sendmail_tools->createFilename($resFile['label'], $resFile['ext']); - $GLOBALS['logger']->write("Set attachment filename : " . $resFilename, 'INFO'); + $GLOBALS['logger']->write("set attachment filename : " . $resFilename, 'INFO'); //File content $file_content = $GLOBALS['mailer']->getFile($resFile['file_path']); @@ -160,7 +159,7 @@ while ($state <> 'END') { if (!empty($email->res_version_id_list)) { $version = explode(',', $email->res_version_id_list); foreach($version as $version_id) { - $GLOBALS['logger']->write("Set attachment for version : " . $version_id, 'INFO'); + $GLOBALS['logger']->write("set attachment for version : " . $version_id, 'INFO'); $versionFile = $sendmail_tools->getVersion( $GLOBALS['collections'], $email->coll_id, @@ -170,7 +169,7 @@ while ($state <> 'END') { if(is_file($versionFile['file_path'])) { //Filename $versionFilename = $sendmail_tools->createFilename($versionFile['label'], $versionFile['ext']); - $GLOBALS['logger']->write("Set attachment filename for version : " . $versionFilename, 'INFO'); + $GLOBALS['logger']->write("set attachment filename for version : " . $versionFilename, 'INFO'); //File content $file_content = $GLOBALS['mailer']->getFile($versionFile['file_path']); @@ -184,7 +183,7 @@ while ($state <> 'END') { if (!empty($email->res_attachment_id_list)) { $attachments = explode(',', $email->res_attachment_id_list); foreach($attachments as $attachment_id) { - $GLOBALS['logger']->write("Set attachment on res attachment : " . $attachment_id, 'INFO'); + $GLOBALS['logger']->write("set attachment on res attachment : " . $attachment_id, 'INFO'); $attachmentFile = $sendmail_tools->getAttachment( $email->coll_id, $email->res_id, @@ -193,7 +192,7 @@ while ($state <> 'END') { if(is_file($attachmentFile['file_path'])) { //Filename $attachmentFilename = $sendmail_tools->createFilename($attachmentFile['label'], $attachmentFile['ext']); - $GLOBALS['logger']->write("Set attachment filename : " . $attachmentFilename, 'INFO'); + $GLOBALS['logger']->write("set attachment filename : " . $attachmentFilename, 'INFO'); //File content $file_content = $GLOBALS['mailer']->getFile($attachmentFile['file_path']); @@ -217,14 +216,19 @@ while ($state <> 'END') { //Now send the mail + $GLOBALS['logger']->write("sending e-mail ...", 'INFO'); $return = $GLOBALS['mailer']->send($to, (string)$mailerParams->type); if( ($return == 1 && ((string)$mailerParams->type == "smtp" || (string)$mailerParams->type == "mail" )) || ($return == 0 && (string)$mailerParams->type == "sendmail")) { $exec_result = 'S'; + $GLOBALS['logger']->write("e-mail sent.", 'INFO'); } else { //$GLOBALS['logger']->write("Errors when sending message through SMTP :" . implode(', ', $GLOBALS['mailer']->errors), 'ERROR'); - $GLOBALS['logger']->write("Errors when sending message through SMTP :" . $GLOBALS['mailer']->errors[0].': '.$GLOBALS['mailer']->errors[1], 'ERROR'); + $GLOBALS['logger']->write("SENDING EMAIL ERROR ! (" . $return[0].")", 'ERROR'); + $GLOBALS['logger']->write("e-mail not sent !", 'ERROR'); $exec_result = 'E'; + $err++; + $errTxt = ' (Last Error : '.$return[0].')'; } //Update emails table $query = "UPDATE " . EMAILS_TABLE @@ -241,10 +245,19 @@ while ($state <> 'END') { } } -$GLOBALS['logger']->write('End of process', 'INFO'); +$emailSent = $totalEmailsToProcess - $err; + +$GLOBALS['logger']->write($emailSent.' email(s) sent', 'INFO'); +$GLOBALS['logger']->write('end of process', 'INFO'); + Bt_logInDataBase( - $totalEmailsToProcess, 0, 'process without error' + $totalEmailsToProcess, $err, $emailSent.' email(s) sent'.$errTxt ); +Bt_updateWorkBatch(); + +//clean tmp directory +echo "clean tmp path ....\n"; +array_map('unlink', glob($_SESSION['config']['tmppath']."/*")); //unlink($GLOBALS['lckFile']); exit($GLOBALS['exitCode']); -- GitLab