Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static $checkMailTicket;
- ________________________________________________________
- public static function getCheckMailTicket()
- {
- self::init();
- if (self::$checkMailTicket) {
- return self::$checkMailTicket;
- }
- $checkMailTicket = 90; // Parameter::getGlobalParameter('cronCheckMailTicket');
- if (!$checkMailTicket) {
- $checkMailTicket = 5 * 60;
- } // Default=every 5 mn
- self::$checkMailTicket = $checkMailTicket;
- return self::$checkMailTicket;
- }
- ________________________________________________________
- $cronCheckMailTicket = self::getCheckMailTicket(); // PARAMETRE TICKET
- ________________________________________________________
- // CheckEmailsTicket : automatically import new tickets from mails
- if ($cronCheckMailTicket > 0) {
- $cronCheckMailTicket -= $cronSleepTime;
- if ($cronCheckMailTicket <= 0) {
- try {
- self::checkMailTicket();
- } catch (Exception $e) {
- traceLog("Cron::run() - Error on checkNewMailTicket()");
- }
- $cronCheckMailTicket = Cron::getCheckMailTicket();
- }
- }
- ________________________________________________________
- /* ==============================================================================
- ==================== CRÉATION DE TICKET PAR LES MAILS =========================
- =============================================================================== */
- public static function checkMailTicket()
- {
- self::init();
- global $globalCronMode, $globalCatchErrors;
- $globalCronMode = true;
- $globalCatchErrors = true;
- require_once("../model/ImapMailbox.php"); // Imap management Class
- if (!ImapMailbox::checkImapEnabled()) {
- traceLog("ERROR - Cron::checkMailTicket() - IMAP extension not enabled in your PHP config. Cannot connect to IMAP Mailbox.");
- return;
- }
- $checkMailTicket = 90; // Parameter::getGlobalParameter('cronCheckMailTicket');
- if (!$checkMailTicket or intval($checkMailTicket) <= 0) {
- return; // disabled
- }
- // IMAP must be enabled in Google Mail Settings
- $emailEmail = Parameter::getGlobalParameter('cronCheckEmailsUser');
- $emailPassword = Parameter::getGlobalParameter('cronCheckEmailsPassword');
- //$emailAttachmentsDir=dirname(__FILE__) . '/../files/attach';
- $paramAttachDir = Parameter::getGlobalParameter('paramAttachmentDirectory');
- $pathSeparator = Parameter::getGlobalParameter('paramPathSeparator');
- if (substr($paramAttachDir, 0, 2) == '..') {
- $curdir = dirname(__FILE__);
- $paramAttachDir = str_replace(array('/model', '\model'), array('', ''), $curdir) . substr($paramAttachDir, 2);
- }
- $emailAttachmentsDir = ((substr($paramAttachDir, -1) == '\\' or substr($paramAttachDir, -1) == "/") ? $paramAttachDir : $paramAttachDir . $pathSeparator) . 'emails';
- if (!file_exists($emailAttachmentsDir)) {
- mkdir($emailAttachmentsDir, 0777, true);
- }
- $emailHost = Parameter::getGlobalParameter('cronCheckEmailsHost'); // {imap.gmail.com:993/imap/ssl}INBOX';
- if (!$emailHost) {
- traceLog("IMAP connection string not defined");
- return;
- }
- $imapServerEncoding = Parameter::getGlobalParameter('imapFilterCriteria');
- if (!$imapServerEncoding) {
- $imapServerEncoding = 'utf-8';
- }
- $mailbox = new ImapMailbox($emailHost, $emailEmail, $emailPassword, $emailAttachmentsDir, $imapServerEncoding);
- $mails = array();
- // Get some mail
- //$mailsIds = $mailbox->searchMailBox('UNSEEN UNDELETED');
- $imapFilterCriteria = Parameter::getGlobalParameter('imapFilterCriteria');
- if (!$imapFilterCriteria) {
- $imapFilterCriteria = 'UNSEEN UNDELETED';
- }
- $mailsIds = $mailbox->searchMailBox($imapFilterCriteria);
- if (!$mailsIds) {
- debugTraceLog("Mailbox is empty (filter='$imapFilterCriteria')"); // Will be a debug level trace
- return;
- }
- foreach ($mailsIds as $mailId) {
- $mail = $mailbox->getMail($mailId);
- $mailbox->markMailAsUnread($mailId);
- debugTraceLog('===============================');
- debugTraceLog("Message read (id $mailId), from $mail->fromAddress");
- $body = $mail->textPlain;
- $bodyHtml = $mail->textHtml;
- if (!$body and $bodyHtml) {
- $toText = new Html2Text($bodyHtml);
- $body = $toText->getText();
- }
- $class = "ticket"; // INITIALISÉ A TICKET CAR UN VEUT CRÉER UN NV TICKET
- $msg = null;
- $senderId = null;
- // Search end of Message (this is valid for text only, treatment of html messages would require other code)
- $posEndMsg = strpos($body, "\r\n>"); // Search for Thunderbird and Gmail
- if ($posEndMsg) {
- $posEndMsg = strrpos(substr($body, 0, $posEndMsg - 20), "\r\n");
- } else {
- $posEndMsg = strpos($body, "\n>");
- }
- if (!$posEndMsg) { // Search for outlook
- preg_match('/<.*?@.*?> [\r\n]/', $body, $matches);
- if (count($matches) > 0) {
- $posEndMsg = strpos($body, $matches[0]);
- $posEndMsg = strrpos(substr($body, 0, $posEndMsg - 2), "\r\n");
- }
- }
- if (!$posEndMsg) {
- $posEndMsg = strpos($body, "\r\n\r\n\r\n");
- if (!$posEndMsg) {
- $posEndMsg = strpos($body, "\n\n\n");
- }
- }
- if ($posEndMsg) {
- $msg = substr($body, 0, $posEndMsg);
- }
- if (!trim($msg)) { // Message not received with previous methods, try another one
- $posEndMsg = strrpos(substr($body, 0), "\n");
- $posDe = strrpos(substr($body, 0, $posEndMsg), "De : ");
- if ($posDe > 2) {
- $posEndMsg = $posDe - 1;
- } else {
- $posDe = strrpos(substr($body, 0, $posEndMsg), "From : ");
- if ($posDe > 2) {
- $posEndMsg = $posDe - 1;
- }
- }
- $msg = substr($body, 0, $posEndMsg);
- }
- // Remove unexpected "tags" // Valid as long as we treat emails as text
- $msg = preg_replace('/<mailto.*?\>/', '', $msg);
- $msg = preg_replace('/<http.*?\>/', '', $msg);
- $msg = preg_replace('/<#[A-F0-9\-]*?\>/', '', $msg);
- $msg = str_replace(" \r\n", "\r\n", $msg);
- $msg = str_replace(" \r\n", "\r\n", $msg);
- $msg = str_replace("\r\n\r\n\r\n", "\r\n\r\n", $msg);
- $msg = str_replace("\r\n\r\n\r\n", "\r\n\r\n", $msg);
- $msg = str_replace(" \n", "\n", $msg);
- $msg = str_replace(" \n", "\n", $msg);
- $msg = str_replace("\n\n\n", "\n\n", $msg);
- $msg = str_replace("\n\n\n", "\n\n", $msg);
- // Sender
- $sender = $mail->fromAddress;
- $crit = array('email' => $sender);
- $usr = new Affectable();
- $usrList = $usr->getSqlElementsFromCriteria($crit, false, null, 'idle asc, isUser desc, isResource desc');
- if (count($usrList)) {
- $senderId = $usrList[0]->id;
- $senderObj = $usrList[0];
- }
- debugTraceLog("User corresponding to email address is #$senderId");
- if (!$senderId) {
- traceLog("Email message received from '$sender', not recognized as resource or user or contact : message not stored as ticket to avoid spamming");
- $mailbox->markMailAsUnread($mailId);
- continue;
- }
- $arrayFrom = array("\n", "\r", " ");
- $arrayTo = array("", "", "");
- $class = str_replace($arrayFrom, $arrayTo, $class);
- $obj = null;
- if (SqlElement::class_exists($class)) {
- $obj = new $class();
- debugTraceLog("Message identified as a message for a new $class");
- }
- if (!trim($msg)) {
- traceLog("Could not retreive body of the mail (empty response) from '$sender' mail concerning a new $class");
- debugTraceLog($body);
- $mailbox->markMailAsRead($mailId);
- continue;
- }
- if ($obj and $senderId) {
- $pjc = 592; // ID of the project "TO DEFINE"
- // Getting sender affectations
- if($senderObj->isUser == 1){ // isUser
- $critere = array('idUser' => $senderId);
- debugTraceLog("Sender $sender identified as a User");
- }
- else{ // isContact
- $critere = array('idContact' => $senderId);
- debugTraceLog("Sender $sender identified as a Contact");
- }
- if($critere){
- $affectation = new Affectation();
- $affectationList = $affectation->getSqlElementsFromCriteria($critere, false, null);
- if (count($affectationList) == 1) {
- $pjc = $affectationList[0]->idProject;
- debugTraceLog("Project identified : $pjc");
- }
- else{
- debugTraceLog("Project could not be identified ...");
- }
- }
- // NEW TICKET CREATION
- $ticket = new Ticket();
- $ticket->name = $mail->subject; // TICKET NAME : SUBJECT
- $ticket->idTicketType = 152; // TICKET TYPE : TO DEFINE
- $ticket->idUser = 943; // EMITTER : MAIL
- $ticket->idContact = intval($senderId); // REQUESTER : SENDER
- $ticket->idProject = $pjc; // PROJECT: ONLY 1 AFFECTATION || TO DEFINE
- $ticket->description = nl2brForPlainText($msg); // DESCRIPTION : PARSED MAIL BODY
- $ticket->creationDateTime = date('Y-m-d H:i:s'); // CREATION DATE : CURRENT
- $ticket->idStatus = 1; // STATUS : REGISTERED
- $ticket->idPriority = 4; // PRIORITY : P3
- $ticket->idCriticality = 1; // CRITICITY : LOW
- $ticket->idProductVersion = 29; // PRODUCT VERSION : TO DEFINE
- $resSaveTicket = $ticket->save(); // Saving the ticket
- $mailbox->markMailAsRead($mailId); // Mark the ticket as read
- // Log the saving operation
- $status = getLastOperationStatus($resSaveTicket);
- if ($status == 'OK') {
- debugTraceLog("Ticket from '$sender' added");
- } else {
- debugTraceLog($status);
- traceLog("ERROR saving ticket from '$sender' ");
- }
- } else {
- $mailbox->markMailAsUnread($mailId); // Repassage en "non-lu"
- }
- // DEBUG IN THE LOG FILE
- ob_start();
- //var_dump($affectationList);
- //var_dump($ticket);
- $result = ob_get_clean();
- debugTraceLog($result);
- }
- // Clean $emailAttachmentsDir for php files
- foreach (glob($emailAttachmentsDir . '/*') as $v) {
- if (!is_file($v)) continue;
- if (substr(strtolower($v), -4) == '.php' or substr(strtolower($v), -5, 4) == '.php') unlink($v); // Default, but not enough
- if (is_file($v)) unlink($v); // delete all files (as of today, we don't retreive attachments)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement