function saveMailboxAction() { $translate = DevblocksPlatform::getTranslationService(); $worker = CerberusApplication::getActiveWorker(); if (!$worker || !$worker->is_superuser) { echo $translate->_('common.access_denied'); return; } if (DEMO_MODE) { DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('config', 'mail'))); return; } @($id = DevblocksPlatform::importGPC($_POST['account_id'], 'integer')); @($enabled = DevblocksPlatform::importGPC($_POST['pop3_enabled'], 'integer', 0)); @($nickname = DevblocksPlatform::importGPC($_POST['nickname'], 'string')); @($protocol = DevblocksPlatform::importGPC($_POST['protocol'], 'string')); @($host = DevblocksPlatform::importGPC($_POST['host'], 'string')); @($username = DevblocksPlatform::importGPC($_POST['username'], 'string')); @($password = DevblocksPlatform::importGPC($_POST['password'], 'string')); @($port = DevblocksPlatform::importGPC($_POST['port'], 'integer')); @($delete = DevblocksPlatform::importGPC($_POST['delete'], 'integer')); if (empty($nickname)) { $nickname = "POP3"; } // Defaults if (empty($port)) { switch ($protocol) { case 'pop3': $port = 110; break; case 'pop3-ssl': $port = 995; break; case 'imap': $port = 143; break; case 'imap-ssl': $port = 993; break; } } // [JAS]: [TODO] convert to field constants $fields = array('enabled' => $enabled, 'nickname' => $nickname, 'protocol' => $protocol, 'host' => $host, 'username' => $username, 'password' => $password, 'port' => $port); if (!empty($id) && !empty($delete)) { DAO_Mail::deletePop3Account($id); } elseif (!empty($id)) { DAO_Mail::updatePop3Account($id, $fields); } else { if (!empty($host) && !empty($username)) { $id = DAO_Mail::createPop3Account($fields); } } DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('config', 'mail'))); return; }
function run() { $logger = DevblocksPlatform::getConsoleLog(); $logger->info("[POP3] Starting POP3 Task"); if (!extension_loaded("imap")) { die("IMAP Extension not loaded!"); } @set_time_limit(0); // Unlimited (if possible) @ini_set('memory_limit', '64M'); $accounts = DAO_Mail::getPop3Accounts(); /* @var $accounts CerberusPop3Account[] */ $timeout = ini_get('max_execution_time'); // Allow runtime overloads (by host, etc.) @($gpc_pop3_max = DevblocksPlatform::importGPC($_REQUEST['pop3_max'], 'integer')); $max_downloads = !empty($gpc_pop3_max) ? $gpc_pop3_max : $this->getParam('max_messages', $timeout ? 20 : 50); // [JAS]: Make sure our output directory is writeable if (!is_writable(APP_MAIL_PATH . 'new' . DIRECTORY_SEPARATOR)) { $logger->err("[POP3] The mail storage directory is not writeable. Skipping POP3 download."); return; } foreach ($accounts as $account) { /* @var $account CerberusPop3Account */ if (!$account->enabled) { continue; } $logger->info('[POP3] Account being parsed is ' . $account->nickname); switch ($account->protocol) { default: case 'pop3': // 110 $connect = sprintf("{%s:%d/pop3/notls}INBOX", $account->host, $account->port); break; case 'pop3-ssl': // 995 $connect = sprintf("{%s:%d/pop3/ssl/novalidate-cert}INBOX", $account->host, $account->port); break; case 'imap': // 143 $connect = sprintf("{%s:%d/notls}INBOX", $account->host, $account->port); break; case 'imap-ssl': // 993 $connect = sprintf("{%s:%d/imap/ssl/novalidate-cert}INBOX", $account->host, $account->port); break; } $runtime = microtime(true); if (false === ($mailbox = @imap_open($connect, !empty($account->username) ? $account->username : "", !empty($account->password) ? $account->password : ""))) { $logger->err("[POP3] Failed with error: " . imap_last_error()); continue; } $messages = array(); $check = imap_check($mailbox); // [TODO] Make this an account setting? $total = min($max_downloads, $check->Nmsgs); $logger->info('[POP3] Init time: ' . (microtime(true) - $runtime) * 1000, " ms"); $runtime = microtime(true); for ($i = 1; $i <= $total; $i++) { /* * [TODO] Logic for max message size (>1MB, etc.) handling. If over a * threshold then use the attachment parser (imap_fetchstructure) to toss * non-plaintext until the message fits. */ $msgno = $i; $time = microtime(true); $headers = imap_fetchheader($mailbox, $msgno); $body = imap_body($mailbox, $msgno); do { $unique = sprintf("%s.%04d", time(), mt_rand(0, 9999)); $filename = APP_MAIL_PATH . 'new' . DIRECTORY_SEPARATOR . $unique; } while (file_exists($filename)); $fp = fopen($filename, 'w'); if ($fp) { fwrite($fp, $headers, strlen($headers)); fwrite($fp, "\r\n\r\n"); fwrite($fp, $body, strlen($body)); @fclose($fp); } /* * [JAS]: We don't add the .msg extension until we're done with the file, * since this will safely be ignored by the parser until we're ready * for it. */ rename($filename, dirname($filename) . DIRECTORY_SEPARATOR . basename($filename) . '.msg'); unset($headers); unset($body); $time = microtime(true) - $time; $logger->info("[POP3] Downloaded message " . $msgno . " (" . sprintf("%d", $time * 1000) . " ms)"); imap_delete($mailbox, $msgno); continue; } imap_expunge($mailbox); imap_close($mailbox); imap_errors(); $logger->info("[POP3] Total Runtime: " . (microtime(true) - $runtime) * 1000 . " ms"); } }
/** * Enter description here... * * @param array $headers * @return array (group_id,address) */ private static function findDestination($headers) { static $routing = null; $settings = CerberusSettings::getInstance(); // [TODO] Should this cache be at the class level? if (is_null($routing)) { $routing = DAO_Mail::getMailboxRouting(); } $destinations = self::getDestinations($headers); if (is_array($destinations)) { foreach ($destinations as $address) { // Test each pattern successively foreach ($routing as $route) { /* @var $route Model_MailRoute */ $pattern = sprintf("/^%s\$/i", str_replace(array('*', '+'), array('.*?', '\\+'), $route->pattern)); if (preg_match($pattern, $address)) { return array($route->team_id, $address); } } } } // Check if we have a default mailbox configured before returning NULL. if (null != ($default_team = DAO_Group::getDefaultGroup())) { return array($default_team->id, ''); } return null; // bounce }
/** * Enter description here... * * @param integer $id * @return CerberusPop3Account */ static function getPop3Account($id) { $accounts = DAO_Mail::getPop3Accounts(array($id)); if (isset($accounts[$id])) { return $accounts[$id]; } return null; }
function ajaxDeleteRoutingAction() { $translate = DevblocksPlatform::getTranslationService(); if (DEMO_MODE) { return; } $worker = CerberusApplication::getActiveWorker(); if (!$worker || !$worker->is_superuser) { echo $translate->_('common.access_denied'); return; } @($id = DevblocksPlatform::importGPC($_REQUEST['id'])); DAO_Mail::deleteMailboxRouting($id); }