imap_general.php 16 KB

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