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