imap_general.php 12 KB

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