imap_general.php 13 KB

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