mail.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:9k
- <?php
- // +-------------------------------------------------------------+
- // | DeskPRO v [2.0.1 Production]
- // | Copyright (C) 2001 - 2004 Headstart Solutions Limited
- // | Supplied by WTN-WDYL
- // | Nullified by WTN-WDYL
- // | Distribution via WebForum, ForumRU and associated file dumps
- // +-------------------------------------------------------------+
- // | DESKPRO IS NOT FREE SOFTWARE
- // +-------------------------------------------------------------+
- // | License ID : Full Enterprise License =) ...
- // | License Owner : WTN-WDYL Team
- // +-------------------------------------------------------------+
- // | $RCSfile: mail.php,v $
- // | $Date: 2004/02/11 20:32:13 $
- // | $Revision: 1.39 $
- // +-------------------------------------------------------------+
- // | File Details:
- // | - Mail gateway handler and wrapper.
- // +-------------------------------------------------------------+
- error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
- define('GATEWAYZONE', 1);
- ############################### INCLUDE FILES ###############################
- require_once('functions.php');
- require_once(INCLUDE_PATH . 'config.php');
- require_once(INCLUDE_PATH . 'license.php');
- require_once(INCLUDE_PATH . 'functions/general_functions.php');
- require_once(INCLUDE_PATH . 'functions/fields_functions.php');
- require_once(INCLUDE_PATH . 'functions/mail/class_mimeDecode.php');
- require_once(INCLUDE_PATH . 'functions/mail/mailDecoding_functions.php');
- require_once(INCLUDE_PATH . 'functions/mail/class_RFC822.php');
- if (constant('DATABASE_TYPE') == 'mysql') {
- require_once(INCLUDE_PATH . 'functions/database/mysql.php');
- new_db_class();
- }
- ############################### SETTINGS + REPLACEMENTS ###############################
- get_settings();
- $use_smtp = $settings['use_smtp'];
- $smtp_settings['host'] = $settings['smtp_host'];
- $smtp_settings['port'] = $settings['smtp_port'];
- $smtp_settings['helo'] = $settings['smtp_helo'];
- $smtp_settings['auth'] = $settings['smtp_auth'];
- $smtp_settings['user'] = $settings['smtp_user'];
- $smtp_settings['pass'] = $settings['smtp_pass'];
- $db->query("SELECT * FROM template_words WHERE !language OR language = '$settings[default_language]'");
- while ($result = $db->row_array()) {
- if ($result['language'] == '0') {
- $default_lang[$result['wordref']] = trim($result['text']);
- } elseif ($result['language'] == $settings['default_language']) {
- $start_lang[$result['wordref']] = trim($result['text']);
- } else {
- $this_lang[$result['wordref']] = trim($result['text']);
- }
- }
- $dplang = array_merge($this_lang, $start_lang);
- $dplang = array_merge($dplang, $default_lang);
- ############################### LANGUAGES ###############################
- $db->query("SELECT * FROM template_words WHERE language = '0' OR language = '$settings[default_language]'");
- while ($result = $db->row_array()) {
- if ($result[language] == '0') {
- $start_lang[$result[wordref]] = trim($result[text]);
- } elseif ($result[language] == $settings[default_language]) {
- $default_lang[$result[wordref]] = trim($result[text]);
- } else {
- $this_lang[$result[wordref]] = trim($result[text]);
- }
- }
- $dplang = array_merge($start_lang, $default_lang);
- $dplang = array_merge($dplang, $this_lang);
- ############################### DEAL WITH EMAILS ###############################
- /*
- Emails are collected from the mechanism that the user sets and then are processed
- by the function process() which is set by the file that init.php is actually
- being called from
- */
- ////////////////////////////////////////////////////////////////////////////////
- // First, figure out how big messages can be. We subtract 2K from the database's
- // max_allowed_packet setting so there's no surprises.
- $max_size = $db->query_return("show variables like '%max_allowed_packet%'");
- $max_size = $max_size[1] - 2048;
- ////////////////////////////////////////////////////////////////////////////////
- // Next, sort out how long we have to run before we're gracelessly killed by
- // the PHP interpreter.
- $max_runtime = ini_get('max_execution_time');
- if ($max_runtime > 5) {
- if (($max_runtime * 0.05) > 5) {
- $max_runtime = $max_runtime * 0.95;
- } else {
- $max_runtime = $max_runtime - 5;
- }
- }
- //////////////////// PART 3 GET EMAIL (FILE METHOD) ////////////////////
- if (constant('GATEWAY_METHOD') == 'FILE') {
- /*
- - no size tests are done here because FILE method is only used for testing
- - reads all the emails found in a directory
- */
- $dir = opendir(FILE_PATH);
- while ($file = readdir($dir)) {
- if (is_file(FILE_PATH . '/' . $file)) {
-
- $fp = fopen(FILE_PATH . '/' . $file, "r");
- $data = fread($fp, filesize(FILE_PATH . '/' . $file));
- print_rr(find_email($data));
- exit();
- process(decodeMail($data), $data);
- fclose($fp);
- echo "Procssed email $file<br />";
- }
- }
- }
- //////////////////// PART 3 GET EMAIL (PIPE METHOD) ////////////////////
- if (constant('GATEWAY_METHOD') == 'PIPE') {
- // check max pipe size setting to reduce max size - used to check on max attachment size
- if ($settings['max_message_size_pipe']) {
- if ($settings['max_message_size_pipe'] < $max_size) {
- $max_size = $settings['max_message_size_pipe'];
- }
- }
- // get the incoming data
- $data = get_pipe();
- // if the email is too big to process
- if ((strlen($data) > $max_size)) {
- // need to email the user asking them to send a smaller email
- $user['email'] == find_email($data);
- log_error('message_too_big');
- } else {
- process(decodeMail($data), $data);
- }
- }
- //////////////////// PART 3 GET EMAIL (POP3 METHOD) ////////////////////
- if (constant('GATEWAY_METHOD') == 'POP' or constant('GATEWAY_METHOD') == 'POP3') {
- // Constrain message size by admin setting for POP mail size maximum
- if ($settings['max_message_size_pop']) {
- if ($settings['max_message_size_pop'] < $max_size) {
- $max_size = $settings['max_message_size_pop'];
- }
- }
- $starttime = time();
- if (defined('TECHPOP')) {
- $target = 'tech';
- } elseif (defined('RETURNPOP')) {
- $target = 'return';
- } elseif (defined('TECHMAILPOP')) {
- $target = 'techmail';
- } else {
- $target = 'user';
- }
- $db->query("SELECT * FROM gateway_pop_accounts WHERE target = '$target'");
- while ($res = $db->row_array()) {
- $pop3_accounts[] = $res;
- $ids[$res['accountid']] = 1;
- }
- if (is_array($pop3_accounts)) {
- $ids = array2sql(array_keys($ids));
- $db->query("
- SELECT
- gateway_accounts.auto_new, gateway_accounts.auto_reply, gateway_accounts.email,
- gateway_accounts.is_default, gateway_accounts.id,
- ticket_pri.id AS priority_id, ticket_pri.name AS priority,
- ticket_cat.id AS category_id, ticket_cat.name AS category,
- tech.id AS tech_id, tech.email AS tech_email
- FROM gateway_accounts
- LEFT JOIN ticket_pri ON (ticket_pri.id = gateway_accounts.priority)
- LEFT JOIN ticket_cat ON (ticket_cat.id = gateway_accounts.category)
- LEFT JOIN tech ON (tech.id = gateway_accounts.tech)
- WHERE gateway_accounts.id IN $ids
- ");
- while ($res = $db->row_array()) {
- $gateways[$res['id']] = $res;
- }
- require_once(INCLUDE_PATH . 'functions/pop3.php');
-
- foreach ($pop3_accounts AS $account) {
- // get the gateway account for user email collection
- $gateway = $gateways[$account['accountid']];
-
- // Connect
- global $pop;
- $pop = new POP_Socket(
- array(
- 'server' => $account['server'],
- 'port' => '110',
- 'username' => $account['username'],
- 'password' => $account['password'],
- 'deletemails' => 1
- )
- );
- // Skip this server if we can't connect or authenticate to it.
- if (!$pop->connect()) {
- log_pop_error("Couldn't connect to server");
- continue;
- }
- if (!$pop->auth()) {
- log_pop_error("Couldn't authenticate to server");
- continue;
- }
- if (!$pop->get_list()) {
- log_pop_error("Couldn't download message list");
- continue;
- }
- $pop_msgs = $pop->msgNums;
- foreach ($pop_msgs AS $msgid => $msgsize) {
- if (($max_runtime) AND (time() - $starttime) >= $max_runtime) {
- // If we're about to exceed the script's maximum runtime, quit processing and exit gracefully
- break;
- $break_now = 1;
- }
- if (($max_size) AND $msgsize > $max_size) {
- // Get the headers, log the failure in gateway_pop_failures
- $headers = '';
- if (!$pop->get_top($msgid, $headers, 50)) {
- log_pop_error("Couldn't retrieve headers for message $msgid");
- continue 2;
- } else {
- log_pop_error("Message $msgid too big; first 50 lines:n$headers");
- continue;
- }
- } else {
- $message = '';
- $pop->get_email($msgid, $message);
- if (!strlen($message)) {
- log_pop_error("Couldn't download message $msgid");
- continue 2;
- }
- if (process(decodeMail($message), $message, $gateway)) {
- // Message processed, can delete.
- if (!$pop->delete_email($msgid)) {
- log_pop_error("Couldn't delete message $msgid");
- continue 2;
- }
- }
- }
- }
- if ($break_now) {
- break;
- }
- // Log out and finish up (note that emails will not be deleted until we get here
- $pop->close();
- }
- }
- }