1 |
<? |
2 |
|
3 |
/* |
4 |
Document manager auth_pop3.php module |
5 |
|
6 |
WARNING: this modules uses e-mail address to check |
7 |
login and password against pop3 server! e-mail must be |
8 |
in following form: |
9 |
|
10 |
login_on_pop3_server@pop3_server.domain |
11 |
|
12 |
That should actually be also a vaild e-mail address |
13 |
|
14 |
|
15 |
this module is based on class.POP3.php3 by cdi@thewebmasters.net |
16 |
*/ |
17 |
|
18 |
function auth_pop3($user) { |
19 |
$email = explode("@",$user[3]); |
20 |
$pop3 = new POP3(); |
21 |
$pop3->connect($email[1]); |
22 |
if ($pop3->checklogin($email[0],$GLOBALS[PHP_AUTH_PW])) { |
23 |
$pop3->quit(); |
24 |
return true; |
25 |
} |
26 |
$pop3->quit(); |
27 |
return false; |
28 |
} |
29 |
|
30 |
//-------------------------------------------------------------------------- |
31 |
|
32 |
/* |
33 |
This is just a part of class.POP3.php3 which is needed for |
34 |
auth_pop3.php module. Please look at original location for |
35 |
whole class! |
36 |
|
37 |
class.POP3.php3 v1.0 99/03/24 CDI cdi@thewebmasters.net |
38 |
Copyright (c) 1999 - CDI (cdi@thewebmasters.net) All Rights Reserved |
39 |
|
40 |
An RFC 1939 compliant wrapper class for the POP3 protocol. |
41 |
*/ |
42 |
|
43 |
class POP3 |
44 |
{ |
45 |
var $ERROR = ""; // Error string. |
46 |
|
47 |
var $TIMEOUT = 60; // Default timeout before giving up on a |
48 |
// network operation. |
49 |
|
50 |
var $COUNT = -1; // Mailbox msg count |
51 |
|
52 |
var $BUFFER = 512; // Socket buffer for socket fgets() calls. |
53 |
// Per RFC 1939 the returned line a POP3 |
54 |
// server can send is 512 bytes. |
55 |
|
56 |
var $FP = ""; // The connection to the server's |
57 |
// file descriptor |
58 |
|
59 |
var $MAILSERVER = ""; // Set this to hard code the server name |
60 |
|
61 |
var $DEBUG = false;// set to true to echo pop3 |
62 |
// commands and responses to error_log |
63 |
// this WILL log passwords! |
64 |
|
65 |
var $BANNER = ""; // Holds the banner returned by the |
66 |
// pop server - used for apop() |
67 |
|
68 |
function POP3 ( $server = "", $timeout = "" ) |
69 |
{ |
70 |
settype($this->BUFFER,"integer"); |
71 |
if(!empty($server)) |
72 |
{ |
73 |
// Do not allow programs to alter MAILSERVER |
74 |
// if it is already specified. They can get around |
75 |
// this if they -really- want to, so don't count on it. |
76 |
if(empty($this->MAILSERVER)) |
77 |
{ |
78 |
$this->MAILSERVER = $server; |
79 |
} |
80 |
} |
81 |
if(!empty($timeout)) |
82 |
{ |
83 |
settype($timeout,"integer"); |
84 |
$this->TIMEOUT = $timeout; |
85 |
set_time_limit($timeout); |
86 |
} |
87 |
return true; |
88 |
} |
89 |
|
90 |
function update_timer () |
91 |
{ |
92 |
set_time_limit($this->TIMEOUT); |
93 |
return true; |
94 |
} |
95 |
|
96 |
function connect ($server, $port = 110) |
97 |
{ |
98 |
// Opens a socket to the specified server. Unless overridden, |
99 |
// port defaults to 110. Returns true on success, false on fail |
100 |
|
101 |
// If MAILSERVER is set, override $server with it's value |
102 |
|
103 |
if(!empty($this->MAILSERVER)) |
104 |
{ |
105 |
$server = $this->MAILSERVER; |
106 |
} |
107 |
|
108 |
if(empty($server)) |
109 |
{ |
110 |
$this->ERROR = "POP3 connect: No server specified"; |
111 |
unset($this->FP); |
112 |
return false; |
113 |
} |
114 |
|
115 |
$fp = fsockopen("$server", $port, &$errno, &$errstr); |
116 |
|
117 |
if(!$fp) |
118 |
{ |
119 |
$this->ERROR = "POP3 connect: Error [$errno] [$errstr]"; |
120 |
unset($this->FP); |
121 |
return false; |
122 |
} |
123 |
|
124 |
set_socket_blocking($fp,-1); |
125 |
$this->update_timer(); |
126 |
$reply = fgets($fp,$this->BUFFER); |
127 |
$reply = $this->strip_clf($reply); |
128 |
if($this->DEBUG) { error_log("POP3 SEND [connect: $server] GOT [$reply]",0); } |
129 |
if(!$this->is_ok($reply)) |
130 |
{ |
131 |
$this->ERROR = "POP3 connect: Error [$reply]"; |
132 |
unset($this->FP); |
133 |
return false; |
134 |
} |
135 |
$this->FP = $fp; |
136 |
$this->BANNER = $this->parse_banner($reply); |
137 |
return true; |
138 |
} |
139 |
|
140 |
//----------------------------- |
141 |
|
142 |
function checklogin ($user, $pass) { |
143 |
$reply = $this->send_cmd("USER $user"); |
144 |
if(!$this->is_ok($reply)) |
145 |
{ |
146 |
$this->ERROR = "POP3 user: Error [$reply]"; |
147 |
return false; |
148 |
} |
149 |
|
150 |
$reply = $this->send_cmd("PASS $pass"); |
151 |
if(!$this->is_ok($reply)) |
152 |
{ |
153 |
$this->ERROR = "POP3 pass: authentication failed [$reply]"; |
154 |
$this->quit(); |
155 |
return false; |
156 |
} |
157 |
// Auth successful. |
158 |
return true; |
159 |
} |
160 |
|
161 |
//------------------------------------------- |
162 |
|
163 |
function noop () |
164 |
{ |
165 |
if(!isset($this->FP)) |
166 |
{ |
167 |
$this->ERROR = "POP3 noop: No connection to server"; |
168 |
return false; |
169 |
} |
170 |
$cmd = "NOOP"; |
171 |
$reply = $this->send_cmd($cmd); |
172 |
if(!$this->is_ok($reply)) |
173 |
{ |
174 |
return false; |
175 |
} |
176 |
return true; |
177 |
} |
178 |
|
179 |
function send_cmd ( $cmd = "" ) |
180 |
{ |
181 |
// Sends a user defined command string to the |
182 |
// POP server and returns the results. Useful for |
183 |
// non-compliant or custom POP servers. |
184 |
// Do NOT include the \r\n as part of your command |
185 |
// string - it will be appended automatically. |
186 |
|
187 |
// The return value is a standard fgets() call, which |
188 |
// will read up to $this->BUFFER bytes of data, until it |
189 |
// encounters a new line, or EOF, whichever happens first. |
190 |
|
191 |
// This method works best if $cmd responds with only |
192 |
// one line of data. |
193 |
|
194 |
if(!isset($this->FP)) |
195 |
{ |
196 |
$this->ERROR = "POP3 send_cmd: No connection to server"; |
197 |
return false; |
198 |
} |
199 |
|
200 |
if(empty($cmd)) |
201 |
{ |
202 |
$this->ERROR = "POP3 send_cmd: Empty command string"; |
203 |
return ""; |
204 |
} |
205 |
|
206 |
$fp = $this->FP; |
207 |
$buffer = $this->BUFFER; |
208 |
$this->update_timer(); |
209 |
fwrite($fp,"$cmd\r\n"); |
210 |
$reply = fgets($fp,$buffer); |
211 |
$reply = $this->strip_clf($reply); |
212 |
if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } |
213 |
return $reply; |
214 |
} |
215 |
|
216 |
function quit () |
217 |
{ |
218 |
// Closes the connection to the POP3 server, deleting |
219 |
// any msgs marked as deleted. |
220 |
|
221 |
if(!isset($this->FP)) |
222 |
{ |
223 |
$this->ERROR = "POP3 quit: connection does not exist"; |
224 |
return false; |
225 |
} |
226 |
$fp = $this->FP; |
227 |
$cmd = "QUIT"; |
228 |
fwrite($fp,"$cmd\r\n"); |
229 |
$reply = fgets($fp,$this->BUFFER); |
230 |
$reply = $this->strip_clf($reply); |
231 |
if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } |
232 |
fclose($fp); |
233 |
unset($this->FP); |
234 |
return true; |
235 |
} |
236 |
|
237 |
// ********************************************************* |
238 |
|
239 |
// The following methods are internal to the class. |
240 |
|
241 |
function is_ok ($cmd = "") |
242 |
{ |
243 |
// Return true or false on +OK or -ERR |
244 |
|
245 |
if(empty($cmd)) { return false; } |
246 |
if ( ereg ("^\+OK", $cmd ) ) { return true; } |
247 |
return false; |
248 |
} |
249 |
|
250 |
function strip_clf ($text = "") |
251 |
{ |
252 |
// Strips \r\n from server responses |
253 |
|
254 |
if(empty($text)) { return $text; } |
255 |
$stripped = ereg_replace("\r","",$text); |
256 |
$stripped = ereg_replace("\n","",$stripped); |
257 |
return $stripped; |
258 |
} |
259 |
|
260 |
function parse_banner ( $server_text ) |
261 |
{ |
262 |
$outside = true; |
263 |
$banner = ""; |
264 |
$length = strlen($server_text); |
265 |
for($count =0; $count < $length; $count++) |
266 |
{ |
267 |
$digit = substr($server_text,$count,1); |
268 |
if(!empty($digit)) |
269 |
{ |
270 |
if( (!$outside) and ($digit != '<') and ($digit != '>') ) |
271 |
{ |
272 |
$banner .= $digit; |
273 |
} |
274 |
if ($digit == '<') |
275 |
{ |
276 |
$outside = false; |
277 |
} |
278 |
if($digit == '>') |
279 |
{ |
280 |
$outside = true; |
281 |
} |
282 |
} |
283 |
} |
284 |
$banner = $this->strip_clf($banner); // Just in case |
285 |
return "<$banner>"; |
286 |
} |
287 |
|
288 |
} // End class |
289 |
|
290 |
?> |