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 isSpecialMailbox( $box ) {
29 global $trash_folder, $sent_folder, $draft_folder,
30 $move_to_trash, $move_to_sent, $save_as_draft;
32 $ret = ( (strtolower($box) == 'inbox') ||
33 (($box == $trash_folder) &&
35 ((substr( $sent_folder, 0, strlen( $box ) ) == $box) &&
37 (($box == $draft_folder) &&
44 function formatMailboxName($imapConnection, $box_array) {
46 global $folder_prefix, $trash_folder, $sent_folder;
47 global $color, $move_to_sent, $move_to_trash;
48 global $unseen_notify, $unseen_type, $collapse_folders;
49 global $draft_folder, $save_as_draft;
50 global $use_special_folder_color;
52 $real_box = $box_array['unformatted'];
53 $mailbox = str_replace(' ','',$box_array['formatted']);
54 $mailboxURL = urlencode($real_box);
56 /* Strip down the mailbox name. */
57 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
63 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
64 $unseen_notify == 3) {
65 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
66 if ($unseen_type == 1 && $unseen > 0) {
67 $unseen_string = "($unseen)";
69 } else if ($unseen_type == 2) {
70 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
71 $unseen_string = "<font color=\"$color[11]\">($unseen/$numMessages)</font>";
76 $special_color = ($use_special_folder_color && isSpecialMailbox( $real_box ) );
78 /* Start off with a blank line. */
81 /* If there are unseen message, bold the line. */
82 if ($unseen > 0) { $line .= '<B>'; }
84 /* Crate the link for this folder. */
85 $line .= "<A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$mailboxURL\" TARGET=\"right\" STYLE=\"text-decoration:none\">";
87 $line .= "<FONT COLOR=\"$color[11]\">";
89 $line .= str_replace(' ',' ',$mailbox);
90 if ($special_color == true)
94 /* If there are unseen message, close bolding. */
95 if ($unseen > 0) { $line .= "</B>"; }
97 /* Print unseen information. */
98 if (isset($unseen_found) && $unseen_found) {
99 $line .= " <SMALL>$unseen_string</SMALL>";
102 if (($move_to_trash) && ($real_box == $trash_folder)) {
103 if (! isset($numMessages)) {
104 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
107 if ($numMessages > 0) {
108 $urlMailbox = urlencode($real_box);
109 $line .= "\n<small>\n" .
110 " (<A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("empty")."</A>)" .
115 /* Return the final product. */
120 * Recursive function that computes the collapsed status and parent
121 * (or not parent) status of this box, and the visiblity and collapsed
122 * status and parent (or not parent) status for all children boxes.
124 function compute_folder_children(&$parbox, $boxcount) {
125 global $boxes, $data_dir, $username, $collapse_folders;
126 $nextbox = $parbox +
1;
128 /* Retreive the name for the parent box. */
129 $parbox_name = $boxes[$parbox]['unformatted'];
131 /* 'Initialize' this parent box to childless. */
132 $boxes[$parbox]['parent'] = FALSE;
134 /* Compute the collapse status for this box. */
135 if( isset($collapse_folders) && $collapse_folders ) {
136 $collapse = getPref($data_dir, $username, 'collapse_folder_' . $parbox_name);
137 $collapse = ($collapse == '' ? SM_BOX_UNCOLLAPSED
: $collapse);
139 $collapse = SM_BOX_UNCOLLAPSED
;
141 $boxes[$parbox]['collapse'] = $collapse;
143 /* Otherwise, get the name of the next box. */
144 if (isset($boxes[$nextbox]['unformatted'])) {
145 $nextbox_name = $boxes[$nextbox]['unformatted'];
150 /* Compute any children boxes for this box. */
151 while (($nextbox < $boxcount) &&
152 (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) {
154 /* Note that this 'parent' box has at least one child. */
155 $boxes[$parbox]['parent'] = TRUE;
157 /* Compute the visiblity of this box. */
158 $boxes[$nextbox]['visible'] = ($boxes[$parbox]['visible'] &&
159 ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED
));
161 /* Compute the visibility of any child boxes. */
162 compute_folder_children($nextbox, $boxcount);
165 /* Set the parent box to the current next box. */
170 * Create the link for a parent folder that will allow that
171 * parent folder to either be collapsed or expaned, as is
172 * currently appropriate.
174 function create_collapse_link($boxnum) {
176 $mailbox = urlencode($boxes[$boxnum]['unformatted']);
178 /* Create the link for this collapse link. */
179 $link = '<a target="left" style="text-decoration:none" ';
180 $link .= 'href="left_main.php?';
181 if ($boxes[$boxnum]['collapse'] == SM_BOX_COLLAPSED
) {
182 $link .= "unfold=$mailbox\">+";
184 $link .= "fold=$mailbox\">-";
188 /* Return the finished product. */
193 * This simple function checks if a box is another box's parent.
195 function is_parent_box($curbox_name, $parbox_name) {
198 /* Extract the name of the parent of the current box. */
199 $curparts = explode($delimiter, $curbox_name);
200 $curname = array_pop($curparts);
201 $actual_parname = implode($delimiter, $curparts);
202 $actual_parname = substr($actual_parname,0,strlen($parbox_name));
204 /* Compare the actual with the given parent name. */
205 return ($parbox_name == $actual_parname);
209 /* -------------------- MAIN ------------------------ */
211 global $delimiter, $default_folder_prefix;
213 // open a connection on the imap port (143)
214 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); // the 10 is to hide the output
217 if (isset($left_refresh) && ($left_refresh != 'none') && ($left_refresh != '')) {
218 $xtra = "\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"Thu, 01 Dec 1994 16:00:00 GMT\">\n" .
219 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n".
220 "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"$left_refresh;URL=left_main.php\">\n";
225 displayHtmlHeader( 'SquirrelMail', $xtra );
227 /* If requested and not yet complete, attempt to autocreate folders. */
228 if ($auto_create_special && !isset($auto_create_done)) {
229 $autocreate = array($sent_folder, $trash_folder, $draft_folder);
230 foreach( $autocreate as $folder ) {
231 if (($folder != '') && ($folder != 'none')) {
232 if ( !sqimap_mailbox_exists($imapConnection, $folder)) {
233 sqimap_mailbox_create($imapConnection, $folder, '');
234 } else if (!sqimap_mailbox_is_subscribed($imapConnection, $folder)) {
235 sqimap_subscribe($imapConnection, $folder);
240 /* Let the world know that autocreation is complete! Hurrah! */
241 $auto_create_done = true;
242 session_register('auto_create_done');
245 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
247 do_hook('left_main_before');
249 $boxes = sqimap_mailbox_list($imapConnection);
251 echo '<CENTER><FONT SIZE=4><B>'. _("Folders") . "</B><BR></FONT>\n\n";
253 if ($date_format != 6) {
254 /* First, display the clock. */
255 if ($hour_format == 1) {
257 if ($date_format == 4) {
261 if ($date_format == 4) {
268 switch( $date_format ) {
270 $clk = date('m/d/y '.$hr, time());
273 $clk = date('d/m/y '.$hr, time());
277 $clk = date($hr, time());
280 $clk = date('D, '.$hr, time());
282 $clk = str_replace(' ',' ',$clk);
284 echo '<CENTER><SMALL>' . str_replace(' ',' ',_("Last Refresh")) .
285 ": $clk</SMALL></CENTER>";
288 /* Next, display the refresh button. */
289 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">'.
290 _("refresh folder list") . '</A>)</SMALL></CENTER><BR>';
292 /* Lastly, display the folder list. */
293 if ( $collapse_folders ) {
294 /* If directed, collapse or uncollapse a folder. */
296 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED
);
297 } else if (isset($unfold)) {
298 setPref($data_dir, $username, 'collapse_folder_' . $unfold, SM_BOX_UNCOLLAPSED
);
302 /* Prepare do do out collapsedness and visibility computation. */
304 $boxcount = count($boxes);
306 /* Compute the collapsedness and visibility of each box. */
307 while ($curbox < $boxcount) {
308 $boxes[$curbox]['visible'] = TRUE;
309 compute_folder_children($curbox, $boxcount);
312 for ($i = 0; $i < count($boxes); $i++
) {
313 if ( $boxes[$i]['visible'] ) {
314 $mailbox = $boxes[$i]['formatted'];
315 $mblevel = substr_count($boxes[$i]['unformatted'], $delimiter) +
1;
317 /* Create the prefix for the folder name and link. */
318 $prefix = str_repeat(' ',$mblevel);
319 if (isset($collapse_folders) && $collapse_folders && $boxes[$i]['parent']) {
320 $prefix = str_replace(' ',' ',substr($prefix,0,strlen($prefix)-2)).
321 create_collapse_link($i) . ' ';
323 $prefix = str_replace(' ',' ',$prefix);
325 $line = "<NOBR><TT>$prefix</TT>";
327 /* Add the folder name and link. */
328 if (! isset($color[15])) {
329 $color[15] = $color[6];
331 if (in_array('noselect', $boxes[$i]['flags'])) {
332 if( isSpecialMailbox( $boxes[$i]['unformatted']) ) {
333 $line .= "<FONT COLOR=\"$color[11]\">";
335 $line .= "<FONT COLOR=\"$color[15]\">";
337 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
338 $mailbox = str_replace(' ','',$mailbox);
339 $line .= str_replace(' ', ' ', $mailbox);
343 $line .= formatMailboxName($imapConnection, $boxes[$i]);
346 /* Put the final touches on our folder line. */
347 $line .= "</NOBR><BR>\n";
349 /* Output the line for this folder. */
354 sqimap_logout($imapConnection);
355 do_hook('left_main_after');
357 echo "</BODY></HTML>\n";