Fixed bug in display special folders code.
[squirrelmail.git] / src / left_main.php
1 <?php
2 /**
3 ** left_main.php
4 ** Copyright (c) 1999-2000 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 ($auto_create_special && ! isset($auto_create_done)) {
28 if (isset ($sent_folder) && $sent_folder != 'none') {
29 if (!sqimap_mailbox_exists ($imapConnection, $sent_folder)) {
30 sqimap_mailbox_create ($imapConnection, $sent_folder, '');
31 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $sent_folder)) {
32 sqimap_subscribe($imapConnection, $sent_folder);
33 }
34 }
35 if (isset ($trash_folder) && $trash_folder != 'none') {
36 if (!sqimap_mailbox_exists ($imapConnection, $trash_folder)) {
37 sqimap_mailbox_create ($imapConnection, $trash_folder, '');
38 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $trash_folder)) {
39 sqimap_subscribe($imapConnection, $trash_folder);
40 }
41 if (isset ($draft_folder) && $draft_folder != 'none') {
42 if (!sqimap_mailbox_exists ($imapConnection, $draft_folder)) {
43 sqimap_mailbox_create ($imapConnection, $draft_folder, '');
44 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $draft_folder)) {
45 sqimap_subscribe($imapConnection, $draft_folder);
46 }
47 }
48 }
49 $auto_create_done = true;
50 session_register('auto_create_done');
51 }
52
53 function formatMailboxName($imapConnection, $box_array, $delimeter) {
54 global $folder_prefix, $trash_folder, $sent_folder;
55 global $color, $move_to_sent, $move_to_trash;
56 global $unseen_notify, $unseen_type, $collapse_folders;
57 global $draft_folder, $save_as_draft;
58
59 $real_box = $box_array['unformatted'];
60 $mailbox = str_replace('&nbsp;','',$box_array['formatted']);
61 $mailboxURL = urlencode($real_box);
62
63 /* Strip down the mailbox name. */
64 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
65 $mailbox = $regs[2];
66 }
67
68 $unseen = 0;
69
70 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
71 $unseen_notify == 3) {
72 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
73 if ($unseen_type == 1 && $unseen > 0) {
74 $unseen_string = "($unseen)";
75 $unseen_found = true;
76 } else if ($unseen_type == 2) {
77 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
78 $unseen_string = "<font color=\"$color[11]\">($unseen/$numMessages)</font>";
79 $unseen_found = true;
80 }
81 }
82
83 $special_color = false;
84 if ((strtolower($real_box) == 'inbox') ||
85 (($real_box == $trash_folder) && ($move_to_trash)) ||
86 (($real_box == $sent_folder) && ($move_to_sent)) ||
87 (($real_box == $draft_folder) && ($save_as_draft)))
88 $special_color = true;
89
90 /* Start off with a blank line. */
91 $line = '';
92
93 /* If there are unseen message, bold the line. */
94 if ($unseen > 0) { $line .= '<B>'; }
95
96 /* Crate the link for this folder. */
97 $line .= "<A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$mailboxURL\" TARGET=\"right\" STYLE=\"text-decoration:none\">";
98 if ($special_color == true)
99 $line .= "<FONT COLOR=\"$color[11]\">";
100 $line .= str_replace(' ','&nbsp;',$mailbox);
101 if ($special_color == true)
102 $line .= "</FONT>";
103 $line .= '</A>';
104
105 /* If there are unseen message, close bolding. */
106 if ($unseen > 0) { $line .= "</B>"; }
107
108 /* Print unseen information. */
109 if (isset($unseen_found) && $unseen_found) {
110 $line .= "&nbsp;<SMALL>$unseen_string</SMALL>";
111 }
112
113 if (($move_to_trash == true) && ($real_box == $trash_folder)) {
114 if (! isset($numMessages)) {
115 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
116 }
117
118 if ($numMessages > 0) {
119 $urlMailbox = urlencode($real_box);
120 $line .= "\n<small>\n";
121 $line .= " &nbsp; (<B><A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("purge")."</A></B>)";
122 $line .= "\n</small>\n";
123 }
124 }
125
126 /* Return the final product. */
127 return ($line);
128 }
129
130 if (isset($left_refresh) && ($left_refresh != 'None') && ($left_refresh != '')) {
131 echo "<META HTTP-EQUIV=\"Expires\" CONTENT=\"Thu, 01 Dec 1994 16:00:00 GMT\">\n";
132 echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n";
133 echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"$left_refresh;URL=left_main.php\">\n";
134 }
135
136 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
137
138 do_hook("left_main_before");
139
140 $boxes = sqimap_mailbox_list($imapConnection);
141
142 echo '<CENTER><FONT SIZE=4><B>';
143 echo _("Folders") . "</B><BR></FONT>\n\n";
144
145 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">';
146 echo _("refresh folder list");
147 echo '</A>)</SMALL></CENTER><BR>';
148 $delimeter = sqimap_get_delimiter($imapConnection);
149
150 if (isset($collapse_folders) && $collapse_folders) {
151 /* If directed, collapse or uncollapse a folder. */
152 if (isset($fold)) {
153 setPref($data_dir, $username, 'collapse_folder_' . $fold, SM_BOX_COLLAPSED);
154 } else if (isset($unfold)) {
155 setPref($data_dir, $username, 'collapse_folder_' . $unfold, $SM_BOX_UNCOLLAPSED);
156 }
157 }
158
159 /* Prepare do do out collapsedness and visibility computation. */
160 $curbox = 0;
161 $boxcount = count($boxes);
162
163 /* Compute the collapsedness and visibility of each box. */
164 while ($curbox < $boxcount) {
165 $boxes[$curbox]['visible'] = true;
166 compute_folder_children($curbox, $boxcount);
167 }
168
169 for ($i = 0;$i < count($boxes); $i++) {
170 if ($boxes[$i]['visible'] == true) {
171 $mailbox = $boxes[$i]['formatted'];
172 $mblevel = substr_count($boxes[$i]['unformatted'], $delimeter) + 1;
173
174 /* Create the prefix for the folder name and link. */
175 $prefix = str_repeat(' ',$mblevel);
176 if (isset($collapse_folders) && $collapse_folders && $boxes[$i]['parent']) {
177 $prefix = str_replace(' ','&nbsp;',substr($prefix,0,strlen($prefix)-2));
178 $prefix .= create_collapse_link($i) . '&nbsp;';
179 } else {
180 $prefix = str_replace(' ','&nbsp;',$prefix);
181 }
182 $line = "<NOBR><TT>$prefix</TT>";
183
184 /* Add the folder name and link. */
185 if (in_array('noselect', $boxes[$i]['flags'])) {
186 $line .= "<FONT COLOR=\"$color[10]\">";
187 if (ereg("^( *)([^ ]*)", $mailbox, $regs)) {
188 $line .= str_replace(' ', '&nbsp;', $mailbox);
189 }
190 $line .= '</FONT>';
191 } else {
192 $line .= formatMailboxName($imapConnection, $boxes[$i], $delimeter);
193 }
194
195 /* Put the final touches on our folder line. */
196 $line .= "</NOBR><BR>\n";
197
198 /* Output the line for this folder. */
199 echo $line;
200 }
201 }
202 sqimap_logout($imapConnection);
203 do_hook("left_main_after");
204
205 /**
206 * Create the link for a parent folder that will allow that
207 * parent folder to either be collapsed or expaned, as is
208 * currently appropriate.
209 */
210 function create_collapse_link($boxnum) {
211 global $boxes;
212 $mailbox = urlencode($boxes[$boxnum]['unformatted']);
213
214 /* Create the link for this collapse link. */
215 $link = '<a target="left" style="text-decoration:none" ';
216 $link .= 'href="left_main.php?';
217 if ($boxes[$boxnum]['collapse'] == SM_BOX_COLLAPSED) {
218 $link .= "unfold=$mailbox\">+";
219 } else {
220 $link .= "fold=$mailbox\">-";
221 }
222 $link .= '</a>';
223
224 /* Return the finished product. */
225 return ($link);
226 }
227
228 /**
229 * This simple function checks if a box is another box's parent.
230 */
231 function is_parent_box($curbox_name, $parbox_name) {
232 global $delimeter;
233
234 /* Extract the name of the parent of the current box. */
235 $curparts = explode($delimeter, $curbox_name);
236 $curname = array_pop($curparts);
237 $actual_parname = implode($delimeter, $curparts);
238 $actual_parname = substr($actual_parname,0,strlen($parbox_name));
239
240 /* Compare the actual with the given parent name. */
241 return ($parbox_name == $actual_parname);
242 }
243
244 /**
245 * Recursive function that computes the collapsed status and parent
246 * (or not parent) status of this box, and the visiblity and collapsed
247 * status and parent (or not parent) status for all children boxes.
248 */
249 function compute_folder_children(&$parbox, $boxcount) {
250 global $boxes, $data_dir, $username;
251 $nextbox = $parbox + 1;
252
253 /* Retreive the name for the parent box. */
254 $parbox_name = $boxes[$parbox]['unformatted'];
255
256 /* 'Initialize' this parent box to childless. */
257 $boxes[$parbox]['parent'] = false;
258
259 /* Compute the collapse status for this box. */
260 $collapse = 0;
261 $collapse = getPref($data_dir, $username, 'collapse_folder_' . $parbox_name);
262 $collapse = ($collapse == '' ? SM_BOX_UNCOLLAPSED : $collapse);
263 $boxes[$parbox]['collapse'] = $collapse;
264
265 /* Otherwise, get the name of the next box. */
266 if (isset($boxes[$nextbox]['unformatted']))
267 $nextbox_name = $boxes[$nextbox]['unformatted'];
268 else
269 $nextbox_name = '';
270
271 /* Compute any children boxes for this box. */
272 while (($nextbox < $boxcount) &&
273 (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) {
274
275 /* Note that this 'parent' box has at least one child. */
276 $boxes[$parbox]['parent'] = true;
277
278 /* Compute the visiblity of this box. */
279 if ($boxes[$parbox]['visible'] &&
280 ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED)) {
281 $boxes[$nextbox]['visible'] = true;
282 } else {
283 $boxes[$nextbox]['visible'] = false;
284 }
285
286 /* Compute the visibility of any child boxes. */
287 compute_folder_children($nextbox, $boxcount);
288 }
289
290 /* Set the parent box to the current next box. */
291 $parbox = $nextbox;
292 }
293 echo "</BODY></HTML>\n";
294 ?>