imap_general.php 14 KB

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