imap_general.php 13 KB

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