| 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 |
|
| 20 |
define('INSTALLDIR', dirname(__FILE__)); |
| 21 |
define('STATUSNET', true); |
| 22 |
define('LACONICA', true); // compatibility |
| 23 |
|
| 24 |
require_once INSTALLDIR . '/lib/common.php'; |
| 25 |
|
| 26 |
$user = null; |
| 27 |
$action = null; |
| 28 |
|
| 29 |
function getPath($req) |
| 30 |
{ |
| 31 |
if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER)) |
| 32 |
&& array_key_exists('p', $req)) { |
| 33 |
return $req['p']; |
| 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)); |
| 39 |
} else { |
| 40 |
return $path; |
| 41 |
} |
| 42 |
} else { |
| 43 |
return null; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
function handleError($error) |
| 48 |
{ |
| 49 |
if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$logmsg = "PEAR error: " . $error->getMessage(); |
| 54 |
if(common_config('site', 'logdebug')) { |
| 55 |
$logmsg .= " : ". $error->getDebugInfo(); |
| 56 |
} |
| 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); |
| 62 |
} |
| 63 |
} |
| 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')); |
| 73 |
} else { |
| 74 |
$msg = _('An important error occured, probably related to email setup. '. |
| 75 |
'Check logfiles for more info..'); |
| 76 |
} |
| 77 |
|
| 78 |
$dac = new DBErrorAction($msg, 500); |
| 79 |
$dac->showPage(); |
| 80 |
exit(-1); |
| 81 |
} |
| 82 |
|
| 83 |
function checkMirror($action_obj, $args) |
| 84 |
{ |
| 85 |
global $config; |
| 86 |
|
| 87 |
static $alwaysRW = array('session', 'remember_me'); |
| 88 |
|
| 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); |
| 94 |
$mirror = $arr[$k]; |
| 95 |
} else { |
| 96 |
$mirror = common_config('db', 'mirror'); |
| 97 |
} |
| 98 |
|
| 99 |
// We ensure that these tables always are used |
| 100 |
// on the master DB |
| 101 |
|
| 102 |
$config['db']['database_rw'] = $config['db']['database']; |
| 103 |
$config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini'; |
| 104 |
|
| 105 |
foreach ($alwaysRW as $table) { |
| 106 |
$config['db']['table_'.$table] = 'rw'; |
| 107 |
} |
| 108 |
|
| 109 |
// everyone else uses the mirror |
| 110 |
|
| 111 |
$config['db']['database'] = $mirror; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
function main() |
| 116 |
{ |
| 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); |
| 121 |
|
| 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; |
| 130 |
} |
| 131 |
$_GET['p'] = $_REQUEST['p'] = $_lighty_path; |
| 132 |
} |
| 133 |
} |
| 134 |
$_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']); |
| 135 |
|
| 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."); |
| 139 |
} |
| 140 |
global $user, $action; |
| 141 |
|
| 142 |
Snapshot::check(); |
| 143 |
|
| 144 |
if (!_have_config()) { |
| 145 |
$msg = sprintf(_("No configuration file found. Try running ". |
| 146 |
"the installation program first.")); |
| 147 |
$sac = new ServerErrorAction($msg); |
| 148 |
$sac->showPage(); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// For database errors |
| 153 |
|
| 154 |
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError'); |
| 155 |
|
| 156 |
// XXX: we need a little more structure in this script |
| 157 |
|
| 158 |
// get and cache current user |
| 159 |
|
| 160 |
$user = common_current_user(); |
| 161 |
|
| 162 |
// initialize language env |
| 163 |
|
| 164 |
common_init_language(); |
| 165 |
|
| 166 |
$path = getPath($_REQUEST); |
| 167 |
|
| 168 |
$r = Router::get(); |
| 169 |
|
| 170 |
$args = $r->map($path); |
| 171 |
|
| 172 |
if (!$args) { |
| 173 |
$cac = new ClientErrorAction(_('Unknown page'), 404); |
| 174 |
$cac->showPage(); |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
$args = array_merge($args, $_REQUEST); |
| 179 |
|
| 180 |
Event::handle('ArgsInitialize', array(&$args)); |
| 181 |
|
| 182 |
$action = $args['action']; |
| 183 |
|
| 184 |
if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) { |
| 185 |
common_redirect(common_local_url('public')); |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
// If the site is private, and they're not on one of the "public" |
| 190 |
// parts of the site, redirect to login |
| 191 |
|
| 192 |
if (!$user && common_config('site', 'private')) { |
| 193 |
$public_actions = array('openidlogin', 'finishopenidlogin', |
| 194 |
'recoverpassword', 'api', 'doc', |
| 195 |
'opensearch'); |
| 196 |
$login_action = 'openidlogin'; |
| 197 |
if (!common_config('site', 'openidonly')) { |
| 198 |
$public_actions[] = 'login'; |
| 199 |
$public_actions[] = 'register'; |
| 200 |
$login_action = 'login'; |
| 201 |
} |
| 202 |
if (!in_array($action, $public_actions) && |
| 203 |
!preg_match('/rss$/', $action)) { |
| 204 |
|
| 205 |
// set returnto |
| 206 |
$rargs =& common_copy_args($args); |
| 207 |
unset($rargs['action']); |
| 208 |
if (common_config('site', 'fancy')) { |
| 209 |
unset($rargs['p']); |
| 210 |
} |
| 211 |
if (array_key_exists('submit', $rargs)) { |
| 212 |
unset($rargs['submit']); |
| 213 |
} |
| 214 |
foreach (array_keys($_COOKIE) as $cookie) { |
| 215 |
unset($rargs[$cookie]); |
| 216 |
} |
| 217 |
common_set_returnto(common_local_url($action, $rargs)); |
| 218 |
|
| 219 |
common_redirect(common_local_url($login_action)); |
| 220 |
return; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
$action_class = ucfirst($action).'Action'; |
| 225 |
|
| 226 |
if (!class_exists($action_class)) { |
| 227 |
$cac = new ClientErrorAction(_('Unknown action'), 404); |
| 228 |
$cac->showPage(); |
| 229 |
} else { |
| 230 |
$action_obj = new $action_class(); |
| 231 |
|
| 232 |
checkMirror($action_obj, $args); |
| 233 |
|
| 234 |
try { |
| 235 |
if ($action_obj->prepare($args)) { |
| 236 |
$action_obj->handle($args); |
| 237 |
} |
| 238 |
} catch (ClientException $cex) { |
| 239 |
$cac = new ClientErrorAction($cex->getMessage(), $cex->getCode()); |
| 240 |
$cac->showPage(); |
| 241 |
} catch (ServerException $sex) { // snort snort guffaw |
| 242 |
$sac = new ServerErrorAction($sex->getMessage(), $sex->getCode()); |
| 243 |
$sac->showPage(); |
| 244 |
} catch (Exception $ex) { |
| 245 |
$sac = new ServerErrorAction($ex->getMessage()); |
| 246 |
$sac->showPage(); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
main(); |
| 252 |
|
| 253 |
// XXX: cleanup exit() calls or add an exit handler so |
| 254 |
// this always gets called |
| 255 |
|
| 256 |
Event::handle('CleanupPlugin'); |