imap_mailbox.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. require_once('../functions/imap_utf7_encode_local.php');
  13. require_once('../functions/imap_utf7_decode_local.php');
  14. global $boxesnew;
  15. class mailboxes {
  16. var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
  17. $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
  18. $is_trash = false, $is_draft = false, $mbxs = array(),
  19. $unseen = false, $total = false;
  20. function addMbx($mbx, $delimiter, $start, $specialfirst) {
  21. $ary = explode($delimiter, $mbx->mailboxname_full);
  22. $mbx_parent = &$this;
  23. for ($i=$start; $i < (count($ary) -1); $i++) {
  24. $mbx_childs = &$mbx_parent->mbxs;
  25. $found = false;
  26. foreach ($mbx_childs as $key => $parent) {
  27. if ($parent->mailboxname_sub == $ary[$i]) {
  28. $mbx_parent = &$mbx_parent->mbxs[$key];
  29. $found = true;
  30. }
  31. }
  32. if (!$found) {
  33. $no_select_mbx = new mailboxes();
  34. if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
  35. $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
  36. } else {
  37. $no_select_mbx->mailboxname_full = $ary[$i];
  38. }
  39. $no_select_mbx->mailboxname_sub = $ary[$i];
  40. $no_select_mbx->is_noselect = true;
  41. $mbx_parent->mbxs[] = $no_select_mbx;
  42. $i--;
  43. }
  44. }
  45. $mbx_parent->mbxs[] = $mbx;
  46. if ($mbx->is_special && $specialfirst) {
  47. usort($mbx_parent->mbxs, 'sortSpecialMbx');
  48. }
  49. }
  50. }
  51. function sortSpecialMbx($a, $b) {
  52. if ($a->is_inbox) {
  53. $acmp = '0'. $a->mailboxname_full;
  54. } else if ($a->is_special) {
  55. $acmp = '1'. $a->mailboxname_full;
  56. } else {
  57. $acmp = '2' . $a->mailboxname_full;
  58. }
  59. if ($b->is_inbox) {
  60. $bcmp = '0'. $b->mailboxname_full;
  61. }else if ($b->is_special) {
  62. $bcmp = '1' . $b->mailboxname_full;
  63. } else {
  64. $bcmp = '2' . $b->mailboxname_full;
  65. }
  66. if ($acmp == $bcmp) return 0;
  67. return ($acmp>$bcmp) ? 1: -1;
  68. }
  69. function find_mailbox_name ($mailbox) {
  70. if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
  71. return $regs[1];
  72. ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
  73. return $regs[1];
  74. }
  75. /**
  76. * If $haystack is a full mailbox name, and $needle is the mailbox
  77. * separator character, returns the second last part of the full
  78. * mailbox name (i.e. the mailbox's parent mailbox)
  79. */
  80. function readMailboxParent($haystack, $needle) {
  81. if ($needle == '') {
  82. $ret = '';
  83. } else {
  84. $parts = explode($needle, $haystack);
  85. $elem = array_pop($parts);
  86. while ($elem == '' && count($parts)) {
  87. $elem = array_pop($parts);
  88. }
  89. $ret = join($needle, $parts);
  90. }
  91. return( $ret );
  92. }
  93. function isBoxBelow( $box2, $box1 ) {
  94. global $delimiter, $folder_prefix, $imap_server_type;
  95. if ( $imap_server_type == 'uw' ) {
  96. $boxs = $box2;
  97. $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
  98. if ( $i === false ) {
  99. $i = strlen( $box2 );
  100. }
  101. } else {
  102. $boxs = $box2 . $delimiter;
  103. /* Skip next second delimiter */
  104. $i = strpos( $box1, $delimiter );
  105. $i = strpos( $box1, $delimiter, $i + 1 );
  106. if ( $i === false ) {
  107. $i = strlen( $box2 );
  108. } else {
  109. $i++;
  110. }
  111. }
  112. return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
  113. }
  114. /* Defines special mailboxes */
  115. function isSpecialMailbox( $box ) {
  116. global $trash_folder, $sent_folder, $draft_folder,
  117. $move_to_trash, $move_to_sent, $save_as_draft;
  118. $ret = ( (strtolower($box) == 'inbox') ||
  119. ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
  120. ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
  121. ($save_as_draft && $box == $draft_folder ) );
  122. if ( !$ret ) {
  123. $ret = do_hook_function( 'special_mailbox', $box );
  124. }
  125. return $ret;
  126. }
  127. /* Expunges a mailbox */
  128. function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true) {
  129. $read = sqimap_run_command($imap_stream, 'EXPUNGE', $handle_errors,
  130. $response, $message);
  131. }
  132. /* Checks whether or not the specified mailbox exists */
  133. function sqimap_mailbox_exists ($imap_stream, $mailbox) {
  134. if (! isset($mailbox)) {
  135. return false;
  136. }
  137. $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
  138. true, $response, $message);
  139. return isset($mbx[0]);
  140. }
  141. /* Selects a mailbox */
  142. function sqimap_mailbox_select ($imap_stream, $mailbox,
  143. $hide = true, $recent = false, $extrainfo = false) {
  144. global $auto_expunge;
  145. if ( $mailbox == 'None' ) {
  146. return;
  147. }
  148. $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
  149. true, $response, $message);
  150. if ($recent) {
  151. for ($i=0; $i<count($read); $i++) {
  152. if (strpos(strtolower($read[$i]), 'recent')) {
  153. $r = explode(' ', $read[$i]);
  154. }
  155. }
  156. return $r[1];
  157. } else {
  158. if ($auto_expunge) {
  159. $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
  160. }
  161. if (isset( $extrainfo ) && $extrainfo) {
  162. $result = array();
  163. for ($i=0; $i<count($read); $i++) {
  164. if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
  165. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
  166. $result['PERMANENTFLAGS'] = $regs[1];
  167. }
  168. else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
  169. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
  170. $result['FLAGS'] = $regs[1];
  171. }
  172. else if (preg_match("/(.*)EXISTS/i",$read[$i], $regs)) {
  173. $result['EXISTS']=trim($regs[1]);
  174. }
  175. else if (preg_match("/(.*)RECENT/i",$read[$i], $regs)) {
  176. $result['RECENT']=trim($regs[1]);
  177. }
  178. else if (preg_match("/\[UNSEEN(.*)\]/i",$read[$i], $regs)) {
  179. $result['UNSEEN']=trim($regs[1]);
  180. }
  181. }
  182. return( $result );
  183. }
  184. }
  185. }
  186. /* Creates a folder */
  187. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  188. global $delimiter;
  189. if (strtolower($type) == 'noselect') {
  190. $mailbox .= $delimiter;
  191. }
  192. $mailbox = imap_utf7_encode_local($mailbox);
  193. $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
  194. true, $response, $message);
  195. sqimap_subscribe ($imap_stream, $mailbox);
  196. }
  197. /* Subscribes to an existing folder */
  198. function sqimap_subscribe ($imap_stream, $mailbox) {
  199. $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
  200. true, $response, $message);
  201. }
  202. /* Unsubscribes to an existing folder */
  203. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  204. global $imap_server_type;
  205. $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
  206. true, $response, $message);
  207. }
  208. /* Deletes the given folder */
  209. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  210. global $data_dir, $username;
  211. $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
  212. true, $response, $message);
  213. sqimap_unsubscribe ($imap_stream, $mailbox);
  214. do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
  215. removePref($data_dir, $username, "thread_$mailbox");
  216. }
  217. /* Determines if the user is subscribed to the folder or not */
  218. function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
  219. $boxesall = sqimap_mailbox_list ($imap_stream);
  220. foreach ($boxesall as $ref) {
  221. if ($ref['unformatted'] == $folder) {
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. /* Renames a mailbox */
  228. function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  229. if ( $old_name != $new_name ) {
  230. global $delimiter, $imap_server_type, $data_dir, $username;
  231. if ( substr( $old_name, -1 ) == $delimiter ) {
  232. $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
  233. $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
  234. $postfix = $delimiter;
  235. } else {
  236. $postfix = '';
  237. }
  238. $boxesall = sqimap_mailbox_list($imap_stream);
  239. $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
  240. $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
  241. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  242. $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix);
  243. removePref($data_dir, $username, "thread_".$old_name.$postfix);
  244. sqimap_subscribe($imap_stream, $new_name.$postfix);
  245. setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
  246. do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
  247. $l = strlen( $old_name ) + 1;
  248. $p = 'unformatted';
  249. foreach ( $boxesall as $box ) {
  250. if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
  251. $new_sub = $new_name . $delimiter . substr($box[$p], $l);
  252. if ($imap_server_type == 'cyrus') {
  253. $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
  254. $data = sqimap_run_command($imap_stream, $cmd, true,
  255. $response, $message);
  256. }
  257. sqimap_unsubscribe($imap_stream, $box[$p]);
  258. $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
  259. removePref($data_dir, $username, "thread_".$box[$p]);
  260. sqimap_subscribe($imap_stream, $new_sub);
  261. setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
  262. do_hook_function("rename_or_delete_folder",
  263. $args = array($box[$p], 'rename', $new_sub));
  264. }
  265. }
  266. }
  267. }
  268. /*
  269. * Formats a mailbox into 4 parts for the $boxesall array
  270. *
  271. * The four parts are:
  272. *
  273. * raw - Raw LIST/LSUB response from the IMAP server
  274. * formatted - nicely formatted folder name
  275. * unformatted - unformatted, but with delimiter at end removed
  276. * unformatted-dm - folder name as it appears in raw response
  277. * unformatted-disp - unformatted without $folder_prefix
  278. */
  279. function sqimap_mailbox_parse ($line, $line_lsub) {
  280. global $folder_prefix, $delimiter;
  281. /* Process each folder line */
  282. for ($g=0; $g < count($line); $g++) {
  283. /* Store the raw IMAP reply */
  284. if (isset($line[$g])) {
  285. $boxesall[$g]["raw"] = $line[$g];
  286. }
  287. else {
  288. $boxesall[$g]["raw"] = '';
  289. }
  290. /* Count number of delimiters ($delimiter) in folder name */
  291. $mailbox = trim($line_lsub[$g]);
  292. $dm_count = substr_count($mailbox, $delimiter);
  293. if (substr($mailbox, -1) == $delimiter) {
  294. /* If name ends in delimiter, decrement count by one */
  295. $dm_count--;
  296. }
  297. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  298. $boxesallbyname[$mailbox] = $g;
  299. $parentfolder = readMailboxParent($mailbox, $delimiter);
  300. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  301. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  302. ( isset($boxesallbyname[$parentfolder]) &&
  303. (strlen($parentfolder) > 0) ) ) {
  304. $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
  305. if ($indent > 0) {
  306. $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
  307. }
  308. else {
  309. $boxesall[$g]['formatted'] = '';
  310. }
  311. $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  312. }
  313. else {
  314. $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  315. }
  316. $boxesall[$g]['unformatted-dm'] = $mailbox;
  317. if (substr($mailbox, -1) == $delimiter) {
  318. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  319. }
  320. $boxesall[$g]['unformatted'] = $mailbox;
  321. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  322. $mailbox = substr($mailbox, strlen($folder_prefix));
  323. }
  324. $boxesall[$g]['unformatted-disp'] = $mailbox;
  325. $boxesall[$g]['id'] = $g;
  326. $boxesall[$g]['flags'] = array();
  327. if (isset($line[$g])) {
  328. ereg("\(([^)]*)\)",$line[$g],$regs);
  329. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  330. if ($flags) {
  331. $boxesall[$g]['flags'] = explode(' ', $flags);
  332. }
  333. }
  334. }
  335. return $boxesall;
  336. }
  337. /*
  338. * Sorting function used to sort mailbox names.
  339. * + Original patch from dave_michmerhuizen@yahoo.com
  340. * + Allows case insensitivity when sorting folders
  341. * + Takes care of the delimiter being sorted to the end, causing
  342. * subfolders to be listed in below folders that are prefixed
  343. * with their parent folders name.
  344. *
  345. * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
  346. * Without special sort function: foobar between foo and foo.bar
  347. * With special sort function: foobar AFTER foo and foo.bar :)
  348. */
  349. function user_strcasecmp($a, $b) {
  350. global $delimiter;
  351. /* Calculate the length of some strings. */
  352. $a_length = strlen($a);
  353. $b_length = strlen($b);
  354. $min_length = min($a_length, $b_length);
  355. $delimiter_length = strlen($delimiter);
  356. /* Set the initial result value. */
  357. $result = 0;
  358. /* Check the strings... */
  359. for ($c = 0; $c < $min_length; ++$c) {
  360. $a_del = substr($a, $c, $delimiter_length);
  361. $b_del = substr($b, $c, $delimiter_length);
  362. if (($a_del == $delimiter) && ($b_del == $delimiter)) {
  363. $result = 0;
  364. } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
  365. $result = -1;
  366. } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
  367. $result = 1;
  368. } else {
  369. $result = strcasecmp($a{$c}, $b{$c});
  370. }
  371. if ($result != 0) {
  372. break;
  373. }
  374. }
  375. /* If one string is a prefix of the other... */
  376. if ($result == 0) {
  377. if ($a_length < $b_length) {
  378. $result = -1;
  379. } else if ($a_length > $b_length) {
  380. $result = 1;
  381. }
  382. }
  383. return $result;
  384. }
  385. /*
  386. * Returns sorted mailbox lists in several different ways.
  387. * See comment on sqimap_mailbox_parse() for info about the returned array.
  388. */
  389. function sqimap_mailbox_list($imap_stream) {
  390. global $boxesnew, $default_folder_prefix;
  391. if ( !isset( $boxesnew ) ) {
  392. global $data_dir, $username, $list_special_folders_first,
  393. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  394. $move_to_trash, $move_to_sent, $save_as_draft,
  395. $delimiter;
  396. $inbox_in_list = false;
  397. $inbox_subscribed = false;
  398. require_once('../src/load_prefs.php');
  399. require_once('../functions/array.php');
  400. /* LSUB array */
  401. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*%\"",
  402. true, $response, $message);
  403. /*
  404. * Section about removing the last element was removed
  405. * We don't return "* OK" anymore from sqimap_read_data
  406. */
  407. $sorted_lsub_ary = array();
  408. for ($i=0;$i < count($lsub_ary); $i++) {
  409. /*
  410. * Workaround for EIMS
  411. * Doesn't work if the mailbox name is multiple lines
  412. */
  413. if (isset($lsub_ary[$i + 1]) &&
  414. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  415. $lsub_ary[$i], $regs)) {
  416. $i ++;
  417. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  418. }
  419. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  420. $sorted_lsub_ary[] = $temp_mailbox_name;
  421. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  422. $inbox_subscribed = true;
  423. }
  424. }
  425. $new_ary = array();
  426. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  427. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  428. $new_ary[] = $sorted_lsub_ary[$i];
  429. }
  430. }
  431. $sorted_lsub_ary = $new_ary;
  432. if (isset($sorted_lsub_ary)) {
  433. usort($sorted_lsub_ary, 'user_strcasecmp');
  434. }
  435. /* LIST array */
  436. $sorted_list_ary = array();
  437. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  438. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  439. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  440. }
  441. else {
  442. $mbx = $sorted_lsub_ary[$i];
  443. }
  444. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  445. true, $response, $message);
  446. /* Another workaround for EIMS */
  447. if (isset($read[1]) &&
  448. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  449. $read[0], $regs)) {
  450. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
  451. }
  452. if (isset($sorted_list_ary[$i])) {
  453. $sorted_list_ary[$i] = '';
  454. }
  455. if (isset($read[0])) {
  456. $sorted_list_ary[$i] = $read[0];
  457. }
  458. else {
  459. $sorted_list_ary[$i] = '';
  460. }
  461. if (isset($sorted_list_ary[$i]) &&
  462. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
  463. $inbox_in_list = true;
  464. }
  465. }
  466. /*
  467. * Just in case they're not subscribed to their inbox,
  468. * we'll get it for them anyway
  469. */
  470. if (!$inbox_subscribed || !$inbox_in_list) {
  471. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  472. true, $response, $message);
  473. /* Another workaround for EIMS */
  474. if (isset($inbox_ary[1]) &&
  475. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  476. $inbox_ary[0], $regs)) {
  477. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  478. '"' . $regs[2];
  479. }
  480. $sorted_list_ary[] = $inbox_ary[0];
  481. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  482. }
  483. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  484. /* Now, lets sort for special folders */
  485. $boxesnew = $used = array();
  486. /* Find INBOX */
  487. foreach ( $boxesall as $k => $box ) {
  488. if ( strtolower($box['unformatted']) == 'inbox') {
  489. $boxesnew[] = $box;
  490. $used[$k] = true;
  491. } else {
  492. $used[$k] = false;
  493. }
  494. }
  495. /* List special folders and their subfolders, if requested. */
  496. if ($list_special_folders_first) {
  497. foreach ( $boxesall as $k => $box ) {
  498. if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
  499. $boxesnew[] = $box;
  500. $used[$k] = true;
  501. }
  502. $spec_sub = str_replace('&nbsp;', '', $box['formatted']);
  503. $spec_sub = str_replace('*', '', $box['formatted']);
  504. /* In case of problems with preg
  505. here is a ereg version
  506. if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
  507. if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
  508. $boxesnew[] = $box;
  509. $used[$k] = true;
  510. }
  511. }
  512. }
  513. /* Rest of the folders */
  514. foreach ( $boxesall as $k => $box ) {
  515. if ( !$used[$k] ) {
  516. $boxesnew[] = $box;
  517. }
  518. }
  519. }
  520. return $boxesnew;
  521. }
  522. /*
  523. * Returns a list of all folders, subscribed or not
  524. */
  525. function sqimap_mailbox_list_all($imap_stream) {
  526. global $list_special_folders_first, $folder_prefix, $delimiter;
  527. require_once('../functions/array.php');
  528. $ssid = sqimap_session_id();
  529. $lsid = strlen( $ssid );
  530. fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
  531. $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
  532. $g = 0;
  533. $phase = 'inbox';
  534. for ($i = 0; $i < count($read_ary); $i++) {
  535. /* Another workaround for EIMS */
  536. if (isset($read_ary[$i + 1]) &&
  537. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  538. $read_ary[$i], $regs)) {
  539. $i ++;
  540. $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
  541. }
  542. if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
  543. /* Store the raw IMAP reply */
  544. $boxes[$g]['raw'] = $read_ary[$i];
  545. /* Count number of delimiters ($delimiter) in folder name */
  546. $mailbox = find_mailbox_name($read_ary[$i]);
  547. $dm_count = substr_count($mailbox, $delimiter);
  548. if (substr($mailbox, -1) == $delimiter) {
  549. /* If name ends in delimiter - decrement count by one */
  550. $dm_count--;
  551. }
  552. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  553. $boxesallbyname[$mailbox] = $g;
  554. $parentfolder = readMailboxParent($mailbox, $delimiter);
  555. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  556. (ereg('^'.$folder_prefix, $mailbox)) ||
  557. ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  558. if ($dm_count) {
  559. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  560. }
  561. else {
  562. $boxes[$g]['formatted'] = '';
  563. }
  564. $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  565. }
  566. else {
  567. $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  568. }
  569. $boxes[$g]['unformatted-dm'] = $mailbox;
  570. if (substr($mailbox, -1) == $delimiter) {
  571. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  572. }
  573. $boxes[$g]['unformatted'] = $mailbox;
  574. $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
  575. $boxes[$g]['id'] = $g;
  576. /* Now lets get the flags for this mailbox */
  577. $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
  578. true, $response, $message);
  579. /* Another workaround for EIMS */
  580. if (isset($read_mlbx[1]) &&
  581. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
  582. $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
  583. }
  584. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
  585. $flags = substr($flags, 0, strpos($flags, ')'));
  586. $flags = str_replace('\\', '', $flags);
  587. $flags = trim(strtolower($flags));
  588. if ($flags) {
  589. $boxes[$g]['flags'] = explode(' ', $flags);
  590. } else {
  591. $boxes[$g]['flags'] = array();
  592. }
  593. }
  594. $g++;
  595. }
  596. if(is_array($boxes)) {
  597. $boxes = ary_sort ($boxes, 'unformatted', 1);
  598. }
  599. return $boxes;
  600. }
  601. function sqimap_mailbox_tree($imap_stream) {
  602. global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
  603. if ( !isset( $boxesnew ) ) {
  604. global $data_dir, $username, $list_special_folders_first,
  605. $folder_prefix, $delimiter;
  606. $inbox_in_list = false;
  607. $inbox_subscribed = false;
  608. require_once('../src/load_prefs.php');
  609. require_once('../functions/array.php');
  610. /* LSUB array */
  611. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  612. true, $response, $message);
  613. /*
  614. * Section about removing the last element was removed
  615. * We don't return "* OK" anymore from sqimap_read_data
  616. */
  617. $sorted_lsub_ary = array();
  618. for ($i=0;$i < count($lsub_ary); $i++) {
  619. /*
  620. * Workaround for EIMS
  621. * Doesn't work if the mailbox name is multiple lines
  622. */
  623. if (isset($lsub_ary[$i + 1]) &&
  624. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  625. $lsub_ary[$i], $regs)) {
  626. $i ++;
  627. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  628. }
  629. // if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
  630. // $flag = $regs[1];
  631. // $mbx = trim($regs[3]);
  632. // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  633. // }
  634. $mbx = find_mailbox_name($lsub_ary[$i]);
  635. if (substr($mbx, -1) == $delimiter) {
  636. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  637. }
  638. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
  639. }
  640. array_multisort($sorted_lsub_ary, SORT_ASC, SORT_STRING);
  641. foreach ($sorted_lsub_ary as $mbx) {
  642. if ($mbx['mbx'] == 'INBOX') {
  643. $inbox_in_list = true;
  644. break;
  645. }
  646. }
  647. /*
  648. * Just in case they're not subscribed to their inbox,
  649. * we'll get it for them anyway
  650. */
  651. if (!$inbox_in_list) {
  652. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  653. true, $response, $message);
  654. /* Another workaround for EIMS */
  655. if (isset($inbox_ary[1]) &&
  656. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  657. $inbox_ary[0], $regs)) {
  658. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  659. '"' . $regs[2];
  660. }
  661. $mbx = find_mailbox_name($inbox_ary[0]);
  662. if (substr($mbx, -1) == $delimiter) {
  663. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  664. }
  665. if ( $mbx == 'INBOX') {
  666. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
  667. sqimap_subscribe($imap_stream, 'INBOX');
  668. }
  669. // if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
  670. // $flag = $regs[1];
  671. // $mbx = trim($regs[3]);
  672. // if (substr($mbx, -1) == $delimiter) {
  673. // $mbx = substr($mbx, 0, strlen($mbx) - 1);
  674. // }
  675. // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  676. // }
  677. }
  678. for ($i=0 ; $i < count($sorted_lsub_ary); $i++) {
  679. if (($unseen_notify == 2 && $sorted_lsub_ary[$i]['mbx'] == 'INBOX')
  680. || $unseen_notify == 3
  681. || ($move_to_trash && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder))) {
  682. $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx['mbx']);
  683. if ($unseen_type == 2 || ($move_to_trash
  684. && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) {
  685. $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx['mbx']);
  686. }
  687. if ($unseen_notify != 3 && !($move_to_trash
  688. && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) break;
  689. }
  690. }
  691. $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
  692. return $boxesnew;
  693. }
  694. }
  695. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
  696. global $data_dir, $username, $list_special_folders_first,
  697. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  698. $move_to_trash, $move_to_sent, $save_as_draft,
  699. $delimiter;
  700. $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  701. /* create virtual root node */
  702. $mailboxes= new mailboxes();
  703. $mailboxes->is_root = true;
  704. $trail_del = false;
  705. if (isset($folder_prefix) && $folder_prefix != '') {
  706. $start = substr_count($folder_prefix,$delimiter);
  707. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  708. $trail_del = true;
  709. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  710. } else {
  711. $mailboxes->mailboxname_full = $folder_prefix;
  712. $start++;
  713. }
  714. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  715. } else $start = 0;
  716. for ($i=0; $i < count($mbx_ary); $i++) {
  717. if ($mbx_ary[$i]['mbx'] !='' ) {
  718. $mbx = new mailboxes();
  719. $mailbox = $mbx_ary[$i]['mbx'];
  720. switch ($mailbox) {
  721. case 'INBOX':
  722. $mbx->is_inbox = true;
  723. $mbx->is_special = true;
  724. break;
  725. case $trash_folder:
  726. $mbx->is_trash = true;
  727. $mbx->is_special = true;
  728. break;
  729. case $sent_folder:
  730. $mbx->is_sent = true;
  731. $mbx->is_special = true;
  732. break;
  733. case $draft_folder:
  734. $mbx->is_draft = true;
  735. $mbx->is_special = true;
  736. break;
  737. }
  738. if (isset($mbx_ary[$i]['unseen'])) {
  739. $mbx->unseen = $mbx_ary[$i]['unseen'];
  740. }
  741. if (isset($mbx_ary[$i]['nummessages'])) {
  742. $mbx->total = $mbx_ary[$i]['nummessages'];
  743. }
  744. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  745. if ($r_del_pos) {
  746. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  747. } else { /* mailbox is root folder */
  748. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  749. }
  750. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  751. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  752. }
  753. }
  754. return $mailboxes;
  755. }
  756. ?>