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