Skip to content

Commit

Permalink
Merge pull request #55 from Progi1984/fixGetMimeType
Browse files Browse the repository at this point in the history
Fix get mime type
  • Loading branch information
Progi1984 committed Nov 5, 2020
2 parents 5a3d340 + 1ac495a commit f53b9e6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion blockreassurance.php
Expand Up @@ -133,7 +133,7 @@ public function __construct()
// Confirm uninstall
$this->confirmUninstall = $this->trans('Are you sure you want to uninstall this module?', [], 'Modules.Blockreassurance.Admin');
$this->ps_url = $this->context->link->getBaseLink();
$this->ps_versions_compliancy = ['min' => '1.7.7.0', 'max' => _PS_VERSION_];
$this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_];
$this->templateFile = 'module:blockreassurance/views/templates/hook/blockreassurance.tpl';
}

Expand Down
42 changes: 40 additions & 2 deletions classes/ReassuranceActivity.php
Expand Up @@ -178,7 +178,7 @@ public static function getAllBlockByStatus($id_lang = 1, $id_shop = 1)
{
$sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'psreassurance` pr
LEFT JOIN ' . _DB_PREFIX_ . 'psreassurance_lang prl ON (pr.id_psreassurance = prl.id_psreassurance)
WHERE prl.id_lang = "' . (int) $id_lang . '"
WHERE prl.id_lang = "' . (int) $id_lang . '"
AND prl.id_shop = "' . (int) $id_shop . '"
AND pr.status = 1
ORDER BY pr.position';
Expand All @@ -187,9 +187,47 @@ public static function getAllBlockByStatus($id_lang = 1, $id_shop = 1)

foreach ($result as &$item) {
$item['is_svg'] = !empty($item['custom_icon'])
&& (ImageManager::getMimeType(str_replace(__PS_BASE_URI__, _PS_ROOT_DIR_ . DIRECTORY_SEPARATOR, $item['custom_icon'])) == 'image/svg');
&& (self::getMimeType(str_replace(__PS_BASE_URI__, _PS_ROOT_DIR_ . DIRECTORY_SEPARATOR, $item['custom_icon'])) == 'image/svg');
}

return $result;
}

/**
* @return string|bool
*/
public static function getMimeType(string $filename)
{
$mimeType = false;
// Try with GD
if (function_exists('getimagesize')) {
$imageInfo = @getimagesize($filename);
if ($imageInfo) {
$mimeType = $imageInfo['mime'];
}
}
// Try with FileInfo
if (!$mimeType && function_exists('finfo_open')) {
$const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
$finfo = finfo_open($const);
$mimeType = finfo_file($finfo, $filename);
finfo_close($finfo);
}
// Try with Mime
if (!$mimeType && function_exists('mime_content_type')) {
$mimeType = mime_content_type($filename);
}
// Try with exec command and file binary
if (!$mimeType && function_exists('exec')) {
$mimeType = trim(exec('file -b --mime-type ' . escapeshellarg($filename)));
if (!$mimeType) {
$mimeType = trim(exec('file --mime ' . escapeshellarg($filename)));
}
if (!$mimeType) {
$mimeType = trim(exec('file -bi ' . escapeshellarg($filename)));
}
}

return $mimeType;
}
}
40 changes: 1 addition & 39 deletions controllers/admin/AdminBlockListingController.php
Expand Up @@ -186,7 +186,7 @@ public function displayAjaxSaveBlockContent()
} else {
// PrestaShop < 1.7.7
$validUpload = false;
$mimeType = $this->getMimeType($customImage['tmp_name']);
$mimeType = ReassuranceActivity::getMimeType($customImage['tmp_name']);
if ($mimeType && (
!in_array($mimeType, $authMimeType)
|| !ImageManager::isCorrectImageFileExt($customImage['name'], $authExtensions)
Expand Down Expand Up @@ -248,42 +248,4 @@ public function displayAjaxUpdatePosition()
// Response
$this->ajaxRenderJson($result ? 'success' : 'error');
}

/**
* @return string|bool
*/
private function getMimeType(string $filename)
{
$mimeType = false;
// Try with GD
if (function_exists('getimagesize')) {
$imageInfo = @getimagesize($filename);
if ($imageInfo) {
$mimeType = $imageInfo['mime'];
}
}
// Try with FileInfo
if (!$mimeType && function_exists('finfo_open')) {
$const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
$finfo = finfo_open($const);
$mimeType = finfo_file($finfo, $filename);
finfo_close($finfo);
}
// Try with Mime
if (!$mimeType && function_exists('mime_content_type')) {
$mimeType = mime_content_type($filename);
}
// Try with exec command and file binary
if (!$mimeType && function_exists('exec')) {
$mimeType = trim(exec('file -b --mime-type ' . escapeshellarg($filename)));
if (!$mimeType) {
$mimeType = trim(exec('file --mime ' . escapeshellarg($filename)));
}
if (!$mimeType) {
$mimeType = trim(exec('file -bi ' . escapeshellarg($filename)));
}
}

return $mimeType;
}
}

0 comments on commit f53b9e6

Please sign in to comment.