6 * Copyright (c) 1999-2002 The SquirrelMail Project 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) {
29 global $folder_prefix, $trash_folder, $sent_folder,
30 $color, $move_to_sent, $move_to_trash,
31 $unseen_notify, $unseen_type, $collapse_folders,
32 $draft_folder, $save_as_draft,
33 $use_special_folder_color;
35 $real_box = $box_array['unformatted'];
36 $mailbox = str_replace(' ','',$box_array['formatted']);
37 $mailboxURL = urlencode($real_box);
39 /* Strip down the mailbox name. */
40 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
46 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
47 $unseen_notify == 3) {
48 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
49 if ($unseen_type == 1 && $unseen > 0) {
50 $unseen_string = "($unseen)";
52 } else if ($unseen_type == 2) {
53 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
54 $unseen_string = "<font color=\"$color[11]\">($unseen/$numMessages)</font>";
59 $special_color = ($use_special_folder_color && isSpecialMailbox( $real_box ) );
61 /* Start off with a blank line. */
64 /* If there are unseen message, bold the line. */
65 if ($unseen > 0) { $line .= '<B>'; }
67 /* Crate the link for this folder. */
68 $line .= "<A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$mailboxURL\" TARGET=\"right\" STYLE=\"text-decoration:none\">";
70 $line .= "<FONT COLOR=\"$color[11]\">";
72 if ( $mailbox == 'INBOX' ) {
75 $line .= str_replace(' ',' ',$mailbox);
77 if ($special_color == TRUE)
81 /* If there are unseen message, close bolding. */
82 if ($unseen > 0) { $line .= "</B>"; }
84 /* Print unseen information. */
85 if (isset($unseen_found) && $unseen_found) {
86 $line .= " <SMALL>$unseen_string</SMALL>";
89 if (($move_to_trash) && ($real_box == $trash_folder)) {
90 if (! isset($numMessages)) {
91 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
94 if ($numMessages > 0) {
95 $urlMailbox = urlencode($real_box);
96 $line .= "\n<small>\n" .
97 " (<A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("empty")."</A>)" .
102 /* Return the final product. */
107 * Recursive function that computes the collapsed status and parent
108 * (or not parent) status of this box, and the visiblity and collapsed
109 * status and parent (or not parent) status for all children boxes.
111 function compute_folder_children(&$parbox, $boxcount) {
112 global $boxes, $data_dir, $username, $collapse_folders;
113 $nextbox = $parbox +
1;
115 /* Retreive the name for the parent box. */
116 $parbox_name = $boxes[$parbox]['unformatted'];
118 /* 'Initialize' this parent box to childless. */
119 $boxes[$parbox]['parent'] = FALSE;
121 /* Compute the collapse status for this box. */
122 if( isset($collapse_folders) && $collapse_folders ) {
123 $collapse = getPref($data_dir, $username, 'collapse_folder_' . $parbox_name);
124 $collapse = ($collapse == '' ? SM_BOX_UNCOLLAPSED
: $collapse);
126 $collapse = SM_BOX_UNCOLLAPSED
;
128 $boxes[$parbox]['collapse'] = $collapse;
130 /* Otherwise, get the name of the next box. */
131 if (isset($boxes[$nextbox]['unformatted'])) {
132 $nextbox_name = $boxes[$nextbox]['unformatted'];
137 /* Compute any children boxes for this box. */
138 while (($nextbox < $boxcount) &&
139 (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) {
141 /* Note that this 'parent' box has at least one child. */
142 $boxes[$parbox]['parent'] = TRUE;
144 /* Compute the visiblity of this box. */
145 $boxes[$nextbox]['visible'] = ($boxes[$parbox]['visible'] &&
146 ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED
));
148 /* Compute the visibility of any child boxes. */
149 compute_folder_children($nextbox, $boxcount);
152 /* Set the parent box to the current next box. */
157 * Create the link for a parent folder that will allow that
158 * parent folder to either be collapsed or expaned, as is
159 * currently appropriate.
161 function create_collapse_link($boxnum) {
163 $mailbox = urlencode($boxes[$boxnum]['unformatted']);
165 /* Create the link for this collapse link. */
166 $link = '<a target="left" style="text-decoration:none" ' .
167 'href="left_main.php?';
168 if ($boxes[$boxnum]['collapse'] == SM_BOX_COLLAPSED
) {
169 $link .= "unfold=$mailbox\">+";
171 $link .= "fold=$mailbox\">-";
175 /* Return the finished product. */
180 * This simple function checks if a box is another box's parent.
182 function is_parent_box($curbox_name, $parbox_name) {
185 /* Extract the name of the parent of the current box. */
186 $curparts = explode($delimiter, $curbox_name);
187 $curname = array_pop($curparts);
188 $actual_parname = implode($delimiter, $curparts);
189 $actual_parname = substr($actual_parname,0,strlen($parbox_name));
191 /* Compare the actual with the given parent name. */
192 return ($parbox_name == $actual_parname);
196 /* -------------------- MAIN ------------------------ */
198 global $delimiter, $default_folder_prefix;
200 // open a connection on the imap port (143)
201 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); // the 10 is to hide the output
204 if (isset($left_refresh) && ($left_refresh != 'none') && ($left_refresh != '')) {
205 $xtra = "\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"Thu, 01 Dec 1994 16:00:00 GMT\">\n" .
206 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n".
207 "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"$left_refresh;URL=left_main.php\">\n";
212 displayHtmlHeader( 'SquirrelMail', $xtra );
214 /* If requested and not yet complete, attempt to autocreate folders. */
215 if ($auto_create_special && !isset($auto_create_done)) {
216 $autocreate = array($sent_folder, $trash_folder, $draft_folder);
217 foreach( $autocreate as $folder ) {
218 if (($folder != '') && ($folder != 'none')) {
219 if ( !sqimap_mailbox_exists($imapConnection, $folder)) {
220 sqimap_mailbox_create($imapConnection, $folder, '');
221 } else if (!sqimap_mailbox_is_subscribed($imapConnection, $folder)) {
222 sqimap_subscribe($imapConnection, $folder);
227 /* Let the world know that autocreation is complete! Hurrah! */
228 $auto_create_done = TRUE;
229 session_register('auto_create_done');
232 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
234 do_hook('left_main_before');
236 $boxes = sqimap_mailbox_list($imapConnection);
238 echo '<CENTER><FONT SIZE=4><B>'. _("Folders") . "</B><BR></FONT>\n\n";
240 if ($date_format != 6) {
241 /* First, display the clock. */
242 if ($hour_format == 1) {
244 if ($date_format == 4) {
248 if ($date_format == 4) {
255 switch( $date_format ) {
257 $clk = date('m/d/y '.$hr, time());
260 $clk = date('d/m/y '.$hr, time());
264 $clk = date($hr, time());
267 $clk = substr( getDayName( date( 'w', time() ) ), 0, 3 ) . date( ', ' . $hr, time() );
269 $clk = str_replace(' ',' ',$clk);
271 echo '<CENTER><SMALL>' . str_replace(' ',' ',_("Last Refresh")) .
272 ": $clk</SMALL></CENTER>";
275 /* Next, display the refresh button. */
276 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">'.
277 _("refresh folder list") . '</A>)</SMALL></CENTER><BR>';
279 /* Lastly, display the folder list. */
280 if ( $collapse_folders ) {
281 /* If directed, collapse or uncollapse a folder. */
283 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED
);
284 } else if (isset($unfold)) {
285 setPref($data_dir, $username, 'collapse_folder_' . $unfold, SM_BOX_UNCOLLAPSED
);
289 /* Prepare do do out collapsedness and visibility computation. */
291 $boxcount = count($boxes);
293 /* Compute the collapsedness and visibility of each box. */
295 while ($curbox < $boxcount) {
296 $boxes[$curbox]['visible'] = TRUE;
297 compute_folder_children($curbox, $boxcount);
301 for ($i = 0; $i < count($boxes); $i++
) {
302 if ( $boxes[$i]['visible'] ) {
303 $mailbox = $boxes[$i]['formatted'];
304 $mblevel = substr_count($boxes[$i]['unformatted'], $delimiter) +
1;
306 /* Create the prefix for the folder name and link. */
307 $prefix = str_repeat(' ',$mblevel);
308 if (isset($collapse_folders) && $collapse_folders && $boxes[$i]['parent']) {
309 $prefix = str_replace(' ',' ',substr($prefix,0,strlen($prefix)-2)).
310 create_collapse_link($i) . ' ';
312 $prefix = str_replace(' ',' ',$prefix);
314 $line = "<NOBR><TT>$prefix</TT>";
316 /* Add the folder name and link. */
317 if (! isset($color[15])) {
318 $color[15] = $color[6];
321 if (in_array('noselect', $boxes[$i]['flags'])) {
322 if( isSpecialMailbox( $boxes[$i]['unformatted']) ) {
323 $line .= "<FONT COLOR=\"$color[11]\">";
325 $line .= "<FONT COLOR=\"$color[15]\">";
327 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
328 $mailbox = str_replace(' ','',$mailbox);
329 $line .= str_replace(' ', ' ', $mailbox);
333 $line .= formatMailboxName($imapConnection, $boxes[$i]);
336 /* Put the final touches on our folder line. */
337 $line .= "</NOBR><BR>\n";
339 /* Output the line for this folder. */
344 sqimap_logout($imapConnection);
345 do_hook('left_main_after');
347 echo "</BODY></HTML>\n";