5 * Copyright (c) 1999-2001 The Squirrelmail Development Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * This is the code for the left bar. The left bar shows the folders
9 * available, and has cookie information.
14 require_once('../src/validate.php');
15 require_once('../functions/array.php');
16 require_once('../functions/imap.php');
17 require_once('../functions/plugin.php');
18 require_once('../functions/page_header.php');
20 /* These constants are used for folder stuff. */
21 define('SM_BOX_UNCOLLAPSED', 0);
22 define('SM_BOX_COLLAPSED', 1);
24 /* --------------------- FUNCTIONS ------------------------- */
26 function formatMailboxName($imapConnection, $box_array) {
27 global $folder_prefix, $trash_folder, $sent_folder;
28 global $color, $move_to_sent, $move_to_trash;
29 global $unseen_notify, $unseen_type, $collapse_folders;
30 global $draft_folder, $save_as_draft;
31 global $use_special_folder_color;
33 $real_box = $box_array['unformatted'];
34 $mailbox = str_replace(' ','',$box_array['formatted']);
35 $mailboxURL = urlencode($real_box);
37 /* Strip down the mailbox name. */
38 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
44 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
45 $unseen_notify == 3) {
46 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
47 if ($unseen_type == 1 && $unseen > 0) {
48 $unseen_string = "($unseen)";
50 } else if ($unseen_type == 2) {
51 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
52 $unseen_string = "<font color=\"$color[11]\">($unseen/$numMessages)</font>";
57 $special_color = false;
58 if ($use_special_folder_color) {
59 if ((strtolower($real_box) == 'inbox')
60 ||
(($real_box == $trash_folder) && ($move_to_trash))
61 ||
(($real_box == $sent_folder) && ($move_to_sent))
62 ||
(($real_box == $draft_folder) && ($save_as_draft))) {
63 $special_color = true;
67 /* Start off with a blank line. */
70 /* If there are unseen message, bold the line. */
71 if ($unseen > 0) { $line .= '<B>'; }
73 /* Crate the link for this folder. */
74 $line .= "<A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$mailboxURL\" TARGET=\"right\" STYLE=\"text-decoration:none\">";
75 if ($special_color == true)
76 $line .= "<FONT COLOR=\"$color[11]\">";
77 $line .= str_replace(' ',' ',$mailbox);
78 if ($special_color == true)
82 /* If there are unseen message, close bolding. */
83 if ($unseen > 0) { $line .= "</B>"; }
85 /* Print unseen information. */
86 if (isset($unseen_found) && $unseen_found) {
87 $line .= " <SMALL>$unseen_string</SMALL>";
90 if (($move_to_trash == true) && ($real_box == $trash_folder)) {
91 if (! isset($numMessages)) {
92 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
95 if ($numMessages > 0) {
96 $urlMailbox = urlencode($real_box);
97 $line .= "\n<small>\n" .
98 " (<A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("empty")."</A>)" .
103 /* Return the final product. */
108 * Recursive function that computes the collapsed status and parent
109 * (or not parent) status of this box, and the visiblity and collapsed
110 * status and parent (or not parent) status for all children boxes.
112 function compute_folder_children(&$parbox, $boxcount) {
113 global $boxes, $data_dir, $username, $collapse_folders;
114 $nextbox = $parbox +
1;
116 /* Retreive the name for the parent box. */
117 $parbox_name = $boxes[$parbox]['unformatted'];
119 /* 'Initialize' this parent box to childless. */
120 $boxes[$parbox]['parent'] = false;
122 /* Compute the collapse status for this box. */
123 if( isset($collapse_folders) && $collapse_folders ) {
124 $collapse = getPref($data_dir, $username, 'collapse_folder_' . $parbox_name);
125 $collapse = ($collapse == '' ? SM_BOX_UNCOLLAPSED
: $collapse);
127 $collapse = SM_BOX_UNCOLLAPSED
;
129 $boxes[$parbox]['collapse'] = $collapse;
131 /* Otherwise, get the name of the next box. */
132 if (isset($boxes[$nextbox]['unformatted']))
133 $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 if ($boxes[$parbox]['visible'] &&
146 ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED
)) {
147 $boxes[$nextbox]['visible'] = true;
149 $boxes[$nextbox]['visible'] = false;
152 /* Compute the visibility of any child boxes. */
153 compute_folder_children($nextbox, $boxcount);
156 /* Set the parent box to the current next box. */
161 * Create the link for a parent folder that will allow that
162 * parent folder to either be collapsed or expaned, as is
163 * currently appropriate.
165 function create_collapse_link($boxnum) {
167 $mailbox = urlencode($boxes[$boxnum]['unformatted']);
169 /* Create the link for this collapse link. */
170 $link = '<a target="left" style="text-decoration:none" ';
171 $link .= 'href="left_main.php?';
172 if ($boxes[$boxnum]['collapse'] == SM_BOX_COLLAPSED
) {
173 $link .= "unfold=$mailbox\">+";
175 $link .= "fold=$mailbox\">-";
179 /* Return the finished product. */
184 * This simple function checks if a box is another box's parent.
186 function is_parent_box($curbox_name, $parbox_name) {
189 /* Extract the name of the parent of the current box. */
190 $curparts = explode($delimiter, $curbox_name);
191 $curname = array_pop($curparts);
192 $actual_parname = implode($delimiter, $curparts);
193 $actual_parname = substr($actual_parname,0,strlen($parbox_name));
195 /* Compare the actual with the given parent name. */
196 return ($parbox_name == $actual_parname);
200 /* -------------------- MAIN ------------------------ */
202 global $delimiter, $default_folder_prefix;
204 // open a connection on the imap port (143)
205 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); // the 10 is to hide the output
208 if (isset($left_refresh) && ($left_refresh != 'none') && ($left_refresh != '')) {
209 $xtra = "\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"Thu, 01 Dec 1994 16:00:00 GMT\">\n" .
210 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n".
211 "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"$left_refresh;URL=left_main.php\">\n";
216 displayHtmlHeader( 'SquirrelMail', $xtra );
218 /* If requested and not yet complete, attempt to autocreate folders. */
219 if ($auto_create_special && !isset($auto_create_done)) {
220 $autocreate = array( $sent_folder,
223 foreach( $autocreate as $folder ) {
224 if ($folder != '' && $folder != 'none') {
225 if ( !sqimap_mailbox_exists($imapConnection, $folder)) {
226 sqimap_mailbox_create($imapConnection, $default_folder_prefix.$folder, '');
227 } elseif ( !sqimap_mailbox_is_subscribed($imapConnection, $folder)) {
228 sqimap_subscribe($imapConnection, $folder);
233 /* Let the world know that autocreation is complete! Hurrah! */
234 $auto_create_done = true;
235 session_register('auto_create_done');
238 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
240 do_hook('left_main_before');
242 $boxes = sqimap_mailbox_list($imapConnection);
244 echo '<CENTER><FONT SIZE=4><B>'. _("Folders") . "</B><BR></FONT>\n\n";
246 if ($date_format != 6) {
247 /* First, display the clock. */
248 if ($hour_format == 1) {
250 if ($date_format == 4) {
254 if ($date_format == 4) {
261 switch( $date_format ) {
263 $clk = date('m/d/y '.$hr, time());
266 $clk = date('d/m/y '.$hr, time());
270 $clk = date($hr, time());
273 $clk = date('D, '.$hr, time());
275 $clk = str_replace(' ',' ',$clk);
277 echo '<CENTER><SMALL>' . str_replace(' ',' ',_("Last Refresh")) .
278 ": $clk</SMALL></CENTER>";
281 /* Next, display the refresh button. */
282 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">'.
283 _("refresh folder list") . '</A>)</SMALL></CENTER><BR>';
285 /* Lastly, display the folder list. */
286 if ( $collapse_folders ) {
287 /* If directed, collapse or uncollapse a folder. */
289 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED
);
290 } else if (isset($unfold)) {
291 setPref($data_dir, $username, 'collapse_folder_' . $unfold, SM_BOX_UNCOLLAPSED
);
295 /* Prepare do do out collapsedness and visibility computation. */
297 $boxcount = count($boxes);
299 /* Compute the collapsedness and visibility of each box. */
300 while ($curbox < $boxcount) {
301 $boxes[$curbox]['visible'] = TRUE;
302 compute_folder_children($curbox, $boxcount);
305 for ($i = 0;$i < count($boxes); $i++
) {
306 if ( $boxes[$i]['visible'] ) {
307 $mailbox = $boxes[$i]['formatted'];
308 $mblevel = substr_count($boxes[$i]['unformatted'], $delimiter) +
1;
310 /* Create the prefix for the folder name and link. */
311 $prefix = str_repeat(' ',$mblevel);
312 if (isset($collapse_folders) && $collapse_folders && $boxes[$i]['parent']) {
313 $prefix = str_replace(' ',' ',substr($prefix,0,strlen($prefix)-2)).
314 create_collapse_link($i) . ' ';
316 $prefix = str_replace(' ',' ',$prefix);
318 $line = "<NOBR><TT>$prefix</TT>";
320 /* Add the folder name and link. */
321 if (in_array('noselect', $boxes[$i]['flags'])) {
322 $line .= "<FONT COLOR=\"$color[10]\">";
323 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
324 $mailbox = str_replace(' ','',$mailbox);
325 $line .= str_replace(' ', ' ', $mailbox);
329 $line .= formatMailboxName($imapConnection, $boxes[$i]);
332 /* Put the final touches on our folder line. */
333 $line .= "</NOBR><BR>\n";
335 /* Output the line for this folder. */
340 sqimap_logout($imapConnection);
341 do_hook('left_main_after');
343 echo "</BODY></HTML>\n";