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 $path = $_SERVER['PATH_INFO'];
36 $script = $_SERVER['SCRIPT_NAME'];
37 if (substr($path, 0, mb_strlen($script)) == $script) {
38 return substr($path, mb_strlen($script));
47 function handleError($error)
49 if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
53 $logmsg = "PEAR error: " . $error->getMessage();
54 if(common_config('site', 'logdebug')) {
55 $logmsg .= " : ". $error->getDebugInfo();
57 common_log(LOG_ERR, $logmsg);
58 if(common_config('site', 'logdebug')) {
59 $bt = $error->getBacktrace();
60 foreach ($bt as $line) {
61 common_log(LOG_ERR, $line);
64 if ($error instanceof DB_DataObject_Error ||
65 $error instanceof DB_Error) {
66 $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
67 'so the site won\'t work properly. '.
68 'The site admins probably know about the problem, '.
69 'but you can contact them at %s to make sure. '.
70 'Otherwise, wait a few minutes and try again.'),
71 common_config('site', 'name'),
72 common_config('site', 'email'));
74 $msg = _('An important error occured, probably related to email setup. '.
75 'Check logfiles for more info..');
78 $dac = new DBErrorAction($msg, 500);
83 function checkMirror($action_obj, $args)
87 static $alwaysRW = array('session', 'remember_me');
89 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
90 if (is_array(common_config('db', 'mirror'))) {
91 // "load balancing", ha ha
92 $arr = common_config('db', 'mirror');
93 $k = array_rand($arr);
96 $mirror = common_config('db', 'mirror');
99 // We ensure that these tables always are used
102 $config['db']['database_rw'] = $config['db']['database'];
103 $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
105 foreach ($alwaysRW as $table) {
106 $config['db']['table_'.$table] = 'rw';
109 // everyone else uses the mirror
111 $config['db']['database'] = $mirror;
117 // fake HTTP redirects using lighttpd's 404 redirects
118 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
119 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
120 $_lighty_url = @parse_url($_lighty_url);
122 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
123 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site','path')).'\//', '', substr($_lighty_url['path'], 1));
124 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
125 if ($_lighty_url['query'])
126 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
127 parse_str($_lighty_url['query'], $_lighty_query);
128 foreach ($_lighty_query as $key => $val) {
129 $_GET[$key] = $_REQUEST[$key] = $val;
131 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
134 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
136 // quick check for fancy URL auto-detection support in installer.
137 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/","",(dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
138 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
140 global $user, $action;
144 if (!_have_config()) {
145 $msg = sprintf(_("No configuration file found. Try running ".
146 "the installation program first."));
147 $sac = new ServerErrorAction($msg);
152 // For database errors
154 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
156 // XXX: we need a little more structure in this script
158 // get and cache current user
160 $user = common_current_user();
162 // initialize language env
164 common_init_language();
166 $path = getPath($_REQUEST);
170 $args = $r->map($path);
173 $cac = new ClientErrorAction(_('Unknown page'), 404);
178 $args = array_merge($args, $_REQUEST);
180 Event::handle('ArgsInitialize', array(&$args));
182 $action = $args['action'];
184 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
185 common_redirect(common_local_url('public'));
189 // If the site is private, and they're not on one of the "public"
190 // parts of the site, redirect to login
192 if (!$user && common_config('site', 'private')) {
193 $public_actions = array('openidlogin', 'finishopenidlogin',
194 'recoverpassword', 'api', 'doc',
196 $login_action = 'openidlogin';
197 if (!common_config('site', 'openidonly')) {
198 $public_actions[] = 'login';
199 $public_actions[] = 'register';
200 $login_action = 'login';
202 if (!in_array($action, $public_actions) &&
203 !preg_match('/rss$/', $action)) {
206 $rargs =& common_copy_args($args);
207 unset($rargs['action']);
208 if (common_config('site', 'fancy')) {
211 if (array_key_exists('submit', $rargs)) {
212 unset($rargs['submit']);
214 foreach (array_keys($_COOKIE) as $cookie) {
215 unset($rargs[$cookie]);
217 common_set_returnto(common_local_url($action, $rargs));
219 common_redirect(common_local_url($login_action));
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');