thomascube
2006-11-22 0a020cac3e62bdcb6d3912ab696d87517c871029
commit | author | age
4e17e6 1 <?php
T 2 /*
3  +-----------------------------------------------------------------------+
4  | RoundCube Webmail IMAP Client                                         |
e34ae1 5  | Version 0.1-20061122                                                  |
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
e34ae1 43 define('RCMAIL_VERSION', '0.1-20061122');
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');
a0109c 84 require_once('lib/html2text.inc');
7902df 85 require_once('PEAR.php');
T 86
87
88 // set PEAR error handling
89 // PEAR::setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_NOTICE);
4e17e6 90
T 91
92 // catch some url/post parameters
e34ae1 93 $_task = strip_quotes(get_input_value('_task', RCUBE_INPUT_GPC));
T 94 $_action = strip_quotes(get_input_value('_action', RCUBE_INPUT_GPC));
597170 95 $_framed = (!empty($_GET['_framed']) || !empty($_POST['_framed']));
42b113 96
e34ae1 97 // use main task if empty or invalid value
T 98 if (empty($_task) || !in_array($_task, $MAIN_TASKS))
03f855 99   $_task = 'mail';
T 100
42b113 101 if (!empty($_GET['_remote']))
T 102   $REMOTE_REQUEST = TRUE;
2f2f15 103   
T 104
105 // set output buffering
106 if ($_action != 'get' && $_action != 'viewsource')
107   {
108   // use gzip compression if supported
109   if (function_exists('ob_gzhandler') && !ini_get('zlib.output_compression'))
110     ob_start('ob_gzhandler');
111   else
112     ob_start();
113   }
114
42b113 115
4e17e6 116 // start session with requested task
T 117 rcmail_startup($_task);
118
119 // set session related variables
bac7d1 120 $COMM_PATH = sprintf('./?_task=%s', $_task);
T 121 $SESS_HIDDEN_FIELD = '';
4e17e6 122
T 123
124 // add framed parameter
597170 125 if ($_framed)
4e17e6 126   {
10c92b 127   $COMM_PATH .= '&amp;_framed=1';
ccfda8 128   $SESS_HIDDEN_FIELD .= "\n".'<input type="hidden" name="_framed" value="1" />';
4e17e6 129   }
T 130
131
132 // init necessary objects for GUI
133 load_gui();
134
8affba 135
T 136 // check DB connections and exit on failure
137 if ($err_str = $DB->is_error())
138   {
139   raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__,
140                     'message' => $err_str), FALSE, TRUE);
141   }
142
143
4e17e6 144 // error steps
597170 145 if ($_action=='error' && !empty($_GET['_code']))
4e17e6 146   {
T 147   raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE);
a0109c 148   }
S 149
150 // handle HTML->text conversion
151 if ($_action=='html2text')
152   {
153   $htmlText = $HTTP_RAW_POST_DATA;
154   $converter = new html2text($htmlText);
155
156   // TODO possibly replace with rcube_remote_response()
157   send_nocacheing_headers();
158   header('Content-Type: text/plain');
159   $plaintext = $converter->get_text();
160   print $plaintext;
161
162   exit;
4e17e6 163   }
T 164
165
166 // try to log in
167 if ($_action=='login' && $_task=='mail')
168   {
0a020c 169   $host = rcmail_autoselect_host();
4e17e6 170   
T 171   // check if client supports cookies
597170 172   if (empty($_COOKIE))
4e17e6 173     {
T 174     show_message("cookiesdisabled", 'warning');
175     }
ea7c46 176   else if (isset($_POST['_user']) && isset($_POST['_pass']) &&
0a020c 177            rcmail_login(get_input_value('_user', RCUBE_INPUT_POST),
T 178               get_input_value('_pass', RCUBE_INPUT_POST, true, 'ISO-8859-1'), $host))
4e17e6 179     {
T 180     // send redirect
181     header("Location: $COMM_PATH");
182     exit;
183     }
184   else
185     {
186     show_message("loginfailed", 'warning');
187     $_SESSION['user_id'] = '';
188     }
189   }
190
191 // end session
00fd33 192 else if ($_action=='logout' && isset($_SESSION['user_id']))
4e17e6 193   {
T 194   show_message('loggedout');
195   rcmail_kill_session();
196   }
197
bac7d1 198 // check session and auth cookie
T 199 else if ($_action!='login' && $_SESSION['user_id'])
4e17e6 200   {
bac7d1 201   if (!rcmail_authenticate_session() ||
e170b4 202       (!empty($CONFIG['session_lifetime']) && isset($SESS_CHANGED) && $SESS_CHANGED + $CONFIG['session_lifetime']*60 < mktime()))
4e17e6 203     {
42b113 204     $message = show_message('sessionerror', 'error');
4e17e6 205     rcmail_kill_session();
T 206     }
207   }
208
209
210 // log in to imap server
597170 211 if (!empty($_SESSION['user_id']) && $_task=='mail')
4e17e6 212   {
7902df 213   $conn = $IMAP->connect($_SESSION['imap_host'], $_SESSION['username'], decrypt_passwd($_SESSION['password']), $_SESSION['imap_port'], $_SESSION['imap_ssl']);
4e17e6 214   if (!$conn)
T 215     {
216     show_message('imaperror', 'error');
217     $_SESSION['user_id'] = '';
218     }
7902df 219   else
T 220     rcmail_set_imap_prop();
4e17e6 221   }
T 222
223
224 // not logged in -> set task to 'login
597170 225 if (empty($_SESSION['user_id']))
42b113 226   {
T 227   if ($REMOTE_REQUEST)
228     {
229     $message .= "setTimeout(\"location.href='\"+this.env.comm_path+\"'\", 2000);";
230     rcube_remote_response($message);
231     }
232   
4e17e6 233   $_task = 'login';
42b113 234   }
4e17e6 235
T 236
237
597170 238 // set task and action to client
4e17e6 239 $script = sprintf("%s.set_env('task', '%s');", $JS_OBJECT_NAME, $_task);
T 240 if (!empty($_action))
241   $script .= sprintf("\n%s.set_env('action', '%s');", $JS_OBJECT_NAME, $_action);
242
243 $OUTPUT->add_script($script);
244
245
246
247 // not logged in -> show login page
248 if (!$_SESSION['user_id'])
249   {
250   parse_template('login');
251   exit;
252   }
253
254
1cded8 255 // handle keep-alive signal
T 256 if ($_action=='keep-alive')
257   {
258   rcube_remote_response('');
259   exit;
260   }
4e17e6 261
T 262 // include task specific files
263 if ($_task=='mail')
264   {
265   include_once('program/steps/mail/func.inc');
88375f 266   
4e17e6 267   if ($_action=='show' || $_action=='print')
T 268     include('program/steps/mail/show.inc');
269
270   if ($_action=='get')
271     include('program/steps/mail/get.inc');
272
273   if ($_action=='moveto' || $_action=='delete')
274     include('program/steps/mail/move_del.inc');
275
276   if ($_action=='mark')
277     include('program/steps/mail/mark.inc');
278
279   if ($_action=='viewsource')
280     include('program/steps/mail/viewsource.inc');
281
282   if ($_action=='send')
283     include('program/steps/mail/sendmail.inc');
284
285   if ($_action=='upload')
286     include('program/steps/mail/upload.inc');
287
a894ba 288   if ($_action=='compose' || $_action=='remove-attachment')
4e17e6 289     include('program/steps/mail/compose.inc');
T 290
291   if ($_action=='addcontact')
292     include('program/steps/mail/addcontact.inc');
15a9d1 293
5e3512 294   if ($_action=='expunge' || $_action=='purge')
15a9d1 295     include('program/steps/mail/folders.inc');
T 296
297   if ($_action=='check-recent')
298     include('program/steps/mail/check_recent.inc');
299
300   if ($_action=='getunread')
301     include('program/steps/mail/getunread.inc');
4e17e6 302     
4647e1 303   if ($_action=='list' && isset($_GET['_remote']))
4e17e6 304     include('program/steps/mail/list.inc');
T 305
4647e1 306    if ($_action=='search')
dd53e2 307      include('program/steps/mail/search.inc');
T 308      
309   if ($_action=='spell')
310     include('program/steps/mail/spell.inc');
4647e1 311
88375f 312   if ($_action=='rss')
T 313     include('program/steps/mail/rss.inc');
3ea0e3 314     
T 315   if ($_action=='quotaimg')
316     include('program/steps/mail/quotaimg.inc');
88375f 317
aade7b 318
01c86f 319   // make sure the message count is refreshed
T 320   $IMAP->messagecount($_SESSION['mbox'], 'ALL', TRUE);
4e17e6 321   }
T 322
323
324 // include task specific files
325 if ($_task=='addressbook')
326   {
327   include_once('program/steps/addressbook/func.inc');
328
329   if ($_action=='save')
330     include('program/steps/addressbook/save.inc');
331   
332   if ($_action=='edit' || $_action=='add')
333     include('program/steps/addressbook/edit.inc');
334   
335   if ($_action=='delete')
336     include('program/steps/addressbook/delete.inc');
337
338   if ($_action=='show')
339     include('program/steps/addressbook/show.inc');  
340
341   if ($_action=='list' && $_GET['_remote'])
342     include('program/steps/addressbook/list.inc');
d1d2c4 343
S 344   if ($_action=='ldappublicsearch')
345     include('program/steps/addressbook/ldapsearchform.inc');
4e17e6 346   }
T 347
348
349 // include task specific files
350 if ($_task=='settings')
351   {
352   include_once('program/steps/settings/func.inc');
353
354   if ($_action=='save-identity')
355     include('program/steps/settings/save_identity.inc');
356
357   if ($_action=='add-identity' || $_action=='edit-identity')
358     include('program/steps/settings/edit_identity.inc');
359
360   if ($_action=='delete-identity')
361     include('program/steps/settings/delete_identity.inc');
362   
363   if ($_action=='identities')
364     include('program/steps/settings/identities.inc');  
365
366   if ($_action=='save-prefs')
367     include('program/steps/settings/save_prefs.inc');  
368
aade7b 369   if ($_action=='folders' || $_action=='subscribe' || $_action=='unsubscribe' ||
T 370       $_action=='create-folder' || $_action=='rename-folder' || $_action=='delete-folder')
4e17e6 371     include('program/steps/settings/manage_folders.inc');
T 372
ecf759 373   }
T 374
375
539cd4 376 // parse main template
e34ae1 377 parse_template($_task);
539cd4 378
T 379
380 // if we arrive here, something went wrong
381 raise_error(array('code' => 404,
382                   'type' => 'php',
383                   'line' => __LINE__,
384                   'file' => __FILE__,
385                   'message' => "Invalid request"), TRUE, TRUE);
386                       
d1d2c4 387 ?>