Cleaning
[squirrelmail.git] / src / left_main.php
CommitLineData
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
7a906c8e 27 if ($auto_create_special && ! isset($auto_create_done)) {
d6def997 28 if (isset ($sent_folder) && $sent_folder != 'none') {
f7bfc9de 29 if (!sqimap_mailbox_exists ($imapConnection, $sent_folder)) {
d6def997 30 sqimap_mailbox_create ($imapConnection, $sent_folder, '');
8d636967 31 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $sent_folder)) {
99c7a62c 32 sqimap_subscribe($imapConnection, $sent_folder);
f7bfc9de 33 }
34 }
d6def997 35 if (isset ($trash_folder) && $trash_folder != 'none') {
f7bfc9de 36 if (!sqimap_mailbox_exists ($imapConnection, $trash_folder)) {
d6def997 37 sqimap_mailbox_create ($imapConnection, $trash_folder, '');
8d636967 38 } else if (! sqimap_mailbox_is_subscribed($imapConnection, $trash_folder)) {
99c7a62c 39 sqimap_subscribe($imapConnection, $trash_folder);
f7bfc9de 40 }
d3b3cda7 41 }
7a906c8e 42 $auto_create_done = true;
43 session_register('auto_create_done');
f7bfc9de 44 }
45
235a65d5 46 function formatMailboxName($imapConnection, $box_array, $delimeter) {
5bdd7223 47 global $folder_prefix, $trash_folder, $sent_folder;
48 global $color, $move_to_sent, $move_to_trash;
235a65d5 49 global $unseen_notify, $unseen_type, $collapse_folders;
50
51 $real_box = $box_array['unformatted'];
a6d2e0de 52 $mailbox = str_replace('&nbsp;','',$box_array['formatted']);
5c42b0dd 53 $mailboxURL = urlencode($real_box);
a6d2e0de 54
55 /* Strip down the mailbox name. */
56 if (ereg("^( *)([^ ]*)$", $mailbox, $regs)) {
57 $mailbox = $regs[2];
58 }
0cd84d75 59
235a65d5 60 $unseen = 0;
4d2c01e3 61
d6def997 62 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
235a65d5 63 $unseen_notify == 3) {
4d2c01e3 64 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
24fc5dd2 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 }
5bdd7223 74
7ce342dc 75 $special_color = false;
d6def997 76 if ((strtolower($real_box) == 'inbox') ||
235a65d5 77 (($real_box == $trash_folder) && ($move_to_trash)) ||
78 (($real_box == $sent_folder) && ($move_to_sent)))
79 $special_color = true;
80
a6d2e0de 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. */
0afa6558 88 $line .= "<A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$mailboxURL\" TARGET=\"right\" STYLE=\"text-decoration:none\">";
235a65d5 89 if ($special_color == true)
90 $line .= "<FONT COLOR=\"$color[11]\">";
d3b3cda7 91 $line .= str_replace(' ','&nbsp;',$mailbox);
235a65d5 92 if ($special_color == true)
a6d2e0de 93 $line .= "</FONT>";
94 $line .= '</A>';
7ce342dc 95
a6d2e0de 96 /* If there are unseen message, close bolding. */
97 if ($unseen > 0) { $line .= "</B>"; }
235a65d5 98
a6d2e0de 99 /* Print unseen information. */
245a6892 100 if (isset($unseen_found) && $unseen_found) {
a6d2e0de 101 $line .= "&nbsp;<SMALL>$unseen_string</SMALL>";
7ce342dc 102 }
103
1e0628fb 104 if (($move_to_trash == true) && ($real_box == $trash_folder)) {
a6d2e0de 105 if (! isset($numMessages)) {
b4cf620b 106 $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
a6d2e0de 107 }
b4cf620b 108
a6d2e0de 109 if ($numMessages > 0) {
b4cf620b 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";
12d61439 114 }
d92b6f31 115 }
d92b6f31 116
a6d2e0de 117 /* Return the final product. */
118 return ($line);
469eb37b 119 }
a6d2e0de 120
d6def997 121 if (isset($left_refresh) && ($left_refresh != 'None') && ($left_refresh != '')) {
a6d2e0de 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 }
813eba2f 126
a6d2e0de 127 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
235a65d5 128
a6d2e0de 129 do_hook("left_main_before");
235a65d5 130
a6d2e0de 131 $boxes = sqimap_mailbox_list($imapConnection);
132
d6def997 133 echo '<CENTER><FONT SIZE=4><B>';
a6d2e0de 134 echo _("Folders") . "</B><BR></FONT>\n\n";
135
d6def997 136 echo '<SMALL>(<A HREF="../src/left_main.php" TARGET="left">';
a6d2e0de 137 echo _("refresh folder list");
d6def997 138 echo '</A>)</SMALL></CENTER><BR>';
a6d2e0de 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;
235a65d5 164
a6d2e0de 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);
235a65d5 172 }
a6d2e0de 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 }
235a65d5 218
a6d2e0de 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 }
d6def997 281 echo "</BODY></HTML>\n";
282?>