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