]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - index.php
Merge branch '0.9.x' into pluginize-twitter-bridge
[quix0rs-gnu-social.git] / index.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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  * @category StatusNet
20  * @package  StatusNet
21  * @license  GNU Affero General Public License http://www.gnu.org/licenses/
22  * @author Brenda Wallace <shiny@cpan.org>
23  * @author Christopher Vollick <psycotica0@gmail.com>
24  * @author CiaranG <ciaran@ciarang.com>
25  * @author Craig Andrews <candrews@integralblue.com>
26  * @author Evan Prodromou <evan@status.net>
27  * @author Gina Haeussge <osd@foosel.net>
28  * @author Jeffery To <jeffery.to@gmail.com>
29  * @author Mike Cochrane <mikec@mikenz.geek.nz>
30  * @author Robin Millette <millette@controlyourself.ca>
31  * @author Sarven Capadisli <csarven@status.net>
32  * @author Tom Adams <tom@holizz.com>
33  */
34
35 define('INSTALLDIR', dirname(__FILE__));
36 define('STATUSNET', true);
37 define('LACONICA', true); // compatibility
38
39 require_once INSTALLDIR . '/lib/common.php';
40
41 $user = null;
42 $action = null;
43
44 function getPath($req)
45 {
46     if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER))
47         && array_key_exists('p', $req)
48     ) {
49         return $req['p'];
50     } else if (array_key_exists('PATH_INFO', $_SERVER)) {
51         return $_SERVER['PATH_INFO'];
52     } else {
53         return null;
54     }
55 }
56
57 function handleError($error)
58 {
59     if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
60         return;
61     }
62
63     $logmsg = "PEAR error: " . $error->getMessage();
64     if (common_config('site', 'logdebug')) {
65         $logmsg .= " : ". $error->getDebugInfo();
66     }
67     common_log(LOG_ERR, $logmsg);
68     if (common_config('site', 'logdebug')) {
69         $bt = $error->getBacktrace();
70         foreach ($bt as $line) {
71             common_log(LOG_ERR, $line);
72         }
73     }
74     if ($error instanceof DB_DataObject_Error
75         || $error instanceof DB_Error
76     ) {
77         $msg = sprintf(
78             _(
79                 'The database for %s isn\'t responding correctly, '.
80                 'so the site won\'t work properly. '.
81                 'The site admins probably know about the problem, '.
82                 'but you can contact them at %s to make sure. '.
83                 'Otherwise, wait a few minutes and try again.'
84             ),
85             common_config('site', 'name'),
86             common_config('site', 'email')
87         );
88     } else {
89         $msg = _(
90             'An important error occured, probably related to email setup. '.
91             'Check logfiles for more info..'
92         );
93     }
94
95     $dac = new DBErrorAction($msg, 500);
96     $dac->showPage();
97     exit(-1);
98 }
99
100 function checkMirror($action_obj, $args)
101 {
102     global $config;
103
104     static $alwaysRW = array('session', 'remember_me');
105
106     if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
107         if (is_array(common_config('db', 'mirror'))) {
108             // "load balancing", ha ha
109             $arr = common_config('db', 'mirror');
110             $k = array_rand($arr);
111             $mirror = $arr[$k];
112         } else {
113             $mirror = common_config('db', 'mirror');
114         }
115
116         // We ensure that these tables always are used
117         // on the master DB
118
119         $config['db']['database_rw'] = $config['db']['database'];
120         $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
121
122         foreach ($alwaysRW as $table) {
123             $config['db']['table_'.$table] = 'rw';
124         }
125
126         // everyone else uses the mirror
127
128         $config['db']['database'] = $mirror;
129     }
130 }
131
132 function isLoginAction($action)
133 {
134     static $loginActions =  array('login', 'recoverpassword', 'api', 'doc', 'register');
135
136     $login = null;
137
138     if (Event::handle('LoginAction', array($action, &$login))) {
139         $login = in_array($action, $loginActions);
140     }
141
142     return $login;
143 }
144
145 function main()
146 {
147     // fake HTTP redirects using lighttpd's 404 redirects
148     if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
149         $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
150         $_lighty_url = @parse_url($_lighty_url);
151
152         if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
153             $_lighty_path = preg_replace('/^'.preg_quote(common_config('site', 'path')).'\//', '', substr($_lighty_url['path'], 1));
154             $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
155             if ($_lighty_url['query']) {
156                 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
157             }
158             parse_str($_lighty_url['query'], $_lighty_query);
159             foreach ($_lighty_query as $key => $val) {
160                 $_GET[$key] = $_REQUEST[$key] = $val;
161             }
162             $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
163         }
164     }
165     $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
166
167     // quick check for fancy URL auto-detection support in installer.
168     if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/", "", (dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
169         die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
170     }
171     global $user, $action;
172
173     Snapshot::check();
174
175     if (!_have_config()) {
176         $msg = sprintf(
177             _(
178                 "No configuration file found. Try running ".
179                 "the installation program first."
180             )
181         );
182         $sac = new ServerErrorAction($msg);
183         $sac->showPage();
184         return;
185     }
186
187     // For database errors
188
189     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
190
191     // XXX: we need a little more structure in this script
192
193     // get and cache current user
194
195     $user = common_current_user();
196
197     // initialize language env
198
199     common_init_language();
200
201     $path = getPath($_REQUEST);
202
203     $r = Router::get();
204
205     $args = $r->map($path);
206
207     if (!$args) {
208         $cac = new ClientErrorAction(_('Unknown page'), 404);
209         $cac->showPage();
210         return;
211     }
212
213     $args = array_merge($args, $_REQUEST);
214
215     Event::handle('ArgsInitialize', array(&$args));
216
217     $action = $args['action'];
218
219     if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
220         common_redirect(common_local_url('public'));
221         return;
222     }
223
224     // If the site is private, and they're not on one of the "public"
225     // parts of the site, redirect to login
226
227     if (!$user && common_config('site', 'private')
228         && !isLoginAction($action)
229         && !preg_match('/rss$/', $action)
230     ) {
231         common_redirect(common_local_url('login'));
232         return;
233     }
234
235     $action_class = ucfirst($action).'Action';
236
237     if (!class_exists($action_class)) {
238         $cac = new ClientErrorAction(_('Unknown action'), 404);
239         $cac->showPage();
240     } else {
241         $action_obj = new $action_class();
242
243         checkMirror($action_obj, $args);
244
245         try {
246             if ($action_obj->prepare($args)) {
247                 $action_obj->handle($args);
248             }
249         } catch (ClientException $cex) {
250             $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
251             $cac->showPage();
252         } catch (ServerException $sex) { // snort snort guffaw
253             $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
254             $sac->showPage();
255         } catch (Exception $ex) {
256             $sac = new ServerErrorAction($ex->getMessage());
257             $sac->showPage();
258         }
259     }
260 }
261
262 main();
263
264 // XXX: cleanup exit() calls or add an exit handler so
265 // this always gets called
266
267 Event::handle('CleanupPlugin');