imap_mailbox.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. * @package squirrelmail
  12. */
  13. /** UTF7 support */
  14. require_once(SM_PATH . 'functions/imap_utf7_local.php');
  15. global $boxesnew;
  16. /**
  17. * Mailboxes class
  18. *
  19. * FIXME. This class should be extracted and placed in a separate file that
  20. * can be included before we start the session. That makes caching of the tree
  21. * possible. On a refresh mailboxes from left_main.php the only function that
  22. * should be called is the sqimap_get_status_mbx_tree. In case of subscribe
  23. * / rename / delete / new we have to create methods for adding/changing the
  24. * mailbox in the mbx_tree without the need for a refresh.
  25. * @package squirrelmail
  26. */
  27. class mailboxes {
  28. var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
  29. $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
  30. $is_trash = false, $is_draft = false, $mbxs = array(),
  31. $unseen = false, $total = false;
  32. function addMbx($mbx, $delimiter, $start, $specialfirst) {
  33. $ary = explode($delimiter, $mbx->mailboxname_full);
  34. $mbx_parent =& $this;
  35. for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
  36. $mbx_childs =& $mbx_parent->mbxs;
  37. $found = false;
  38. if ($mbx_childs) {
  39. foreach ($mbx_childs as $key => $parent) {
  40. if ($parent->mailboxname_sub == $ary[$i]) {
  41. $mbx_parent =& $mbx_parent->mbxs[$key];
  42. $found = true;
  43. break;
  44. }
  45. }
  46. }
  47. if (!$found) {
  48. $no_select_mbx = new mailboxes();
  49. if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
  50. $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
  51. } else {
  52. $no_select_mbx->mailboxname_full = $ary[$i];
  53. }
  54. $no_select_mbx->mailboxname_sub = $ary[$i];
  55. $no_select_mbx->is_noselect = true;
  56. $mbx_parent->mbxs[] = $no_select_mbx;
  57. $i--;
  58. }
  59. }
  60. $mbx_parent->mbxs[] = $mbx;
  61. if ($mbx->is_special && $specialfirst) {
  62. usort($mbx_parent->mbxs, 'sortSpecialMbx');
  63. }
  64. }
  65. }
  66. function sortSpecialMbx($a, $b) {
  67. if ($a->is_inbox) {
  68. $acmp = '0'. $a->mailboxname_full;
  69. } else if ($a->is_special) {
  70. $acmp = '1'. $a->mailboxname_full;
  71. } else {
  72. $acmp = '2' . $a->mailboxname_full;
  73. }
  74. if ($b->is_inbox) {
  75. $bcmp = '0'. $b->mailboxname_full;
  76. }else if ($b->is_special) {
  77. $bcmp = '1' . $b->mailboxname_full;
  78. } else {
  79. $bcmp = '2' . $b->mailboxname_full;
  80. }
  81. return strnatcasecmp($acmp, $bcmp);
  82. }
  83. function compact_mailboxes_response($ary)
  84. {
  85. /*
  86. * Workaround for mailboxes returned as literal
  87. * FIXME : Doesn't work if the mailbox name is multiple lines
  88. * (larger then fgets buffer)
  89. */
  90. for ($i = 0, $iCnt=count($ary); $i < $iCnt; $i++) {
  91. if (isset($ary[$i + 1]) && substr($ary[$i], -3) == "}\r\n") {
  92. if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  93. $ary[$i], $regs)) {
  94. $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+1])) . '"' . $regs[2];
  95. array_splice($ary, $i+1, 2);
  96. }
  97. }
  98. }
  99. /* remove duplicates and ensure array is contiguous */
  100. return array_values(array_unique($ary));
  101. }
  102. /**
  103. * Extract the mailbox name from an untagged LIST (7.2.2) or LSUB (7.2.3) answer
  104. * (LIST|LSUB) (<Flags list>) (NIL|"<separator atom>") <mailbox name string>\r\n
  105. * mailbox name in quoted string MUST be unquoted and stripslashed (sm API)
  106. */
  107. function find_mailbox_name($line)
  108. {
  109. if (preg_match('/^\* (?:LIST|LSUB) \([^\)]*\) (?:NIL|\"[^\"]*\") ([^\r\n]*)[\r\n]*$/i', $line, $regs)) {
  110. if (substr($regs[1], 0, 1) == '"')
  111. return stripslashes(substr($regs[1], 1, -1));
  112. return $regs[1];
  113. }
  114. return '';
  115. }
  116. /**
  117. * @return bool whether this is a Noselect mailbox.
  118. */
  119. function check_is_noselect ($lsub_line) {
  120. return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
  121. }
  122. /**
  123. * @return bool whether this is a Noinferiors mailbox.
  124. */
  125. function check_is_noinferiors ($lsub_line) {
  126. return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
  127. }
  128. /**
  129. * If $haystack is a full mailbox name, and $needle is the mailbox
  130. * separator character, returns the second last part of the full
  131. * mailbox name (i.e. the mailbox's parent mailbox)
  132. */
  133. function readMailboxParent($haystack, $needle) {
  134. if ($needle == '') {
  135. $ret = '';
  136. } else {
  137. $parts = explode($needle, $haystack);
  138. $elem = array_pop($parts);
  139. while ($elem == '' && count($parts)) {
  140. $elem = array_pop($parts);
  141. }
  142. $ret = join($needle, $parts);
  143. }
  144. return( $ret );
  145. }
  146. /**
  147. * Check if $subbox is below the specified $parentbox
  148. */
  149. function isBoxBelow( $subbox, $parentbox ) {
  150. global $delimiter;
  151. /*
  152. * Eliminate the obvious mismatch, where the
  153. * subfolder path is shorter than that of the potential parent
  154. */
  155. if ( strlen($subbox) < strlen($parentbox) ) {
  156. return false;
  157. }
  158. /* check for delimiter */
  159. if (!substr($parentbox,-1) == $delimiter) {
  160. $parentbox.=$delimiter;
  161. }
  162. if (substr($subbox,0,strlen($parentbox)) == $parentbox) {
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }
  168. /**
  169. * Defines special mailboxes: given a mailbox name, it checks if this is a
  170. * "special" one: INBOX, Trash, Sent or Draft.
  171. */
  172. function isSpecialMailbox( $box ) {
  173. $ret = ( (strtolower($box) == 'inbox') ||
  174. isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
  175. if ( !$ret ) {
  176. $ret = boolean_hook_function('special_mailbox',$box,1);
  177. }
  178. return $ret;
  179. }
  180. /**
  181. * @return bool whether this is a Trash folder
  182. */
  183. function isTrashMailbox ($box) {
  184. global $trash_folder, $move_to_trash;
  185. return $move_to_trash && $trash_folder &&
  186. ( $box == $trash_folder || isBoxBelow($box, $trash_folder) );
  187. }
  188. /**
  189. * @return bool whether this is a Sent folder
  190. */
  191. function isSentMailbox($box) {
  192. global $sent_folder, $move_to_sent;
  193. return $move_to_sent && $sent_folder &&
  194. ( $box == $sent_folder || isBoxBelow($box, $sent_folder) );
  195. }
  196. /**
  197. * @return bool whether this is a Draft folder
  198. */
  199. function isDraftMailbox($box) {
  200. global $draft_folder, $save_as_draft;
  201. return $save_as_draft &&
  202. ( $box == $draft_folder || isBoxBelow($box, $draft_folder) );
  203. }
  204. /**
  205. * Expunges a mailbox, ie. delete all contents.
  206. */
  207. function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
  208. global $uid_support;
  209. if ($id) {
  210. if (is_array($id)) {
  211. $id = sqimap_message_list_squisher($id);
  212. }
  213. $id = ' '.$id;
  214. $uid = $uid_support;
  215. } else {
  216. $uid = false;
  217. }
  218. $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
  219. $response, $message, $uid);
  220. $cnt = 0;
  221. if (is_array($read)) {
  222. foreach ($read as $r) {
  223. if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
  224. $cnt++;
  225. }
  226. }
  227. }
  228. return $cnt;
  229. }
  230. /**
  231. * Checks whether or not the specified mailbox exists
  232. */
  233. function sqimap_mailbox_exists ($imap_stream, $mailbox) {
  234. if (!isset($mailbox) || empty($mailbox)) {
  235. return false;
  236. }
  237. $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
  238. true, $response, $message);
  239. return isset($mbx[0]);
  240. }
  241. /**
  242. * Selects a mailbox
  243. */
  244. function sqimap_mailbox_select ($imap_stream, $mailbox) {
  245. global $auto_expunge;
  246. if ($mailbox == 'None') {
  247. return;
  248. }
  249. $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
  250. true, $response, $message);
  251. $result = array();
  252. for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
  253. if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
  254. $result[strtoupper($regs[1])] = $regs[2];
  255. } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
  256. $result[strtoupper($regs[2])] = $regs[1];
  257. } else {
  258. if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
  259. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
  260. $result['PERMANENTFLAGS'] = $regs[1];
  261. } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
  262. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
  263. $result['FLAGS'] = $regs[1];
  264. }
  265. }
  266. }
  267. if (preg_match('/^\[(.+)\]/',$message, $regs)) {
  268. $result['RIGHTS']=$regs[1];
  269. }
  270. if ($auto_expunge) {
  271. $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
  272. }
  273. return $result;
  274. }
  275. /**
  276. * Creates a folder.
  277. */
  278. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  279. global $delimiter;
  280. if (strtolower($type) == 'noselect') {
  281. $mailbox .= $delimiter;
  282. }
  283. $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
  284. sqimap_encode_mailbox_name($mailbox),
  285. true, $response, $message);
  286. sqimap_subscribe ($imap_stream, $mailbox);
  287. }
  288. /**
  289. * Subscribes to an existing folder.
  290. */
  291. function sqimap_subscribe ($imap_stream, $mailbox) {
  292. $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
  293. sqimap_encode_mailbox_name($mailbox),
  294. true, $response, $message);
  295. }
  296. /**
  297. * Unsubscribes from an existing folder
  298. */
  299. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  300. $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
  301. sqimap_encode_mailbox_name($mailbox),
  302. false, $response, $message);
  303. }
  304. /**
  305. * Deletes the given folder
  306. */
  307. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  308. global $data_dir, $username;
  309. sqimap_unsubscribe ($imap_stream, $mailbox);
  310. $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
  311. sqimap_encode_mailbox_name($mailbox),
  312. true, $response, $message);
  313. if ($response !== 'OK') {
  314. // subscribe again
  315. sqimap_subscribe ($imap_stream, $mailbox);
  316. } else {
  317. do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
  318. removePref($data_dir, $username, "thread_$mailbox");
  319. }
  320. }
  321. /**
  322. * Determines if the user is subscribed to the folder or not
  323. */
  324. function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
  325. $boxesall = sqimap_mailbox_list ($imap_stream);
  326. foreach ($boxesall as $ref) {
  327. if ($ref['unformatted'] == $folder) {
  328. return true;
  329. }
  330. }
  331. return false;
  332. }
  333. /**
  334. * Renames a mailbox.
  335. */
  336. function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  337. if ( $old_name != $new_name ) {
  338. global $delimiter, $imap_server_type, $data_dir, $username;
  339. if ( substr( $old_name, -1 ) == $delimiter ) {
  340. $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
  341. $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
  342. $postfix = $delimiter;
  343. } else {
  344. $postfix = '';
  345. }
  346. $boxesall = sqimap_mailbox_list($imap_stream);
  347. $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
  348. ' ' . sqimap_encode_mailbox_name($new_name);
  349. $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
  350. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  351. $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
  352. removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
  353. sqimap_subscribe($imap_stream, $new_name.$postfix);
  354. setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
  355. do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
  356. $l = strlen( $old_name ) + 1;
  357. $p = 'unformatted';
  358. foreach ($boxesall as $box) {
  359. if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
  360. $new_sub = $new_name . $delimiter . substr($box[$p], $l);
  361. if ($imap_server_type == 'cyrus') {
  362. $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
  363. $data = sqimap_run_command($imap_stream, $cmd, true,
  364. $response, $message);
  365. }
  366. sqimap_unsubscribe($imap_stream, $box[$p]);
  367. $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
  368. removePref($data_dir, $username, 'thread_'.$box[$p]);
  369. sqimap_subscribe($imap_stream, $new_sub);
  370. setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
  371. do_hook_function('rename_or_delete_folder',
  372. $args = array($box[$p], 'rename', $new_sub));
  373. }
  374. }
  375. }
  376. }
  377. /**
  378. * Formats a mailbox into parts for the $boxesall array
  379. *
  380. * The parts are:
  381. *
  382. * raw - Raw LIST/LSUB response from the IMAP server
  383. * formatted - nicely formatted folder name
  384. * unformatted - unformatted, but with delimiter at end removed
  385. * unformatted-dm - folder name as it appears in raw response
  386. * unformatted-disp - unformatted without $folder_prefix
  387. */
  388. function sqimap_mailbox_parse ($line, $line_lsub) {
  389. global $folder_prefix, $delimiter;
  390. /* Process each folder line */
  391. for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
  392. /* Store the raw IMAP reply */
  393. if (isset($line[$g])) {
  394. $boxesall[$g]['raw'] = $line[$g];
  395. } else {
  396. $boxesall[$g]['raw'] = '';
  397. }
  398. /* Count number of delimiters ($delimiter) in folder name */
  399. $mailbox = /*trim(*/$line_lsub[$g]/*)*/;
  400. $dm_count = substr_count($mailbox, $delimiter);
  401. if (substr($mailbox, -1) == $delimiter) {
  402. /* If name ends in delimiter, decrement count by one */
  403. $dm_count--;
  404. }
  405. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  406. $boxesallbyname[$mailbox] = $g;
  407. $parentfolder = readMailboxParent($mailbox, $delimiter);
  408. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  409. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  410. (isset($boxesallbyname[$parentfolder]) &&
  411. (strlen($parentfolder) > 0) ) ) {
  412. $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
  413. if ($indent > 0) {
  414. $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
  415. } else {
  416. $boxesall[$g]['formatted'] = '';
  417. }
  418. $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  419. } else {
  420. $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  421. }
  422. $boxesall[$g]['unformatted-dm'] = $mailbox;
  423. if (substr($mailbox, -1) == $delimiter) {
  424. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  425. }
  426. $boxesall[$g]['unformatted'] = $mailbox;
  427. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  428. $mailbox = substr($mailbox, strlen($folder_prefix));
  429. }
  430. $boxesall[$g]['unformatted-disp'] = $mailbox;
  431. $boxesall[$g]['id'] = $g;
  432. $boxesall[$g]['flags'] = array();
  433. if (isset($line[$g])) {
  434. ereg("\(([^)]*)\)",$line[$g],$regs);
  435. // FIXME Flags do contain the \ character. \NoSelect \NoInferiors
  436. // and $MDNSent <= last one doesn't have the \
  437. // It's better to follow RFC3501 instead of using our own naming.
  438. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  439. if ($flags) {
  440. $boxesall[$g]['flags'] = explode(' ', $flags);
  441. }
  442. }
  443. }
  444. return $boxesall;
  445. }
  446. /**
  447. * Returns list of options (to be echoed into select statement
  448. * based on available mailboxes and separators
  449. * Caller should surround options with <SELECT..> </SELECT> and
  450. * any formatting.
  451. * $imap_stream - $imapConnection to query for mailboxes
  452. * $show_selected - array containing list of mailboxes to pre-select (0 if none)
  453. * $folder_skip - array of folders to keep out of option list (compared in lower)
  454. * $boxes - list of already fetched boxes (for places like folder panel, where
  455. * you know these options will be shown 3 times in a row.. (most often unset).
  456. * $flag - flag to check for in mailbox flags, used to filter out mailboxes.
  457. * 'noselect' by default to remove unselectable mailboxes.
  458. * 'noinferiors' used to filter out folders that can not contain subfolders.
  459. * NULL to avoid flag check entirely.
  460. * NOTE: noselect and noiferiors are used internally. The IMAP representation is
  461. * \NoSelect and \NoInferiors
  462. * $use_long_format - override folder display preference and always show full folder name.
  463. */
  464. function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
  465. $flag = 'noselect', $use_long_format = false ) {
  466. global $username, $data_dir;
  467. $mbox_options = '';
  468. if ( $use_long_format ) {
  469. $shorten_box_names = 0;
  470. } else {
  471. $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
  472. }
  473. if ($boxes == 0) {
  474. $boxes = sqimap_mailbox_list($imap_stream);
  475. }
  476. foreach ($boxes as $boxes_part) {
  477. if ($flag == NULL || !in_array($flag, $boxes_part['flags'])) {
  478. $box = $boxes_part['unformatted'];
  479. if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
  480. continue;
  481. }
  482. $lowerbox = strtolower($box);
  483. // mailboxes are casesensitive => inbox.sent != inbox.Sent
  484. // nevermind, to many dependencies this should be fixed!
  485. if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
  486. $box2 = _("INBOX");
  487. } else {
  488. switch ($shorten_box_names)
  489. {
  490. case 2: /* delimited, style = 2 */
  491. $box2 = str_replace('&nbsp;&nbsp;', '.&nbsp;', $boxes_part['formatted']);
  492. break;
  493. case 1: /* indent, style = 1 */
  494. $box2 = $boxes_part['formatted'];
  495. break;
  496. default: /* default, long names, style = 0 */
  497. $box2 = str_replace(' ', '&nbsp;', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
  498. break;
  499. }
  500. }
  501. if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
  502. $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'" SELECTED>'.$box2.'</OPTION>' . "\n";
  503. } else {
  504. $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'">'.$box2.'</OPTION>' . "\n";
  505. }
  506. }
  507. }
  508. return $mbox_options;
  509. }
  510. /**
  511. * Returns sorted mailbox lists in several different ways.
  512. * See comment on sqimap_mailbox_parse() for info about the returned array.
  513. */
  514. function sqimap_mailbox_list($imap_stream) {
  515. global $default_folder_prefix;
  516. if (!isset($boxesnew)) {
  517. global $data_dir, $username, $list_special_folders_first,
  518. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  519. $move_to_trash, $move_to_sent, $save_as_draft,
  520. $delimiter, $noselect_fix_enable;
  521. $inbox_in_list = false;
  522. $inbox_subscribed = false;
  523. require_once(SM_PATH . 'include/load_prefs.php');
  524. if ($noselect_fix_enable) {
  525. $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
  526. } else {
  527. $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
  528. }
  529. /* LSUB array */
  530. $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
  531. true, $response, $message);
  532. $lsub_ary = compact_mailboxes_response($lsub_ary);
  533. $sorted_lsub_ary = array();
  534. for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
  535. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  536. $sorted_lsub_ary[] = $temp_mailbox_name;
  537. if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
  538. $inbox_subscribed = true;
  539. }
  540. }
  541. /* natural sort mailboxes */
  542. if (isset($sorted_lsub_ary)) {
  543. usort($sorted_lsub_ary, 'strnatcasecmp');
  544. }
  545. /*
  546. * The LSUB response doesn't provide us information about \Noselect
  547. * mail boxes. The LIST response does, that's why we need to do a LIST
  548. * call to retrieve the flags for the mailbox
  549. * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
  550. * in other words, we cannot rely on it.
  551. */
  552. $sorted_list_ary = array();
  553. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  554. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  555. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  556. }
  557. else {
  558. $mbx = $sorted_lsub_ary[$i];
  559. }
  560. $mbx = stripslashes($mbx);
  561. $read = sqimap_run_command ($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mbx),
  562. true, $response, $message);
  563. $read = compact_mailboxes_response($read);
  564. if (isset($read[0])) {
  565. $sorted_list_ary[$i] = $read[0];
  566. } else {
  567. $sorted_list_ary[$i] = '';
  568. }
  569. }
  570. /*
  571. * Just in case they're not subscribed to their inbox,
  572. * we'll get it for them anyway
  573. */
  574. if (!$inbox_subscribed) {
  575. $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" INBOX',
  576. true, $response, $message);
  577. $sorted_list_ary[] = implode('', compact_mailboxes_response($inbox_ary));
  578. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  579. }
  580. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  581. /* Now, lets sort for special folders */
  582. $boxesnew = $used = array();
  583. /* Find INBOX */
  584. $cnt = count($boxesall);
  585. $used = array_pad($used,$cnt,false);
  586. for($k = 0; $k < $cnt; ++$k) {
  587. if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
  588. $boxesnew[] = $boxesall[$k];
  589. $used[$k] = true;
  590. break;
  591. }
  592. }
  593. /* List special folders and their subfolders, if requested. */
  594. if ($list_special_folders_first) {
  595. for($k = 0; $k < $cnt; ++$k) {
  596. if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
  597. $boxesnew[] = $boxesall[$k];
  598. $used[$k] = true;
  599. }
  600. }
  601. }
  602. /* Rest of the folders */
  603. for($k = 0; $k < $cnt; $k++) {
  604. if (!$used[$k]) {
  605. $boxesnew[] = $boxesall[$k];
  606. }
  607. }
  608. }
  609. return $boxesnew;
  610. }
  611. /**
  612. * Returns a list of all folders, subscribed or not
  613. */
  614. function sqimap_mailbox_list_all($imap_stream) {
  615. global $list_special_folders_first, $folder_prefix, $delimiter;
  616. $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false);
  617. $read_ary = compact_mailboxes_response($read_ary);
  618. $g = 0;
  619. $phase = 'inbox';
  620. $fld_pre_length = strlen($folder_prefix);
  621. for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
  622. /* Store the raw IMAP reply */
  623. $boxes[$g]['raw'] = $read_ary[$i];
  624. /* Count number of delimiters ($delimiter) in folder name */
  625. $mailbox = find_mailbox_name($read_ary[$i]);
  626. $dm_count = substr_count($mailbox, $delimiter);
  627. if (substr($mailbox, -1) == $delimiter) {
  628. /* If name ends in delimiter - decrement count by one */
  629. $dm_count--;
  630. }
  631. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  632. $boxesallbyname[$mailbox] = $g;
  633. $parentfolder = readMailboxParent($mailbox, $delimiter);
  634. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  635. (ereg('^'.$folder_prefix, $mailbox)) ||
  636. ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  637. if ($dm_count) {
  638. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  639. } else {
  640. $boxes[$g]['formatted'] = '';
  641. }
  642. $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  643. } else {
  644. $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  645. }
  646. $boxes[$g]['unformatted-dm'] = $mailbox;
  647. if (substr($mailbox, -1) == $delimiter) {
  648. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  649. }
  650. $boxes[$g]['unformatted'] = $mailbox;
  651. $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
  652. $boxes[$g]['id'] = $g;
  653. /* Now lets get the flags for this mailbox */
  654. $read_mlbx = $read_ary[$i];
  655. $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
  656. $flags = substr($flags, 0, strpos($flags, ')'));
  657. $flags = str_replace('\\', '', $flags);
  658. $flags = trim(strtolower($flags));
  659. if ($flags) {
  660. $boxes[$g]['flags'] = explode(' ', $flags);
  661. } else {
  662. $boxes[$g]['flags'] = array();
  663. }
  664. $g++;
  665. }
  666. if(is_array($boxes)) {
  667. sort ($boxes);
  668. }
  669. return $boxes;
  670. }
  671. function sqimap_mailbox_tree($imap_stream) {
  672. global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
  673. if (!isset($boxesnew)) {
  674. global $data_dir, $username, $list_special_folders_first,
  675. $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
  676. $imap_server_type;
  677. $inbox_in_list = false;
  678. $inbox_subscribed = false;
  679. $noselect = false;
  680. $noinferiors = false;
  681. require_once(SM_PATH . 'include/load_prefs.php');
  682. /* LSUB array */
  683. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  684. true, $response, $message);
  685. $lsub_ary = compact_mailboxes_response($lsub_ary);
  686. /* Check to see if we have an INBOX */
  687. $has_inbox = false;
  688. for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
  689. if (preg_match("/^\*\s+LSUB\s+(.*)\"?INBOX\"?[^(\/\.)].*$/i",$lsub_ary[$i])) {
  690. $lsub_ary[$i] = strtoupper($lsub_ary[$i]);
  691. $has_inbox = true;
  692. break;
  693. }
  694. }
  695. if ($has_inbox == false) {
  696. $lsub_ary[] = '* LSUB () NIL INBOX';
  697. }
  698. /*
  699. * Section about removing the last element was removed
  700. * We don't return "* OK" anymore from sqimap_read_data
  701. */
  702. $sorted_lsub_ary = array();
  703. $cnt = count($lsub_ary);
  704. for ($i = 0; $i < $cnt; $i++) {
  705. $mbx = find_mailbox_name($lsub_ary[$i]);
  706. // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
  707. if ($imap_server_type != "uw") {
  708. $noselect = check_is_noselect($lsub_ary[$i]);
  709. $noinferiors = check_is_noinferiors($lsub_ary[$i]);
  710. }
  711. if (substr($mbx, -1) == $delimiter) {
  712. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  713. }
  714. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
  715. }
  716. // FIX ME this requires a config setting inside conf.pl instead of checking on server type
  717. if ($imap_server_type == "uw") {
  718. $aQuery = array();
  719. $aTag = array();
  720. // prepare an array with queries
  721. foreach ($sorted_lsub_ary as $aMbx) {
  722. $mbx = stripslashes($aMbx['mbx']);
  723. sqimap_prepare_pipelined_query('LIST "" ' . sqimap_encode_mailbox_name($mbx), $tag, $aQuery, false);
  724. $aTag[$tag] = $mbx;
  725. }
  726. $sorted_lsub_ary = array();
  727. // execute all the queries at once
  728. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  729. foreach($aTag as $tag => $mbx) {
  730. if ($aServerResponse[$tag] == 'OK') {
  731. $sResponse = implode('', $aResponse[$tag]);
  732. $noselect = check_is_noselect($sResponse);
  733. $noinferiors = check_is_noinferiors($sResponse);
  734. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
  735. }
  736. }
  737. $cnt = count($sorted_lsub_ary);
  738. }
  739. $sorted_lsub_ary = array_values($sorted_lsub_ary);
  740. array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
  741. $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
  742. return $boxesnew;
  743. }
  744. }
  745. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
  746. global $data_dir, $username, $list_special_folders_first,
  747. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  748. $move_to_trash, $move_to_sent, $save_as_draft,
  749. $delimiter, $imap_server_type;
  750. $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  751. /* create virtual root node */
  752. $mailboxes= new mailboxes();
  753. $mailboxes->is_root = true;
  754. $trail_del = false;
  755. $start = 0;
  756. if (isset($folder_prefix) && ($folder_prefix != '')) {
  757. $start = substr_count($folder_prefix,$delimiter);
  758. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  759. $trail_del = true;
  760. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  761. } else {
  762. $mailboxes->mailboxname_full = $folder_prefix;
  763. $start++;
  764. }
  765. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  766. } else {
  767. $start = 0;
  768. }
  769. $cnt = count($mbx_ary);
  770. for ($i=0; $i < $cnt; $i++) {
  771. if ($mbx_ary[$i]['mbx'] !='' ) {
  772. $mbx = new mailboxes();
  773. $mailbox = $mbx_ary[$i]['mbx'];
  774. /*
  775. sent subfolders messes up using existing code as subfolders
  776. were marked, but the parents were ordered somewhere else in
  777. the list, despite having "special folders at top" option set.
  778. Need a better method than this.
  779. */
  780. /*
  781. if ($mailbox == 'INBOX') {
  782. $mbx->is_special = true;
  783. } elseif (stristr($trash_folder , $mailbox)) {
  784. $mbx->is_special = true;
  785. } elseif (stristr($sent_folder , $mailbox)) {
  786. $mbx->is_special = true;
  787. } elseif (stristr($draft_folder , $mailbox)) {
  788. $mbx->is_special = true;
  789. }
  790. switch ($mailbox) {
  791. case 'INBOX':
  792. $mbx->is_inbox = true;
  793. $mbx->is_special = true;
  794. $mbx_ary[$i]['noselect'] = false;
  795. break;
  796. case $trash_folder:
  797. $mbx->is_trash = true;
  798. $mbx->is_special = true;
  799. break;
  800. case $sent_folder:
  801. $mbx->is_sent = true;
  802. $mbx->is_special = true;
  803. break;
  804. case $draft_folder:
  805. $mbx->is_draft = true;
  806. $mbx->is_special = true;
  807. break;
  808. }
  809. */
  810. $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
  811. $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
  812. $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
  813. $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
  814. if (!$mbx->is_special)
  815. $mbx->is_special = do_hook_function('special_mailbox', $mailbox);
  816. if (isset($mbx_ary[$i]['unseen'])) {
  817. $mbx->unseen = $mbx_ary[$i]['unseen'];
  818. }
  819. if (isset($mbx_ary[$i]['nummessages'])) {
  820. $mbx->total = $mbx_ary[$i]['nummessages'];
  821. }
  822. $mbx->is_noselect = $mbx_ary[$i]['noselect'];
  823. $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
  824. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  825. if ($r_del_pos) {
  826. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  827. } else { /* mailbox is root folder */
  828. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  829. }
  830. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  831. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  832. }
  833. }
  834. sqimap_utf7_decode_mbx_tree($mailboxes);
  835. sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
  836. return $mailboxes;
  837. }
  838. function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
  839. if (strtoupper($mbx_tree->mailboxname_sub) == 'INBOX')
  840. $mbx_tree->mailboxname_sub = _("INBOX");
  841. else
  842. $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
  843. if ($mbx_tree->mbxs) {
  844. $iCnt = count($mbx_tree->mbxs);
  845. for ($i=0;$i<$iCnt;++$i) {
  846. $mbxs_tree->mbxs[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
  847. }
  848. }
  849. }
  850. function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
  851. if ($mbx_tree)
  852. $aMbxs[] =& $mbx_tree;
  853. if ($mbx_tree->mbxs) {
  854. $iCnt = count($mbx_tree->mbxs);
  855. for ($i=0;$i<$iCnt;++$i) {
  856. sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
  857. }
  858. }
  859. }
  860. function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
  861. global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
  862. $aMbxs = $aQuery = $aTag = array();
  863. sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
  864. // remove the root node
  865. array_shift($aMbxs);
  866. if($unseen_notify == 3) {
  867. $cnt = count($aMbxs);
  868. for($i=0;$i<$cnt;++$i) {
  869. $oMbx =& $aMbxs[$i];
  870. if (!$oMbx->is_noselect) {
  871. $mbx = $oMbx->mailboxname_full;
  872. if ($unseen_type == 2 ||
  873. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  874. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
  875. } else {
  876. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
  877. }
  878. sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
  879. } else {
  880. $oMbx->unseen = $oMbx->total = false;
  881. $tag = false;
  882. }
  883. $oMbx->tag = $tag;
  884. $aMbxs[$i] =& $oMbx;
  885. }
  886. // execute all the queries at once
  887. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  888. $cnt = count($aMbxs);
  889. for($i=0;$i<$cnt;++$i) {
  890. $oMbx =& $aMbxs[$i];
  891. $tag = $oMbx->tag;
  892. if ($tag && $aServerResponse[$tag] == 'OK') {
  893. $sResponse = implode('', $aResponse[$tag]);
  894. if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
  895. $oMbx->unseen = $regs[1];
  896. }
  897. if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
  898. $oMbx->total = $regs[1];
  899. }
  900. }
  901. unset($oMbx->tag);
  902. }
  903. } else if ($unseen_notify == 2) { // INBOX only
  904. $cnt = count($aMbxs);
  905. for($i=0;$i<$cnt;++$i) {
  906. $oMbx =& $aMbxs[$i];
  907. if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
  908. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  909. if ($unseen_type == 2 ||
  910. ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
  911. $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
  912. $oMbx->unseen = $aStatus['UNSEEN'];
  913. $oMbx->total = $aStatus['MESSAGES'];
  914. } else {
  915. $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
  916. }
  917. $aMbxs[$i] =& $oMbx;
  918. if (!$move_to_trash && $trash_folder) {
  919. break;
  920. } else {
  921. // trash comes after INBOX
  922. if ($oMbx->mailboxname_full == $trash_folder) {
  923. break;
  924. }
  925. }
  926. }
  927. }
  928. }
  929. }
  930. ?>