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/
22 * @author Brenda Wallace <shiny@cpan.org>
23 * @author Christopher Vollick <psycotica0@gmail.com>
24 * @author CiaranG <ciaran@ciarang.com>
25 * @author Craig Andrews <candrews@integralblue.com>
26 * @author Evan Prodromou <evan@status.net>
27 * @author Gina Haeussge <osd@foosel.net>
28 * @author Jeffery To <jeffery.to@gmail.com>
29 * @author Mike Cochrane <mikec@mikenz.geek.nz>
30 * @author Robin Millette <millette@controlyourself.ca>
31 * @author Sarven Capadisli <csarven@status.net>
32 * @author Tom Adams <tom@holizz.com>
35 define('INSTALLDIR', dirname(__FILE__));
36 define('STATUSNET', true);
37 define('LACONICA', true); // compatibility
39 require_once INSTALLDIR . '/lib/common.php';
44 function getPath($req)
46 if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER))
47 && array_key_exists('p', $req)
50 } else if (array_key_exists('PATH_INFO', $_SERVER)) {
51 return $_SERVER['PATH_INFO'];
57 function handleError($error)
59 if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
63 $logmsg = "PEAR error: " . $error->getMessage();
64 if (common_config('site', 'logdebug')) {
65 $logmsg .= " : ". $error->getDebugInfo();
67 common_log(LOG_ERR, $logmsg);
68 if (common_config('site', 'logdebug')) {
69 $bt = $error->getBacktrace();
70 foreach ($bt as $line) {
71 common_log(LOG_ERR, $line);
74 if ($error instanceof DB_DataObject_Error
75 || $error instanceof DB_Error
79 'The database for %s isn\'t responding correctly, '.
80 'so the site won\'t work properly. '.
81 'The site admins probably know about the problem, '.
82 'but you can contact them at %s to make sure. '.
83 'Otherwise, wait a few minutes and try again.'
85 common_config('site', 'name'),
86 common_config('site', 'email')
90 'An important error occured, probably related to email setup. '.
91 'Check logfiles for more info..'
95 $dac = new DBErrorAction($msg, 500);
100 function checkMirror($action_obj, $args)
104 static $alwaysRW = array('session', 'remember_me');
106 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
107 if (is_array(common_config('db', 'mirror'))) {
108 // "load balancing", ha ha
109 $arr = common_config('db', 'mirror');
110 $k = array_rand($arr);
113 $mirror = common_config('db', 'mirror');
116 // We ensure that these tables always are used
119 $config['db']['database_rw'] = $config['db']['database'];
120 $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
122 foreach ($alwaysRW as $table) {
123 $config['db']['table_'.$table] = 'rw';
126 // everyone else uses the mirror
128 $config['db']['database'] = $mirror;
132 function isLoginAction($action)
134 static $loginActions = array('login', 'recoverpassword', 'api', 'doc', 'register');
138 if (Event::handle('LoginAction', array($action, &$login))) {
139 $login = in_array($action, $loginActions);
147 // fake HTTP redirects using lighttpd's 404 redirects
148 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
149 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
150 $_lighty_url = @parse_url($_lighty_url);
152 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
153 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site', 'path')).'\//', '', substr($_lighty_url['path'], 1));
154 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
155 if ($_lighty_url['query']) {
156 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
158 parse_str($_lighty_url['query'], $_lighty_query);
159 foreach ($_lighty_query as $key => $val) {
160 $_GET[$key] = $_REQUEST[$key] = $val;
162 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
165 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
167 // quick check for fancy URL auto-detection support in installer.
168 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/", "", (dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
169 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
171 global $user, $action;
175 if (!_have_config()) {
178 "No configuration file found. Try running ".
179 "the installation program first."
182 $sac = new ServerErrorAction($msg);
187 // For database errors
189 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
191 // XXX: we need a little more structure in this script
193 // get and cache current user
195 $user = common_current_user();
197 // initialize language env
199 common_init_language();
201 $path = getPath($_REQUEST);
205 $args = $r->map($path);
208 $cac = new ClientErrorAction(_('Unknown page'), 404);
213 $args = array_merge($args, $_REQUEST);
215 Event::handle('ArgsInitialize', array(&$args));
217 $action = $args['action'];
219 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
220 common_redirect(common_local_url('public'));
224 // If the site is private, and they're not on one of the "public"
225 // parts of the site, redirect to login
227 if (!$user && common_config('site', 'private')
228 && !isLoginAction($action)
229 && !preg_match('/rss$/', $action)
231 common_redirect(common_local_url('login'));
235 $action_class = ucfirst($action).'Action';
237 if (!class_exists($action_class)) {
238 $cac = new ClientErrorAction(_('Unknown action'), 404);
241 $action_obj = new $action_class();
243 checkMirror($action_obj, $args);
246 if ($action_obj->prepare($args)) {
247 $action_obj->handle($args);
249 } catch (ClientException $cex) {
250 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
252 } catch (ServerException $sex) { // snort snort guffaw
253 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
255 } catch (Exception $ex) {
256 $sac = new ServerErrorAction($ex->getMessage());
264 // XXX: cleanup exit() calls or add an exit handler so
265 // this always gets called
267 Event::handle('CleanupPlugin');