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