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