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