PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/GSoC-config/squirrelmail/functions/imap_general.php

#
PHP | 1422 lines | 984 code | 69 blank | 369 comment | 218 complexity | 72f8c51d9a1b97d9629f132e0e46c0e4 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * imap_general.php
  4. *
  5. * This implements all functions that do general IMAP functions.
  6. *
  7. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id: imap_general.php 12128 2007-01-13 20:15:44Z kink $
  10. * @package squirrelmail
  11. * @subpackage imap
  12. */
  13. /** Includes.. */
  14. require_once(SM_PATH . 'functions/rfc822address.php');
  15. /**
  16. * Generates a new session ID by incrementing the last one used;
  17. * this ensures that each command has a unique ID.
  18. * @param bool $unique_id (since 1.3.0) controls use of unique
  19. * identifiers/message sequence numbers in IMAP commands. See IMAP
  20. * rfc 'UID command' chapter.
  21. * @return string IMAP session id of the form 'A000'.
  22. * @since 1.2.0
  23. */
  24. function sqimap_session_id($unique_id = FALSE) {
  25. static $sqimap_session_id = 1;
  26. if (!$unique_id) {
  27. return( sprintf("A%03d", $sqimap_session_id++) );
  28. } else {
  29. return( sprintf("A%03d", $sqimap_session_id++) . ' UID' );
  30. }
  31. }
  32. /**
  33. * Both send a command and accept the result from the command.
  34. * This is to allow proper session number handling.
  35. * @param stream $imap_stream imap connection resource
  36. * @param string $query imap command
  37. * @param boolean $handle_errors see sqimap_retrieve_imap_response()
  38. * @param array $response
  39. * @param array $message
  40. * @param boolean $unique_id (since 1.3.0) see sqimap_session_id().
  41. * @return mixed returns false on imap error. displays error message
  42. * if imap stream is not available.
  43. * @since 1.2.3
  44. */
  45. function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
  46. if ($imap_stream) {
  47. $sid = sqimap_session_id($unique_id);
  48. fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
  49. $tag_uid_a = explode(' ',trim($sid));
  50. $tag = $tag_uid_a[0];
  51. $read = sqimap_retrieve_imap_response ($imap_stream, $tag, $handle_errors, $response, $message, $query );
  52. /* get the response and the message */
  53. $message = $message[$tag];
  54. $response = $response[$tag];
  55. return $read[$tag];
  56. } else {
  57. global $squirrelmail_language, $color;
  58. set_up_language($squirrelmail_language);
  59. $string = "<b><font color=\"$color[2]\">\n" .
  60. _("ERROR: No available IMAP stream.") .
  61. "</b></font>\n";
  62. error_box($string);
  63. return false;
  64. }
  65. }
  66. /**
  67. * @param stream $imap_stream imap connection resource
  68. * @param string $query imap command
  69. * @param boolean $handle_errors see sqimap_retrieve_imap_response()
  70. * @param array $response empty string, if return = false
  71. * @param array $message empty string, if return = false
  72. * @param boolean $unique_id (since 1.3.0) see sqimap_session_id()
  73. * @param boolean $filter (since 1.4.1 and 1.5.0) see sqimap_fread()
  74. * @param mixed $outputstream (since 1.4.1 and 1.5.0) see sqimap_fread()
  75. * @param boolean $no_return (since 1.4.1 and 1.5.0) see sqimap_fread()
  76. * @return mixed returns false on imap error. displays error message
  77. * if imap stream is not available.
  78. * @since 1.2.3
  79. */
  80. function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response,
  81. &$message, $unique_id = false,$filter=false,
  82. $outputstream=false,$no_return=false) {
  83. if ($imap_stream) {
  84. $sid = sqimap_session_id($unique_id);
  85. fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
  86. $tag_uid_a = explode(' ',trim($sid));
  87. $tag = $tag_uid_a[0];
  88. $read = sqimap_read_data ($imap_stream, $tag, $handle_errors, $response,
  89. $message, $query,$filter,$outputstream,$no_return);
  90. if (empty($read)) { //IMAP server dropped its connection
  91. $response = '';
  92. $message = '';
  93. return false;
  94. }
  95. /* retrieve the response and the message */
  96. $response = $response[$tag];
  97. $message = $message[$tag];
  98. if (!empty($read[$tag])) {
  99. return $read[$tag][0];
  100. } else {
  101. return $read[$tag];
  102. }
  103. } else {
  104. global $squirrelmail_language, $color;
  105. set_up_language($squirrelmail_language);
  106. $string = "<b><font color=\"$color[2]\">\n" .
  107. _("ERROR: No available IMAP stream.") .
  108. "</b></font>\n";
  109. error_box($string);
  110. return false;
  111. }
  112. }
  113. /**
  114. * @param mixed $new_query
  115. * @param string $tag
  116. * @param array $aQuery
  117. * @param boolean $unique_id see sqimap_session_id()
  118. * @since 1.5.0
  119. */
  120. function sqimap_prepare_pipelined_query($new_query,&$tag,&$aQuery,$unique_id) {
  121. $sid = sqimap_session_id($unique_id);
  122. $tag_uid_a = explode(' ',trim($sid));
  123. $tag = $tag_uid_a[0];
  124. $query = $sid . ' '.$new_query."\r\n";
  125. $aQuery[$tag] = $query;
  126. }
  127. /**
  128. * @param stream $imap_stream imap stream
  129. * @param array $aQueryList
  130. * @param boolean $handle_errors
  131. * @param array $aServerResponse
  132. * @param array $aServerMessage
  133. * @param boolean $unique_id see sqimap_session_id()
  134. * @param boolean $filter see sqimap_fread()
  135. * @param mixed $outputstream see sqimap_fread()
  136. * @param boolean $no_return see sqimap_fread()
  137. * @since 1.5.0
  138. */
  139. function sqimap_run_pipelined_command ($imap_stream, $aQueryList, $handle_errors,
  140. &$aServerResponse, &$aServerMessage, $unique_id = false,
  141. $filter=false,$outputstream=false,$no_return=false) {
  142. $aResponse = false;
  143. /*
  144. Do not fire all calls at once to the IMAP server but split the calls up
  145. in portions of $iChunkSize. If we do not do that I think we misbehave as
  146. IMAP client or should handle BYE calls if the IMAP server drops the
  147. connection because the number of queries is to large. This isn't tested
  148. but a wild guess how it could work in the field.
  149. After testing it on Exchange 2000 we discovered that a chunksize of 32
  150. was quicker then when we raised it to 128.
  151. */
  152. $iQueryCount = count($aQueryList);
  153. $iChunkSize = 32;
  154. // array_chunk would also do the job but it's supported from php > 4.2
  155. $aQueryChunks = array();
  156. $iLoops = floor($iQueryCount / $iChunkSize);
  157. if ($iLoops * $iChunkSize != $iQueryCount) ++$iLoops;
  158. if (!function_exists('array_chunk')) { // arraychunk replacement
  159. reset($aQueryList);
  160. for($i=0;$i<$iLoops;++$i) {
  161. for($j=0;$j<$iChunkSize;++$j) {
  162. $key = key($aQueryList);
  163. $aTmp[$key] = $aQueryList[$key];
  164. if (next($aQueryList) === false) break;
  165. }
  166. $aQueryChunks[] = $aTmp;
  167. }
  168. } else {
  169. $aQueryChunks = array_chunk($aQueryList,$iChunkSize,true);
  170. }
  171. for ($i=0;$i<$iLoops;++$i) {
  172. $aQuery = $aQueryChunks[$i];
  173. foreach($aQuery as $tag => $query) {
  174. fputs($imap_stream,$query);
  175. $aResults[$tag] = false;
  176. }
  177. foreach($aQuery as $tag => $query) {
  178. if ($aResults[$tag] == false) {
  179. $aReturnedResponse = sqimap_retrieve_imap_response ($imap_stream, $tag,
  180. $handle_errors, $response, $message, $query,
  181. $filter,$outputstream,$no_return);
  182. foreach ($aReturnedResponse as $returned_tag => $aResponse) {
  183. if (!empty($aResponse)) {
  184. $aResults[$returned_tag] = $aResponse[0];
  185. } else {
  186. $aResults[$returned_tag] = $aResponse;
  187. }
  188. $aServerResponse[$returned_tag] = $response[$returned_tag];
  189. $aServerMessage[$returned_tag] = $message[$returned_tag];
  190. }
  191. }
  192. }
  193. }
  194. return $aResults;
  195. }
  196. /**
  197. * Custom fgets function: gets a line from the IMAP server,
  198. * no matter how big it may be.
  199. * @param stream $imap_stream the stream to read from
  200. * @return string a line
  201. * @since 1.2.8
  202. */
  203. function sqimap_fgets($imap_stream) {
  204. $read = '';
  205. $buffer = 4096;
  206. $results = '';
  207. $offset = 0;
  208. while (strpos($results, "\r\n", $offset) === false) {
  209. if (!($read = fgets($imap_stream, $buffer))) {
  210. /* this happens in case of an error */
  211. /* reset $results because it's useless */
  212. $results = false;
  213. break;
  214. }
  215. if ( $results != '' ) {
  216. $offset = strlen($results) - 1;
  217. }
  218. $results .= $read;
  219. }
  220. return $results;
  221. }
  222. /**
  223. * @param stream $imap_stream
  224. * @param integer $iSize
  225. * @param boolean $filter
  226. * @param mixed $outputstream stream or 'php://stdout' string
  227. * @param boolean $no_return controls data returned by function
  228. * @return string
  229. * @since 1.4.1
  230. */
  231. function sqimap_fread($imap_stream,$iSize,$filter=false,
  232. $outputstream=false, $no_return=false) {
  233. if (!$filter || !$outputstream) {
  234. $iBufferSize = $iSize;
  235. } else {
  236. // see php bug 24033. They changed fread behaviour %$^&$%
  237. $iBufferSize = 7800; // multiple of 78 in case of base64 decoding.
  238. }
  239. if ($iSize < $iBufferSize) {
  240. $iBufferSize = $iSize;
  241. }
  242. $iRetrieved = 0;
  243. $results = '';
  244. $sRead = $sReadRem = '';
  245. // NB: fread can also stop at end of a packet on sockets.
  246. while ($iRetrieved < $iSize) {
  247. $sRead = fread($imap_stream,$iBufferSize);
  248. $iLength = strlen($sRead);
  249. $iRetrieved += $iLength ;
  250. $iRemaining = $iSize - $iRetrieved;
  251. if ($iRemaining < $iBufferSize) {
  252. $iBufferSize = $iRemaining;
  253. }
  254. if ($sRead == '') {
  255. $results = false;
  256. break;
  257. }
  258. if ($sReadRem != '') {
  259. $sRead = $sReadRem . $sRead;
  260. $sReadRem = '';
  261. }
  262. if ($filter && $sRead != '') {
  263. // in case the filter is base64 decoding we return a remainder
  264. $sReadRem = $filter($sRead);
  265. }
  266. if ($outputstream && $sRead != '') {
  267. if (is_resource($outputstream)) {
  268. fwrite($outputstream,$sRead);
  269. } else if ($outputstream == 'php://stdout') {
  270. echo $sRead;
  271. }
  272. }
  273. if ($no_return) {
  274. $sRead = '';
  275. } else {
  276. $results .= $sRead;
  277. }
  278. }
  279. return $results;
  280. }
  281. /**
  282. * Obsolete function, inform plugins that use it
  283. * @param stream $imap_stream
  284. * @param string $tag
  285. * @param boolean $handle_errors
  286. * @param array $response
  287. * @param array $message
  288. * @param string $query
  289. * @since 1.1.3
  290. * @deprecated (since 1.5.0) use sqimap_run_command or sqimap_run_command_list instead
  291. */
  292. function sqimap_read_data_list($imap_stream, $tag, $handle_errors,
  293. &$response, &$message, $query = '') {
  294. global $color, $oTemplate, $squirrelmail_language;
  295. set_up_language($squirrelmail_language);
  296. $string = "<b><font color=\"$color[2]\">\n" .
  297. _("ERROR: Bad function call.") .
  298. "</b><br />\n" .
  299. _("Reason:") . ' '.
  300. 'There is a plugin installed which make use of the <br />' .
  301. 'SquirrelMail internal function sqimap_read_data_list.<br />'.
  302. 'Please adapt the installed plugin and let it use<br />'.
  303. 'sqimap_run_command or sqimap_run_command_list instead<br /><br />'.
  304. 'The following query was issued:<br />'.
  305. htmlspecialchars($query) . '<br />' . "</font><br />\n";
  306. error_box($string);
  307. $oTemplate->display('footer.tpl');
  308. exit;
  309. }
  310. /**
  311. * Function to display an error related to an IMAP query.
  312. * @param string title the caption of the error box
  313. * @param string query the query that went wrong
  314. * @param string message_title optional message title
  315. * @param string message optional error message
  316. * @param string $link an optional link to try again
  317. * @return void
  318. * @since 1.5.0
  319. */
  320. function sqimap_error_box($title, $query = '', $message_title = '', $message = '', $link = '')
  321. {
  322. global $color, $squirrelmail_language;
  323. set_up_language($squirrelmail_language);
  324. $string = "<font color=\"$color[2]\"><b>\n" . $title . "</b><br />\n";
  325. $cmd = explode(' ',$query);
  326. $cmd= strtolower($cmd[0]);
  327. if ($query != '' && $cmd != 'login')
  328. $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br />';
  329. if ($message_title != '')
  330. $string .= $message_title;
  331. if ($message != '')
  332. $string .= htmlspecialchars($message);
  333. $string .= "</font><br />\n";
  334. if ($link != '')
  335. $string .= $link;
  336. error_box($string);
  337. }
  338. /**
  339. * Reads the output from the IMAP stream. If handle_errors is set to true,
  340. * this will also handle all errors that are received. If it is not set,
  341. * the errors will be sent back through $response and $message.
  342. * @param stream $imap_stream imap stream
  343. * @param string $tag
  344. * @param boolean $handle_errors handle errors internally or send them in $response and $message.
  345. * @param array $response
  346. * @param array $message
  347. * @param string $query command that can be printed if something fails
  348. * @param boolean $filter see sqimap_fread()
  349. * @param mixed $outputstream see sqimap_fread()
  350. * @param boolean $no_return see sqimap_fread()
  351. * @since 1.5.0
  352. */
  353. function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
  354. &$response, &$message, $query = '',
  355. $filter = false, $outputstream = false, $no_return = false) {
  356. global $color, $squirrelmail_language;
  357. $read = '';
  358. if (!is_array($message)) $message = array();
  359. if (!is_array($response)) $response = array();
  360. $aResponse = '';
  361. $resultlist = array();
  362. $data = array();
  363. $sCommand = '';
  364. if (preg_match("/^(\w+)\s*/",$query,$aMatch)) {
  365. $sCommand = strtoupper($aMatch[1]);
  366. } else {
  367. // error reporting (shouldn't happen)
  368. }
  369. $read = sqimap_fgets($imap_stream);
  370. $i = 0;
  371. while ($read) {
  372. $char = $read{0};
  373. switch ($char)
  374. {
  375. case '+':
  376. default:
  377. $read = sqimap_fgets($imap_stream);
  378. break;
  379. case $tag{0}:
  380. {
  381. /* get the command */
  382. $arg = '';
  383. $i = strlen($tag)+1;
  384. $s = substr($read,$i);
  385. if (($j = strpos($s,' ')) || ($j = strpos($s,"\n"))) {
  386. $arg = substr($s,0,$j);
  387. }
  388. $found_tag = substr($read,0,$i-1);
  389. if ($found_tag) {
  390. switch ($arg)
  391. {
  392. case 'OK':
  393. case 'BAD':
  394. case 'NO':
  395. case 'BYE':
  396. case 'PREAUTH':
  397. $response[$found_tag] = $arg;
  398. $message[$found_tag] = trim(substr($read,$i+strlen($arg)));
  399. if (!empty($data)) {
  400. $resultlist[] = $data;
  401. }
  402. $aResponse[$found_tag] = $resultlist;
  403. $data = $resultlist = array();
  404. if ($found_tag == $tag) {
  405. break 3; /* switch switch while */
  406. }
  407. break;
  408. default:
  409. /* this shouldn't happen */
  410. $response[$found_tag] = $arg;
  411. $message[$found_tag] = trim(substr($read,$i+strlen($arg)));
  412. if (!empty($data)) {
  413. $resultlist[] = $data;
  414. }
  415. $aResponse[$found_tag] = $resultlist;
  416. $data = $resultlist = array();
  417. if ($found_tag == $tag) {
  418. break 3; /* switch switch while */
  419. }
  420. }
  421. }
  422. $read = sqimap_fgets($imap_stream);
  423. if ($read === false) { /* error */
  424. break 2; /* switch while */
  425. }
  426. break;
  427. } // end case $tag{0}
  428. case '*':
  429. {
  430. if (($sCommand == "FETCH" || $sCommand == "STORE") && preg_match('/^\*\s\d+\sFETCH/',$read)) {
  431. /* check for literal */
  432. $s = substr($read,-3);
  433. $fetch_data = array();
  434. do { /* outer loop, continue until next untagged fetch
  435. or tagged reponse */
  436. do { /* innerloop for fetching literals. with this loop
  437. we prohibid that literal responses appear in the
  438. outer loop so we can trust the untagged and
  439. tagged info provided by $read */
  440. if ($s === "}\r\n") {
  441. $j = strrpos($read,'{');
  442. $iLit = substr($read,$j+1,-3);
  443. $fetch_data[] = $read;
  444. $sLiteral = sqimap_fread($imap_stream,$iLit,$filter,$outputstream,$no_return);
  445. if ($sLiteral === false) { /* error */
  446. break 4; /* while while switch while */
  447. }
  448. /* backwards compattibility */
  449. $aLiteral = explode("\n", $sLiteral);
  450. /* release not neaded data */
  451. unset($sLiteral);
  452. foreach ($aLiteral as $line) {
  453. $fetch_data[] = $line ."\n";
  454. }
  455. /* release not neaded data */
  456. unset($aLiteral);
  457. /* next fgets belongs to this fetch because
  458. we just got the exact literalsize and there
  459. must follow data to complete the response */
  460. $read = sqimap_fgets($imap_stream);
  461. if ($read === false) { /* error */
  462. break 4; /* while while switch while */
  463. }
  464. $fetch_data[] = $read;
  465. } else {
  466. $fetch_data[] = $read;
  467. }
  468. /* retrieve next line and check in the while
  469. statements if it belongs to this fetch response */
  470. $read = sqimap_fgets($imap_stream);
  471. if ($read === false) { /* error */
  472. break 4; /* while while switch while */
  473. }
  474. /* check for next untagged reponse and break */
  475. if ($read{0} == '*') break 2;
  476. $s = substr($read,-3);
  477. } while ($s === "}\r\n");
  478. $s = substr($read,-3);
  479. } while ($read{0} !== '*' &&
  480. substr($read,0,strlen($tag)) !== $tag);
  481. $resultlist[] = $fetch_data;
  482. /* release not neaded data */
  483. unset ($fetch_data);
  484. } else {
  485. $s = substr($read,-3);
  486. do {
  487. if ($s === "}\r\n") {
  488. $j = strrpos($read,'{');
  489. $iLit = substr($read,$j+1,-3);
  490. // check for numeric value to avoid that untagged responses like:
  491. // * OK [PARSE] Unexpected characters at end of address: {SET:debug=51}
  492. // will trigger literal fetching ({SET:debug=51} !== int )
  493. if (is_numeric($iLit)) {
  494. $data[] = $read;
  495. $sLiteral = fread($imap_stream,$iLit);
  496. if ($sLiteral === false) { /* error */
  497. $read = false;
  498. break 3; /* while switch while */
  499. }
  500. $data[] = $sLiteral;
  501. $data[] = sqimap_fgets($imap_stream);
  502. } else {
  503. $data[] = $read;
  504. }
  505. } else {
  506. $data[] = $read;
  507. }
  508. $read = sqimap_fgets($imap_stream);
  509. if ($read === false) {
  510. break 3; /* while switch while */
  511. } else if ($read{0} == '*') {
  512. break;
  513. }
  514. $s = substr($read,-3);
  515. } while ($s === "}\r\n");
  516. break 1;
  517. }
  518. break;
  519. } // end case '*'
  520. } // end switch
  521. } // end while
  522. /* error processing in case $read is false */
  523. if ($read === false) {
  524. // try to retrieve an untagged bye respons from the results
  525. $sResponse = array_pop($data);
  526. if ($sResponse !== NULL && strpos($sResponse,'* BYE') !== false) {
  527. if (!$handle_errors) {
  528. $query = '';
  529. }
  530. sqimap_error_box(_("ERROR: IMAP server closed the connection."), $query, _("Server responded:"),$sResponse);
  531. echo '</body></html>';
  532. exit;
  533. } else if ($handle_errors) {
  534. unset($data);
  535. sqimap_error_box(_("ERROR: Connection dropped by IMAP server."), $query);
  536. exit;
  537. }
  538. }
  539. /* Set $resultlist array */
  540. if (!empty($data)) {
  541. //$resultlist[] = $data;
  542. }
  543. elseif (empty($resultlist)) {
  544. $resultlist[] = array();
  545. }
  546. /* Return result or handle errors */
  547. if ($handle_errors == false) {
  548. return $aResponse;
  549. }
  550. switch ($response[$tag]) {
  551. case 'OK':
  552. return $aResponse;
  553. break;
  554. case 'NO':
  555. /* ignore this error from M$ exchange, it is not fatal (aka bug) */
  556. if (strstr($message[$tag], 'command resulted in') === false) {
  557. sqimap_error_box(_("ERROR: Could not complete request."), $query, _("Reason Given:") . ' ', $message[$tag]);
  558. echo '</body></html>';
  559. exit;
  560. }
  561. break;
  562. case 'BAD':
  563. sqimap_error_box(_("ERROR: Bad or malformed request."), $query, _("Server responded:") . ' ', $message[$tag]);
  564. echo '</body></html>';
  565. exit;
  566. case 'BYE':
  567. sqimap_error_box(_("ERROR: IMAP server closed the connection."), $query, _("Server responded:") . ' ', $message[$tag]);
  568. echo '</body></html>';
  569. exit;
  570. default:
  571. sqimap_error_box(_("ERROR: Unknown IMAP response."), $query, _("Server responded:") . ' ', $message[$tag]);
  572. /* the error is displayed but because we don't know the reponse we
  573. return the result anyway */
  574. return $aResponse;
  575. break;
  576. }
  577. }
  578. /**
  579. * @param stream $imap_stream imap string
  580. * @param string $tag_uid
  581. * @param boolean $handle_errors
  582. * @param array $response
  583. * @param array $message
  584. * @param string $query (since 1.2.5)
  585. * @param boolean $filter (since 1.4.1) see sqimap_fread()
  586. * @param mixed $outputstream (since 1.4.1) see sqimap_fread()
  587. * @param boolean $no_return (since 1.4.1) see sqimap_fread()
  588. */
  589. function sqimap_read_data ($imap_stream, $tag_uid, $handle_errors,
  590. &$response, &$message, $query = '',
  591. $filter=false,$outputstream=false,$no_return=false) {
  592. $tag_uid_a = explode(' ',trim($tag_uid));
  593. $tag = $tag_uid_a[0];
  594. $res = sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
  595. $response, $message, $query,$filter,$outputstream,$no_return);
  596. return $res;
  597. }
  598. /**
  599. * Connects to the IMAP server and returns a resource identifier for use with
  600. * the other SquirrelMail IMAP functions. Does NOT login!
  601. * @param string server hostname of IMAP server
  602. * @param int port port number to connect to
  603. * @param integer $tls whether to use plain text(0), TLS(1) or STARTTLS(2) when connecting.
  604. * Argument was boolean before 1.5.1.
  605. * @return imap-stream resource identifier
  606. * @since 1.5.0 (usable only in 1.5.1 or later)
  607. */
  608. function sqimap_create_stream($server,$port,$tls=0) {
  609. global $squirrelmail_language;
  610. if (strstr($server,':') && ! preg_match("/^\[.*\]$/",$server)) {
  611. // numerical IPv6 address must be enclosed in square brackets
  612. $server = '['.$server.']';
  613. }
  614. if ($tls == 1) {
  615. if ((check_php_version(4,3)) and (extension_loaded('openssl'))) {
  616. /* Use TLS by prefixing "tls://" to the hostname */
  617. $server = 'tls://' . $server;
  618. } else {
  619. require_once(SM_PATH . 'functions/display_messages.php');
  620. logout_error( sprintf(_("Error connecting to IMAP server: %s."), $server).
  621. '<br />'.
  622. _("TLS is enabled, but this version of PHP does not support TLS sockets, or is missing the openssl extension.").
  623. '<br /><br />'.
  624. _("Please contact your system administrator and report this error."),
  625. sprintf(_("Error connecting to IMAP server: %s."), $server));
  626. }
  627. }
  628. $imap_stream = @fsockopen($server, $port, $error_number, $error_string, 15);
  629. /* Do some error correction */
  630. if (!$imap_stream) {
  631. set_up_language($squirrelmail_language, true);
  632. require_once(SM_PATH . 'functions/display_messages.php');
  633. logout_error( sprintf(_("Error connecting to IMAP server: %s."), $server).
  634. "<br />\r\n$error_number : $error_string<br />\r\n",
  635. sprintf(_("Error connecting to IMAP server: %s."), $server) );
  636. exit;
  637. }
  638. $server_info = fgets ($imap_stream, 1024);
  639. /**
  640. * Implementing IMAP STARTTLS (rfc2595) in php 5.1.0+
  641. * http://www.php.net/stream-socket-enable-crypto
  642. */
  643. if ($tls === 2) {
  644. if (function_exists('stream_socket_enable_crypto')) {
  645. // check starttls capability, don't use cached capability version
  646. if (! sqimap_capability($imap_stream, 'STARTTLS', false)) {
  647. // imap server does not declare starttls support
  648. sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server),
  649. '','',
  650. _("IMAP STARTTLS is enabled in SquirrelMail configuration, but used IMAP server does not support STARTTLS."));
  651. exit;
  652. }
  653. // issue starttls command and check response
  654. sqimap_run_command($imap_stream, 'STARTTLS', false, $starttls_response, $starttls_message);
  655. // check response
  656. if ($starttls_response!='OK') {
  657. // starttls command failed
  658. sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server),
  659. 'STARTTLS',
  660. _("Server replied:") . ' ',
  661. $starttls_message);
  662. exit();
  663. }
  664. // start crypto on connection. suppress function errors.
  665. if (@stream_socket_enable_crypto($imap_stream,true,STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
  666. // starttls was successful
  667. /**
  668. * RFC 2595 requires to discard CAPABILITY information after successful
  669. * STARTTLS command. We don't follow RFC, because SquirrelMail stores CAPABILITY
  670. * information only after successful login (src/redirect.php) and cached information
  671. * is used only in other php script connections after successful STARTTLS. If script
  672. * issues sqimap_capability() call before sqimap_login() and wants to get initial
  673. * capability response, script should set third sqimap_capability() argument to false.
  674. */
  675. //sqsession_unregister('sqimap_capabilities');
  676. } else {
  677. /**
  678. * stream_socket_enable_crypto() call failed. Possible issues:
  679. * - broken ssl certificate (uw drops connection, error is in syslog mail facility)
  680. * - some ssl error (can reproduce with STREAM_CRYPTO_METHOD_SSLv3_CLIENT, PHP E_WARNING
  681. * suppressed in stream_socket_enable_crypto() call)
  682. */
  683. sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server),
  684. '','',
  685. _("Unable to start TLS."));
  686. /**
  687. * Bug: stream_socket_enable_crypto() does not register SSL errors in
  688. * openssl_error_string() or stream notification wrapper and displays
  689. * them in E_WARNING level message. It is impossible to retrieve error
  690. * message without own error handler.
  691. */
  692. exit;
  693. }
  694. } else {
  695. // php install does not support stream_socket_enable_crypto() function
  696. sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server),
  697. '','',
  698. _("IMAP STARTTLS is enabled in SquirrelMail configuration, but used PHP version does not support functions that allow to enable encryption on open socket."));
  699. exit;
  700. }
  701. }
  702. return $imap_stream;
  703. }
  704. /**
  705. * Logs the user into the IMAP server. If $hide is set, no error messages
  706. * will be displayed (if set to 1, just exits, if set to 2, returns FALSE).
  707. * This function returns the IMAP connection handle.
  708. * @param string $username user name
  709. * @param string $password password encrypted with onetimepad. Since 1.5.2
  710. * function can use internal password functions, if parameter is set to
  711. * boolean false.
  712. * @param string $imap_server_address address of imap server
  713. * @param integer $imap_port port of imap server
  714. * @param int $hide controls display connection errors:
  715. * 0 = do not hide
  716. * 1 = show no errors (just exit)
  717. * 2 = show no errors (return FALSE)
  718. * 3 = show no errors (return error string)
  719. * @return mixed The IMAP connection stream, or if the connection fails,
  720. * FALSE if $hide is set to 2 or an error string if $hide
  721. * is set to 3.
  722. */
  723. function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
  724. global $color, $squirrelmail_language, $onetimepad, $use_imap_tls,
  725. $imap_auth_mech, $sqimap_capabilities;
  726. // Note/TODO: This hack grabs the $authz argument from the session. In the short future,
  727. // a new argument in function sqimap_login() will be used instead.
  728. $authz = '';
  729. global $authz;
  730. sqgetglobalvar('authz' , $authz , SQ_SESSION);
  731. if(!empty($authz)) {
  732. /* authz plugin - specific:
  733. * Get proxy login parameters from authz plugin configuration. If they
  734. * exist, they will override the current ones.
  735. * This is useful if we want to use different SASL authentication mechanism
  736. * and/or different TLS settings for proxy logins. */
  737. global $authz_imap_auth_mech, $authz_use_imap_tls, $authz_imapPort_tls;
  738. $imap_auth_mech = !empty($authz_imap_auth_mech) ? strtolower($authz_imap_auth_mech) : $imap_auth_mech;
  739. $use_imap_tls = !empty($authz_use_imap_tls)? $authz_use_imap_tls : $use_imap_tls;
  740. $imap_port = !empty($authz_use_imap_tls)? $authz_imapPort_tls : $imap_port;
  741. if($imap_auth_mech == 'login' || $imap_auth_mech == 'cram-md5') {
  742. logout_error("Misconfigured Plugin (authz or equivalent):<br/>".
  743. "The LOGIN and CRAM-MD5 authentication mechanisms cannot be used when attempting proxy login.");
  744. exit;
  745. }
  746. }
  747. /* get imap login password */
  748. if ($password===false) {
  749. /* standard functions */
  750. $password = sqauth_read_password();
  751. } else {
  752. /* old way. $key must be extracted from cookie */
  753. if (!isset($onetimepad) || empty($onetimepad)) {
  754. sqgetglobalvar('onetimepad' , $onetimepad , SQ_SESSION );
  755. }
  756. /* Decrypt the password */
  757. $password = OneTimePadDecrypt($password, $onetimepad);
  758. }
  759. if (!isset($sqimap_capabilities)) {
  760. sqgetglobalvar('sqimap_capabilities' , $sqimap_capabilities , SQ_SESSION );
  761. }
  762. $host = $imap_server_address;
  763. $imap_server_address = sqimap_get_user_server($imap_server_address, $username);
  764. $imap_stream = sqimap_create_stream($imap_server_address,$imap_port,$use_imap_tls);
  765. if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) {
  766. // We're using some sort of authentication OTHER than plain or login
  767. $tag=sqimap_session_id(false);
  768. if ($imap_auth_mech == 'digest-md5') {
  769. $query = $tag . " AUTHENTICATE DIGEST-MD5\r\n";
  770. } elseif ($imap_auth_mech == 'cram-md5') {
  771. $query = $tag . " AUTHENTICATE CRAM-MD5\r\n";
  772. }
  773. fputs($imap_stream,$query);
  774. $answer=sqimap_fgets($imap_stream);
  775. // Trim the "+ " off the front
  776. $response=explode(" ",$answer,3);
  777. if ($response[0] == '+') {
  778. // Got a challenge back
  779. $challenge=$response[1];
  780. if ($imap_auth_mech == 'digest-md5') {
  781. $reply = digest_md5_response($username,$password,$challenge,'imap',$host,$authz);
  782. } elseif ($imap_auth_mech == 'cram-md5') {
  783. $reply = cram_md5_response($username,$password,$challenge);
  784. }
  785. fputs($imap_stream,$reply);
  786. $read=sqimap_fgets($imap_stream);
  787. if ($imap_auth_mech == 'digest-md5') {
  788. // DIGEST-MD5 has an extra step..
  789. if (substr($read,0,1) == '+') { // OK so far..
  790. fputs($imap_stream,"\r\n");
  791. $read=sqimap_fgets($imap_stream);
  792. }
  793. }
  794. $results=explode(" ",$read,3);
  795. $response=$results[1];
  796. $message=$results[2];
  797. } else {
  798. // Fake the response, so the error trap at the bottom will work
  799. $response="BAD";
  800. $message='IMAP server does not appear to support the authentication method selected.';
  801. $message .= ' Please contact your system administrator.';
  802. }
  803. } elseif ($imap_auth_mech == 'login') {
  804. // Original IMAP login code
  805. $query = 'LOGIN "' . quoteimap($username) . '" "' . quoteimap($password) . '"';
  806. $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
  807. } elseif ($imap_auth_mech == 'plain') {
  808. /***
  809. * SASL PLAIN, RFC 4616 (updates 2595)
  810. *
  811. * The mechanism consists of a single message, a string of [UTF-8]
  812. * encoded [Unicode] characters, from the client to the server. The
  813. * client presents the authorization identity (identity to act as),
  814. * followed by a NUL (U+0000) character, followed by the authentication
  815. * identity (identity whose password will be used), followed by a NUL
  816. * (U+0000) character, followed by the clear-text password. As with
  817. * other SASL mechanisms, the client does not provide an authorization
  818. * identity when it wishes the server to derive an identity from the
  819. * credentials and use that as the authorization identity.
  820. */
  821. $tag=sqimap_session_id(false);
  822. $sasl = (isset($sqimap_capabilities['SASL-IR']) && $sqimap_capabilities['SASL-IR']) ? true : false;
  823. if(!empty($authz)) {
  824. $auth = base64_encode("$username\0$authz\0$password");
  825. } else {
  826. $auth = base64_encode("$username\0$username\0$password");
  827. }
  828. if ($sasl) {
  829. // IMAP Extension for SASL Initial Client Response
  830. // <draft-siemborski-imap-sasl-initial-response-01b.txt>
  831. $query = $tag . " AUTHENTICATE PLAIN $auth\r\n";
  832. fputs($imap_stream, $query);
  833. $read = sqimap_fgets($imap_stream);
  834. } else {
  835. $query = $tag . " AUTHENTICATE PLAIN\r\n";
  836. fputs($imap_stream, $query);
  837. $read=sqimap_fgets($imap_stream);
  838. if (substr($read,0,1) == '+') { // OK so far..
  839. fputs($imap_stream, "$auth\r\n");
  840. $read = sqimap_fgets($imap_stream);
  841. }
  842. }
  843. $results=explode(" ",$read,3);
  844. $response=$results[1];
  845. $message=$results[2];
  846. } else {
  847. $response="BAD";
  848. $message="Internal SquirrelMail error - unknown IMAP authentication method chosen. Please contact the developers.";
  849. }
  850. /* If the connection was not successful, lets see why */
  851. if ($response != 'OK') {
  852. if (!$hide || $hide == 3) {
  853. //FIXME: UUURG... We don't want HTML in error messages, should also do html sanitizing of error messages elsewhere; should't assume output is destined for an HTML browser here
  854. if ($response != 'NO') {
  855. /* "BAD" and anything else gets reported here. */
  856. $message = htmlspecialchars($message);
  857. set_up_language($squirrelmail_language, true);
  858. if ($response == 'BAD') {
  859. if ($hide == 3) return sprintf(_("Bad request: %s"), $message);
  860. $string = sprintf (_("Bad request: %s")."<br />\r\n", $message);
  861. } else {
  862. if ($hide == 3) return sprintf(_("Unknown error: %s"), $message);
  863. $string = sprintf (_("Unknown error: %s") . "<br />\n", $message);
  864. }
  865. if (isset($read) && is_array($read)) {
  866. $string .= '<br />' . _("Read data:") . "<br />\n";
  867. foreach ($read as $line) {
  868. $string .= htmlspecialchars($line) . "<br />\n";
  869. }
  870. }
  871. error_box($string);
  872. exit;
  873. } else {
  874. /*
  875. * If the user does not log in with the correct
  876. * username and password it is not possible to get the
  877. * correct locale from the user's preferences.
  878. * Therefore, apply the same hack as on the login
  879. * screen.
  880. *
  881. * $squirrelmail_language is set by a cookie when
  882. * the user selects language and logs out
  883. */
  884. set_up_language($squirrelmail_language, true);
  885. sqsession_destroy();
  886. /* terminate the session nicely */
  887. sqimap_logout($imap_stream);
  888. if ($hide == 3) return _("Unknown user or password incorrect.");
  889. logout_error( _("Unknown user or password incorrect.") );
  890. exit;
  891. }
  892. } else {
  893. if ($hide == 2) return FALSE;
  894. exit;
  895. }
  896. }
  897. /* Special error case:
  898. * Login referrals. The server returns:
  899. * ? OK [REFERRAL <imap url>]
  900. * Check RFC 2221 for details. Since we do not support login referrals yet
  901. * we log the user out.
  902. */
  903. if ( stristr($message, 'REFERRAL imap') === TRUE ) {
  904. sqimap_logout($imap_stream);
  905. set_up_language($squirrelmail_language, true);
  906. sqsession_destroy();
  907. logout_error( _("Your mailbox is not located at this server. Try a different server or consult your system administrator") );
  908. exit;
  909. }
  910. return $imap_stream;
  911. }
  912. /**
  913. * Simply logs out the IMAP session
  914. * @param stream $imap_stream the IMAP connection to log out.
  915. * @return void
  916. */
  917. function sqimap_logout ($imap_stream) {
  918. /* Logout is not valid until the server returns 'BYE'
  919. * If we don't have an imap_ stream we're already logged out */
  920. if(isset($imap_stream) && $imap_stream)
  921. sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
  922. }
  923. /**
  924. * Retrieve the CAPABILITY string from the IMAP server.
  925. * If capability is set, returns only that specific capability,
  926. * else returns array of all capabilities.
  927. * @param stream $imap_stream
  928. * @param string $capability (since 1.3.0)
  929. * @param boolean $bUseCache (since 1.5.1) Controls use of capability data stored in session
  930. * @return mixed (string if $capability is set and found,
  931. * false, if $capability is set and not found,
  932. * array if $capability not set)
  933. */
  934. function sqimap_capability($imap_stream, $capability='', $bUseCache=true) {
  935. // sqgetGlobalVar('sqimap_capabilities', $sqimap_capabilities, SQ_SESSION);
  936. if (!$bUseCache || ! sqgetGlobalVar('sqimap_capabilities', $sqimap_capabilities, SQ_SESSION)) {
  937. $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
  938. $c = explode(' ', $read[0]);
  939. for ($i=2; $i < count($c); $i++) {
  940. $cap_list = explode('=', $c[$i]);
  941. if (isset($cap_list[1])) {
  942. if(isset($sqimap_capabilities[trim($cap_list[0])]) &&
  943. !is_array($sqimap_capabilities[trim($cap_list[0])])) {
  944. // Remove array key that was added in 'else' block below
  945. // This is to accomodate for capabilities like:
  946. // SORT SORT=MODSEQ
  947. unset($sqimap_capabilities[trim($cap_list[0])]);
  948. }
  949. $sqimap_capabilities[trim($cap_list[0])][] = $cap_list[1];
  950. } else {
  951. if(!isset($sqimap_capabilities[trim($cap_list[0])])) {
  952. $sqimap_capabilities[trim($cap_list[0])] = TRUE;
  953. }
  954. }
  955. }
  956. }
  957. if ($capability) {
  958. if (isset($sqimap_capabilities[$capability])) {
  959. return $sqimap_capabilities[$capability];
  960. } else {
  961. return false;
  962. }
  963. }
  964. return $sqimap_capabilities;
  965. }
  966. /**
  967. * Returns the delimiter between mailboxes: INBOX/Test, or INBOX.Test
  968. * @param stream $imap_stream
  969. * @return string
  970. */
  971. function sqimap_get_delimiter ($imap_stream = false) {
  972. global $sqimap_delimiter, $optional_delimiter;
  973. /* Use configured delimiter if set */
  974. if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
  975. return $optional_delimiter;
  976. }
  977. /* Delimiter is stored in the session from redirect. Try fetching from there first */
  978. if (empty($sqimap_delimiter)) {
  979. sqgetGlobalVar('delimiter',$sqimap_delimiter,SQ_SESSION);
  980. }
  981. /* Do some caching here */
  982. if (!$sqimap_delimiter) {
  983. if (sqimap_capability($imap_stream, 'NAMESPACE')) {
  984. /*
  985. * According to something that I can't find, this is supposed to work on all systems
  986. * OS: This won't work in Courier IMAP.
  987. * OS: According to rfc2342 response from NAMESPACE command is:
  988. * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
  989. * OS: We want to lookup all personal NAMESPACES...
  990. *
  991. * TODO: remove this in favour of the information from sqimap_get_namespace()
  992. */
  993. $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
  994. if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
  995. if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
  996. $pn = $data2[1];
  997. }
  998. $pna = explode(')(', $pn);
  999. while (list($k, $v) = each($pna)) {
  1000. $lst = explode('"', $v);
  1001. if (isset($lst[3])) {
  1002. $pn[$lst[1]] = $lst[3];
  1003. } else {
  1004. $pn[$lst[1]] = '';
  1005. }
  1006. }
  1007. }
  1008. $sqimap_delimiter = $pn[0];
  1009. } else {
  1010. fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
  1011. $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
  1012. $read = $read['.'][0]; //sqimap_read_data() now returns a tag array of response array
  1013. $quote_position = strpos ($read[0], '"');
  1014. $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
  1015. }
  1016. }
  1017. return $sqimap_delimiter;
  1018. }
  1019. /**
  1020. * Retrieves the namespaces from the IMAP server.
  1021. * NAMESPACE is an IMAP extension defined in RFC 2342.
  1022. *
  1023. * @param stream $imap_stream
  1024. * @return array
  1025. */
  1026. function sqimap_get_namespace($imap_stream) {
  1027. $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
  1028. return sqimap_parse_namespace($read[0]);
  1029. }
  1030. /**
  1031. * Parses a NAMESPACE response and returns an array with the available
  1032. * personal, users and shared namespaces.
  1033. *
  1034. * @param string $input
  1035. * @return array The returned array has the following format:
  1036. * <pre>
  1037. * array(
  1038. * 'personal' => array(
  1039. * 0 => array('prefix'=>'INBOX.','delimiter' =>'.'),
  1040. * 1 => ...
  1041. * ),
  1042. * 'users' => array(..
  1043. * ),
  1044. * 'shared' => array( ..
  1045. * )
  1046. * )
  1047. * </pre>
  1048. * Note that if a namespace is not defined in the server, then the corresponding
  1049. * array will be empty.
  1050. */
  1051. function sqimap_parse_namespace(&$input) {
  1052. $ns_strings = array(1=>'personal', 2=>'users', 3=>'shared');
  1053. $namespace = array();
  1054. if(ereg('NAMESPACE (\(\(.*\)\)|NIL) (\(\(.*\)\)|NIL) (\(\(.*\)\)|NIL)', $input, $regs) !== false) {
  1055. for($i=1; $i<=3; $i++) {
  1056. if($regs[$i] == 'NIL') {
  1057. $namespace[$ns_strings[$i]] = array();
  1058. } else {
  1059. // Pop-out the first ( and last ) for easier parsing
  1060. $ns = substr($regs[$i], 1, sizeof($regs[$i])-2);
  1061. if($c = preg_match_all('/\((?:(.*?)\s*?)\)/', $ns, $regs2)) {
  1062. $namespace[$ns_strings[$i]] = array();
  1063. for($j=0; $j<sizeof($regs2[1]); $j++) {
  1064. preg_match('/"(.*)"\s+("(.*)"|NIL)/', $regs2[1][$j], $regs3);
  1065. $namespace[$ns_strings[$i]][$j]['prefix'] = $regs3[1];
  1066. if($regs3[2] == 'NIL') {
  1067. $namespace[$ns_strings[$i]][$j]['delimiter'] = null;
  1068. } else {
  1069. // $regs[3] is $regs[2] without the quotes
  1070. $namespace[$ns_strings[$i]][$j]['delimiter'] = $regs3[3];
  1071. }
  1072. unset($regs3);
  1073. }
  1074. }
  1075. unset($ns);
  1076. }
  1077. }
  1078. }
  1079. return($namespace);
  1080. }
  1081. /**
  1082. * This encodes a mailbox name for use in IMAP commands.
  1083. * @param string $what the mailbox to encode
  1084. * @return string the encoded mailbox string
  1085. * @since 1.5.0
  1086. */
  1087. function sqimap_encode_mailbox_name($what)
  1088. {
  1089. if (ereg("[\"\\\r\n]", $what))
  1090. return '{' . strlen($what) . "}\r\n" . $what; /* 4.3 literal form */
  1091. return '"' . $what . '"'; /* 4.3 quoted string form */
  1092. }
  1093. /**
  1094. * Gets the number of messages in the current mailbox.
  1095. *
  1096. * OBSOLETE use sqimap_status_messages instead.
  1097. * @param stream $imap_stream imap stream
  1098. * @param string $mailbox
  1099. * @deprecated
  1100. */
  1101. function sqimap_get_num_messages ($imap_stream, $mailbox) {
  1102. $aStatus = sqimap_status_messages($imap_stream,$mailbox,array('MESSAGES'));
  1103. return $aStatus['MESSAGES'];
  1104. }
  1105. /**
  1106. * OBSOLETE FUNCTION should be removed after mailbox_display,
  1107. * printMessage function is adapted
  1108. * $addr_ar = array(), $group = '' and $host='' arguments are used in 1.4.0
  1109. * @param string $address
  1110. * @param integer $max
  1111. * @since 1.4.0
  1112. * @deprecated See Rfc822Address.php
  1113. */
  1114. function parseAddress($address, $max=0) {
  1115. $aAddress = parseRFC822Address($address,array('limit'=> $max));
  1116. /*
  1117. * Because the expected format of the array element is changed we adapt it now.
  1118. * This also implies that this function is obsolete and should be removed after the
  1119. * rest of the source is adapted. See Rfc822Address.php for the new function.
  1120. */
  1121. array_walk($aAddress, '_adaptAddress');
  1122. return $aAddress;
  1123. }
  1124. /**
  1125. * OBSOLETE FUNCTION should be removed after mailbox_display,
  1126. * printMessage function is adapted
  1127. *
  1128. * callback function used for formating of addresses array in
  1129. * parseAddress() function
  1130. * @param array $aAddr
  1131. * @param integer $k array key
  1132. * @since 1.5.1
  1133. * @deprecated
  1134. */
  1135. function _adaptAddress(&$aAddr,$k) {
  1136. $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL]) && $aAddr[SQM_ADDR_PERSONAL]) ?
  1137. $aAddr[SQM_ADDR_PERSONAL] : '';
  1138. $sEmail = ($aAddr[SQM_ADDR_HOST]) ?
  1139. $aAddr[SQM_ADDR_MAILBOX] . '@'.$aAddr[SQM_ADDR_HOST] :
  1140. $aAddr[SQM_ADDR_MAILBOX];
  1141. $aAddr = array($sEmail,$sPersonal);
  1142. }
  1143. /**
  1144. * Returns the number of unseen messages in this folder.
  1145. * obsoleted by sqimap_status_messages !
  1146. * Arguments differ in 1.0.x
  1147. * @param stream $imap_stream
  1148. * @param string $mailbox
  1149. * @return integer
  1150. * @deprecated
  1151. */
  1152. function sqimap_unseen_messages ($imap_stream, $mailbox) {
  1153. $aStatus = sqimap_status_messages($imap_stream,$mailbox,array('UNSEEN'));
  1154. return $aStatus['UNSEEN'];
  1155. }
  1156. /**
  1157. * Returns the status items of a mailbox.
  1158. * Default it returns MESSAGES,UNSEEN and RECENT
  1159. * Supported status items are MESSAGES, UNSEEN, RECENT (since 1.4.0),
  1160. * UIDNEXT (since 1.5.1) and UIDVALIDITY (since 1.5.1)
  1161. * @param stream $imap_stream imap stream
  1162. * @param string $mailbox mail folder
  1163. * @param array $aStatusItems status items
  1164. * @return array
  1165. * @since 1.3.2
  1166. */
  1167. function sqimap_status_messages ($imap_stream, $mailbox,
  1168. $aStatusItems = array('MESSAGES','UNSEEN','RECENT')) {
  1169. $aStatusItems = implode(' ',$aStatusItems);
  1170. $read_ary = sqimap_run_command ($imap_stream, 'STATUS ' . sqimap_encode_mailbox_name($mailbox) .
  1171. " ($aStatusItems)", false, $result, $message);
  1172. $i = 0;
  1173. $messages = $unseen = $recent = $uidnext = $uidvalidity = false;
  1174. $regs = array(false,false);
  1175. while (isset($read_ary[$i])) {
  1176. if (preg_match('/UNSEEā€¦

Large files files are truncated, but you can click here to view the full file