imap_mailbox.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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(
  312. readShortMailboxName($mailbox, $delimiter));
  313. }
  314. else {
  315. $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  316. }
  317. $boxesall[$g]['unformatted-dm'] = $mailbox;
  318. if (substr($mailbox, -1) == $delimiter) {
  319. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  320. }
  321. $boxesall[$g]['unformatted'] = $mailbox;
  322. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  323. $mailbox = substr($mailbox, strlen($folder_prefix));
  324. }
  325. $boxesall[$g]['unformatted-disp'] = $mailbox;
  326. $boxesall[$g]['id'] = $g;
  327. $boxesall[$g]['flags'] = array();
  328. if (isset($line[$g])) {
  329. ereg("\(([^)]*)\)",$line[$g],$regs);
  330. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  331. if ($flags) {
  332. $boxesall[$g]['flags'] = explode(' ', $flags);
  333. }
  334. }
  335. }
  336. return $boxesall;
  337. }
  338. /*
  339. * Sorting function used to sort mailbox names.
  340. * + Original patch from dave_michmerhuizen@yahoo.com
  341. * + Allows case insensitivity when sorting folders
  342. * + Takes care of the delimiter being sorted to the end, causing
  343. * subfolders to be listed in below folders that are prefixed
  344. * with their parent folders name.
  345. *
  346. * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
  347. * Without special sort function: foobar between foo and foo.bar
  348. * With special sort function: foobar AFTER foo and foo.bar :)
  349. */
  350. function user_strcasecmp($a, $b) {
  351. global $delimiter;
  352. /* Calculate the length of some strings. */
  353. $a_length = strlen($a);
  354. $b_length = strlen($b);
  355. $min_length = min($a_length, $b_length);
  356. $delimiter_length = strlen($delimiter);
  357. /* Set the initial result value. */
  358. $result = 0;
  359. /* Check the strings... */
  360. for ($c = 0; $c < $min_length; ++$c) {
  361. $a_del = substr($a, $c, $delimiter_length);
  362. $b_del = substr($b, $c, $delimiter_length);
  363. if (($a_del == $delimiter) && ($b_del == $delimiter)) {
  364. $result = 0;
  365. } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
  366. $result = -1;
  367. } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
  368. $result = 1;
  369. } else {
  370. $result = strcasecmp($a{$c}, $b{$c});
  371. }
  372. if ($result != 0) {
  373. break;
  374. }
  375. }
  376. /* If one string is a prefix of the other... */
  377. if ($result == 0) {
  378. if ($a_length < $b_length) {
  379. $result = -1;
  380. } else if ($a_length > $b_length) {
  381. $result = 1;
  382. }
  383. }
  384. return $result;
  385. }
  386. /*
  387. * Returns sorted mailbox lists in several different ways.
  388. * See comment on sqimap_mailbox_parse() for info about the returned array.
  389. */
  390. function sqimap_mailbox_list($imap_stream) {
  391. global $boxesnew, $default_folder_prefix;
  392. if ( !isset( $boxesnew ) ) {
  393. global $data_dir, $username, $list_special_folders_first,
  394. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  395. $move_to_trash, $move_to_sent, $save_as_draft,
  396. $delimiter;
  397. $inbox_in_list = false;
  398. $inbox_subscribed = false;
  399. require_once('../src/load_prefs.php');
  400. require_once('../functions/array.php');
  401. /* LSUB array */
  402. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*%\"",
  403. true, $response, $message);
  404. /*
  405. * Section about removing the last element was removed
  406. * We don't return "* OK" anymore from sqimap_read_data
  407. */
  408. $sorted_lsub_ary = array();
  409. for ($i=0;$i < count($lsub_ary); $i++) {
  410. /*
  411. * Workaround for EIMS
  412. * Doesn't work if the mailbox name is multiple lines
  413. */
  414. if (isset($lsub_ary[$i + 1]) &&
  415. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  416. $lsub_ary[$i], $regs)) {
  417. $i ++;
  418. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  419. }
  420. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  421. $sorted_lsub_ary[] = $temp_mailbox_name;
  422. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  423. $inbox_subscribed = true;
  424. }
  425. }
  426. $new_ary = array();
  427. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  428. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  429. $new_ary[] = $sorted_lsub_ary[$i];
  430. }
  431. }
  432. $sorted_lsub_ary = $new_ary;
  433. if (isset($sorted_lsub_ary)) {
  434. usort($sorted_lsub_ary, 'user_strcasecmp');
  435. }
  436. /* LIST array */
  437. $sorted_list_ary = array();
  438. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  439. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  440. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  441. }
  442. else {
  443. $mbx = $sorted_lsub_ary[$i];
  444. }
  445. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  446. true, $response, $message);
  447. /* Another workaround for EIMS */
  448. if (isset($read[1]) &&
  449. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  450. $read[0], $regs)) {
  451. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
  452. }
  453. if (isset($sorted_list_ary[$i])) {
  454. $sorted_list_ary[$i] = '';
  455. }
  456. if (isset($read[0])) {
  457. $sorted_list_ary[$i] = $read[0];
  458. }
  459. else {
  460. $sorted_list_ary[$i] = '';
  461. }
  462. if (isset($sorted_list_ary[$i]) &&
  463. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
  464. $inbox_in_list = true;
  465. }
  466. }
  467. /*
  468. * Just in case they're not subscribed to their inbox,
  469. * we'll get it for them anyway
  470. */
  471. if (!$inbox_subscribed || !$inbox_in_list) {
  472. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  473. true, $response, $message);
  474. /* Another workaround for EIMS */
  475. if (isset($inbox_ary[1]) &&
  476. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  477. $inbox_ary[0], $regs)) {
  478. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  479. '"' . $regs[2];
  480. }
  481. $sorted_list_ary[] = $inbox_ary[0];
  482. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  483. }
  484. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  485. /* Now, lets sort for special folders */
  486. $boxesnew = $used = array();
  487. /* Find INBOX */
  488. foreach ( $boxesall as $k => $box ) {
  489. if ( strtolower($box['unformatted']) == 'inbox') {
  490. $boxesnew[] = $box;
  491. $used[$k] = true;
  492. } else {
  493. $used[$k] = false;
  494. }
  495. }
  496. /* List special folders and their subfolders, if requested. */
  497. if ($list_special_folders_first) {
  498. foreach ( $boxesall as $k => $box ) {
  499. if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
  500. $boxesnew[] = $box;
  501. $used[$k] = true;
  502. }
  503. $spec_sub = str_replace('&nbsp;', '', $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, $trash_folder, $sent_folder, $draft_folder,
  606. $move_to_trash, $move_to_sent, $save_as_draft,
  607. $delimiter;
  608. $inbox_in_list = false;
  609. $inbox_subscribed = false;
  610. require_once('../src/load_prefs.php');
  611. require_once('../functions/array.php');
  612. /* LSUB array */
  613. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  614. true, $response, $message);
  615. /*
  616. * Section about removing the last element was removed
  617. * We don't return "* OK" anymore from sqimap_read_data
  618. */
  619. $sorted_lsub_ary = array();
  620. for ($i=0;$i < count($lsub_ary); $i++) {
  621. /*
  622. * Workaround for EIMS
  623. * Doesn't work if the mailbox name is multiple lines
  624. */
  625. if (isset($lsub_ary[$i + 1]) &&
  626. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  627. $lsub_ary[$i], $regs)) {
  628. $i ++;
  629. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  630. }
  631. if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
  632. $flag = $regs[1];
  633. $mbx = trim($regs[3]);
  634. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  635. }
  636. }
  637. array_multisort($sorted_lsub_ary, SORT_ASC, SORT_STRING);
  638. foreach ($sorted_lsub_ary as $mbx) {
  639. if ($mbx['mbx'] == 'INBOX') {
  640. $inbox_in_list = true;
  641. break;
  642. }
  643. }
  644. /*
  645. * Just in case they're not subscribed to their inbox,
  646. * we'll get it for them anyway
  647. */
  648. if (!$inbox_in_list) {
  649. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  650. true, $response, $message);
  651. /* Another workaround for EIMS */
  652. if (isset($inbox_ary[1]) &&
  653. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  654. $inbox_ary[0], $regs)) {
  655. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  656. '"' . $regs[2];
  657. }
  658. if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
  659. $flag = $regs[1];
  660. $mbx = trim($regs[3]);
  661. if (substr($mbx, -1) == $delimiter) {
  662. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  663. }
  664. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  665. }
  666. }
  667. for ($i=0 ; $i < count($sorted_lsub_ary); $i++) {
  668. if (($unseen_notify == 2 && $sorted_lsub_ary[$i]['mbx'] == 'INBOX')
  669. || $unseen_notify == 3
  670. || ($move_to_trash && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder))) {
  671. $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx['mbx']);
  672. if ($unseen_type == 2 || ($move_to_trash
  673. && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) {
  674. $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx['mbx']);
  675. }
  676. if ($unseen_notify != 3 && !($move_to_trash
  677. && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) break;
  678. }
  679. }
  680. $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
  681. return $boxesnew;
  682. }
  683. }
  684. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
  685. global $data_dir, $username, $list_special_folders_first,
  686. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  687. $move_to_trash, $move_to_sent, $save_as_draft,
  688. $delimiter;
  689. $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  690. /* create virtual root node */
  691. $mailboxes= new mailboxes();
  692. $mailboxes->is_root = true;
  693. $trail_del = false;
  694. if (isset($folder_prefix) && $folder_prefix != '') {
  695. $start = substr_count($folder_prefix,$delimiter);
  696. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  697. $trail_del = true;
  698. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  699. } else {
  700. $mailboxes->mailboxname_full = $folder_prefix;
  701. $start++;
  702. }
  703. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  704. } else $start = 0;
  705. for ($i=0; $i < count($mbx_ary); $i++) {
  706. if ($mbx_ary[$i]['mbx'] !='' ) {
  707. $mbx = new mailboxes();
  708. $mailbox = $mbx_ary[$i]['mbx'];
  709. switch ($mailbox) {
  710. case 'INBOX':
  711. $mbx->is_inbox = true;
  712. $mbx->is_special = true;
  713. break;
  714. case $trash_folder:
  715. $mbx->is_trash = true;
  716. $mbx->is_special = true;
  717. break;
  718. case $sent_folder:
  719. $mbx->is_sent = true;
  720. $mbx->is_special = true;
  721. break;
  722. case $draft_folder:
  723. $mbx->is_draft = true;
  724. $mbx->is_special = true;
  725. break;
  726. }
  727. if (isset($mbx_ary[$i]['unseen'])) {
  728. $mbx->unseen = $mbx_ary[$i]['unseen'];
  729. }
  730. if (isset($mbx_ary[$i]['nummessages'])) {
  731. $mbx->total = $mbx_ary[$i]['nummessages'];
  732. }
  733. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  734. if ($r_del_pos) {
  735. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  736. } else { /* mailbox is root folder */
  737. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  738. }
  739. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  740. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  741. }
  742. }
  743. return $mailboxes;
  744. }
  745. ?>