]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - index.php
e83d08c13e9024fe5482aa7b4540b178754dbad4
[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     $msg = sprintf(_('The database for %s isn\'t responding correctly, '.
52                      'so the site won\'t work properly. '.
53                      'The site admins probably know about the problem, '.
54                      'but you can contact them at %s to make sure. '.
55                      'Otherwise, wait a few minutes and try again.'),
56                    common_config('site', 'name'),
57                    common_config('site', 'email'));
58
59     $dac = new DBErrorAction($msg, 500);
60     $dac->showPage();
61     exit(-1);
62 }
63
64 function main()
65 {
66     global $user, $action, $config;
67
68     if (!_have_config()) {
69         $msg = sprintf(_("No configuration file found. Try running ".
70                          "the installation program first."));
71         $sac = new ServerErrorAction($msg);
72         $sac->showPage();
73         return;
74     }
75
76     // For database errors
77
78     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
79
80     // XXX: we need a little more structure in this script
81
82     // get and cache current user
83
84     $user = common_current_user();
85
86     // initialize language env
87
88     common_init_language();
89
90     $path = getPath($_REQUEST);
91
92     $r = Router::get();
93
94     $args = $r->map($path);
95
96     if (!$args) {
97         $cac = new ClientErrorAction(_('Unknown page'), 404);
98         $cac->showPage();
99         return;
100     }
101
102     $args = array_merge($args, $_REQUEST);
103
104     $action = $args['action'];
105
106     if (!$action || !preg_match('/^[a-zA-Z0-9_-]*$/', $action)) {
107         common_redirect(common_local_url('public'));
108         return;
109     }
110
111     // If the site is private, and they're not on one of the "public"
112     // parts of the site, redirect to login
113
114     if (!$user && common_config('site', 'private') &&
115         !in_array($action, array('login', 'openidlogin', 'finishopenidlogin',
116                                  'recoverpassword', 'api', 'doc', 'register'))) {
117         common_redirect(common_local_url('login'));
118         return;
119     }
120
121     $action_class = ucfirst($action).'Action';
122
123     if (!class_exists($action_class)) {
124         $cac = new ClientErrorAction(_('Unknown action'), 404);
125         $cac->showPage();
126     } else {
127         $action_obj = new $action_class();
128
129         // XXX: find somewhere for this little block to live
130
131         if (common_config('db', 'mirror') && $action_obj->isReadOnly()) {
132             if (is_array(common_config('db', 'mirror'))) {
133                 // "load balancing", ha ha
134                 $arr = common_config('db', 'mirror');
135                 $k = array_rand($arr);
136                 $mirror = $arr[$k];
137             } else {
138                 $mirror = common_config('db', 'mirror');
139             }
140             $config['db']['database'] = $mirror;
141         }
142
143         try {
144             if ($action_obj->prepare($args)) {
145                 $action_obj->handle($args);
146             }
147         } catch (ClientException $cex) {
148             $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode());
149             $cac->showPage();
150         } catch (ServerException $sex) { // snort snort guffaw
151             $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode());
152             $sac->showPage();
153         } catch (Exception $ex) {
154             $sac = new ServerErrorAction($ex->getMessage());
155             $sac->showPage();
156         }
157     }
158 }
159
160 main();
161
162 // XXX: cleanup exit() calls or add an exit handler so
163 // this always gets called
164
165 Event::handle('CleanupPlugin');