imap_mailbox.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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 list of options (to be echoed into select statement
  402. * based on available mailboxes and separators
  403. * Caller should surround options with <SELECT..> </SELECT> and
  404. * any formatting.
  405. * $imap_stream - $imapConnection to query for mailboxes
  406. * $show_selected - array containing list of mailboxes to pre-select (0 if none)
  407. * $folder_skip - array of folders to keep out of option list (compared in lower)
  408. * $boxes - list of already fetched boxes (for places like folder panel, where
  409. * you know these options will be shown 3 times in a row.. (most often unset).
  410. */
  411. function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0 ) {
  412. global $username, $data_dir;
  413. $mbox_options = '';
  414. $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
  415. if ( $boxes == 0 ) {
  416. $boxes = sqimap_mailbox_list($imap_stream);
  417. }
  418. foreach ($boxes as $boxes_part) {
  419. if (!in_array('noselect', $boxes_part['flags'])) {
  420. $box = $boxes_part['unformatted'];
  421. $lowerbox = strtolower($box);
  422. if ( $folder_skip != 0 && in_array($lowerbox, $folder_skip) ) {
  423. continue;
  424. }
  425. if ($lowerbox == 'inbox'){
  426. $box2 = _("INBOX");
  427. } else if ( $shorten_box_names == 2 ) { /* delimited, style = 2 */
  428. $box2 = str_replace('&nbsp;&nbsp;', '.&nbsp;', $boxes_part['formatted']);
  429. } else if ( $shorten_box_names == 1 ) { /* indent, style = 1 */
  430. $box2 = $boxes_part['formatted'];
  431. } else { /* default, long names, style = 0 */
  432. $box2 = str_replace(' ', '&nbsp;', imap_utf7_decode_local($boxes_part['unformatted-disp']));
  433. }
  434. if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
  435. $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
  436. } else {
  437. $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
  438. }
  439. }
  440. }
  441. return $mbox_options;
  442. }
  443. /*
  444. * Returns sorted mailbox lists in several different ways.
  445. * See comment on sqimap_mailbox_parse() for info about the returned array.
  446. */
  447. function sqimap_mailbox_list($imap_stream) {
  448. global $default_folder_prefix;
  449. if ( !isset( $boxesnew ) ) {
  450. global $data_dir, $username, $list_special_folders_first,
  451. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  452. $move_to_trash, $move_to_sent, $save_as_draft,
  453. $delimiter, $noselect_fix_enable;
  454. $inbox_in_list = false;
  455. $inbox_subscribed = false;
  456. require_once(SM_PATH . 'include/load_prefs.php');
  457. require_once(SM_PATH . 'functions/array.php');
  458. if ($noselect_fix_enable) {
  459. $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
  460. }
  461. else {
  462. $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
  463. }
  464. /* LSUB array */
  465. $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
  466. true, $response, $message);
  467. /*
  468. * Section about removing the last element was removed
  469. * We don't return "* OK" anymore from sqimap_read_data
  470. */
  471. $sorted_lsub_ary = array();
  472. for ($i=0;$i < count($lsub_ary); $i++) {
  473. /*
  474. * Workaround for EIMS
  475. * Doesn't work if the mailbox name is multiple lines
  476. */
  477. if (isset($lsub_ary[$i + 1]) &&
  478. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  479. $lsub_ary[$i], $regs)) {
  480. $i ++;
  481. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  482. }
  483. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  484. $sorted_lsub_ary[] = $temp_mailbox_name;
  485. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  486. $inbox_subscribed = true;
  487. }
  488. }
  489. $new_ary = array();
  490. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  491. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  492. $new_ary[] = $sorted_lsub_ary[$i];
  493. }
  494. }
  495. $sorted_lsub_ary = $new_ary;
  496. if (isset($sorted_lsub_ary)) {
  497. usort($sorted_lsub_ary, 'user_strcasecmp');
  498. }
  499. $sorted_list_ary = $sorted_lsub_ary;
  500. /* LIST array */
  501. // $sorted_list_ary = array();
  502. // for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  503. if (false) {
  504. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  505. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  506. }
  507. else {
  508. $mbx = $sorted_lsub_ary[$i];
  509. }
  510. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  511. true, $response, $message);
  512. /* Another workaround for EIMS */
  513. if (isset($read[1]) &&
  514. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  515. $read[0], $regs)) {
  516. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
  517. }
  518. if (isset($sorted_list_ary[$i])) {
  519. $sorted_list_ary[$i] = '';
  520. }
  521. if (isset($read[0])) {
  522. $sorted_list_ary[$i] = $read[0];
  523. }
  524. else {
  525. $sorted_list_ary[$i] = '';
  526. }
  527. if (isset($sorted_list_ary[$i]) &&
  528. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
  529. $inbox_in_list = true;
  530. }
  531. }
  532. // $inbox_in_list = true;
  533. /*
  534. * Just in case they're not subscribed to their inbox,
  535. * we'll get it for them anyway
  536. */
  537. if (!$inbox_subscribed) {// || !$inbox_in_list) {
  538. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  539. true, $response, $message);
  540. /* Another workaround for EIMS */
  541. if (isset($inbox_ary[1]) &&
  542. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  543. $inbox_ary[0], $regs)) {
  544. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  545. '"' . $regs[2];
  546. }
  547. $sorted_list_ary[] = $inbox_ary[0];
  548. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  549. }
  550. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  551. /* Now, lets sort for special folders */
  552. $boxesnew = $used = array();
  553. /* Find INBOX */
  554. foreach ( $boxesall as $k => $box ) {
  555. if ( strtolower($box['unformatted']) == 'inbox') {
  556. $boxesnew[] = $box;
  557. $used[$k] = true;
  558. } else {
  559. $used[$k] = false;
  560. }
  561. }
  562. /* List special folders and their subfolders, if requested. */
  563. if ($list_special_folders_first) {
  564. foreach ( $boxesall as $k => $box ) {
  565. if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
  566. $boxesnew[] = $box;
  567. $used[$k] = true;
  568. }
  569. $spec_sub = str_replace('&nbsp;', '', $box['formatted']);
  570. $spec_sub = preg_replace("/(\*|\[|\]|\(|\)|\?|\+|\{|\}|\^|\\$)/", '\\\\'.'\\1', $spec_sub);
  571. /* In case of problems with preg
  572. here is a ereg version
  573. if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
  574. if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
  575. $boxesnew[] = $box;
  576. $used[$k] = true;
  577. }
  578. }
  579. }
  580. /* Rest of the folders */
  581. foreach ( $boxesall as $k => $box ) {
  582. if ( !$used[$k] ) {
  583. $boxesnew[] = $box;
  584. }
  585. }
  586. }
  587. return $boxesnew;
  588. }
  589. /*
  590. * Returns a list of all folders, subscribed or not
  591. */
  592. function sqimap_mailbox_list_all($imap_stream) {
  593. global $list_special_folders_first, $folder_prefix, $delimiter;
  594. require_once(SM_PATH . 'functions/array.php');
  595. $ssid = sqimap_session_id();
  596. $lsid = strlen( $ssid );
  597. fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
  598. $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
  599. $g = 0;
  600. $phase = 'inbox';
  601. $fld_pre_length = strlen($folder_prefix);
  602. for ($i = 0; $i < count($read_ary); $i++) {
  603. /* Another workaround for EIMS */
  604. if (isset($read_ary[$i + 1]) &&
  605. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  606. $read_ary[$i], $regs)) {
  607. $i ++;
  608. $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
  609. }
  610. if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
  611. /* Store the raw IMAP reply */
  612. $boxes[$g]['raw'] = $read_ary[$i];
  613. /* Count number of delimiters ($delimiter) in folder name */
  614. $mailbox = find_mailbox_name($read_ary[$i]);
  615. $dm_count = substr_count($mailbox, $delimiter);
  616. if (substr($mailbox, -1) == $delimiter) {
  617. /* If name ends in delimiter - decrement count by one */
  618. $dm_count--;
  619. }
  620. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  621. $boxesallbyname[$mailbox] = $g;
  622. $parentfolder = readMailboxParent($mailbox, $delimiter);
  623. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  624. (ereg('^'.$folder_prefix, $mailbox)) ||
  625. ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  626. if ($dm_count) {
  627. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  628. }
  629. else {
  630. $boxes[$g]['formatted'] = '';
  631. }
  632. $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  633. }
  634. else {
  635. $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  636. }
  637. $boxes[$g]['unformatted-dm'] = $mailbox;
  638. if (substr($mailbox, -1) == $delimiter) {
  639. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  640. }
  641. $boxes[$g]['unformatted'] = $mailbox;
  642. $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
  643. $boxes[$g]['id'] = $g;
  644. /* Now lets get the flags for this mailbox */
  645. $read_mlbx = $read_ary[$i];
  646. // $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
  647. // true, $response, $message);
  648. /* Another workaround for EIMS */
  649. // if (isset($read_mlbx[1]) &&
  650. // ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
  651. // $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
  652. // }
  653. // echo $read_mlbx[0] .' raw 2 <br>';
  654. $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
  655. $flags = substr($flags, 0, strpos($flags, ')'));
  656. $flags = str_replace('\\', '', $flags);
  657. $flags = trim(strtolower($flags));
  658. if ($flags) {
  659. $boxes[$g]['flags'] = explode(' ', $flags);
  660. } else {
  661. $boxes[$g]['flags'] = array();
  662. }
  663. }
  664. $g++;
  665. }
  666. if(is_array($boxes)) {
  667. $boxes = ary_sort ($boxes, 'unformatted', 1);
  668. }
  669. return $boxes;
  670. }
  671. function sqimap_mailbox_tree($imap_stream) {
  672. global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
  673. if ( !isset( $boxesnew ) ) {
  674. global $data_dir, $username, $list_special_folders_first,
  675. $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
  676. $inbox_in_list = false;
  677. $inbox_subscribed = false;
  678. require_once(SM_PATH . 'include/load_prefs.php');
  679. require_once(SM_PATH . 'functions/array.php');
  680. /* LSUB array */
  681. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  682. true, $response, $message);
  683. /*
  684. * Section about removing the last element was removed
  685. * We don't return "* OK" anymore from sqimap_read_data
  686. */
  687. $sorted_lsub_ary = array();
  688. $cnt = count($lsub_ary);
  689. for ($i=0;$i < $cnt; $i++) {
  690. /*
  691. * Workaround for EIMS
  692. * Doesn't work if the mailbox name is multiple lines
  693. */
  694. if (isset($lsub_ary[$i + 1]) &&
  695. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  696. $lsub_ary[$i], $regs)) {
  697. $i ++;
  698. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  699. }
  700. // if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
  701. // $flag = $regs[1];
  702. // $mbx = trim($regs[3]);
  703. // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  704. // }
  705. $mbx = find_mailbox_name($lsub_ary[$i]);
  706. $noselect = check_is_noselect($lsub_ary[$i]);
  707. if (substr($mbx, -1) == $delimiter) {
  708. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  709. }
  710. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
  711. }
  712. array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
  713. foreach ($sorted_lsub_ary as $mbx) {
  714. if ($mbx['mbx'] == 'INBOX') {
  715. $inbox_in_list = true;
  716. break;
  717. }
  718. }
  719. /*
  720. * Just in case they're not subscribed to their inbox,
  721. * we'll get it for them anyway
  722. */
  723. if (!$inbox_in_list) {
  724. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  725. true, $response, $message);
  726. /* Another workaround for EIMS */
  727. if (isset($inbox_ary[1]) &&
  728. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  729. $inbox_ary[0], $regs)) {
  730. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  731. '"' . $regs[2];
  732. }
  733. $mbx = find_mailbox_name($inbox_ary[0]);
  734. if (substr($mbx, -1) == $delimiter) {
  735. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  736. }
  737. if ( $mbx == 'INBOX') {
  738. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
  739. sqimap_subscribe($imap_stream, 'INBOX');
  740. }
  741. // if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
  742. // $flag = $regs[1];
  743. // $mbx = trim($regs[3]);
  744. // if (substr($mbx, -1) == $delimiter) {
  745. // $mbx = substr($mbx, 0, strlen($mbx) - 1);
  746. // }
  747. // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
  748. // }
  749. }
  750. $cnt = count($sorted_lsub_ary);
  751. for ($i=0 ; $i < $cnt; $i++) {
  752. $mbx = $sorted_lsub_ary[$i]['mbx'];
  753. if (($unseen_notify == 2 && $mbx == 'INBOX')
  754. || $unseen_notify == 3
  755. || ($move_to_trash && ($mbx == $trash_folder))) {
  756. $sorted_lsub_ary[$i]['unseen'] =
  757. $sorted_lsub_ary[$i]['noselect'] ?
  758. 0 : sqimap_unseen_messages($imap_stream, $mbx);
  759. if ($unseen_type == 2 || ($move_to_trash
  760. && ($mbx == $trash_folder) )) {
  761. $sorted_lsub_ary[$i]['nummessages'] =
  762. $sorted_lsub_ary[$i]['noselect'] ?
  763. 0 : sqimap_get_num_messages($imap_stream, $mbx);
  764. }
  765. if ($mbx == $trash_folder) {
  766. $sorted_lsub_ary[$i]['nummessages'] =
  767. $sorted_lsub_ary[$i]['noselect'] ?
  768. 0 : sqimap_get_num_messages($imap_stream, $mbx);
  769. }
  770. }
  771. }
  772. $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
  773. return $boxesnew;
  774. }
  775. }
  776. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
  777. global $data_dir, $username, $list_special_folders_first,
  778. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  779. $move_to_trash, $move_to_sent, $save_as_draft,
  780. $delimiter;
  781. $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  782. /* create virtual root node */
  783. $mailboxes= new mailboxes();
  784. $mailboxes->is_root = true;
  785. $trail_del = false;
  786. if (isset($folder_prefix) && $folder_prefix != '') {
  787. $start = substr_count($folder_prefix,$delimiter);
  788. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  789. $trail_del = true;
  790. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  791. } else {
  792. $mailboxes->mailboxname_full = $folder_prefix;
  793. $start++;
  794. }
  795. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  796. } else $start = 0;
  797. $cnt = count($mbx_ary);
  798. for ($i=0; $i < $cnt; $i++) {
  799. if ($mbx_ary[$i]['mbx'] !='' ) {
  800. $mbx = new mailboxes();
  801. $mailbox = $mbx_ary[$i]['mbx'];
  802. switch ($mailbox) {
  803. case 'INBOX':
  804. $mbx->is_inbox = true;
  805. $mbx->is_special = true;
  806. break;
  807. case $trash_folder:
  808. $mbx->is_trash = true;
  809. $mbx->is_special = true;
  810. break;
  811. case $sent_folder:
  812. $mbx->is_sent = true;
  813. $mbx->is_special = true;
  814. break;
  815. case $draft_folder:
  816. $mbx->is_draft = true;
  817. $mbx->is_special = true;
  818. break;
  819. }
  820. if (isset($mbx_ary[$i]['unseen'])) {
  821. $mbx->unseen = $mbx_ary[$i]['unseen'];
  822. }
  823. if (isset($mbx_ary[$i]['nummessages'])) {
  824. $mbx->total = $mbx_ary[$i]['nummessages'];
  825. }
  826. $mbx->is_noselect = $mbx_ary[$i]['noselect'];
  827. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  828. if ($r_del_pos) {
  829. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  830. } else { /* mailbox is root folder */
  831. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  832. }
  833. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  834. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  835. }
  836. }
  837. return $mailboxes;
  838. }
  839. ?>