imap_mailbox.php 36 KB

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