imap_mailbox.php 31 KB

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