3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, 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('LACONICA', true);
23 require_once INSTALLDIR . '/lib/common.php';
28 function getPath($req)
30 if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER))
31 && array_key_exists('p', $req)) {
33 } else if (array_key_exists('PATH_INFO', $_SERVER)) {
34 return $_SERVER['PATH_INFO'];
40 function handleError($error)
42 if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
46 $logmsg = "PEAR error: " . $error->getMessage();
47 if(common_config('site', 'logdebug')) {
48 $logmsg .= " : ". $error->getDebugInfo();
50 common_log(LOG_ERR, $logmsg);
51 if(common_config('site', 'logdebug')) {
52 $bt = $error->getBacktrace();
53 foreach ($bt as $line) {
54 common_log(LOG_ERR, $line);
57 if ($error instanceof DB_DataObject_Error ||
58 $error instanceof DB_Error) {
59 $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
60 'so the site won\'t work properly. '.
61 'The site admins probably know about the problem, '.
62 'but you can contact them at %s to make sure. '.
63 'Otherwise, wait a few minutes and try again.'),
64 common_config('site', 'name'),
65 common_config('site', 'email'));
67 $msg = _('An important error occured, probably related to email setup. '.
68 'Check logfiles for more info..');
71 $dac = new DBErrorAction($msg, 500);
76 function checkMirror($action_obj, $args)
80 static $alwaysRW = array('session', 'remember_me');
82 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
83 if (is_array(common_config('db', 'mirror'))) {
84 // "load balancing", ha ha
85 $arr = common_config('db', 'mirror');
86 $k = array_rand($arr);
89 $mirror = common_config('db', 'mirror');
92 // We ensure that these tables always are used
95 $config['db']['database_rw'] = $config['db']['database'];
96 $config['db']['ini_rw'] = INSTALLDIR.'/classes/laconica.ini';
98 foreach ($alwaysRW as $table) {
99 $config['db']['table_'.$table] = 'rw';
102 // everyone else uses the mirror
104 $config['db']['database'] = $mirror;
110 // fake HTTP redirects using lighttpd's 404 redirects
111 if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
112 $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
113 $_lighty_url = @parse_url($_lighty_url);
115 if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
116 $_lighty_path = preg_replace('/^'.preg_quote(common_config('site','path')).'\//', '', substr($_lighty_url['path'], 1));
117 $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
118 if ($_lighty_url['query'])
119 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
120 parse_str($_lighty_url['query'], $_lighty_query);
121 foreach ($_lighty_query as $key => $val) {
122 $_GET[$key] = $_REQUEST[$key] = $val;
124 $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
127 $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
129 // quick check for fancy URL auto-detection support in installer.
130 if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/","",(dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
131 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
133 global $user, $action;
137 if (!_have_config()) {
138 $msg = sprintf(_("No configuration file found. Try running ".
139 "the installation program first."));
140 $sac = new ServerErrorAction($msg);
145 // For database errors
147 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
149 // XXX: we need a little more structure in this script
151 // get and cache current user
153 $user = common_current_user();
155 // initialize language env
157 common_init_language();
159 $path = getPath($_REQUEST);
163 $args = $r->map($path);
166 $cac = new ClientErrorAction(_('Unknown page'), 404);
171 $args = array_merge($args, $_REQUEST);
173 Event::handle('ArgsInitialize', array(&$args));
175 $action = $args['action'];
177 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
178 common_redirect(common_local_url('public'));
182 // If the site is private, and they're not on one of the "public"
183 // parts of the site, redirect to login
185 if (!$user && common_config('site', 'private')) {
186 $public_actions = array('openidlogin', 'finishopenidlogin',
187 'recoverpassword', 'api', 'doc',
189 $login_action = 'openidlogin';
190 if (!common_config('site', 'openidonly')) {
191 $public_actions[] = 'login';
192 $public_actions[] = 'register';
193 $login_action = 'login';
195 if (!in_array($action, $public_actions) &&
196 !preg_match('/rss$/', $action)) {
199 $rargs =& common_copy_args($args);
200 unset($rargs['action']);
201 if (common_config('site', 'fancy')) {
204 if (array_key_exists('submit', $rargs)) {
205 unset($rargs['submit']);
207 foreach (array_keys($_COOKIE) as $cookie) {
208 unset($rargs[$cookie]);
210 common_set_returnto(common_local_url($action, $rargs));
212 common_redirect(common_local_url($login_action));
217 $action_class = ucfirst($action).'Action';
219 if (!class_exists($action_class)) {
220 $cac = new ClientErrorAction(_('Unknown action'), 404);
223 $action_obj = new $action_class();
225 checkMirror($action_obj, $args);
228 if ($action_obj->prepare($args)) {
229 $action_obj->handle($args);
231 } catch (ClientException $cex) {
232 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
234 } catch (ServerException $sex) { // snort snort guffaw
235 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
237 } catch (Exception $ex) {
238 $sac = new ServerErrorAction($ex->getMessage());
246 // XXX: cleanup exit() calls or add an exit handler so
247 // this always gets called
249 Event::handle('CleanupPlugin');