imap_general.php 12 KB

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