imap_mailbox.php 34 KB

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