]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - index.php
Check for config file when running
[quix0rs-gnu-social.git] / index.php
1 <?php
2 /**
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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('LACONICA', true);
22
23 require_once INSTALLDIR . '/lib/common.php';
24
25 $user = null;
26 $action = null;
27
28 function getPath($req)
29 {
30     if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER))
31         && array_key_exists('p', $req)) {
32         return $req['p'];
33     } else if (array_key_exists('PATH_INFO', $_SERVER)) {
34         return $_SERVER['PATH_INFO'];
35     } else {
36         return null;
37     }
38 }
39
40 function handleError($error)
41 {
42     if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
43         return;
44     }
45
46     common_log(LOG_ERR, "PEAR error: " . $error->getMessage());
47     $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
48                      'so the site won\'t work properly. '.
49                      'The site admins probably know about the problem, '.
50                      'but you can contact them at %s to make sure. '.
51                      'Otherwise, wait a few minutes and try again.'),
52                    common_config('site', 'name'),
53                    common_config('site', 'email'));
54
55     $dac = new DBErrorAction($msg, 500);
56     $dac->showPage();
57     exit(-1);
58 }
59
60 function main()
61 {
62     global $user, $action;
63
64     if (!_have_config()) {
65         $msg = sprintf(_("No configuration file found. Try running ".
66                          "the installation program first."));
67         $sac = new ServerErrorAction($msg);
68         $sac->showPage();
69         return;
70     }
71
72     // For database errors
73
74     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
75
76     // XXX: we need a little more structure in this script
77
78     // get and cache current user
79
80     $user = common_current_user();
81
82     // initialize language env
83
84     common_init_language();
85
86     $path = getPath($_REQUEST);
87
88     $r = Router::get();
89
90     $args = $r->map($path);
91
92     if (!$args) {
93         $cac = new ClientErrorAction(_('Unknown page'), 404);
94         $cac->showPage();
95         return;
96     }
97
98     $args = array_merge($args, $_REQUEST);
99
100     $action = $args['action'];
101
102     if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
103         common_redirect(common_local_url('public'));
104         return;
105     }
106
107     // If the site is private, and they're not on one of the "public"
108     // parts of the site, redirect to login
109
110     if (!$user && common_config('site', 'private') &&
111         !in_array($action, array('login', 'openidlogin', 'finishopenidlogin',
112                                  'recoverpassword', 'api', 'doc', 'register'))) {
113         common_redirect(common_local_url('login'));
114         return;
115     }
116
117     $action_class = ucfirst($action).'Action';
118
119     if (!class_exists($action_class)) {
120         $cac = new ClientErrorAction(_('Unknown action'), 404);
121         $cac->showPage();
122     } else {
123         $action_obj = new $action_class();
124
125         // XXX: find somewhere for this little block to live
126
127         if (common_config('db', 'mirror') && $action_obj->isReadOnly()) {
128             if (is_array(common_config('db', 'mirror'))) {
129                 // "load balancing", ha ha
130                 $k = array_rand($config['db']['mirror']);
131
132                 $mirror = $config['db']['mirror'][$k];
133             } else {
134                 $mirror = $config['db']['mirror'];
135             }
136             $config['db']['database'] = $mirror;
137         }
138
139         try {
140             if ($action_obj->prepare($args)) {
141                 $action_obj->handle($args);
142             }
143         } catch (ClientException $cex) {
144             $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
145             $cac->showPage();
146         } catch (ServerException $sex) { // snort snort guffaw
147             $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
148             $sac->showPage();
149         } catch (Exception $ex) {
150             $sac = new ServerErrorAction($ex->getMessage());
151             $sac->showPage();
152         }
153     }
154 }
155
156 main();
157
158 // XXX: cleanup exit() calls or add an exit handler so
159 // this always gets called
160
161 Event::handle('CleanupPlugin');