-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathindex.php
211 lines (164 loc) · 5.65 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/*
* Copyright (C) 2012 Platoniq y Fundación Goteo (see README for details)
* This file is part of Goteo.
*
* Goteo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Goteo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Goteo. If not, see <http://www.gnu.org/licenses/agpl.txt>.
*
*/
use Goteo\Core\Resource,
Goteo\Core\Error,
Goteo\Core\Registry,
Goteo\Core\Redirection,
Goteo\Core\ACL,
Goteo\Library\Text,
Goteo\Library\Message,
Goteo\Library\i18n\Locale,
Goteo\Library\i18n\Lang;
if( !file_exists("config.php") ) {
$msg = "This instance of Goteo doesn't seem to be configured, please read the deployment guide, configure and try again.";
error_log($msg);
echo "<div id='failure'><h1>{$msg}</h1></div>";
die;
}
require_once 'config.php';
require_once 'core/common.php';
require_once 'library/i18n/Locale.php';
require_once 'library/i18n/Lang.php';
/*
* Pagina de en mantenimiento
*/
if (GOTEO_MAINTENANCE === true && $_SERVER['REQUEST_URI'] != '/about/maintenance'
&& !isset($_POST['Num_operacion'])
) {
header('Location: /about/maintenance');
}
// Include path
//set_include_path(GOTEO_PATH . PATH_SEPARATOR . '.');
// Autoloader
spl_autoload_register(
function ($cls) {
//echo "Trying to autoload {$cls}...";
$file = __DIR__ . '/' . implode('/', explode('\\', strtolower(substr($cls, 6)))) . '.php';
$file = realpath($file);
if ($file === false) {
// Try in library
// $file = __DIR__ . '/library/' . implode('/', explode('\\', strtolower(substr($cls, 6)))) . '.php';
$file = __DIR__ . '/library/' . strtolower($cls) . '.php';
// die($cls . ' - ' . $file); //Si uso Text::get(id) no lo pilla
}
if ($file !== false) {
//echo "Autoloading {$file}...";
include $file;
}
}
);
// Error handler
set_error_handler (
function ($errno, $errstr, $errfile, $errline, $errcontext) {
// @todo Insert error into buffer
// echo "Error: {$errno}, {$errstr}, {$errfile}, {$errline}, {$errcontext}<br />";
//throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
);
//@NODESYS
define('NODE_ID', GOTEO_NODE);
/**
* Sesión.
*/
session_name('goteo');
session_start();
// set Lang
Lang::set();
// change current locale
$locale_name = Lang::locale();
$locale = new Locale($config['locale']);
// avoid Fatal Error if $local_name is empty.
if ($locale_name)
$locale->set($locale_name);
else
$locale->set('en_GB');
Registry::set('locale', $locale);
// Get URI without query string
$uri = strtok($_SERVER['REQUEST_URI'], '?');
// Get requested segments
$segments = preg_split('!\s*/+\s*!', $uri, -1, \PREG_SPLIT_NO_EMPTY);
// Normalize URI
$uri = '/' . implode('/', $segments);
try {
// Check permissions on requested URI
if (!ACL::check($uri)) {
Message::Info(Text::get('user-login-required-access'));
//si es un cron (ejecutandose) con los parámetros adecuados, no redireccionamos
if (strpos($uri, 'cron') !== false && strcmp($_GET[md5(CRON_PARAM)], md5(CRON_VALUE)) === 0) {
define('CRON_EXEC', true);
} else {
throw new Redirection("/user/login/?return=".rawurlencode($uri));
}
}
// Get controller name
if (!empty($segments) && class_exists("Goteo\\Controller\\{$segments[0]}")) {
// Take first segment as controller
$controller = array_shift($segments);
} else {
$controller = 'index';
}
// Continue
try {
$class = new ReflectionClass("Goteo\\Controller\\{$controller}");
if (!empty($segments) && $class->hasMethod($segments[0])) {
$method = array_shift($segments);
} else {
// Try default method
$method = 'index';
}
// ReflectionMethod
$method = $class->getMethod($method);
// Number of params defined in method
$numParams = $method->getNumberOfParameters();
// Number of required params
$reqParams = $method->getNumberOfRequiredParameters();
// Given params
$gvnParams = count($segments);
if ($gvnParams >= $reqParams && (!($gvnParams > $numParams && $numParams <= $reqParams))) {
// Try to instantiate
$instance = $class->newInstance();
// Start output buffer
ob_start();
// Invoke method
$result = $method->invokeArgs($instance, $segments);
if ($result === null) {
// Get buffer contents
$result = ob_get_contents();
}
ob_end_clean();
if ($result instanceof Resource\MIME) {
header("Content-type: {$result->getMIME()}");
}
echo $result;
// Farewell
die;
}
} catch (\ReflectionException $e) {}
throw new Error(Error::NOT_FOUND);
} catch (Redirection $redirection) {
$url = $redirection->getURL();
$code = $redirection->getCode();
header("Location: {$url}");
} catch (Error $error) {
include "view/error.html.php";
} catch (Exception $exception) {
// Default error (500)
include "view/error.html.php";
}