]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - index.php
Merge branch '0.7.x' into 0.8.x
[quix0rs-gnu-social.git] / index.php
1 <?php
2 /**
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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
20 define('INSTALLDIR', dirname(__FILE__));
21 define('LACONICA', true);
22
23 require_once INSTALLDIR . '/lib/common.php';
24
25 $user = null;
26 $action = null;
27
28 function getPath($req)
29 {
30     if ((common_config('site', 'fancy') || !array_key_exists('PATH_INFO', $_SERVER))
31         && array_key_exists('p', $req)) {
32         return $req['p'];
33     } else if (array_key_exists('PATH_INFO', $_SERVER)) {
34         return $_SERVER['PATH_INFO'];
35     } else {
36         return null;
37     }
38 }
39
40 function handleError($error)
41 {
42     if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
43         return;
44     }
45
46     $logmsg = "PEAR error: " . $error->getMessage();
47     if(common_config('site', 'logdebug')) {
48         $logmsg .= " : ". $error->getDebugInfo();
49     }
50     common_log(LOG_ERR, $logmsg);
51     if ($error instanceof DB_DataObject_Error) {
52         $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
53                          'so the site won\'t work properly. '.
54                          'The site admins probably know about the problem, '.
55                          'but you can contact them at %s to make sure. '.
56                          'Otherwise, wait a few minutes and try again.'),
57                        common_config('site', 'name'),
58                        common_config('site', 'email'));
59     } else {
60         $msg = _('An important error occured, probably related to email setup. '.
61                  'Check logfiles for more info..');
62     }
63
64     $dac = new DBErrorAction($msg, 500);
65     $dac->showPage();
66     exit(-1);
67 }
68
69 function main()
70 {
71     // quick check for fancy URL auto-detection support in installer.
72     if (isset($_SERVER['REDIRECT_URL']) && ((dirname($_SERVER['REQUEST_URI']) . '/check-fancy') === $_SERVER['REDIRECT_URL'])) {
73         die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs.");
74     }
75     global $user, $action, $config;
76
77     Snapshot::check();
78
79     if (!_have_config()) {
80         $msg = sprintf(_("No configuration file found. Try running ".
81                          "the installation program first."));
82         $sac = new ServerErrorAction($msg);
83         $sac->showPage();
84         return;
85     }
86
87     // For database errors
88
89     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
90
91     // XXX: we need a little more structure in this script
92
93     // get and cache current user
94
95     $user = common_current_user();
96
97     // initialize language env
98
99     common_init_language();
100
101     $path = getPath($_REQUEST);
102
103     $r = Router::get();
104
105     $args = $r->map($path);
106
107     if (!$args) {
108         $cac = new ClientErrorAction(_('Unknown page'), 404);
109         $cac->showPage();
110         return;
111     }
112
113     $args = array_merge($args, $_REQUEST);
114
115     Event::handle('ArgsInitialize', array(&$args));
116
117     $action = $args['action'];
118
119     if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
120         common_redirect(common_local_url('public'));
121         return;
122     }
123
124     // If the site is private, and they're not on one of the "public"
125     // parts of the site, redirect to login
126
127     if (!$user && common_config('site', 'private') &&
128         !in_array($action, array('login', 'openidlogin', 'finishopenidlogin',
129                                  'recoverpassword', 'api', 'doc', 'register'))) {
130         common_redirect(common_local_url('login'));
131         return;
132     }
133
134     $action_class = ucfirst($action).'Action';
135
136     if (!class_exists($action_class)) {
137         $cac = new ClientErrorAction(_('Unknown action'), 404);
138         $cac->showPage();
139     } else {
140         $action_obj = new $action_class();
141
142         // XXX: find somewhere for this little block to live
143
144         if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
145             if (is_array(common_config('db', 'mirror'))) {
146                 // "load balancing", ha ha
147                 $arr = common_config('db', 'mirror');
148                 $k = array_rand($arr);
149                 $mirror = $arr[$k];
150             } else {
151                 $mirror = common_config('db', 'mirror');
152             }
153             $config['db']['database'] = $mirror;
154         }
155
156         try {
157             if ($action_obj->prepare($args)) {
158                 $action_obj->handle($args);
159             }
160         } catch (ClientException $cex) {
161             $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
162             $cac->showPage();
163         } catch (ServerException $sex) { // snort snort guffaw
164             $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
165             $sac->showPage();
166         } catch (Exception $ex) {
167             $sac = new ServerErrorAction($ex->getMessage());
168             $sac->showPage();
169         }
170     }
171 }
172
173 main();
174
175 // XXX: cleanup exit() calls or add an exit handler so
176 // this always gets called
177
178 Event::handle('CleanupPlugin');