]> git.donarmstrong.com Git - roundcube.git/blob - index.php
Imported Upstream version 0.2~alpha
[roundcube.git] / index.php
1 <?php
2 /*
3  +-------------------------------------------------------------------------+
4  | RoundCube Webmail IMAP Client                                           |
5  | Version 0.2-alpha                                                       |
6  |                                                                         |
7  | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland                   |
8  |                                                                         |
9  | This program is free software; you can redistribute it and/or modify    |
10  | it under the terms of the GNU General Public License version 2          |
11  | as published by the Free Software Foundation.                           |
12  |                                                                         |
13  | This program is distributed in the hope that it will be useful,         |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
16  | GNU General Public License for more details.                            |
17  |                                                                         |
18  | You should have received a copy of the GNU General Public License along |
19  | with this program; if not, write to the Free Software Foundation, Inc., |
20  | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.             |
21  |                                                                         |
22  +-------------------------------------------------------------------------+
23  | Author: Thomas Bruederli <roundcube@gmail.com>                          |
24  +-------------------------------------------------------------------------+
25
26  $Id: index.php 1499 2008-06-09 20:57:53Z thomasb $
27
28 */
29
30 // include environment
31 require_once 'program/include/iniset.php';
32
33 // define global vars
34 $OUTPUT_TYPE = 'html';
35
36 // set output buffering
37 if ($RCMAIL->action != 'get' && $RCMAIL->action != 'viewsource') {
38   // use gzip compression if supported
39   if (function_exists('ob_gzhandler')
40       && !ini_get('zlib.output_compression')
41       && ini_get('output_handler') != 'ob_gzhandler') {
42     ob_start('ob_gzhandler');
43   }
44   else {
45     ob_start();
46   }
47 }
48
49
50 // init application and start session with requested task
51 $RCMAIL = rcmail::get_instance();
52
53 // init output class
54 $OUTPUT = (!empty($_GET['_remote']) || !empty($_POST['_remote'])) ? $RCMAIL->init_json() : $RCMAIL->load_gui((!empty($_GET['_framed']) || !empty($_POST['_framed'])));
55
56
57 // check DB connections and exit on failure
58 if ($err_str = $DB->is_error()) {
59   raise_error(array(
60     'code' => 603,
61     'type' => 'db',
62     'message' => $err_str), FALSE, TRUE);
63 }
64
65
66 // error steps
67 if ($RCMAIL->action=='error' && !empty($_GET['_code'])) {
68   raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE);
69 }
70
71 // try to log in
72 if ($RCMAIL->action=='login' && $RCMAIL->task=='mail') {
73   $host = $RCMAIL->autoselect_host();
74   
75   // check if client supports cookies
76   if (empty($_COOKIE)) {
77     $OUTPUT->show_message("cookiesdisabled", 'warning');
78   }
79   else if ($_SESSION['temp'] && !empty($_POST['_user']) && isset($_POST['_pass']) &&
80            $RCMAIL->login(trim(get_input_value('_user', RCUBE_INPUT_POST), ' '),
81               get_input_value('_pass', RCUBE_INPUT_POST, true, 'ISO-8859-1'), $host)) {
82     // create new session ID
83     unset($_SESSION['temp']);
84     sess_regenerate_id();
85
86     // send auth cookie if necessary
87     $RCMAIL->authenticate_session();
88
89     // log successful login
90     if ($RCMAIL->config->get('log_logins') && $RCMAIL->config->get('debug_level') & 1)
91       console(sprintf('Successful login for %s (id %d) from %s',
92                       trim(get_input_value('_user', RCUBE_INPUT_POST), ' '),
93                       $_SESSION['user_id'],
94                       $_SERVER['REMOTE_ADDR']));
95
96     // send redirect
97     header("Location: {$RCMAIL->comm_path}");
98     exit;
99   }
100   else {
101     $OUTPUT->show_message($IMAP->error_code == -1 ? 'imaperror' : 'loginfailed', 'warning');
102     $RCMAIL->kill_session();
103   }
104 }
105
106 // end session
107 else if (($RCMAIL->task=='logout' || $RCMAIL->action=='logout') && isset($_SESSION['user_id'])) {
108   $OUTPUT->show_message('loggedout');
109   $RCMAIL->logout_actions();
110   $RCMAIL->kill_session();
111 }
112
113 // check session and auth cookie
114 else if ($RCMAIL->action != 'login' && $_SESSION['user_id'] && $RCMAIL->action != 'send') {
115   if (!$RCMAIL->authenticate_session()) {
116     $OUTPUT->show_message('sessionerror', 'error');
117     $RCMAIL->kill_session();
118   }
119 }
120
121
122 // log in to imap server
123 if (!empty($RCMAIL->user->ID) && $RCMAIL->task == 'mail') {
124   if (!$RCMAIL->imap_connect()) {
125     $RCMAIL->kill_session();
126   }
127 }
128
129
130 // not logged in -> set task to 'login
131 if (empty($RCMAIL->user->ID)) {
132   if ($OUTPUT->ajax_call)
133     $OUTPUT->remote_response("setTimeout(\"location.href='\"+this.env.comm_path+\"'\", 2000);");
134   
135   $RCMAIL->set_task('login');
136 }
137
138
139 // check client X-header to verify request origin
140 if ($OUTPUT->ajax_call) {
141   if (empty($CONFIG['devel_mode']) && !rc_request_header('X-RoundCube-Referer')) {
142     header('HTTP/1.1 404 Not Found');
143     die("Invalid Request");
144   }
145 }
146
147
148 // not logged in -> show login page
149 if (empty($RCMAIL->user->ID)) {
150   // check if installer is still active
151   if ($CONFIG['enable_installer'] && is_readable('./installer/index.php')) {
152     $OUTPUT->add_footer(html::div(array('style' => "background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em"),
153       html::tag('h2', array('style' => "margin-top:0.2em"), "Installer script is still accessible") .
154       html::p(null, "The install script of your RoundCube installation is still stored in its default location!") .
155       html::p(null, "Please <b>remove</b> the whole <tt>installer</tt> folder from the RoundCube directory because .
156         these files may expose sensitive configuration data like server passwords and encryption keys
157         to the public. Make sure you cannot access the <a href=\"./installer/\">installer script</a> from your browser.")
158       )
159     );
160   }
161   
162   $OUTPUT->set_env('task', 'login');
163   $OUTPUT->task = 'login';
164   $OUTPUT->send('login');
165   exit;
166 }
167
168
169 // handle keep-alive signal
170 if ($RCMAIL->action=='keep-alive') {
171   $OUTPUT->reset();
172   $OUTPUT->send('');
173   exit;
174 }
175
176 // include task specific files
177 if ($RCMAIL->task=='mail') {
178   include_once('program/steps/mail/func.inc');
179   
180   if ($RCMAIL->action=='show' || $RCMAIL->action=='preview' || $RCMAIL->action=='print')
181     include('program/steps/mail/show.inc');
182
183   if ($RCMAIL->action=='get')
184     include('program/steps/mail/get.inc');
185
186   if ($RCMAIL->action=='moveto' || $RCMAIL->action=='delete')
187     include('program/steps/mail/move_del.inc');
188
189   if ($RCMAIL->action=='mark')
190     include('program/steps/mail/mark.inc');
191
192   if ($RCMAIL->action=='viewsource')
193     include('program/steps/mail/viewsource.inc');
194
195   if ($RCMAIL->action=='sendmdn')
196     include('program/steps/mail/sendmdn.inc');
197
198   if ($RCMAIL->action=='send')
199     include('program/steps/mail/sendmail.inc');
200
201   if ($RCMAIL->action=='upload')
202     include('program/steps/mail/upload.inc');
203
204   if ($RCMAIL->action=='compose' || $RCMAIL->action=='remove-attachment' || $RCMAIL->action=='display-attachment')
205     include('program/steps/mail/compose.inc');
206
207   if ($RCMAIL->action=='addcontact')
208     include('program/steps/mail/addcontact.inc');
209
210   if ($RCMAIL->action=='expunge' || $RCMAIL->action=='purge')
211     include('program/steps/mail/folders.inc');
212
213   if ($RCMAIL->action=='check-recent')
214     include('program/steps/mail/check_recent.inc');
215
216   if ($RCMAIL->action=='getunread')
217     include('program/steps/mail/getunread.inc');
218     
219   if ($RCMAIL->action=='list' && isset($_REQUEST['_remote']))
220     include('program/steps/mail/list.inc');
221
222    if ($RCMAIL->action=='search')
223      include('program/steps/mail/search.inc');
224      
225   if ($RCMAIL->action=='spell')
226     include('program/steps/mail/spell.inc');
227
228   if ($RCMAIL->action=='rss')
229     include('program/steps/mail/rss.inc');
230     
231   // make sure the message count is refreshed
232   $IMAP->messagecount($_SESSION['mbox'], 'ALL', true);
233 }
234
235
236 // include task specific files
237 if ($RCMAIL->task=='addressbook') {
238   include_once('program/steps/addressbook/func.inc');
239
240   if ($RCMAIL->action=='save')
241     include('program/steps/addressbook/save.inc');
242   
243   if ($RCMAIL->action=='edit' || $RCMAIL->action=='add')
244     include('program/steps/addressbook/edit.inc');
245   
246   if ($RCMAIL->action=='delete')
247     include('program/steps/addressbook/delete.inc');
248
249   if ($RCMAIL->action=='show')
250     include('program/steps/addressbook/show.inc');  
251
252   if ($RCMAIL->action=='list' && $_REQUEST['_remote'])
253     include('program/steps/addressbook/list.inc');
254
255   if ($RCMAIL->action=='search')
256     include('program/steps/addressbook/search.inc');
257
258   if ($RCMAIL->action=='copy')
259     include('program/steps/addressbook/copy.inc');
260
261   if ($RCMAIL->action=='mailto')
262     include('program/steps/addressbook/mailto.inc');
263 }
264
265
266 // include task specific files
267 if ($RCMAIL->task=='settings') {
268   include_once('program/steps/settings/func.inc');
269
270   if ($RCMAIL->action=='save-identity')
271     include('program/steps/settings/save_identity.inc');
272
273   if ($RCMAIL->action=='add-identity' || $RCMAIL->action=='edit-identity')
274     include('program/steps/settings/edit_identity.inc');
275
276   if ($RCMAIL->action=='delete-identity')
277     include('program/steps/settings/delete_identity.inc');
278   
279   if ($RCMAIL->action=='identities')
280     include('program/steps/settings/identities.inc');  
281
282   if ($RCMAIL->action=='save-prefs')
283     include('program/steps/settings/save_prefs.inc');  
284
285   if ($RCMAIL->action=='folders' || $RCMAIL->action=='subscribe' || $RCMAIL->action=='unsubscribe' ||
286       $RCMAIL->action=='create-folder' || $RCMAIL->action=='rename-folder' || $RCMAIL->action=='delete-folder')
287     include('program/steps/settings/manage_folders.inc');
288 }
289
290
291 // parse main template
292 $OUTPUT->send($RCMAIL->task);
293
294
295 // if we arrive here, something went wrong
296 raise_error(array(
297   'code' => 404,
298   'type' => 'php',
299   'line' => __LINE__,
300   'file' => __FILE__,
301   'message' => "Invalid request"), true, true);
302                       
303 ?>