imap_general.php 13 KB

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