PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-pop3.php

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