imap_general.php 15 KB

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