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)) {
38 } else if (array_key_exists('PATH_INFO', $_SERVER)) {
39 return $_SERVER['PATH_INFO'];
45 function handleError($error)
47 if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
51 $logmsg = "PEAR error: " . $error->getMessage();
52 if (common_config('site', 'logdebug')) {
53 $logmsg .= " : ". $error->getDebugInfo();
55 common_log(LOG_ERR, $logmsg);
56 if (common_config('site', 'logdebug')) {
57 $bt = $error->getBacktrace();
58 foreach ($bt as $line) {
59 common_log(LOG_ERR, $line);
62 if ($error instanceof DB_DataObject_Error ||
63 $error instanceof DB_Error) {
64 $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
65 'so the site won\'t work properly. '.
66 'The site admins probably know about the problem, '.
67 'but you can contact them at %s to make sure. '.
68 'Otherwise, wait a few minutes and try again.'),
69 common_config('site', 'name'),
70 common_config('site', 'email'));
72 $msg = _('An important error occured, probably related to email setup. '.
73 'Check logfiles for more info..');
76 $dac = new DBErrorAction($msg, 500);
81 function checkMirror($action_obj, $args)
85 static $alwaysRW = array('session', 'remember_me');
87 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
88 if (is_array(common_config('db', 'mirror'))) {
89 // "load balancing", ha ha
90 $arr = common_config('db', 'mirror');
91 $k = array_rand($arr);
94 $mirror = common_config('db', 'mirror');
97 // We ensure that these tables always are used
100 $config['db']['database_rw'] = $config['db']['database'];
101 $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
103 foreach ($alwaysRW as $table) {
104 $config['db']['table_'.$table] = 'rw';
107 // everyone else uses the mirror
109 $config['db']['database'] = $mirror;
113 function isLoginAction($action)
115 static $loginActions = array('login', 'recoverpassword', 'api', 'doc', 'register');
119 if (Event::handle('LoginAction', array($action, &$login))) {
120 $login = in_array($action, $loginActions);
128 // fake HTTP redirects using lighttpd's 404 redirects
129 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
130 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
131 $_lighty_url = @parse_url($_lighty_url);
133 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
134 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site','path')).'\//', '', substr($_lighty_url['path'], 1));
135 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
136 if ($_lighty_url['query'])
137 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
138 parse_str($_lighty_url['query'], $_lighty_query);
139 foreach ($_lighty_query as $key => $val) {
140 $_GET[$key] = $_REQUEST[$key] = $val;
142 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
145 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
147 // quick check for fancy URL auto-detection support in installer.
148 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/","",(dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
149 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
151 global $user, $action;
155 if (!_have_config()) {
156 $msg = sprintf(_("No configuration file found. Try running ".
157 "the installation program first."));
158 $sac = new ServerErrorAction($msg);
163 // For database errors
165 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
167 // XXX: we need a little more structure in this script
169 // get and cache current user
171 $user = common_current_user();
173 // initialize language env
175 common_init_language();
177 $path = getPath($_REQUEST);
181 $args = $r->map($path);
184 $cac = new ClientErrorAction(_('Unknown page'), 404);
189 $args = array_merge($args, $_REQUEST);
191 Event::handle('ArgsInitialize', array(&$args));
193 $action = $args['action'];
195 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
196 common_redirect(common_local_url('public'));
200 // If the site is private, and they're not on one of the "public"
201 // parts of the site, redirect to login
203 if (!$user && common_config('site', 'private') &&
204 !isLoginAction($action) &&
205 !preg_match('/rss$/', $action)) {
206 common_redirect(common_local_url('login'));
210 $action_class = ucfirst($action).'Action';
212 if (!class_exists($action_class)) {
213 $cac = new ClientErrorAction(_('Unknown action'), 404);
216 $action_obj = new $action_class();
218 checkMirror($action_obj, $args);
221 if ($action_obj->prepare($args)) {
222 $action_obj->handle($args);
224 } catch (ClientException $cex) {
225 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
227 } catch (ServerException $sex) { // snort snort guffaw
228 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
230 } catch (Exception $ex) {
231 $sac = new ServerErrorAction($ex->getMessage());
239 // XXX: cleanup exit() calls or add an exit handler so
240 // this always gets called
242 Event::handle('CleanupPlugin');