imap_mailbox.php 30 KB

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