imap_mailbox.php 37 KB

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