3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @license GNU Affero General Public License http://www.gnu.org/licenses/
22 * @author Brenda Wallace <shiny@cpan.org>
23 * @author Christopher Vollick <psycotica0@gmail.com>
24 * @author CiaranG <ciaran@ciarang.com>
25 * @author Craig Andrews <candrews@integralblue.com>
26 * @author Evan Prodromou <evan@controlezvous.ca>
27 * @author Evan Prodromou <evan@controlyourself.ca>
28 * @author Evan Prodromou <evan@prodromou.name>
29 * @author Evan Prodromou <evan@status.net>
30 * @author Gina Haeussge <osd@foosel.net>
31 * @author Jeffery To <jeffery.to@gmail.com>
32 * @author Mike Cochrane <mikec@mikenz.geek.nz>
33 * @author Robin Millette <millette@controlyourself.ca>
34 * @author Sarven Capadisli <csarven@controlyourself.ca>
35 * @author Tom Adams <tom@holizz.com>
38 define('INSTALLDIR', dirname(__FILE__));
39 define('STATUSNET', true);
40 define('LACONICA', true); // compatibility
42 require_once INSTALLDIR . '/lib/common.php';
47 function getPath($req)
49 if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER))
50 && array_key_exists('p', $req)
53 } else if (array_key_exists('PATH_INFO', $_SERVER)) {
54 return $_SERVER['PATH_INFO'];
60 function handleError($error)
62 if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
66 $logmsg = "PEAR error: " . $error->getMessage();
67 if (common_config('site', 'logdebug')) {
68 $logmsg .= " : ". $error->getDebugInfo();
70 common_log(LOG_ERR, $logmsg);
71 if (common_config('site', 'logdebug')) {
72 $bt = $error->getBacktrace();
73 foreach ($bt as $line) {
74 common_log(LOG_ERR, $line);
77 if ($error instanceof DB_DataObject_Error
78 || $error instanceof DB_Error
82 'The database for %s isn\'t responding correctly, '.
83 'so the site won\'t work properly. '.
84 'The site admins probably know about the problem, '.
85 'but you can contact them at %s to make sure. '.
86 'Otherwise, wait a few minutes and try again.'
88 common_config('site', 'name'),
89 common_config('site', 'email')
93 'An important error occured, probably related to email setup. '.
94 'Check logfiles for more info..'
98 $dac = new DBErrorAction($msg, 500);
103 function checkMirror($action_obj, $args)
107 static $alwaysRW = array('session', 'remember_me');
109 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
110 if (is_array(common_config('db', 'mirror'))) {
111 // "load balancing", ha ha
112 $arr = common_config('db', 'mirror');
113 $k = array_rand($arr);
116 $mirror = common_config('db', 'mirror');
119 // We ensure that these tables always are used
122 $config['db']['database_rw'] = $config['db']['database'];
123 $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
125 foreach ($alwaysRW as $table) {
126 $config['db']['table_'.$table] = 'rw';
129 // everyone else uses the mirror
131 $config['db']['database'] = $mirror;
135 function isLoginAction($action)
137 static $loginActions = array('login', 'recoverpassword', 'api', 'doc', 'register');
141 if (Event::handle('LoginAction', array($action, &$login))) {
142 $login = in_array($action, $loginActions);
150 // fake HTTP redirects using lighttpd's 404 redirects
151 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
152 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
153 $_lighty_url = @parse_url($_lighty_url);
155 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
156 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site', 'path')).'\//', '', substr($_lighty_url['path'], 1));
157 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
158 if ($_lighty_url['query']) {
159 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
161 parse_str($_lighty_url['query'], $_lighty_query);
162 foreach ($_lighty_query as $key => $val) {
163 $_GET[$key] = $_REQUEST[$key] = $val;
165 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
168 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
170 // quick check for fancy URL auto-detection support in installer.
171 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/", "", (dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
172 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
174 global $user, $action;
178 if (!_have_config()) {
181 "No configuration file found. Try running ".
182 "the installation program first."
185 $sac = new ServerErrorAction($msg);
190 // For database errors
192 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
194 // XXX: we need a little more structure in this script
196 // get and cache current user
198 $user = common_current_user();
200 // initialize language env
202 common_init_language();
204 $path = getPath($_REQUEST);
208 $args = $r->map($path);
211 $cac = new ClientErrorAction(_('Unknown page'), 404);
216 $args = array_merge($args, $_REQUEST);
218 Event::handle('ArgsInitialize', array(&$args));
220 $action = $args['action'];
222 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
223 common_redirect(common_local_url('public'));
227 // If the site is private, and they're not on one of the "public"
228 // parts of the site, redirect to login
230 if (!$user && common_config('site', 'private')
231 && !isLoginAction($action)
232 && !preg_match('/rss$/', $action)
234 common_redirect(common_local_url('login'));
238 $action_class = ucfirst($action).'Action';
240 if (!class_exists($action_class)) {
241 $cac = new ClientErrorAction(_('Unknown action'), 404);
244 $action_obj = new $action_class();
246 checkMirror($action_obj, $args);
249 if ($action_obj->prepare($args)) {
250 $action_obj->handle($args);
252 } catch (ClientException $cex) {
253 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
255 } catch (ServerException $sex) { // snort snort guffaw
256 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
258 } catch (Exception $ex) {
259 $sac = new ServerErrorAction($ex->getMessage());
267 // XXX: cleanup exit() calls or add an exit handler so
268 // this always gets called
270 Event::handle('CleanupPlugin');