Skip to content

Commit

Permalink
Clean SVG and adjust file size
Browse files Browse the repository at this point in the history
  • Loading branch information
remdex committed Dec 7, 2021
1 parent 9a7b159 commit 0ce1dd2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lhc_web/lib/core/lhcore/lhfileupload.php
Expand Up @@ -71,10 +71,15 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro

$matches = array();
if (strpos($name, '.') === false && preg_match('/^image\/(gif|jpe?g|png)/', $fileUpload->type, $matches)) {
$fileUpload->extension = $matches[1];
$fileUpload->extension = strtolower($matches[1]);
} else {
$partsFile = explode('.', $fileUpload->upload_name);
$fileUpload->extension = end($partsFile);
$fileUpload->extension = strtolower(end($partsFile));
}

if ($fileUpload->extension == 'svg') {
erLhcoreClassFileUploadAdmin::cleanSVG($fileUpload->file_path_server);
$file->size = $fileUpload->size = filesize($fileUpload->file_path_server);
}

if (isset($this->options['remove_meta']) && $this->options['remove_meta'] == true && in_array($fileUpload->extension, array('jpg', 'jpeg', 'png', 'gif'))) {
Expand Down
17 changes: 16 additions & 1 deletion lhc_web/lib/core/lhcore/lhfileuploadadmin.php
Expand Up @@ -73,11 +73,16 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
$fileUpload->extension = strtolower(end($partsFile));
}

if ($fileUpload->extension == 'svg') {
self::cleanSVG($fileUpload->file_path_server);
$file->size = $fileUpload->size = filesize($fileUpload->file_path_server);
}

if (isset($this->options['remove_meta']) && $this->options['remove_meta'] == true && in_array($fileUpload->extension, array('jpg', 'jpeg', 'png', 'gif'))) {
self::removeExif($fileUpload->file_path_server, $fileUpload->file_path_server . '_exif');
unlink($fileUpload->file_path_server);
rename($fileUpload->file_path_server . '_exif', $fileUpload->file_path_server);
$fileUpload->size = filesize($fileUpload->file_path_server);
$file->size = $fileUpload->size = filesize($fileUpload->file_path_server);
}

$fileUpload->saveThis();
Expand All @@ -89,6 +94,16 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro

return $file;
}

public static function cleanSVG($path)
{
$sanitizer = new \enshrined\svgSanitize\Sanitizer();
$dirtySVG = file_get_contents($path);
$cleanSVG = $sanitizer->sanitize($dirtySVG);
file_put_contents($path.'_svg', $cleanSVG);
unlink($path);
rename($path.'_svg', $path);
}
}

?>

0 comments on commit 0ce1dd2

Please sign in to comment.