imap_mailbox.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 $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, $noselect_fix_enable;
  396. $inbox_in_list = false;
  397. $inbox_subscribed = false;
  398. require_once('../src/load_prefs.php');
  399. require_once('../functions/array.php');
  400. if ($noselect_fix_enable) {
  401. $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
  402. }
  403. else {
  404. $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
  405. }
  406. /* LSUB array */
  407. $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
  408. true, $response, $message);
  409. /*
  410. * Section about removing the last element was removed
  411. * We don't return "* OK" anymore from sqimap_read_data
  412. */
  413. $sorted_lsub_ary = array();
  414. for ($i=0;$i < count($lsub_ary); $i++) {
  415. /*
  416. * Workaround for EIMS
  417. * Doesn't work if the mailbox name is multiple lines
  418. */
  419. if (isset($lsub_ary[$i + 1]) &&
  420. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  421. $lsub_ary[$i], $regs)) {
  422. $i ++;
  423. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  424. }
  425. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  426. $sorted_lsub_ary[] = $temp_mailbox_name;
  427. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  428. $inbox_subscribed = true;
  429. }
  430. }
  431. $new_ary = array();
  432. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  433. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  434. $new_ary[] = $sorted_lsub_ary[$i];
  435. }
  436. }
  437. $sorted_lsub_ary = $new_ary;
  438. if (isset($sorted_lsub_ary)) {
  439. usort($sorted_lsub_ary, 'user_strcasecmp');
  440. }
  441. /* LIST array */
  442. $sorted_list_ary = array();
  443. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  444. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  445. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  446. }
  447. else {
  448. $mbx = $sorted_lsub_ary[$i];
  449. }
  450. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  451. true, $response, $message);
  452. /* Another workaround for EIMS */
  453. if (isset($read[1]) &&
  454. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  455. $read[0], $regs)) {
  456. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
  457. }
  458. if (isset($sorted_list_ary[$i])) {
  459. $sorted_list_ary[$i] = '';
  460. }
  461. if (isset($read[0])) {
  462. $sorted_list_ary[$i] = $read[0];
  463. }
  464. else {
  465. $sorted_list_ary[$i] = '';
  466. }
  467. if (isset($sorted_list_ary[$i]) &&
  468. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
  469. $inbox_in_list = true;
  470. }
  471. }
  472. /*
  473. * Just in case they're not subscribed to their inbox,
  474. * we'll get it for them anyway
  475. */
  476. if (!$inbox_subscribed || !$inbox_in_list) {
  477. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  478. true, $response, $message);
  479. /* Another workaround for EIMS */
  480. if (isset($inbox_ary[1]) &&
  481. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  482. $inbox_ary[0], $regs)) {
  483. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  484. '"' . $regs[2];
  485. }
  486. $sorted_list_ary[] = $inbox_ary[0];
  487. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  488. }
  489. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  490. /* Now, lets sort for special folders */
  491. $boxesnew = $used = array();
  492. /* Find INBOX */
  493. foreach ( $boxesall as $k => $box ) {
  494. if ( strtolower($box['unformatted']) == 'inbox') {
  495. $boxesnew[] = $box;
  496. $used[$k] = true;
  497. } else {
  498. $used[$k] = false;
  499. }
  500. }
  501. /* List special folders and their subfolders, if requested. */
  502. if ($list_special_folders_first) {
  503. foreach ( $boxesall as $k => $box ) {
  504. if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
  505. $boxesnew[] = $box;
  506. $used[$k] = true;
  507. }
  508. $spec_sub = str_replace('&nbsp;', '', $box['formatted']);
  509. $spec_sub = str_replace('*', '', $spec_sub);
  510. $spec_sub = str_replace('?', '\?', $spec_sub);
  511. /* In case of problems with preg
  512. here is a ereg version
  513. if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
  514. if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
  515. $boxesnew[] = $box;
  516. $used[$k] = true;
  517. }
  518. }
  519. }
  520. /* Rest of the folders */
  521. foreach ( $boxesall as $k => $box ) {
  522. if ( !$used[$k] ) {
  523. $boxesnew[] = $box;
  524. }
  525. }
  526. }
  527. return $boxesnew;
  528. }
  529. /*
  530. * Returns a list of all folders, subscribed or not
  531. */
  532. function sqimap_mailbox_list_all($imap_stream) {
  533. global $list_special_folders_first, $folder_prefix, $delimiter;
  534. require_once('../functions/array.php');
  535. $ssid = sqimap_session_id();
  536. $lsid = strlen( $ssid );
  537. fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
  538. $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
  539. $g = 0;
  540. $phase = 'inbox';
  541. for ($i = 0; $i < count($read_ary); $i++) {
  542. /* Another workaround for EIMS */
  543. if (isset($read_ary[$i + 1]) &&
  544. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  545. $read_ary[$i], $regs)) {
  546. $i ++;
  547. $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
  548. }
  549. if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
  550. /* Store the raw IMAP reply */
  551. $boxes[$g]['raw'] = $read_ary[$i];
  552. /* Count number of delimiters ($delimiter) in folder name */
  553. $mailbox = find_mailbox_name($read_ary[$i]);
  554. $dm_count = substr_count($mailbox, $delimiter);
  555. if (substr($mailbox, -1) == $delimiter) {
  556. /* If name ends in delimiter - decrement count by one */
  557. $dm_count--;
  558. }
  559. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  560. $boxesallbyname[$mailbox] = $g;
  561. $parentfolder = readMailboxParent($mailbox, $delimiter);
  562. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  563. (ereg('^'.$folder_prefix, $mailbox)) ||
  564. ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  565. if ($dm_count) {
  566. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  567. }
  568. else {
  569. $boxes[$g]['formatted'] = '';
  570. }
  571. $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  572. }
  573. else {
  574. $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  575. }
  576. $boxes[$g]['unformatted-dm'] = $mailbox;
  577. if (substr($mailbox, -1) == $delimiter) {
  578. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  579. }
  580. $boxes[$g]['unformatted'] = $mailbox;
  581. $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
  582. $boxes[$g]['id'] = $g;
  583. /* Now lets get the flags for this mailbox */
  584. $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
  585. true, $response, $message);
  586. /* Another workaround for EIMS */
  587. if (isset($read_mlbx[1]) &&
  588. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
  589. $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
  590. }
  591. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
  592. $flags = substr($flags, 0, strpos($flags, ')'));
  593. $flags = str_replace('\\', '', $flags);
  594. $flags = trim(strtolower($flags));
  595. if ($flags) {
  596. $boxes[$g]['flags'] = explode(' ', $flags);
  597. } else {
  598. $boxes[$g]['flags'] = array();
  599. }
  600. }
  601. $g++;
  602. }
  603. if(is_array($boxes)) {
  604. $boxes = ary_sort ($boxes, 'unformatted', 1);
  605. }
  606. return $boxes;
  607. }
  608. function sqimap_mailbox_tree($imap_stream) {
  609. global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
  610. if ( !isset( $boxesnew ) ) {
  611. global $data_dir, $username, $list_special_folders_first,
  612. $folder_prefix, $delimiter;
  613. $inbox_in_list = false;
  614. $inbox_subscribed = false;
  615. require_once('../src/load_prefs.php');
  616. require_once('../functions/array.php');
  617. /* LSUB array */
  618. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  619. true, $response, $message);
  620. /*
  621. * Section about removing the last element was removed
  622. * We don't return "* OK" anymore from sqimap_read_data
  623. */
  624. $sorted_lsub_ary = array();
  625. for ($i=0;$i < count($lsub_ary); $i++) {
  626. /*
  627. * Workaround for EIMS
  628. * Doesn't work if the mailbox name is multiple lines
  629. */
  630. if (isset($lsub_ary[$i + 1]) &&
  631. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  632. $lsub_ary[$i], $regs)) {
  633. $i ++;
  634. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  635. }
  636. // if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
  637. // $flag = $regs[1];
  638. // $mbx = trim($regs[3]);
  639. // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  640. // }
  641. $mbx = find_mailbox_name($lsub_ary[$i]);
  642. if (substr($mbx, -1) == $delimiter) {
  643. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  644. }
  645. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
  646. }
  647. array_multisort($sorted_lsub_ary, SORT_ASC, SORT_STRING);
  648. foreach ($sorted_lsub_ary as $mbx) {
  649. if ($mbx['mbx'] == 'INBOX') {
  650. $inbox_in_list = true;
  651. break;
  652. }
  653. }
  654. /*
  655. * Just in case they're not subscribed to their inbox,
  656. * we'll get it for them anyway
  657. */
  658. if (!$inbox_in_list) {
  659. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  660. true, $response, $message);
  661. /* Another workaround for EIMS */
  662. if (isset($inbox_ary[1]) &&
  663. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  664. $inbox_ary[0], $regs)) {
  665. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  666. '"' . $regs[2];
  667. }
  668. $mbx = find_mailbox_name($inbox_ary[0]);
  669. if (substr($mbx, -1) == $delimiter) {
  670. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  671. }
  672. if ( $mbx == 'INBOX') {
  673. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
  674. sqimap_subscribe($imap_stream, 'INBOX');
  675. }
  676. // if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
  677. // $flag = $regs[1];
  678. // $mbx = trim($regs[3]);
  679. // if (substr($mbx, -1) == $delimiter) {
  680. // $mbx = substr($mbx, 0, strlen($mbx) - 1);
  681. // }
  682. // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  683. // }
  684. }
  685. for ($i=0 ; $i < count($sorted_lsub_ary); $i++) {
  686. if (($unseen_notify == 2 && $sorted_lsub_ary[$i]['mbx'] == 'INBOX')
  687. || $unseen_notify == 3
  688. || ($move_to_trash && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder))) {
  689. $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx['mbx']);
  690. if ($unseen_type == 2 || ($move_to_trash
  691. && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) {
  692. $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx['mbx']);
  693. }
  694. if ($unseen_notify != 3 && !($move_to_trash
  695. && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) break;
  696. }
  697. }
  698. $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
  699. return $boxesnew;
  700. }
  701. }
  702. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
  703. global $data_dir, $username, $list_special_folders_first,
  704. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  705. $move_to_trash, $move_to_sent, $save_as_draft,
  706. $delimiter;
  707. $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  708. /* create virtual root node */
  709. $mailboxes= new mailboxes();
  710. $mailboxes->is_root = true;
  711. $trail_del = false;
  712. if (isset($folder_prefix) && $folder_prefix != '') {
  713. $start = substr_count($folder_prefix,$delimiter);
  714. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  715. $trail_del = true;
  716. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  717. } else {
  718. $mailboxes->mailboxname_full = $folder_prefix;
  719. $start++;
  720. }
  721. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  722. } else $start = 0;
  723. for ($i=0; $i < count($mbx_ary); $i++) {
  724. if ($mbx_ary[$i]['mbx'] !='' ) {
  725. $mbx = new mailboxes();
  726. $mailbox = $mbx_ary[$i]['mbx'];
  727. switch ($mailbox) {
  728. case 'INBOX':
  729. $mbx->is_inbox = true;
  730. $mbx->is_special = true;
  731. break;
  732. case $trash_folder:
  733. $mbx->is_trash = true;
  734. $mbx->is_special = true;
  735. break;
  736. case $sent_folder:
  737. $mbx->is_sent = true;
  738. $mbx->is_special = true;
  739. break;
  740. case $draft_folder:
  741. $mbx->is_draft = true;
  742. $mbx->is_special = true;
  743. break;
  744. }
  745. if (isset($mbx_ary[$i]['unseen'])) {
  746. $mbx->unseen = $mbx_ary[$i]['unseen'];
  747. }
  748. if (isset($mbx_ary[$i]['nummessages'])) {
  749. $mbx->total = $mbx_ary[$i]['nummessages'];
  750. }
  751. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  752. if ($r_del_pos) {
  753. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  754. } else { /* mailbox is root folder */
  755. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  756. }
  757. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  758. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  759. }
  760. }
  761. return $mailboxes;
  762. }
  763. ?>