imap_mailbox.php 36 KB

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