thomascube
2006-08-06 aade7b98044ad4abb9021452688ec3af57f36acb
commit | author | age
4e17e6 1 <?php
T 2 /*
3  +-----------------------------------------------------------------------+
4  | RoundCube Webmail IMAP Client                                         |
aade7b 5  | Version 0.1-beta2                                                     |
4e17e6 6  |                                                                       |
aade7b 7  | Copyright (C) 2005-2006, RoundCube Dev. - Switzerland                 |
15fee7 8  | Licensed under the GNU GPL                                            |
4e17e6 9  |                                                                       |
T 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$
40
41 */
15a9d1 42
aade7b 43 define('RCMAIL_VERSION', '0.1-beta2');
15a9d1 44
4e17e6 45 // define global vars
8c2e58 46 $CHARSET = 'UTF-8';
4e17e6 47 $OUTPUT_TYPE = 'html';
T 48 $JS_OBJECT_NAME = 'rcmail';
321302 49 $INSTALL_PATH = dirname(__FILE__);
8c2e58 50 $MAIN_TASKS = array('mail','settings','addressbook','logout');
4e17e6 51
7cc38e 52 if (empty($INSTALL_PATH))
T 53   $INSTALL_PATH = './';
54 else
55   $INSTALL_PATH .= '/';
bac7d1 56
T 57
58 // make sure path_separator is defined
59 if (!defined('PATH_SEPARATOR'))
60   define('PATH_SEPARATOR', (eregi('win', PHP_OS) ? ';' : ':'));
61
62
d7cb77 63 // RC include folders MUST be included FIRST to avoid other
S 64 // possible not compatible libraries (i.e PEAR) to be included
65 // instead the ones provided by RC
7cc38e 66 ini_set('include_path', $INSTALL_PATH.PATH_SEPARATOR.$INSTALL_PATH.'program'.PATH_SEPARATOR.$INSTALL_PATH.'program/lib'.PATH_SEPARATOR.ini_get('include_path'));
d7cb77 67
4e17e6 68 ini_set('session.name', 'sessid');
T 69 ini_set('session.use_cookies', 1);
977a29 70 ini_set('session.gc_maxlifetime', 21600);
T 71 ini_set('session.gc_divisor', 500);
72 ini_set('error_reporting', E_ALL&~E_NOTICE); 
4e17e6 73
T 74 // increase maximum execution time for php scripts
00fd33 75 // (does not work in safe mode)
1cded8 76 @set_time_limit(120);
4e17e6 77
T 78 // include base files
79 require_once('include/rcube_shared.inc');
80 require_once('include/rcube_imap.inc');
81 require_once('include/bugs.inc');
82 require_once('include/main.inc');
83 require_once('include/cache.inc');
7902df 84 require_once('PEAR.php');
T 85
86
87 // set PEAR error handling
88 // PEAR::setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_NOTICE);
4e17e6 89
c1ba95 90 // use gzip compression if supported
8f4834 91 if (function_exists('ob_gzhandler') && !ini_get('zlib.output_compression'))
c1ba95 92   ob_start('ob_gzhandler');
8a256e 93 else
T 94   ob_start();
c1ba95 95
4e17e6 96
T 97 // catch some url/post parameters
03f855 98 $_task = get_input_value('_task', RCUBE_INPUT_GPC);
T 99 $_action = get_input_value('_action', RCUBE_INPUT_GPC);
597170 100 $_framed = (!empty($_GET['_framed']) || !empty($_POST['_framed']));
42b113 101
03f855 102 if (empty($_task))
T 103   $_task = 'mail';
104
42b113 105 if (!empty($_GET['_remote']))
T 106   $REMOTE_REQUEST = TRUE;
107
4e17e6 108 // start session with requested task
T 109 rcmail_startup($_task);
110
111 // set session related variables
bac7d1 112 $COMM_PATH = sprintf('./?_task=%s', $_task);
T 113 $SESS_HIDDEN_FIELD = '';
4e17e6 114
T 115
116 // add framed parameter
597170 117 if ($_framed)
4e17e6 118   {
T 119   $COMM_PATH .= '&_framed=1';
ccfda8 120   $SESS_HIDDEN_FIELD .= "\n".'<input type="hidden" name="_framed" value="1" />';
4e17e6 121   }
T 122
123
124 // init necessary objects for GUI
125 load_gui();
126
8affba 127
T 128 // check DB connections and exit on failure
129 if ($err_str = $DB->is_error())
130   {
131   raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__,
132                     'message' => $err_str), FALSE, TRUE);
133   }
134
135
4e17e6 136 // error steps
597170 137 if ($_action=='error' && !empty($_GET['_code']))
4e17e6 138   {
T 139   raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE);
140   }
141
142
143 // try to log in
144 if ($_action=='login' && $_task=='mail')
145   {
146   $host = $_POST['_host'] ? $_POST['_host'] : $CONFIG['default_host'];
147   
148   // check if client supports cookies
597170 149   if (empty($_COOKIE))
4e17e6 150     {
T 151     show_message("cookiesdisabled", 'warning');
152     }
ea7c46 153   else if (isset($_POST['_user']) && isset($_POST['_pass']) &&
bac7d1 154            rcmail_login(get_input_value('_user', RCUBE_INPUT_POST), $_POST['_pass'], $host))
4e17e6 155     {
T 156     // send redirect
157     header("Location: $COMM_PATH");
158     exit;
159     }
160   else
161     {
162     show_message("loginfailed", 'warning');
163     $_SESSION['user_id'] = '';
164     }
165   }
166
167 // end session
00fd33 168 else if ($_action=='logout' && isset($_SESSION['user_id']))
4e17e6 169   {
T 170   show_message('loggedout');
171   rcmail_kill_session();
172   }
173
bac7d1 174 // check session and auth cookie
T 175 else if ($_action!='login' && $_SESSION['user_id'])
4e17e6 176   {
bac7d1 177   if (!rcmail_authenticate_session() ||
ccfda8 178       ($CONFIG['session_lifetime'] && isset($SESS_CHANGED) && $SESS_CHANGED + $CONFIG['session_lifetime']*60 < mktime()))
4e17e6 179     {
42b113 180     $message = show_message('sessionerror', 'error');
4e17e6 181     rcmail_kill_session();
T 182     }
183   }
184
185
186 // log in to imap server
597170 187 if (!empty($_SESSION['user_id']) && $_task=='mail')
4e17e6 188   {
7902df 189   $conn = $IMAP->connect($_SESSION['imap_host'], $_SESSION['username'], decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl']);
4e17e6 190   if (!$conn)
T 191     {
192     show_message('imaperror', 'error');
193     $_SESSION['user_id'] = '';
194     }
7902df 195   else
T 196     rcmail_set_imap_prop();
4e17e6 197   }
T 198
199
200 // not logged in -> set task to 'login
597170 201 if (empty($_SESSION['user_id']))
42b113 202   {
T 203   if ($REMOTE_REQUEST)
204     {
205     $message .= "setTimeout(\"location.href='\"+this.env.comm_path+\"'\", 2000);";
206     rcube_remote_response($message);
207     }
208   
4e17e6 209   $_task = 'login';
42b113 210   }
4e17e6 211
T 212
213
597170 214 // set task and action to client
4e17e6 215 $script = sprintf("%s.set_env('task', '%s');", $JS_OBJECT_NAME, $_task);
T 216 if (!empty($_action))
217   $script .= sprintf("\n%s.set_env('action', '%s');", $JS_OBJECT_NAME, $_action);
218
219 $OUTPUT->add_script($script);
220
221
222
223 // not logged in -> show login page
224 if (!$_SESSION['user_id'])
225   {
226   parse_template('login');
227   exit;
228   }
229
230
1cded8 231 // handle keep-alive signal
T 232 if ($_action=='keep-alive')
233   {
234   rcube_remote_response('');
235   exit;
236   }
237
4e17e6 238
T 239 // include task specific files
240 if ($_task=='mail')
241   {
242   include_once('program/steps/mail/func.inc');
88375f 243   
4e17e6 244   if ($_action=='show' || $_action=='print')
T 245     include('program/steps/mail/show.inc');
246
247   if ($_action=='get')
248     include('program/steps/mail/get.inc');
249
250   if ($_action=='moveto' || $_action=='delete')
251     include('program/steps/mail/move_del.inc');
252
253   if ($_action=='mark')
254     include('program/steps/mail/mark.inc');
255
256   if ($_action=='viewsource')
257     include('program/steps/mail/viewsource.inc');
258
259   if ($_action=='send')
260     include('program/steps/mail/sendmail.inc');
261
262   if ($_action=='upload')
263     include('program/steps/mail/upload.inc');
264
a894ba 265   if ($_action=='compose' || $_action=='remove-attachment')
4e17e6 266     include('program/steps/mail/compose.inc');
T 267
268   if ($_action=='addcontact')
269     include('program/steps/mail/addcontact.inc');
15a9d1 270
5e3512 271   if ($_action=='expunge' || $_action=='purge')
15a9d1 272     include('program/steps/mail/folders.inc');
T 273
274   if ($_action=='check-recent')
275     include('program/steps/mail/check_recent.inc');
276
277   if ($_action=='getunread')
278     include('program/steps/mail/getunread.inc');
4e17e6 279     
4647e1 280   if ($_action=='list' && isset($_GET['_remote']))
4e17e6 281     include('program/steps/mail/list.inc');
T 282
4647e1 283    if ($_action=='search')
dd53e2 284      include('program/steps/mail/search.inc');
T 285      
286   if ($_action=='spell')
287     include('program/steps/mail/spell.inc');
4647e1 288
88375f 289   if ($_action=='rss')
T 290     include('program/steps/mail/rss.inc');
291
aade7b 292
01c86f 293   // make sure the message count is refreshed
T 294   $IMAP->messagecount($_SESSION['mbox'], 'ALL', TRUE);
4e17e6 295   }
T 296
297
298 // include task specific files
299 if ($_task=='addressbook')
300   {
301   include_once('program/steps/addressbook/func.inc');
302
303   if ($_action=='save')
304     include('program/steps/addressbook/save.inc');
305   
306   if ($_action=='edit' || $_action=='add')
307     include('program/steps/addressbook/edit.inc');
308   
309   if ($_action=='delete')
310     include('program/steps/addressbook/delete.inc');
311
312   if ($_action=='show')
313     include('program/steps/addressbook/show.inc');  
314
315   if ($_action=='list' && $_GET['_remote'])
316     include('program/steps/addressbook/list.inc');
d1d2c4 317
S 318   if ($_action=='ldappublicsearch')
319     include('program/steps/addressbook/ldapsearchform.inc');
4e17e6 320   }
T 321
322
323 // include task specific files
324 if ($_task=='settings')
325   {
326   include_once('program/steps/settings/func.inc');
327
328   if ($_action=='save-identity')
329     include('program/steps/settings/save_identity.inc');
330
331   if ($_action=='add-identity' || $_action=='edit-identity')
332     include('program/steps/settings/edit_identity.inc');
333
334   if ($_action=='delete-identity')
335     include('program/steps/settings/delete_identity.inc');
336   
337   if ($_action=='identities')
338     include('program/steps/settings/identities.inc');  
339
340   if ($_action=='save-prefs')
341     include('program/steps/settings/save_prefs.inc');  
342
aade7b 343   if ($_action=='folders' || $_action=='subscribe' || $_action=='unsubscribe' ||
T 344       $_action=='create-folder' || $_action=='rename-folder' || $_action=='delete-folder')
4e17e6 345     include('program/steps/settings/manage_folders.inc');
T 346
ecf759 347   }
T 348
349
539cd4 350 // parse main template
8c2e58 351 // only allow these templates to be included
T 352 if (in_array($_task, $MAIN_TASKS))
539cd4 353   parse_template($_task);
T 354
355
356 // if we arrive here, something went wrong
357 raise_error(array('code' => 404,
358                   'type' => 'php',
359                   'line' => __LINE__,
360                   'file' => __FILE__,
361                   'message' => "Invalid request"), TRUE, TRUE);
362                       
d1d2c4 363 ?>