imap_general.php 14 KB

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