6 ** Copyright (c) 1999-2001 The Squirrelmail Development Team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
9 ** This is the code for the left bar. The left bar shows the folders
10 ** available, and has cookie information.
15 require_once('../src/validate.php');
16 require_once('../functions/array.php');
17 require_once('../functions/imap.php');
18 require_once('../functions/plugin.php');
19 require_once('../functions/page_header.php');
21 /* These constants are used for folder stuff. */
22 define('SM_BOX_UNCOLLAPSED', 0);
23 define('SM_BOX_COLLAPSED', 1);
25 /* --------------------- FUNCTIONS ------------------------- */
27 function formatMailboxName($imapConnection, $box_array) {
28 global $folder_prefix, $trash_folder, $sent_folder;
29 global $color, $move_to_sent, $move_to_trash;
30 global $unseen_notify, $unseen_type, $collapse_folders;
31 global $draft_folder, $save_as_draft;
32 global $use_special_folder_color;
34 $real_box = $box_array['unformatted'];
35 $mailbox = str_replace(' ','',$box_array['formatted']);
36 $mailboxURL = urlencode($real_box);
38 /* Strip down the mailbox name. */
39 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
45 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
46 $unseen_notify == 3) {
47 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
48 if ($unseen_type == 1 && $unseen > 0) {
49 $unseen_string = "($unseen)";
51 } else if ($unseen_type == 2) {
52 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
53 $unseen_string = "<font color=\"$color[11]\">($unseen/$numMessages)</font>";
58 $special_color = false;
59 if ($use_special_folder_color) {
60 if ((strtolower($real_box) == 'inbox')
61 ||
(($real_box == $trash_folder) && ($move_to_trash))
62 ||
(($real_box == $sent_folder) && ($move_to_sent))
63 ||
(($real_box == $draft_folder) && ($save_as_draft))) {
64 $special_color = true;
68 /* Start off with a blank line. */
71 /* If there are unseen message, bold the line. */
72 if ($unseen > 0) { $line .= '<B>'; }
74 /* Crate the link for this folder. */
75 $line .= "<A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$mailboxURL\" TARGET=\"right\" STYLE=\"text-decoration:none\">";
76 if ($special_color == true)
77 $line .= "<FONT COLOR=\"$color[11]\">";
78 $line .= str_replace(' ',' ',$mailbox);
79 if ($special_color == true)
83 /* If there are unseen message, close bolding. */
84 if ($unseen > 0) { $line .= "</B>"; }
86 /* Print unseen information. */
87 if (isset($unseen_found) && $unseen_found) {
88 $line .= " <SMALL>$unseen_string</SMALL>";
91 if (($move_to_trash == true) && ($real_box == $trash_folder)) {
92 if (! isset($numMessages)) {
93 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
96 if ($numMessages > 0) {
97 $urlMailbox = urlencode($real_box);
98 $line .= "\n<small>\n" .
99 " (<A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("empty")."</A>)" .
104 /* Return the final product. */
109 * Recursive function that computes the collapsed status and parent
110 * (or not parent) status of this box, and the visiblity and collapsed
111 * status and parent (or not parent) status for all children boxes.
113 function compute_folder_children(&$parbox, $boxcount) {
114 global $boxes, $data_dir, $username, $collapse_folders;
115 $nextbox = $parbox +
1;
117 /* Retreive the name for the parent box. */
118 $parbox_name = $boxes[$parbox]['unformatted'];
120 /* 'Initialize' this parent box to childless. */
121 $boxes[$parbox]['parent'] = false;
123 /* Compute the collapse status for this box. */
124 if( isset($collapse_folders) && $collapse_folders ) {
125 $collapse = getPref($data_dir, $username, 'collapse_folder_' . $parbox_name);
126 $collapse = ($collapse == '' ? SM_BOX_UNCOLLAPSED
: $collapse);
128 $collapse = SM_BOX_UNCOLLAPSED
;
130 $boxes[$parbox]['collapse'] = $collapse;
132 /* Otherwise, get the name of the next box. */
133 if (isset($boxes[$nextbox]['unformatted']))
134 $nextbox_name = $boxes[$nextbox]['unformatted'];
138 /* Compute any children boxes for this box. */
139 while (($nextbox < $boxcount) &&
140 (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) {
142 /* Note that this 'parent' box has at least one child. */
143 $boxes[$parbox]['parent'] = true;
145 /* Compute the visiblity of this box. */
146 if ($boxes[$parbox]['visible'] &&
147 ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED
)) {
148 $boxes[$nextbox]['visible'] = true;
150 $boxes[$nextbox]['visible'] = false;
153 /* Compute the visibility of any child boxes. */
154 compute_folder_children($nextbox, $boxcount);
157 /* Set the parent box to the current next box. */
162 * Create the link for a parent folder that will allow that
163 * parent folder to either be collapsed or expaned, as is
164 * currently appropriate.
166 function create_collapse_link($boxnum) {
168 $mailbox = urlencode($boxes[$boxnum]['unformatted']);
170 /* Create the link for this collapse link. */
171 $link = '<a target="left" style="text-decoration:none" ';
172 $link .= 'href="left_main.php?';
173 if ($boxes[$boxnum]['collapse'] == SM_BOX_COLLAPSED
) {
174 $link .= "unfold=$mailbox\">+";
176 $link .= "fold=$mailbox\">-";
180 /* Return the finished product. */
185 * This simple function checks if a box is another box's parent.
187 function is_parent_box($curbox_name, $parbox_name) {
190 /* Extract the name of the parent of the current box. */
191 $curparts = explode($delimiter, $curbox_name);
192 $curname = array_pop($curparts);
193 $actual_parname = implode($delimiter, $curparts);
194 $actual_parname = substr($actual_parname,0,strlen($parbox_name));
196 /* Compare the actual with the given parent name. */
197 return ($parbox_name == $actual_parname);
201 /* -------------------- MAIN ------------------------ */
203 global $delimiter, $default_folder_prefix;
205 // open a connection on the imap port (143)
206 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); // the 10 is to hide the output
209 if (isset($left_refresh) && ($left_refresh != 'none') && ($left_refresh != '')) {
210 $xtra = "\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"Thu, 01 Dec 1994 16:00:00 GMT\">\n" .
211 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n".
212 "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"$left_refresh;URL=left_main.php\">\n";
217 displayHtmlHeader( 'SquirrelMail', $xtra );
219 /* If requested and not yet complete, attempt to autocreate folders. */
220 if ($auto_create_special && !isset($auto_create_done)) {
221 $autocreate = array( $sent_folder,
224 foreach( $autocreate as $folder ) {
225 if ($folder != '' && $folder != 'none') {
226 if ( !sqimap_mailbox_exists($imapConnection, $folder)) {
227 sqimap_mailbox_create($imapConnection, $default_folder_prefix.$folder, '');
228 } elseif ( !sqimap_mailbox_is_subscribed($imapConnection, $folder)) {
229 sqimap_subscribe($imapConnection, $folder);
234 /* Let the world know that autocreation is complete! Hurrah! */
235 $auto_create_done = true;
236 session_register('auto_create_done');
239 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
241 do_hook('left_main_before');
243 $boxes = sqimap_mailbox_list($imapConnection);
245 echo '<CENTER><FONT SIZE=4><B>'. _("Folders") . "</B><BR></FONT>\n\n";
247 if ($date_format != 6) {
248 /* First, display the clock. */
249 if ($hour_format == 1) {
251 if ($date_format == 4) {
255 if ($date_format == 4) {
262 switch( $date_format ) {
264 $clk = date('m/d/y '.$hr, time());
267 $clk = date('d/m/y '.$hr, time());
271 $clk = date($hr, time());
274 $clk = date('D, '.$hr, time());
276 $clk = str_replace(' ',' ',$clk);
278 echo '<CENTER><SMALL>' . str_replace(' ',' ',_("Last Refresh")) .
279 ": $clk</SMALL></CENTER>";
282 /* Next, display the refresh button. */
283 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">'.
284 _("refresh folder list") . '</A>)</SMALL></CENTER><BR>';
286 /* Lastly, display the folder list. */
287 if ( $collapse_folders ) {
288 /* If directed, collapse or uncollapse a folder. */
290 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED
);
291 } else if (isset($unfold)) {
292 setPref($data_dir, $username, 'collapse_folder_' . $unfold, SM_BOX_UNCOLLAPSED
);
296 /* Prepare do do out collapsedness and visibility computation. */
298 $boxcount = count($boxes);
300 /* Compute the collapsedness and visibility of each box. */
301 while ($curbox < $boxcount) {
302 $boxes[$curbox]['visible'] = TRUE;
303 compute_folder_children($curbox, $boxcount);
306 for ($i = 0;$i < count($boxes); $i++
) {
307 if ( $boxes[$i]['visible'] ) {
308 $mailbox = $boxes[$i]['formatted'];
309 $mblevel = substr_count($boxes[$i]['unformatted'], $delimiter) +
1;
311 /* Create the prefix for the folder name and link. */
312 $prefix = str_repeat(' ',$mblevel);
313 if (isset($collapse_folders) && $collapse_folders && $boxes[$i]['parent']) {
314 $prefix = str_replace(' ',' ',substr($prefix,0,strlen($prefix)-2)).
315 create_collapse_link($i) . ' ';
317 $prefix = str_replace(' ',' ',$prefix);
319 $line = "<NOBR><TT>$prefix</TT>";
321 /* Add the folder name and link. */
322 if (in_array('noselect', $boxes[$i]['flags'])) {
323 $line .= "<FONT COLOR=\"$color[10]\">";
324 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
325 $mailbox = str_replace(' ','',$mailbox);
326 $line .= str_replace(' ', ' ', $mailbox);
330 $line .= formatMailboxName($imapConnection, $boxes[$i]);
333 /* Put the final touches on our folder line. */
334 $line .= "</NOBR><BR>\n";
336 /* Output the line for this folder. */
341 sqimap_logout($imapConnection);
342 do_hook('left_main_after');
344 echo "</BODY></HTML>\n";