imap_mailbox.php 35 KB

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