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