imap_general.php 15 KB

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