imap_general.php 15 KB

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