f77c21aa1b1a4e0246e783d6af9d35fd3bf528bb
[squirrelmail.git] / src / right_main.php
1 <?php
2 /**
3 * right_main.php
4 *
5 * Copyright (c) 1999-2004 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * This is where the mailboxes are listed. This controls most of what
9 * goes on in SquirrelMail.
10 *
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /**
16 * Path for SquirrelMail required files.
17 * @ignore
18 */
19 define('SM_PATH','../');
20
21 /* SquirrelMail required files. */
22 require_once(SM_PATH . 'include/validate.php');
23 require_once(SM_PATH . 'functions/global.php');
24 require_once(SM_PATH . 'functions/imap.php');
25 require_once(SM_PATH . 'functions/date.php');
26 require_once(SM_PATH . 'functions/mime.php');
27 require_once(SM_PATH . 'functions/mailbox_display.php');
28 require_once(SM_PATH . 'functions/display_messages.php');
29 require_once(SM_PATH . 'functions/html.php');
30 require_once(SM_PATH . 'functions/plugin.php');
31
32 /***********************************************************
33 * incoming variables from URL: *
34 * $sort Direction to sort by date *
35 * values: 0 - descending order *
36 * values: 1 - ascending order *
37 * $startMessage Message to start at *
38 * $mailbox Full Mailbox name *
39 * *
40 * incoming from cookie: *
41 * $key pass *
42 * incoming from session: *
43 * $username duh *
44 * *
45 ***********************************************************/
46
47
48 /* lets get the global vars we may need */
49 sqgetGlobalVar('key', $key, SQ_COOKIE);
50 sqgetGlobalVar('username', $username, SQ_SESSION);
51 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
52 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
53 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
54
55 sqgetGlobalVar('mailbox', $mailbox);
56 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
57 sqgetGlobalVar('numMessages' , $numMessages, SQ_SESSION);
58 sqgetGlobalVar('session', $session, SQ_GET);
59 sqgetGlobalVar('note', $note, SQ_GET);
60 sqgetGlobalVar('mail_sent', $mail_sent, SQ_GET);
61 sqgetGlobalVar('use_mailbox_cache', $use_mailbox_cache, SQ_GET);
62
63 if ( sqgetGlobalVar('startMessage', $temp) ) {
64 $startMessage = (int) $temp;
65 }
66 if ( sqgetGlobalVar('PG_SHOWNUM', $temp) ) {
67 $PG_SHOWNUM = (int) $temp;
68 }
69 if ( sqgetGlobalVar('PG_SHOWALL', $temp, SQ_GET) ) {
70 $PG_SHOWALL = (int) $temp;
71 }
72 if ( sqgetGlobalVar('newsort', $temp, SQ_GET) ) {
73 $newsort = (int) $temp;
74 }
75 if ( sqgetGlobalVar('checkall', $temp, SQ_GET) ) {
76 $checkall = (int) $temp;
77 }
78 if ( sqgetGlobalVar('set_thread', $temp, SQ_GET) ) {
79 $set_thread = (int) $temp;
80 }
81 if ( !sqgetGlobalVar('composenew', $composenew, SQ_GET) ) {
82 $composenew = false;
83 }
84 /* end of get globals */
85
86
87 /* Open a connection on the imap port (143) */
88
89 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
90
91 if (isset($PG_SHOWALL)) {
92 if ($PG_SHOWALL) {
93 $PG_SHOWNUM=999999;
94 $show_num=$PG_SHOWNUM;
95 sqsession_register($PG_SHOWNUM, 'PG_SHOWNUM');
96 }
97 else {
98 sqsession_unregister('PG_SHOWNUM');
99 unset($PG_SHOWNUM);
100 }
101 }
102 else if( isset( $PG_SHOWNUM ) ) {
103 $show_num = $PG_SHOWNUM;
104 }
105
106 if (isset($newsort) ) {
107 if ( $newsort != $sort )
108 setPref($data_dir, $username, 'sort', $newsort);
109
110 $sort = $newsort;
111 sqsession_register($sort, 'sort');
112 }
113
114 /* If the page has been loaded without a specific mailbox, */
115 /* send them to the inbox */
116 //if (!isset($mailbox)) {
117 // $mailbox = 'INBOX';
118 // $startMessage = 1;
119 //}
120
121 //if (!isset($startMessage) || ($startMessage == '')) {
122 // $startMessage = 1;
123 //}
124
125 /* compensate for the UW vulnerability. */
126 if ($imap_server_type == 'uw' && (strstr($mailbox, '../') ||
127 substr($mailbox, 0, 1) == '/')) {
128 $mailbox = 'INBOX';
129 }
130
131 do_hook ('generic_header');
132
133 $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
134 $aMbxResponse['SORT_ARRAY'] = false;
135
136 sqgetGlobalVar('aLastSelectedMailbox',$aLastSelectedMailbox,SQ_SESSION);
137
138 // deal with imap servers that do not return the required UIDNEXT or
139 // UIDVALIDITY response
140 // from a SELECT call (since rfc 3501 it's required)
141 if (!isset($aMbxResponse['UIDNEXT']) || !isset($aMbxResponse['UIDVALIDITY'])) {
142 $aStatus = sqimap_status_messages($imapConnection,$mailbox,
143 array('UIDNEXT','UIDVALIDITY'));
144 $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT'];
145 $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY'];
146 }
147
148 if ($aLastSelectedMailbox && !isset($newsort)) {
149 // check if we deal with the same mailbox
150 if ($aLastSelectedMailbox['NAME'] == $mailbox) {
151 if ($aLastSelectedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
152 $aLastSelectedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
153 $aLastSelectedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT']) {
154 // sort is still valid
155 sqgetGlobalVar('server_sort_array',$server_sort_array,SQ_SESSION);
156 if ($server_sort_array && is_array($server_sort_array)) {
157 $aMbxResponse['SORT_ARRAY'] = $server_sort_array;
158 }
159 }
160 }
161 }
162
163 $aLastSelectedMailbox['NAME'] = $mailbox;
164 $aLastSelectedMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
165 $aLastSelectedMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY'];
166 $aLastSelectedMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT'];
167 $aLastSelectedMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS'];
168 $aLastSelectedMailbox['OFFSET'] = (isset($startMessage) && $startMessage) ? $startMessage -1 : 0;
169 $aLastSelectedMailbox['PAGEOFFSET'] = (isset($startMessage) && $startMessage) ? $startMessage : 1;
170 $aLastSelectedMailbox['SORT'] = ($sort !== false) ? $sort : 0;
171 $aLastSelectedMailbox['LIMIT'] = $show_num;
172
173 $aLastSelectedMailbox['UIDSET'] = $aMbxResponse['SORT_ARRAY'];
174 $aLastSelectedMailbox['SEEN'] = (isset($aMbxResponse['SEEN'])) ? $aMbxResponse['SEEN'] : $aMbxResponse['EXISTS'];
175 $aLastSelectedMailbox['RECENT'] = (isset($aMbxResponse['RECENT'])) ? $aMbxResponse['RECENT'] : 0;
176
177 $aLastSelectedMailbox['AUTO_EXPUNGE'] = $auto_expunge;
178
179 /* decide if we are thread sorting or not */
180 $aLastSelectedMailbox['ALLOW_THREAD'] = $allow_thread_sort;
181 if ($allow_thread_sort == TRUE) {
182 if (isset($set_thread)) {
183 $aLastSelectedMailbox['SORT_ARRAY'] = false;
184 if (sqsession_is_registered('indent_array')) {
185 sqsession_unregister('indent_array');
186 }
187 if (sqsession_is_registered('server_sort_array')) {
188 sqsession_unregister('server_sort_array');
189 }
190 if ($set_thread == 1) {
191 setPref($data_dir, $username, "thread_$mailbox", 1);
192 $thread_sort_messages = '1';
193 } else if ($set_thread == 2) {
194 setPref($data_dir, $username, "thread_$mailbox", 0);
195 $thread_sort_messages = '0';
196 }
197 } else {
198 $thread_sort_messages = getPref($data_dir, $username, "thread_$mailbox");
199 }
200 } else {
201 $thread_sort_messages = 0;
202 }
203 if ($thread_sort_messages == 1) {
204 $aLastSelectedMailbox['SORT_METHOD'] = 'THREAD';
205 } else if ($allow_server_sort) {
206 $aLastSelectedMailbox['SORT_METHOD'] = 'SERVER';
207 } else {
208 $aLastSelectedMailbox['SORT_METHOD'] = 'SQUIRREL';
209 }
210
211 if ($composenew) {
212 $comp_uri = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
213 "&session=$session";
214 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
215 } else {
216 displayPageHeader($color, $mailbox);
217 }
218 do_hook('right_main_after_header');
219
220 /* display a message to the user that their mail has been sent */
221 if (isset($mail_sent) && $mail_sent == 'yes') {
222 $note = _("Your Message has been sent.");
223 }
224 if (isset($note)) {
225 echo html_tag( 'div', '<b>' . $note .'</b>', 'center' ) . "<br>\n";
226 }
227
228 if ( sqgetGlobalVar('just_logged_in', $just_logged_in, SQ_SESSION) ) {
229 if ($just_logged_in == true) {
230 $just_logged_in = false;
231 sqsession_register($just_logged_in, 'just_logged_in');
232
233 if (strlen(trim($motd)) > 0) {
234 echo html_tag( 'table',
235 html_tag( 'tr',
236 html_tag( 'td',
237 html_tag( 'table',
238 html_tag( 'tr',
239 html_tag( 'td', $motd, 'center' )
240 ) ,
241 '', $color[4], 'width="100%" cellpadding="5" cellspacing="1" border="0"' )
242 )
243 ) ,
244 'center', $color[9], 'width="70%" cellpadding="0" cellspacing="3" border="0"' );
245 }
246 }
247 }
248
249
250 /*********************************************************************
251 * Check to see if we can use cache or not. Currently the only time *
252 * when you will not use it is when a link on the left hand frame is *
253 * used. Also check to make sure we actually have the array in the *
254 * registered session data. :) *
255 *********************************************************************/
256 if (! isset($use_mailbox_cache)) {
257 $use_mailbox_cache = 0;
258 }
259
260 if ($use_mailbox_cache && sqsession_is_registered('msgs')) {
261 showMessagesForMailbox($imapConnection,$aLastSelectedMailbox);
262 // showMessagesForMailbox($imapConnection, $mailbox, $numMessages,
263 // $startMessage, $sort, $color, $show_num,
264 // $use_mailbox_cache, '',$aMbxResponse);
265 } else {
266 if (sqsession_is_registered('msgs')) {
267 unset($msgs);
268 }
269
270 if (sqsession_is_registered('msort')) {
271 unset($msort);
272 }
273
274 if (sqsession_is_registered('numMessages')) {
275 unset($numMessages);
276 }
277
278 $numMessages = $aMbxResponse['EXISTS'];
279
280 showMessagesForMailbox($imapConnection,$aLastSelectedMailbox);
281 // showMessagesForMailbox($imapConnection, $mailbox, $numMessages,
282 // $startMessage, $sort, $color, $show_num,
283 // $use_mailbox_cache,'',$aMbxResponse);
284
285 if (sqsession_is_registered('msgs') && isset($msgs)) {
286 sqsession_register($msgs, 'msgs');
287 }
288
289 if (sqsession_is_registered('msort') && isset($msort)) {
290 sqsession_register($msort, 'msort');
291 }
292
293 sqsession_register($numMessages, 'numMessages');
294 }
295 do_hook('right_main_bottom');
296 sqimap_logout ($imapConnection);
297 echo '</body></html>';
298
299 sqsession_register($aLastSelectedMailbox,'aLastSelectedMailbox');
300
301 ?>