6491ec9687fb9a050a8f6ec4be164e07af94ea3a
[squirrelmail.git] / src / left_main.php
1 <?php
2
3 /**
4 * left_main.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This is the code for the left bar. The left bar shows the folders
10 * available, and has cookie information.
11 *
12 * $Id$
13 */
14
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');
20
21 /* These constants are used for folder stuff. */
22 define('SM_BOX_UNCOLLAPSED', 0);
23 define('SM_BOX_COLLAPSED', 1);
24
25 /* --------------------- FUNCTIONS ------------------------- */
26
27 function formatMailboxName($imapConnection, $box_array) {
28
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;
34
35 $real_box = $box_array['unformatted'];
36 $mailbox = str_replace('&nbsp;','',$box_array['formatted']);
37 $mailboxURL = urlencode($real_box);
38
39 /* Strip down the mailbox name. */
40 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
41 $mailbox = $regs[2];
42 }
43
44 $unseen = 0;
45
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)";
51 $unseen_found = TRUE;
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>";
55 $unseen_found = TRUE;
56 }
57 }
58
59 $special_color = ($use_special_folder_color && isSpecialMailbox( $real_box ) );
60
61 /* Start off with a blank line. */
62 $line = '';
63
64 /* If there are unseen message, bold the line. */
65 if ($unseen > 0) { $line .= '<B>'; }
66
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\">";
69 if ($special_color) {
70 $line .= "<FONT COLOR=\"$color[11]\">";
71 }
72 $line .= str_replace(' ','&nbsp;',$mailbox);
73 if ($special_color == TRUE)
74 $line .= "</FONT>";
75 $line .= '</A>';
76
77 /* If there are unseen message, close bolding. */
78 if ($unseen > 0) { $line .= "</B>"; }
79
80 /* Print unseen information. */
81 if (isset($unseen_found) && $unseen_found) {
82 $line .= "&nbsp;<SMALL>$unseen_string</SMALL>";
83 }
84
85 if (($move_to_trash) && ($real_box == $trash_folder)) {
86 if (! isset($numMessages)) {
87 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
88 }
89
90 if ($numMessages > 0) {
91 $urlMailbox = urlencode($real_box);
92 $line .= "\n<small>\n" .
93 "&nbsp;&nbsp;(<A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("empty")."</A>)" .
94 "</small>";
95 }
96 }
97
98 /* Return the final product. */
99 return ($line);
100 }
101
102 /**
103 * Recursive function that computes the collapsed status and parent
104 * (or not parent) status of this box, and the visiblity and collapsed
105 * status and parent (or not parent) status for all children boxes.
106 */
107 function compute_folder_children(&$parbox, $boxcount) {
108 global $boxes, $data_dir, $username, $collapse_folders;
109 $nextbox = $parbox + 1;
110
111 /* Retreive the name for the parent box. */
112 $parbox_name = $boxes[$parbox]['unformatted'];
113
114 /* 'Initialize' this parent box to childless. */
115 $boxes[$parbox]['parent'] = FALSE;
116
117 /* Compute the collapse status for this box. */
118 if( isset($collapse_folders) && $collapse_folders ) {
119 $collapse = getPref($data_dir, $username, 'collapse_folder_' . $parbox_name);
120 $collapse = ($collapse == '' ? SM_BOX_UNCOLLAPSED : $collapse);
121 } else {
122 $collapse = SM_BOX_UNCOLLAPSED;
123 }
124 $boxes[$parbox]['collapse'] = $collapse;
125
126 /* Otherwise, get the name of the next box. */
127 if (isset($boxes[$nextbox]['unformatted'])) {
128 $nextbox_name = $boxes[$nextbox]['unformatted'];
129 } else {
130 $nextbox_name = '';
131 }
132
133 /* Compute any children boxes for this box. */
134 while (($nextbox < $boxcount) &&
135 (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) {
136
137 /* Note that this 'parent' box has at least one child. */
138 $boxes[$parbox]['parent'] = TRUE;
139
140 /* Compute the visiblity of this box. */
141 $boxes[$nextbox]['visible'] = ($boxes[$parbox]['visible'] &&
142 ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED));
143
144 /* Compute the visibility of any child boxes. */
145 compute_folder_children($nextbox, $boxcount);
146 }
147
148 /* Set the parent box to the current next box. */
149 $parbox = $nextbox;
150 }
151
152 /**
153 * Create the link for a parent folder that will allow that
154 * parent folder to either be collapsed or expaned, as is
155 * currently appropriate.
156 */
157 function create_collapse_link($boxnum) {
158 global $boxes;
159 $mailbox = urlencode($boxes[$boxnum]['unformatted']);
160
161 /* Create the link for this collapse link. */
162 $link = '<a target="left" style="text-decoration:none" ' .
163 'href="left_main.php?';
164 if ($boxes[$boxnum]['collapse'] == SM_BOX_COLLAPSED) {
165 $link .= "unfold=$mailbox\">+";
166 } else {
167 $link .= "fold=$mailbox\">-";
168 }
169 $link .= '</a>';
170
171 /* Return the finished product. */
172 return ($link);
173 }
174
175 /**
176 * This simple function checks if a box is another box's parent.
177 */
178 function is_parent_box($curbox_name, $parbox_name) {
179 global $delimiter;
180
181 /* Extract the name of the parent of the current box. */
182 $curparts = explode($delimiter, $curbox_name);
183 $curname = array_pop($curparts);
184 $actual_parname = implode($delimiter, $curparts);
185 $actual_parname = substr($actual_parname,0,strlen($parbox_name));
186
187 /* Compare the actual with the given parent name. */
188 return ($parbox_name == $actual_parname);
189 }
190
191
192 /* -------------------- MAIN ------------------------ */
193
194 global $delimiter, $default_folder_prefix;
195
196 // open a connection on the imap port (143)
197 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); // the 10 is to hide the output
198
199
200 if (isset($left_refresh) && ($left_refresh != 'none') && ($left_refresh != '')) {
201 $xtra = "\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"Thu, 01 Dec 1994 16:00:00 GMT\">\n" .
202 "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n".
203 "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"$left_refresh;URL=left_main.php\">\n";
204 } else {
205 $xtra = '';
206 }
207
208 displayHtmlHeader( 'SquirrelMail', $xtra );
209
210 /* If requested and not yet complete, attempt to autocreate folders. */
211 if ($auto_create_special && !isset($auto_create_done)) {
212 $autocreate = array($sent_folder, $trash_folder, $draft_folder);
213 foreach( $autocreate as $folder ) {
214 if (($folder != '') && ($folder != 'none')) {
215 if ( !sqimap_mailbox_exists($imapConnection, $folder)) {
216 sqimap_mailbox_create($imapConnection, $folder, '');
217 } else if (!sqimap_mailbox_is_subscribed($imapConnection, $folder)) {
218 sqimap_subscribe($imapConnection, $folder);
219 }
220 }
221 }
222
223 /* Let the world know that autocreation is complete! Hurrah! */
224 $auto_create_done = TRUE;
225 session_register('auto_create_done');
226 }
227
228 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
229
230 do_hook('left_main_before');
231
232 $boxes = sqimap_mailbox_list($imapConnection);
233
234 echo '<CENTER><FONT SIZE=4><B>'. _("Folders") . "</B><BR></FONT>\n\n";
235
236 if ($date_format != 6) {
237 /* First, display the clock. */
238 if ($hour_format == 1) {
239 $hr = 'G:i';
240 if ($date_format == 4) {
241 $hr .= ':s';
242 }
243 } else {
244 if ($date_format == 4) {
245 $hr = 'g:i:s a';
246 } else {
247 $hr = 'g:i a';
248 }
249 }
250
251 switch( $date_format ) {
252 case 1:
253 $clk = date('m/d/y '.$hr, time());
254 break;
255 case 2:
256 $clk = date('d/m/y '.$hr, time());
257 break;
258 case 4:
259 case 5:
260 $clk = date($hr, time());
261 break;
262 default:
263 $clk = substr( getDayName( date( 'w', time() ) ), 0, 3 ) . date( ', ' . $hr, time() );
264 }
265 $clk = str_replace(' ','&nbsp;',$clk);
266
267 echo '<CENTER><SMALL>' . str_replace(' ','&nbsp;',_("Last Refresh")) .
268 ": $clk</SMALL></CENTER>";
269 }
270
271 /* Next, display the refresh button. */
272 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">'.
273 _("refresh folder list") . '</A>)</SMALL></CENTER><BR>';
274
275 /* Lastly, display the folder list. */
276 if ( $collapse_folders ) {
277 /* If directed, collapse or uncollapse a folder. */
278 if (isset($fold)) {
279 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED);
280 } else if (isset($unfold)) {
281 setPref($data_dir, $username, 'collapse_folder_' . $unfold, SM_BOX_UNCOLLAPSED);
282 }
283 }
284
285 /* Prepare do do out collapsedness and visibility computation. */
286 $curbox = 0;
287 $boxcount = count($boxes);
288
289 /* Compute the collapsedness and visibility of each box. */
290
291 while ($curbox < $boxcount) {
292 $boxes[$curbox]['visible'] = TRUE;
293 compute_folder_children($curbox, $boxcount);
294 }
295
296
297 for ($i = 0; $i < count($boxes); $i++) {
298 if ( $boxes[$i]['visible'] ) {
299 $mailbox = $boxes[$i]['formatted'];
300 $mblevel = substr_count($boxes[$i]['unformatted'], $delimiter) + 1;
301
302 /* Create the prefix for the folder name and link. */
303 $prefix = str_repeat(' ',$mblevel);
304 if (isset($collapse_folders) && $collapse_folders && $boxes[$i]['parent']) {
305 $prefix = str_replace(' ','&nbsp;',substr($prefix,0,strlen($prefix)-2)).
306 create_collapse_link($i) . '&nbsp;';
307 } else {
308 $prefix = str_replace(' ','&nbsp;',$prefix);
309 }
310 $line = "<NOBR><TT>$prefix</TT>";
311
312 /* Add the folder name and link. */
313 if (! isset($color[15])) {
314 $color[15] = $color[6];
315 }
316
317 if (in_array('noselect', $boxes[$i]['flags'])) {
318 if( isSpecialMailbox( $boxes[$i]['unformatted']) ) {
319 $line .= "<FONT COLOR=\"$color[11]\">";
320 } else {
321 $line .= "<FONT COLOR=\"$color[15]\">";
322 }
323 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
324 $mailbox = str_replace('&nbsp;','',$mailbox);
325 $line .= str_replace(' ', '&nbsp;', $mailbox);
326 }
327 $line .= '</FONT>';
328 } else {
329 $line .= formatMailboxName($imapConnection, $boxes[$i]);
330 }
331
332 /* Put the final touches on our folder line. */
333 $line .= "</NOBR><BR>\n";
334
335 /* Output the line for this folder. */
336 echo $line;
337 }
338 }
339
340 sqimap_logout($imapConnection);
341 do_hook('left_main_after');
342
343 echo "</BODY></HTML>\n";
344
345 ?>