imap_general.php 13 KB

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