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