& to & fix
[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&amp;startMessage=1&amp;mailbox=$mailboxURL\" TARGET=\"right\" STYLE=\"text-decoration:none\">";
69 if ($special_color) {
70 $line .= "<FONT COLOR=\"$color[11]\">";
71 }
72 if ( $mailbox == 'INBOX' ) {
73 $line .= _("INBOX");
74 } else {
75 $line .= str_replace(' ','&nbsp;',$mailbox);
76 }
77 if ($special_color == TRUE)
78 $line .= "</FONT>";
79 $line .= '</A>';
80
81 /* If there are unseen message, close bolding. */
82 if ($unseen > 0) { $line .= "</B>"; }
83
84 /* Print unseen information. */
85 if (isset($unseen_found) && $unseen_found) {
86 $line .= "&nbsp;<SMALL>$unseen_string</SMALL>";
87 }
88
89 if (($move_to_trash) && ($real_box == $trash_folder)) {
90 if (! isset($numMessages)) {
91 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
92 }
93
94 if ($numMessages > 0) {
95 $urlMailbox = urlencode($real_box);
96 $line .= "\n<small>\n" .
97 "&nbsp;&nbsp;(<A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("empty")."</A>)" .
98 "</small>";
99 }
100 }
101
102 /* Return the final product. */
103 return ($line);
104 }
105
106 /**
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.
110 */
111 function compute_folder_children(&$parbox, $boxcount) {
112 global $boxes, $data_dir, $username, $collapse_folders;
113 $nextbox = $parbox + 1;
114
115 /* Retreive the name for the parent box. */
116 $parbox_name = $boxes[$parbox]['unformatted'];
117
118 /* 'Initialize' this parent box to childless. */
119 $boxes[$parbox]['parent'] = FALSE;
120
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);
125 } else {
126 $collapse = SM_BOX_UNCOLLAPSED;
127 }
128 $boxes[$parbox]['collapse'] = $collapse;
129
130 /* Otherwise, get the name of the next box. */
131 if (isset($boxes[$nextbox]['unformatted'])) {
132 $nextbox_name = $boxes[$nextbox]['unformatted'];
133 } else {
134 $nextbox_name = '';
135 }
136
137 /* Compute any children boxes for this box. */
138 while (($nextbox < $boxcount) &&
139 (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) {
140
141 /* Note that this 'parent' box has at least one child. */
142 $boxes[$parbox]['parent'] = TRUE;
143
144 /* Compute the visiblity of this box. */
145 $boxes[$nextbox]['visible'] = ($boxes[$parbox]['visible'] &&
146 ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED));
147
148 /* Compute the visibility of any child boxes. */
149 compute_folder_children($nextbox, $boxcount);
150 }
151
152 /* Set the parent box to the current next box. */
153 $parbox = $nextbox;
154 }
155
156 /**
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.
160 */
161 function create_collapse_link($boxnum) {
162 global $boxes;
163 $mailbox = urlencode($boxes[$boxnum]['unformatted']);
164
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\">+";
170 } else {
171 $link .= "fold=$mailbox\">-";
172 }
173 $link .= '</a>';
174
175 /* Return the finished product. */
176 return ($link);
177 }
178
179 /**
180 * This simple function checks if a box is another box's parent.
181 */
182 function is_parent_box($curbox_name, $parbox_name) {
183 global $delimiter;
184
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));
190
191 /* Compare the actual with the given parent name. */
192 return ($parbox_name == $actual_parname);
193 }
194
195
196 /* -------------------- MAIN ------------------------ */
197
198 global $delimiter, $default_folder_prefix;
199
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
202
203
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";
208 } else {
209 $xtra = '';
210 }
211
212 displayHtmlHeader( 'SquirrelMail', $xtra );
213
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);
223 }
224 }
225 }
226
227 /* Let the world know that autocreation is complete! Hurrah! */
228 $auto_create_done = TRUE;
229 session_register('auto_create_done');
230 }
231
232 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
233
234 do_hook('left_main_before');
235
236 $boxes = sqimap_mailbox_list($imapConnection);
237
238 echo '<CENTER><FONT SIZE=4><B>'. _("Folders") . "</B><BR></FONT>\n\n";
239
240 if ($date_format != 6) {
241 /* First, display the clock. */
242 if ($hour_format == 1) {
243 $hr = 'G:i';
244 if ($date_format == 4) {
245 $hr .= ':s';
246 }
247 } else {
248 if ($date_format == 4) {
249 $hr = 'g:i:s a';
250 } else {
251 $hr = 'g:i a';
252 }
253 }
254
255 switch( $date_format ) {
256 case 1:
257 $clk = date('m/d/y '.$hr, time());
258 break;
259 case 2:
260 $clk = date('d/m/y '.$hr, time());
261 break;
262 case 4:
263 case 5:
264 $clk = date($hr, time());
265 break;
266 default:
267 $clk = substr( getDayName( date( 'w', time() ) ), 0, 3 ) . date( ', ' . $hr, time() );
268 }
269 $clk = str_replace(' ','&nbsp;',$clk);
270
271 echo '<CENTER><SMALL>' . str_replace(' ','&nbsp;',_("Last Refresh")) .
272 ": $clk</SMALL></CENTER>";
273 }
274
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>';
278
279 /* Lastly, display the folder list. */
280 if ( $collapse_folders ) {
281 /* If directed, collapse or uncollapse a folder. */
282 if (isset($fold)) {
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);
286 }
287 }
288
289 /* Prepare do do out collapsedness and visibility computation. */
290 $curbox = 0;
291 $boxcount = count($boxes);
292
293 /* Compute the collapsedness and visibility of each box. */
294
295 while ($curbox < $boxcount) {
296 $boxes[$curbox]['visible'] = TRUE;
297 compute_folder_children($curbox, $boxcount);
298 }
299
300
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;
305
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(' ','&nbsp;',substr($prefix,0,strlen($prefix)-2)).
310 create_collapse_link($i) . '&nbsp;';
311 } else {
312 $prefix = str_replace(' ','&nbsp;',$prefix);
313 }
314 $line = "<NOBR><TT>$prefix</TT>";
315
316 /* Add the folder name and link. */
317 if (! isset($color[15])) {
318 $color[15] = $color[6];
319 }
320
321 if (in_array('noselect', $boxes[$i]['flags'])) {
322 if( isSpecialMailbox( $boxes[$i]['unformatted']) ) {
323 $line .= "<FONT COLOR=\"$color[11]\">";
324 } else {
325 $line .= "<FONT COLOR=\"$color[15]\">";
326 }
327 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
328 $mailbox = str_replace('&nbsp;','',$mailbox);
329 $line .= str_replace(' ', '&nbsp;', $mailbox);
330 }
331 $line .= '</FONT>';
332 } else {
333 $line .= formatMailboxName($imapConnection, $boxes[$i]);
334 }
335
336 /* Put the final touches on our folder line. */
337 $line .= "</NOBR><BR>\n";
338
339 /* Output the line for this folder. */
340 echo $line;
341 }
342 }
343
344 sqimap_logout($imapConnection);
345 do_hook('left_main_after');
346
347 echo "</BODY></HTML>\n";
348
349 ?>