imap_general.php 13 KB

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