]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - index.php
add hooks to allow loading custom help documentation
[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 checkMirror($action_obj, $args)
77 {
78     global $config;
79
80     static $alwaysRW = array('session', 'remember_me');
81
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);
87             $mirror = $arr[$k];
88         } else {
89             $mirror = common_config('db', 'mirror');
90         }
91
92         // We ensure that these tables always are used
93         // on the master DB
94
95         $config['db']['database_rw'] = $config['db']['database'];
96         $config['db']['ini_rw'] = INSTALLDIR.'/classes/laconica.ini';
97
98         foreach ($alwaysRW as $table) {
99             $config['db']['table_'.$table] = 'rw';
100         }
101
102         // everyone else uses the mirror
103
104         $config['db']['database'] = $mirror;
105     }
106 }
107
108 function isLoginAction($action)
109 {
110     static $loginActions =  array('login', 'recoverpassword', 'api', 'doc', 'register');
111
112     $login = null;
113
114     if (Event::handle('LoginAction', array($action, &$login))) {
115         $login = in_array($action, $loginActions);
116     }
117
118     return $login;
119 }
120
121 function main()
122 {
123     // fake HTTP redirects using lighttpd's 404 redirects
124     if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
125         $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
126         $_lighty_url = @parse_url($_lighty_url);
127
128         if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
129             $_lighty_path = preg_replace('/^'.preg_quote(common_config('site','path')).'\//', '', substr($_lighty_url['path'], 1));
130             $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
131             if ($_lighty_url['query'])
132                 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
133             parse_str($_lighty_url['query'], $_lighty_query);
134             foreach ($_lighty_query as $key => $val) {
135                 $_GET[$key] = $_REQUEST[$key] = $val;
136             }
137             $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
138         }
139     }
140     $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
141
142     // quick check for fancy URL auto-detection support in installer.
143     if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/","",(dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
144         die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
145     }
146     global $user, $action;
147
148     Snapshot::check();
149
150     if (!_have_config()) {
151         $msg = sprintf(_("No configuration file found. Try running ".
152                          "the installation program first."));
153         $sac = new ServerErrorAction($msg);
154         $sac->showPage();
155         return;
156     }
157
158     // For database errors
159
160     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
161
162     // XXX: we need a little more structure in this script
163
164     // get and cache current user
165
166     $user = common_current_user();
167
168     // initialize language env
169
170     common_init_language();
171
172     $path = getPath($_REQUEST);
173
174     $r = Router::get();
175
176     $args = $r->map($path);
177
178     if (!$args) {
179         $cac = new ClientErrorAction(_('Unknown page'), 404);
180         $cac->showPage();
181         return;
182     }
183
184     $args = array_merge($args, $_REQUEST);
185
186     Event::handle('ArgsInitialize', array(&$args));
187
188     $action = $args['action'];
189
190     if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
191         common_redirect(common_local_url('public'));
192         return;
193     }
194
195     // If the site is private, and they're not on one of the "public"
196     // parts of the site, redirect to login
197
198     if (!$user && common_config('site', 'private') &&
199         !isLoginAction($action) &&
200         !preg_match('/rss$/', $action)) {
201         common_redirect(common_local_url('login'));
202         return;
203     }
204
205     $action_class = ucfirst($action).'Action';
206
207     if (!class_exists($action_class)) {
208         $cac = new ClientErrorAction(_('Unknown action'), 404);
209         $cac->showPage();
210     } else {
211         $action_obj = new $action_class();
212
213         checkMirror($action_obj, $args);
214
215         try {
216             if ($action_obj->prepare($args)) {
217                 $action_obj->handle($args);
218             }
219         } catch (ClientException $cex) {
220             $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
221             $cac->showPage();
222         } catch (ServerException $sex) { // snort snort guffaw
223             $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
224             $sac->showPage();
225         } catch (Exception $ex) {
226             $sac = new ServerErrorAction($ex->getMessage());
227             $sac->showPage();
228         }
229     }
230 }
231
232 main();
233
234 // XXX: cleanup exit() calls or add an exit handler so
235 // this always gets called
236
237 Event::handle('CleanupPlugin');