imap_general.php 15 KB

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