3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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')) {
32 } else if ($_SERVER['PATH_INFO']) {
33 return $_SERVER['PATH_INFO'];
39 function handleError($error)
41 common_log(LOG_ERR, "PEAR error: " . $error->getMessage());
42 $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
43 'so the site won\'t work properly. '.
44 'The site admins probably know about the problem, '.
45 'but you can contact them at %s to make sure. '.
46 'Otherwise, wait a few minutes and try again.'),
47 common_config('site', 'name'),
48 common_config('site', 'email'));
50 $dac = new DBErrorAction($msg, 500);
57 global $user, $action;
59 // For database errors
61 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
63 // XXX: we need a little more structure in this script
65 // get and cache current user
67 $user = common_current_user();
69 // initialize language env
71 common_init_language();
73 $path = getPath($_REQUEST);
77 $args = $r->map($path);
80 $cac = new ClientErrorAction(_('Unknown page'), 404);
85 $args = array_merge($args, $_REQUEST);
87 $action = $args['action'];
89 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
90 common_redirect(common_local_url('public'));
94 // If the site is private, and they're not on one of the "public"
95 // parts of the site, redirect to login
97 if (!$user && common_config('site', 'private') &&
98 !in_array($action, array('login', 'openidlogin', 'finishopenidlogin',
99 'recoverpassword', 'api', 'doc', 'register'))) {
100 common_redirect(common_local_url('login'));
104 $action_class = ucfirst($action).'Action';
106 if (!class_exists($action_class)) {
107 $cac = new ClientErrorAction(_('Unknown action'), 404);
110 $action_obj = new $action_class();
112 // XXX: find somewhere for this little block to live
114 if ($config['db']['mirror'] && $action_obj->isReadOnly()) {
115 if (is_array($config['db']['mirror'])) {
116 // "load balancing", ha ha
117 $k = array_rand($config['db']['mirror']);
119 $mirror = $config['db']['mirror'][$k];
121 $mirror = $config['db']['mirror'];
123 $config['db']['database'] = $mirror;
127 if ($action_obj->prepare($args)) {
128 $action_obj->handle($args);
130 } catch (ClientException $cex) {
131 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
133 } catch (ServerException $sex) { // snort snort guffaw
134 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
136 } catch (Exception $ex) {
137 $sac = new ServerErrorAction($ex->getMessage());
145 // XXX: cleanup exit() calls or add an exit handler so
146 // this always gets called
148 Event::handle('CleanupPlugin');