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