PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 56ms app.codeStats 1ms

/library/swift/lib/Swift/Authenticator/POP3SMTP_.php

https://github.com/fb83/Project-Pier
PHP | 167 lines | 56 code | 12 blank | 99 comment | 11 complexity | 2a9cdcdecb0b1b2d6b74c4c868b30e6a MD5 | raw file
  1<?php
  2
  3/**
  4 * This is the POP3 Before SMTP Authentication for Swift Mailer, a PHP Mailer class.
  5 *
  6 * @package	Swift
  7 * @version	>= 2.0.0
  8 * @author	Chris Corbyn
  9 * @date	30th July 2006
 10 * @license http://www.gnu.org/licenses/lgpl.txt Lesser GNU Public License
 11 *
 12 * @copyright Copyright &copy; 2006 Chris Corbyn - All Rights Reserved.
 13 * @filesource
 14 * 
 15 *   This library is free software; you can redistribute it and/or
 16 *   modify it under the terms of the GNU Lesser General Public
 17 *   License as published by the Free Software Foundation; either
 18 *   version 2.1 of the License, or (at your option) any later version.
 19 *
 20 *   This library is distributed in the hope that it will be useful,
 21 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 22 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 23 *   Lesser General Public License for more details.
 24 *
 25 *   You should have received a copy of the GNU Lesser General Public
 26 *   License along with this library; if not, write to
 27 *
 28 *   The Free Software Foundation, Inc.,
 29 *   51 Franklin Street,
 30 *   Fifth Floor,
 31 *   Boston,
 32 *   MA  02110-1301  USA
 33 *
 34 *    "Chris Corbyn" <chris@w3style.co.uk>
 35 *
 36 */
 37
 38/**
 39 * SMTP CRAM-MD5 Authenticator Class.
 40 * Runs the commands needed in order to use LOGIN SMTP authentication
 41 * @package Swift
 42 */
 43class Swift_Authenticator_POP3SMTP implements Swift_IAuthenticator
 44{
 45	/**
 46	 * The string the SMTP server returns to identify
 47	 * that it supports this authentication mechanism
 48	 * @var string serverString
 49	 */
 50	public $serverString = '*POP-SMTP';
 51	/**
 52	 * SwiftInstance parent object
 53	 * @var object SwiftInstance (reference)
 54	 */
 55	protected $baseObject;
 56	/**
 57	 * The port we need to connect to
 58	 * @var int $port
 59	 */
 60	protected $port;
 61	/**
 62	 * The server we need to connect to
 63	 * @var string server
 64	 */
 65	protected $server;
 66	/**
 67	 * The connection to POP3
 68	 * @var resource connect handle
 69	 */
 70	protected $socket;
 71	
 72	public function __construct($server, $port=110)
 73	{
 74		$this->port = $port;
 75		$this->server = $server;
 76	}
 77	/**
 78	 * Loads an instance of Swift to the Plugin
 79	 *
 80	 * @param	object	SwiftInstance
 81	 * @return	void
 82	 */
 83	public function loadBaseObject(&$object)
 84	{
 85		$this->baseObject =& $object;
 86	}
 87	/**
 88	 * Executes the logic in the authentication mechanism
 89	 *
 90	 * @param	string	username
 91	 * @param	string	password
 92	 * @return	bool	successful
 93	 */
 94	public function run($username, $password)
 95	{
 96		return $this->popB4SMTP($username, $password);
 97	}
 98	/**
 99	 * Connect to the POP3 server and return true on success
100	 * @return bool success
101	 */
102	protected function connect()
103	{
104		$this->socket = @fsockopen($this->server, $this->port, $errno, $errstr, 15);
105		
106		if (!$this->socket) return false;
107		if (!$this->isOK($this->response())) return false;
108		
109		return true;
110	}
111	/**
112	 * Check for an +OK string
113	 * @return bool +OK
114	 */
115	protected function isOK($string)
116	{
117		if (substr($string, 0, 3) == '+OK') return true;
118		else return false;
119	}
120	/**
121	 * Send a command to the server
122	 */
123	protected function command($comm)
124	{
125		@fwrite($this->socket, $comm);
126	}
127	/**
128	 * Read the server response
129	 * @return string response
130	 */
131	protected function response()
132	{
133		return fgets($this->socket);
134	}
135	/**
136	 * Executes the logic in the authentication mechanism
137	 *
138	 * @param	string	username
139	 * @param	string	password
140	 * @return	bool	successful
141	 */
142	protected function popB4SMTP($username, $password)
143	{
144		//Kill any open session so we can authenticate to POP3 first
145		$this->baseObject->close();
146		
147		if (!$this->connect()) return false;
148		
149		$this->command("USER $username\r\n");
150		if (!$this->isOK($this->response())) return false;
151		
152		$this->command("PASS $password\r\n");
153		if (!$this->isOK($this->response())) return false;
154
155//START patch from Jakob Truelsen - antialize (SF.net)	
156		$this->command("QUIT\r\n");
157		if (!$this->isOK($this->response())) return false;
158//END patch
159
160		//Reconnect to the SMTP server
161		if ($this->baseObject->connect()) return true;
162		else return false;
163	}
164	
165}
166
167?>