]> git.donarmstrong.com Git - roundcube.git/blob - index.php
Merge commit 'debian/0.1-1' into debian
[roundcube.git] / index.php
1 <?php
2 /*
3  +-----------------------------------------------------------------------+
4  | RoundCube Webmail IMAP Client                                         |
5  | Version 0.1-20080302                                                  |
6  |                                                                       |
7  | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
8  | Licensed under the GNU GPL                                            |
9  |                                                                       |
10  | Redistribution and use in source and binary forms, with or without    |
11  | modification, are permitted provided that the following conditions    |
12  | are met:                                                              |
13  |                                                                       |
14  | o Redistributions of source code must retain the above copyright      |
15  |   notice, this list of conditions and the following disclaimer.       |
16  | o Redistributions in binary form must reproduce the above copyright   |
17  |   notice, this list of conditions and the following disclaimer in the |
18  |   documentation and/or other materials provided with the distribution.|
19  | o The names of the authors may not be used to endorse or promote      |
20  |   products derived from this software without specific prior written  |
21  |   permission.                                                         |
22  |                                                                       |
23  | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
24  | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
25  | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
26  | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
27  | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
28  | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
29  | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
30  | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
31  | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
32  | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
33  | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
34  |                                                                       |
35  +-----------------------------------------------------------------------+
36  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
37  +-----------------------------------------------------------------------+
38
39  $Id: index.php 1162 2008-03-02 13:20:21Z thomasb $
40
41 */
42
43 // application constants
44 define('RCMAIL_VERSION', '0.1-trunk');
45 define('RCMAIL_CHARSET', 'UTF-8');
46 define('JS_OBJECT_NAME', 'rcmail');
47
48 // define global vars
49 $OUTPUT_TYPE = 'html';
50 $INSTALL_PATH = dirname(__FILE__);
51 $MAIN_TASKS = array('mail','settings','addressbook','logout');
52
53 if (empty($INSTALL_PATH))
54   $INSTALL_PATH = './';
55 else
56   $INSTALL_PATH .= '/';
57
58
59 // make sure path_separator is defined
60 if (!defined('PATH_SEPARATOR'))
61   define('PATH_SEPARATOR', (eregi('win', PHP_OS) ? ';' : ':'));
62
63
64 // RC include folders MUST be included FIRST to avoid other
65 // possible not compatible libraries (i.e PEAR) to be included
66 // instead the ones provided by RC
67 ini_set('include_path', $INSTALL_PATH.PATH_SEPARATOR.$INSTALL_PATH.'program'.PATH_SEPARATOR.$INSTALL_PATH.'program/lib'.PATH_SEPARATOR.ini_get('include_path'));
68
69 ini_set('session.name', 'roundcube_sessid');
70 ini_set('session.use_cookies', 1);
71 ini_set('session.gc_maxlifetime', 21600);
72 ini_set('session.gc_divisor', 500);
73 ini_set('error_reporting', E_ALL&~E_NOTICE); 
74 set_magic_quotes_runtime(0);
75
76 // increase maximum execution time for php scripts
77 // (does not work in safe mode)
78 if (!ini_get('safe_mode')) @set_time_limit(120);
79
80 // include base files
81 require_once('include/rcube_shared.inc');
82 require_once('include/rcube_imap.inc');
83 require_once('include/bugs.inc');
84 require_once('include/main.inc');
85 require_once('PEAR.php');
86
87
88 // set PEAR error handling
89 // PEAR::setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_NOTICE);
90
91
92 // catch some url/post parameters
93 $_task = strip_quotes(get_input_value('_task', RCUBE_INPUT_GPC));
94 $_action = strip_quotes(get_input_value('_action', RCUBE_INPUT_GPC));
95 $_framed = (!empty($_GET['_framed']) || !empty($_POST['_framed']));
96
97 // use main task if empty or invalid value
98 if (empty($_task) || !in_array($_task, $MAIN_TASKS))
99   $_task = 'mail';
100
101
102 // set output buffering
103 if ($_action != 'get' && $_action != 'viewsource')
104 {
105   // use gzip compression if supported
106   if (function_exists('ob_gzhandler') && !ini_get('zlib.output_compression'))
107     ob_start('ob_gzhandler');
108   else
109     ob_start();
110 }
111
112
113 // start session with requested task
114 rcmail_startup($_task);
115
116 // set session related variables
117 $COMM_PATH = sprintf('./?_task=%s', $_task);
118 $SESS_HIDDEN_FIELD = '';
119
120
121 // add framed parameter
122 if ($_framed)
123 {
124   $COMM_PATH .= '&_framed=1';
125   $SESS_HIDDEN_FIELD .= "\n".'<input type="hidden" name="_framed" value="1" />';
126 }
127
128
129 // init necessary objects for GUI
130 rcmail_load_gui();
131
132
133 // check DB connections and exit on failure
134 if ($err_str = $DB->is_error())
135 {
136   raise_error(array(
137     'code' => 603,
138     'type' => 'db',
139     'message' => $err_str), FALSE, TRUE);
140 }
141
142
143 // error steps
144 if ($_action=='error' && !empty($_GET['_code']))
145   raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE);
146
147
148 // try to log in
149 if ($_action=='login' && $_task=='mail')
150 {
151   $host = rcmail_autoselect_host();
152   
153   // check if client supports cookies
154   if (empty($_COOKIE))
155   {
156     $OUTPUT->show_message("cookiesdisabled", 'warning');
157   }
158   else if ($_SESSION['temp'] && !empty($_POST['_user']) && isset($_POST['_pass']) &&
159            rcmail_login(get_input_value('_user', RCUBE_INPUT_POST),
160               get_input_value('_pass', RCUBE_INPUT_POST, true, 'ISO-8859-1'), $host))
161   {
162     // create new session ID
163     unset($_SESSION['temp']);
164     sess_regenerate_id();
165
166     // send auth cookie if necessary
167     rcmail_authenticate_session();
168
169     // send redirect
170     header("Location: $COMM_PATH");
171     exit;
172   }
173   else
174   {
175     $OUTPUT->show_message($IMAP->error_code == -1 ? 'imaperror' : 'loginfailed', 'warning');
176     rcmail_kill_session();
177   }
178 }
179
180 // end session
181 else if (($_task=='logout' || $_action=='logout') && isset($_SESSION['user_id']))
182 {
183   $OUTPUT->show_message('loggedout');
184   rcmail_kill_session();
185 }
186
187 // check session and auth cookie
188 else if ($_action != 'login' && $_SESSION['user_id'] && $_action != 'send')
189 {
190   if (!rcmail_authenticate_session())
191   {
192     $OUTPUT->show_message('sessionerror', 'error');
193     rcmail_kill_session();
194   }
195 }
196
197
198 // log in to imap server
199 if (!empty($USER->ID) && $_task=='mail')
200 {
201   $conn = $IMAP->connect($_SESSION['imap_host'], $_SESSION['username'], decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl']);
202   if (!$conn)
203   {
204     $OUTPUT->show_message($IMAP->error_code == -1 ? 'imaperror' : 'sessionerror', 'error');
205     rcmail_kill_session();
206   }
207   else
208     rcmail_set_imap_prop();
209 }
210
211
212 // not logged in -> set task to 'login
213 if (empty($USER->ID))
214 {
215   if ($OUTPUT->ajax_call)
216     $OUTPUT->remote_response("setTimeout(\"location.href='\"+this.env.comm_path+\"'\", 2000);");
217   
218   $_task = 'login';
219 }
220
221
222 // check client X-header to verify request origin
223 if ($OUTPUT->ajax_call)
224 {
225   if (empty($CONFIG['devel_mode']) && !rc_request_header('X-RoundCube-Referer'))
226   {
227     header('HTTP/1.1 404 Not Found');
228     die("Invalid Request");
229   }
230 }
231
232
233 // set task and action to client
234 $OUTPUT->set_env('task', $_task);
235 if (!empty($_action))
236   $OUTPUT->set_env('action', $_action);
237
238
239
240 // not logged in -> show login page
241 if (empty($USER->ID))
242 {
243   // check if installer is still active
244   if (is_file('./installer/index.php'))
245     $OUTPUT->add_footer('
246   <div style="background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em">
247   <h2 style="margin-top:0.2em">Installer script is still accessible</h2>
248   <p>The install script of your RoundCube installation is still stored in its default location!</p>
249   <p>Please <b>remove</b> the whole <tt>installer</tt> folder from the RoundCube directory because
250   these files may expose sensitive configuration data like server passwords and encryption keys
251   to the public. Make sure you cannot access the <a href="./installer/">installer script</a> from your browser.</p>
252   </div>');
253   
254   $OUTPUT->task = 'login';
255   $OUTPUT->send('login');
256   exit;
257 }
258
259
260 // handle keep-alive signal
261 if ($_action=='keep-alive')
262 {
263   $OUTPUT->reset();
264   $OUTPUT->send('');
265   exit;
266 }
267
268 // include task specific files
269 if ($_task=='mail')
270 {
271   include_once('program/steps/mail/func.inc');
272   
273   if ($_action=='show' || $_action=='preview' || $_action=='print')
274     include('program/steps/mail/show.inc');
275
276   if ($_action=='get')
277     include('program/steps/mail/get.inc');
278
279   if ($_action=='moveto' || $_action=='delete')
280     include('program/steps/mail/move_del.inc');
281
282   if ($_action=='mark')
283     include('program/steps/mail/mark.inc');
284
285   if ($_action=='viewsource')
286     include('program/steps/mail/viewsource.inc');
287
288   if ($_action=='sendmdn')
289     include('program/steps/mail/sendmdn.inc');
290
291   if ($_action=='send')
292     include('program/steps/mail/sendmail.inc');
293
294   if ($_action=='upload')
295     include('program/steps/mail/upload.inc');
296
297   if ($_action=='compose' || $_action=='remove-attachment' || $_action=='display-attachment')
298     include('program/steps/mail/compose.inc');
299
300   if ($_action=='addcontact')
301     include('program/steps/mail/addcontact.inc');
302
303   if ($_action=='expunge' || $_action=='purge')
304     include('program/steps/mail/folders.inc');
305
306   if ($_action=='check-recent')
307     include('program/steps/mail/check_recent.inc');
308
309   if ($_action=='getunread')
310     include('program/steps/mail/getunread.inc');
311     
312   if ($_action=='list' && isset($_REQUEST['_remote']))
313     include('program/steps/mail/list.inc');
314
315    if ($_action=='search')
316      include('program/steps/mail/search.inc');
317      
318   if ($_action=='spell')
319     include('program/steps/mail/spell.inc');
320
321   if ($_action=='rss')
322     include('program/steps/mail/rss.inc');
323     
324   if ($_action=='quotadisplay')
325     include('program/steps/mail/quotadisplay.inc');
326
327
328   // make sure the message count is refreshed
329   $IMAP->messagecount($_SESSION['mbox'], 'ALL', TRUE);
330 }
331
332
333 // include task specific files
334 if ($_task=='addressbook')
335 {
336   include_once('program/steps/addressbook/func.inc');
337
338   if ($_action=='save')
339     include('program/steps/addressbook/save.inc');
340   
341   if ($_action=='edit' || $_action=='add')
342     include('program/steps/addressbook/edit.inc');
343   
344   if ($_action=='delete')
345     include('program/steps/addressbook/delete.inc');
346
347   if ($_action=='show')
348     include('program/steps/addressbook/show.inc');  
349
350   if ($_action=='list' && $_REQUEST['_remote'])
351     include('program/steps/addressbook/list.inc');
352
353   if ($_action=='search')
354     include('program/steps/addressbook/search.inc');
355
356   if ($_action=='copy')
357     include('program/steps/addressbook/copy.inc');
358
359   if ($_action=='mailto')
360     include('program/steps/addressbook/mailto.inc');
361 }
362
363
364 // include task specific files
365 if ($_task=='settings')
366 {
367   include_once('program/steps/settings/func.inc');
368
369   if ($_action=='save-identity')
370     include('program/steps/settings/save_identity.inc');
371
372   if ($_action=='add-identity' || $_action=='edit-identity')
373     include('program/steps/settings/edit_identity.inc');
374
375   if ($_action=='delete-identity')
376     include('program/steps/settings/delete_identity.inc');
377   
378   if ($_action=='identities')
379     include('program/steps/settings/identities.inc');  
380
381   if ($_action=='save-prefs')
382     include('program/steps/settings/save_prefs.inc');  
383
384   if ($_action=='folders' || $_action=='subscribe' || $_action=='unsubscribe' ||
385       $_action=='create-folder' || $_action=='rename-folder' || $_action=='delete-folder')
386     include('program/steps/settings/manage_folders.inc');
387
388 }
389
390
391 // parse main template
392 $OUTPUT->send($_task);
393
394
395 // if we arrive here, something went wrong
396 raise_error(array(
397   'code' => 404,
398   'type' => 'php',
399   'line' => __LINE__,
400   'file' => __FILE__,
401   'message' => "Invalid request"), TRUE, TRUE);
402                       
403 ?>