mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-02 14:47:35 +02:00
refactor: general code base refactor (#2950)
* refactor * fix: bug in previous refactor * chore: exclude phpcompat sniff due to bug in phpcompat * fix: do not leak absolute paths * refactor/fix: batch extensions checking, fix DOS issue
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
<?php
|
||||
|
||||
final class HttpException extends \Exception
|
||||
{
|
||||
}
|
||||
|
||||
// todo: move this somewhere useful, possibly into a function
|
||||
const RSSBRIDGE_HTTP_STATUS_CODES = [
|
||||
'100' => 'Continue',
|
||||
@@ -128,7 +124,8 @@ function getContents(
|
||||
}
|
||||
$cache->saveData($result['body']);
|
||||
break;
|
||||
case 304: // Not Modified
|
||||
case 304:
|
||||
// Not Modified
|
||||
$response['content'] = $cache->loadData();
|
||||
break;
|
||||
default:
|
||||
@@ -379,68 +376,3 @@ function getSimpleHTMLDOMCached(
|
||||
$defaultSpanText
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 getMimeType($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',
|
||||
'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';
|
||||
}
|
||||
|
Reference in New Issue
Block a user