source: subversion/branches/devel-api/index.php @ 2287

Last change on this file since 2287 was 2287, checked in by thomasb, 4 years ago

Init plugins AFTER output object was created + make localized texts available on the client

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1<?php
2/*
3 +-------------------------------------------------------------------------+
4 | RoundCube Webmail IMAP Client                                           |
5 | Version 0.3-20090210                                                    |
6 |                                                                         |
7 | Copyright (C) 2005-2009, 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$
27
28*/
29
30// include environment
31require_once 'program/include/iniset.php';
32
33// init application and start session with requested task
34$RCMAIL = rcmail::get_instance();
35
36// init output class
37$OUTPUT = !empty($_REQUEST['_remote']) ? $RCMAIL->init_json() : $RCMAIL->load_gui(!empty($_REQUEST['_framed']));
38
39// init plugin API
40$RCMAIL->plugins->init();
41
42// set output buffering
43if ($RCMAIL->action != 'get' && $RCMAIL->action != 'viewsource') {
44  // use gzip compression if supported
45  if (function_exists('ob_gzhandler')
46      && !ini_get('zlib.output_compression')
47      && ini_get('output_handler') != 'ob_gzhandler') {
48    ob_start('ob_gzhandler');
49  }
50  else {
51    ob_start();
52  }
53}
54
55// check if config files had errors
56if ($err_str = $RCMAIL->config->get_error()) {
57  raise_error(array(
58    'code' => 601,
59    'type' => 'php',
60    'message' => $err_str), false, true);
61}
62
63// check DB connections and exit on failure
64if ($err_str = $DB->is_error()) {
65  raise_error(array(
66    'code' => 603,
67    'type' => 'db',
68    'message' => $err_str), FALSE, TRUE);
69}
70
71// error steps
72if ($RCMAIL->action=='error' && !empty($_GET['_code'])) {
73  raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE);
74}
75
76
77// trigger startup plugin hook
78$startup = $RCMAIL->plugins->exec_hook('startup', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action));
79$RCMAIL->set_task($startup['task']);
80$RCMAIL->action = $startup['action'];
81
82
83// try to log in
84if ($RCMAIL->action=='login' && $RCMAIL->task=='mail') {
85  $auth = $RCMAIL->plugins->exec_hook('authenticate', array(
86    'host' => $RCMAIL->autoselect_host(),
87    'user' => trim(get_input_value('_user', RCUBE_INPUT_POST)),
88  )) + array('pass' => get_input_value('_pass', RCUBE_INPUT_POST, true, 'ISO-8859-1'));
89 
90  // check if client supports cookies
91  if (empty($_COOKIE)) {
92    $OUTPUT->show_message("cookiesdisabled", 'warning');
93  }
94  else if ($_SESSION['temp'] && !empty($auth['user']) && !empty($auth['host']) && isset($auth['pass']) && 
95            $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'])) {
96    // create new session ID
97    unset($_SESSION['temp']);
98    rcube_sess_regenerate_id();
99
100    // send auth cookie if necessary
101    $RCMAIL->authenticate_session();
102
103    // log successful login
104    if ($RCMAIL->config->get('log_logins')) {
105      write_log('userlogins', sprintf('Successful login for %s (id %d) from %s',
106        $RCMAIL->user->get_username(),
107        $RCMAIL->user->ID,
108        $_SERVER['REMOTE_ADDR']));
109    }
110
111    // send redirect
112    $OUTPUT->redirect();
113  }
114  else {
115    $OUTPUT->show_message($IMAP->error_code < -1 ? 'imaperror' : 'loginfailed', 'warning');
116    $RCMAIL->kill_session();
117  }
118}
119
120// end session
121else if (($RCMAIL->task=='logout' || $RCMAIL->action=='logout') && isset($_SESSION['user_id'])) {
122  $OUTPUT->show_message('loggedout');
123  $RCMAIL->logout_actions();
124  $RCMAIL->kill_session();
125}
126
127// check session and auth cookie
128else if ($RCMAIL->action != 'login' && $_SESSION['user_id'] && $RCMAIL->action != 'send') {
129  if (!$RCMAIL->authenticate_session()) {
130    $OUTPUT->show_message('sessionerror', 'error');
131    $RCMAIL->kill_session();
132  }
133}
134
135
136// check client X-header to verify request origin
137if ($OUTPUT->ajax_call) {
138  if (!$RCMAIL->config->get('devel_mode') && !rc_request_header('X-RoundCube-Referer')) {
139    header('HTTP/1.1 404 Not Found');
140    die("Invalid Request");
141  }
142}
143
144
145// not logged in -> show login page
146if (empty($RCMAIL->user->ID)) {
147 
148  if ($OUTPUT->ajax_call)
149    $OUTPUT->redirect(array(), 2000);
150 
151  // check if installer is still active
152  if ($RCMAIL->config->get('enable_installer') && is_readable('./installer/index.php')) {
153    $OUTPUT->add_footer(html::div(array('style' => "background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em"),
154      html::tag('h2', array('style' => "margin-top:0.2em"), "Installer script is still accessible") .
155      html::p(null, "The install script of your RoundCube installation is still stored in its default location!") .
156      html::p(null, "Please <b>remove</b> the whole <tt>installer</tt> folder from the RoundCube directory because .
157        these files may expose sensitive configuration data like server passwords and encryption keys
158        to the public. Make sure you cannot access the <a href=\"./installer/\">installer script</a> from your browser.")
159      )
160    );
161  }
162 
163  $OUTPUT->set_env('task', 'login');
164  $OUTPUT->send('login');
165}
166
167
168// handle keep-alive signal
169if ($RCMAIL->action == 'keep-alive') {
170  $OUTPUT->reset();
171  $OUTPUT->send();
172}
173// save preference value
174else if ($RCMAIL->action == 'save-pref') {
175  $RCMAIL->user->save_prefs(array(get_input_value('_name', RCUBE_INPUT_POST) => get_input_value('_value', RCUBE_INPUT_POST)));
176  $OUTPUT->reset();
177  $OUTPUT->send();
178}
179
180
181// map task/action to a certain include file
182$action_map = array(
183  'mail' => array(
184    'preview' => 'show.inc',
185    'print'   => 'show.inc',
186    'moveto'  => 'move_del.inc',
187    'delete'  => 'move_del.inc',
188    'send'    => 'sendmail.inc',
189    'expunge' => 'folders.inc',
190    'purge'   => 'folders.inc',
191    'remove-attachment'  => 'attachments.inc',
192    'display-attachment' => 'attachments.inc',
193    'upload' => 'attachments.inc',
194  ),
195 
196  'addressbook' => array(
197    'add' => 'edit.inc',
198  ),
199 
200  'settings' => array(
201    'folders'       => 'manage_folders.inc',
202    'create-folder' => 'manage_folders.inc',
203    'rename-folder' => 'manage_folders.inc',
204    'delete-folder' => 'manage_folders.inc',
205    'subscribe'     => 'manage_folders.inc',
206    'unsubscribe'   => 'manage_folders.inc',
207    'add-identity'  => 'edit_identity.inc',
208  )
209);
210
211// include task specific functions
212include_once 'program/steps/'.$RCMAIL->task.'/func.inc';
213
214// allow 5 "redirects" to another action
215$redirects = 0; $incstep = null;
216while ($redirects < 5) {
217  $stepfile = !empty($action_map[$RCMAIL->task][$RCMAIL->action]) ?
218    $action_map[$RCMAIL->task][$RCMAIL->action] : strtr($RCMAIL->action, '-', '_') . '.inc';
219
220  // execute a plugin action
221  if (eregi('^plugin.', $RCMAIL->action)) {
222    $RCMAIL->plugins->exec_action($RCMAIL->action);
223    break;
224  }
225  // try to include the step file
226  else if (is_file(($incfile = 'program/steps/'.$RCMAIL->task.'/'.$stepfile))) {
227    include($incfile);
228    $redirects++;
229  }
230  else {
231    break;
232  }
233}
234
235
236// parse main template (default)
237$OUTPUT->send($RCMAIL->task);
238
239
240// if we arrive here, something went wrong
241raise_error(array(
242  'code' => 404,
243  'type' => 'php',
244  'line' => __LINE__,
245  'file' => __FILE__,
246  'message' => "Invalid request"), true, true);
247                     
248?>
Note: See TracBrowser for help on using the repository browser.