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/>.
20 define('INSTALLDIR', dirname(__FILE__));
21 define('STATUSNET', true);
22 define('LACONICA', true); // compatibility
24 require_once INSTALLDIR . '/lib/common.php';
29 function getPath($req)
31 if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER))
32 && array_key_exists('p', $req)) {
34 } else if (array_key_exists('PATH_INFO', $_SERVER)) {
35 return $_SERVER['PATH_INFO'];
41 function handleError($error)
43 if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
47 $logmsg = "PEAR error: " . $error->getMessage();
48 if(common_config('site', 'logdebug')) {
49 $logmsg .= " : ". $error->getDebugInfo();
51 common_log(LOG_ERR, $logmsg);
52 if(common_config('site', 'logdebug')) {
53 $bt = $error->getBacktrace();
54 foreach ($bt as $line) {
55 common_log(LOG_ERR, $line);
58 if ($error instanceof DB_DataObject_Error ||
59 $error instanceof DB_Error) {
60 $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
61 'so the site won\'t work properly. '.
62 'The site admins probably know about the problem, '.
63 'but you can contact them at %s to make sure. '.
64 'Otherwise, wait a few minutes and try again.'),
65 common_config('site', 'name'),
66 common_config('site', 'email'));
68 $msg = _('An important error occured, probably related to email setup. '.
69 'Check logfiles for more info..');
72 $dac = new DBErrorAction($msg, 500);
77 function checkMirror($action_obj, $args)
81 static $alwaysRW = array('session', 'remember_me');
83 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
84 if (is_array(common_config('db', 'mirror'))) {
85 // "load balancing", ha ha
86 $arr = common_config('db', 'mirror');
87 $k = array_rand($arr);
90 $mirror = common_config('db', 'mirror');
93 // We ensure that these tables always are used
96 $config['db']['database_rw'] = $config['db']['database'];
97 $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
99 foreach ($alwaysRW as $table) {
100 $config['db']['table_'.$table] = 'rw';
103 // everyone else uses the mirror
105 $config['db']['database'] = $mirror;
109 function isLoginAction($action)
111 static $loginActions = array('login', 'recoverpassword', 'api', 'doc', 'register');
115 if (Event::handle('LoginAction', array($action, &$login))) {
116 $login = in_array($action, $loginActions);
124 // fake HTTP redirects using lighttpd's 404 redirects
125 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
126 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
127 $_lighty_url = @parse_url($_lighty_url);
129 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
130 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site','path')).'\//', '', substr($_lighty_url['path'], 1));
131 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
132 if ($_lighty_url['query'])
133 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
134 parse_str($_lighty_url['query'], $_lighty_query);
135 foreach ($_lighty_query as $key => $val) {
136 $_GET[$key] = $_REQUEST[$key] = $val;
138 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
141 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
143 // quick check for fancy URL auto-detection support in installer.
144 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/","",(dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
145 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
147 global $user, $action;
151 if (!_have_config()) {
152 $msg = sprintf(_("No configuration file found. Try running ".
153 "the installation program first."));
154 $sac = new ServerErrorAction($msg);
159 // For database errors
161 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
163 // XXX: we need a little more structure in this script
165 // get and cache current user
167 $user = common_current_user();
169 // initialize language env
171 common_init_language();
173 $path = getPath($_REQUEST);
177 $args = $r->map($path);
180 $cac = new ClientErrorAction(_('Unknown page'), 404);
185 $args = array_merge($args, $_REQUEST);
187 Event::handle('ArgsInitialize', array(&$args));
189 $action = $args['action'];
191 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
192 common_redirect(common_local_url('public'));
196 // If the site is private, and they're not on one of the "public"
197 // parts of the site, redirect to login
199 if (!$user && common_config('site', 'private') &&
200 !isLoginAction($action) &&
201 !preg_match('/rss$/', $action)) {
202 common_redirect(common_local_url('login'));
206 $action_class = ucfirst($action).'Action';
208 if (!class_exists($action_class)) {
209 $cac = new ClientErrorAction(_('Unknown action'), 404);
212 $action_obj = new $action_class();
214 checkMirror($action_obj, $args);
217 if ($action_obj->prepare($args)) {
218 $action_obj->handle($args);
220 } catch (ClientException $cex) {
221 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
223 } catch (ServerException $sex) { // snort snort guffaw
224 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
226 } catch (Exception $ex) {
227 $sac = new ServerErrorAction($ex->getMessage());
235 // XXX: cleanup exit() calls or add an exit handler so
236 // this always gets called
238 Event::handle('CleanupPlugin');