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