Added sent_subfolders plugin, tweaked config stuff, other stuff.
[squirrelmail.git] / src / left_main.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 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/*****************************************************************/
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
31require_once('../src/validate.php');
32require_once('../functions/array.php');
33require_once('../functions/imap.php');
34require_once('../functions/plugin.php');
35require_once('../functions/page_header.php');
142499d4 36
95e93571 37/* These constants are used for folder stuff. */
38define('SM_BOX_UNCOLLAPSED', 0);
39define('SM_BOX_COLLAPSED', 1);
a6d2e0de 40
2d367c68 41/* --------------------- FUNCTIONS ------------------------- */
525b7ae6 42
95e93571 43function 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;
94f3a73b 49
95e93571 50 $real_box = $box_array['unformatted'];
51 $mailbox = str_replace('&nbsp;','',$box_array['formatted']);
52 $mailboxURL = urlencode($real_box);
21c3249f 53
95e93571 54 /* Strip down the mailbox name. */
55 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
56 $mailbox = $regs[2];
57 }
94f3a73b 58
95e93571 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;
94f3a73b 71 }
95e93571 72 }
94f3a73b 73
95e93571 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;
94f3a73b 81 }
95e93571 82 }
f7b1b3b1 83
95e93571 84 /* Start off with a blank line. */
85 $line = '';
235a65d5 86
95e93571 87 /* If there are unseen message, bold the line. */
88 if ($unseen > 0) { $line .= '<B>'; }
a6d2e0de 89
95e93571 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>';
a6d2e0de 98
95e93571 99 /* If there are unseen message, close bolding. */
100 if ($unseen > 0) { $line .= "</B>"; }
2d367c68 101
95e93571 102 /* Print unseen information. */
103 if (isset($unseen_found) && $unseen_found) {
104 $line .= "&nbsp;<SMALL>$unseen_string</SMALL>";
2016e645 105 }
106
95e93571 107 if (($move_to_trash == true) && ($real_box == $trash_folder)) {
108 if (! isset($numMessages)) {
109 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
a6d2e0de 110 }
a6d2e0de 111
95e93571 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";
a6d2e0de 117 }
118 }
a6d2e0de 119
95e93571 120 /* Return the final product. */
121 return ($line);
122}
a6d2e0de 123
95e93571 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 */
129function 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;
a6d2e0de 145 }
95e93571 146 $boxes[$parbox]['collapse'] = $collapse;
2d367c68 147
95e93571 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 = '';
a6d2e0de 153
95e93571 154 /* Compute any children boxes for this box. */
155 while (($nextbox < $boxcount) &&
156 (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) {
a6d2e0de 157
95e93571 158 /* Note that this 'parent' box has at least one child. */
159 $boxes[$parbox]['parent'] = true;
a6d2e0de 160
95e93571 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 }
a6d2e0de 168
95e93571 169 /* Compute the visibility of any child boxes. */
170 compute_folder_children($nextbox, $boxcount);
171 }
2d367c68 172
95e93571 173 /* Set the parent box to the current next box. */
174 $parbox = $nextbox;
175}
2d367c68 176
95e93571 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 */
182function 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\">+";
2d367c68 191 } else {
95e93571 192 $link .= "fold=$mailbox\">-";
2d367c68 193 }
95e93571 194 $link .= '</a>';
2d367c68 195
95e93571 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 */
203function 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
219global $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
225if (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
233displayHtmlHeader( 'SquirrelMail', $xtra );
234
235/* If requested and not yet complete, attempt to autocreate folders. */
236if ($auto_create_special && !isset($auto_create_done)) {
a3439b27 237 $autocreate = array($sent_folder, $trash_folder, $draft_folder);
95e93571 238 foreach( $autocreate as $folder ) {
a3439b27 239 if (($folder != '') && ($folder != 'none')) {
95e93571 240 if ( !sqimap_mailbox_exists($imapConnection, $folder)) {
241 sqimap_mailbox_create($imapConnection, $folder, '');
a3439b27 242 } else if (!sqimap_mailbox_is_subscribed($imapConnection, $folder)) {
95e93571 243 sqimap_subscribe($imapConnection, $folder);
2d367c68 244 }
ebf18afa 245 }
2d367c68 246 }
a6d2e0de 247
95e93571 248 /* Let the world know that autocreation is complete! Hurrah! */
249 $auto_create_done = true;
250 session_register('auto_create_done');
251}
a6d2e0de 252
95e93571 253echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
a6d2e0de 254
95e93571 255do_hook('left_main_before');
2d367c68 256
95e93571 257$boxes = sqimap_mailbox_list($imapConnection);
2d367c68 258
95e93571 259echo '<CENTER><FONT SIZE=4><B>'. _("Folders") . "</B><BR></FONT>\n\n";
a6d2e0de 260
95e93571 261if ($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';
a6d2e0de 267 }
95e93571 268 } else {
269 if ($date_format == 4) {
270 $hr = 'g:i:s a';
271 } else {
272 $hr = 'g:i a';
2d367c68 273 }
274 }
275
95e93571 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());
2d367c68 289 }
95e93571 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. */
297echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">'.
298 _("refresh folder list") . '</A>)</SMALL></CENTER><BR>';
299
300/* Lastly, display the folder list. */
301if ( $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. */
315while ($curbox < $boxcount) {
316 $boxes[$curbox]['visible'] = TRUE;
317 compute_folder_children($curbox, $boxcount);
318}
319
320for ($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 (in_array('noselect', $boxes[$i]['flags'])) {
4689abb8 337 $line .= "<FONT COLOR=\"$color[7]\">";
95e93571 338 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
339 $mailbox = str_replace('&nbsp;','',$mailbox);
340 $line .= str_replace(' ', '&nbsp;', $mailbox);
2d367c68 341 }
95e93571 342 $line .= '</FONT>';
343 } else {
344 $line .= formatMailboxName($imapConnection, $boxes[$i]);
345 }
2d367c68 346
95e93571 347 /* Put the final touches on our folder line. */
348 $line .= "</NOBR><BR>\n";
2d367c68 349
95e93571 350 /* Output the line for this folder. */
351 echo $line;
2d367c68 352 }
95e93571 353}
2d367c68 354
95e93571 355sqimap_logout($imapConnection);
356do_hook('left_main_after');
2d367c68 357
95e93571 358echo "</BODY></HTML>\n";
2d367c68 359
165829a3 360?>