imap_general.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. if( !$imap_stream ) {
  147. return FALSE;
  148. }
  149. $server_info = fgets ($imap_stream, 1024);
  150. // Decrypt the password
  151. $password = OneTimePadDecrypt($password, $onetimepad);
  152. /** Do some error correction **/
  153. if (!$imap_stream) {
  154. if (!$hide) {
  155. set_up_language($squirrelmail_language, true);
  156. printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
  157. echo "$error_number : $error_string<br>\r\n";
  158. }
  159. exit;
  160. }
  161. fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
  162. '" "' . quoteIMAP($password) . "\"\r\n");
  163. $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
  164. /** If the connection was not successful, lets see why **/
  165. if ($response != "OK") {
  166. if (!$hide) {
  167. if ($response != 'NO') {
  168. // "BAD" and anything else gets reported here.
  169. set_up_language($squirrelmail_language, true);
  170. if ($response == 'BAD')
  171. printf (_("Bad request: %s")."<br>\r\n", $message);
  172. else
  173. printf (_("Unknown error: %s") . "<br>\n", $message);
  174. echo '<br>';
  175. echo _("Read data:") . "<br>\n";
  176. if (is_array($read))
  177. {
  178. foreach ($read as $line)
  179. {
  180. echo htmlspecialchars($line) . "<br>\n";
  181. }
  182. }
  183. exit;
  184. } else {
  185. // If the user does not log in with the correct
  186. // username and password it is not possible to get the
  187. // correct locale from the user's preferences.
  188. // Therefore, apply the same hack as on the login
  189. // screen.
  190. // $squirrelmail_language is set by a cookie when
  191. // the user selects language and logs out
  192. set_up_language($squirrelmail_language, true);
  193. ?>
  194. <html>
  195. <body bgcolor="ffffff">
  196. <br>
  197. <center>
  198. <table width="70%" noborder bgcolor="ffffff" align="center">
  199. <tr>
  200. <td bgcolor="dcdcdc">
  201. <font color="cc0000">
  202. <center>
  203. <?php echo _("ERROR") ?>
  204. </center>
  205. </font>
  206. </td>
  207. </tr>
  208. <tr>
  209. <td>
  210. <center>
  211. <?php echo _("Unknown user or password incorrect.") ?><br>
  212. <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
  213. </center>
  214. </td>
  215. </tr>
  216. </table>
  217. </center>
  218. </body>
  219. </html>
  220. <?php
  221. session_destroy();
  222. exit;
  223. }
  224. } else {
  225. exit;
  226. }
  227. }
  228. return $imap_stream;
  229. }
  230. /******************************************************************************
  231. ** Simply logs out the imap session
  232. ******************************************************************************/
  233. function sqimap_logout ($imap_stream) {
  234. fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
  235. }
  236. function sqimap_capability($imap_stream, $capability) {
  237. global $sqimap_capabilities;
  238. if (!is_array($sqimap_capabilities)) {
  239. fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
  240. $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
  241. $c = explode(' ', $read[0]);
  242. for ($i=2; $i < count($c); $i++) {
  243. $cap_list = explode('=', $c[$i]);
  244. if (isset($cap_list[1]))
  245. $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
  246. else
  247. $sqimap_capabilities[$cap_list[0]] = TRUE;
  248. }
  249. }
  250. if (! isset($sqimap_capabilities[$capability]))
  251. return false;
  252. return $sqimap_capabilities[$capability];
  253. }
  254. /******************************************************************************
  255. ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
  256. ******************************************************************************/
  257. function sqimap_get_delimiter ($imap_stream = false) {
  258. global $sqimap_delimiter;
  259. global $optional_delimiter;
  260. /* Use configured delimiter if set */
  261. if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
  262. return $optional_delimiter;
  263. /* Do some caching here */
  264. if (!$sqimap_delimiter) {
  265. if (sqimap_capability($imap_stream, "NAMESPACE")) {
  266. /* According to something that I can't find, this is supposed to work on all systems
  267. OS: This won't work in Courier IMAP.
  268. OS: According to rfc2342 response from NAMESPACE command is:
  269. OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
  270. OS: We want to lookup all personal NAMESPACES...
  271. */
  272. fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
  273. $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
  274. if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
  275. if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
  276. $pn = $data2[1];
  277. $pna = explode(')(', $pn);
  278. while (list($k, $v) = each($pna))
  279. {
  280. $lst = explode('"', $v);
  281. if (isset($lst[3])) {
  282. $pn[$lst[1]] = $lst[3];
  283. } else {
  284. $pn[$lst[1]] = '';
  285. }
  286. }
  287. }
  288. $sqimap_delimiter = $pn[0];
  289. } else {
  290. fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
  291. $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
  292. $quote_position = strpos ($read[0], '"');
  293. $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
  294. }
  295. }
  296. return $sqimap_delimiter;
  297. }
  298. /******************************************************************************
  299. ** Gets the number of messages in the current mailbox.
  300. ******************************************************************************/
  301. function sqimap_get_num_messages ($imap_stream, $mailbox) {
  302. fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
  303. $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
  304. for ($i = 0; $i < count($read_ary); $i++) {
  305. if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
  306. return $regs[1];
  307. }
  308. }
  309. return "BUG! Couldn't get number of messages in $mailbox!";
  310. }
  311. /******************************************************************************
  312. ** Returns a displayable email address
  313. ******************************************************************************/
  314. function sqimap_find_email ($string) {
  315. /** Luke Ehresman <lehresma@css.tayloru.edu>
  316. ** <lehresma@css.tayloru.edu>
  317. ** lehresma@css.tayloru.edu
  318. **
  319. ** What about
  320. ** lehresma@css.tayloru.edu (Luke Ehresman)
  321. **/
  322. if (ereg("<([^>]+)>", $string, $regs)) {
  323. $string = $regs[1];
  324. }
  325. return trim($string);
  326. }
  327. /******************************************************************************
  328. ** Takes the From: field, and creates a displayable name.
  329. ** Luke Ehresman <lkehresman@yahoo.com>
  330. ** becomes: Luke Ehresman
  331. ** <lkehresman@yahoo.com>
  332. ** becomes: lkehresman@yahoo.com
  333. ******************************************************************************/
  334. function sqimap_find_displayable_name ($string) {
  335. $string = ' '.trim($string);
  336. $orig_string = $string;
  337. if (strpos($string, '<') && strpos($string, '>')) {
  338. if (strpos($string, '<') == 1) {
  339. $string = sqimap_find_email($string);
  340. } else {
  341. $string = trim($string);
  342. $string = substr($string, 0, strpos($string, '<'));
  343. $string = ereg_replace ('"', '', $string);
  344. }
  345. if (trim($string) == '') {
  346. $string = sqimap_find_email($orig_string);
  347. }
  348. }
  349. return $string;
  350. }
  351. /******************************************************************************
  352. ** Returns the number of unseen messages in this folder
  353. ******************************************************************************/
  354. function sqimap_unseen_messages ($imap_stream, $mailbox) {
  355. //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
  356. fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
  357. $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
  358. ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
  359. return $regs[1];
  360. }
  361. /******************************************************************************
  362. ** Saves a message to a given folder -- used for saving sent messages
  363. ******************************************************************************/
  364. function sqimap_append ($imap_stream, $sent_folder, $length) {
  365. fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
  366. $tmp = fgets ($imap_stream, 1024);
  367. }
  368. function sqimap_append_done ($imap_stream) {
  369. fputs ($imap_stream, "\r\n");
  370. $tmp = fgets ($imap_stream, 1024);
  371. }
  372. ?>