-
Notifications
You must be signed in to change notification settings - Fork 96
/
gazelle.php
198 lines (178 loc) · 6.41 KB
/
gazelle.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
<?php
use Gazelle\Util\Crypto;
use Gazelle\Util\Time;
// 1. Initialization
require_once(__DIR__ . '/lib/bootstrap.php');
global $Cache, $Debug, $Twig;
// Get the user's actual IP address if they're proxied.
if (
!empty($_SERVER['HTTP_X_FORWARDED_FOR'])
&& proxyCheck($_SERVER['REMOTE_ADDR'])
&& filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$context = new Gazelle\BaseRequestContext(
$_SERVER['SCRIPT_NAME'],
$_SERVER['REMOTE_ADDR'],
$_SERVER['HTTP_USER_AGENT'] ?? '[no-useragent]',
);
if (!$context->isValid()) {
exit;
}
$module = $context->module();
if (
in_array($module, ['announce', 'scrape'])
|| (
isset($_REQUEST['info_hash'])
&& isset($_REQUEST['peer_id'])
)
) {
die("d14:failure reason40:Invalid .torrent, try downloading again.e");
}
// 2. Do we have a viewer?
$SessionID = false;
$Viewer = null;
$ipv4Man = new Gazelle\Manager\IPv4();
$userMan = new Gazelle\Manager\User();
Gazelle\Util\Twig::setUserMan($userMan);
// Authorization header only makes sense for the ajax endpoint
if (!empty($_SERVER['HTTP_AUTHORIZATION']) && $module === 'ajax') {
if ($ipv4Man->isBanned($context->remoteAddr())) {
header('Content-type: application/json');
json_die('failure', 'your ip address has been banned');
}
[$success, $result] = $userMan->findByAuthorization($ipv4Man, $_SERVER['HTTP_AUTHORIZATION']);
if ($success) {
$Viewer = $result;
define('AUTHED_BY_TOKEN', true);
} else {
header('Content-type: application/json');
json_die('failure', $result);
}
} elseif (isset($_COOKIE['session'])) {
$forceLogout = function (): never {
setcookie('session', '', [
'expires' => time() - 86_400 * 90,
'path' => '/',
'secure' => !DEBUG_MODE,
'httponly' => true,
'samesite' => 'Lax',
]);
header('Location: login.php');
exit;
};
$cookieData = Crypto::decrypt($_COOKIE['session'], ENCKEY);
if ($cookieData === false) {
$forceLogout();
}
[$SessionID, $userId] = explode('|~|', $cookieData);
$Viewer = $userMan->findById((int)$userId);
if (is_null($Viewer)) {
$forceLogout();
}
if ($Viewer->isDisabled() && !in_array($module, ['index', 'login'])) {
$Viewer->logoutEverywhere();
$forceLogout();
}
$session = new Gazelle\User\Session($Viewer);
if (!$session->valid($SessionID)) {
$Viewer->logout($SessionID);
$forceLogout();
}
$session->refresh($SessionID, $context->remoteAddr(), $context->ua());
unset($browser, $session, $userId, $cookieData, $forceLogout);
} elseif ($module === 'torrents' && ($_REQUEST['action'] ?? '') == 'download' && isset($_REQUEST['torrent_pass'])) {
$Viewer = $userMan->findByAnnounceKey($_REQUEST['torrent_pass']);
if (is_null($Viewer) || $Viewer->isDisabled() || $Viewer->isLocked()) {
header('HTTP/1.1 403 Forbidden');
exit;
}
} elseif (!in_array($module, ['enable', 'index', 'login', 'recovery', 'register'])) {
if (
// Ocelot is allowed
!($module === 'tools' && ($_GET['action'] ?? '') === 'ocelot' && ($_GET['key'] ?? '') === TRACKER_SECRET)
) {
// but for everything else, we need a $Viewer
header('Location: login.php');
exit;
}
}
// 3. We have a viewer (or this is a login or registration attempt)
if ($Viewer) {
if ($Viewer->hasAttr('admin-error-reporting')) {
error_reporting(E_ALL);
}
if ($Viewer->permitted('site_disable_ip_history')) {
$context->anonymize();
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
}
if ($Viewer->ipaddr() != $context->remoteAddr() && !$Viewer->permitted('site_disable_ip_history')) {
if ($ipv4Man->isBanned($context->remoteAddr())) {
error('Your IP address has been banned.');
}
$ipv4Man->register($Viewer, $context->remoteAddr());
}
if ($Viewer->isLocked() && !in_array($module, ['staffpm', 'ajax', 'locked', 'logout', 'login'])) {
$context->setModule('locked');
}
// To proxify images (or not), or e.g. not render the name of a thread
// for a user who may lack the privileges to see it in the first place.
\Text::setViewer($Viewer);
}
$Debug->mark('load page');
if (DEBUG_MODE || ($Viewer && $Viewer->permitted('site_debug'))) {
$Twig->addExtension(new Twig\Extension\DebugExtension());
}
Gazelle\Base::setRequestContext($context);
// for sections/tools/development/process_info.php
$Cache->cache_value('php_' . getmypid(), [
'start' => Time::sqlTime(),
'document' => $module,
'query' => $_SERVER['QUERY_STRING'],
'get' => $_GET,
'post' => array_diff_key(
$_POST,
array_fill_keys(['password', 'new_pass_1', 'new_pass_2', 'verifypassword', 'confirm_password', 'ChangePassword', 'Password'], true)
)
], 600);
register_shutdown_function(
function () {
if (preg_match(DEBUG_URI, $_SERVER['REQUEST_URI'])) {
require(DEBUG_TRACE);
}
$error = error_get_last();
if ($error['type'] ?? 0 == E_ERROR) {
global $Debug;
$Debug->saveCase(str_replace(SERVER_ROOT . '/', '', $error['message']));
}
}
);
// 4. Display the page
header('Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
$file = realpath(__DIR__ . "/sections/{$module}/index.php");
if (!$file || !preg_match('/^[a-z][a-z0-9_]+$/', $module)) {
error($Viewer ? 403 : 404);
}
try {
require_once($file);
} catch (Gazelle\DB\MysqlException $e) {
Gazelle\DB::DB()->rollback(); // if there was an ongoing transaction, abort it
if (DEBUG_MODE || (isset($Viewer) && $Viewer->permitted('site_debug'))) {
echo $Twig->render('error-db.twig', [
'message' => $e->getMessage(),
'trace' => str_replace(SERVER_ROOT . '/', '', $e->getTraceAsString()),
]);
} else {
$id = $Debug->saveError($e);
error("That is not supposed to happen, please create a thread in the Bugs forum explaining what you were doing and referencing Error ID $id");
}
} catch (\Exception $e) {
$Debug->saveError($e);
}
// 5. Finish up
$Debug->mark('send to user');
if (!is_null($Viewer)) {
$Debug->profile($Viewer, $module);
}