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 //error_log(print_r($error,1));
72 if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
76 $logmsg = "PEAR error: " . $error->getMessage();
77 if (common_config('site', 'logdebug')) {
78 $logmsg .= " : ". $error->getDebugInfo();
80 common_log(LOG_ERR, $logmsg);
81 if (common_config('site', 'logdebug')) {
82 $bt = $error->getBacktrace();
83 foreach ($bt as $line) {
84 common_log(LOG_ERR, $line);
87 if ($error instanceof DB_DataObject_Error
88 || $error instanceof DB_Error
92 'The database for %s isn\'t responding correctly, '.
93 'so the site won\'t work properly. '.
94 'The site admins probably know about the problem, '.
95 'but you can contact them at %s to make sure. '.
96 'Otherwise, wait a few minutes and try again.'
98 common_config('site', 'name'),
99 common_config('site', 'email')
103 'An important error occured, probably related to email setup. '.
104 'Check logfiles for more info..'
108 $dac = new DBErrorAction($msg, 500);
113 function checkMirror($action_obj, $args)
117 static $alwaysRW = array('session', 'remember_me');
119 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
120 if (is_array(common_config('db', 'mirror'))) {
121 // "load balancing", ha ha
122 $arr = common_config('db', 'mirror');
123 $k = array_rand($arr);
126 $mirror = common_config('db', 'mirror');
129 // We ensure that these tables always are used
132 $config['db']['database_rw'] = $config['db']['database'];
133 $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
135 foreach ($alwaysRW as $table) {
136 $config['db']['table_'.$table] = 'rw';
139 // everyone else uses the mirror
141 $config['db']['database'] = $mirror;
145 function isLoginAction($action)
147 static $loginActions = array('login', 'recoverpassword', 'api', 'doc', 'register', 'publicxrds');
151 if (Event::handle('LoginAction', array($action, &$login))) {
152 $login = in_array($action, $loginActions);
160 // fake HTTP redirects using lighttpd's 404 redirects
161 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
162 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
163 $_lighty_url = @parse_url($_lighty_url);
165 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
166 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site', 'path')).'\//', '', substr($_lighty_url['path'], 1));
167 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
168 if ($_lighty_url['query']) {
169 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
171 parse_str($_lighty_url['query'], $_lighty_query);
172 foreach ($_lighty_query as $key => $val) {
173 $_GET[$key] = $_REQUEST[$key] = $val;
175 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
178 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
180 // quick check for fancy URL auto-detection support in installer.
181 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/", "", (dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
182 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
184 global $user, $action;
188 if (!_have_config()) {
191 "No configuration file found. Try running ".
192 "the installation program first."
195 $sac = new ServerErrorAction($msg);
200 // For database errors
202 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
204 // XXX: we need a little more structure in this script
206 // get and cache current user
208 $user = common_current_user();
210 // initialize language env
212 common_init_language();
214 $path = getPath($_REQUEST);
218 $args = $r->map($path);
221 $cac = new ClientErrorAction(_('Unknown page'), 404);
226 $args = array_merge($args, $_REQUEST);
228 Event::handle('ArgsInitialize', array(&$args));
230 $action = $args['action'];
232 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
233 common_redirect(common_local_url('public'));
237 // If the site is private, and they're not on one of the "public"
238 // parts of the site, redirect to login
240 if (!$user && common_config('site', 'private')
241 && !isLoginAction($action)
242 && !preg_match('/rss$/', $action)
243 && !preg_match('/^Api/', $action)
245 common_redirect(common_local_url('login'));
249 $action_class = ucfirst($action).'Action';
251 if (!class_exists($action_class)) {
252 $cac = new ClientErrorAction(_('Unknown action'), 404);
255 $action_obj = new $action_class();
257 checkMirror($action_obj, $args);
260 if ($action_obj->prepare($args)) {
261 $action_obj->handle($args);
263 } catch (ClientException $cex) {
264 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
266 } catch (ServerException $sex) { // snort snort guffaw
267 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
269 } catch (Exception $ex) {
270 $sac = new ServerErrorAction($ex->getMessage());
278 // XXX: cleanup exit() calls or add an exit handler so
279 // this always gets called
281 Event::handle('CleanupPlugin');