Ticket #21559: patch.diff
File patch.diff, 29.0 KB (added by , 12 years ago) |
---|
-
.php
old new 32 32 // file descriptor 33 33 34 34 var $MAILSERVER = ''; // Set this to hard code the server name 35 var $PORT = 110; // Server port 35 36 36 37 var $DEBUG = FALSE; // set to true to echo pop3 37 38 // commands and responses to error_log … … 43 44 var $ALLOWAPOP = FALSE; // Allow or disallow apop() 44 45 // This must be set to true 45 46 // manually 47 48 var $safemode_enabled = true; 46 49 47 function POP3 ( $server = '', $timeout = '' ) { 48 settype($this->BUFFER,"integer"); 49 if( !empty($server) ) { 50 // Do not allow programs to alter MAILSERVER 51 // if it is already specified. They can get around 52 // this if they -really- want to, so don't count on it. 53 if(empty($this->MAILSERVER)) 54 $this->MAILSERVER = $server; 55 } 56 if(!empty($timeout)) { 57 settype($timeout,"integer"); 58 $this->TIMEOUT = $timeout; 59 if (!ini_get('safe_mode')) 60 set_time_limit($timeout); 61 } 62 return true; 50 function POP3 ( $server = '', $timeout = 0, $port = 110, $connect = false ) { 51 52 $this->safemode_enabled = (bool)ini_get('safe_mode'); 53 $this->BUFFER = (int)$this->BUFFER; 54 55 $success = $this->_init( $server, $port, $timeout ); 56 57 if ($connect && $success) 58 $this->connect( $this->MAILSERVER, $this->PORT ); 59 } 60 61 private function _init($server = '', $port = 110, $timeout = 0){ 62 63 // Do not allow programs to alter MAILSERVER 64 // if it is already specified. They can get around 65 // this if they -really- want to, so don't count on it. 66 if (empty($this->MAILSERVER)) { 67 if (!empty($server)) 68 $this->MAILSERVER = $server; 69 } 70 71 if ($timeout) { 72 $this->TIMEOUT = (int)$timeout; 73 if (!$this->safemode_enabled) 74 set_time_limit( $this->TIMEOUT ); 75 } 76 77 $port = (int)$port; 78 if ($port) { 79 $this->PORT = $port; 80 } 81 82 return !empty($this->MAILSERVER); 83 } 84 85 function set_error( $error , $caller = null, $close = false ) { 86 $this->ERROR = 'POP3 '. ( $caller ? $caller.'()' : '' ).': '. $error; 87 88 if ($close) 89 unset($this->FP); 90 91 return false; 63 92 } 64 93 65 94 function update_timer () { 66 if (! ini_get('safe_mode'))95 if (!$this->safemode_enabled) 67 96 set_time_limit($this->TIMEOUT); 68 97 return true; 69 98 } 70 99 71 function connect ($server, $port = 110) { 72 // Opens a socket to the specified server. Unless overridden, 73 // port defaults to 110. Returns true on success, false on fail 74 75 // If MAILSERVER is set, override $server with it's value 76 77 if (!isset($port) || !$port) {$port = 110;} 78 if(!empty($this->MAILSERVER)) 79 $server = $this->MAILSERVER; 80 81 if(empty($server)){ 82 $this->ERROR = "POP3 connect: " . _("No server specified"); 83 unset($this->FP); 84 return false; 100 function connect ($server = '', $port = 110) { 101 102 if ( empty($this->MAILSERVER) || $this->PORT != $port ) { 103 if (!$this->_init( $server, $port )) { 104 return $this->set_error( _("No server specified"), __FUNCTION__, true ); 105 } 85 106 } 107 108 $this->FP = @fsockopen($this->MAILSERVER, $this->PORT, $errno, $errstr); 86 109 87 $fp = @fsockopen("$server", $port, $errno, $errstr); 88 89 if(!$fp) { 90 $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]"; 91 unset($this->FP); 92 return false; 110 if(!$this->FP) { 111 return $this->set_error( _("Error ") . "[$errno] [$errstr]", __FUNCTION__, true ); 93 112 } 113 114 function_exists('stream_set_blocking') ? stream_set_blocking($this->FP, 1) : socket_set_blocking($this->FP, -1); 94 115 95 socket_set_blocking($fp,-1);96 116 $this->update_timer(); 97 $reply = fgets($fp,$this->BUFFER); 98 $reply = $this->strip_clf($reply); 99 if($this->DEBUG) 100 error_log("POP3 SEND [connect: $server] GOT [$reply]",0); 117 $reply = $this->get_reply(); 118 101 119 if(!$this->is_ok($reply)) { 102 $this->ERROR = "POP3 connect: " . _("Error ") . "[$reply]"; 103 unset($this->FP); 104 return false; 120 return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__, true ); 105 121 } 106 $this->FP = $fp;122 107 123 $this->BANNER = $this->parse_banner($reply); 108 124 return true; 109 125 } 126 127 private function send($string) { 128 $raw = $string; 129 130 if ($this->FP) { 131 $string = strpos($string, "\r\n") === FALSE ? $string."\r\n" : $string; 132 133 if ( ($number = @fwrite($this->FP, $string)) ) { 134 if ($this->DEBUG) { 135 @error_log('POP3 '. __FUNCTION__.'(): ['.$raw.']'); 136 } 137 return $number; 138 } 139 } 140 141 return false; 142 } 143 144 private function get_reply($strip_clf = true) { 145 $reply = @fgets( $this->FP, $this->BUFFER ); 146 147 if ($reply) { 148 if ($strip_clf) { 149 $reply = $this->strip_clf( $reply ); 150 } 151 152 if ($this->DEBUG) { 153 @error_log('POP3 GOT: ['. $reply .'] FROM ['.$this->MAILSERVER.']',0); 154 } 155 } 156 157 return $reply; 158 } 110 159 111 160 function user ($user = "") { 112 161 // Sends the USER command, returns true or false 113 162 114 if( empty($user) ) { 115 $this->ERROR = "POP3 user: " . _("no login ID submitted"); 116 return false; 117 } elseif(!isset($this->FP)) { 118 $this->ERROR = "POP3 user: " . _("connection not established"); 119 return false; 120 } else { 121 $reply = $this->send_cmd("USER $user"); 122 if(!$this->is_ok($reply)) { 123 $this->ERROR = "POP3 user: " . _("Error ") . "[$reply]"; 124 return false; 125 } else 126 return true; 127 } 163 if( empty($user) ) 164 return $this->set_error( _("no login ID submitted"), __FUNCTION__ ); 165 166 if(!isset($this->FP)) 167 return $this->set_error( _("connection not established"), __FUNCTION__ ); 168 169 $reply = $this->send_cmd("USER $user"); 170 if(!$this->is_ok($reply)) 171 return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ ); 172 173 return $this; 128 174 } 129 175 130 176 function pass ($pass = "") { 131 177 // Sends the PASS command, returns # of msgs in mailbox, 132 178 // returns false (undef) on Auth failure 133 179 134 if(empty($pass)) { 135 $this->ERROR = "POP3 pass: " . _("No password submitted"); 136 return false; 137 } elseif(!isset($this->FP)) { 138 $this->ERROR = "POP3 pass: " . _("connection not established"); 180 if(empty($pass)) 181 return $this->set_error( _("No password submitted"), __FUNCTION__ ); 182 183 if(!isset($this->FP)) 184 return $this->set_error( _("connection not established"), __FUNCTION__ ); 185 186 $reply = $this->send_cmd("PASS $pass"); 187 if(!$this->is_ok($reply)) { 188 $this->set_error( _("Authentication failed") . " [$reply]", __FUNCTION__ ); 189 $this->quit(); 139 190 return false; 140 } else {141 $reply = $this->send_cmd("PASS $pass");142 if(!$this->is_ok($reply)) {143 $this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]";144 $this->quit();145 return false;146 } else {147 // Auth successful.148 $count = $this->last("count");149 $this->COUNT = $count;150 return $count;151 }152 191 } 192 193 // Auth successful. 194 $this->COUNT = $this->last("count"); 195 return $this->COUNT; 153 196 } 154 197 155 198 function apop ($login,$pass) { … … 158 201 // THE USE OF THE APOP COMMAND! 159 202 // (apop is optional per rfc1939) 160 203 161 if(!isset($this->FP)) { 162 $this->ERROR = "POP3 apop: " . _("No connection to server"); 163 return false; 164 } elseif(!$this->ALLOWAPOP) { 204 if(!isset($this->FP)) 205 return $this->set_error( _("No connection to server"), __FUNCTION__ ); 206 207 if(!$this->ALLOWAPOP) { 208 $retVal = $this->login($login,$pass); 209 return $retVal; 210 } 211 212 if (empty($login)) 213 return $this->set_error( _("No login ID submitted"), __FUNCTION__ ); 214 215 if(empty($pass)) 216 return $this->set_error( _("No password submitted"), __FUNCTION__ ); 217 218 219 $banner = $this->BANNER; 220 if( !$banner ) { 221 $this->set_error( _("No server banner") . ' - ' . _("abort"), __FUNCTION__ ); 222 $retVal = $this->login($login,$pass); 223 return $retVal; 224 } 225 226 $AuthString = $banner. $pass; 227 $APOPString = md5($AuthString); 228 229 $reply = $this->send_cmd("APOP $login $APOPString"); 230 if(!$this->is_ok($reply)) { 231 $this->set_error( _("apop authentication failed") . ' - ' . _("abort"), __FUNCTION__ ); 165 232 $retVal = $this->login($login,$pass); 166 233 return $retVal; 167 } elseif(empty($login)) {168 $this->ERROR = "POP3 apop: " . _("No login ID submitted");169 return false;170 } elseif(empty($pass)) {171 $this->ERROR = "POP3 apop: " . _("No password submitted");172 return false;173 } else {174 $banner = $this->BANNER;175 if( (!$banner) or (empty($banner)) ) {176 $this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort");177 $retVal = $this->login($login,$pass);178 return $retVal;179 } else {180 $AuthString = $banner;181 $AuthString .= $pass;182 $APOPString = md5($AuthString);183 $cmd = "APOP $login $APOPString";184 $reply = $this->send_cmd($cmd);185 if(!$this->is_ok($reply)) {186 $this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort");187 $retVal = $this->login($login,$pass);188 return $retVal;189 } else {190 // Auth successful.191 $count = $this->last("count");192 $this->COUNT = $count;193 return $count;194 }195 }196 234 } 235 236 // Auth successful. 237 $this->COUNT = $this->last("count"); 238 return $this->COUNT; 197 239 } 198 240 199 241 function login ($login = "", $pass = "") { … … 202 244 // the number of messages.) 203 245 204 246 if( !isset($this->FP) ) { 205 $this->ERROR = "POP3 login: " . _("No connection to server"); 206 return false; 247 return $this->set_error( _("No connection to server"), __FUNCTION__ ); 207 248 } else { 208 249 $fp = $this->FP; 209 250 if( !$this->user( $login ) ) { … … 220 261 } 221 262 } 222 263 223 function top ($msgNum, $numLines = "0") {264 function top ($msgNum, $numLines = 0) { 224 265 // Gets the header and first $numLines of the msg body 225 266 // returns data in an array with each returned line being 226 267 // an array element. If $numLines is empty, returns 227 268 // only the header information, and none of the body. 228 269 229 if(!isset($this->FP)) { 230 $this->ERROR = "POP3 top: " . _("No connection to server"); 231 return false; 232 } 270 if(!isset($this->FP)) 271 return $this->set_error( _("No connection to server"), __FUNCTION__ ); 272 233 273 $this->update_timer(); 234 274 235 $fp = $this->FP; 236 $buffer = $this->BUFFER; 237 $cmd = "TOP $msgNum $numLines"; 238 fwrite($fp, "TOP $msgNum $numLines\r\n"); 239 $reply = fgets($fp, $buffer); 240 $reply = $this->strip_clf($reply); 241 if($this->DEBUG) { 242 @error_log("POP3 SEND [$cmd] GOT [$reply]",0); 243 } 244 if(!$this->is_ok($reply)) 245 { 246 $this->ERROR = "POP3 top: " . _("Error ") . "[$reply]"; 247 return false; 275 $this->send( "TOP $msgNum $numLines" ); 276 $reply = $this->get_reply(); 277 if (!$this->is_ok($reply)) { 278 return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ ); 248 279 } 249 280 250 $count = 0;251 281 $MsgArray = array(); 252 253 $line = fgets($fp,$buffer); 254 while ( !preg_match('/^\.\r\n/',$line)) 255 { 256 $MsgArray[$count] = $line; 257 $count++; 258 $line = fgets($fp,$buffer); 259 if(empty($line)) { break; } 282 while ( ( $line = fgets($this->FP, $this->BUFFER) ) && !$this->iseof( $line ) ) { 283 $MsgArray[] = $line; 260 284 } 261 285 262 286 return $MsgArray; 263 287 } 264 288 265 function pop_list ($msgNum = "") {289 function pop_list ($msgNum = '') { 266 290 // If called with an argument, returns that msgs' size in octets 267 291 // No argument returns an associative array of undeleted 268 292 // msg numbers and their sizes in octets 269 293 270 if(!isset($this->FP)) 271 { 272 $this->ERROR = "POP3 pop_list: " . _("No connection to server"); 273 return false; 274 } 275 $fp = $this->FP; 276 $Total = $this->COUNT; 277 if( (!$Total) or ($Total == -1) ) 278 { 279 return false; 280 } 281 if($Total == 0) 282 { 283 return array("0","0"); 284 // return -1; // mailbox empty 294 if (!isset($this->FP)) 295 return $this->set_error( _("No connection to server"), __FUNCTION__ ); 296 297 if ($this->COUNT === FALSE || $this->COUNT == -1) { 298 return false; 299 } 300 301 if ( $this->COUNT == 0 ) { 302 return array(0,0); 285 303 } 286 304 287 305 $this->update_timer(); 288 289 if(!empty($msgNum)) 290 { 291 $cmd = "LIST $msgNum"; 292 fwrite($fp,"$cmd\r\n"); 293 $reply = fgets($fp,$this->BUFFER); 294 $reply = $this->strip_clf($reply); 295 if($this->DEBUG) { 296 @error_log("POP3 SEND [$cmd] GOT [$reply]",0); 306 307 if (!$msgNum) { 308 $this->send( "LIST $msgNum" ); 309 310 $reply = $this->get_reply(); 311 if (!$this->is_ok($reply)) 312 return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ ); 313 314 // Gmail and some email providers return the word 'messages' in the response, example: 315 // +OK 307 messages (23191343 bytes) 316 preg_match_all('([0-9]+)', $reply, $m); 317 $size = FALSE; 318 if( !empty($m[0]) ) { 319 $size = array_pop($m[0]); 297 320 } 298 if(!$this->is_ok($reply))299 {300 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";301 return false;302 }303 list($junk,$num,$size) = preg_split('/\s+/',$reply);304 321 return $size; 305 322 } 306 $cmd = "LIST"; 307 $reply = $this->send_cmd($cmd); 308 if(!$this->is_ok($reply)) 309 { 310 $reply = $this->strip_clf($reply); 311 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]"; 312 return false; 313 } 314 $MsgArray = array(); 315 $MsgArray[0] = $Total; 316 for($msgC=1;$msgC <= $Total; $msgC++) 317 { 318 if($msgC > $Total) { break; } 319 $line = fgets($fp,$this->BUFFER); 320 $line = $this->strip_clf($line); 321 if(strpos($line, '.') === 0) 322 { 323 $this->ERROR = "POP3 pop_list: " . _("Premature end of list"); 324 return false; 325 } 326 list($thisMsg,$msgSize) = preg_split('/\s+/',$line); 327 settype($thisMsg,"integer"); 328 if($thisMsg != $msgC) 329 { 330 $MsgArray[$msgC] = "deleted"; 331 } 332 else 333 { 334 $MsgArray[$msgC] = $msgSize; 335 } 336 } 337 return $MsgArray; 323 324 $reply = $this->send_cmd('LIST'); 325 if (!$this->is_ok($reply)) 326 return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ ); 327 328 $msg = array(); 329 for( $i=1; $i <= $this->COUNT; $i++ ) { 330 $info = $this->get_reply(); 331 if ( !empty($info) ) { 332 $info = explode(' ', $info); 333 $msg[ $info[0] ] = $info[1]; 334 } 335 } 336 337 return $msg; 338 338 } 339 339 340 340 function get ($msgNum) { 341 341 // Retrieve the specified msg number. Returns an array 342 342 // where each line of the msg is an array element. 343 343 344 if(!isset($this->FP)) 345 { 346 $this->ERROR = "POP3 get: " . _("No connection to server"); 347 return false; 348 } 344 if (!isset($this->FP)) 345 return $this->set_error( _("No connection to server"), __FUNCTION__ ); 349 346 350 347 $this->update_timer(); 348 $reply = $this->send_cmd('RETR '. $msgNum); 351 349 352 $fp = $this->FP; 353 $buffer = $this->BUFFER; 354 $cmd = "RETR $msgNum"; 355 $reply = $this->send_cmd($cmd); 356 357 if(!$this->is_ok($reply)) 358 { 359 $this->ERROR = "POP3 get: " . _("Error ") . "[$reply]"; 360 return false; 361 } 350 if(!$this->is_ok($reply)) 351 return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ ); 362 352 363 $count = 0;364 353 $MsgArray = array(); 365 354 366 $line = fgets($fp,$buffer); 367 while ( !preg_match('/^\.\r\n/',$line)) 368 { 369 if ( $line{0} == '.' ) { $line = substr($line,1); } 370 $MsgArray[$count] = $line; 371 $count++; 372 $line = fgets($fp,$buffer); 373 if(empty($line)) { break; } 355 while ( ( $line = fgets($this->FP, $this->BUFFER) ) && !$this->iseof( $line ) ) { 356 $MsgArray[] = $line; 374 357 } 358 375 359 return $MsgArray; 376 360 } 361 362 private function iseof( $line ) { 363 return $line == ".\r\n" || empty($line); 364 } 377 365 378 366 function last ( $type = "count" ) { 367 static $saved_last; 379 368 // Returns the highest msg number in the mailbox. 380 369 // returns -1 on error, 0+ on success, if type != count 381 370 // results in a popstat() call (2 element array returned) 382 371 383 $last = -1; 384 if(!isset($this->FP)) 385 { 386 $this->ERROR = "POP3 last: " . _("No connection to server"); 387 return $last; 372 if (!isset($this->FP)) { 373 $this->set_error( _("No connection to server"), __FUNCTION__ ); 374 return -1; 388 375 } 389 376 390 377 $reply = $this->send_cmd("STAT"); 391 if(!$this->is_ok($reply)) 392 { 393 $this->ERROR = "POP3 last: " . _("Error ") . "[$reply]"; 394 return $last; 378 if(!$this->is_ok($reply)) { 379 $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ ); 380 return -1; 395 381 } 396 382 397 $Vars = preg_split('/\s+/',$reply); 398 $count = $Vars[1]; 399 $size = $Vars[2]; 400 settype($count,"integer"); 401 settype($size,"integer"); 402 if($type != "count") 403 { 404 return array($count,$size); 383 if( !$saved_last ) { 384 list( $ok, $count, $size) = explode( ' ', $reply); 385 $saved_last = array( $count, $size ); 405 386 } 406 return $count; 387 388 return $type == 'count' ? $saved_last['count'] : $saved_last; 407 389 } 408 390 409 391 function reset () { … … 411 393 // resetting the status of ALL msgs to not be deleted. 412 394 // This method automatically closes the connection to the server. 413 395 414 if(!isset($this->FP)) 415 { 416 $this->ERROR = "POP3 reset: " . _("No connection to server"); 417 return false; 418 } 396 if (!isset($this->FP)) 397 return $this->set_error( _("No connection to server"), __FUNCTION__ ); 398 419 399 $reply = $this->send_cmd("RSET"); 420 400 if(!$this->is_ok($reply)) 421 401 { … … 430 410 return true; 431 411 } 432 412 433 function send_cmd ( $cmd = "" ) 434 { 413 function send_cmd ( $cmd ) { 435 414 // Sends a user defined command string to the 436 415 // POP server and returns the results. Useful for 437 416 // non-compliant or custom POP servers. 438 // Do NOT includ the \r\n as part of your command439 // string - it will be appended automatically.440 417 441 418 // The return value is a standard fgets() call, which 442 419 // will read up to $this->BUFFER bytes of data, until it … … 445 422 // This method works best if $cmd responds with only 446 423 // one line of data. 447 424 448 if(!isset($this->FP)) 449 { 450 $this->ERROR = "POP3 send_cmd: " . _("No connection to server"); 451 return false; 452 } 425 if (!isset($this->FP)) 426 return $this->set_error( _("No connection to server"), __FUNCTION__ ); 453 427 454 if(empty($cmd)) 455 { 456 $this->ERROR = "POP3 send_cmd: " . _("Empty command string"); 428 if (empty($cmd)) { 429 $this->set_error( _("Empty command string") , __FUNCTION__ ); 457 430 return ""; 458 431 } 459 432 460 $fp = $this->FP;461 $buffer = $this->BUFFER;462 433 $this->update_timer(); 463 fwrite($fp,"$cmd\r\n");464 $reply = fgets($fp,$buffer);465 $reply = $this->strip_clf($reply);466 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }434 $reply = false; 435 if ( $this->send( $cmd ) ) 436 $reply = $this->get_reply(); 437 467 438 return $reply; 468 439 } 469 440 … … 471 442 // Closes the connection to the POP3 server, deleting 472 443 // any msgs marked as deleted. 473 444 474 if(!isset($this->FP)) 475 { 476 $this->ERROR = "POP3 quit: " . _("connection does not exist"); 477 return false; 478 } 479 $fp = $this->FP; 480 $cmd = "QUIT"; 481 fwrite($fp,"$cmd\r\n"); 482 $reply = fgets($fp,$this->BUFFER); 483 $reply = $this->strip_clf($reply); 484 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } 485 fclose($fp); 486 unset($this->FP); 445 if (!isset($this->FP)) 446 return $this->set_error( _("Connection already closed."), __FUNCTION__ ); 447 448 $this->send('QUIT') && $this->get_reply(); 449 450 fclose($this->FP); 487 451 return true; 488 452 } 489 453 … … 493 457 494 458 $PopArray = $this->last("array"); 495 459 496 if($PopArray == -1) { return false; } 497 498 if( (!$PopArray) or (empty($PopArray)) ) 499 { 500 return false; 501 } 460 if ( empty($PopArray) || $PopArray == -1 ) 461 return false; 462 502 463 return $PopArray; 503 464 } 504 465 … … 509 470 // undeleted msg num is a key, and the msg's uidl is the element 510 471 // Array element 0 will contain the total number of msgs 511 472 512 if(!isset($this->FP)) { 513 $this->ERROR = "POP3 uidl: " . _("No connection to server"); 514 return false; 515 } 473 if (!isset($this->FP)) 474 return $this->set_error( _("No connection to server"), __FUNCTION__ ); 516 475 517 $fp = $this->FP; 518 $buffer = $this->BUFFER; 519 520 if(!empty($msgNum)) { 521 $cmd = "UIDL $msgNum"; 522 $reply = $this->send_cmd($cmd); 476 if (!empty($msgNum)) { 477 $reply = $this->send_cmd("UIDL $msgNum"); 478 523 479 if(!$this->is_ok($reply)) 524 { 525 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]"; 526 return false; 527 } 480 return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ ); 481 528 482 list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply); 529 483 return $myUidl; 530 } else { 531 $this->update_timer(); 532 533 $UIDLArray = array(); 534 $Total = $this->COUNT; 535 $UIDLArray[0] = $Total; 536 537 if ($Total < 1) 538 { 539 return $UIDLArray; 540 } 541 $cmd = "UIDL"; 542 fwrite($fp, "UIDL\r\n"); 543 $reply = fgets($fp, $buffer); 544 $reply = $this->strip_clf($reply); 545 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); } 546 if(!$this->is_ok($reply)) 547 { 548 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]"; 549 return false; 550 } 484 } 485 486 $this->update_timer(); 551 487 552 $line = ""; 553 $count = 1; 554 $line = fgets($fp,$buffer); 555 while ( !preg_match('/^\.\r\n/',$line)) { 556 list ($msg,$msgUidl) = preg_split('/\s+/',$line); 557 $msgUidl = $this->strip_clf($msgUidl); 558 if($count == $msg) { 559 $UIDLArray[$msg] = $msgUidl; 560 } 561 else 562 { 563 $UIDLArray[$count] = 'deleted'; 564 } 565 $count++; 566 $line = fgets($fp,$buffer); 567 } 488 $this->send('UIDL'); 489 $reply = $this->get_reply(); 490 491 if ( !$this->is_ok($reply) ) 492 return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ ); 493 494 $response = array(); 495 while ( ( $line = fgets($this->FP, $this->BUFFER) ) && !$this->iseof( $line ) ) { 496 $exploded = preg_split('#\s+#', $line); 497 $response[ $exploded[0] ] = $exploded[1]; 568 498 } 569 return $UIDLArray; 499 500 return $response; 570 501 } 571 502 572 function delete ($msgNum = "") {503 function delete ($msgNum) { 573 504 // Flags a specified msg as deleted. The msg will not 574 505 // be deleted until a quit() method is called. 575 506 576 if(!isset($this->FP)) 577 { 578 $this->ERROR = "POP3 delete: " . _("No connection to server"); 579 return false; 580 } 581 if(empty($msgNum)) 582 { 583 $this->ERROR = "POP3 delete: " . _("No msg number submitted"); 584 return false; 585 } 507 if (!isset($this->FP)) 508 return $this->set_error( _("No connection to server"), __FUNCTION__ ); 509 510 if (empty($msgNum)) 511 return $this->set_error( _("No msg number submitted"), __FUNCTION__ ); 512 586 513 $reply = $this->send_cmd("DELE $msgNum"); 587 if(!$this->is_ok($reply)) 588 { 589 $this->ERROR = "POP3 delete: " . _("Command failed ") . "[$reply]"; 590 return false; 591 } 514 if (!$this->is_ok($reply)) 515 return $this->set_error( _("Command failed ") . "[$reply]", __FUNCTION__ ); 516 592 517 return true; 593 518 } 594 519 … … 596 521 597 522 // The following methods are internal to the class. 598 523 599 function is_ok ($cmd = "") {524 function is_ok ($cmd) { 600 525 // Return true or false on +OK or -ERR 601 602 if( empty($cmd) ) 603 return false; 604 else 605 return( stripos($cmd, '+OK') !== false ); 526 return stristr($cmd, '+OK') !== FALSE; 606 527 } 607 528 608 function strip_clf ($text = "") {529 function strip_clf ($text) { 609 530 // Strips \r\n from server responses 610 531 611 if(empty($text)) 612 return $text; 613 else { 614 $stripped = str_replace(array("\r","\n"),'',$text); 615 return $stripped; 616 } 532 return str_replace(array("\r","\n"), '', $text); 617 533 } 618 534 619 535 function parse_banner ( $server_text ) { 620 $outside = true; 621 $banner = ""; 622 $length = strlen($server_text); 623 for($count =0; $count < $length; $count++) 624 { 625 $digit = substr($server_text,$count,1); 626 if(!empty($digit)) { 627 if( (!$outside) && ($digit != '<') && ($digit != '>') ) 628 { 629 $banner .= $digit; 630 } 631 if ($digit == '<') 632 { 633 $outside = false; 634 } 635 if($digit == '>') 636 { 637 $outside = true; 638 } 639 } 640 } 641 $banner = $this->strip_clf($banner); // Just in case 642 return "<$banner>"; 536 if (empty($server_text)) { 537 return $server_text; 538 } 539 540 preg_match_all('#<([^<>]+)>#', $server_text, $matches); 541 542 $banners = array(); 543 if (isset($matches[1]) && !empty($matches[1])) { 544 $banners = $matches[1]; 545 } 546 547 return implode('', $banners); 548 } 549 550 function get_error() { 551 $error = $this->ERROR; 552 unset($this->ERROR); 553 return $error; 643 554 } 644 555 645 556 } // End class 646 557 647 // For php4 compatibility648 if (!function_exists("stripos")) {649 function stripos($haystack, $needle){650 return strpos($haystack, stristr( $haystack, $needle ));651 }652 }