59177427 |
1 | <?php |
895905c0 |
2 | |
35586184 |
3 | /** |
4 | * left_main.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
35586184 |
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$ |
1c52ba77 |
13 | */ |
35586184 |
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'); |
142499d4 |
20 | |
95e93571 |
21 | /* These constants are used for folder stuff. */ |
22 | define('SM_BOX_UNCOLLAPSED', 0); |
23 | define('SM_BOX_COLLAPSED', 1); |
a6d2e0de |
24 | |
2d367c68 |
25 | /* --------------------- FUNCTIONS ------------------------- */ |
525b7ae6 |
26 | |
1c52ba77 |
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 | |
95e93571 |
44 | function formatMailboxName($imapConnection, $box_array) { |
1c52ba77 |
45 | |
95e93571 |
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; |
94f3a73b |
51 | |
95e93571 |
52 | $real_box = $box_array['unformatted']; |
53 | $mailbox = str_replace(' ','',$box_array['formatted']); |
54 | $mailboxURL = urlencode($real_box); |
21c3249f |
55 | |
95e93571 |
56 | /* Strip down the mailbox name. */ |
57 | if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) { |
58 | $mailbox = $regs[2]; |
59 | } |
94f3a73b |
60 | |
95e93571 |
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; |
94f3a73b |
73 | } |
95e93571 |
74 | } |
94f3a73b |
75 | |
1c52ba77 |
76 | $special_color = ($use_special_folder_color && isSpecialMailbox( $real_box ) ); |
f7b1b3b1 |
77 | |
95e93571 |
78 | /* Start off with a blank line. */ |
79 | $line = ''; |
235a65d5 |
80 | |
95e93571 |
81 | /* If there are unseen message, bold the line. */ |
82 | if ($unseen > 0) { $line .= '<B>'; } |
a6d2e0de |
83 | |
95e93571 |
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\">"; |
1c52ba77 |
86 | if ($special_color) { |
95e93571 |
87 | $line .= "<FONT COLOR=\"$color[11]\">"; |
1c52ba77 |
88 | } |
95e93571 |
89 | $line .= str_replace(' ',' ',$mailbox); |
90 | if ($special_color == true) |
91 | $line .= "</FONT>"; |
92 | $line .= '</A>'; |
a6d2e0de |
93 | |
95e93571 |
94 | /* If there are unseen message, close bolding. */ |
95 | if ($unseen > 0) { $line .= "</B>"; } |
2d367c68 |
96 | |
95e93571 |
97 | /* Print unseen information. */ |
98 | if (isset($unseen_found) && $unseen_found) { |
99 | $line .= " <SMALL>$unseen_string</SMALL>"; |
2016e645 |
100 | } |
101 | |
1c52ba77 |
102 | if (($move_to_trash) && ($real_box == $trash_folder)) { |
95e93571 |
103 | if (! isset($numMessages)) { |
104 | $numMessages = sqimap_get_num_messages($imapConnection, $real_box); |
a6d2e0de |
105 | } |
a6d2e0de |
106 | |
95e93571 |
107 | if ($numMessages > 0) { |
108 | $urlMailbox = urlencode($real_box); |
109 | $line .= "\n<small>\n" . |
110 | " (<A HREF=\"empty_trash.php\" style=\"text-decoration:none\">"._("empty")."</A>)" . |
111 | "\n</small>\n"; |
a6d2e0de |
112 | } |
113 | } |
a6d2e0de |
114 | |
95e93571 |
115 | /* Return the final product. */ |
116 | return ($line); |
117 | } |
a6d2e0de |
118 | |
95e93571 |
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. */ |
1c52ba77 |
132 | $boxes[$parbox]['parent'] = FALSE; |
95e93571 |
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; |
a6d2e0de |
140 | } |
95e93571 |
141 | $boxes[$parbox]['collapse'] = $collapse; |
2d367c68 |
142 | |
95e93571 |
143 | /* Otherwise, get the name of the next box. */ |
1c52ba77 |
144 | if (isset($boxes[$nextbox]['unformatted'])) { |
95e93571 |
145 | $nextbox_name = $boxes[$nextbox]['unformatted']; |
1c52ba77 |
146 | } else { |
95e93571 |
147 | $nextbox_name = ''; |
1c52ba77 |
148 | } |
a6d2e0de |
149 | |
95e93571 |
150 | /* Compute any children boxes for this box. */ |
151 | while (($nextbox < $boxcount) && |
152 | (is_parent_box($boxes[$nextbox]['unformatted'], $parbox_name))) { |
a6d2e0de |
153 | |
95e93571 |
154 | /* Note that this 'parent' box has at least one child. */ |
1c52ba77 |
155 | $boxes[$parbox]['parent'] = TRUE; |
a6d2e0de |
156 | |
95e93571 |
157 | /* Compute the visiblity of this box. */ |
1c52ba77 |
158 | $boxes[$nextbox]['visible'] = ($boxes[$parbox]['visible'] && |
159 | ($boxes[$parbox]['collapse'] != SM_BOX_COLLAPSED)); |
a6d2e0de |
160 | |
95e93571 |
161 | /* Compute the visibility of any child boxes. */ |
162 | compute_folder_children($nextbox, $boxcount); |
163 | } |
2d367c68 |
164 | |
95e93571 |
165 | /* Set the parent box to the current next box. */ |
166 | $parbox = $nextbox; |
167 | } |
2d367c68 |
168 | |
95e93571 |
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\">+"; |
2d367c68 |
183 | } else { |
95e93571 |
184 | $link .= "fold=$mailbox\">-"; |
2d367c68 |
185 | } |
95e93571 |
186 | $link .= '</a>'; |
2d367c68 |
187 | |
95e93571 |
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)) { |
a3439b27 |
229 | $autocreate = array($sent_folder, $trash_folder, $draft_folder); |
95e93571 |
230 | foreach( $autocreate as $folder ) { |
a3439b27 |
231 | if (($folder != '') && ($folder != 'none')) { |
95e93571 |
232 | if ( !sqimap_mailbox_exists($imapConnection, $folder)) { |
233 | sqimap_mailbox_create($imapConnection, $folder, ''); |
a3439b27 |
234 | } else if (!sqimap_mailbox_is_subscribed($imapConnection, $folder)) { |
95e93571 |
235 | sqimap_subscribe($imapConnection, $folder); |
2d367c68 |
236 | } |
ebf18afa |
237 | } |
2d367c68 |
238 | } |
a6d2e0de |
239 | |
95e93571 |
240 | /* Let the world know that autocreation is complete! Hurrah! */ |
241 | $auto_create_done = true; |
242 | session_register('auto_create_done'); |
243 | } |
a6d2e0de |
244 | |
95e93571 |
245 | echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n"; |
a6d2e0de |
246 | |
95e93571 |
247 | do_hook('left_main_before'); |
2d367c68 |
248 | |
95e93571 |
249 | $boxes = sqimap_mailbox_list($imapConnection); |
2d367c68 |
250 | |
95e93571 |
251 | echo '<CENTER><FONT SIZE=4><B>'. _("Folders") . "</B><BR></FONT>\n\n"; |
a6d2e0de |
252 | |
95e93571 |
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'; |
a6d2e0de |
259 | } |
95e93571 |
260 | } else { |
261 | if ($date_format == 4) { |
262 | $hr = 'g:i:s a'; |
263 | } else { |
264 | $hr = 'g:i a'; |
2d367c68 |
265 | } |
266 | } |
267 | |
95e93571 |
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()); |
2d367c68 |
281 | } |
95e93571 |
282 | $clk = str_replace(' ',' ',$clk); |
283 | |
284 | echo '<CENTER><SMALL>' . str_replace(' ',' ',_("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 | |
1c52ba77 |
312 | for ($i = 0; $i < count($boxes); $i++) { |
95e93571 |
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(' ',' ',substr($prefix,0,strlen($prefix)-2)). |
321 | create_collapse_link($i) . ' '; |
322 | } else { |
323 | $prefix = str_replace(' ',' ',$prefix); |
324 | } |
325 | $line = "<NOBR><TT>$prefix</TT>"; |
326 | |
327 | /* Add the folder name and link. */ |
1c52ba77 |
328 | if (! isset($color[15])) { |
05611cba |
329 | $color[15] = $color[6]; |
1c52ba77 |
330 | } |
95e93571 |
331 | if (in_array('noselect', $boxes[$i]['flags'])) { |
1c52ba77 |
332 | if( isSpecialMailbox( $boxes[$i]['unformatted']) ) { |
333 | $line .= "<FONT COLOR=\"$color[11]\">"; |
334 | } else { |
335 | $line .= "<FONT COLOR=\"$color[15]\">"; |
336 | } |
95e93571 |
337 | if (ereg("^( *)([^ ]*)", $mailbox, $regs)) { |
338 | $mailbox = str_replace(' ','',$mailbox); |
339 | $line .= str_replace(' ', ' ', $mailbox); |
2d367c68 |
340 | } |
95e93571 |
341 | $line .= '</FONT>'; |
342 | } else { |
343 | $line .= formatMailboxName($imapConnection, $boxes[$i]); |
344 | } |
2d367c68 |
345 | |
95e93571 |
346 | /* Put the final touches on our folder line. */ |
347 | $line .= "</NOBR><BR>\n"; |
2d367c68 |
348 | |
95e93571 |
349 | /* Output the line for this folder. */ |
350 | echo $line; |
2d367c68 |
351 | } |
95e93571 |
352 | } |
2d367c68 |
353 | |
95e93571 |
354 | sqimap_logout($imapConnection); |
355 | do_hook('left_main_after'); |
2d367c68 |
356 | |
95e93571 |
357 | echo "</BODY></HTML>\n"; |
2d367c68 |
358 | |
1c52ba77 |
359 | ?> |