imap_mailbox.php 29 KB

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