]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - index.php
Merge branch '0.8.x' into notice-search-no-results
[quix0rs-gnu-social.git] / index.php
1 <?php
2 /**
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, 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     $logmsg = "PEAR error: " . $error->getMessage();
47     if(common_config('site', 'logdebug')) {
48         $logmsg .= " : ". $error->getDebugInfo();
49     }
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);
55         }
56     }
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'));
66     } else {
67         $msg = _('An important error occured, probably related to email setup. '.
68                  'Check logfiles for more info..');
69     }
70
71     $dac = new DBErrorAction($msg, 500);
72     $dac->showPage();
73     exit(-1);
74 }
75
76 function main()
77 {
78     // quick check for fancy URL auto-detection support in installer.
79     if (isset($_SERVER['REDIRECT_URL']) && ((dirname($_SERVER['REQUEST_URI']) . '/check-fancy') === $_SERVER['REDIRECT_URL'])) {
80         die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
81     }
82     global $user, $action, $config;
83
84     Snapshot::check();
85
86     if (!_have_config()) {
87         $msg = sprintf(_("No configuration file found. Try running ".
88                          "the installation program first."));
89         $sac = new ServerErrorAction($msg);
90         $sac->showPage();
91         return;
92     }
93
94     // For database errors
95
96     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
97
98     // XXX: we need a little more structure in this script
99
100     // get and cache current user
101
102     $user = common_current_user();
103
104     // initialize language env
105
106     common_init_language();
107
108     $path = getPath($_REQUEST);
109
110     $r = Router::get();
111
112     $args = $r->map($path);
113
114     if (!$args) {
115         $cac = new ClientErrorAction(_('Unknown page'), 404);
116         $cac->showPage();
117         return;
118     }
119
120     $args = array_merge($args, $_REQUEST);
121
122     Event::handle('ArgsInitialize', array(&$args));
123
124     $action = $args['action'];
125
126     if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
127         common_redirect(common_local_url('public'));
128         return;
129     }
130
131     // If the site is private, and they're not on one of the "public"
132     // parts of the site, redirect to login
133
134     if (!$user && common_config('site', 'private') &&
135         !in_array($action, array('login', 'openidlogin', 'finishopenidlogin',
136                                  'recoverpassword', 'api', 'doc', 'register'))) {
137         common_redirect(common_local_url('login'));
138         return;
139     }
140
141     $action_class = ucfirst($action).'Action';
142
143     if (!class_exists($action_class)) {
144         $cac = new ClientErrorAction(_('Unknown action'), 404);
145         $cac->showPage();
146     } else {
147         $action_obj = new $action_class();
148
149         // XXX: find somewhere for this little block to live
150
151         if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
152             if (is_array(common_config('db', 'mirror'))) {
153                 // "load balancing", ha ha
154                 $arr = common_config('db', 'mirror');
155                 $k = array_rand($arr);
156                 $mirror = $arr[$k];
157             } else {
158                 $mirror = common_config('db', 'mirror');
159             }
160             $config['db']['database'] = $mirror;
161         }
162
163         try {
164             if ($action_obj->prepare($args)) {
165                 $action_obj->handle($args);
166             }
167         } catch (ClientException $cex) {
168             $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
169             $cac->showPage();
170         } catch (ServerException $sex) { // snort snort guffaw
171             $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
172             $sac->showPage();
173         } catch (Exception $ex) {
174             $sac = new ServerErrorAction($ex->getMessage());
175             $sac->showPage();
176         }
177     }
178 }
179
180 main();
181
182 // XXX: cleanup exit() calls or add an exit handler so
183 // this always gets called
184
185 Event::handle('CleanupPlugin');