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