imap_mailbox.php 34 KB

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