Ejemplo n.º 1
0
 function run()
 {
     global $ost;
     if (!$ost->getConfig()->isEmailPollingEnabled()) {
         return;
     }
     //We require imap ext to fetch emails via IMAP/POP3
     //We check here just in case the extension gets disabled post email config...
     if (!function_exists('imap_open')) {
         $msg = _S('osTicket requires PHP IMAP extension enabled for IMAP/POP3 email fetch to work!');
         $ost->logWarning(_S('Mail Fetch Error'), $msg);
         return;
     }
     //Hardcoded error control...
     $MAXERRORS = 5;
     //Max errors before we start delayed fetch attempts
     $TIMEOUT = 10;
     //Timeout in minutes after max errors is reached.
     $sql = ' SELECT email_id, mail_errors FROM ' . EMAIL_TABLE . ' WHERE mail_active=1 ' . '  AND (mail_errors<=' . $MAXERRORS . ' OR (TIME_TO_SEC(TIMEDIFF(NOW(), mail_lasterror))>' . $TIMEOUT * 60 . ') )' . '  AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(), mail_lastfetch))>mail_fetchfreq*60)' . ' ORDER BY mail_lastfetch ASC';
     if (!($res = db_query($sql)) || !db_num_rows($res)) {
         return;
     }
     /* Failed query (get's logged) or nothing to do... */
     //Get max execution time so we can figure out how long we can fetch
     // take fetching emails.
     if (!($max_time = ini_get('max_execution_time'))) {
         $max_time = 300;
     }
     //Start time
     $start_time = Misc::micro_time();
     while (list($emailId, $errors) = db_fetch_row($res)) {
         //Break if we're 80% into max execution time
         if (Misc::micro_time() - $start_time > $max_time * 0.8) {
             break;
         }
         $fetcher = new MailFetcher($emailId);
         if ($fetcher->connect()) {
             db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id=' . db_input($emailId));
             $fetcher->fetchEmails();
             $fetcher->close();
         } else {
             db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id=' . db_input($emailId));
             if (++$errors >= $MAXERRORS) {
                 //We've reached the MAX consecutive errors...will attempt logins at delayed intervals
                 $msg = "\n" . _S('osTicket is having trouble fetching emails from the following mail account') . ": \n" . "\n" . _S('User') . ": " . $fetcher->getUsername() . "\n" . _S('Host') . ": " . $fetcher->getHost() . "\n" . _S('Error') . ": " . $fetcher->getLastError() . "\n\n " . sprintf(_S('%1$d consecutive errors. Maximum of %2$d allowed'), $errors, $MAXERRORS) . "\n\n " . sprintf(_S('This could be connection issues related to the mail server. Next delayed login attempt in aprox. %d minutes'), $TIMEOUT);
                 $ost->alertAdmin(_S('Mail Fetch Failure Alert'), $msg, true);
             }
         }
     }
     //end while.
 }
Ejemplo n.º 2
0
 function run()
 {
     global $ost;
     if (!$ost->getConfig()->isEmailPollingEnabled()) {
         return;
     }
     //We require imap ext to fetch emails via IMAP/POP3
     //We check here just in case the extension gets disabled post email config...
     if (!function_exists('imap_open')) {
         $msg = 'osTicket requires PHP IMAP extension enabled for IMAP/POP3 email fetch to work!';
         $ost->logWarning('Mail Fetch Error', $msg);
         return;
     }
     //Hardcoded error control...
     $MAXERRORS = 5;
     //Max errors before we start delayed fetch attempts
     $TIMEOUT = 10;
     //Timeout in minutes after max errors is reached.
     $sql = ' SELECT email_id, mail_errors FROM ' . EMAIL_TABLE . ' WHERE mail_active=1 ' . '  AND (mail_errors<=' . $MAXERRORS . ' OR (TIME_TO_SEC(TIMEDIFF(NOW(), mail_lasterror))>' . $TIMEOUT * 60 . ') )' . '  AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(), mail_lastfetch))>mail_fetchfreq*60)' . ' ORDER BY mail_lastfetch DESC' . ' LIMIT 10';
     //Processing up to 10 emails at a time.
     // echo $sql;
     if (!($res = db_query($sql)) || !db_num_rows($res)) {
         return;
     }
     /* Failed query (get's logged) or nothing to do... */
     //TODO: Lock the table here??
     while (list($emailId, $errors) = db_fetch_row($res)) {
         $fetcher = new MailFetcher($emailId);
         if ($fetcher->connect()) {
             $fetcher->fetchEmails();
             $fetcher->close();
             db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id=' . db_input($emailId));
         } else {
             db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id=' . db_input($emailId));
             if (++$errors >= $MAXERRORS) {
                 //We've reached the MAX consecutive errors...will attempt logins at delayed intervals
                 $msg = "\nosTicket is having trouble fetching emails from the following mail account: \n" . "\nUser: "******"\nHost: " . $fetcher->getHost() . "\nError: " . $fetcher->getLastError() . "\n\n " . $errors . ' consecutive errors. Maximum of ' . $MAXERRORS . ' allowed' . "\n\n This could be connection issues related to the mail server. Next delayed login attempt in aprox. {$TIMEOUT} minutes";
                 $ost->alertAdmin('Mail Fetch Failure Alert', $msg, true);
             }
         }
     }
     //end while.
 }