imap_mailbox.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. /**
  3. * imap_mailbox.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 impliments all functions that manipulate mailboxes
  9. *
  10. * $Id$
  11. */
  12. /*
  13. Defines Special Mail Boxes
  14. */
  15. function isSpecialMailbox( $box ) {
  16. global $trash_folder, $sent_folder, $draft_folder,
  17. $move_to_trash, $move_to_sent, $save_as_draft,
  18. $delimiter, $folder_prefix;
  19. $i = strpos( $sent_folder, $delimiter, strlen( $folder_prefix ) );
  20. $ret = ( (strtolower($box) == 'inbox') ||
  21. ($box == $trash_folder &&
  22. $move_to_trash) ||
  23. (substr( $sent_folder, 0, max( $i, strlen( $box ) ) ) == $box &&
  24. $move_to_sent) ||
  25. ($box == $draft_folder &&
  26. $save_as_draft) );
  27. return( $ret );
  28. }
  29. /*************************
  30. ** Expunges a mailbox **
  31. *************************/
  32. function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
  33. {
  34. $read = sqimap_run_command($imap_stream, 'EXPUNGE',
  35. $handle_errors, $response, $message);
  36. }
  37. /******************************************************************************
  38. ** Checks whether or not the specified mailbox exists
  39. ******************************************************************************/
  40. function sqimap_mailbox_exists ($imap_stream, $mailbox)
  41. {
  42. if (! isset($mailbox)) {
  43. return false;
  44. }
  45. $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
  46. TRUE, $response, $message);
  47. return isset($mbx[0]);
  48. }
  49. /******************************************************************************
  50. ** Selects a mailbox
  51. ******************************************************************************/
  52. function sqimap_mailbox_select ($imap_stream, $mailbox,
  53. $hide=TRUE, $recent=false)
  54. {
  55. global $auto_expunge;
  56. if ( $mailbox == 'None' ) {
  57. return;
  58. }
  59. $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
  60. TRUE, $response, $message);
  61. if ($recent) {
  62. for ($i=0; $i<count($read); $i++) {
  63. if (strpos(strtolower($read[$i]), 'recent')) {
  64. $r = explode(' ', $read[$i]);
  65. }
  66. }
  67. return $r[1];
  68. }
  69. if ($auto_expunge) {
  70. $tmp = sqimap_run_command($imap_stream, "EXPUNGE",
  71. false, $a, $b);
  72. }
  73. }
  74. /******************************************************************************
  75. ** Creates a folder
  76. ******************************************************************************/
  77. function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
  78. {
  79. global $delimiter;
  80. if (strtolower($type) == 'noselect') {
  81. $mailbox = $mailbox.$delimiter;
  82. }
  83. $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
  84. TRUE, $response, $message);
  85. sqimap_subscribe ($imap_stream, $mailbox);
  86. }
  87. /******************************************************************************
  88. ** Subscribes to an existing folder
  89. ******************************************************************************/
  90. function sqimap_subscribe ($imap_stream, $mailbox)
  91. {
  92. $read_ary = sqimap_run_command($imap_stream, " SUBSCRIBE \"$mailbox\"",
  93. TRUE, $response, $message);
  94. }
  95. /******************************************************************************
  96. ** Unsubscribes to an existing folder
  97. ******************************************************************************/
  98. function sqimap_unsubscribe ($imap_stream, $mailbox)
  99. {
  100. global $imap_server_type;
  101. $read_ary = sqimap_run_command($imap_stream, " UNSUBSCRIBE \"$mailbox\"",
  102. TRUE, $response, $message);
  103. }
  104. /******************************************************************************
  105. ** This function simply deletes the given folder
  106. ******************************************************************************/
  107. function sqimap_mailbox_delete ($imap_stream, $mailbox)
  108. {
  109. $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
  110. TRUE, $response, $message);
  111. sqimap_unsubscribe ($imap_stream, $mailbox);
  112. }
  113. /***********************************************************************
  114. ** Determines if the user is subscribed to the folder or not
  115. **********************************************************************/
  116. function sqimap_mailbox_is_subscribed($imap_stream, $folder)
  117. {
  118. $boxes = sqimap_mailbox_list ($imap_stream);
  119. foreach ($boxes as $ref) {
  120. if ($ref['unformatted'] == $folder) {
  121. return TRUE;
  122. }
  123. }
  124. return false;
  125. }
  126. /*
  127. Renames a mailbox
  128. */
  129. function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  130. if ( $old_name <> $new_name ) {
  131. global $delimiter;
  132. if ( substr( $old_name, -1 ) == $delimiter ) {
  133. $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
  134. $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
  135. $postfix = $delimiter;
  136. $boxes = sqimap_mailbox_list($imap_stream);
  137. } else {
  138. $postfix = '';
  139. $boxes = FALSE;
  140. }
  141. $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
  142. $data = sqimap_run_command($imap_stream, $cmd,
  143. TRUE, $response, $message);
  144. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  145. sqimap_subscribe($imap_stream, $new_name.$postfix);
  146. if ( $boxes ) {
  147. // Sub-unsub subfolders
  148. $l = strlen( $old_name ) + 1;
  149. $p = 'unformatted';
  150. foreach ( $boxes as $box ) {
  151. if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
  152. sqimap_unsubscribe($imap_stream, $box[$p]);
  153. sqimap_subscribe($imap_stream,
  154. $new_name . $delimiter . substr( $box[$p], $l ) );
  155. }
  156. }
  157. }
  158. }
  159. }
  160. /******************************************************************************
  161. ** Formats a mailbox into 4 parts for the $boxes array
  162. **
  163. ** The four parts are:
  164. **
  165. ** raw - Raw LIST/LSUB response from the IMAP server
  166. ** formatted - nicely formatted folder name
  167. ** unformatted - unformatted, but with delimiter at end removed
  168. ** unformatted-dm - folder name as it appears in raw response
  169. ** unformatted-disp - unformatted without $folder_prefix
  170. **
  171. ******************************************************************************/
  172. function sqimap_mailbox_parse ($line, $line_lsub)
  173. {
  174. global $folder_prefix, $delimiter;
  175. /* Process each folder line */
  176. for ($g=0; $g < count($line); $g++) {
  177. /* Store the raw IMAP reply */
  178. if (isset($line[$g])) {
  179. $boxes[$g]["raw"] = $line[$g];
  180. }
  181. else {
  182. $boxes[$g]["raw"] = "";
  183. }
  184. /* Count number of delimiters ($delimiter) in folder name */
  185. $mailbox = trim($line_lsub[$g]);
  186. $dm_count = substr_count($mailbox, $delimiter);
  187. if (substr($mailbox, -1) == $delimiter) {
  188. /* If name ends in delimiter - decrement count by one */
  189. $dm_count--;
  190. }
  191. /* Format folder name, but only if it's a INBOX.* or have */
  192. /* a parent. */
  193. $boxesbyname[$mailbox] = $g;
  194. $parentfolder = readMailboxParent($mailbox, $delimiter);
  195. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  196. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  197. ( isset($boxesbyname[$parentfolder]) &&
  198. (strlen($parentfolder) > 0) ) ) {
  199. $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
  200. if ($indent > 0) {
  201. $boxes[$g]['formatted'] = str_repeat("&nbsp;&nbsp;", $indent);
  202. }
  203. else {
  204. $boxes[$g]['formatted'] = '';
  205. }
  206. $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
  207. }
  208. else {
  209. $boxes[$g]['formatted'] = $mailbox;
  210. }
  211. $boxes[$g]['unformatted-dm'] = $mailbox;
  212. if (substr($mailbox, -1) == $delimiter) {
  213. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  214. }
  215. $boxes[$g]['unformatted'] = $mailbox;
  216. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  217. $mailbox = substr($mailbox, strlen($folder_prefix));
  218. }
  219. $boxes[$g]['unformatted-disp'] = $mailbox;
  220. $boxes[$g]['id'] = $g;
  221. $boxes[$g]['flags'] = array();
  222. if (isset($line[$g])) {
  223. ereg("\(([^)]*)\)",$line[$g],$regs);
  224. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  225. if ($flags) {
  226. $boxes[$g]['flags'] = explode(' ', $flags);
  227. }
  228. }
  229. }
  230. return $boxes;
  231. }
  232. /**
  233. * Sorting function used to sort mailbox names.
  234. * + Original patch from dave_michmerhuizen@yahoo.com
  235. * + Allows case insensitivity when sorting folders
  236. * + Takes care of the delimiter being sorted to the end, causing
  237. * subfolders to be listed in below folders that are prefixed
  238. * with their parent folders name.
  239. * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
  240. * Without special sort function: foobar between foo and foo.bar
  241. * With special sort function: foobar AFTER foo and foo.bar :)
  242. */
  243. function user_strcasecmp($a, $b) {
  244. global $delimiter;
  245. /* Calculate the length of some strings. */
  246. $a_length = strlen($a);
  247. $b_length = strlen($b);
  248. $min_length = min($a_length, $b_length);
  249. $delimiter_length = strlen($delimiter);
  250. /* Set the initial result value. */
  251. $result = 0;
  252. /* Check the strings... */
  253. for ($c = 0; $c < $min_length; ++$c) {
  254. $a_del = substr($a, $c, $delimiter_length);
  255. $b_del = substr($b, $c, $delimiter_length);
  256. if (($a_del == $delimiter) && ($b_del == $delimiter)) {
  257. $result = 0;
  258. } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
  259. $result = -1;
  260. } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
  261. $result = 1;
  262. } else {
  263. $result = strcasecmp($a{$c}, $b{$c});
  264. }
  265. if ($result != 0) {
  266. break;
  267. }
  268. }
  269. /* If one string is a prefix of the other... */
  270. if ($result == 0) {
  271. if ($a_length < $b_length) {
  272. $result = -1;
  273. } else if ($a_length > $b_length) {
  274. $result = 1;
  275. }
  276. }
  277. return ($result);
  278. }
  279. /******************************************************************************
  280. ** Returns sorted mailbox lists in several different ways.
  281. ** See comment on sqimap_mailbox_parse() for info about the returned array.
  282. ******************************************************************************/
  283. function sqimap_mailbox_list ($imap_stream) {
  284. global $data_dir, $username, $list_special_folders_first,
  285. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  286. $move_to_trash, $move_to_sent, $save_as_draft,
  287. $delimiter;
  288. $inbox_in_list = false;
  289. $inbox_subscribed = false;
  290. require_once('../src/load_prefs.php');
  291. require_once('../functions/array.php');
  292. /** LSUB array **/
  293. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  294. TRUE, $response, $message);
  295. /* Section about removing the last element was removed */
  296. /* We don't return "* OK" anymore from sqimap_read_data */
  297. $sorted_lsub_ary = array();
  298. for ($i=0;$i < count($lsub_ary); $i++) {
  299. /* Workaround for EIMS */
  300. /* Doesn't work if the mailbox name is multiple lines */
  301. if (isset($lsub_ary[$i + 1]) &&
  302. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  303. $lsub_ary[$i], $regs)) {
  304. $i ++;
  305. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
  306. '"' . $regs[2];
  307. }
  308. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  309. $sorted_lsub_ary[] = $temp_mailbox_name;
  310. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  311. $inbox_subscribed = TRUE;
  312. }
  313. }
  314. $new_ary = array();
  315. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  316. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  317. $new_ary[] = $sorted_lsub_ary[$i];
  318. }
  319. }
  320. $sorted_lsub_ary = $new_ary;
  321. if (isset($sorted_lsub_ary)) {
  322. usort($sorted_lsub_ary, 'user_strcasecmp');
  323. }
  324. /** LIST array **/
  325. $sorted_list_ary = array();
  326. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  327. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  328. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  329. }
  330. else {
  331. $mbx = $sorted_lsub_ary[$i];
  332. }
  333. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  334. TRUE, $response, $message);
  335. /* Another workaround for EIMS */
  336. if (isset($read[1]) &&
  337. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  338. $read[0], $regs)) {
  339. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
  340. '"' . $regs[2];
  341. }
  342. if (isset($sorted_list_ary[$i])) {
  343. $sorted_list_ary[$i] = '';
  344. }
  345. if (isset($read[0])) {
  346. $sorted_list_ary[$i] = $read[0];
  347. }
  348. else {
  349. $sorted_list_ary[$i] = '';
  350. }
  351. if (isset($sorted_list_ary[$i]) &&
  352. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
  353. $inbox_in_list = TRUE;
  354. }
  355. }
  356. /**
  357. * Just in case they're not subscribed to their inbox,
  358. * we'll get it for them anyway
  359. */
  360. if ($inbox_subscribed == false || $inbox_in_list == false) {
  361. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  362. TRUE, $response, $message);
  363. /* Another workaround for EIMS */
  364. if (isset($inbox_ary[1]) &&
  365. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  366. $inbox_ary[0], $regs)) {
  367. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  368. '"' . $regs[2];
  369. }
  370. $sorted_list_ary[] = $inbox_ary[0];
  371. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  372. }
  373. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  374. /** Now, lets sort for special folders **/
  375. $boxesnew = $used = array();
  376. /* Find INBOX */
  377. foreach ( $boxes as $k => $box ) {
  378. if ( strtolower($box['unformatted']) == 'inbox') {
  379. $boxesnew[] = $box;
  380. $used[$k] = TRUE;
  381. } else {
  382. $used[$k] = FALSE;
  383. }
  384. }
  385. /* List special folders and their subfolders, if requested. */
  386. if ($list_special_folders_first == TRUE) {
  387. foreach ( $boxes as $k => $box ) {
  388. if ( !$used[$k] &&
  389. isSpecialMailbox( $box['unformatted'] ) ) {
  390. $boxesnew[] = $box;
  391. $used[$k] = TRUE;
  392. }
  393. }
  394. }
  395. /* Rest of the folders */
  396. foreach ( $boxes as $k => $box ) {
  397. if ( !$used[$k] ) {
  398. $boxesnew[] = $box;
  399. }
  400. }
  401. return( $boxesnew );
  402. }
  403. /*
  404. * Returns a list of all folders, subscribed or not
  405. */
  406. function sqimap_mailbox_list_all ($imap_stream)
  407. {
  408. global $list_special_folders_first, $folder_prefix;
  409. global $delimiter;
  410. if (!function_exists('ary_sort')) {
  411. include_once('../functions/array.php');
  412. }
  413. $ssid = sqimap_session_id();
  414. $lsid = strlen( $ssid );
  415. fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
  416. $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
  417. $g = 0;
  418. $phase = 'inbox';
  419. for ($i = 0; $i < count($read_ary); $i++) {
  420. /* Another workaround for EIMS */
  421. if (isset($read_ary[$i + 1]) &&
  422. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  423. $read_ary[$i], $regs)) {
  424. $i ++;
  425. $read_ary[$i] = $regs[1] . '"' .
  426. addslashes(trim($read_ary[$i])) .
  427. '"' . $regs[2];
  428. }
  429. if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
  430. /* Store the raw IMAP reply */
  431. $boxes[$g]["raw"] = $read_ary[$i];
  432. /* Count number of delimiters ($delimiter) in folder name */
  433. $mailbox = find_mailbox_name($read_ary[$i]);
  434. $dm_count = substr_count($mailbox, $delimiter);
  435. if (substr($mailbox, -1) == $delimiter) {
  436. /* If name ends in delimiter - decrement count by one */
  437. $dm_count--;
  438. }
  439. /* Format folder name, but only if it's a INBOX.* or have */
  440. /* a parent. */
  441. $boxesbyname[$mailbox] = $g;
  442. $parentfolder = readMailboxParent($mailbox, $delimiter);
  443. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  444. (ereg('^'.$folder_prefix, $mailbox)) ||
  445. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  446. if ($dm_count) {
  447. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  448. }
  449. else {
  450. $boxes[$g]['formatted'] = '';
  451. }
  452. $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
  453. }
  454. else {
  455. $boxes[$g]['formatted'] = $mailbox;
  456. }
  457. $boxes[$g]["unformatted-dm"] = $mailbox;
  458. if (substr($mailbox, -1) == $delimiter) {
  459. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  460. }
  461. $boxes[$g]['unformatted'] = $mailbox;
  462. $boxes[$g]['unformatted-disp'] =
  463. ereg_replace('^' . $folder_prefix, '', $mailbox);
  464. $boxes[$g]['id'] = $g;
  465. /** Now lets get the flags for this mailbox **/
  466. $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
  467. TRUE, $response, $message);
  468. /* Another workaround for EIMS */
  469. if (isset($read_mlbx[1]) &&
  470. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  471. $read_mlbx[0], $regs)) {
  472. $read_mlbx[0] = $regs[1] . '"' .
  473. addslashes(trim($read_mlbx[1])) .
  474. '"' . $regs[2];
  475. }
  476. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
  477. $flags = substr($flags, 0, strpos($flags, ')'));
  478. $flags = str_replace('\\', '', $flags);
  479. $flags = trim(strtolower($flags));
  480. if ($flags) {
  481. $boxes[$g]['flags'] = explode(' ', $flags);
  482. } else {
  483. $boxes[$g]['flags'] = array();
  484. }
  485. }
  486. $g++;
  487. }
  488. if(is_array($boxes)) {
  489. $boxes = ary_sort ($boxes, 'unformatted', 1);
  490. }
  491. return $boxes;
  492. }
  493. ?>