imap_general.php 16 KB

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