imap_general.php 12 KB

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