Project-Pier /library/swift/lib/Swift/Authenticator/POP3SMTP_.php

Language PHP Lines 167
MD5 Hash 2a9cdcdecb0b1b2d6b74c4c868b30e6a
Repository https://github.com/fb83/Project-Pier.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

/**
 * This is the POP3 Before SMTP Authentication for Swift Mailer, a PHP Mailer class.
 *
 * @package	Swift
 * @version	>= 2.0.0
 * @author	Chris Corbyn
 * @date	30th July 2006
 * @license http://www.gnu.org/licenses/lgpl.txt Lesser GNU Public License
 *
 * @copyright Copyright &copy; 2006 Chris Corbyn - All Rights Reserved.
 * @filesource
 * 
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to
 *
 *   The Free Software Foundation, Inc.,
 *   51 Franklin Street,
 *   Fifth Floor,
 *   Boston,
 *   MA  02110-1301  USA
 *
 *    "Chris Corbyn" <chris@w3style.co.uk>
 *
 */

/**
 * SMTP CRAM-MD5 Authenticator Class.
 * Runs the commands needed in order to use LOGIN SMTP authentication
 * @package Swift
 */
class Swift_Authenticator_POP3SMTP implements Swift_IAuthenticator
{
	/**
	 * The string the SMTP server returns to identify
	 * that it supports this authentication mechanism
	 * @var string serverString
	 */
	public $serverString = '*POP-SMTP';
	/**
	 * SwiftInstance parent object
	 * @var object SwiftInstance (reference)
	 */
	protected $baseObject;
	/**
	 * The port we need to connect to
	 * @var int $port
	 */
	protected $port;
	/**
	 * The server we need to connect to
	 * @var string server
	 */
	protected $server;
	/**
	 * The connection to POP3
	 * @var resource connect handle
	 */
	protected $socket;
	
	public function __construct($server, $port=110)
	{
		$this->port = $port;
		$this->server = $server;
	}
	/**
	 * Loads an instance of Swift to the Plugin
	 *
	 * @param	object	SwiftInstance
	 * @return	void
	 */
	public function loadBaseObject(&$object)
	{
		$this->baseObject =& $object;
	}
	/**
	 * Executes the logic in the authentication mechanism
	 *
	 * @param	string	username
	 * @param	string	password
	 * @return	bool	successful
	 */
	public function run($username, $password)
	{
		return $this->popB4SMTP($username, $password);
	}
	/**
	 * Connect to the POP3 server and return true on success
	 * @return bool success
	 */
	protected function connect()
	{
		$this->socket = @fsockopen($this->server, $this->port, $errno, $errstr, 15);
		
		if (!$this->socket) return false;
		if (!$this->isOK($this->response())) return false;
		
		return true;
	}
	/**
	 * Check for an +OK string
	 * @return bool +OK
	 */
	protected function isOK($string)
	{
		if (substr($string, 0, 3) == '+OK') return true;
		else return false;
	}
	/**
	 * Send a command to the server
	 */
	protected function command($comm)
	{
		@fwrite($this->socket, $comm);
	}
	/**
	 * Read the server response
	 * @return string response
	 */
	protected function response()
	{
		return fgets($this->socket);
	}
	/**
	 * Executes the logic in the authentication mechanism
	 *
	 * @param	string	username
	 * @param	string	password
	 * @return	bool	successful
	 */
	protected function popB4SMTP($username, $password)
	{
		//Kill any open session so we can authenticate to POP3 first
		$this->baseObject->close();
		
		if (!$this->connect()) return false;
		
		$this->command("USER $username\r\n");
		if (!$this->isOK($this->response())) return false;
		
		$this->command("PASS $password\r\n");
		if (!$this->isOK($this->response())) return false;

//START patch from Jakob Truelsen - antialize (SF.net)	
		$this->command("QUIT\r\n");
		if (!$this->isOK($this->response())) return false;
//END patch

		//Reconnect to the SMTP server
		if ($this->baseObject->connect()) return true;
		else return false;
	}
	
}

?>
Back to Top