389c6a9c7c6575c06678958fe3915e7669ce6b59
[squirrelmail.git] / src / redirect.php
1 <?php
2
3 /**
4 * Prevents users from reposting their form data after a successful logout.
5 *
6 * Derived from webmail.php by Ralf Kraudelt <kraude@wiwi.uni-rostock.de>
7 *
8 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 */
13 $sInitLocation = 'redirect';
14
15 /**
16 * Include the SquirrelMail initialization file.
17 */
18 require('../include/init.php');
19
20 /* SquirrelMail required files. */
21 require_once(SM_PATH . 'functions/imap_general.php');
22 require_once(SM_PATH . 'functions/strings.php');
23
24 header('Pragma: no-cache');
25 $location = get_location();
26
27 // session_set_cookie_params (0, $base_uri);
28
29 sqsession_unregister ('user_is_logged_in');
30 sqsession_register ($base_uri, 'base_uri');
31
32 /* get globals we me need */
33 sqGetGlobalVar('login_username', $login_username);
34 sqGetGlobalVar('secretkey', $secretkey);
35 if(!sqGetGlobalVar('squirrelmail_language', $squirrelmail_language) || $squirrelmail_language == '') {
36 $squirrelmail_language = $squirrelmail_default_language;
37 }
38 if (!sqgetGlobalVar('mailtodata', $mailtodata)) {
39 $mailtodata = '';
40 }
41
42 /* end of get globals */
43
44 set_up_language($squirrelmail_language, true);
45 /* Refresh the language cookie. */
46 sqsetcookie('squirrelmail_language', $squirrelmail_language, time()+2592000,
47 $base_uri);
48
49 if (!isset($login_username)) {
50 logout_error( _("You must be logged in to access this page.") );
51 exit;
52 }
53
54 if (!sqsession_is_registered('user_is_logged_in')) {
55 do_hook('login_before', $null);
56
57 $onetimepad = OneTimePadCreate(strlen($secretkey));
58 $key = OneTimePadEncrypt($secretkey, $onetimepad);
59
60 /* remove redundant spaces */
61 $login_username = trim($login_username);
62
63 /* Verify that username and password are correct. */
64 if ($force_username_lowercase) {
65 $login_username = strtolower($login_username);
66 }
67
68 $imapConnection = sqimap_login($login_username, $key, $imapServerAddress, $imapPort, 0);
69 /* From now on we are logged it. If the login failed then sqimap_login handles it */
70
71 /* regenerate the session id to avoid session hyijacking */
72 sqsession_destroy();
73 @sqsession_is_active();
74 session_regenerate_id();
75 /**
76 * The cookie part. session_start and session_regenerate_session normally set
77 * their own cookie. SquirrelMail sets another cookie which overwites the
78 * php cookies. The sqsetcookie function sets the cookie by using the header
79 * function which gives us full control how the cookie is set. We do that
80 * to add the HttpOnly cookie attribute which blocks javascript access on
81 * IE6 SP1.
82 * sqsetcookieflush is needed to send out the headers. sqsetcookie caches
83 * the cookies to be send. If we don't do that we only can send 1 single cookie
84 * which is not sufficient.
85 */
86 sqsetcookie(session_name(),session_id(),false,$base_uri);
87 sqsetcookie('key', $key, false, $base_uri);
88 sqsetcookieflush();
89
90 sqsession_register($onetimepad, 'onetimepad');
91
92 $sqimap_capabilities = sqimap_capability($imapConnection);
93
94 /* Server side sorting control */
95 if (isset($sqimap_capabilities['SORT']) && $sqimap_capabilities['SORT'] == true &&
96 isset($disable_server_sort) && $disable_server_sort) {
97 unset($sqimap_capabilities['SORT']);
98 }
99
100 /* Thread sort control */
101 if (isset($sqimap_capabilities['THREAD']) && $sqimap_capabilities['THREAD'] == true &&
102 isset($disable_thread_sort) && $disable_thread_sort) {
103 unset($sqimap_capabilities['THREAD']);
104 }
105
106 sqsession_register($sqimap_capabilities, 'sqimap_capabilities');
107 $delimiter = sqimap_get_delimiter ($imapConnection);
108
109 if (isset($sqimap_capabilities['NAMESPACE']) && $sqimap_capabilities['NAMESPACE'] == true) {
110 $namespace = sqimap_get_namespace($imapConnection);
111 sqsession_register($namespace, 'sqimap_namespace');
112 }
113
114 sqimap_logout($imapConnection);
115 sqsession_register($delimiter, 'delimiter');
116
117 $username = $login_username;
118 sqsession_register ($username, 'username');
119 do_hook('login_verified', $null);
120 }
121
122 /* Set the login variables. */
123 $user_is_logged_in = true;
124 $just_logged_in = true;
125
126 /* And register with them with the session. */
127 sqsession_register ($user_is_logged_in, 'user_is_logged_in');
128 sqsession_register ($just_logged_in, 'just_logged_in');
129
130 /* parse the accepted content-types of the client */
131 $attachment_common_types = array();
132 $attachment_common_types_parsed = array();
133 sqsession_register($attachment_common_types, 'attachment_common_types');
134 sqsession_register($attachment_common_types_parsed, 'attachment_common_types_parsed');
135
136 $debug = false;
137
138 if ( sqgetGlobalVar('HTTP_ACCEPT', $http_accept, SQ_SERVER) &&
139 !isset($attachment_common_types_parsed[$http_accept]) ) {
140 attachment_common_parse($http_accept, $debug);
141 }
142
143 /* Complete autodetection of Javascript. */
144 checkForJavascript();
145
146 /* Compute the URL to forward the user to. */
147 $redirect_url = $location . '/webmail.php';
148
149 if ( sqgetGlobalVar('session_expired_location', $session_expired_location, SQ_SESSION) ) {
150 sqsession_unregister('session_expired_location');
151 if ( strpos($session_expired_location, 'compose.php') !== FALSE ) {
152 $compose_new_win = getPref($data_dir, $username, 'compose_new_win', 0);
153 if ($compose_new_win) {
154 // do not prefix $location here because $session_expired_location is set to PHP_SELF
155 // of the last page
156 $redirect_url = $session_expired_location;
157 } else {
158 $redirect_url = $location.'/webmail.php?right_frame='.urldecode($session_expired_location);
159 }
160 }
161 unset($session_expired_location);
162 }
163
164 if($mailtodata != '') {
165 $redirect_url = $location . '/webmail.php?right_frame=compose.php&mailtodata=';
166 $redirect_url .= urlencode($mailtodata);
167 }
168
169 /* Write session data and send them off to the appropriate page. */
170 session_write_close();
171 header("Location: $redirect_url");
172 exit;
173
174 /* --------------------- end main ----------------------- */
175
176 function attachment_common_parse($str, $debug) {
177 global $attachment_common_types, $attachment_common_types_parsed;
178
179 $attachment_common_types_parsed[$str] = true;
180
181 /*
182 * Replace ", " with "," and explode on that as Mozilla 1.x seems to
183 * use "," to seperate whilst IE, and earlier versions of Mozilla use
184 * ", " to seperate
185 */
186
187 $str = str_replace( ', ' , ',' , $str );
188 $types = explode(',', $str);
189
190 foreach ($types as $val) {
191 // Ignore the ";q=1.0" stuff
192 if (strpos($val, ';') !== false)
193 $val = substr($val, 0, strpos($val, ';'));
194
195 if (! isset($attachment_common_types[$val])) {
196 $attachment_common_types[$val] = true;
197 }
198 }
199 sqsession_register($attachment_common_types, 'attachment_common_types');
200 }