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