Removed code I was playing around with.
[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 /*****************************************************************/
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, $trash_folder, $draft_folder);
238 foreach( $autocreate as $folder ) {
239 if (($folder != '') && ($folder != 'none')) {
240 if ( !sqimap_mailbox_exists($imapConnection, $folder)) {
241 sqimap_mailbox_create($imapConnection, $folder, '');
242 } else if (!sqimap_mailbox_is_subscribed($imapConnection, $folder)) {
243 sqimap_subscribe($imapConnection, $folder);
244 }
245 }
246 }
247
248 /* Let the world know that autocreation is complete! Hurrah! */
249 $auto_create_done = true;
250 session_register('auto_create_done');
251 }
252
253 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
254
255 do_hook('left_main_before');
256
257 $boxes = sqimap_mailbox_list($imapConnection);
258
259 echo '<CENTER><FONT SIZE=4><B>'. _("Folders") . "</B><BR></FONT>\n\n";
260
261 if ($date_format != 6) {
262 /* First, display the clock. */
263 if ($hour_format == 1) {
264 $hr = 'G:i';
265 if ($date_format == 4) {
266 $hr .= ':s';
267 }
268 } else {
269 if ($date_format == 4) {
270 $hr = 'g:i:s a';
271 } else {
272 $hr = 'g:i a';
273 }
274 }
275
276 switch( $date_format ) {
277 case 1:
278 $clk = date('m/d/y '.$hr, time());
279 break;
280 case 2:
281 $clk = date('d/m/y '.$hr, time());
282 break;
283 case 4:
284 case 5:
285 $clk = date($hr, time());
286 break;
287 default:
288 $clk = date('D, '.$hr, time());
289 }
290 $clk = str_replace(' ','&nbsp;',$clk);
291
292 echo '<CENTER><SMALL>' . str_replace(' ','&nbsp;',_("Last Refresh")) .
293 ": $clk</SMALL></CENTER>";
294 }
295
296 /* Next, display the refresh button. */
297 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">'.
298 _("refresh folder list") . '</A>)</SMALL></CENTER><BR>';
299
300 /* Lastly, display the folder list. */
301 if ( $collapse_folders ) {
302 /* If directed, collapse or uncollapse a folder. */
303 if (isset($fold)) {
304 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED);
305 } else if (isset($unfold)) {
306 setPref($data_dir, $username, 'collapse_folder_' . $unfold, SM_BOX_UNCOLLAPSED);
307 }
308 }
309
310 /* Prepare do do out collapsedness and visibility computation. */
311 $curbox = 0;
312 $boxcount = count($boxes);
313
314 /* Compute the collapsedness and visibility of each box. */
315 while ($curbox < $boxcount) {
316 $boxes[$curbox]['visible'] = TRUE;
317 compute_folder_children($curbox, $boxcount);
318 }
319
320 for ($i = 0;$i < count($boxes); $i++) {
321 if ( $boxes[$i]['visible'] ) {
322 $mailbox = $boxes[$i]['formatted'];
323 $mblevel = substr_count($boxes[$i]['unformatted'], $delimiter) + 1;
324
325 /* Create the prefix for the folder name and link. */
326 $prefix = str_repeat(' ',$mblevel);
327 if (isset($collapse_folders) && $collapse_folders && $boxes[$i]['parent']) {
328 $prefix = str_replace(' ','&nbsp;',substr($prefix,0,strlen($prefix)-2)).
329 create_collapse_link($i) . '&nbsp;';
330 } else {
331 $prefix = str_replace(' ','&nbsp;',$prefix);
332 }
333 $line = "<NOBR><TT>$prefix</TT>";
334
335 /* Add the folder name and link. */
336 if (! isset($color[15])) {
337 $color[15] = $color[6];
338 }
339 if (in_array('noselect', $boxes[$i]['flags'])) {
340 $line .= "<FONT COLOR=\"$color[15]\">";
341 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
342 $mailbox = str_replace('&nbsp;','',$mailbox);
343 $line .= str_replace(' ', '&nbsp;', $mailbox);
344 }
345 $line .= '</FONT>';
346 } else {
347 $line .= formatMailboxName($imapConnection, $boxes[$i]);
348 }
349
350 /* Put the final touches on our folder line. */
351 $line .= "</NOBR><BR>\n";
352
353 /* Output the line for this folder. */
354 echo $line;
355 }
356 }
357
358 sqimap_logout($imapConnection);
359 do_hook('left_main_after');
360
361 echo "</BODY></HTML>\n";
362
363 ?>