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') || !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 $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
52 'so the site won\'t work properly. '.
53 'The site admins probably know about the problem, '.
54 'but you can contact them at %s to make sure. '.
55 'Otherwise, wait a few minutes and try again.'),
56 common_config('site', 'name'),
57 common_config('site', 'email'));
59 $dac = new DBErrorAction($msg, 500);
66 // quick check for fancy URL auto-detection support in installer.
67 if (isset($_SERVER['REDIRECT_URL']) && ('/check-fancy' === $_SERVER['REDIRECT_URL'])) {
68 die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
70 global $user, $action, $config;
72 if (!_have_config()) {
73 $msg = sprintf(_("No configuration file found. Try running ".
74 "the installation program first."));
75 $sac = new ServerErrorAction($msg);
80 // For database errors
82 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
84 // XXX: we need a little more structure in this script
86 // get and cache current user
88 $user = common_current_user();
90 // initialize language env
92 common_init_language();
94 $path = getPath($_REQUEST);
98 $args = $r->map($path);
101 $cac = new ClientErrorAction(_('Unknown page'), 404);
106 $args = array_merge($args, $_REQUEST);
108 $action = $args['action'];
110 if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
111 common_redirect(common_local_url('public'));
115 // If the site is private, and they're not on one of the "public"
116 // parts of the site, redirect to login
118 if (!$user && common_config('site', 'private') &&
119 !in_array($action, array('login', 'openidlogin', 'finishopenidlogin',
120 'recoverpassword', 'api', 'doc', 'register'))) {
121 common_redirect(common_local_url('login'));
125 $action_class = ucfirst($action).'Action';
127 if (!class_exists($action_class)) {
128 $cac = new ClientErrorAction(_('Unknown action'), 404);
131 $action_obj = new $action_class();
133 // XXX: find somewhere for this little block to live
135 if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
136 if (is_array(common_config('db', 'mirror'))) {
137 // "load balancing", ha ha
138 $arr = common_config('db', 'mirror');
139 $k = array_rand($arr);
142 $mirror = common_config('db', 'mirror');
144 $config['db']['database'] = $mirror;
148 if ($action_obj->prepare($args)) {
149 $action_obj->handle($args);
151 } catch (ClientException $cex) {
152 $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
154 } catch (ServerException $sex) { // snort snort guffaw
155 $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
157 } catch (Exception $ex) {
158 $sac = new ServerErrorAction($ex->getMessage());
166 // XXX: cleanup exit() calls or add an exit handler so
167 // this always gets called
169 Event::handle('CleanupPlugin');