Example #1
0
 /**
  * Constructor
  *
  * @param $mailgateID   ID of the mailgate
  * @param $display      display messages in MessageAfterRedirect or just return error (default 0=)
  *
  * @return if $display = false return messages result string
  **/
 function collect($mailgateID, $display = 0)
 {
     global $CFG_GLPI;
     if ($this->getFromDB($mailgateID)) {
         $this->mid = -1;
         $this->fetch_emails = 0;
         //Connect to the Mail Box
         $this->connect();
         $rejected = new NotImportedEmail();
         // Clean from previous collect (from GUI, cron already truncate the table)
         $rejected->deleteByCriteria(array('mailcollectors_id' => $this->fields['id']));
         if ($this->marubox) {
             // Get Total Number of Unread Email in mail box
             $tot = $this->getTotalMails();
             //Total Mails in Inbox Return integer value
             $error = 0;
             $refused = 0;
             $blacklisted = 0;
             for ($i = 1; $i <= $tot && $this->fetch_emails < $this->maxfetch_emails; $i++) {
                 $tkt = $this->buildTicket($i, array('mailgates_id' => $mailgateID, 'play_rules' => true));
                 toolbox::logdebug("tkt", $tkt);
                 //Indicates that the mail must be deleted from the mailbox
                 $delete_mail = false;
                 //If entity assigned, or email refused by rule, or no user and no supplier associated with the email
                 $user_condition = $CFG_GLPI["use_anonymous_helpdesk"] || isset($tkt['_users_id_requester']) && $tkt['_users_id_requester'] > 0 || isset($tkt['_supplier_email']) && $tkt['_supplier_email'];
                 $rejinput = array();
                 $rejinput['mailcollectors_id'] = $mailgateID;
                 if (!$tkt['_blacklisted']) {
                     $rejinput['from'] = $tkt['_head']['from'];
                     $rejinput['to'] = $tkt['_head']['to'];
                     $rejinput['users_id'] = $tkt['_users_id_requester'];
                     $rejinput['subject'] = $this->textCleaner($tkt['_head']['subject']);
                     $rejinput['messageid'] = $tkt['_head']['message_id'];
                 }
                 $rejinput['date'] = $_SESSION["glpi_currenttime"];
                 // Manage blacklisted emails
                 if (isset($tkt['_blacklisted']) && $tkt['_blacklisted']) {
                     $this->deleteMails($i, self::REFUSED_FOLDER);
                     $blacklisted++;
                     // entities_id set when new ticket / tickets_id when new followup
                 } else {
                     if ((isset($tkt['entities_id']) || isset($tkt['tickets_id'])) && $user_condition || isset($tkt['_refuse_email_no_response']) || isset($tkt['_refuse_email_with_response'])) {
                         if (isset($tkt['_refuse_email_with_response'])) {
                             $this->sendMailRefusedResponse($tkt['_head']['from'], $tkt['name']);
                             $delete_mail = self::REFUSED_FOLDER;
                             $refused++;
                         } else {
                             if (isset($tkt['_refuse_email_no_response'])) {
                                 $delete_mail = self::REFUSED_FOLDER;
                                 $refused++;
                             } else {
                                 if (isset($tkt['entities_id']) || isset($tkt['tickets_id'])) {
                                     // Is a mail responding of an already existing ticket ?
                                     if (isset($tkt['tickets_id'])) {
                                         $fup = new TicketFollowup();
                                         if ($fup->add($tkt)) {
                                             $delete_mail = self::ACCEPTED_FOLDER;
                                         } else {
                                             $error++;
                                             $rejinput['reason'] = NotImportedEmail::FAILED_INSERT;
                                             $rejected->add($rejinput);
                                         }
                                     } else {
                                         // New ticket
                                         $track = new Ticket();
                                         if ($track->add($tkt)) {
                                             $delete_mail = self::ACCEPTED_FOLDER;
                                         } else {
                                             $error++;
                                             $rejinput['reason'] = NotImportedEmail::FAILED_INSERT;
                                             $rejected->add($rejinput);
                                         }
                                     }
                                 } else {
                                     // Case never raise
                                     $delete_mail = self::REFUSED_FOLDER;
                                     $refused++;
                                 }
                             }
                         }
                         //Delete Mail from Mail box if ticket is added successfully
                         if ($delete_mail) {
                             $this->deleteMails($i, $delete_mail);
                         }
                     } else {
                         if (!$tkt['_users_id_requester']) {
                             $rejinput['reason'] = NotImportedEmail::USER_UNKNOWN;
                         } else {
                             $rejinput['reason'] = NotImportedEmail::MATCH_NO_RULE;
                         }
                         $rejected->add($rejinput);
                     }
                 }
                 $this->fetch_emails++;
             }
             imap_expunge($this->marubox);
             $this->close_mailbox();
             //Close Mail Box
             //TRANS: %1$d, %2$d, %3$d, %4$d and %5$d are number of messages
             $msg = sprintf(__('Number of messages: available=%1$d, retrieved=%2$d, refused=%3$d, errors=%4$d, blacklisted=%5$d'), $tot, $this->fetch_emails, $refused, $error, $blacklisted);
             if ($display) {
                 Session::addMessageAfterRedirect($msg, false, $error ? ERROR : INFO);
             } else {
                 return $msg;
             }
         } else {
             $msg = __('Could not connect to mailgate server');
             if ($display) {
                 Session::addMessageAfterRedirect($msg, false, ERROR);
             } else {
                 return $msg;
             }
         }
     } else {
         //TRANS: %s is the ID of the mailgate
         $msg = sprintf(__('Could not find mailgate %d'), $mailgateID);
         if ($display) {
             Session::addMessageAfterRedirect($msg, false, ERROR);
         } else {
             return $msg;
         }
     }
 }
 /**
  * Constructor
  *
  * @param $mailgateID ID of the mailgate
  * @param $display display messages in MessageAfterRedirect or just return error
  *
  * @return if $display = false return messages result string
  **/
 function collect($mailgateID, $display = 0)
 {
     global $LANG, $CFG_GLPI;
     if ($this->getFromDB($mailgateID)) {
         $this->mid = -1;
         $this->fetch_emails = 0;
         //Connect to the Mail Box
         $this->connect();
         $rejected = new NotImportedEmail();
         // Clean from previous collect (from GUI, cron already truncate the table)
         $rejected->deleteByCriteria(array('mailcollectors_id' => $this->fields['id']));
         if ($this->marubox) {
             // Get Total Number of Unread Email in mail box
             $tot = $this->getTotalMails();
             //Total Mails in Inbox Return integer value
             $error = 0;
             $refused = 0;
             for ($i = 1; $i <= $tot && $this->fetch_emails < $this->maxfetch_emails; $i++) {
                 $tkt = $this->buildTicket($i, array('mailgates_id' => $mailgateID, 'play_rules' => true));
                 //Indicates that the mail must be deleted from the mailbox
                 $delete_mail = false;
                 //If entity assigned, or email refused by rule, or no user associated with the email
                 $user_condition = $CFG_GLPI["use_anonymous_helpdesk"] || isset($tkt['_users_id_requester']) && $tkt['_users_id_requester'] > 0;
                 // Manage blacklisted emails
                 if (isset($tkt['_blacklisted']) && $tkt['_blacklisted']) {
                     $this->deleteMails($i);
                     // entities_id set when new ticket / tickets_id when new followup
                 } else {
                     if ((isset($tkt['entities_id']) || isset($tkt['tickets_id'])) && $user_condition || isset($tkt['_refuse_email_no_response']) || isset($tkt['_refuse_email_with_response'])) {
                         if (isset($tkt['_refuse_email_with_response'])) {
                             $this->sendMailRefusedResponse($tkt['_head']['from'], $tkt['name']);
                             $delete_mail = true;
                             $refused++;
                         } else {
                             if (isset($tkt['_refuse_email_no_response'])) {
                                 $delete_mail = true;
                                 $refused++;
                             } else {
                                 if (isset($tkt['entities_id']) || isset($tkt['tickets_id'])) {
                                     $tkt['_mailgate'] = $mailgateID;
                                     $result = imap_fetchheader($this->marubox, $i);
                                     // Is a mail responding of an already existgin ticket ?
                                     if (isset($tkt['tickets_id'])) {
                                         // Deletion of message with sucess
                                         if (false === is_array($result)) {
                                             $fup = new TicketFollowup();
                                             if ($fup->add($tkt)) {
                                                 $delete_mail = true;
                                             }
                                         } else {
                                             $error++;
                                         }
                                     } else {
                                         // New ticket
                                         // Deletion of message with sucess
                                         if (false === is_array($result)) {
                                             $track = new Ticket();
                                             if ($track->add($tkt)) {
                                                 $delete_mail = true;
                                             }
                                         } else {
                                             $error++;
                                         }
                                     }
                                 } else {
                                     // Case never raise
                                     $delete_mail = true;
                                     $refused++;
                                 }
                             }
                         }
                         //Delete Mail from Mail box if ticket is added successfully
                         if ($delete_mail) {
                             $this->deleteMails($i);
                         }
                     } else {
                         $input = array();
                         $input['mailcollectors_id'] = $mailgateID;
                         $input['from'] = $tkt['_head']['from'];
                         $input['to'] = $tkt['_head']['to'];
                         if (!$tkt['_users_id_requester']) {
                             $input['reason'] = NotImportedEmail::USER_UNKNOWN;
                         } else {
                             $input['reason'] = NotImportedEmail::MATCH_NO_RULE;
                         }
                         $input['users_id'] = $tkt['_users_id_requester'];
                         $input['subject'] = $this->textCleaner($tkt['_head']['subject']);
                         $input['messageid'] = $tkt['_head']['message_id'];
                         $input['date'] = $_SESSION["glpi_currenttime"];
                         $rejected->add($input);
                     }
                 }
                 $this->fetch_emails++;
             }
             imap_expunge($this->marubox);
             $this->close_mailbox();
             //Close Mail Box
             if ($display) {
                 if ($error == 0) {
                     addMessageAfterRedirect($LANG['mailgate'][3] . "&nbsp;: " . $this->fetch_emails);
                 } else {
                     addMessageAfterRedirect($LANG['mailgate'][3] . "&nbsp;: " . $this->fetch_emails . " ({$error} " . $LANG['common'][63] . ")", false, ERROR);
                 }
             } else {
                 return "Number of messages: available={$tot}, collected=" . $this->fetch_emails . ($error > 0 ? " ({$error} error(s))" : "" . ($refused > 0 ? " ({$refused} refused)" : ""));
             }
         } else {
             if ($display) {
                 addMessageAfterRedirect($LANG['log'][41], false, ERROR);
             } else {
                 return "Could not connect to mailgate server";
             }
         }
     } else {
         if ($display) {
             addMessageAfterRedirect($LANG['common'][54] . "&nbsp;: mailgate " . $mailgateID, false, ERROR);
         } else {
             return 'Could find mailgate ' . $mailgateID;
         }
     }
 }