Changeset 8762 for trunk/wp-includes/class-pop3.php
- Timestamp:
- 08/28/2008 08:26:36 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/class-pop3.php
r8082 r8762 1 1 <?php 2 /*~ class.pop3.php 3 .---------------------------------------------------------------------------. 4 | Software: PHPMailer - PHP email class | 5 | Version: 2.0.2 | 6 | Contact: via sourceforge.net support pages (also www.codeworxtech.com) | 7 | Info: http://phpmailer.sourceforge.net | 8 | Support: http://sourceforge.net/projects/phpmailer/ | 9 | ------------------------------------------------------------------------- | 10 | Author: Andy Prevost (project admininistrator) | 11 | Author: Brent R. Matzelle (original founder) | 12 | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. | 13 | Copyright (c) 2001-2003, Brent R. Matzelle | 14 | ------------------------------------------------------------------------- | 15 | License: Distributed under the Lesser General Public License (LGPL) | 16 | http://www.gnu.org/copyleft/lesser.html | 17 | This program is distributed in the hope that it will be useful - WITHOUT | 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 19 | FITNESS FOR A PARTICULAR PURPOSE. | 20 | ------------------------------------------------------------------------- | 21 | We offer a number of paid services (www.codeworxtech.com): | 22 | - Web Hosting on highly optimized fast and secure servers | 23 | - Technology Consulting | 24 | - Oursourcing (highly qualified programmers and graphic designers) | 25 '---------------------------------------------------------------------------' 26 2 27 /** 3 * mail_fetch/setup.php28 * POP Before SMTP Authentication Class 4 29 * 5 * @package SquirrelMail 30 * Author: Richard Davey (rich@corephp.co.uk) 31 * License: LGPL, see PHPMailer License 6 32 * 7 * @copyright (c) 1999-2006 The SquirrelMail Project Team 33 * Specifically for PHPMailer to allow POP before SMTP authentication. 34 * Does not yet work with APOP - if you have an APOP account, contact me 35 * and we can test changes to this script. 8 36 * 9 * @copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved 10 * Modified by Philippe Mingo 2001 mingo@rotedic.com 11 * An RFC 1939 compliant wrapper class for the POP3 protocol. 37 * This class is based on the structure of the SMTP class by Chris Ryan 12 38 * 13 * Licensed under the GNU GPL. For full terms see the file COPYING. 39 * This class is rfc 1939 compliant and implements all the commands 40 * required for POP3 connection, authentication and disconnection. 14 41 * 15 * pop3 class 16 * 17 * $Id$ 42 * @package PHPMailer 43 * @author Richard Davey 18 44 */ 19 45 20 /** 21 * POP3 22 * 23 * @package SquirrelMail 24 */ 25 class POP3 { 26 var $ERROR = ''; // Error string. 27 28 var $TIMEOUT = 60; // Default timeout before giving up on a 29 // network operation. 30 31 var $COUNT = -1; // Mailbox msg count 32 33 var $BUFFER = 512; // Socket buffer for socket fgets() calls. 34 // Per RFC 1939 the returned line a POP3 35 // server can send is 512 bytes. 36 37 var $FP = ''; // The connection to the server's 38 // file descriptor 39 40 var $MAILSERVER = ''; // Set this to hard code the server name 41 42 var $DEBUG = FALSE; // set to true to echo pop3 43 // commands and responses to error_log 44 // this WILL log passwords! 45 46 var $BANNER = ''; // Holds the banner returned by the 47 // pop server - used for apop() 48 49 var $ALLOWAPOP = FALSE; // Allow or disallow apop() 50 // This must be set to true 51 // manually 52 53 function POP3 ( $server = '', $timeout = '' ) { 54 settype($this->BUFFER,"integer"); 55 if( !empty($server) ) { 56 // Do not allow programs to alter MAILSERVER 57 // if it is already specified. They can get around 58 // this if they -really- want to, so don't count on it. 59 if(empty($this->MAILSERVER)) 60 $this->MAILSERVER = $server; 61 } 62 if(!empty($timeout)) { 63 settype($timeout,"integer"); 64 $this->TIMEOUT = $timeout; 65 if (!ini_get('safe_mode')) 66 set_time_limit($timeout); 67 } 46 class POP3 47 { 48 /** 49 * Default POP3 port 50 * @var int 51 */ 52 var $POP3_PORT = 110; 53 54 /** 55 * Default Timeout 56 * @var int 57 */ 58 var $POP3_TIMEOUT = 30; 59 60 /** 61 * POP3 Carriage Return + Line Feed 62 * @var string 63 */ 64 var $CRLF = "\r\n"; 65 66 /** 67 * Displaying Debug warnings? (0 = now, 1+ = yes) 68 * @var int 69 */ 70 var $do_debug = 2; 71 72 /** 73 * POP3 Mail Server 74 * @var string 75 */ 76 var $host; 77 78 /** 79 * POP3 Port 80 * @var int 81 */ 82 var $port; 83 84 /** 85 * POP3 Timeout Value 86 * @var int 87 */ 88 var $tval; 89 90 /** 91 * POP3 Username 92 * @var string 93 */ 94 var $username; 95 96 /** 97 * POP3 Password 98 * @var string 99 */ 100 var $password; 101 102 /**#@+ 103 * @access private 104 */ 105 var $pop_conn; 106 var $connected; 107 var $error; // Error log array 108 /**#@-*/ 109 110 /** 111 * Constructor, sets the initial values 112 * 113 * @return POP3 114 */ 115 function POP3 () 116 { 117 $this->pop_conn = 0; 118 $this->connected = false; 119 $this->error = null; 120 } 121 122 /** 123 * Combination of public events - connect, login, disconnect 124 * 125 * @param string $host 126 * @param integer $port 127 * @param integer $tval 128 * @param string $username 129 * @param string $password 130 */ 131 function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) 132 { 133 $this->host = $host; 134 135 // If no port value is passed, retrieve it 136 if ($port == false) 137 { 138 $this->port = $this->POP3_PORT; 139 } 140 else 141 { 142 $this->port = $port; 143 } 144 145 // If no port value is passed, retrieve it 146 if ($tval == false) 147 { 148 $this->tval = $this->POP3_TIMEOUT; 149 } 150 else 151 { 152 $this->tval = $tval; 153 } 154 155 $this->do_debug = $debug_level; 156 $this->username = $username; 157 $this->password = $password; 158 159 // Refresh the error log 160 $this->error = null; 161 162 // Connect 163 $result = $this->Connect($this->host, $this->port, $this->tval); 164 165 if ($result) 166 { 167 $login_result = $this->Login($this->username, $this->password); 168 169 if ($login_result) 170 { 171 $this->Disconnect(); 172 68 173 return true; 69 } 70 71 function update_timer () { 72 if (!ini_get('safe_mode')) 73 set_time_limit($this->TIMEOUT); 174 } 175 176 } 177 178 // We need to disconnect regardless if the login succeeded 179 $this->Disconnect(); 180 181 return false; 182 } 183 184 /** 185 * Connect to the POP3 server 186 * 187 * @param string $host 188 * @param integer $port 189 * @param integer $tval 190 * @return boolean 191 */ 192 function Connect ($host, $port = false, $tval = 30) 193 { 194 // Are we already connected? 195 if ($this->connected) 196 { 197 return true; 198 } 199 200 /* 201 On Windows this will raise a PHP Warning error if the hostname doesn't exist. 202 Rather than supress it with @fsockopen, let's capture it cleanly instead 203 */ 204 205 set_error_handler(array(&$this, 'catchWarning')); 206 207 // Connect to the POP3 server 208 $this->pop_conn = fsockopen($host, // POP3 Host 209 $port, // Port # 210 $errno, // Error Number 211 $errstr, // Error Message 212 $tval); // Timeout (seconds) 213 214 // Restore the error handler 215 restore_error_handler(); 216 217 // Does the Error Log now contain anything? 218 if ($this->error && $this->do_debug >= 1) 219 { 220 $this->displayErrors(); 221 } 222 223 // Did we connect? 224 if ($this->pop_conn == false) 225 { 226 // It would appear not... 227 $this->error = array( 228 'error' => "Failed to connect to server $host on port $port", 229 'errno' => $errno, 230 'errstr' => $errstr 231 ); 232 233 if ($this->do_debug >= 1) 234 { 235 $this->displayErrors(); 236 } 237 238 return false; 239 } 240 241 // Increase the stream time-out 242 243 // Check for PHP 4.3.0 or later 244 if (version_compare(phpversion(), '4.3.0', 'ge')) 245 { 246 stream_set_timeout($this->pop_conn, $tval, 0); 247 } 248 else 249 { 250 // Does not work on Windows 251 if (substr(PHP_OS, 0, 3) !== 'WIN') 252 { 253 socket_set_timeout($this->pop_conn, $tval, 0); 254 } 255 } 256 257 // Get the POP3 server response 258 $pop3_response = $this->getResponse(); 259 260 // Check for the +OK 261 if ($this->checkResponse($pop3_response)) 262 { 263 // The connection is established and the POP3 server is talking 264 $this->connected = true; 74 265 return true; 75 } 76 77 function connect ($server, $port = 110) { 78 // Opens a socket to the specified server. Unless overridden, 79 // port defaults to 110. Returns true on success, false on fail 80 81 // If MAILSERVER is set, override $server with it's value 82 83 if (!isset($port) || !$port) {$port = 110;} 84 if(!empty($this->MAILSERVER)) 85 $server = $this->MAILSERVER; 86 87 if(empty($server)){ 88 $this->ERROR = "POP3 connect: " . _("No server specified"); 89 unset($this->FP); 90 return false; 91 } 92 93 $fp = @fsockopen("$server", $port, $errno, $errstr); 94 95 if(!$fp) { 96 $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]"; 97 unset($this->FP); 98 return false; 99 } 100 101 socket_set_blocking($fp,-1); 102 $this->update_timer(); 103 $reply = fgets($fp,$this->BUFFER); 104 $reply = $this->strip_clf($reply); 105 if($this->DEBUG) 106 error_log("POP3 SEND [connect: $server] GOT [$reply]",0); 107 if(!$this->is_ok($reply)) { 108 $this->ERROR = "POP3 connect: " . _("Error ") . "[$reply]"; 109 unset($this->FP); 110 return false; 111 } 112 $this->FP = $fp; 113 $this->BANNER = $this->parse_banner($reply); 266 } 267 268 } 269 270 /** 271 * Login to the POP3 server (does not support APOP yet) 272 * 273 * @param string $username 274 * @param string $password 275 * @return boolean 276 */ 277 function Login ($username = '', $password = '') 278 { 279 if ($this->connected == false) 280 { 281 $this->error = 'Not connected to POP3 server'; 282 283 if ($this->do_debug >= 1) 284 { 285 $this->displayErrors(); 286 } 287 } 288 289 if (empty($username)) 290 { 291 $username = $this->username; 292 } 293 294 if (empty($password)) 295 { 296 $password = $this->password; 297 } 298 299 $pop_username = "USER $username" . $this->CRLF; 300 $pop_password = "PASS $password" . $this->CRLF; 301 302 // Send the Username 303 $this->sendString($pop_username); 304 $pop3_response = $this->getResponse(); 305 306 if ($this->checkResponse($pop3_response)) 307 { 308 // Send the Password 309 $this->sendString($pop_password); 310 $pop3_response = $this->getResponse(); 311 312 if ($this->checkResponse($pop3_response)) 313 { 314 return true; 315 } 316 else 317 { 318 return false; 319 } 320 } 321 else 322 { 323 return false; 324 } 325 } 326 327 /** 328 * Disconnect from the POP3 server 329 */ 330 function Disconnect () 331 { 332 $this->sendString('QUIT'); 333 334 fclose($this->pop_conn); 335 } 336 337 /* 338 --------------- 339 Private Methods 340 --------------- 341 */ 342 343 /** 344 * Get the socket response back. 345 * $size is the maximum number of bytes to retrieve 346 * 347 * @param integer $size 348 * @return string 349 */ 350 function getResponse ($size = 128) 351 { 352 $pop3_response = fgets($this->pop_conn, $size); 353 354 return $pop3_response; 355 } 356 357 /** 358 * Send a string down the open socket connection to the POP3 server 359 * 360 * @param string $string 361 * @return integer 362 */ 363 function sendString ($string) 364 { 365 $bytes_sent = fwrite($this->pop_conn, $string, strlen($string)); 366 367 return $bytes_sent; 368 369 } 370 371 /** 372 * Checks the POP3 server response for +OK or -ERR 373 * 374 * @param string $string 375 * @return boolean 376 */ 377 function checkResponse ($string) 378 { 379 if (substr($string, 0, 3) !== '+OK') 380 { 381 $this->error = array( 382 'error' => "Server reported an error: $string", 383 'errno' => 0, 384 'errstr' => '' 385 ); 386 387 if ($this->do_debug >= 1) 388 { 389 $this->displayErrors(); 390 } 391 392 return false; 393 } 394 else 395 { 114 396 return true; 115 } 116 117 function user ($user = "") { 118 // Sends the USER command, returns true or false 119 120 if( empty($user) ) { 121 $this->ERROR = "POP3 user: " . _("no login ID submitted"); 122 return false; 123 } elseif(!isset($this->FP)) { 124 $this->ERROR = "POP3 user: " . _("connection not established"); 125 return false; 126 } else { 127 $reply = $this->send_cmd("USER $user"); 128 if(!$this->is_ok($reply)) { 129 $this->ERROR = "POP3 user: " . _("Error ") . "[$reply]"; 130 return false; 131 } else 132 return true; 133 } 134 } 135 136 function pass ($pass = "") { 137 // Sends the PASS command, returns # of msgs in mailbox, 138 // returns false (undef) on Auth failure 139 140 if(empty($pass)) { 141 $this->ERROR = "POP3 pass: " . _("No password submitted"); 142 return false; 143 } elseif(!isset($this->FP)) { 144 $this->ERROR = "POP3 pass: " . _("connection not established"); 145 return false; 146 } else { 147 $reply = $this->send_cmd("PASS $pass"); 148 if(!$this->is_ok($reply)) { 149 $this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]"; 150 $this->quit(); 151 return false; 152 } else { 153 // Auth successful. 154 $count = $this->last("count"); 155 $this->COUNT = $count; 156 return $count; 157 } 158 } 159 } 160 161 function apop ($login,$pass) { 162 // Attempts an APOP login. If this fails, it'll 163 // try a standard login. YOUR SERVER MUST SUPPORT 164 // THE USE OF THE APOP COMMAND! 165 // (apop is optional per rfc1939) 166 167 if(!isset($this->FP)) { 168 $this->ERROR = "POP3 apop: " . _("No connection to server"); 169 return false; 170 } elseif(!$this->ALLOWAPOP) { 171 $retVal = $this->login($login,$pass); 172 return $retVal; 173 } elseif(empty($login)) { 174 $this->ERROR = "POP3 apop: " . _("No login ID submitted"); 175 return false; 176 } elseif(empty($pass)) { 177 $this->ERROR = "POP3 apop: " . _("No password submitted"); 178 return false; 179 } else { 180 $banner = $this->BANNER; 181 if( (!$banner) or (empty($banner)) ) { 182 $this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort"); 183 $retVal = $this->login($login,$pass); 184 return $retVal; 185 } else { 186 $AuthString = $banner; 187 $AuthString .= $pass; 188 $APOPString = md5($AuthString); 189 $cmd = "APOP $login $APOPString"; 190 $reply = $this->send_cmd($cmd); 191 if(!$this->is_ok($reply)) { 192 $this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort"); 193 $retVal = $this->login($login,$pass); 194 return $retVal; 195 } else { 196 // Auth successful. 197 $count = $this->last("count"); 198 $this->COUNT = $count; 199 return $count; 200 } 201 } 202 } 203 } 204 205 function login ($login = "", $pass = "") { 206 // Sends both user and pass. Returns # of msgs in mailbox or 207 // false on failure (or -1, if the error occurs while getting 208 // the number of messages.) 209 210 if( !isset($this->FP) ) { 211 $this->ERROR = "POP3 login: " . _("No connection to server"); 212 return false; 213 } else { 214 $fp = $this->FP; 215 if( !$this->user( $login ) ) { 216 // Preserve the error generated by user() 217 return false; 218 } else { 219 $count = $this->pass($pass); 220 if( (!$count) || ($count == -1) ) { 221 // Preserve the error generated by last() and pass() 222 return false; 223 } else 224 return $count; 225 } 226 } 227 } 228 229 function top ($msgNum, $numLines = "0") { 230 // Gets the header and first $numLines of the msg body 231 // returns data in an array with each returned line being 232 // an array element. If $numLines is empty, returns 233 // only the header information, and none of the body. 234 235 if(!isset($this->FP)) { 236 $this->ERROR = "POP3 top: " . _("No connection to server"); 237 return false; 238 } 239 $this->update_timer(); 240 241 $fp = $this->FP; 242 $buffer = $this->BUFFER; 243 $cmd = "TOP $msgNum $numLines"; 244 fwrite($fp, "TOP $msgNum $numLines\r\n"); 245 $reply = fgets($fp, $buffer); 246 $reply = $this->strip_clf($reply); 247 if($this->DEBUG) { 248 @error_log("POP3 SEND [$cmd] GOT [$reply]",0); 249 } 250 if(!$this->is_ok($reply)) 251 { 252 $this->ERROR = "POP3 top: " . _("Error ") . "[$reply]"; 253 return false; 254 } 255 256 $count = 0; 257 $MsgArray = array(); 258 259 $line = fgets($fp,$buffer); 260 while ( !ereg("^\.\r\n",$line)) 261 { 262 $MsgArray[$count] = $line; 263 $count++; 264 $line = fgets($fp,$buffer); 265 if(empty($line)) { break; } 266 } 267 268 return $MsgArray; 269 } 270 271 function pop_list ($msgNum = "") { 272 // If called with an argument, returns that msgs' size in octets 273 // No argument returns an associative array of undeleted 274 // msg numbers and their sizes in octets 275 276 if(!isset($this->FP)) 277 { 278 $this->ERROR = "POP3 pop_list: " . _("No connection to server"); 279 return false; 280 } 281 $fp = $this->FP; 282 $Total = $this->COUNT; 283 if( (!$Total) or ($Total == -1) ) 284 { 285 return false; 286 } 287 if($Total == 0) 288 { 289 return array("0","0"); 290 // return -1; // mailbox empty 291 } 292 293 $this->update_timer(); 294 295 if(!empty($msgNum)) 296 { 297 $cmd = "LIST $msgNum"; 298 fwrite($fp,"$cmd\r\n"); 299 $reply = fgets($fp,$this->BUFFER); 300 $reply = $this->strip_clf($reply); 301 if($this->DEBUG) { 302 @error_log("POP3 SEND [$cmd] GOT [$reply]",0); 303 } 304 if(!$this->is_ok($reply)) 305 { 306 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]"; 307 return false; 308 } 309 list($junk,$num,$size) = preg_split('/\s+/',$reply); 310 return $size; 311 } 312 $cmd = "LIST"; 313 $reply = $this->send_cmd($cmd); 314 if(!$this->is_ok($reply)) 315 { 316 $reply = $this->strip_clf($reply); 317 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]"; 318 return false; 319 } 320 $MsgArray = array(); 321 $MsgArray[0] = $Total; 322 for($msgC=1;$msgC <= $Total; $msgC++) 323 { 324 if($msgC > $Total) { break; } 325 $line = fgets($fp,$this->BUFFER); 326 $line = $this->strip_clf($line); 327 if(ereg("^\.",$line)) 328 { 329 $this->ERROR = "POP3 pop_list: " . _("Premature end of list"); 330 return false; 331 } 332 list($thisMsg,$msgSize) = preg_split('/\s+/',$line); 333 settype($thisMsg,"integer"); 334 if($thisMsg != $msgC) 335 { 336 $MsgArray[$msgC] = "deleted"; 337 } 338 else 339 { 340 $MsgArray[$msgC] = $msgSize; 341 } 342 } 343 return $MsgArray; 344 } 345 346 function get ($msgNum) { 347 // Retrieve the specified msg number. Returns an array 348 // where each line of the msg is an array element. 349 350 if(!isset($this->FP)) 351 { 352 $this->ERROR = "POP3 get: " . _("No connection to server"); 353 return false; 354 } 355 356 $this->update_timer(); 357 358 $fp = $this->FP; 359 $buffer = $this->BUFFER; 360 $cmd = "RETR $msgNum"; 361 $reply = $this->send_cmd($cmd); 362 363 if(!$this->is_ok($reply)) 364 { 365 $this->ERROR = "POP3 get: " . _("Error ") . "[$reply]"; 366 return false; 367 } 368 369 $count = 0; 370 $MsgArray = array(); 371 372 $line = fgets($fp,$buffer); 373 while ( !ereg("^\.\r\n",$line)) 374 { 375 if ( $line{0} == '.' ) { $line = substr($line,1); } 376 $MsgArray[$count] = $line; 377 $count++; 378 $line = fgets($fp,$buffer); 379 if(empty($line)) { break; } 380 } 381 return $MsgArray; 382 } 383 384 function last ( $type = "count" ) { 385 // Returns the highest msg number in the mailbox. 386 // returns -1 on error, 0+ on success, if type != count 387 // results in a popstat() call (2 element array returned) 388 389 $last = -1; 390 if(!isset($this->FP)) 391 { 392 $this->ERROR = "POP3 last: " . _("No connection to server"); 393 return $last; 394 } 395 396 $reply = $this->send_cmd("STAT"); 397 if(!$this->is_ok($reply)) 398 { 399 $this->ERROR = "POP3 last: " . _("Error ") . "[$reply]"; 400 return $last; 401 } 402 403 $Vars = preg_split('/\s+/',$reply); 404 $count = $Vars[1]; 405 $size = $Vars[2]; 406 settype($count,"integer"); 407 settype($size,"integer"); 408 if($type != "count") 409 { 410 return array($count,$size); 411 } 412 return $count; 413 } 414 415 function reset () { 416 // Resets the status of the remote server. This includes 417 // resetting the status of ALL msgs to not be deleted. 418 // This method automatically closes the connection to the server. 419 420 if(!isset($this->FP)) 421 { 422 $this->ERROR = "POP3 reset: " . _("No connection to server"); 423 return false; 424 } 425 $reply = $this->send_cmd("RSET"); 426 if(!$this->is_ok($reply)) 427 { 428 // The POP3 RSET command -never- gives a -ERR 429 // response - if it ever does, something truely 430 // wild is going on. 431 432 $this->ERROR = "POP3 reset: " . _("Error ") . "[$reply]"; 433 @error_log("POP3 reset: ERROR [$reply]",0); 434 } 435 $this->quit(); 436 return true; 437 } 438 439 function send_cmd ( $cmd = "" ) 440 { 441 // Sends a user defined command string to the 442 // POP server and returns the results. Useful for 443 // non-compliant or custom POP servers. 444 // Do NOT includ the \r\n as part of your command 445 // string - it will be appended automatically. 446 447 // The return value is a standard fgets() call, which 448 // will read up to $this->BUFFER bytes of data, until it 449 // encounters a new line, or EOF, whichever happens first. 450 451 // This method works best if $cmd responds with only 452 // one line of data. 453 454 if(!isset($this->FP)) 455 { 456 $this->ERROR = "POP3 send_cmd: " . _("No connection to server"); 457 return false; 458 } 459 460 if(empty($cmd)) 461 { 462 $this->ERROR = "POP3 send_cmd: " . _("Empty command string"); 463 return ""; 464 } 465 466 $fp = $this->FP; 467 $buffer = $this->BUFFER; 468 $this->update_timer(); 469 fwrite($fp,"$cmd\r\n"); 470 $reply = fgets($fp,$buffer); 471 $reply = $this->strip_clf($reply); 472 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } 473 return $reply; 474 } 475 476 function quit() { 477 // Closes the connection to the POP3 server, deleting 478 // any msgs marked as deleted. 479 480 if(!isset($this->FP)) 481 { 482 $this->ERROR = "POP3 quit: " . _("connection does not exist"); 483 return false; 484 } 485 $fp = $this->FP; 486 $cmd = "QUIT"; 487 fwrite($fp,"$cmd\r\n"); 488 $reply = fgets($fp,$this->BUFFER); 489 $reply = $this->strip_clf($reply); 490 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } 491 fclose($fp); 492 unset($this->FP); 493 return true; 494 } 495 496 function popstat () { 497 // Returns an array of 2 elements. The number of undeleted 498 // msgs in the mailbox, and the size of the mbox in octets. 499 500 $PopArray = $this->last("array"); 501 502 if($PopArray == -1) { return false; } 503 504 if( (!$PopArray) or (empty($PopArray)) ) 505 { 506 return false; 507 } 508 return $PopArray; 509 } 510 511 function uidl ($msgNum = "") 512 { 513 // Returns the UIDL of the msg specified. If called with 514 // no arguments, returns an associative array where each 515 // undeleted msg num is a key, and the msg's uidl is the element 516 // Array element 0 will contain the total number of msgs 517 518 if(!isset($this->FP)) { 519 $this->ERROR = "POP3 uidl: " . _("No connection to server"); 520 return false; 521 } 522 523 $fp = $this->FP; 524 $buffer = $this->BUFFER; 525 526 if(!empty($msgNum)) { 527 $cmd = "UIDL $msgNum"; 528 $reply = $this->send_cmd($cmd); 529 if(!$this->is_ok($reply)) 530 { 531 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]"; 532 return false; 533 } 534 list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply); 535 return $myUidl; 536 } else { 537 $this->update_timer(); 538 539 $UIDLArray = array(); 540 $Total = $this->COUNT; 541 $UIDLArray[0] = $Total; 542 543 if ($Total < 1) 544 { 545 return $UIDLArray; 546 } 547 $cmd = "UIDL"; 548 fwrite($fp, "UIDL\r\n"); 549 $reply = fgets($fp, $buffer); 550 $reply = $this->strip_clf($reply); 551 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } 552 if(!$this->is_ok($reply)) 553 { 554 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]"; 555 return false; 556 } 557 558 $line = ""; 559 $count = 1; 560 $line = fgets($fp,$buffer); 561 while ( !ereg("^\.\r\n",$line)) { 562 if(ereg("^\.\r\n",$line)) { 563 break; 564 } 565 list ($msg,$msgUidl) = preg_split('/\s+/',$line); 566 $msgUidl = $this->strip_clf($msgUidl); 567 if($count == $msg) { 568 $UIDLArray[$msg] = $msgUidl; 569 } 570 else 571 { 572 $UIDLArray[$count] = 'deleted'; 573 } 574 $count++; 575 $line = fgets($fp,$buffer); 576 } 577 } 578 return $UIDLArray; 579 } 580 581 function delete ($msgNum = "") { 582 // Flags a specified msg as deleted. The msg will not 583 // be deleted until a quit() method is called. 584 585 if(!isset($this->FP)) 586 { 587 $this->ERROR = "POP3 delete: " . _("No connection to server"); 588 return false; 589 } 590 if(empty($msgNum)) 591 { 592 $this->ERROR = "POP3 delete: " . _("No msg number submitted"); 593 return false; 594 } 595 $reply = $this->send_cmd("DELE $msgNum"); 596 if(!$this->is_ok($reply)) 597 { 598 $this->ERROR = "POP3 delete: " . _("Command failed ") . "[$reply]"; 599 return false; 600 } 601 return true; 602 } 603 604 // ********************************************************* 605 606 // The following methods are internal to the class. 607 608 function is_ok ($cmd = "") { 609 // Return true or false on +OK or -ERR 610 611 if( empty($cmd) ) 612 return false; 613 else 614 return( ereg ("^\+OK", $cmd ) ); 615 } 616 617 function strip_clf ($text = "") { 618 // Strips \r\n from server responses 619 620 if(empty($text)) 621 return $text; 622 else { 623 $stripped = str_replace("\r",'',$text); 624 $stripped = str_replace("\n",'',$stripped); 625 return $stripped; 626 } 627 } 628 629 function parse_banner ( $server_text ) { 630 $outside = true; 631 $banner = ""; 632 $length = strlen($server_text); 633 for($count =0; $count < $length; $count++) 634 { 635 $digit = substr($server_text,$count,1); 636 if(!empty($digit)) { 637 if( (!$outside) && ($digit != '<') && ($digit != '>') ) 638 { 639 $banner .= $digit; 640 } 641 if ($digit == '<') 642 { 643 $outside = false; 644 } 645 if($digit == '>') 646 { 647 $outside = true; 648 } 649 } 650 } 651 $banner = $this->strip_clf($banner); // Just in case 652 return "<$banner>"; 653 } 654 655 } // End class 397 } 398 399 } 400 401 /** 402 * If debug is enabled, display the error message array 403 * 404 */ 405 function displayErrors () 406 { 407 echo '<pre>'; 408 409 foreach ($this->error as $single_error) 410 { 411 print_r($single_error); 412 } 413 414 echo '</pre>'; 415 } 416 417 /** 418 * Takes over from PHP for the socket warning handler 419 * 420 * @param integer $errno 421 * @param string $errstr 422 * @param string $errfile 423 * @param integer $errline 424 */ 425 function catchWarning ($errno, $errstr, $errfile, $errline) 426 { 427 $this->error[] = array( 428 'error' => "Connecting to the POP3 server raised a PHP warning: ", 429 'errno' => $errno, 430 'errstr' => $errstr 431 ); 432 } 433 434 // End of class 435 } 656 436 ?>
Note: See TracChangeset
for help on using the changeset viewer.