imap_general.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * imap.php
  4. *
  5. * Copyright (c) 1999-2001 The SquirrelMail Development 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('../functions/page_header.php');
  13. require_once('../functions/display_messages.php');
  14. /**
  15. * Unique SessionId
  16. *
  17. * Sets an unique session id in order to avoid simultanous sessions crash.
  18. *
  19. * @return string a 4 chars unique string
  20. */
  21. function sqimap_session_id() {
  22. global $data_dir, $username;
  23. $IMAPSessionID = substr(session_id(), -4);
  24. if( $IMAPSessionID == '' ) {
  25. $IMAPSessionID = 'A001';
  26. }
  27. return( $IMAPSessionID );
  28. }
  29. /******************************************************************************
  30. ** Reads the output from the IMAP stream. If handle_errors is set to true,
  31. ** this will also handle all errors that are received. If it is not set,
  32. ** the errors will be sent back through $response and $message
  33. ******************************************************************************/
  34. function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
  35. &$response, &$message) {
  36. global $color, $squirrelmail_language;
  37. $read = '';
  38. $resultlist = array();
  39. $more_msgs = true;
  40. while ($more_msgs) {
  41. $data = array();
  42. $total_size = 0;
  43. while (strpos($read, "\n") === false) {
  44. $read .= fgets($imap_stream, 9096);
  45. }
  46. if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
  47. $size = $regs[1];
  48. } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
  49. // Sizeless response, probably single-line
  50. $size = -1;
  51. $data[] = $read;
  52. $read = fgets($imap_stream, 9096);
  53. } else {
  54. $size = -1;
  55. }
  56. while (1) {
  57. while (strpos($read, "\n") === false) {
  58. $read .= fgets($imap_stream, 9096);
  59. }
  60. // If we know the size, no need to look at the end parameters
  61. if ($size > 0) {
  62. if ($total_size == $size) {
  63. // We've reached the end of this 'message', switch to the next one.
  64. $data[] = $read;
  65. break;
  66. } else if ($total_size > $size) {
  67. $difference = $total_size - $size;
  68. $total_size = $total_size - strlen($read);
  69. $data[] = substr ($read, 0, strlen($read)-$difference);
  70. $read = substr ($read, strlen($read)-$difference, strlen($read));
  71. break;
  72. } else {
  73. $data[] = $read;
  74. $read = fgets($imap_stream, 9096);
  75. while (strpos($read, "\n") === false) {
  76. $read .= fgets($imap_stream, 9096);
  77. }
  78. }
  79. $total_size += strlen($read);
  80. } else {
  81. if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
  82. (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
  83. break;
  84. } else {
  85. $data[] = $read;
  86. $read = fgets ($imap_stream, 9096);
  87. }
  88. }
  89. }
  90. while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
  91. !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
  92. $read = fgets($imap_stream, 9096);
  93. }
  94. $resultlist[] = $data;
  95. }
  96. $response = $regs[1];
  97. $message = trim($regs[2]);
  98. if ($handle_errors == false) { return $resultlist; }
  99. if ($response == 'NO') {
  100. // ignore this error from m$ exchange, it is not fatal (aka bug)
  101. if (strstr($message, 'command resulted in') === false) {
  102. set_up_language($squirrelmail_language);
  103. echo "<br><b><font color=$color[2]>\n" .
  104. _("ERROR : Could not complete request.") .
  105. "</b><br>\n" .
  106. _("Reason Given: ") .
  107. $message . "</font><br>\n";
  108. exit;
  109. }
  110. } else if ($response == 'BAD') {
  111. set_up_language($squirrelmail_language);
  112. echo "<br><b><font color=$color[2]>\n" .
  113. _("ERROR : Bad or malformed request.") .
  114. "</b><br>\n" .
  115. _("Server responded: ") .
  116. $message . "</font><br>\n";
  117. exit;
  118. }
  119. return $resultlist;
  120. }
  121. function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
  122. $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
  123. return $res[0];
  124. }
  125. /******************************************************************************
  126. ** Logs the user into the imap server. If $hide is set, no error messages
  127. ** will be displayed. This function returns the imap connection handle.
  128. ******************************************************************************/
  129. function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
  130. global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
  131. $imap_stream = fsockopen ( $imap_server_address, $imap_port,
  132. $error_number, $error_string, 15);
  133. if ( !$imap_stream ) {
  134. return FALSE;
  135. }
  136. $server_info = fgets ($imap_stream, 1024);
  137. // Decrypt the password
  138. $password = OneTimePadDecrypt($password, $onetimepad);
  139. /** Do some error correction **/
  140. if (!$imap_stream) {
  141. if (!$hide) {
  142. set_up_language($squirrelmail_language, true);
  143. printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
  144. echo "$error_number : $error_string<br>\r\n";
  145. }
  146. exit;
  147. }
  148. fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
  149. '" "' . quoteIMAP($password) . "\"\r\n");
  150. $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
  151. /** If the connection was not successful, lets see why **/
  152. if ($response != 'OK') {
  153. if (!$hide) {
  154. if ($response != 'NO') {
  155. // "BAD" and anything else gets reported here.
  156. set_up_language($squirrelmail_language, true);
  157. if ($response == 'BAD') {
  158. printf (_("Bad request: %s")."<br>\r\n", $message);
  159. } else {
  160. printf (_("Unknown error: %s") . "<br>\n", $message);
  161. }
  162. echo '<br>' . _("Read data:") . "<br>\n";
  163. if (is_array($read)) {
  164. foreach ($read as $line) {
  165. echo htmlspecialchars($line) . "<br>\n";
  166. }
  167. }
  168. exit;
  169. } else {
  170. // If the user does not log in with the correct
  171. // username and password it is not possible to get the
  172. // correct locale from the user's preferences.
  173. // Therefore, apply the same hack as on the login
  174. // screen.
  175. // $squirrelmail_language is set by a cookie when
  176. // the user selects language and logs out
  177. set_up_language($squirrelmail_language, true);
  178. displayHtmlHeader( _("Unknown user or password incorrect.") );
  179. echo "<body bgcolor=\"#ffffff\">\n";
  180. error_username_password_incorrect();
  181. session_destroy();
  182. exit;
  183. }
  184. } else {
  185. exit;
  186. }
  187. }
  188. return $imap_stream;
  189. }
  190. /*
  191. * Simply logs out the imap session
  192. */
  193. function sqimap_logout ($imap_stream) {
  194. fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
  195. }
  196. function sqimap_capability($imap_stream, $capability) {
  197. global $sqimap_capabilities;
  198. if (!is_array($sqimap_capabilities)) {
  199. fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
  200. $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
  201. $c = explode(' ', $read[0]);
  202. for ($i=2; $i < count($c); $i++) {
  203. $cap_list = explode('=', $c[$i]);
  204. if (isset($cap_list[1]))
  205. $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
  206. else
  207. $sqimap_capabilities[$cap_list[0]] = TRUE;
  208. }
  209. }
  210. if (! isset($sqimap_capabilities[$capability])) {
  211. return false;
  212. } else {
  213. return $sqimap_capabilities[$capability];
  214. }
  215. }
  216. /******************************************************************************
  217. ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
  218. ******************************************************************************/
  219. function sqimap_get_delimiter ($imap_stream = false) {
  220. global $sqimap_delimiter;
  221. global $optional_delimiter;
  222. /* Use configured delimiter if set */
  223. if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
  224. return $optional_delimiter;
  225. }
  226. /* Do some caching here */
  227. if (!$sqimap_delimiter) {
  228. if (sqimap_capability($imap_stream, 'NAMESPACE')) {
  229. /* According to something that I can't find, this is supposed to work on all systems
  230. OS: This won't work in Courier IMAP.
  231. OS: According to rfc2342 response from NAMESPACE command is:
  232. OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
  233. OS: We want to lookup all personal NAMESPACES...
  234. */
  235. fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
  236. $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
  237. if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
  238. if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
  239. $pn = $data2[1];
  240. }
  241. $pna = explode(')(', $pn);
  242. while (list($k, $v) = each($pna)) {
  243. $lst = explode('"', $v);
  244. if (isset($lst[3])) {
  245. $pn[$lst[1]] = $lst[3];
  246. } else {
  247. $pn[$lst[1]] = '';
  248. }
  249. }
  250. }
  251. $sqimap_delimiter = $pn[0];
  252. } else {
  253. fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
  254. $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
  255. $quote_position = strpos ($read[0], '"');
  256. $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
  257. }
  258. }
  259. return $sqimap_delimiter;
  260. }
  261. /*
  262. * Gets the number of messages in the current mailbox.
  263. */
  264. function sqimap_get_num_messages ($imap_stream, $mailbox) {
  265. fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
  266. $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
  267. for ($i = 0; $i < count($read_ary); $i++) {
  268. if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
  269. return $regs[1];
  270. }
  271. }
  272. return sprintf( "BUG! Couldn't get number of messages in %s!", $mailbox );
  273. }
  274. /*
  275. * Returns a displayable email address
  276. */
  277. function sqimap_find_email ($string) {
  278. /** Luke Ehresman <lehresma@css.tayloru.edu>
  279. ** <lehresma@css.tayloru.edu>
  280. ** lehresma@css.tayloru.edu
  281. **
  282. ** What about
  283. ** lehresma@css.tayloru.edu (Luke Ehresman)
  284. **/
  285. if (ereg("<([^>]+)>", $string, $regs)) {
  286. $string = $regs[1];
  287. }
  288. return trim($string);
  289. }
  290. /*
  291. * Takes the From: field, and creates a displayable name.
  292. * Luke Ehresman <lkehresman@yahoo.com>
  293. * becomes: Luke Ehresman
  294. * <lkehresman@yahoo.com>
  295. * becomes: lkehresman@yahoo.com
  296. */
  297. function sqimap_find_displayable_name ($string) {
  298. $string = ' '.trim($string);
  299. $orig_string = $string;
  300. if (strpos($string, '<') && strpos($string, '>')) {
  301. if (strpos($string, '<') == 1) {
  302. $string = sqimap_find_email($string);
  303. } else {
  304. $string = trim($string);
  305. $string = substr($string, 0, strpos($string, '<'));
  306. $string = ereg_replace ('"', '', $string);
  307. }
  308. if (trim($string) == '') {
  309. $string = sqimap_find_email($orig_string);
  310. }
  311. }
  312. return $string;
  313. }
  314. /*
  315. * Returns the number of unseen messages in this folder
  316. */
  317. function sqimap_unseen_messages ($imap_stream, $mailbox) {
  318. //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
  319. fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
  320. $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
  321. ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
  322. return $regs[1];
  323. }
  324. /*
  325. * Saves a message to a given folder -- used for saving sent messages
  326. */
  327. function sqimap_append ($imap_stream, $sent_folder, $length) {
  328. fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
  329. $tmp = fgets ($imap_stream, 1024);
  330. }
  331. function sqimap_append_done ($imap_stream) {
  332. fputs ($imap_stream, "\r\n");
  333. $tmp = fgets ($imap_stream, 1024);
  334. }
  335. ?>