Cleanup the code
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
b92ebb928a
commit
1336dedd5d
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
/**
|
/**
|
||||||
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
||||||
*
|
*
|
||||||
|
@ -77,8 +78,8 @@ class Detection implements IMimeTypeDetector {
|
||||||
*/
|
*/
|
||||||
public function __construct(IURLGenerator $urlGenerator,
|
public function __construct(IURLGenerator $urlGenerator,
|
||||||
ILogger $logger,
|
ILogger $logger,
|
||||||
$customConfigDir,
|
string $customConfigDir,
|
||||||
$defaultConfigDir) {
|
string $defaultConfigDir) {
|
||||||
$this->urlGenerator = $urlGenerator;
|
$this->urlGenerator = $urlGenerator;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->customConfigDir = $customConfigDir;
|
$this->customConfigDir = $customConfigDir;
|
||||||
|
@ -96,9 +97,9 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $mimetype
|
* @param string $mimetype
|
||||||
* @param string|null $secureMimeType
|
* @param string|null $secureMimeType
|
||||||
*/
|
*/
|
||||||
public function registerType($extension,
|
public function registerType(string $extension,
|
||||||
$mimetype,
|
string $mimetype,
|
||||||
$secureMimeType = null) {
|
?string $secureMimeType = null): void {
|
||||||
$this->mimetypes[$extension] = array($mimetype, $secureMimeType);
|
$this->mimetypes[$extension] = array($mimetype, $secureMimeType);
|
||||||
$this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype;
|
$this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype;
|
||||||
}
|
}
|
||||||
|
@ -112,12 +113,12 @@ class Detection implements IMimeTypeDetector {
|
||||||
*
|
*
|
||||||
* @param array $types
|
* @param array $types
|
||||||
*/
|
*/
|
||||||
public function registerTypeArray($types) {
|
public function registerTypeArray(array $types): void {
|
||||||
$this->mimetypes = array_merge($this->mimetypes, $types);
|
$this->mimetypes = array_merge($this->mimetypes, $types);
|
||||||
|
|
||||||
// Update the alternative mimetypes to avoid having to look them up each time.
|
// Update the alternative mimetypes to avoid having to look them up each time.
|
||||||
foreach ($this->mimetypes as $mimeType) {
|
foreach ($this->mimetypes as $mimeType) {
|
||||||
$this->secureMimeTypes[$mimeType[0]] = isset($mimeType[1]) ? $mimeType[1]: $mimeType[0];
|
$this->secureMimeTypes[$mimeType[0]] = $mimeType[1] ?? $mimeType[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +137,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
/**
|
/**
|
||||||
* Add the mimetype aliases if they are not yet present
|
* Add the mimetype aliases if they are not yet present
|
||||||
*/
|
*/
|
||||||
private function loadAliases() {
|
private function loadAliases(): void {
|
||||||
if (!empty($this->mimeTypeAlias)) {
|
if (!empty($this->mimeTypeAlias)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -148,12 +149,12 @@ class Detection implements IMimeTypeDetector {
|
||||||
/**
|
/**
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
public function getAllAliases() {
|
public function getAllAliases(): array {
|
||||||
$this->loadAliases();
|
$this->loadAliases();
|
||||||
return $this->mimeTypeAlias;
|
return $this->mimeTypeAlias;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOnlyDefaultAliases() {
|
public function getOnlyDefaultAliases(): array {
|
||||||
$this->loadMappings();
|
$this->loadMappings();
|
||||||
$this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
|
$this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
|
||||||
return $this->mimeTypeAlias;
|
return $this->mimeTypeAlias;
|
||||||
|
@ -162,7 +163,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
/**
|
/**
|
||||||
* Add mimetype mappings if they are not yet present
|
* Add mimetype mappings if they are not yet present
|
||||||
*/
|
*/
|
||||||
private function loadMappings() {
|
private function loadMappings(): void {
|
||||||
if (!empty($this->mimetypes)) {
|
if (!empty($this->mimetypes)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -176,7 +177,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getAllMappings() {
|
public function getAllMappings(): array {
|
||||||
$this->loadMappings();
|
$this->loadMappings();
|
||||||
return $this->mimetypes;
|
return $this->mimetypes;
|
||||||
}
|
}
|
||||||
|
@ -187,7 +188,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function detectPath($path) {
|
public function detectPath($path): string {
|
||||||
$this->loadMappings();
|
$this->loadMappings();
|
||||||
|
|
||||||
$fileName = basename($path);
|
$fileName = basename($path);
|
||||||
|
@ -204,12 +205,10 @@ class Detection implements IMimeTypeDetector {
|
||||||
//try to guess the type by the file extension
|
//try to guess the type by the file extension
|
||||||
$extension = strtolower(strrchr($fileName, '.'));
|
$extension = strtolower(strrchr($fileName, '.'));
|
||||||
$extension = substr($extension, 1); //remove leading .
|
$extension = substr($extension, 1); //remove leading .
|
||||||
return (isset($this->mimetypes[$extension]) && isset($this->mimetypes[$extension][0]))
|
return $this->mimetypes[$extension][0] ?? 'application/octet-stream';
|
||||||
? $this->mimetypes[$extension][0]
|
|
||||||
: 'application/octet-stream';
|
|
||||||
} else {
|
|
||||||
return 'application/octet-stream';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 'application/octet-stream';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -223,12 +222,13 @@ class Detection implements IMimeTypeDetector {
|
||||||
|
|
||||||
if (@is_dir($path)) {
|
if (@is_dir($path)) {
|
||||||
// directories are easy
|
// directories are easy
|
||||||
return "httpd/unix-directory";
|
return 'httpd/unix-directory';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$mimeType = 'application/octet-stream';
|
||||||
if (function_exists('finfo_open')
|
if (function_exists('finfo_open')
|
||||||
and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)
|
&& function_exists('finfo_file')
|
||||||
) {
|
&& $finfo = finfo_open(FILEINFO_MIME)) {
|
||||||
$info = @strtolower(finfo_file($finfo, $path));
|
$info = @strtolower(finfo_file($finfo, $path));
|
||||||
finfo_close($finfo);
|
finfo_close($finfo);
|
||||||
if ($info) {
|
if ($info) {
|
||||||
|
@ -237,16 +237,21 @@ class Detection implements IMimeTypeDetector {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
$isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://');
|
|
||||||
if (!$isWrapped and function_exists("mime_content_type")) {
|
if (strpos($path, '://') !== false && strpos($path, 'file://') === 0) {
|
||||||
|
// Is the file wrapped in a stream?
|
||||||
|
return $mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('mime_content_type')) {
|
||||||
// use mime magic extension if available
|
// use mime magic extension if available
|
||||||
$mimeType = mime_content_type($path);
|
$mimeType = mime_content_type($path);
|
||||||
}
|
}
|
||||||
if (!$isWrapped and $mimeType === 'application/octet-stream' && \OC_Helper::canExecute("file")) {
|
if ($mimeType === 'application/octet-stream' && \OC_Helper::canExecute('file')) {
|
||||||
// it looks like we have a 'file' command,
|
// it looks like we have a 'file' command,
|
||||||
// lets see if it does have mime support
|
// lets see if it does have mime support
|
||||||
$path = escapeshellarg($path);
|
$path = escapeshellarg($path);
|
||||||
$fp = popen("file -b --mime-type $path 2>/dev/null", "r");
|
$fp = popen("file -b --mime-type $path 2>/dev/null", 'r');
|
||||||
$reply = fgets($fp);
|
$reply = fgets($fp);
|
||||||
pclose($fp);
|
pclose($fp);
|
||||||
|
|
||||||
|
@ -267,7 +272,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function detect($path) {
|
public function detect($path): string {
|
||||||
$mimeType = $this->detectPath($path);
|
$mimeType = $this->detectPath($path);
|
||||||
|
|
||||||
if ($mimeType !== 'application/octet-stream') {
|
if ($mimeType !== 'application/octet-stream') {
|
||||||
|
@ -283,12 +288,13 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $data
|
* @param string $data
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function detectString($data) {
|
public function detectString($data): string {
|
||||||
if (function_exists('finfo_open') and function_exists('finfo_file')) {
|
if (function_exists('finfo_open') && function_exists('finfo_file')) {
|
||||||
$finfo = finfo_open(FILEINFO_MIME);
|
$finfo = finfo_open(FILEINFO_MIME);
|
||||||
$info = finfo_buffer($finfo, $data);
|
$info = finfo_buffer($finfo, $data);
|
||||||
return strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
|
return strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
|
$tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
|
||||||
$fh = fopen($tmpFile, 'wb');
|
$fh = fopen($tmpFile, 'wb');
|
||||||
fwrite($fh, $data, 8024);
|
fwrite($fh, $data, 8024);
|
||||||
|
@ -297,7 +303,6 @@ class Detection implements IMimeTypeDetector {
|
||||||
unset($tmpFile);
|
unset($tmpFile);
|
||||||
return $mime;
|
return $mime;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a secure mimetype that won't expose potential XSS.
|
* Get a secure mimetype that won't expose potential XSS.
|
||||||
|
@ -305,12 +310,10 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $mimeType
|
* @param string $mimeType
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getSecureMimeType($mimeType) {
|
public function getSecureMimeType($mimeType): string {
|
||||||
$this->loadMappings();
|
$this->loadMappings();
|
||||||
|
|
||||||
return isset($this->secureMimeTypes[$mimeType])
|
return $this->secureMimeTypes[$mimeType] ?? 'application/octet-stream';
|
||||||
? $this->secureMimeTypes[$mimeType]
|
|
||||||
: 'application/octet-stream';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -318,7 +321,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
* @param string $mimetype the MIME type
|
* @param string $mimetype the MIME type
|
||||||
* @return string the url
|
* @return string the url
|
||||||
*/
|
*/
|
||||||
public function mimeTypeIcon($mimetype) {
|
public function mimeTypeIcon($mimetype): string {
|
||||||
$this->loadAliases();
|
$this->loadAliases();
|
||||||
|
|
||||||
while (isset($this->mimeTypeAlias[$mimetype])) {
|
while (isset($this->mimeTypeAlias[$mimetype])) {
|
||||||
|
@ -329,8 +332,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace slash and backslash with a minus
|
// Replace slash and backslash with a minus
|
||||||
$icon = str_replace('/', '-', $mimetype);
|
$icon = str_replace(['/', '\\'], '-', $mimetype);
|
||||||
$icon = str_replace('\\', '-', $icon);
|
|
||||||
|
|
||||||
// Is it a dir?
|
// Is it a dir?
|
||||||
if ($mimetype === 'dir') {
|
if ($mimetype === 'dir') {
|
||||||
|
|
Loading…
Reference in New Issue