imap_general.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <?php
  2. /**
  3. * imap_general.php
  4. *
  5. * Copyright (c) 1999-2003 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This implements all functions that do general imap functions.
  9. *
  10. * $Id$
  11. */
  12. require_once(SM_PATH . 'functions/page_header.php');
  13. require_once(SM_PATH . 'functions/auth.php');
  14. global $sqimap_session_id;
  15. $sqimap_session_id = 1;
  16. /* Sets an unique session id in order to avoid simultanous sessions crash. */
  17. function sqimap_session_id($unique_id = false) {
  18. global $data_dir, $username, $sqimap_session_id;
  19. if (!$unique_id) {
  20. return( sprintf("A%03d", $sqimap_session_id++) );
  21. } else {
  22. return( sprintf("A%03d", $sqimap_session_id++) . ' UID' );
  23. }
  24. }
  25. /*
  26. * Both send a command and accept the result from the command.
  27. * This is to allow proper session number handling.
  28. */
  29. function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
  30. if ($imap_stream) {
  31. $sid = sqimap_session_id($unique_id);
  32. fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
  33. $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
  34. return $read;
  35. } else {
  36. global $squirrelmail_language, $color;
  37. set_up_language($squirrelmail_language);
  38. require_once(SM_PATH . 'functions/display_messages.php');
  39. $string = "<b><font color=$color[2]>\n" .
  40. _("ERROR : No available imapstream.") .
  41. "</b></font>\n";
  42. error_box($string,$color);
  43. return false;
  44. }
  45. }
  46. function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
  47. if ($imap_stream) {
  48. $sid = sqimap_session_id($unique_id);
  49. fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
  50. $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
  51. return $read;
  52. } else {
  53. global $squirrelmail_language, $color;
  54. set_up_language($squirrelmail_language);
  55. require_once(SM_PATH . 'functions/display_messages.php');
  56. $string = "<b><font color=$color[2]>\n" .
  57. _("ERROR : No available imapstream.") .
  58. "</b></font>\n";
  59. error_box($string,$color);
  60. return false;
  61. }
  62. }
  63. /*
  64. * custom fgets function. gets a line from IMAP
  65. * no matter how big it may be
  66. */
  67. function sqimap_fgets($imap_stream) {
  68. $read = '';
  69. $buffer = 4096;
  70. $results = '';
  71. $offset = 0;
  72. while (strpos($results, "\r\n", $offset) === false) {
  73. if (!($read = fgets($imap_stream, $buffer))) {
  74. break;
  75. }
  76. if ( $results != '' ) {
  77. $offset = strlen($results) - 1;
  78. }
  79. $results .= $read;
  80. }
  81. return $results;
  82. }
  83. /*
  84. * Reads the output from the IMAP stream. If handle_errors is set to true,
  85. * this will also handle all errors that are received. If it is not set,
  86. * the errors will be sent back through $response and $message
  87. */
  88. function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
  89. global $color, $squirrelmail_language;
  90. $read = '';
  91. $pre_a = explode(' ',trim($pre));
  92. $pre = $pre_a[0];
  93. $resultlist = array();
  94. $data = array();
  95. $read = sqimap_fgets($imap_stream);
  96. while (1) {
  97. switch (true) {
  98. case preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs):
  99. case preg_match('/^\* (BYE \[ALERT\])(.*)$/', $read, $regs):
  100. $response = $regs[1];
  101. $message = trim($regs[2]);
  102. break 2;
  103. case preg_match("/^\* (OK \[PARSE\])(.*)$/", $read):
  104. $read = sqimap_fgets($imap_stream);
  105. break 1;
  106. case preg_match('/^\* ([0-9]+) FETCH.*/', $read, $regs):
  107. $fetch_data = array();
  108. $fetch_data[] = $read;
  109. $read = sqimap_fgets($imap_stream);
  110. while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) &&
  111. !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) {
  112. $fetch_data[] = $read;
  113. $last = $read;
  114. $read = sqimap_fgets($imap_stream);
  115. }
  116. if (isset($last) && preg_match('/^\)/', $last)) {
  117. array_pop($fetch_data);
  118. }
  119. $resultlist[] = $fetch_data;
  120. break 1;
  121. default:
  122. $data[] = $read;
  123. $read = sqimap_fgets($imap_stream);
  124. break 1;
  125. }
  126. }
  127. if (!empty($data)) {
  128. $resultlist[] = $data;
  129. }
  130. elseif (empty($resultlist)) {
  131. $resultlist[] = array();
  132. }
  133. if ($handle_errors == false) {
  134. return( $resultlist );
  135. }
  136. elseif ($response == 'NO') {
  137. /* ignore this error from M$ exchange, it is not fatal (aka bug) */
  138. if (strstr($message, 'command resulted in') === false) {
  139. set_up_language($squirrelmail_language);
  140. require_once(SM_PATH . 'functions/display_messages.php');
  141. $string = "<b><font color=$color[2]>\n" .
  142. _("ERROR : Could not complete request.") .
  143. "</b><br>\n" .
  144. _("Query:") . ' ' .
  145. htmlspecialchars($query) . '<br>' .
  146. _("Reason Given: ") .
  147. htmlspecialchars($message) . "</font><br>\n";
  148. error_box($string,$color);
  149. exit;
  150. }
  151. }
  152. elseif ($response == 'BAD') {
  153. set_up_language($squirrelmail_language);
  154. require_once(SM_PATH . 'functions/display_messages.php');
  155. $string = "<b><font color=$color[2]>\n" .
  156. _("ERROR : Bad or malformed request.") .
  157. "</b><br>\n" .
  158. _("Query:") . ' '.
  159. htmlspecialchars($query) . '<br>' .
  160. _("Server responded: ") .
  161. htmlspecialchars($message) . "</font><br>\n";
  162. error_box($string,$color);
  163. exit;
  164. }
  165. else {
  166. return $resultlist;
  167. }
  168. }
  169. function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
  170. $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
  171. /* sqimap_read_data should be called for one response
  172. but since it just calls sqimap_read_data_list which
  173. handles multiple responses we need to check for that
  174. and merge the $res array IF they are seperated and
  175. IF it was a FETCH response. */
  176. if (isset($res[1]) && is_array($res[1]) && isset($res[1][0])
  177. && preg_match('/^\* \d+ FETCH/', $res[1][0])) {
  178. $result = array();
  179. foreach($res as $index=>$value) {
  180. $result = array_merge($result, $res["$index"]);
  181. }
  182. }
  183. if (isset($result)) {
  184. return $result;
  185. }
  186. else {
  187. return $res[0];
  188. }
  189. }
  190. /*
  191. * Logs the user into the imap server. If $hide is set, no error messages
  192. * will be displayed. This function returns the imap connection handle.
  193. */
  194. function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
  195. global $color, $squirrelmail_language, $onetimepad, $use_imap_tls, $imap_auth_mech;
  196. if (!isset($onetimepad) || empty($onetimepad)) {
  197. sqgetglobalvar('onetimepad' , $onetimepad , SQ_SESSION );
  198. }
  199. $imap_server_address = sqimap_get_user_server($imap_server_address, $username);
  200. $host=$imap_server_address;
  201. if (($use_imap_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
  202. /* Use TLS by prefixing "tls://" to the hostname */
  203. $imap_server_address = 'tls://' . $imap_server_address;
  204. }
  205. $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
  206. /* Do some error correction */
  207. if (!$imap_stream) {
  208. if (!$hide) {
  209. set_up_language($squirrelmail_language, true);
  210. require_once(SM_PATH . 'functions/display_messages.php');
  211. $string = sprintf (_("Error connecting to IMAP server: %s.") .
  212. "<br>\r\n", $imap_server_address) .
  213. "$error_number : $error_string<br>\r\n";
  214. logout_error($string,$color);
  215. }
  216. exit;
  217. }
  218. $server_info = fgets ($imap_stream, 1024);
  219. /* Decrypt the password */
  220. $password = OneTimePadDecrypt($password, $onetimepad);
  221. if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) {
  222. // We're using some sort of authentication OTHER than plain or login
  223. $tag=sqimap_session_id(false);
  224. if ($imap_auth_mech == 'digest-md5') {
  225. $query = $tag . " AUTHENTICATE DIGEST-MD5\r\n";
  226. } elseif ($imap_auth_mech == 'cram-md5') {
  227. $query = $tag . " AUTHENTICATE CRAM-MD5\r\n";
  228. }
  229. fputs($imap_stream,$query);
  230. $answer=sqimap_fgets($imap_stream);
  231. // Trim the "+ " off the front
  232. $response=explode(" ",$answer,3);
  233. if ($response[0] == '+') {
  234. // Got a challenge back
  235. $challenge=$response[1];
  236. if ($imap_auth_mech == 'digest-md5') {
  237. $reply = digest_md5_response($username,$password,$challenge,'imap',$host);
  238. } elseif ($imap_auth_mech == 'cram-md5') {
  239. $reply = cram_md5_response($username,$password,$challenge);
  240. }
  241. fputs($imap_stream,$reply);
  242. $read=sqimap_fgets($imap_stream);
  243. if ($imap_auth_mech == 'digest-md5') {
  244. // DIGEST-MD5 has an extra step..
  245. if (substr($read,0,1) == '+') { // OK so far..
  246. fputs($imap_stream,"\r\n");
  247. $read=sqimap_fgets($imap_stream);
  248. }
  249. }
  250. $results=explode(" ",$read,3);
  251. $response=$results[1];
  252. $message=$results[2];
  253. } else {
  254. // Fake the response, so the error trap at the bottom will work
  255. $response="BAD";
  256. $message='IMAP server does not appear to support the authentication method selected.';
  257. $message .= ' Please contact your system administrator.';
  258. }
  259. } elseif ($imap_auth_mech == 'login') {
  260. // Original IMAP login code
  261. $query = 'LOGIN "' . quoteimap($username) . '" "' . quoteimap($password) . '"';
  262. $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
  263. } elseif ($imap_auth_mech == 'plain') {
  264. /* Replace this with SASL PLAIN if it ever gets implemented */
  265. $response="BAD";
  266. $message='SquirrelMail does not support SASL PLAIN yet. Rerun conf.pl and use login instead.';
  267. } else {
  268. $response="BAD";
  269. $message="Internal SquirrelMail error - unknown IMAP authentication method chosen. Please contact the developers.";
  270. }
  271. /* If the connection was not successful, lets see why */
  272. if ($response != 'OK') {
  273. if (!$hide) {
  274. if ($response != 'NO') {
  275. /* "BAD" and anything else gets reported here. */
  276. $message = htmlspecialchars($message);
  277. set_up_language($squirrelmail_language, true);
  278. require_once(SM_PATH . 'functions/display_messages.php');
  279. if ($response == 'BAD') {
  280. $string = sprintf (_("Bad request: %s")."<br>\r\n", $message);
  281. } else {
  282. $string = sprintf (_("Unknown error: %s") . "<br>\n", $message);
  283. }
  284. if (isset($read) && is_array($read)) {
  285. $string .= '<br>' . _("Read data:") . "<br>\n";
  286. foreach ($read as $line) {
  287. $string .= htmlspecialchars($line) . "<br>\n";
  288. }
  289. }
  290. error_box($string,$color);
  291. exit;
  292. } else {
  293. /*
  294. * If the user does not log in with the correct
  295. * username and password it is not possible to get the
  296. * correct locale from the user's preferences.
  297. * Therefore, apply the same hack as on the login
  298. * screen.
  299. *
  300. * $squirrelmail_language is set by a cookie when
  301. * the user selects language and logs out
  302. */
  303. set_up_language($squirrelmail_language, true);
  304. include_once(SM_PATH . 'functions/display_messages.php' );
  305. sqsession_destroy();
  306. logout_error( _("Unknown user or password incorrect.") );
  307. exit;
  308. }
  309. } else {
  310. exit;
  311. }
  312. }
  313. return $imap_stream;
  314. }
  315. /* Simply logs out the IMAP session */
  316. function sqimap_logout ($imap_stream) {
  317. /* Logout is not valid until the server returns 'BYE'
  318. * If we don't have an imap_ stream we're already logged out */
  319. if(isset($imap_stream) && $imap_stream)
  320. sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
  321. }
  322. function sqimap_capability($imap_stream, $capability='') {
  323. global $sqimap_capabilities;
  324. if (!is_array($sqimap_capabilities)) {
  325. $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
  326. $c = explode(' ', $read[0]);
  327. for ($i=2; $i < count($c); $i++) {
  328. $cap_list = explode('=', $c[$i]);
  329. if (isset($cap_list[1])) {
  330. $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
  331. } else {
  332. $sqimap_capabilities[$cap_list[0]] = TRUE;
  333. }
  334. }
  335. }
  336. if ($capability) {
  337. if (isset($sqimap_capabilities[$capability])) {
  338. return $sqimap_capabilities[$capability];
  339. } else {
  340. return false;
  341. }
  342. }
  343. return $sqimap_capabilities;
  344. }
  345. /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
  346. function sqimap_get_delimiter ($imap_stream = false) {
  347. global $sqimap_delimiter, $optional_delimiter;
  348. /* Use configured delimiter if set */
  349. if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
  350. return $optional_delimiter;
  351. }
  352. /* Do some caching here */
  353. if (!$sqimap_delimiter) {
  354. if (sqimap_capability($imap_stream, 'NAMESPACE')) {
  355. /*
  356. * According to something that I can't find, this is supposed to work on all systems
  357. * OS: This won't work in Courier IMAP.
  358. * OS: According to rfc2342 response from NAMESPACE command is:
  359. * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
  360. * OS: We want to lookup all personal NAMESPACES...
  361. */
  362. $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
  363. if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
  364. if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
  365. $pn = $data2[1];
  366. }
  367. $pna = explode(')(', $pn);
  368. while (list($k, $v) = each($pna)) {
  369. $lst = explode('"', $v);
  370. if (isset($lst[3])) {
  371. $pn[$lst[1]] = $lst[3];
  372. } else {
  373. $pn[$lst[1]] = '';
  374. }
  375. }
  376. }
  377. $sqimap_delimiter = $pn[0];
  378. } else {
  379. fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
  380. $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
  381. $quote_position = strpos ($read[0], '"');
  382. $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
  383. }
  384. }
  385. return $sqimap_delimiter;
  386. }
  387. /* Gets the number of messages in the current mailbox. */
  388. function sqimap_get_num_messages ($imap_stream, $mailbox) {
  389. $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", false, $result, $message);
  390. for ($i = 0; $i < count($read_ary); $i++) {
  391. if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
  392. return $regs[1];
  393. }
  394. }
  395. return false; //"BUG! Couldn't get number of messages in $mailbox!";
  396. }
  397. function parseAddress($address, $max=0, $addr_ar = array(), $group = '', $host='', $limit=0) {
  398. $pos = 0;
  399. $j = strlen($address);
  400. $personal = '';
  401. $addr = '';
  402. $comment = '';
  403. if ($max && $max == count($addr_ar)) {
  404. return $addr_ar;
  405. }
  406. while ($pos < $j) {
  407. if ($max && $max == count($addr_ar)) {
  408. return $addr_ar;
  409. }
  410. $char = $address{$pos};
  411. switch ($char) {
  412. case '=':
  413. /* check if it is an encoded string */
  414. if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',substr($address,$pos),$reg)) {
  415. /* add stringpart before the encoded string to the personal var */
  416. if (!$personal) {
  417. $personal = substr($address,0,$pos);
  418. }
  419. $personal .= $reg[1];
  420. $pos += strlen($reg[1]);
  421. } else {
  422. ++$pos;
  423. }
  424. break;
  425. case '"': /* get the personal name */
  426. ++$pos;
  427. if ($address{$pos} == '"') {
  428. ++$pos;
  429. } else {
  430. $personal_start = $personal_end = $pos;
  431. while ($pos < $j) {
  432. $personal_end = strpos($address,'"',$pos);
  433. if (($personal_end-2)>0 && (substr($address,$personal_end-2,2) === '\\"' ||
  434. substr($address,$personal_end-2,2) === '\\\\')) {
  435. $pos = $personal_end+1;
  436. } else {
  437. $personal = substr($address,$personal_start,$personal_end-$personal_start);
  438. break;
  439. }
  440. }
  441. if ($personal_end) { /* prohibit endless loops due to very wrong addresses */
  442. $pos = $personal_end+1;
  443. } else {
  444. $pos = $j;
  445. }
  446. }
  447. break;
  448. case '<': /* get email address */
  449. $addr_start = $pos;
  450. $addr_end = strpos($address,'>',$addr_start);
  451. if($addr_end === FALSE) {
  452. // in case the address doesn't end, prevent loop
  453. $pos++;
  454. } else {
  455. $addr = substr($address,$addr_start+1,$addr_end-$addr_start-1);
  456. $pos = $addr_end+1;
  457. }
  458. break;
  459. case '(': /* rip off comments */
  460. $addr_start = $pos;
  461. $pos = strpos($address,')');
  462. if ($pos !== false) {
  463. $comment = substr($address, $addr_start+1,($pos-$addr_start-1));
  464. $address_start = substr($address, 0, $addr_start);
  465. $address_end = substr($address, $pos + 1);
  466. $address = $address_start . $address_end;
  467. }
  468. $j = strlen($address);
  469. $pos = $addr_start + 1;
  470. break;
  471. case ';': /* we reached a non rfc2822 compliant delimiter */
  472. if ($group) {
  473. $address = substr($address, 0, $pos - 1);
  474. ++$pos;
  475. break;
  476. }
  477. case ',': /* we reached a delimiter */
  478. if ($addr == '') {
  479. $addr = substr($address, 0, $pos);
  480. } else if ($personal == '') {
  481. $personal = trim(substr($address, 0, $addr_start));
  482. }
  483. if (!$personal && $comment) $personal = $comment;
  484. if ($personal) $personal = decodeHeader($personal);
  485. $addr_ar[] = array($addr,$personal);
  486. $address = trim(substr($address, $pos+1));
  487. $j = strlen($address);
  488. $pos = 0;
  489. $personal = '';
  490. $addr = '';
  491. break;
  492. case ':': /* process the group addresses */
  493. /* group marker */
  494. $group = substr($address, 0, $pos);
  495. $address = substr($address, $pos+1);
  496. $result = parseAddress($address, $max, $addr_ar, $group);
  497. $addr_ar = $result[0];
  498. $pos = $result[1];
  499. $address = substr($address, $pos++);
  500. $j = strlen($address);
  501. $group = '';
  502. break;
  503. default:
  504. ++$pos;
  505. break;
  506. }
  507. }
  508. if ($addr == '') {
  509. $addr = substr($address, 0, $pos);
  510. } else if ($personal == '') {
  511. $personal = trim(substr($address, 0, $addr_start));
  512. }
  513. if (!$personal && $comment) $personal = $comment;
  514. $email = $addr;
  515. if ($group && $addr == '') { /* no addresses found in group */
  516. $personal = $group;
  517. $addr_ar[] = array('',$personal);
  518. return (array($addr_ar,$pos+1 ));
  519. } elseif ($group) {
  520. $addr_ar[] = array($addr,$personal);
  521. return (array($addr_ar,$pos+1 ));
  522. } else {
  523. if ($personal || $addr) {
  524. $addr_ar[] = array($addr, $personal);
  525. }
  526. }
  527. return ($addr_ar);
  528. }
  529. /*
  530. * Returns the number of unseen messages in this folder
  531. */
  532. function sqimap_unseen_messages ($imap_stream, $mailbox) {
  533. $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", false, $result, $message);
  534. $i = 0;
  535. $regs = array(false, false);
  536. while (isset($read_ary[$i])) {
  537. if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
  538. break;
  539. }
  540. $i++;
  541. }
  542. return $regs[1];
  543. }
  544. /*
  545. * Returns the number of unseen/total messages in this folder
  546. */
  547. function sqimap_status_messages ($imap_stream, $mailbox) {
  548. $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (MESSAGES UNSEEN RECENT)", false, $result, $message);
  549. $i = 0;
  550. $messages = $unseen = $recent = false;
  551. $regs = array(false,false);
  552. while (isset($read_ary[$i])) {
  553. if (preg_match('/UNSEEN\s+([0-9]+)/i', $read_ary[$i], $regs)) {
  554. $unseen = $regs[1];
  555. }
  556. if (preg_match('/MESSAGES\s+([0-9]+)/i', $read_ary[$i], $regs)) {
  557. $messages = $regs[1];
  558. }
  559. if (preg_match('/RECENT\s+([0-9]+)/i', $read_ary[$i], $regs)) {
  560. $recent = $regs[1];
  561. }
  562. $i++;
  563. }
  564. return array('MESSAGES' => $messages, 'UNSEEN'=>$unseen, 'RECENT' => $recent);
  565. }
  566. /*
  567. * Saves a message to a given folder -- used for saving sent messages
  568. */
  569. function sqimap_append ($imap_stream, $sent_folder, $length) {
  570. fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
  571. $tmp = fgets ($imap_stream, 1024);
  572. }
  573. function sqimap_append_done ($imap_stream, $folder='') {
  574. global $squirrelmail_language, $color;
  575. fputs ($imap_stream, "\r\n");
  576. $tmp = fgets ($imap_stream, 1024);
  577. if (preg_match("/(.*)(BAD|NO)(.*)$/", $tmp, $regs)) {
  578. set_up_language($squirrelmail_language);
  579. require_once(SM_PATH . 'functions/display_messages.php');
  580. $reason = $regs[3];
  581. if ($regs[2] == 'NO') {
  582. $string = "<b><font color=$color[2]>\n" .
  583. _("ERROR : Could not append message to") ." $folder." .
  584. "</b><br>\n" .
  585. _("Server responded: ") .
  586. $reason . "<br>\n";
  587. if (preg_match("/(.*)(quota)(.*)$/i", $reason, $regs)) {
  588. $string .= _("Solution: ") .
  589. _("Remove unneccessary messages from your folder and start with your Trash folder.")
  590. ."<br>\n";
  591. }
  592. $string .= "</font>\n";
  593. error_box($string,$color);
  594. } else {
  595. $string = "<b><font color=$color[2]>\n" .
  596. _("ERROR : Bad or malformed request.") .
  597. "</b><br>\n" .
  598. _("Server responded: ") .
  599. $tmp . "</font><br>\n";
  600. error_box($string,$color);
  601. exit;
  602. }
  603. }
  604. }
  605. function sqimap_get_user_server ($imap_server, $username) {
  606. if (substr($imap_server, 0, 4) != "map:") {
  607. return $imap_server;
  608. }
  609. $function = substr($imap_server, 4);
  610. return $function($username);
  611. }
  612. /* This is an example that gets imapservers from yellowpages (NIS).
  613. * you can simple put map:map_yp_alias in your $imap_server_address
  614. * in config.php use your own function instead map_yp_alias to map your
  615. * LDAP whatever way to find the users imapserver. */
  616. function map_yp_alias($username) {
  617. $yp = `ypmatch $username aliases`;
  618. return chop(substr($yp, strlen($username)+1));
  619. }
  620. ?>