imap_general.php 12 KB

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