This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
storage.php
185 lines (173 loc) · 9.32 KB
/
storage.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
<?php
$deep = preg_match('/^deep/', $_SERVER['HTTP_HOST']);
$beta = strpos($_SERVER['HTTP_HOST'], 'beta') !== false;
set_time_limit(0);
ini_set('log_errors', 1);
ini_set('error_log', '/tmp/php-error-storage'.($beta ? 'beta' : '').'.log');
require_once '../db_connect.php';
function no_cache($status, $wut)
{
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
http_response_code($status);
echo $wut;
exit;
}
function analytics($ok, $uri, $bot_id, $user, $pass)
{
return;
require_once '../storage_url.php';
if (!isset($official_pwr)) {
return;
}
try {
$pdo = new \PDO('mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=stats', $user, $pass);
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
$pdo->prepare('INSERT INTO pwrtelegram (method, id, peak_ram, duration, ok, params) VALUES (?, ?, ?, ?, ?, ?)')->execute(['/file', $bot_id, memory_get_peak_usage(), getrusage()['ru_utime.tv_usec'], (int) $ok, $uri]);
} catch (PDOException $e) {
error_log($e);
}
}
if ($_SERVER['REQUEST_URI'] == '/') {
header("HTTP/1.1 418 I'm a teapot");
analytics(true, '/', null, $dbuser, $dbpassword);
exit('<html><h1>418 I'm a teapot.</h1><br><p>My little teapot, my little teapot, oooh oooh oooh oooh...</p></html>');
}
try {
$servefile = $_SERVER['REQUEST_METHOD'] !== 'HEAD';
$homedir = realpath(__DIR__.'/../').'/';
$pwrhomedir = realpath(__DIR__);
$file_path = urldecode(preg_replace("/^\/*/", '', $_SERVER['REQUEST_URI']));
require_once 'vendor/autoload.php';
$pdo = new PDO($deep ? $deepdb : $db, $deep ? $deepdbuser : $dbuser, $deep ? $deepdbpassword : $dbpassword);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$selectstmt = $pdo->prepare('SELECT * FROM dl WHERE file_path=? LIMIT 1;');
$selectstmt->execute([$file_path]);
$select = $selectstmt->fetch(PDO::FETCH_ASSOC);
if (!($selectstmt->rowCount() > 0)) {
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(404, '<html><body><h1>404 File not found.</h1><br><p>Could not fetch file info from database.</p></body></html>');
}
if (isset($_SERVER['HTTP_RANGE'])) {
$range = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (count($range) == 1) {
$range[1] = '';
}
list($size_unit, $range_orig) = $range;
if ($size_unit == 'bytes') {
//multiple ranges could be specified at the same time, but for simplicity only serve the first range
//http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
$list = explode(',', $range_orig, 2);
if (count($list) == 1) {
$list[1] = '';
}
list($range, $extra_ranges) = $list;
} else {
$range = '';
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(416, '<html><body><h1>416 Requested Range Not Satisfiable.</h1><br><p>Could not use selected range.</p></body></html>');
}
} else {
$range = '';
}
$listseek = explode('-', $range, 2);
if (count($listseek) == 1) {
$listseek[1] = '';
}
list($seek_start, $seek_end) = $listseek;
$seek_end = empty($seek_end) ? ($select['file_size'] - 1) : min(abs(intval($seek_end)), $select['file_size'] - 1);
if (!empty($seek_start) && $seek_end < abs(intval($seek_start))) {
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(416, '<html><body><h1>416 Requested Range Not Satisfiable.</h1><br><p>Could not use selected range.</p></body></html>');
}
$seek_start = empty($seek_start) ? 0 : abs(intval($seek_start));
if ($servefile) {
$pdo->prepare('INSERT INTO dl_stats (file, count) VALUES (?, 1) ON DUPLICATE KEY UPDATE count = count + 1;')->execute([$file_path]);
$madeline = glob($homedir.'/sessions/pwr_'.$select['bot'].'*')[0];
if ($madeline === null) {
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(404, '<html><body><h1>404 File not found.</h1><br><p>Could not fetch file info from database.</p></body></html>');
}
$MadelineProto = new \danog\MadelineProto\API($madeline);
\danog\MadelineProto\Logger::log($file_path);
$MadelineProto->API->getting_state = true;
if ($seek_start > 0 || $seek_end < $select['file_size'] - 1) {
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$select['file_size']);
header('Content-Length: '.($seek_end - $seek_start + 1));
} else {
header('Content-Length: '.$select['file_size']);
}
header('Content-Type: '.$select['mime']);
header('Cache-Control: max-age=31556926;');
header('Content-Transfer-Encoding: Binary');
header('Accept-Ranges: bytes');
//header('Content-disposition: attachment: filename="'.basename($select['file_path']).'"');
$MadelineProto->download_to_stream($select['file_id'], fopen('php://output', 'w'), function ($percent) {
flush();
ob_flush();
\danog\MadelineProto\Logger::log('Download status: '.$percent.'%');
}, $seek_start, $seek_end + 1);
//analytics(true, $file_path, $MadelineProto->get_self()['id'], $dbuser, $dbpassword);
$MadelineProto->API->getting_state = false;
$MadelineProto->API->store_db([], true);
$MadelineProto->API->reset_session();
} else {
if ($seek_start > 0 || $seek_end < $select['file_size'] - 1) {
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$select['file_size']);
header('Content-Length: '.($seek_end - $seek_start + 1));
} else {
header('Content-Length: '.$select['file_size']);
}
header('Content-Type: '.$select['mime']);
header('Cache-Control: max-age=31556926;');
header('Content-Transfer-Encoding: Binary');
header('Accept-Ranges: bytes');
analytics(true, $file_path, null, $dbuser, $dbpassword);
//header('Content-disposition: attachment: filename="'.basename($select['file_path']).'"');
}
} catch (\danog\MadelineProto\ResponseException $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTLTrace());
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(500, '<html><body><h1>500 internal server error</h1><br><p>'.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()).'</p></body></html>');
} catch (\danog\MadelineProto\Exception $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTLTrace());
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(500, '<html><body><h1>500 internal server error</h1><br><p>'.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()).'</p></body></html>');
} catch (\danog\MadelineProto\RPCErrorException $e) {
if (in_array($e->rpc, ['AUTH_KEY_UNREGISTERED', 'SESSION_REVOKED', 'USER_DEACTIVATED'])) {
foreach (glob($madeline.'*') as $file) {
unlink($file);
}
if (isset($MadelineProto)) {
$MadelineProto->session = null;
}
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(500, '<html><body><h1>500 internal server error</h1><br><p>The token/session was revoked</p></body></html>');
}
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTLTrace());
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(500, '<html><body><h1>500 internal server error</h1><br><p>Telegram said: '.$e->getMessage().'</p></body></html>');
} catch (\danog\MadelineProto\TL\Exception $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTLTrace());
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(500, '<html><body><h1>500 internal server error</h1><br><p>'.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()).'</p></body></html>');
} catch (\PDOException $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTLTrace());
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(500, '<html><body><h1>500 internal server error</h1><br><p>'.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()).'</p></body></html>');
} catch (\Exception $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTLTrace());
analytics(false, $file_path, null, $dbuser, $dbpassword);
no_cache(500, '<html><body><h1>500 internal server error</h1><br><p>'.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()).'</p></body></html>');
}