imap_mailbox.php 34 KB

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