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