Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: 
  3: /*
  4:  * This file is part of SwiftMailer.
  5:  * (c) 2004-2009 Chris Corbyn
  6:  *
  7:  * For the full copyright and license information, please view the LICENSE
  8:  * file that was distributed with this source code.
  9:  */
 10: 
 11: /**
 12:  * Makes sure a connection to a POP3 host has been established prior to connecting to SMTP.
 13:  *
 14:  * @package Swift
 15:  * @subpackage Plugins
 16:  *
 17:  * @author Chris Corbyn
 18:  */
 19: class Swift_Plugins_PopBeforeSmtpPlugin implements Swift_Events_TransportChangeListener, Swift_Plugins_Pop_Pop3Connection
 20: {
 21:     /** A delegate connection to use (mostly a test hook) */
 22:     private $_connection;
 23: 
 24:     /** Hostname of the POP3 server */
 25:     private $_host;
 26: 
 27:     /** Port number to connect on */
 28:     private $_port;
 29: 
 30:     /** Encryption type to use (if any) */
 31:     private $_crypto;
 32: 
 33:     /** Username to use (if any) */
 34:     private $_username;
 35: 
 36:     /** Password to use (if any) */
 37:     private $_password;
 38: 
 39:     /** Established connection via TCP socket */
 40:     private $_socket;
 41: 
 42:     /** Connect timeout in seconds */
 43:     private $_timeout = 10;
 44: 
 45:     /** SMTP Transport to bind to */
 46:     private $_transport;
 47: 
 48:     /**
 49:      * Create a new PopBeforeSmtpPlugin for $host and $port.
 50:      *
 51:      * @param string $host
 52:      * @param int    $port
 53:      * @param string $cypto as "tls" or "ssl"
 54:      */
 55:     public function __construct($host, $port = 110, $crypto = null)
 56:     {
 57:         $this->_host = $host;
 58:         $this->_port = $port;
 59:         $this->_crypto = $crypto;
 60:     }
 61: 
 62:     /**
 63:      * Create a new PopBeforeSmtpPlugin for $host and $port.
 64:      *
 65:      * @param string $host
 66:      * @param int    $port
 67:      * @param string $cypto as "tls" or "ssl"
 68:      *
 69:      * @return Swift_Plugins_PopBeforeSmtpPlugin
 70:      */
 71:     public static function newInstance($host, $port = 110, $crypto = null)
 72:     {
 73:         return new self($host, $port, $crypto);
 74:     }
 75: 
 76:     /**
 77:      * Set a Pop3Connection to delegate to instead of connecting directly.
 78:      *
 79:      * @param Swift_Plugins_Pop_Pop3Connection $connection
 80:      */
 81:     public function setConnection(Swift_Plugins_Pop_Pop3Connection $connection)
 82:     {
 83:         $this->_connection = $connection;
 84: 
 85:         return $this;
 86:     }
 87: 
 88:     /**
 89:      * Bind this plugin to a specific SMTP transport instance.
 90:      *
 91:      * @param Swift_Transport
 92:      */
 93:     public function bindSmtp(Swift_Transport $smtp)
 94:     {
 95:         $this->_transport = $smtp;
 96:     }
 97: 
 98:     /**
 99:      * Set the connection timeout in seconds (default 10).
100:      *
101:      * @param int $timeout
102:      */
103:     public function setTimeout($timeout)
104:     {
105:         $this->_timeout = (int) $timeout;
106: 
107:         return $this;
108:     }
109: 
110:     /**
111:      * Set the username to use when connecting (if needed).
112:      *
113:      * @param string $username
114:      */
115:     public function setUsername($username)
116:     {
117:         $this->_username = $username;
118: 
119:         return $this;
120:     }
121: 
122:     /**
123:      * Set the password to use when connecting (if needed).
124:      *
125:      * @param string $password
126:      */
127:     public function setPassword($password)
128:     {
129:         $this->_password = $password;
130: 
131:         return $this;
132:     }
133: 
134:     /**
135:      * Connect to the POP3 host and authenticate.
136:      *
137:      * @throws Swift_Plugins_Pop_Pop3Exception if connection fails
138:      */
139:     public function connect()
140:     {
141:         if (isset($this->_connection)) {
142:             $this->_connection->connect();
143:         } else {
144:             if (!isset($this->_socket)) {
145:                 if (!$socket = fsockopen(
146:                     $this->_getHostString(), $this->_port, $errno, $errstr, $this->_timeout))
147:                 {
148:                     throw new Swift_Plugins_Pop_Pop3Exception(
149:                         sprintf('Failed to connect to POP3 host [%s]: %s', $this->_host, $errstr)
150:                     );
151:                 }
152:                 $this->_socket = $socket;
153: 
154:                 if (false === $greeting = fgets($this->_socket)) {
155:                     throw new Swift_Plugins_Pop_Pop3Exception(
156:                         sprintf('Failed to connect to POP3 host [%s]', trim($greeting))
157:                     );
158:                 }
159: 
160:                 $this->_assertOk($greeting);
161: 
162:                 if ($this->_username) {
163:                     $this->_command(sprintf("USER %s\r\n", $this->_username));
164:                     $this->_command(sprintf("PASS %s\r\n", $this->_password));
165:                 }
166:             }
167:         }
168:     }
169: 
170:     /**
171:      * Disconnect from the POP3 host.
172:      */
173:     public function disconnect()
174:     {
175:         if (isset($this->_connection)) {
176:             $this->_connection->disconnect();
177:         } else {
178:             $this->_command("QUIT\r\n");
179:             if (!fclose($this->_socket)) {
180:                 throw new Swift_Plugins_Pop_Pop3Exception(
181:                     sprintf('POP3 host [%s] connection could not be stopped', $this->_host)
182:                 );
183:             }
184:             $this->_socket = null;
185:         }
186:     }
187: 
188:     /**
189:      * Invoked just before a Transport is started.
190:      *
191:      * @param Swift_Events_TransportChangeEvent $evt
192:      */
193:     public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt)
194:     {
195:         if (isset($this->_transport)) {
196:             if ($this->_transport !== $evt->getTransport()) {
197:                 return;
198:             }
199:         }
200: 
201:         $this->connect();
202:         $this->disconnect();
203:     }
204: 
205:     /**
206:      * Not used.
207:      */
208:     public function transportStarted(Swift_Events_TransportChangeEvent $evt)
209:     {
210:     }
211: 
212:     /**
213:      * Not used.
214:      */
215:     public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt)
216:     {
217:     }
218: 
219:     /**
220:      * Not used.
221:      */
222:     public function transportStopped(Swift_Events_TransportChangeEvent $evt)
223:     {
224:     }
225: 
226:     // -- Private Methods
227: 
228:     private function _command($command)
229:     {
230:         if (!fwrite($this->_socket, $command)) {
231:             throw new Swift_Plugins_Pop_Pop3Exception(
232:                 sprintf('Failed to write command [%s] to POP3 host', trim($command))
233:             );
234:         }
235: 
236:         if (false === $response = fgets($this->_socket)) {
237:             throw new Swift_Plugins_Pop_Pop3Exception(
238:                 sprintf('Failed to read from POP3 host after command [%s]', trim($command))
239:             );
240:         }
241: 
242:         $this->_assertOk($response);
243: 
244:         return $response;
245:     }
246: 
247:     private function _assertOk($response)
248:     {
249:         if (substr($response, 0, 3) != '+OK') {
250:             throw new Swift_Plugins_Pop_Pop3Exception(
251:                 sprintf('POP3 command failed [%s]', trim($response))
252:             );
253:         }
254:     }
255: 
256:     private function _getHostString()
257:     {
258:         $host = $this->_host;
259:         switch (strtolower($this->_crypto)) {
260:             case 'ssl':
261:                 $host = 'ssl://' . $host;
262:                 break;
263: 
264:             case 'tls':
265:                 $host = 'tls://' . $host;
266:                 break;
267:         }
268: 
269:         return $host;
270:     }
271: }
272: 
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen