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