imap_general.php 12 KB

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