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/
24 define('INSTALLDIR', dirname(__FILE__));
25 define('STATUSNET', true);
26 define('LACONICA', true); // compatibility
28 require_once INSTALLDIR . '/lib/common.php';
33 function getPath($req)
35 if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER))
36 && array_key_exists('p', $req)
39 } else if (array_key_exists('PATH_INFO', $_SERVER)) {
40 return $_SERVER['PATH_INFO'];
46 function handleError($error)
48 if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
52 $logmsg = "PEAR error: " . $error->getMessage();
53 if (common_config('site', 'logdebug')) {
54 $logmsg .= " : ". $error->getDebugInfo();
56 common_log(LOG_ERR, $logmsg);
57 if (common_config('site', 'logdebug')) {
58 $bt = $error->getBacktrace();
59 foreach ($bt as $line) {
60 common_log(LOG_ERR, $line);
63 if ($error instanceof DB_DataObject_Error
64 || $error instanceof DB_Error
68 'The database for %s isn\'t responding correctly, '.
69 'so the site won\'t work properly. '.
70 'The site admins probably know about the problem, '.
71 'but you can contact them at %s to make sure. '.
72 'Otherwise, wait a few minutes and try again.'
74 common_config('site', 'name'),
75 common_config('site', 'email')
79 'An important error occured, probably related to email setup. '.
80 'Check logfiles for more info..'
84 $dac = new DBErrorAction($msg, 500);
89 function checkMirror($action_obj, $args)
93 static $alwaysRW = array('session', 'remember_me');
95 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
96 if (is_array(common_config('db', 'mirror'))) {
97 // "load balancing", ha ha
98 $arr = common_config('db', 'mirror');
99 $k = array_rand($arr);
102 $mirror = common_config('db', 'mirror');
105 // We ensure that these tables always are used
108 $config['db']['database_rw'] = $config['db']['database'];
109 $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
111 foreach ($alwaysRW as $table) {
112 $config['db']['table_'.$table] = 'rw';
115 // everyone else uses the mirror
117 $config['db']['database'] = $mirror;
121 function isLoginAction($action)
123 static $loginActions = array('login', 'recoverpassword', 'api', 'doc', 'register');
127 if (Event::handle('LoginAction', array($action, &$login))) {
128 $login = in_array($action, $loginActions);
136 // fake HTTP redirects using lighttpd's 404 redirects
137 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
138 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
139 $_lighty_url = @parse_url($_lighty_url);
141 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
142 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site', 'path')).'\//', '', substr($_lighty_url['path'], 1));
143 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
144 if ($_lighty_url['query']) {
145 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
147 parse_str($_lighty_url['query'], $_lighty_query);
148 foreach ($_lighty_query as $key => $val) {
149 $_GET[$key] = $_REQUEST[$key] = $val;
151 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
154 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
156 // quick check for fancy URL auto-detection support in installer.
157 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/", "", (dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
158 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
160 global $user, $action;
164 if (!_have_config()) {
167 "No configuration file found. Try running ".
168 "the installation program first."
171 $sac = new ServerErrorAction($msg);
176 // For database errors
178 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
180 // XXX: we need a little more structure in this script
182 // get and cache current user
184 $user = common_current_user();
186 // initialize language env
188 common_init_language();
190 $path = getPath($_REQUEST);
194 $args = $r->map($path);
197 $cac = new ClientErrorAction(_('Unknown page'), 404);
202 $args = array_merge($args, $_REQUEST);
204 Event::handle('ArgsInitialize', array(&$args));
206 $action = $args['action'];
208 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
209 common_redirect(common_local_url('public'));
213 // If the site is private, and they're not on one of the "public"
214 // parts of the site, redirect to login
216 if (!$user && common_config('site', 'private')
217 && !isLoginAction($action)
218 && !preg_match('/rss$/', $action)
220 common_redirect(common_local_url('login'));
224 $action_class = ucfirst($action).'Action';
226 if (!class_exists($action_class)) {
227 $cac = new ClientErrorAction(_('Unknown action'), 404);
230 $action_obj = new $action_class();
232 checkMirror($action_obj, $args);
235 if ($action_obj->prepare($args)) {
236 $action_obj->handle($args);
238 } catch (ClientException $cex) {
239 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
241 } catch (ServerException $sex) { // snort snort guffaw
242 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
244 } catch (Exception $ex) {
245 $sac = new ServerErrorAction($ex->getMessage());
253 // XXX: cleanup exit() calls or add an exit handler so
254 // this always gets called
256 Event::handle('CleanupPlugin');