PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/squirrelmail/functions/imap_general.php

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

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