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 * @author Brenda Wallace <shiny@cpan.org>
22 * @author Christopher Vollick <psycotica0@gmail.com>
23 * @author CiaranG <ciaran@ciarang.com>
24 * @author Craig Andrews <candrews@integralblue.com>
25 * @author Evan Prodromou <evan@controlezvous.ca>
26 * @author Gina Haeussge <osd@foosel.net>
27 * @author Jeffery To <jeffery.to@gmail.com>
28 * @author Mike Cochrane <mikec@mikenz.geek.nz>
29 * @author Robin Millette <millette@controlyourself.ca>
30 * @author Sarven Capadisli <csarven@controlyourself.ca>
31 * @author Tom Adams <tom@holizz.com>
33 * @license GNU Affero General Public License http://www.gnu.org/licenses/
36 define('INSTALLDIR', dirname(__FILE__));
37 define('STATUSNET', true);
38 define('LACONICA', true); // compatibility
40 require_once INSTALLDIR . '/lib/common.php';
45 function getPath($req)
47 if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER))
48 && array_key_exists('p', $req)
51 } else if (array_key_exists('PATH_INFO', $_SERVER)) {
52 $path = $_SERVER['PATH_INFO'];
53 $script = $_SERVER['SCRIPT_NAME'];
54 if (substr($path, 0, mb_strlen($script)) == $script) {
55 return substr($path, mb_strlen($script));
65 * logs and then displays error messages
69 function handleError($error)
71 if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
75 $logmsg = "PEAR error: " . $error->getMessage();
76 if (common_config('site', 'logdebug')) {
77 $logmsg .= " : ". $error->getDebugInfo();
79 common_log(LOG_ERR, $logmsg);
80 if (common_config('site', 'logdebug')) {
81 $bt = $error->getBacktrace();
82 foreach ($bt as $line) {
83 common_log(LOG_ERR, $line);
86 if ($error instanceof DB_DataObject_Error
87 || $error instanceof DB_Error
91 'The database for %s isn\'t responding correctly, '.
92 'so the site won\'t work properly. '.
93 'The site admins probably know about the problem, '.
94 'but you can contact them at %s to make sure. '.
95 'Otherwise, wait a few minutes and try again.'
97 common_config('site', 'name'),
98 common_config('site', 'email')
102 'An important error occured, probably related to email setup. '.
103 'Check logfiles for more info..'
107 $dac = new DBErrorAction($msg, 500);
112 function checkMirror($action_obj, $args)
116 static $alwaysRW = array('session', 'remember_me');
118 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
119 if (is_array(common_config('db', 'mirror'))) {
120 // "load balancing", ha ha
121 $arr = common_config('db', 'mirror');
122 $k = array_rand($arr);
125 $mirror = common_config('db', 'mirror');
128 // We ensure that these tables always are used
131 $config['db']['database_rw'] = $config['db']['database'];
132 $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
134 foreach ($alwaysRW as $table) {
135 $config['db']['table_'.$table] = 'rw';
138 // everyone else uses the mirror
140 $config['db']['database'] = $mirror;
144 function isLoginAction($action)
146 static $loginActions = array('login', 'recoverpassword', 'api', 'doc', 'register', 'publicxrds');
150 if (Event::handle('LoginAction', array($action, &$login))) {
151 $login = in_array($action, $loginActions);
159 // fake HTTP redirects using lighttpd's 404 redirects
160 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
161 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
162 $_lighty_url = @parse_url($_lighty_url);
164 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
165 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site', 'path')).'\//', '', substr($_lighty_url['path'], 1));
166 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
167 if ($_lighty_url['query']) {
168 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
170 parse_str($_lighty_url['query'], $_lighty_query);
171 foreach ($_lighty_query as $key => $val) {
172 $_GET[$key] = $_REQUEST[$key] = $val;
174 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
177 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
179 // quick check for fancy URL auto-detection support in installer.
180 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/", "", (dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
181 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
183 global $user, $action;
187 if (!_have_config()) {
190 "No configuration file found. Try running ".
191 "the installation program first."
194 $sac = new ServerErrorAction($msg);
199 // For database errors
201 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
203 // XXX: we need a little more structure in this script
205 // get and cache current user
207 $user = common_current_user();
209 // initialize language env
211 common_init_language();
213 $path = getPath($_REQUEST);
217 $args = $r->map($path);
220 $cac = new ClientErrorAction(_('Unknown page'), 404);
225 $args = array_merge($args, $_REQUEST);
227 Event::handle('ArgsInitialize', array(&$args));
229 $action = $args['action'];
231 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
232 common_redirect(common_local_url('public'));
236 // If the site is private, and they're not on one of the "public"
237 // parts of the site, redirect to login
239 if (!$user && common_config('site', 'private')
240 && !isLoginAction($action)
241 && !preg_match('/rss$/', $action)
242 && !preg_match('/^Api/', $action)
244 common_redirect(common_local_url('login'));
248 $action_class = ucfirst($action).'Action';
250 if (!class_exists($action_class)) {
251 $cac = new ClientErrorAction(_('Unknown action'), 404);
254 $action_obj = new $action_class();
256 checkMirror($action_obj, $args);
259 if ($action_obj->prepare($args)) {
260 $action_obj->handle($args);
262 } catch (ClientException $cex) {
263 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
265 } catch (ServerException $sex) { // snort snort guffaw
266 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
268 } catch (Exception $ex) {
269 $sac = new ServerErrorAction($ex->getMessage());
277 // XXX: cleanup exit() calls or add an exit handler so
278 // this always gets called
280 Event::handle('CleanupPlugin');