]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - index.php
Merge branch '0.9.x' into userflag
[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 //error_log(print_r($error,1));
72     if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
73         return;
74     }
75
76     $logmsg = "PEAR error: " . $error->getMessage();
77     if (common_config('site', 'logdebug')) {
78         $logmsg .= " : ". $error->getDebugInfo();
79     }
80     common_log(LOG_ERR, $logmsg);
81     if (common_config('site', 'logdebug')) {
82         $bt = $error->getBacktrace();
83         foreach ($bt as $line) {
84             common_log(LOG_ERR, $line);
85         }
86     }
87     if ($error instanceof DB_DataObject_Error
88         || $error instanceof DB_Error
89     ) {
90         $msg = sprintf(
91             _(
92                 'The database for %s isn\'t responding correctly, '.
93                 'so the site won\'t work properly. '.
94                 'The site admins probably know about the problem, '.
95                 'but you can contact them at %s to make sure. '.
96                 'Otherwise, wait a few minutes and try again.'
97             ),
98             common_config('site', 'name'),
99             common_config('site', 'email')
100         );
101     } else {
102         $msg = _(
103             'An important error occured, probably related to email setup. '.
104             'Check logfiles for more info..'
105         );
106     }
107
108     $dac = new DBErrorAction($msg, 500);
109     $dac->showPage();
110     exit(-1);
111 }
112
113 function checkMirror($action_obj, $args)
114 {
115     global $config;
116
117     static $alwaysRW = array('session', 'remember_me');
118
119     if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
120         if (is_array(common_config('db', 'mirror'))) {
121             // "load balancing", ha ha
122             $arr = common_config('db', 'mirror');
123             $k = array_rand($arr);
124             $mirror = $arr[$k];
125         } else {
126             $mirror = common_config('db', 'mirror');
127         }
128
129         // We ensure that these tables always are used
130         // on the master DB
131
132         $config['db']['database_rw'] = $config['db']['database'];
133         $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
134
135         foreach ($alwaysRW as $table) {
136             $config['db']['table_'.$table] = 'rw';
137         }
138
139         // everyone else uses the mirror
140
141         $config['db']['database'] = $mirror;
142     }
143 }
144
145 function isLoginAction($action)
146 {
147     static $loginActions =  array('login', 'recoverpassword', 'api', 'doc', 'register', 'publicxrds');
148
149     $login = null;
150
151     if (Event::handle('LoginAction', array($action, &$login))) {
152         $login = in_array($action, $loginActions);
153     }
154
155     return $login;
156 }
157
158 function main()
159 {
160     // fake HTTP redirects using lighttpd's 404 redirects
161     if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
162         $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
163         $_lighty_url = @parse_url($_lighty_url);
164
165         if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
166             $_lighty_path = preg_replace('/^'.preg_quote(common_config('site', 'path')).'\//', '', substr($_lighty_url['path'], 1));
167             $_SERVER['QUERY_STRING'] = 'p='.$_lighty_path;
168             if ($_lighty_url['query']) {
169                 $_SERVER['QUERY_STRING'] .= '&'.$_lighty_url['query'];
170             }
171             parse_str($_lighty_url['query'], $_lighty_query);
172             foreach ($_lighty_query as $key => $val) {
173                 $_GET[$key] = $_REQUEST[$key] = $val;
174             }
175             $_GET['p'] = $_REQUEST['p'] = $_lighty_path;
176         }
177     }
178     $_SERVER['REDIRECT_URL'] = preg_replace("/\?.+$/", "", $_SERVER['REQUEST_URI']);
179
180     // quick check for fancy URL auto-detection support in installer.
181     if (isset($_SERVER['REDIRECT_URL']) && (preg_replace("/^\/$/", "", (dirname($_SERVER['REQUEST_URI']))) . '/check-fancy') === $_SERVER['REDIRECT_URL']) {
182         die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
183     }
184     global $user, $action;
185
186     Snapshot::check();
187
188     if (!_have_config()) {
189         $msg = sprintf(
190             _(
191                 "No configuration file found. Try running ".
192                 "the installation program first."
193             )
194         );
195         $sac = new ServerErrorAction($msg);
196         $sac->showPage();
197         return;
198     }
199
200     // For database errors
201
202     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
203
204     // XXX: we need a little more structure in this script
205
206     // get and cache current user
207
208     $user = common_current_user();
209
210     // initialize language env
211
212     common_init_language();
213
214     $path = getPath($_REQUEST);
215
216     $r = Router::get();
217
218     $args = $r->map($path);
219
220     if (!$args) {
221         $cac = new ClientErrorAction(_('Unknown page'), 404);
222         $cac->showPage();
223         return;
224     }
225
226     $args = array_merge($args, $_REQUEST);
227
228     Event::handle('ArgsInitialize', array(&$args));
229
230     $action = $args['action'];
231
232     if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
233         common_redirect(common_local_url('public'));
234         return;
235     }
236
237     // If the site is private, and they're not on one of the "public"
238     // parts of the site, redirect to login
239
240     if (!$user && common_config('site', 'private')
241         && !isLoginAction($action)
242         && !preg_match('/rss$/', $action)
243         && !preg_match('/^Api/', $action)
244     ) {
245         common_redirect(common_local_url('login'));
246         return;
247     }
248
249     $action_class = ucfirst($action).'Action';
250
251     if (!class_exists($action_class)) {
252         $cac = new ClientErrorAction(_('Unknown action'), 404);
253         $cac->showPage();
254     } else {
255         $action_obj = new $action_class();
256
257         checkMirror($action_obj, $args);
258
259         try {
260             if ($action_obj->prepare($args)) {
261                 $action_obj->handle($args);
262             }
263         } catch (ClientException $cex) {
264             $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
265             $cac->showPage();
266         } catch (ServerException $sex) { // snort snort guffaw
267             $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
268             $sac->showPage();
269         } catch (Exception $ex) {
270             $sac = new ServerErrorAction($ex->getMessage());
271             $sac->showPage();
272         }
273     }
274 }
275
276 main();
277
278 // XXX: cleanup exit() calls or add an exit handler so
279 // this always gets called
280
281 Event::handle('CleanupPlugin');