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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
<?php
// https://github.com/nette/utils/blob/master/src/Utils/Json.php
final class Json
{
public static function encode($value, $pretty = true, bool $asciiSafe = false): string
{
$flags = JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES;
if (!$asciiSafe) {
$flags = $flags | JSON_UNESCAPED_UNICODE;
}
if ($pretty) {
$flags = $flags | JSON_PRETTY_PRINT;
}
return \json_encode($value, $flags);
}
public static function decode(string $json, bool $assoc = true)
{
return \json_decode($json, $assoc, 512, JSON_THROW_ON_ERROR);
}
}
/**
* Get the home page url of rss-bridge e.g. 'https://example.com/' or 'https://example.com/bridge/'
*/
function get_home_page_url(): string
{
$https = $_SERVER['HTTPS'] ?? '';
$host = $_SERVER['HTTP_HOST'] ?? '';
$uri = $_SERVER['REQUEST_URI'] ?? '';
if (($pos = strpos($uri, '?')) !== false) {
$uri = substr($uri, 0, $pos);
}
$scheme = $https === 'on' ? 'https' : 'http';
return "$scheme://$host$uri";
}
/**
* Get the full current url e.g. 'http://example.com/?action=display&bridge=FooBridge'
*/
function get_current_url(): string
{
$https = $_SERVER['HTTPS'] ?? '';
$host = $_SERVER['HTTP_HOST'] ?? '';
$uri = $_SERVER['REQUEST_URI'] ?? '';
$scheme = $https === 'on' ? 'https' : 'http';
return "$scheme://$host$uri";
}
function create_sane_exception_message(\Throwable $e): string
{
$sanitizedMessage = sanitize_root($e->getMessage());
$sanitizedFilepath = sanitize_root($e->getFile());
return sprintf(
'%s: %s in %s line %s',
get_class($e),
$sanitizedMessage,
$sanitizedFilepath,
$e->getLine()
);
}
/**
* Returns e.g. https://github.com/RSS-Bridge/rss-bridge/blob/master/bridges/AO3Bridge.php#L8
*/
function render_github_url(string $file, int $line, string $revision = 'master'): string
{
return sprintf('https://github.com/RSS-Bridge/rss-bridge/blob/%s/%s#L%s', $revision, $file, $line);
}
function trace_from_exception(\Throwable $e): array
{
$frames = array_reverse($e->getTrace());
$frames[] = [
'file' => $e->getFile(),
'line' => $e->getLine(),
];
$trace = [];
foreach ($frames as $frame) {
$trace[] = [
'file' => sanitize_root($frame['file'] ?? ''),
'line' => $frame['line'] ?? null,
'class' => $frame['class'] ?? null,
'type' => $frame['type'] ?? null,
'function' => $frame['function'] ?? null,
];
}
return $trace;
}
function trace_to_call_points(array $trace): array
{
return array_map(fn($frame) => frame_to_call_point($frame), $trace);
}
function frame_to_call_point(array $frame): string
{
if ($frame['class']) {
return sprintf(
'%s(%s): %s%s%s()',
$frame['file'],
$frame['line'],
$frame['class'],
$frame['type'],
$frame['function'],
);
} elseif ($frame['function']) {
return sprintf(
'%s(%s): %s()',
$frame['file'],
$frame['line'],
$frame['function'],
);
} else {
return sprintf(
'%s(%s)',
$frame['file'],
$frame['line'],
);
}
}
/**
* Trim path prefix for privacy/security reasons
*
* Example: "/home/davidsf/rss-bridge/index.php" => "index.php"
*/
function sanitize_root(string $filePath): string
{
// Root folder of the project e.g. /home/satoshi/repos/rss-bridge
$root = dirname(__DIR__);
return _sanitize_path_name($filePath, $root);
}
function _sanitize_path_name(string $s, string $pathName): string
{
// Remove all occurrences of $pathName in the string
return str_replace(["$pathName/", $pathName], '', $s);
}
/**
* This is buggy because strip_tags() removes a lot that isn't html
*/
function is_html(string $text): bool
{
return strlen(strip_tags($text)) !== strlen($text);
}
/**
* Determines the MIME type from a URL/Path file extension.
*
* _Remarks_:
*
* * The built-in functions `mime_content_type` and `fileinfo` require fetching
* remote contents.
* * A caller can hint for a MIME type by appending `#.ext` to the URL (i.e. `#.image`).
*
* Based on https://stackoverflow.com/a/1147952
*
* @param string $url The URL or path to the file.
* @return string The MIME type of the file.
*/
function parse_mime_type($url)
{
static $mime = null;
if (is_null($mime)) {
// Default values, overriden by /etc/mime.types when present
$mime = [
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'webp' => 'image/webp',
'image' => 'image/*',
'mp3' => 'audio/mpeg',
];
// '@' is used to mute open_basedir warning, see issue #818
if (@is_readable('/etc/mime.types')) {
$file = fopen('/etc/mime.types', 'r');
while (($line = fgets($file)) !== false) {
$line = trim(preg_replace('/#.*/', '', $line));
if (!$line) {
continue;
}
$parts = preg_split('/\s+/', $line);
if (count($parts) == 1) {
continue;
}
$type = array_shift($parts);
foreach ($parts as $part) {
$mime[$part] = $type;
}
}
fclose($file);
}
}
if (strpos($url, '?') !== false) {
$url_temp = substr($url, 0, strpos($url, '?'));
if (strpos($url, '#') !== false) {
$anchor = substr($url, strpos($url, '#'));
$url_temp .= $anchor;
}
$url = $url_temp;
}
$ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
if (!empty($mime[$ext])) {
return $mime[$ext];
}
return 'application/octet-stream';
}
/**
* https://stackoverflow.com/a/2510459
*/
function format_bytes(int $bytes, $precision = 2)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
function now(): \DateTimeImmutable
{
return new \DateTimeImmutable();
}
function create_random_string(int $bytes = 16): string
{
return bin2hex(openssl_random_pseudo_bytes($bytes));
}
function returnClientError($message)
{
throw new \Exception($message, 400);
}
function returnServerError($message)
{
throw new \Exception($message, 500);
}
|