imap_general.php 26 KB

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