| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | +-----------------------------------------------------------------------+ |
|---|
| 4 | | RoundCube Webmail IMAP Client | |
|---|
| 5 | | Version 0.1-rc1 | |
|---|
| 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 579 2007-05-18 13:11:22Z thomasb $ |
|---|
| 40 | |
|---|
| 41 | */ |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * tfk_debug |
|---|
| 45 | * |
|---|
| 46 | * @param string $str |
|---|
| 47 | * @ignore |
|---|
| 48 | */ |
|---|
| 49 | function tfk_debug($str) |
|---|
| 50 | { |
|---|
| 51 | $str = "\n\n" . @date('Y-m-d H:i:s') . "\n" . $str; |
|---|
| 52 | $fp = @fopen(dirname(__FILE__) . '/logs/debug.tfk', 'a'); |
|---|
| 53 | if ($fp !== false) { |
|---|
| 54 | @fwrite($fp, $str); |
|---|
| 55 | @fclose($fp); |
|---|
| 56 | } else { |
|---|
| 57 | die('Could not open logs/debug.tfk.'); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | /** |
|---|
| 61 | * log all $_POST |
|---|
| 62 | * @author Till Klampaeckel <till@php.net> |
|---|
| 63 | * @ignore |
|---|
| 64 | */ |
|---|
| 65 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
|---|
| 66 | tfk_debug(var_export($_POST, true)); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | // application constants |
|---|
| 71 | define('RCMAIL_VERSION', '0.1-rc1'); |
|---|
| 72 | define('RCMAIL_CHARSET', 'UTF-8'); |
|---|
| 73 | define('JS_OBJECT_NAME', 'rcmail'); |
|---|
| 74 | |
|---|
| 75 | // define global vars |
|---|
| 76 | $OUTPUT_TYPE = 'html'; |
|---|
| 77 | $INSTALL_PATH = dirname(__FILE__); |
|---|
| 78 | $MAIN_TASKS = array('mail','settings','addressbook','logout'); |
|---|
| 79 | |
|---|
| 80 | if (empty($INSTALL_PATH)) { |
|---|
| 81 | $INSTALL_PATH = './'; |
|---|
| 82 | } |
|---|
| 83 | else { |
|---|
| 84 | $INSTALL_PATH .= '/'; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | // make sure path_separator is defined |
|---|
| 88 | if (!defined('PATH_SEPARATOR')) { |
|---|
| 89 | define('PATH_SEPARATOR', (eregi('win', PHP_OS) ? ';' : ':')); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | // RC include folders MUST be included FIRST to avoid other |
|---|
| 93 | // possible not compatible libraries (i.e PEAR) to be included |
|---|
| 94 | // instead the ones provided by RC |
|---|
| 95 | ini_set('include_path', $INSTALL_PATH.PATH_SEPARATOR.$INSTALL_PATH.'program'.PATH_SEPARATOR.$INSTALL_PATH.'program/lib'.PATH_SEPARATOR.ini_get('include_path')); |
|---|
| 96 | |
|---|
| 97 | ini_set('session.name', 'sessid'); |
|---|
| 98 | ini_set('session.use_cookies', 1); |
|---|
| 99 | ini_set('session.gc_maxlifetime', 21600); |
|---|
| 100 | ini_set('session.gc_divisor', 500); |
|---|
| 101 | ini_set('error_reporting', E_ALL&~E_NOTICE); |
|---|
| 102 | |
|---|
| 103 | // increase maximum execution time for php scripts |
|---|
| 104 | // (does not work in safe mode) |
|---|
| 105 | if (!ini_get('safe_mode')) { |
|---|
| 106 | @set_time_limit(120); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | // include base files |
|---|
| 110 | require_once 'include/rcube_shared.inc'; |
|---|
| 111 | require_once 'include/rcube_imap.inc'; |
|---|
| 112 | require_once 'include/bugs.inc'; |
|---|
| 113 | require_once 'include/main.inc'; |
|---|
| 114 | require_once 'include/cache.inc'; |
|---|
| 115 | require_once 'PEAR.php'; |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | // set PEAR error handling |
|---|
| 119 | // PEAR::setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_NOTICE); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | // catch some url/post parameters |
|---|
| 123 | $_task = strip_quotes(get_input_value('_task', RCUBE_INPUT_GPC)); |
|---|
| 124 | $_action = strip_quotes(get_input_value('_action', RCUBE_INPUT_GPC)); |
|---|
| 125 | $_framed = (!empty($_GET['_framed']) || !empty($_POST['_framed'])); |
|---|
| 126 | |
|---|
| 127 | // use main task if empty or invalid value |
|---|
| 128 | if (empty($_task) || !in_array($_task, $MAIN_TASKS)) { |
|---|
| 129 | $_task = 'mail'; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | // set output buffering |
|---|
| 133 | if ($_action != 'get' && $_action != 'viewsource') { |
|---|
| 134 | // use gzip compression if supported |
|---|
| 135 | if (function_exists('ob_gzhandler') && ini_get('zlib.output_compression')) { |
|---|
| 136 | ob_start('ob_gzhandler'); |
|---|
| 137 | } |
|---|
| 138 | else { |
|---|
| 139 | ob_start(); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | // start session with requested task |
|---|
| 145 | rcmail_startup($_task); |
|---|
| 146 | |
|---|
| 147 | // set session related variables |
|---|
| 148 | $COMM_PATH = sprintf('./?_task=%s', $_task); |
|---|
| 149 | $SESS_HIDDEN_FIELD = ''; |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | // add framed parameter |
|---|
| 153 | if ($_framed) { |
|---|
| 154 | $COMM_PATH .= '&_framed=1'; |
|---|
| 155 | $SESS_HIDDEN_FIELD .= "\n".'<input type="hidden" name="_framed" value="1" />'; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | // init necessary objects for GUI |
|---|
| 160 | rcmail_load_gui(); |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | // check DB connections and exit on failure |
|---|
| 164 | if ($err_str = $DB->is_error()) { |
|---|
| 165 | raise_error(array( |
|---|
| 166 | 'code' => 603, |
|---|
| 167 | 'type' => 'db', |
|---|
| 168 | 'message' => $err_str), FALSE, TRUE |
|---|
| 169 | ); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | // error steps |
|---|
| 174 | if ($_action=='error' && !empty($_GET['_code'])) { |
|---|
| 175 | raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | // try to log in |
|---|
| 179 | if ($_action=='login' && $_task=='mail') { |
|---|
| 180 | |
|---|
| 181 | tfk_debug('Here we go, a login.'); |
|---|
| 182 | |
|---|
| 183 | $host = rcmail_autoselect_host(); |
|---|
| 184 | |
|---|
| 185 | tfk_debug('Selected host: ' . $host); |
|---|
| 186 | |
|---|
| 187 | // check if client supports cookies |
|---|
| 188 | if (empty($_COOKIE)) { |
|---|
| 189 | $OUTPUT->show_message("cookiesdisabled", 'warning'); |
|---|
| 190 | } |
|---|
| 191 | elseif ( |
|---|
| 192 | $_SESSION['temp'] |
|---|
| 193 | && !empty($_POST['_user']) |
|---|
| 194 | && isset($_POST['_pass']) |
|---|
| 195 | && rcmail_login( |
|---|
| 196 | get_input_value('_user', RCUBE_INPUT_POST), |
|---|
| 197 | get_input_value('_pass', RCUBE_INPUT_POST, true, 'ISO-8859-1'), |
|---|
| 198 | $host |
|---|
| 199 | ) |
|---|
| 200 | ) { |
|---|
| 201 | // create new session ID |
|---|
| 202 | unset($_SESSION['temp']); |
|---|
| 203 | sess_regenerate_id(); |
|---|
| 204 | |
|---|
| 205 | tfk_debug('Yay, we log in.'); |
|---|
| 206 | |
|---|
| 207 | // send auth cookie if necessary |
|---|
| 208 | rcmail_authenticate_session(); |
|---|
| 209 | |
|---|
| 210 | // send redirect |
|---|
| 211 | header("Location: $COMM_PATH"); |
|---|
| 212 | exit; |
|---|
| 213 | } |
|---|
| 214 | else { |
|---|
| 215 | |
|---|
| 216 | tfk_debug('Oops, failed.'); |
|---|
| 217 | //tfk_debug(var_export($_SESSION, true)); |
|---|
| 218 | tfk_debug(date('Y-m-d H:i:s', $_SESSION['auth_time'])); |
|---|
| 219 | |
|---|
| 220 | $OUTPUT->show_message("loginfailed", 'warning'); |
|---|
| 221 | $_SESSION['user_id'] = ''; |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | // end session |
|---|
| 226 | else if (($_task=='logout' || $_action=='logout') && isset($_SESSION['user_id'])) { |
|---|
| 227 | $OUTPUT->show_message('loggedout'); |
|---|
| 228 | rcmail_kill_session(); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | // check session and auth cookie |
|---|
| 232 | else if ($_action != 'login' && $_SESSION['user_id'] && $_action != 'send') { |
|---|
| 233 | if (!rcmail_authenticate_session()) { |
|---|
| 234 | $OUTPUT->show_message('sessionerror', 'error'); |
|---|
| 235 | rcmail_kill_session(); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | // log in to imap server |
|---|
| 241 | if (!empty($_SESSION['user_id']) && $_task=='mail') { |
|---|
| 242 | $conn = $IMAP->connect( |
|---|
| 243 | $_SESSION['imap_host'], |
|---|
| 244 | $_SESSION['username'], |
|---|
| 245 | decrypt_passwd($_SESSION['password']), |
|---|
| 246 | $_SESSION['imap_port'], |
|---|
| 247 | $_SESSION['imap_ssl'] |
|---|
| 248 | ); |
|---|
| 249 | if (!$conn) { |
|---|
| 250 | $OUTPUT->show_message('imaperror', 'error'); |
|---|
| 251 | $_SESSION['user_id'] = ''; |
|---|
| 252 | } |
|---|
| 253 | else { |
|---|
| 254 | rcmail_set_imap_prop(); |
|---|
| 255 | |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | // not logged in -> set task to 'login |
|---|
| 261 | if (empty($_SESSION['user_id'])) { |
|---|
| 262 | if ($OUTPUT->ajax_call){ |
|---|
| 263 | $OUTPUT->remote_response("setTimeout(\"location.href='\"+this.env.comm_path+\"'\", 2000);"); |
|---|
| 264 | } |
|---|
| 265 | $_task = 'login'; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | // set task and action to client |
|---|
| 271 | $OUTPUT->set_env('task', $_task); |
|---|
| 272 | if (!empty($_action)) { |
|---|
| 273 | $OUTPUT->set_env('action', $_action); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | // not logged in -> show login page |
|---|
| 278 | if (!$_SESSION['user_id']) { |
|---|
| 279 | $OUTPUT->task = 'login'; |
|---|
| 280 | $OUTPUT->send('login'); |
|---|
| 281 | exit; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | // handle keep-alive signal |
|---|
| 286 | if ($_action=='keep-alive') { |
|---|
| 287 | $OUTPUT->reset(); |
|---|
| 288 | $OUTPUT->send(''); |
|---|
| 289 | exit; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | /** |
|---|
| 293 | * $_name |
|---|
| 294 | * |
|---|
| 295 | * Used to build the filename for the include. |
|---|
| 296 | * @var string |
|---|
| 297 | */ |
|---|
| 298 | $_name = ''; |
|---|
| 299 | |
|---|
| 300 | // include task specific files |
|---|
| 301 | if ($_task=='mail') { |
|---|
| 302 | include_once 'program/steps/mail/func.inc'; |
|---|
| 303 | |
|---|
| 304 | switch($_action) { |
|---|
| 305 | default: |
|---|
| 306 | $_name.= $_action; |
|---|
| 307 | break; |
|---|
| 308 | |
|---|
| 309 | case 'preview': |
|---|
| 310 | case 'print': |
|---|
| 311 | $_name.= 'show'; |
|---|
| 312 | break; |
|---|
| 313 | |
|---|
| 314 | case 'moveto': |
|---|
| 315 | case 'delete': |
|---|
| 316 | $_name.= 'move_del'; |
|---|
| 317 | break; |
|---|
| 318 | |
|---|
| 319 | case 'send': |
|---|
| 320 | $_name.= 'sendmail'; |
|---|
| 321 | break; |
|---|
| 322 | |
|---|
| 323 | case 'remove-attachment': |
|---|
| 324 | $_name.= 'compose'; |
|---|
| 325 | break; |
|---|
| 326 | |
|---|
| 327 | case 'expunge': |
|---|
| 328 | case 'purge': |
|---|
| 329 | $_name.= 'folders'; |
|---|
| 330 | break; |
|---|
| 331 | case 'list': |
|---|
| 332 | if (isset($_REQUEST['_remote']) === true) { |
|---|
| 333 | $_name.= 'list'; |
|---|
| 334 | } |
|---|
| 335 | break; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | // make sure the message count is refreshed |
|---|
| 339 | $IMAP->messagecount($_SESSION['mbox'], 'ALL', TRUE); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | // include task specific files |
|---|
| 343 | if ($_task=='addressbook') { |
|---|
| 344 | include_once 'program/steps/addressbook/func.inc'; |
|---|
| 345 | |
|---|
| 346 | switch($_action) { |
|---|
| 347 | default: |
|---|
| 348 | $_name.= $_action; |
|---|
| 349 | break; |
|---|
| 350 | case 'edit': |
|---|
| 351 | case 'add': |
|---|
| 352 | $_name.= 'edit'; |
|---|
| 353 | break; |
|---|
| 354 | |
|---|
| 355 | case 'list': |
|---|
| 356 | if (isset($_REQUEST['_remote']) === true) { |
|---|
| 357 | $_name.= $_action; |
|---|
| 358 | } |
|---|
| 359 | break; |
|---|
| 360 | } |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | // include task specific files |
|---|
| 364 | if ($_task=='settings') { |
|---|
| 365 | include_once 'program/steps/settings/func.inc'; |
|---|
| 366 | |
|---|
| 367 | $_name = ''; |
|---|
| 368 | switch($_action) { |
|---|
| 369 | default: |
|---|
| 370 | $_name.= $_action; |
|---|
| 371 | break; |
|---|
| 372 | |
|---|
| 373 | case 'add-identity': |
|---|
| 374 | $_name.= 'edit_identity'; |
|---|
| 375 | break; |
|---|
| 376 | |
|---|
| 377 | case 'folders': |
|---|
| 378 | case 'subscribe': |
|---|
| 379 | case 'unsubscribe': |
|---|
| 380 | case 'create-folder': |
|---|
| 381 | case 'rename-folder': |
|---|
| 382 | case 'delete-folder': |
|---|
| 383 | $_name.= 'manage_folders'; |
|---|
| 384 | break; |
|---|
| 385 | |
|---|
| 386 | } |
|---|
| 387 | $_name = str_replace('-', '_', $_name); |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | if (empty($_name) === false) { |
|---|
| 391 | $_file = dirname(__FILE__) . '/program/steps/'; |
|---|
| 392 | $_file.= $_task . '/'; |
|---|
| 393 | $_file.= $_name . '.inc'; |
|---|
| 394 | if (file_exists($_file) === true) { |
|---|
| 395 | include $_file; |
|---|
| 396 | } |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | // parse main template |
|---|
| 400 | $OUTPUT->send($_task); |
|---|
| 401 | |
|---|
| 402 | // if we arrive here, something went wrong |
|---|
| 403 | raise_error( |
|---|
| 404 | array( |
|---|
| 405 | 'code' => 404, |
|---|
| 406 | 'type' => 'php', |
|---|
| 407 | 'line' => __LINE__, |
|---|
| 408 | 'file' => __FILE__, |
|---|
| 409 | 'message' => "Invalid request" |
|---|
| 410 | ), |
|---|
| 411 | TRUE, |
|---|
| 412 | TRUE |
|---|
| 413 | ); |
|---|
| 414 | ?> |
|---|