From 4e1aa60c167b3593b8a1081c1ff60557e90c2d0f Mon Sep 17 00:00:00 2001 From: Aleksander Machniak <alec@alec.pl> Date: Thu, 07 Jan 2016 03:48:18 -0500 Subject: [PATCH] Make possible to disable authentication via rcube_smtp::connect() arguments (#1490621) --- program/lib/Roundcube/rcube_smtp.php | 71 ++++++++++++++++++++--------------- 1 files changed, 40 insertions(+), 31 deletions(-) diff --git a/program/lib/Roundcube/rcube_smtp.php b/program/lib/Roundcube/rcube_smtp.php index 70f15dc..fc3f28c 100644 --- a/program/lib/Roundcube/rcube_smtp.php +++ b/program/lib/Roundcube/rcube_smtp.php @@ -1,6 +1,6 @@ <?php -/* +/** +-----------------------------------------------------------------------+ | This file is part of the Roundcube Webmail client | | Copyright (C) 2005-2012, The Roundcube Dev Team | @@ -26,7 +26,7 @@ */ class rcube_smtp { - private $conn = null; + private $conn; private $response; private $error; private $anonymize_log = 0; @@ -47,7 +47,7 @@ * * @return bool Returns true on success, or false on error */ - public function connect($host=null, $port=null, $user=null, $pass=null) + public function connect($host = null, $port = null, $user = null, $pass = null) { $rcube = rcube::get_instance(); @@ -59,10 +59,10 @@ // let plugins alter smtp connection config $CONFIG = $rcube->plugins->exec_hook('smtp_connect', array( - 'smtp_server' => $host ? $host : $rcube->config->get('smtp_server'), - 'smtp_port' => $port ? $port : $rcube->config->get('smtp_port', 25), - 'smtp_user' => $user ? $user : $rcube->config->get('smtp_user'), - 'smtp_pass' => $pass ? $pass : $rcube->config->get('smtp_pass'), + 'smtp_server' => $host ?: $rcube->config->get('smtp_server'), + 'smtp_port' => $port ?: $rcube->config->get('smtp_port', 25), + 'smtp_user' => $user !== null ? $user : $rcube->config->get('smtp_user'), + 'smtp_pass' => $pass !== null ? $pass : $rcube->config->get('smtp_pass'), 'smtp_auth_cid' => $rcube->config->get('smtp_auth_cid'), 'smtp_auth_pw' => $rcube->config->get('smtp_auth_pw'), 'smtp_auth_type' => $rcube->config->get('smtp_auth_type'), @@ -126,10 +126,13 @@ // try to connect to server and exit on failure $result = $this->conn->connect($CONFIG['smtp_timeout']); - if (PEAR::isError($result)) { - $this->response[] = "Connection failed: ".$result->getMessage(); - $this->error = array('label' => 'smtpconnerror', 'vars' => array('code' => $this->conn->_code)); + if (is_a($result, 'PEAR_Error')) { + $this->response[] = "Connection failed: " . $result->getMessage(); + + list($code,) = $this->conn->getResponse(); + $this->error = array('label' => 'smtpconnerror', 'vars' => array('code' => $code)); $this->conn = null; + return false; } @@ -142,7 +145,7 @@ $smtp_user = str_replace('%u', $rcube->get_user_name(), $CONFIG['smtp_user']); $smtp_pass = str_replace('%p', $rcube->get_user_password(), $CONFIG['smtp_pass']); - $smtp_auth_type = empty($CONFIG['smtp_auth_type']) ? NULL : $CONFIG['smtp_auth_type']; + $smtp_auth_type = $CONFIG['smtp_auth_type'] ?: null; if (!empty($CONFIG['smtp_auth_cid'])) { $smtp_authz = $smtp_user; @@ -159,11 +162,15 @@ $result = $this->conn->auth($smtp_user, $smtp_pass, $smtp_auth_type, $use_tls, $smtp_authz); - if (PEAR::isError($result)) { - $this->error = array('label' => 'smtpautherror', 'vars' => array('code' => $this->conn->_code)); - $this->response[] .= 'Authentication failure: ' . $result->getMessage() . ' (Code: ' . $result->getCode() . ')'; + if (is_a($result, 'PEAR_Error')) { + list($code,) = $this->conn->getResponse(); + $this->error = array('label' => 'smtpautherror', 'vars' => array('code' => $code)); + $this->response[] = 'Authentication failure: ' . $result->getMessage() + . ' (Code: ' . $result->getCode() . ')'; + $this->reset(); $this->disconnect(); + return false; } } @@ -208,11 +215,6 @@ else if (is_string($headers)) { $text_headers = $headers; } - else { - $this->reset(); - $this->response[] = "Invalid message headers"; - return false; - } // exit if no from address is given if (!isset($from)) { @@ -240,18 +242,20 @@ } // set From: address - if (PEAR::isError($this->conn->mailFrom($from, $from_params))) { + $result = $this->conn->mailFrom($from, $from_params); + if (is_a($result, 'PEAR_Error')) { $err = $this->conn->getResponse(); $this->error = array('label' => 'smtpfromerror', 'vars' => array( - 'from' => $from, 'code' => $this->conn->_code, 'msg' => $err[1])); - $this->response[] = "Failed to set sender '$from'"; + 'from' => $from, 'code' => $err[0], 'msg' => $err[1])); + $this->response[] = "Failed to set sender '$from'. " + . $err[1] . ' (Code: ' . $err[0] . ')'; $this->reset(); return false; } // prepare list of recipients $recipients = $this->_parse_rfc822($recipients); - if (PEAR::isError($recipients)) { + if (is_a($recipients, 'PEAR_Error')) { $this->error = array('label' => 'smtprecipientserror'); $this->reset(); return false; @@ -259,11 +263,13 @@ // set mail recipients foreach ($recipients as $recipient) { - if (PEAR::isError($this->conn->rcptTo($recipient, $recipient_params))) { + $result = $this->conn->rcptTo($recipient, $recipient_params); + if (is_a($result, 'PEAR_Error')) { $err = $this->conn->getResponse(); $this->error = array('label' => 'smtptoerror', 'vars' => array( - 'to' => $recipient, 'code' => $this->conn->_code, 'msg' => $err[1])); - $this->response[] = "Failed to add recipient '$recipient'"; + 'to' => $recipient, 'code' => $err[0], 'msg' => $err[1])); + $this->response[] = "Failed to add recipient '$recipient'. " + . $err[1] . ' (Code: ' . $err[0] . ')'; $this->reset(); return false; } @@ -271,8 +277,11 @@ if (is_resource($body)) { // file handle - $data = $body; - $text_headers = preg_replace('/[\r\n]+$/', '', $text_headers); + $data = $body; + + if ($text_headers) { + $text_headers = preg_replace('/[\r\n]+$/', '', $text_headers); + } } else { // Concatenate headers and body so it can be passed by reference to SMTP_CONN->data @@ -286,7 +295,8 @@ } // Send the message's headers and the body as SMTP data. - if (PEAR::isError($result = $this->conn->data($data, $text_headers))) { + $result = $this->conn->data($data, $text_headers); + if (is_a($result, 'PEAR_Error')) { $err = $this->conn->getResponse(); if (!in_array($err[0], array(354, 250, 221))) { $msg = sprintf('[%d] %s', $err[0], $err[1]); @@ -296,7 +306,7 @@ } $this->error = array('label' => 'smtperror', 'vars' => array('msg' => $msg)); - $this->response[] = "Failed to send data"; + $this->response[] = "Failed to send data. " . $msg; $this->reset(); return false; } @@ -325,7 +335,6 @@ $this->conn = null; } } - /** * This is our own debug handler for the SMTP connection -- Gitblit v1.9.1