| 1 |
<?php |
| 2 |
/** |
| 3 |
* StatusNet - the distributed open-source microblogging tool |
| 4 |
* Copyright (C) 2008, 2009, StatusNet, Inc. |
| 5 |
* |
| 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. |
| 10 |
* |
| 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. |
| 15 |
* |
| 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/>. |
| 18 |
* |
| 19 |
* @category StatusNet |
| 20 |
* @package StatusNet |
| 21 |
* @author Brenda Wallace <shiny@cpan.org> |
| 22 |
* @author Christopher Vollick <psycotica0@gmail.com> |
| 23 |
* @author CiaranG <ciaran@ciarang.com> |
| 24 |
* @author Craig Andrews <candrews@integralblue.com> |
| 25 |
* @author Evan Prodromou <evan@controlezvous.ca> |
| 26 |
* @author Gina Haeussge <osd@foosel.net> |
| 27 |
* @author Jeffery To <jeffery.to@gmail.com> |
| 28 |
* @author Mike Cochrane <mikec@mikenz.geek.nz> |
| 29 |
* @author Robin Millette <millette@controlyourself.ca> |
| 30 |
* @author Sarven Capadisli <csarven@controlyourself.ca> |
| 31 |
* @author Tom Adams <tom@holizz.com> |
| 32 |
* |
| 33 |
* @license GNU Affero General Public License http://www.gnu.org/licenses/ |
| 34 |
*/ |
| 35 |
|
| 36 |
define('INSTALLDIR', dirname(__FILE__)); |
| 37 |
define('STATUSNET', true); |
| 38 |
define('LACONICA', true); // compatibility |
| 39 |
|
| 40 |
require_once INSTALLDIR . '/lib/common.php'; |
| 41 |
|
| 42 |
$user = null; |
| 43 |
$action = null; |
| 44 |
|
| 45 |
function getPath($req) |
| 46 |
{ |
| 47 |
if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER)) |
| 48 |
&& array_key_exists('p', $req) |
| 49 |
) { |
| 50 |
return $req['p']; |
| 51 |
} else if (array_key_exists('PATH_INFO', $_SERVER)) { |
| 52 |
$path = $_SERVER['PATH_INFO']; |
| 53 |
$script = $_SERVER['SCRIPT_NAME']; |
| 54 |
if (substr($path, 0, mb_strlen($script)) == $script) { |
| 55 |
return substr($path, mb_strlen($script)); |
| 56 |
} else { |
| 57 |
return $path; |
| 58 |
} |
| 59 |
} else { |
| 60 |
return null; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* logs and then displays error messages |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
function handleError($error) |
| 70 |
{ |
| 71 |
if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$logmsg = "PEAR error: " . $error->getMessage(); |
| 76 |
if (common_config('site', 'logdebug')) { |
| 77 |
$logmsg .= " : ". $error->getDebugInfo(); |
| 78 |
} |
| 79 |
// DB queries often end up with a lot of newlines; merge to a single line |
| 80 |
// for easier grepability... |
| 81 |
$logmsg = str_replace("\n", " ", $logmsg); |
| 82 |
common_log(LOG_ERR, $logmsg); |
| 83 |
|
| 84 |
// @fixme backtrace output should be consistent with exception handling |
| 85 |
if (common_config('site', 'logdebug')) { |
| 86 |
$bt = $error->getBacktrace(); |
| 87 |
foreach ($bt as $n => $line) { |
| 88 |
common_log(LOG_ERR, formatBacktraceLine($n, $line)); |
| 89 |
} |
| 90 |
} |
| 91 |
if ($error instanceof DB_DataObject_Error |
| 92 |
|| $error instanceof DB_Error |
| 93 |
) { |
| 94 |
$msg = sprintf( |
| 95 |
_( |
| 96 |
'The database for %s isn\'t responding correctly, '. |
| 97 |
'so the site won\'t work properly. '. |
| 98 |
'The site admins probably know about the problem, '. |
| 99 |
'but you can contact them at %s to make sure. '. |
| 100 |
'Otherwise, wait a few minutes and try again.' |
| 101 |
), |
| 102 |
common_config('site', 'name'), |
| 103 |
common_config('site', 'email') |
| 104 |
); |
| 105 |
} else { |
| 106 |
$msg = _( |
| 107 |
'An important error occured, probably related to email setup. '. |
| 108 |
'Check logfiles for more info..' |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
$dac = new DBErrorAction($msg, 500); |
| 113 |
$dac->showPage(); |
| 114 |
exit(-1); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Format a backtrace line for debug output roughly like debug_print_backtrace() does. |
| 119 |
* Exceptions already have this built in, but PEAR error objects just give us the array. |
| 120 |
* |
| 121 |
* @param int $n line number |
| 122 |
* @param array $line per-frame array item from debug_backtrace() |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
function formatBacktraceLine($n, $line) |
| 126 |
{ |
| 127 |
$out = "#$n "; |
| 128 |
if (isset($line['class'])) $out .= $line['class']; |
| 129 |
if (isset($line['type'])) $out .= $line['type']; |
| 130 |
if (isset($line['function'])) $out .= $line['function']; |
| 131 |
$out .= '('; |
| 132 |
if (isset($line['args'])) { |
| 133 |
$args = array(); |
| 134 |
foreach ($line['args'] as $arg) { |
| 135 |
// debug_print_backtrace seems to use var_export |
| 136 |
// but this gets *very* verbose! |
| 137 |
$args[] = gettype($arg); |
| 138 |
} |
| 139 |
$out .= implode(',', $args); |
| 140 |
} |
| 141 |
$out .= ')'; |
| 142 |
$out .= ' called at ['; |
| 143 |
if (isset($line['file'])) $out .= $line['file']; |
| 144 |
if (isset($line['line'])) $out .= ':' . $line['line']; |
| 145 |
$out .= ']'; |
| 146 |
return $out; |
| 147 |
} |
| 148 |
|
| 149 |
function checkMirror($action_obj, $args) |
| 150 |
{ |
| 151 |
global $config; |
| 152 |
|
| 153 |
static $alwaysRW = array('session', 'remember_me'); |
| 154 |
|
| 155 |
if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) { |
| 156 |
if (is_array(common_config('db', 'mirror'))) { |
| 157 |
// "load balancing", ha ha |
| 158 |
$arr = common_config('db', 'mirror'); |
| 159 |
$k = array_rand($arr); |
| 160 |
$mirror = $arr[$k]; |
| 161 |
} else { |
| 162 |
$mirror = common_config('db', 'mirror'); |
| 163 |
} |
| 164 |
|
| 165 |
// We ensure that these tables always are used |
| 166 |
// on the master DB |
| 167 |
|
| 168 |
$config['db']['database_rw'] = $config['db']['database']; |
| 169 |
$config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini'; |
| 170 |
|
| 171 |
foreach ($alwaysRW as $table) { |
| 172 |
$config['db']['table_'.$table] = 'rw'; |
| 173 |
} |
| 174 |
|
| 175 |
// everyone else uses the mirror |
| 176 |
|
| 177 |
$config['db']['database'] = $mirror; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
function isLoginAction($action) |
| 182 |
{ |
| 183 |
static $loginActions = array('login', 'recoverpassword', 'api', 'doc', 'register', 'publicxrds'); |
| 184 |
|
| 185 |
$login = null; |
| 186 |
|
| 187 |
if (Event::handle('LoginAction', array($action, &$login))) { |
| 188 |
$login = in_array($action, $loginActions); |
| 189 |
} |
| 190 |
|
| 191 |
return $login; |
| 192 |
} |
| 193 |
|
| 194 |
function main() |
| 195 |
{ |
| 196 |
// fake HTTP redirects using lighttpd's 404 redirects |
| 197 |
if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) { |
| 198 |
$_lighty_url = $base_url.$_SERVER['REQUEST_URI']; |
| 199 |
$_lighty_url = @parse_url($_lighty_url); |
| 200 |
|
| 201 |
if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') { |
| 202 |
$_lighty_path = preg_replace('/^'.preg_quote(common_config('site', 'path')).'\//', '', substr($_lighty_url['path'], 1)); |
| 203 |
$_SERVER['QUERY_STRING'] = 'p='.$_lighty_path; |
| 204 |
if ($_lighty_url['query']) { |
| 205 |
$_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query']; |
| 206 |
} |
| 207 |
parse_str($_lighty_url['query'], $_lighty_query); |
| 208 |
foreach ($_lighty_query as $key => $val) { |
| 209 |
$_GET[$key] = $_REQUEST[$key] = $val; |
| 210 |
} |
| 211 |
$_GET['p'] = $_REQUEST['p'] = $_lighty_path; |
| 212 |
} |
| 213 |
} |
| 214 |
$_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']); |
| 215 |
|
| 216 |
// quick check for fancy URL auto-detection support in installer. |
| 217 |
if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/", "", (dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) { |
| 218 |
die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs."); |
| 219 |
} |
| 220 |
global $user, $action; |
| 221 |
|
| 222 |
Snapshot::check(); |
| 223 |
|
| 224 |
if (!_have_config()) { |
| 225 |
$msg = sprintf( |
| 226 |
_( |
| 227 |
"No configuration file found. Try running ". |
| 228 |
"the installation program first." |
| 229 |
) |
| 230 |
); |
| 231 |
$sac = new ServerErrorAction($msg); |
| 232 |
$sac->showPage(); |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
// For database errors |
| 237 |
|
| 238 |
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError'); |
| 239 |
|
| 240 |
// XXX: we need a little more structure in this script |
| 241 |
|
| 242 |
// get and cache current user |
| 243 |
|
| 244 |
$user = common_current_user(); |
| 245 |
|
| 246 |
// initialize language env |
| 247 |
|
| 248 |
common_init_language(); |
| 249 |
|
| 250 |
$path = getPath($_REQUEST); |
| 251 |
|
| 252 |
$r = Router::get(); |
| 253 |
|
| 254 |
$args = $r->map($path); |
| 255 |
|
| 256 |
if (!$args) { |
| 257 |
$cac = new ClientErrorAction(_('Unknown page'), 404); |
| 258 |
$cac->showPage(); |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
$args = array_merge($args, $_REQUEST); |
| 263 |
|
| 264 |
Event::handle('ArgsInitialize', array(&$args)); |
| 265 |
|
| 266 |
$action = $args['action']; |
| 267 |
|
| 268 |
if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) { |
| 269 |
common_redirect(common_local_url('public')); |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
// If the site is private, and they're not on one of the "public" |
| 274 |
// parts of the site, redirect to login |
| 275 |
|
| 276 |
if (!$user && common_config('site', 'private') |
| 277 |
&& !isLoginAction($action) |
| 278 |
&& !preg_match('/rss$/', $action) |
| 279 |
&& !preg_match('/^Api/', $action) |
| 280 |
) { |
| 281 |
common_redirect(common_local_url('login')); |
| 282 |
return; |
| 283 |
} |
| 284 |
|
| 285 |
$action_class = ucfirst($action).'Action'; |
| 286 |
|
| 287 |
if (!class_exists($action_class)) { |
| 288 |
$cac = new ClientErrorAction(_('Unknown action'), 404); |
| 289 |
$cac->showPage(); |
| 290 |
} else { |
| 291 |
$action_obj = new $action_class(); |
| 292 |
|
| 293 |
checkMirror($action_obj, $args); |
| 294 |
|
| 295 |
try { |
| 296 |
if ($action_obj->prepare($args)) { |
| 297 |
$action_obj->handle($args); |
| 298 |
} |
| 299 |
} catch (ClientException $cex) { |
| 300 |
$cac = new ClientErrorAction($cex->getMessage(), $cex->getCode()); |
| 301 |
$cac->showPage(); |
| 302 |
} catch (ServerException $sex) { // snort snort guffaw |
| 303 |
$sac = new ServerErrorAction($sex->getMessage(), $sex->getCode()); |
| 304 |
$sac->showPage(); |
| 305 |
} catch (Exception $ex) { |
| 306 |
$sac = new ServerErrorAction($ex->getMessage()); |
| 307 |
$sac->showPage(); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
main(); |
| 313 |
|
| 314 |
// XXX: cleanup exit() calls or add an exit handler so |
| 315 |
// this always gets called |
| 316 |
|
| 317 |
Event::handle('CleanupPlugin'); |