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 // DB queries often end up with a lot of newlines; merge to a single line
80 // for easier grepability...
81 $logmsg = str_replace("\n", " ", $logmsg);
82 common_log(LOG_ERR, $logmsg);
84 // @fixme backtrace output should be consistent with exception handling
85 if (common_config('site', 'logdebug')) {
86 $bt = $error->getBacktrace();
87 foreach ($bt as $n => $line) {
88 common_log(LOG_ERR, formatBacktraceLine($n, $line));
91 if ($error instanceof DB_DataObject_Error
92 || $error instanceof DB_Error
96 'The database for %s isn\'t responding correctly, '.
97 'so the site won\'t work properly. '.
98 'The site admins probably know about the problem, '.
99 'but you can contact them at %s to make sure. '.
100 'Otherwise, wait a few minutes and try again.'
102 common_config('site', 'name'),
103 common_config('site', 'email')
107 'An important error occured, probably related to email setup. '.
108 'Check logfiles for more info..'
112 $dac = new DBErrorAction($msg, 500);
118 * Format a backtrace line for debug output roughly like debug_print_backtrace() does.
119 * Exceptions already have this built in, but PEAR error objects just give us the array.
121 * @param int $n line number
122 * @param array $line per-frame array item from debug_backtrace()
125 function formatBacktraceLine($n, $line)
128 if (isset($line['class'])) $out .= $line['class'];
129 if (isset($line['type'])) $out .= $line['type'];
130 if (isset($line['function'])) $out .= $line['function'];
132 if (isset($line['args'])) {
134 foreach ($line['args'] as $arg) {
135 // debug_print_backtrace seems to use var_export
136 // but this gets *very* verbose!
137 $args[] = gettype($arg);
139 $out .= implode(',', $args);
142 $out .= ' called at [';
143 if (isset($line['file'])) $out .= $line['file'];
144 if (isset($line['line'])) $out .= ':' . $line['line'];
153 static $alwaysRW = array('session', 'remember_me');
155 // We ensure that these tables always are used
158 $config['db']['database_rw'] = $config['db']['database'];
159 $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
161 foreach ($alwaysRW as $table) {
162 $config['db']['table_'.$table] = 'rw';
166 function checkMirror($action_obj, $args)
170 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
171 if (is_array(common_config('db', 'mirror'))) {
172 // "load balancing", ha ha
173 $arr = common_config('db', 'mirror');
174 $k = array_rand($arr);
177 $mirror = common_config('db', 'mirror');
180 // everyone else uses the mirror
182 $config['db']['database'] = $mirror;
186 function isLoginAction($action)
188 static $loginActions = array('login', 'recoverpassword', 'api', 'doc', 'register', 'publicxrds', 'otp');
192 if (Event::handle('LoginAction', array($action, &$login))) {
193 $login = in_array($action, $loginActions);
201 // fake HTTP redirects using lighttpd's 404 redirects
202 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
203 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
204 $_lighty_url = @parse_url($_lighty_url);
206 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
207 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site', 'path')).'\//', '', substr($_lighty_url['path'], 1));
208 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
209 if ($_lighty_url['query']) {
210 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
212 parse_str($_lighty_url['query'], $_lighty_query);
213 foreach ($_lighty_query as $key => $val) {
214 $_GET[$key] = $_REQUEST[$key] = $val;
216 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
219 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
221 // quick check for fancy URL auto-detection support in installer.
222 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/", "", (dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
223 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
225 global $user, $action;
229 if (!_have_config()) {
232 "No configuration file found. Try running ".
233 "the installation program first."
236 $sac = new ServerErrorAction($msg);
241 // For database errors
243 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
245 // Make sure RW database is setup
249 // XXX: we need a little more structure in this script
251 // get and cache current user (may hit RW!)
253 $user = common_current_user();
255 // initialize language env
257 common_init_language();
259 $path = getPath($_REQUEST);
263 $args = $r->map($path);
266 $cac = new ClientErrorAction(_('Unknown page'), 404);
271 $args = array_merge($args, $_REQUEST);
273 Event::handle('ArgsInitialize', array(&$args));
275 $action = $args['action'];
277 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
278 common_redirect(common_local_url('public'));
282 // If the site is private, and they're not on one of the "public"
283 // parts of the site, redirect to login
285 if (!$user && common_config('site', 'private')
286 && !isLoginAction($action)
287 && !preg_match('/rss$/', $action)
288 && $action != 'robotstxt'
289 && !preg_match('/^Api/', $action)) {
292 $rargs =& common_copy_args($args);
293 unset($rargs['action']);
294 if (common_config('site', 'fancy')) {
297 if (array_key_exists('submit', $rargs)) {
298 unset($rargs['submit']);
300 foreach (array_keys($_COOKIE) as $cookie) {
301 unset($rargs[$cookie]);
303 common_set_returnto(common_local_url($action, $rargs));
305 common_redirect(common_local_url('login'));
309 $action_class = ucfirst($action).'Action';
311 if (!class_exists($action_class)) {
312 $cac = new ClientErrorAction(_('Unknown action'), 404);
315 $action_obj = new $action_class();
317 checkMirror($action_obj, $args);
320 if ($action_obj->prepare($args)) {
321 $action_obj->handle($args);
323 } catch (ClientException $cex) {
324 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
326 } catch (ServerException $sex) { // snort snort guffaw
327 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode(), $sex);
329 } catch (Exception $ex) {
330 $sac = new ServerErrorAction($ex->getMessage(), 500, $ex);
338 // XXX: cleanup exit() calls or add an exit handler so
339 // this always gets called
341 Event::handle('CleanupPlugin');