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. }
function save($id, $vars, &$errors) { global $cfg; //very basic checks $vars['name'] = Format::striptags(trim($vars['name'])); if ($id && $id != $vars['id']) { $errors['err'] = 'Internal error. Get technical help.'; } if (!$vars['email'] || !Validator::is_email($vars['email'])) { $errors['email'] = 'Valid email required'; } elseif (($eid = Email::getIdByEmail($vars['email'])) && $eid != $id) { $errors['email'] = 'Email already exits'; } elseif ($cfg && !strcasecmp($cfg->getAdminEmail(), $vars['email'])) { $errors['email'] = 'Email already used as admin email!'; } elseif (Staff::getIdByEmail($vars['email'])) { //make sure the email doesn't belong to any of the staff $errors['email'] = 'Email in-use by a staff member'; } if (!$vars['name']) { $errors['name'] = 'Email name required'; } if ($vars['mail_active'] || $vars['smtp_active'] && $vars['smtp_auth']) { if (!$vars['userid']) { $errors['userid'] = 'Username missing'; } if (!$id && !$vars['passwd']) { $errors['passwd'] = 'Password required'; } } if ($vars['mail_active']) { //Check pop/imapinfo only when enabled. if (!function_exists('imap_open')) { $errors['mail_active'] = 'IMAP doesn\'t exist. PHP must be compiled with IMAP enabled.'; } if (!$vars['mail_host']) { $errors['mail_host'] = 'Host name required'; } if (!$vars['mail_port']) { $errors['mail_port'] = 'Port required'; } if (!$vars['mail_protocol']) { $errors['mail_protocol'] = 'Select protocol'; } if (!$vars['mail_fetchfreq'] || !is_numeric($vars['mail_fetchfreq'])) { $errors['mail_fetchfreq'] = 'Fetch interval required'; } if (!$vars['mail_fetchmax'] || !is_numeric($vars['mail_fetchmax'])) { $errors['mail_fetchmax'] = 'Maximum emails required'; } if (!$vars['dept_id'] || !is_numeric($vars['dept_id'])) { $errors['dept_id'] = 'You must select a Dept.'; } if (!$vars['priority_id']) { $errors['priority_id'] = 'You must select a priority'; } if (!isset($vars['postfetch'])) { $errors['postfetch'] = 'Indicate what to do with fetched emails'; } elseif (!strcasecmp($vars['postfetch'], 'archive')) { if (!$vars['mail_archivefolder']) { $errors['postfetch'] = 'Valid folder required'; } } } if ($vars['smtp_active']) { if (!$vars['smtp_host']) { $errors['smtp_host'] = 'Host name required'; } if (!$vars['smtp_port']) { $errors['smtp_port'] = 'Port required'; } } //abort on errors if ($errors) { return false; } if (!$errors && ($vars['mail_host'] && $vars['userid'])) { $sql = 'SELECT email_id FROM ' . EMAIL_TABLE . ' WHERE mail_host=' . db_input($vars['mail_host']) . ' AND userid=' . db_input($vars['userid']); if ($id) { $sql .= ' AND email_id!=' . db_input($id); } if (db_num_rows(db_query($sql))) { $errors['userid'] = $errors['host'] = 'Host/userid combination already in-use.'; } } $passwd = $vars['passwd'] ? $vars['passwd'] : $vars['cpasswd']; if (!$errors && $vars['mail_active']) { //note: password is unencrypted at this point...MailFetcher expect plain text. $fetcher = new MailFetcher($vars['userid'], $passwd, $vars['mail_host'], $vars['mail_port'], $vars['mail_protocol'], $vars['mail_encryption']); if (!$fetcher->connect()) { $errors['err'] = 'Invalid login. Check ' . Format::htmlchars($vars['mail_protocol']) . ' settings'; $errors['mail'] = '<br>' . $fetcher->getLastError(); } elseif ($vars['mail_archivefolder'] && !$fetcher->checkMailbox($vars['mail_archivefolder'], true)) { $errors['postfetch'] = 'Invalid or unknown mail folder! >> ' . $fetcher->getLastError() . ''; if (!$errors['mail']) { $errors['mail'] = 'Invalid or unknown archive folder!'; } } } if (!$errors && $vars['smtp_active']) { //Check SMTP login only. require_once 'Mail.php'; // PEAR Mail package $smtp = mail::factory('smtp', array('host' => $vars['smtp_host'], 'port' => $vars['smtp_port'], 'auth' => $vars['smtp_auth'] ? true : false, 'username' => $vars['userid'], 'password' => $passwd, 'timeout' => 20, 'debug' => false)); $mail = $smtp->connect(); if (PEAR::isError($mail)) { $errors['err'] = 'Unable to login. Check SMTP settings.'; $errors['smtp'] = '<br>' . $mail->getMessage(); } else { $smtp->disconnect(); //Thank you, sir! } } if ($errors) { return false; } //Default to default priority and dept.. if (!$vars['priority_id'] && $cfg) { $vars['priority_id'] = $cfg->getDefaultPriorityId(); } if (!$vars['dept_id'] && $cfg) { $vars['dept_id'] = $cfg->getDefaultDeptId(); } $sql = 'updated=NOW(),mail_errors=0, mail_lastfetch=NULL' . ',email=' . db_input($vars['email']) . ',name=' . db_input(Format::striptags($vars['name'])) . ',dept_id=' . db_input($vars['dept_id']) . ',priority_id=' . db_input($vars['priority_id']) . ',noautoresp=' . db_input(isset($vars['noautoresp']) ? 1 : 0) . ',userid=' . db_input($vars['userid']) . ',mail_active=' . db_input($vars['mail_active']) . ',mail_host=' . db_input($vars['mail_host']) . ',mail_protocol=' . db_input($vars['mail_protocol'] ? $vars['mail_protocol'] : 'POP') . ',mail_encryption=' . db_input($vars['mail_encryption']) . ',mail_port=' . db_input($vars['mail_port'] ? $vars['mail_port'] : 0) . ',mail_fetchfreq=' . db_input($vars['mail_fetchfreq'] ? $vars['mail_fetchfreq'] : 0) . ',mail_fetchmax=' . db_input($vars['mail_fetchmax'] ? $vars['mail_fetchmax'] : 0) . ',smtp_active=' . db_input($vars['smtp_active']) . ',smtp_host=' . db_input($vars['smtp_host']) . ',smtp_port=' . db_input($vars['smtp_port'] ? $vars['smtp_port'] : 0) . ',smtp_auth=' . db_input($vars['smtp_auth']) . ',smtp_spoofing=' . db_input(isset($vars['smtp_spoofing']) ? 1 : 0) . ',notes=' . db_input($vars['notes']); //Post fetch email handling... if ($vars['postfetch'] && !strcasecmp($vars['postfetch'], 'delete')) { $sql .= ',mail_delete=1,mail_archivefolder=NULL'; } elseif ($vars['postfetch'] && !strcasecmp($vars['postfetch'], 'archive') && $vars['mail_archivefolder']) { $sql .= ',mail_delete=0,mail_archivefolder=' . db_input($vars['mail_archivefolder']); } else { $sql .= ',mail_delete=0,mail_archivefolder=NULL'; } if ($vars['passwd']) { //New password - encrypt. $sql .= ',userpass='******'passwd'], SECRET_SALT)); } if ($id) { //update $sql = 'UPDATE ' . EMAIL_TABLE . ' SET ' . $sql . ' WHERE email_id=' . db_input($id); if (db_query($sql) && db_affected_rows()) { return true; } $errors['err'] = 'Unable to update email. Internal error occurred'; } else { $sql = 'INSERT INTO ' . EMAIL_TABLE . ' SET ' . $sql . ',created=NOW()'; if (db_query($sql) && ($id = db_insert_id())) { return $id; } $errors['err'] = 'Unable to add email. Internal error'; } return false; }
function save($id, $vars, &$errors) { global $cfg; //very basic checks if ($id && $id != $vars['email_id']) { $errors['err'] = 'Erro interno.'; } if (!$vars['email'] || !Validator::is_email($vars['email'])) { $errors['email'] = 'Email válido obrigatório'; } elseif (($eid = Email::getIdByEmail($vars['email'])) && $eid != $id) { $errors['email'] = 'Email já existe.'; } elseif (!strcasecmp($cfg->getAdminEmail(), $vars['email'])) { $errors['email'] = 'Email já usado como email do administrador!'; } else { //make sure the email doesn't belong to any of the staff $sql = 'SELECT staff_id FROM ' . STAFF_TABLE . ' WHERE email=' . db_input($vars['email']); if (($res = db_query($sql)) && db_num_rows($res)) { $errors['email'] = 'Email em uso por um membro do suporte.'; } } if (!$vars['dept_id'] || !is_numeric($vars['dept_id'])) { $errors['dept_id'] = 'Você deve selecionar um departamento.'; } if (!$vars['priority_id']) { $errors['priority_id'] = 'Você deve selecionar uma prioridade'; } if ($vars['mail_active'] || $vars['smtp_active'] && $vars['smtp_auth']) { if (!$vars['userid']) { $errors['userid'] = 'Nome de usuário ausente'; } if (!$vars['userpass']) { $errors['userpass'] = '******'; } } if ($vars['mail_active']) { //Check pop/imapinfo only when enabled. if (!function_exists('imap_open')) { $errors['mail_active'] = 'IMAP não existe. PHP deve ser compilado com IMAP habilitado.'; } if (!$vars['mail_host']) { $errors['mail_host'] = 'Nome do host obrigatório'; } if (!$vars['mail_port']) { $errors['mail_port'] = 'Porta obrigatória'; } if (!$vars['mail_protocol']) { $errors['mail_protocol'] = 'Selecione protocolo'; } if (!$vars['mail_fetchfreq'] || !is_numeric($vars['mail_fetchfreq'])) { $errors['mail_fetchfreq'] = 'Buscar intervalo obrigatório'; } if (!$vars['mail_fetchmax'] || !is_numeric($vars['mail_fetchmax'])) { $errors['mail_fetchmax'] = 'Máximo de emails exigidos'; } } if ($vars['smtp_active']) { if (!$vars['smtp_host']) { $errors['smtp_host'] = 'Nome do host obrigatório'; } if (!$vars['smtp_port']) { $errors['smtp_port'] = 'Porta obrigatória'; } } if (!$errors && ($vars['mail_host'] && $vars['userid'])) { $sql = 'SELECT email_id FROM ' . EMAIL_TABLE . ' WHERE mail_host=' . db_input($vars['mail_host']) . ' AND userid=' . db_input($vars['userid']); if ($id) { $sql .= ' AND email_id!=' . db_input($id); } if (db_num_rows(db_query($sql))) { $errors['userid'] = $errors['host'] = 'Outro departamento está usando combinação de nome/host.'; } } if (!$errors && $vars['mail_active']) { //note: password is unencrypted at this point...MailFetcher expect plain text. $fetcher = new MailFetcher($vars['userid'], $vars['userpass'], $vars['mail_host'], $vars['mail_port'], $vars['mail_protocol'], $vars['mail_encryption']); if (!$fetcher->connect()) { $errors['userpass'] = '******' . $vars['mail_protocol'] . ' configurações'; $errors['mail'] = '<br>' . $fetcher->getLastError(); } } if (!$errors && $vars['smtp_active']) { //Check SMTP login only. require_once 'Mail.php'; // PEAR Mail package $smtp = mail::factory('smtp', array('host' => $vars['smtp_host'], 'port' => $vars['smtp_port'], 'auth' => $vars['smtp_auth'] ? true : false, 'username' => $vars['userid'], 'password' => $vars['userpass'], 'timeout' => 20, 'debug' => false)); $mail = $smtp->connect(); if (PEAR::isError($mail)) { $errors['userpass'] = '******'; $errors['smtp'] = '<br>' . $mail->getMessage(); } else { $smtp->disconnect(); //Thank you, sir! } } if (!$errors) { $sql = 'updated=NOW(),mail_errors=0, mail_lastfetch=NULL' . ',email=' . db_input($vars['email']) . ',name=' . db_input(Format::striptags($vars['name'])) . ',dept_id=' . db_input($vars['dept_id']) . ',priority_id=' . db_input($vars['priority_id']) . ',noautoresp=' . db_input(isset($vars['noautoresp']) ? 1 : 0) . ',userid=' . db_input($vars['userid']) . ',userpass='******'userpass'], SECRET_SALT)) . ',mail_active=' . db_input($vars['mail_active']) . ',mail_host=' . db_input($vars['mail_host']) . ',mail_protocol=' . db_input($vars['mail_protocol'] ? $vars['mail_protocol'] : 'POP') . ',mail_encryption=' . db_input($vars['mail_encryption']) . ',mail_port=' . db_input($vars['mail_port'] ? $vars['mail_port'] : 0) . ',mail_fetchfreq=' . db_input($vars['mail_fetchfreq'] ? $vars['mail_fetchfreq'] : 0) . ',mail_fetchmax=' . db_input($vars['mail_fetchmax'] ? $vars['mail_fetchmax'] : 0) . ',mail_delete=' . db_input(isset($vars['mail_delete']) ? $vars['mail_delete'] : 0) . ',smtp_active=' . db_input($vars['smtp_active']) . ',smtp_host=' . db_input($vars['smtp_host']) . ',smtp_port=' . db_input($vars['smtp_port'] ? $vars['smtp_port'] : 0) . ',smtp_auth=' . db_input($vars['smtp_auth']); if ($id) { //update $sql = 'UPDATE ' . EMAIL_TABLE . ' SET ' . $sql . ' WHERE email_id=' . db_input($id); if (!db_query($sql) || !db_affected_rows()) { $errors['err'] = 'Não é possível atualizar e-mail. Erro interno'; } } else { $sql = 'INSERT INTO ' . EMAIL_TABLE . ' SET ' . $sql . ',created=NOW()'; if (!db_query($sql) or !($emailID = db_insert_id())) { $errors['err'] = 'Não é possível adicionar e-mail. Erro interno'; } else { return $emailID; } //newly created email. } } else { $errors['err'] = 'Erro(s). Tente novamente'; } return $errors ? FALSE : TRUE; }
function save($id, $vars, &$errors) { global $cfg; //very basic checks if ($id && $id != $vars['email_id']) { $errors['err'] = 'Internal error.'; } if (!$vars['email'] || !Validator::is_email($vars['email'])) { $errors['email'] = 'Valid email required'; } elseif (($eid = Email::getIdByEmail($vars['email'])) && $eid != $id) { $errors['email'] = 'Email already exits'; } elseif (!strcasecmp($cfg->getAdminEmail(), $vars['email'])) { $errors['email'] = 'Email already used as admin email!'; } else { //make sure the email doesn't belong to any of the staff $sql = 'SELECT staff_id FROM ' . STAFF_TABLE . ' WHERE email=' . db_input($vars['email']); if (($res = db_query($sql)) && db_num_rows($res)) { $errors['email'] = 'Email in-use by a staff member'; } } if (!$vars['dept_id'] || !is_numeric($vars['dept_id'])) { $errors['dept_id'] = 'You must select a Dept.'; } if (!$vars['priority_id']) { $errors['priority_id'] = 'You must select a priority'; } if ($vars['mail_active'] || $vars['smtp_active'] && $vars['smtp_auth']) { if (!$vars['userid']) { $errors['userid'] = 'Username missing'; } if (!$vars['userpass']) { $errors['userpass'] = '******'; } } if ($vars['mail_active']) { //Check pop/imapinfo only when enabled. if (!function_exists('imap_open')) { $errors['mail_active'] = 'IMAP doesn\'t exist. PHP must be compiled with IMAP enabled.'; } if (!$vars['mail_host']) { $errors['mail_host'] = 'Host name required'; } if (!$vars['mail_port']) { $errors['mail_port'] = 'Port required'; } if (!$vars['mail_protocol']) { $errors['mail_protocol'] = 'Select protocol'; } if (!$vars['mail_fetchfreq'] || !is_numeric($vars['mail_fetchfreq'])) { $errors['mail_fetchfreq'] = 'Fetch interval required'; } if (!$vars['mail_fetchmax'] || !is_numeric($vars['mail_fetchmax'])) { $errors['mail_fetchmax'] = 'Maximum emails required'; } } if ($vars['smtp_active']) { if (!$vars['smtp_host']) { $errors['smtp_host'] = 'Host name required'; } if (!$vars['smtp_port']) { $errors['smtp_port'] = 'Port required'; } } if (!$errors && ($vars['mail_host'] && $vars['userid'])) { $sql = 'SELECT email_id FROM ' . EMAIL_TABLE . ' WHERE mail_host=' . db_input($vars['mail_host']) . ' AND userid=' . db_input($vars['userid']); if ($id) { $sql .= ' AND email_id!=' . db_input($id); } if (db_num_rows(db_query($sql))) { $errors['userid'] = $errors['host'] = 'Another department using host/username combination.'; } } if (!$errors && $vars['mail_active']) { //note: password is unencrypted at this point...MailFetcher expect plain text. $fetcher = new MailFetcher($vars['userid'], $vars['userpass'], $vars['mail_host'], $vars['mail_port'], $vars['mail_protocol'], $vars['mail_encryption']); if (!$fetcher->connect()) { $errors['userpass'] = '******' . $vars['mail_protocol'] . ' settings'; $errors['mail'] = '<br>' . $fetcher->getLastError(); } } if (!$errors && $vars['smtp_active']) { //Check SMTP login only. require_once 'Mail.php'; // PEAR Mail package $smtp = mail::factory('smtp', array('host' => $vars['smtp_host'], 'port' => $vars['smtp_port'], 'auth' => $vars['smtp_auth'] ? true : false, 'username' => $vars['userid'], 'password' => $vars['userpass'], 'timeout' => 20, 'debug' => false)); $mail = $smtp->connect(); if (PEAR::isError($mail)) { $errors['userpass'] = '******'; $errors['smtp'] = '<br>' . $mail->getMessage(); } else { $smtp->disconnect(); //Thank you, sir! } } if (!$errors) { $sql = 'updated=NOW(),mail_errors=0, mail_lastfetch=NULL' . ',email=' . db_input($vars['email']) . ',name=' . db_input(Format::striptags($vars['name'])) . ',dept_id=' . db_input($vars['dept_id']) . ',priority_id=' . db_input($vars['priority_id']) . ',noautoresp=' . db_input(isset($vars['noautoresp']) ? 1 : 0) . ',userid=' . db_input($vars['userid']) . ',userpass='******'userpass'], SECRET_SALT)) . ',mail_active=' . db_input($vars['mail_active']) . ',mail_host=' . db_input($vars['mail_host']) . ',mail_protocol=' . db_input($vars['mail_protocol'] ? $vars['mail_protocol'] : 'POP') . ',mail_encryption=' . db_input($vars['mail_encryption']) . ',mail_port=' . db_input($vars['mail_port'] ? $vars['mail_port'] : 0) . ',mail_fetchfreq=' . db_input($vars['mail_fetchfreq'] ? $vars['mail_fetchfreq'] : 0) . ',mail_fetchmax=' . db_input($vars['mail_fetchmax'] ? $vars['mail_fetchmax'] : 0) . ',mail_delete=' . db_input(isset($vars['mail_delete']) ? $vars['mail_delete'] : 0) . ',smtp_active=' . db_input($vars['smtp_active']) . ',smtp_host=' . db_input($vars['smtp_host']) . ',smtp_port=' . db_input($vars['smtp_port'] ? $vars['smtp_port'] : 0) . ',smtp_auth=' . db_input($vars['smtp_auth']); if ($id) { //update $sql = 'UPDATE ' . EMAIL_TABLE . ' SET ' . $sql . ' WHERE email_id=' . db_input($id); if (!db_query($sql) || !db_affected_rows()) { $errors['err'] = 'Unable to update email. Internal error occured'; } } else { $sql = 'INSERT INTO ' . EMAIL_TABLE . ' SET ' . $sql . ',created=NOW()'; if (!db_query($sql) or !($emailID = db_insert_id())) { $errors['err'] = 'Unable to add email. Internal error'; } else { return $emailID; } //newly created email. } } else { $errors['err'] = 'Error(s) Occured. Try again'; } return $errors ? FALSE : TRUE; }
function fetchMail(){ global $cfg; if(!$cfg->canFetchMail()) return; //We require imap ext to fetch emails via IMAP/POP3 if(!function_exists('imap_open')) { $msg='PHP must be compiled with IMAP extension enabled for IMAP/POP3 fetch to work!'; Sys::log(LOG_WARN,'Mail Fetch Error',$msg); return; } $MAX_ERRORS=5; //Max errors before we start delayed fetch attempts - hardcoded for now. $sql=' SELECT email_id,mail_host,mail_port,mail_protocol,mail_encryption,mail_delete,mail_errors,userid,userpass FROM '.EMAIL_TABLE. ' WHERE mail_active=1 AND (mail_errors<='.$MAX_ERRORS.' OR (TIME_TO_SEC(TIMEDIFF(NOW(),mail_lasterror))>5*60) )'. ' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(),mail_lastfetch))>mail_fetchfreq*60) '; //echo $sql; if(!($accounts=db_query($sql)) || !db_num_rows($accounts)) return; //TODO: Lock the table here?? while($row=db_fetch_array($accounts)) { $fetcher = new MailFetcher($row['userid'],Misc::decrypt($row['userpass'],SECRET_SALT), $row['mail_host'],$row['mail_port'],$row['mail_protocol'],$row['mail_encryption']); if($fetcher->connect()){ $fetcher->fetchTickets($row['email_id'],$row['mail_fetchmax'],$row['mail_delete']?true:false); $fetcher->close(); db_query('UPDATE '.EMAIL_TABLE.' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id='.db_input($row['email_id'])); }else{ $errors=$row['mail_errors']+1; db_query('UPDATE '.EMAIL_TABLE.' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id='.db_input($row['email_id'])); if($errors>=$MAX_ERRORS){ //We've reached the MAX consecutive errors...will attempt logins at delayed intervals $msg="\nThe system is having trouble fetching emails from the following mail account: \n". "\nUser: "******"\nHost: ".$row['mail_host']. "\nError: ".$fetcher->getLastError(). "\n\n ".$errors.' consecutive errors. Maximum of '.$MAX_ERRORS. ' allowed'. "\n\n This could be connection issues related to the host. Next delayed login attempt in aprox. 10 minutes"; Sys::alertAdmin('Mail Fetch Failure Alert',$msg,true); } } } }
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. }
function fetchMail() { global $cfg; if (!$cfg->canFetchMail()) { return; } //We require imap ext to fetch emails via IMAP/POP3 if (!function_exists('imap_open')) { $msg = 'PHP deve ser compilado com extensão IMAP habilitado para buscar a trabalhar IMAP/POP3!'; Sys::log(LOG_WARN, 'Erro em buscar o email', $msg); return; } $MAX_ERRORS = 5; //Max errors before we start delayed fetch attempts - hardcoded for now. $sql = ' SELECT email_id,mail_host,mail_port,mail_protocol,mail_encryption,mail_delete,mail_errors,userid,userpass FROM ' . EMAIL_TABLE . ' WHERE mail_active=1 AND (mail_errors<=' . $MAX_ERRORS . ' OR (TIME_TO_SEC(TIMEDIFF(NOW(),mail_lasterror))>5*60) )' . ' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(),mail_lastfetch))>mail_fetchfreq*60) '; //echo $sql; if (!($accounts = db_query($sql)) || !db_num_rows($accounts)) { return; } //TODO: Lock the table here?? while ($row = db_fetch_array($accounts)) { $fetcher = new MailFetcher($row['userid'], Misc::decrypt($row['userpass'], SECRET_SALT), $row['mail_host'], $row['mail_port'], $row['mail_protocol'], $row['mail_encryption']); if ($fetcher->connect()) { $fetcher->fetchTickets($row['email_id'], $row['mail_fetchmax'], $row['mail_delete'] ? true : false); $fetcher->close(); db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id=' . db_input($row['email_id'])); } else { $errors = $row['mail_errors'] + 1; db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id=' . db_input($row['email_id'])); if ($errors >= $MAX_ERRORS) { //We've reached the MAX consecutive errors...will attempt logins at delayed intervals $msg = "\nO sistema está tendo problemas para coletar e-mails da conta do e-mail seguinte: \n" . "\nUsuário: " . $row['userid'] . "\nHost: " . $row['mail_host'] . "\nErro: " . $fetcher->getLastError() . "\n\n " . $errors . ' erros consecutivos. Máximo de ' . $MAX_ERRORS . ' permitido' . "\n\n Isso pode ser problemas de conexão relacionados ao hospedeiro. Próxima tentativa de login em aprox. 10 minutos"; Sys::alertAdmin('Alerta de falha na busca por email', $msg, true); } } } }
function fetchMail() { global $cfg; if (!$cfg->canFetchMail()) { return; } //We require imap ext to fetch emails via IMAP/POP3 if (!function_exists('imap_open')) { $msg = 'PHP debe ser compilado con la extensión IMAP habilitada para IMAP/POP3 fetch(captura de correo) para que funcione'; Sys::log(LOG_WARN, 'Error de captura de correo', $msg); return; } $MAX_ERRORS = 5; //Max errors before we start delayed fetch attempts - hardcoded for now. $sql = ' SELECT email_id,mail_host,mail_port,mail_protocol,mail_encryption,mail_delete,mail_errors,userid,userpass FROM ' . EMAIL_TABLE . ' WHERE mail_active=1 AND (mail_errors<=' . $MAX_ERRORS . ' OR (TIME_TO_SEC(TIMEDIFF(NOW(),mail_lasterror))>5*60) )' . ' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(),mail_lastfetch))>mail_fetchfreq*60) '; //echo $sql; if (!($accounts = db_query($sql)) || !db_num_rows($accounts)) { return; } //TODO: Lock the table here?? while ($row = db_fetch_array($accounts)) { $fetcher = new MailFetcher($row['userid'], Misc::decrypt($row['userpass'], SECRET_SALT), $row['mail_host'], $row['mail_port'], $row['mail_protocol'], $row['mail_encryption']); if ($fetcher->connect()) { $fetcher->fetchTickets($row['email_id'], $row['mail_fetchmax'], $row['mail_delete'] ? true : false); $fetcher->close(); db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id=' . db_input($row['email_id'])); } else { $errors = $row['mail_errors'] + 1; db_query('UPDATE ' . EMAIL_TABLE . ' SET mail_errors=mail_errors+1, mail_lasterror=NOW() WHERE email_id=' . db_input($row['email_id'])); if ($errors >= $MAX_ERRORS) { //We've reached the MAX consecutive errors...will attempt logins at delayed intervals $msg = "\nEl sistema está teniendo problemas para obtener los correos electrónicos de la cuenta de correo siguiente: \n" . "\nUsuario: " . $row['userid'] . "\nHost: " . $row['mail_host'] . "\nError: " . $fetcher->getLastError() . "\n\n " . $errors . ' errores consecutivos. Máximo de ' . $MAX_ERRORS . ' permitidos' . "\n\n Esto podría ser una cuestión relacionada con la conexión al host. Siguiente intento en aprox. 10 min"; Sys::alertAdmin('Alerta de fallo en la captura de correo', $msg, true); } } } }
function save($id, $vars, &$errors) { global $cfg; //very basic checks if ($id && $id != $vars['email_id']) { $errors['err'] = 'Error Interno.'; } if (!$vars['email'] || !Validator::is_email($vars['email'])) { $errors['email'] = 'Se Requiere un Email Valido'; } elseif (($eid = Email::getIdByEmail($vars['email'])) && $eid != $id) { $errors['email'] = 'Este Email ya existe'; } elseif (!strcasecmp($cfg->getAdminEmail(), $vars['email'])) { $errors['email'] = 'Este Email ya se esta usando en la cuenta de Administrador'; } else { //make sure the email doesn't belong to any of the staff $sql = 'SELECT staff_id FROM ' . STAFF_TABLE . ' WHERE email=' . db_input($vars['email']); if (($res = db_query($sql)) && db_num_rows($res)) { $errors['email'] = 'Este Email ya se esta usando por un miembro del Staff'; } } if (!$vars['dept_id'] || !is_numeric($vars['dept_id'])) { $errors['dept_id'] = 'Debes seleccionar un Departamento'; } if (!$vars['priority_id']) { $errors['priority_id'] = 'Debes seleccionar prioridad'; } if ($vars['mail_active'] || $vars['smtp_active'] && $vars['smtp_auth']) { if (!$vars['userid']) { $errors['userid'] = 'Falta nombre de Usuario'; } if (!$vars['userpass']) { $errors['userpass'] = '******'; } } if ($vars['mail_active']) { //Check pop/imapinfo only when enabled. if (!function_exists('imap_open')) { $errors['mail_active'] = 'IMAP no existe. PHP debe ser compilado con IMAP.'; } if (!$vars['mail_host']) { $errors['mail_host'] = 'Se requiere nombre del Host'; } if (!$vars['mail_port']) { $errors['mail_port'] = 'Se requiere numero de Puerto'; } if (!$vars['mail_protocol']) { $errors['mail_protocol'] = 'Selecciona Protocolo'; } if (!$vars['mail_fetchfreq'] || !is_numeric($vars['mail_fetchfreq'])) { $errors['mail_fetchfreq'] = 'Se requiere frecuencia de captura'; } if (!$vars['mail_fetchmax'] || !is_numeric($vars['mail_fetchmax'])) { $errors['mail_fetchmax'] = 'Se requiere numero máximo de correos por captura'; } } if ($vars['smtp_active']) { if (!$vars['smtp_host']) { $errors['smtp_host'] = 'Se requiere nombre del Host'; } if (!$vars['smtp_port']) { $errors['smtp_port'] = 'Se requiere numero de puerto'; } } if (!$errors && ($vars['mail_host'] && $vars['userid'])) { $sql = 'SELECT email_id FROM ' . EMAIL_TABLE . ' WHERE mail_host=' . db_input($vars['mail_host']) . ' AND userid=' . db_input($vars['userid']); if ($id) { $sql .= ' AND email_id!=' . db_input($id); } if (db_num_rows(db_query($sql))) { $errors['userid'] = $errors['host'] = 'La combinacion Host/Nombre de usuario ya esta en uso por otro departamento.'; } } if (!$errors && $vars['mail_active']) { //note: password is unencrypted at this point...MailFetcher expect plain text. $fetcher = new MailFetcher($vars['userid'], $vars['userpass'], $vars['mail_host'], $vars['mail_port'], $vars['mail_protocol'], $vars['mail_encryption']); if (!$fetcher->connect()) { $errors['userpass'] = '******' . $vars['mail_protocol'] . ''; $errors['mail'] = '<br>' . $fetcher->getLastError(); } } if (!$errors && $vars['smtp_active']) { //Check SMTP login only. require_once 'Mail.php'; // PEAR Mail package $smtp = mail::factory('smtp', array('host' => $vars['smtp_host'], 'port' => $vars['smtp_port'], 'auth' => $vars['smtp_auth'] ? true : false, 'username' => $vars['userid'], 'password' => $vars['userpass'], 'timeout' => 20, 'debug' => false)); $mail = $smtp->connect(); if (PEAR::isError($mail)) { $errors['userpass'] = '******'; $errors['smtp'] = '<br>' . $mail->getMessage(); } else { $smtp->disconnect(); //Thank you, sir! } } if (!$errors) { $sql = 'updated=NOW(),mail_errors=0, mail_lastfetch=NULL' . ',email=' . db_input($vars['email']) . ',name=' . db_input(Format::striptags($vars['name'])) . ',dept_id=' . db_input($vars['dept_id']) . ',priority_id=' . db_input($vars['priority_id']) . ',noautoresp=' . db_input(isset($vars['noautoresp']) ? 1 : 0) . ',userid=' . db_input($vars['userid']) . ',userpass='******'userpass'], SECRET_SALT)) . ',mail_active=' . db_input($vars['mail_active']) . ',mail_host=' . db_input($vars['mail_host']) . ',mail_protocol=' . db_input($vars['mail_protocol'] ? $vars['mail_protocol'] : 'POP') . ',mail_encryption=' . db_input($vars['mail_encryption']) . ',mail_port=' . db_input($vars['mail_port'] ? $vars['mail_port'] : 0) . ',mail_fetchfreq=' . db_input($vars['mail_fetchfreq'] ? $vars['mail_fetchfreq'] : 0) . ',mail_fetchmax=' . db_input($vars['mail_fetchmax'] ? $vars['mail_fetchmax'] : 0) . ',mail_delete=' . db_input(isset($vars['mail_delete']) ? $vars['mail_delete'] : 0) . ',smtp_active=' . db_input($vars['smtp_active']) . ',smtp_host=' . db_input($vars['smtp_host']) . ',smtp_port=' . db_input($vars['smtp_port'] ? $vars['smtp_port'] : 0) . ',smtp_auth=' . db_input($vars['smtp_auth']); if ($id) { //update $sql = 'UPDATE ' . EMAIL_TABLE . ' SET ' . $sql . ' WHERE email_id=' . db_input($id); if (!db_query($sql) || !db_affected_rows()) { $errors['err'] = 'No se a podido actualizar el Email, Error interno'; } } else { $sql = 'INSERT INTO ' . EMAIL_TABLE . ' SET ' . $sql . ',created=NOW()'; if (!db_query($sql) or !($emailID = db_insert_id())) { $errors['err'] = 'No se a podido añadir el Email, Error interno '; } else { return $emailID; } //newly created email. } } else { $errors['err'] = 'Se an producido Errores, intentelo de nuevo'; } return $errors ? FALSE : TRUE; }