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