/** * Test POP3 * * @return @e void */ protected function test() { require_once IPS_KERNEL_PATH . '/pop3class/pop3.php'; /*noLibHook*/ $pop3 = new pop3_class(); $pop3->hostname = $this->settings['pop3_server']; $pop3->port = $this->settings['pop3_port']; $pop3->tls = $this->settings['pop3_tls']; $pop3->realm = ''; $pop3->workstation = ''; $pop3->authentication_mechanism = 'USER'; $pop3->debug = FALSE; $pop3->html_debug = FALSE; $pop3->join_continuation_header_lines = TRUE; $user = $this->settings['pop3_user']; $password = $this->settings['pop3_password']; $apop = FALSE; $open = $pop3->Open(); if ($open != '') { $this->registry->output->global_error = '<strong>' . $this->lang->words['pop3_err_connect'] . '</strong><br /> ' . $open; return; } $login = $pop3->Login($user, $password, $apop); if ($login != '') { $this->registry->output->global_error = '<strong>' . $this->lang->words['pop3_err_login'] . '</strong><br /> ' . $login; return; } $messages = NULL; $size = NULL; $pop3->Statistics($messages, $size); if ($messages === NULL or $size === NULL) { $this->registry->output->global_error = $this->lang->words['pop3_err_stats']; } $pop3->Close(); $this->registry->output->global_message = sprintf($this->lang->words['pop3_okay'], $messages); }
function stream_open($path, $mode, $options, &$opened_path) { $this->report_errors = ($options & STREAM_REPORT_ERRORS) != 0; if (strcmp($mode, "r")) { return $this->SetError("the message can only be opened for reading"); } $url = parse_url($path); $host = $url['host']; $pop3 =& pop3_class::SetConnection(0, $host, $this->pop3); if (isset($pop3)) { $this->pop3 =& $pop3; $this->previous_connection = 1; } else { $this->pop3 = new pop3_class(); } if (!$this->ParsePath($path, $url)) { return FALSE; } $message = substr($url["path"], 1); if (strcmp(intval($message), $message) || $message <= 0) { return $this->SetError("it was not specified a valid message to retrieve"); } if (!$this->previous_connection) { if (strlen($error = $this->pop3->Open())) { return $this->SetError($error); } $this->opened = 1; $apop = isset($url["query"]["apop"]) ? intval($url["query"]["apop"]) : 0; if (strlen($error = $this->pop3->Login(UrlDecode($url["user"]), UrlDecode($url["pass"]), $apop))) { $this->stream_close(); return $this->SetError($error); } } if (strlen($error = $this->pop3->OpenMessage($message, -1))) { $this->stream_close(); return $this->SetError($error); } $this->end_of_message = FALSE; if ($options & STREAM_USE_PATH) { $opened_path = $path; } $this->read = 0; $this->buffer = ""; return TRUE; }
function hesk_testPOP3() { global $hesk_settings, $hesklang, $set; $set['pop3_host_name'] = hesk_input(hesk_POST('s_pop3_host_name', 'mail.domain.com')); $set['pop3_host_port'] = intval(hesk_POST('s_pop3_host_port', 110)); $set['pop3_tls'] = empty($_POST['s_pop3_tls']) ? 0 : 1; $set['pop3_keep'] = empty($_POST['s_pop3_keep']) ? 0 : 1; $set['pop3_user'] = hesk_input(hesk_POST('s_pop3_user')); $set['pop3_password'] = hesk_input(hesk_POST('s_pop3_password')); // Initiate POP3 class and set parameters require_once HESK_PATH . 'inc/mail/pop3.php'; $pop3 = new pop3_class(); $pop3->hostname = $set['pop3_host_name']; $pop3->port = $set['pop3_host_port']; $pop3->tls = $set['pop3_tls']; $pop3->debug = 1; $connection_OK = false; ob_start(); // Connect to POP3 if (($error = $pop3->Open()) == "") { // Authenticate if (($error = $pop3->Login($set['pop3_user'], hesk_htmlspecialchars_decode(stripslashes($set['pop3_password'])))) == "") { if (($error = $pop3->Close()) == "") { // Connection OK $connection_OK = true; } } } if ($error != '') { global $pop3_error, $pop3_log; $pop3_error = $error; $pop3_log = ob_get_contents(); } ob_end_clean(); return $connection_OK; }
?> <HTML> <HEAD> <TITLE>Test for Manuel Lemos's PHP POP3 class</TITLE> </HEAD> <BODY> <?php require "pop3.php"; /* Uncomment when using SASL authentication mechanisms */ /* require("sasl.php"); */ if ($_REQUEST['pop3_port'] == '') { $_REQUEST['pop3_port'] = 110; } $pop3 = new pop3_class(); $pop3->hostname = $_REQUEST['smtp']; /* POP 3 server host name */ $pop3->port = $_REQUEST['pop3_port']; /* POP 3 server host port */ $user = $_REQUEST['user']; /* Authentication user name */ $password = $_REQUEST['pass']; /* Authentication password */ $pop3->realm = ""; /* Authentication realm or domain */ $pop3->workstation = ""; /* Workstation for NTLM authentication */ $apop = 0; /* Use APOP authentication */ $pop3->authentication_mechanism = "USER";
# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/. ########################################################################### define('NO_HOUSE_KEEPING', true); require "../config.php"; require dirname(__FILE__) . "/admin_common.php"; JB_admin_header('Admin -> Email Test'); require_once "../include/lib/mail/email_message.php"; require_once "../include/lib/mail/smtp_message.php"; require_once "../include/lib/mail/smtp.php"; require_once "../include/lib/mail/pop3.php"; if ($_REQUEST['pop3_port'] == '') { $_REQUEST['pop3_port'] = 110; } $pop3 = new pop3_class(); $pop3->hostname = $_REQUEST['pop']; /* POP 3 server host name */ $pop3->port = $_REQUEST['pop3_port']; /* POP 3 server host port */ $user = $_REQUEST['user']; /* Authentication user name */ $password = $_REQUEST['pass']; /* Authentication password */ $pop3->realm = ""; /* Authentication realm or domain */ $pop3->workstation = ""; /* Workstation for NTLM authentication */ $apop = 0; /* Use APOP authentication */ $pop3->authentication_mechanism = "USER";
<body> <center><h1>Parsing a message with Manuel Lemos' PHP POP3 and MIME Parser classes</h1></center> <hr /> <?php require 'mime_parser.php'; /*noLibHook*/ require 'rfc822_addresses.php'; /*noLibHook*/ require "pop3.php"; /*noLibHook*/ /* Uncomment when using SASL authentication mechanisms */ /* require("sasl.php");/*noLibHook*/ stream_wrapper_register('pop3', 'pop3_stream'); /* Register the pop3 stream handler class */ $pop3 = new pop3_class(); $pop3->hostname = "localhost"; /* POP 3 server host name */ $pop3->port = 110; /* POP 3 server host port, usually 110 but some servers use other ports Gmail uses 995 */ $pop3->tls = 0; /* Establish secure connections using TLS */ $user = "******"; /* Authentication user name */ $password = "******"; /* Authentication password */ $pop3->realm = ""; /* Authentication realm or domain */ $pop3->workstation = "";
} } // Get other required includes require HESK_PATH . 'inc/pipe_functions.inc.php'; // Get POP3 class require HESK_PATH . 'inc/mail/pop3.php'; // Uncomment when using SASL authentication mechanisms # require(HESK_PATH . 'inc/mail/sasl/sasl.php'); // If a pop3 wrapper is registered un register it, we need our custom wrapper if (in_array('pop3', stream_get_wrappers())) { stream_wrapper_unregister('pop3'); } // Register the pop3 stream handler class stream_wrapper_register('pop3', 'pop3_stream'); // Setup required variables $pop3 = new pop3_class(); $pop3->hostname = $hesk_settings['pop3_host_name']; $pop3->port = $hesk_settings['pop3_host_port']; $pop3->tls = $hesk_settings['pop3_tls']; $pop3->debug = 0; $pop3->join_continuation_header_lines = 1; // Connect to POP3 if (($error = $pop3->Open()) == "") { echo $hesk_settings['debug_mode'] ? "<pre>Connected to the POP3 server "" . $pop3->hostname . "".</pre>\n" : ''; // Authenticate if (($error = $pop3->Login($hesk_settings['pop3_user'], hesk_htmlspecialchars_decode($hesk_settings['pop3_password']))) == "") { echo $hesk_settings['debug_mode'] ? "<pre>User "" . $hesk_settings['pop3_user'] . "" logged in.</pre>\n" : ''; // Get number of messages and total size if (($error = $pop3->Statistics($messages, $size)) == "") { echo $hesk_settings['debug_mode'] ? "<pre>There are {$messages} messages in the mail box with a total of {$size} bytes.</pre>\n" : ''; // If we have any messages, process them
function do_pop_before_smtp() { $now = gmdate("Y-m-d H:i:s"); $unix_time = time(); // get the time of pop $sql = "SELECT * FROM `config` where `key` = 'LAST_MAIL_POP' "; $result = @mysql_query($sql) or $DB_ERROR = mysql_error(); $t_row = @mysql_fetch_array($result); $twenty_min = 60 * 20; if ($unix_time > $t_row['val'] + $twenty_min) { // do the POP if 20 minutes elapsed. require "../mail/pop3.php"; $pop3 = new pop3_class(); $pop3->hostname = EMAIL_POP_SERVER; /* POP 3 server host name */ $pop3->port = POP3_PORT; /* POP 3 server host port */ $user = EMAIL_SMTP_USER; /* Authentication user name */ $password = EMAIL_SMTP_PASS; /* Authentication password */ $pop3->realm = ""; /* Authentication realm or domain */ $pop3->workstation = ""; /* Workstation for NTLM authentication */ $apop = 0; /* Use APOP authentication */ $pop3->authentication_mechanism = "USER"; /* SASL authentication mechanism */ $pop3->debug = 0; /* Output debug information */ $pop3->html_debug = 0; /* Debug information is in HTML */ if (($error = $pop3->Open()) == "") { if (($error = $pop3->Login($user, $password, $apop)) == "") { if (($error = $pop3->Statistics($messages, $size)) == "") { } } } $sql = "REPLACE INTO config (`key`, `val`) VALUES ('LAST_MAIL_POP', '{$unix_time}') "; $result = @mysql_query($sql) or $DB_ERROR = mysql_error(); } }
function auth_user_login($auth, $test_username, $test_password, $settings) { global $webDir; $testauth = false; switch ($auth) { case '1': $unamewhere = get_config('case_insensitive_usernames') ? "= " : "COLLATE utf8_bin = "; $result = Database::get()->querySingle("SELECT password FROM user WHERE username {$unamewhere} ?s", $test_username); if ($result) { foreach ($result as $myrow) { $hasher = new PasswordHash(8, false); if ($hasher->CheckPassword($test_password, $myrow->password)) { $testauth = true; } else { if (strlen($myrow->password) < 60 && md5($test_password) == $myrow->password) { $testauth = true; // password is in old md5 format, update transparently $password_encrypted = $hasher->HashPassword($test_password); Database::get()->query("UPDATE user SET password = ?s WHERE username COLLATE utf8_bin = ?s", $password_encrypted, $test_username); } } } } break; case '2': $pop3 = new pop3_class(); $pop3->hostname = $settings['pop3host']; // POP 3 server host name $pop3->port = 110; // POP 3 server host port $user = $test_username; // Authentication user name $password = $test_password; // Authentication password $pop3->realm = ''; // Authentication realm or domain $pop3->workstation = ''; // Workstation for NTLM authentication $apop = 0; // Use APOP authentication $pop3->authentication_mechanism = 'USER'; // SASL authentication mechanism $pop3->debug = 0; // Output debug information $pop3->html_debug = 1; // Debug information is in HTML $pop3->join_continuation_header_lines = 1; // Concatenate headers split in multiple lines if (($error = $pop3->Open()) == '') { if (($error = $pop3->Login($user, $password, $apop)) == '') { if ($error == '' and ($error = $pop3->Close()) == '') { $testauth = true; } } } if ($error != '') { $testauth = false; } break; case '3': $imaphost = $settings['imaphost']; $imapauth = imap_auth($imaphost, $test_username, $test_password); if ($imapauth) { $testauth = true; } break; case '4': $ldap = ldap_connect($settings['ldaphost']); if (!$ldap) { $GLOBALS['auth_errors'] = 'Error connecting to LDAP host'; return false; } else { // LDAP connection established - now search for user dn @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); if (@ldap_bind($ldap, $settings['ldapbind_dn'], $settings['ldapbind_pw'])) { if (empty($settings['ldap_login_attr2'])) { $search_filter = "({$settings['ldap_login_attr']}={$test_username})"; } else { $search_filter = "(|({$settings['ldap_login_attr']}={$test_username})\n ({$settings['ldap_login_attr2']}={$test_username}))"; } $userinforequest = ldap_search($ldap, $settings['ldap_base'], $search_filter); if ($entry_id = ldap_first_entry($ldap, $userinforequest)) { $user_dn = ldap_get_dn($ldap, $entry_id); if (@ldap_bind($ldap, $user_dn, $test_password)) { $testauth = true; $userinfo = ldap_get_entries($ldap, $userinforequest); if ($userinfo['count'] == 1) { $lastname = get_ldap_attribute($userinfo, 'sn'); $firstname = get_ldap_attribute($userinfo, 'givenname'); if (empty($firstname)) { $cn = get_ldap_attribute($userinfo, 'cn'); $firstname = trim(str_replace($lastname, '', $cn)); } $GLOBALS['auth_user_info'] = array('firstname' => $firstname, 'lastname' => $lastname, 'email' => get_ldap_attribute($userinfo, 'mail')); } } } } else { $GLOBALS['auth_errors'] = ldap_error($ldap); return false; } @ldap_unbind($ldap); } break; case '5': $link = new Database($settings['dbhost'], $settings['dbname'], $settings['dbuser'], $settings['dbpass']); if ($link) { if ($link) { $res = $link->querySingle("SELECT `{$settings['dbfieldpass']}`\n FROM `{$settings['dbtable']}`\n WHERE `{$settings['dbfielduser']}` = ?s", $test_username); if ($res) { $testauth = external_DB_Check_Pass($test_password, $res->{$settings}['dbfieldpass'], $settings['dbpassencr']); } } } break; case '6': $path = "{$webDir}secure/"; if (!file_exists($path)) { if (!mkdir($path, 0700)) { $testauth = false; } } else { $indexfile = $path . 'index.php'; $index_regfile = $path . 'index_reg.php'; // creation of secure/index.php file $f = fopen($indexfile, 'w'); $filecontents = '<?php session_start(); $_SESSION[\'shib_email\'] = ' . $settings['shibemail'] . '; $_SESSION[\'shib_uname\'] = ' . $settings['shibuname'] . '; $_SESSION[\'shib_surname\'] = ' . $settings['shibcn'] . '; header("Location: ../index.php"); '; if (fwrite($f, $filecontents)) { $testauth = true; } fclose($f); // creation of secure/index_reg.php // used in professor request registration process via shibboleth $f = fopen($index_regfile, "w"); $filecontents = '<?php session_start(); $_SESSION[\'shib_email\'] = ' . $settings['shibemail'] . '; $_SESSION[\'shib_uname\'] = ' . $settings['shibuname'] . '; $_SESSION[\'shib_surname\'] = ' . $settings['shibcn'] . '; $_SESSION[\'shib_status\'] = $_SERVER[\'unscoped-affiliation\']; $_SESSION[\'shib_auth\'] = true; header("Location: ../modules/auth/altsearch.php" . (isset($_GET["p"]) && $_GET["p"]? "?p=1": "")); '; if (fwrite($f, $filecontents)) { $testauth = true; } fclose($f); } break; case '7': cas_authenticate($auth); if (phpCAS::checkAuthentication()) { $testauth = true; } break; } return $testauth; }
function JB_monitor_mail_box() { $pop3 = new pop3_class(); $pop3->hostname = MON_EMAIL_POP_SERVER; /* POP 3 server host name */ $pop3->port = MON_POP3_PORT; /* POP 3 server host port */ $user = MON_EMAIL_POP_USER; /* Authentication user name */ $password = MON_EMAIL_POP_PASS; /* Authentication password */ $pop3->realm = ""; /* Authentication realm or domain */ $pop3->workstation = ""; /* Workstation for NTLM authentication */ $apop = 0; /* Use APOP authentication */ $pop3->authentication_mechanism = "USER"; /* SASL authentication mechanism */ $pop3->debug = 0; /* Output debug information */ $pop3->html_debug = 0; /* Debug information is in HTML */ if ($_REQUEST['scan'] != '') { echo "opening Pop Connection"; } if (($error = $pop3->Open()) == "") { if (($error = $pop3->Login($user, $password, $apop)) == "") { if (($error = $pop3->Statistics($messages, $size)) == "") { if ($_REQUEST['scan'] != '') { echo "<PRE>There are {$messages} messages in the mail box with a total of {$size} bytes.</PRE>\n"; echo "<h3>Pop3 connection was successful.</h3>"; } $result = $pop3->ListMessages("", 0); if (GetType($result) == "array") { $result = $pop3->ListMessages('', 1); // list all, unique if (is_array($result)) { for (Reset($result), $message = 0; $message < count($result); Next($result), $message++) { if (($error = $pop3->RetrieveMessage(key($result), $headers, $body, -1)) == "") { $head_txt = ''; $body_txt = ''; for ($line = 0; $line < count($headers); $line++) { $head_txt .= $headers[$line] . "\n"; } preg_match('#Delivery-date: (.+)?\\n#i', $head_txt, $m); $ts = strtotime($m[1]); if (intval(MON_DEL_DAYS) > 0) { if (time() - $ts > 60 * 60 * 24 * MON_DEL_DAYS) { // more than 30 days if (($error = $pop3->DeleteMessage(key($result))) == "") { continue; } } } for ($line = 0; $line < count($body); $line++) { $body_txt .= $body[$line] . "\n"; } $from_name = JB_mon_extract_from_name($head_txt); if ($failed_rec = JB_mon_extract_recipient($head_txt . $body_txt)) { if (JB_match_mon_from_pattern($from_name) && JB_match_mon_body_pattern($body_txt)) { if (JB_mon_match_user_email($failed_rec)) { $user_id = JB_mon_unsubscribe_user($failed_rec); if (($error = $pop3->DeleteMessage(key($result))) == "") { JB_mon_append_log($failed_rec, 'C'); } } if (JB_mon_match_employer_email($failed_rec)) { $user_id = JB_mon_unsubscribe_employer($failed_rec); if (($error = $pop3->DeleteMessage(key($result))) == "") { //echo "dlete ".key($result)."<br>"; JB_mon_append_log($failed_rec, 'E'); } } } } } } if ($error == "" && ($error = $pop3->Close()) == "") { //echo "<PRE>Disconnected from the POP3 server "".$pop3->hostname."".</PRE>\n"; } } else { $error = $result; } } else { $error = $result; } } } } // proces mail_monitor_log $now = gmdate("Y-m-d H:i:s"); if (intval(MON_LOG_DAYS) > 0) { $sql = "DELETE FROM mail_monitor_log WHERE DATE_SUB('{$now}', INTERVAL " . MON_LOG_DAYS . " DAY) >= `date` "; JB_mysql_query($sql); } }
/** * Get the emails from a given server * * @param string $server * @param string $user * @param string $pass * @param boolean $delete whether to delete the emails after * retrieval */ public function readEmailFrom($server, $user, $pass, $delete = true, $secure = false) { $parts = split(":", $server); $port = "110"; if (count($parts) == 2) { $port = $parts[1]; } if ($port == "995") { $secure = true; } $server = $parts[0]; // Connect to mail server include_once dirname(__FILE__) . '/lib/pop3.phpclasses.php'; $pop3 = new pop3_class(); $pop3->hostname = $server; $pop3->port = $port; // $pop3->join_continuation_header_lines=1; if ($secure) { $pop3->tls = 1; } if (($error = $pop3->open()) != "") { throw new Exception($error); } if (($error = $pop3->login($user, $pass)) != "") { throw new Exception($error); } $messageInfo = $pop3->ListMessages("", 0); $pop3->Statistics($messages, $size); $count = $messages; $emails = array(); $this->log->debug("Retrieved {$count} new emails"); for ($i = 1; $i <= $count; $i++) { // If result at $i - 1 > 50000, just delete, it's too big to process $size = $messageInfo[$i]; if ($size > za()->getConfig('email_max_size', 100000)) { // skip for now, NEED TO SEND A BOUNCEBACK! $pop3->DeleteMessage($i); $this->log->err("Deleted email of size {$size}"); continue; } if (($error = $pop3->RetrieveMessage($i, $headers, $body, -1)) != "") { $this->log->err("Failed retrieving message: " . $error); continue; } $email = implode("\r\n", $headers); $email .= "\r\n\r\n" . implode("\r\n", $body); $email .= "\r\n."; $decoder = new Mail_mimeDecode($email); $email = $decoder->decode(array('include_bodies' => true)); if ($delete) { // $this->popService->delete_mail($i); if (($error = $pop3->DeleteMessage($i)) != "") { $this->log->err("Failed deleting message {$i}: " . $error); continue; } $this->log->debug("Deleted message {$i}"); } else { $this->log->debug("No messages being deleted"); } if ($email !== false) { if ($this->isAutoReply($email)) { // log and quit $from = ifset($email->headers, 'from', "unknown@email"); $this->log->warn("AutoReply email from " . $from . " has been ignored"); } else { $emails[] = $email; } } else { $this->log->err("Failed decoding email {$i}"); } } $pop3->Close(); return $emails; }