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