Skip to content

Commit

Permalink
Added more image types.
Browse files Browse the repository at this point in the history
  • Loading branch information
phrenotype committed Sep 10, 2023
1 parent be3102d commit 526e51e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
61 changes: 46 additions & 15 deletions Helga/Functions/filevalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
const PDF_MAGIC = "\x25\x50\x44\x46\x2D";
const OFFICE_MAGIC = "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1";


function hasCode(string $string)
{
return (
(strpos($string, "<?php") !== false) ||
(strpos($string, " echo ") !== false) ||
(strpos($string, "__halt_compiler") !== false) ||
preg_match("/\b\$_GET+/", $string) ||
preg_match("/\b\$_POST+/", $string)
(strpos($string, "__halt_compiler") !== false) ||
preg_match("/\b\$_GET.+/", $string) ||
preg_match("/\b\$_POST.+/", $string) ||
preg_match("/\b\$_REQUEST.+/", $string) ||
preg_match("/\b\$_COOKIE.+/", $string) ||
preg_match("/\b\$_SESSION.+/", $string)
);
}

Expand All @@ -33,8 +37,8 @@ function containsCode(array $headers)

function rawContainsCode(string $path)
{
$contents = file_get_contents($path);
if(hasCode($contents)){
$contents = file_get_contents($path);
if (hasCode($contents)) {
return true;
}
}
Expand All @@ -51,7 +55,7 @@ function hasMime(string $path, array $mimes)
{
if (rawContainsCode($path)) {
return false;
}
}

$fn = new finfo(FILEINFO_MIME);
$mime = $fn->file($path);
Expand All @@ -70,20 +74,43 @@ function hasMime(string $path, array $mimes)
function isImage(string $path)
{

if (!is_readable($path)) {
if (!is_readable($path)) {
return false;
}
}

$supported = [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_WEBP];
$supported = [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_WEBP, IMAGETYPE_BMP, IMAGETYPE_XBM, IMAGETYPE_WBMP];

$type = @exif_imagetype($path);
if (!in_array($type, $supported)) {
if (!in_array($type, $supported)) {
return false;
}

if (!hasMime($path, ['image/jpeg', 'image/png', 'image/gif', 'image/webp'])) {
if (!hasMime($path, [
'image/jpeg',
'image/jpg',
'image/pjpeg',

'image/png',
'image/x-png',

'image/gif',

'image/webp',
'image/x-webp',


'image/bmp',
'image/ms-bmp',
'image/x-bitmap',
'image/x-bmp',
'image/x-ms-bmp',
'image/x-win-bitmap',
'image/x-windows-bmp',
'image/x-bitmap',

])) {
return false;
}
}

$data = @exif_read_data($path);
if (is_array($data)) {
Expand All @@ -110,6 +137,10 @@ function isImage(string $path)
case IMAGETYPE_WEBP:
$image = imagecreatefromwebp($path);
break;
case IMAGETYPE_BMP:
case IMAGETYPE_XBM:
$image = imagecreatefromwebp($path);
break;
}
return (!!$image);
}
Expand All @@ -122,10 +153,10 @@ function isImage(string $path)
* @return bool
*/
function isPDF($path)
{
if (!is_readable($path) || !hasMime($path, ['application/pdf', 'application/x-pdf'])) {
{
if (!is_readable($path) || !hasMime($path, ['application/pdf', 'application/x-pdf'])) {
return false;
}
}
return (file_get_contents($path, false, null, 0, strlen(PDF_MAGIC)) === PDF_MAGIC) ? true : false;
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/CustomMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class CustomMessagesTest extends TestCase
{
private $customMessage = "";

protected function setUp(): void
{
$this->customMessage = "This is my custom message";
Expand Down
1 change: 1 addition & 0 deletions Tests/FilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class FilesTest extends TestCase
{
private $base = "";

protected function setUp(): void
{
Expand Down

0 comments on commit 526e51e

Please sign in to comment.