<?php
namespace support\utils;
use support\exception\FileToolkitException;
use support\Exception\InvalidArgumentException;
use support\Exception\UnexpectedValueException;
use Imagine\Image\Box;
use Imagine\Image\Point;
use Symfony\Component\Filesystem\Filesystem;
use support\ServiceKernel;
class FileToolkit
{
public static function mungeFilename($fileName, $extensions)
{
$original = $fileName;
// Remove any null bytes. See http://php.net/manual/en/security.filesystem.nullbytes.php
$fileName = str_replace(chr(0), '', $fileName);
$whitelist = array_unique(explode(' ', trim($extensions)));
// Split the filename up by periods. The first part becomes the basename
// the last part the final extension.
$fileNameParts = explode('.', $fileName);
$newFilename = array_shift($fileNameParts); // Remove file basename.
$finalExtension = array_pop($fileNameParts);
// Remove final extension.
// Loop through the middle parts of the name and add an underscore to the
// end of each section that could be a file extension but isn't in the list
// of allowed extensions.
foreach ($fileNameParts as $fileNamePart) {
$newFilename .= '.' . $fileNamePart;
if (!in_array($fileNamePart, $whitelist) && preg_match("/^[a-zA-Z]{2,5}\d?$/", $fileNamePart)) {
$newFilename .= '_';
}
}
$fileName = $newFilename . '.' . $finalExtension;
return $fileName;
}
public static function generateFilename($ext = '')
{
$filename = date('Yndhis') . '-' . substr(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36), 0, 6);
return $filename . '.' . $ext;
}
public static function getSecureFileMimeTypes()
{
$extensions = self::getSecureFileExtensions();
$extensions = explode(' ', $extensions);
$mimeTypes = [];
foreach ($extensions as $key => $extension) {
$mimeTypes[] = self::getMimeTypeByExtension($extension);
}
return $mimeTypes;
}
public static function getSecureFileExtensions()
{
return 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ods odp mp4 mp3 avi flv wmv wma mov zip rar gz tar 7z swf ico emf';
}
public static function getImageExtensions()
{
return 'bmp jpg jpeg gif png ico';
}
public static function getMimeTypeByExtension($extension)
{
$mimes = [
'ez' => 'application/andrew-inset',
'aw' => 'application/applixware',
'atom' => 'application/atom+xml',
'atomcat' => 'application/atomcat+xml',
'atomsvc' => 'application/atomsvc+xml',
'ccxml' => 'application/ccxml+xml',
'cdmia' => 'application/cdmi-capability',
'cdmic' => 'application/cdmi-container',
'cdmid' => 'application/cdmi-domain',
'cdmio' => 'application/cdmi-object',
'cdmiq' => 'application/cdmi-queue',
'cu' => 'application/cu-seeme',
'davmount' => 'application/davmount+xml',
'dbk' => 'application/docbook+xml',
'dssc' => 'application/dssc+der',
'xdssc' => 'application/dssc+xml',
'ecma' => 'application/ecmascript',
'emma' => 'application/emma+xml',
'epub' => 'application/epub+zip',
'exi' => 'application/exi',
'pfr' => 'application/font-tdpfr',
'gml' => 'application/gml+xml',
'gpx' => 'application/gpx+xml',
'gxf' => 'application/gxf',
'stk' => 'application/hyperstudio',
'ink' => 'application/inkml+xml',
'ipfix' => 'application/ipfix',
'jar' => 'application/java-archive',
'ser' => 'application/java-serialized-object',
'class' => 'application/java-vm',
'js' => 'application/javascript',
'json' => 'application/json',
'jsonml' => 'application/jsonml+json',
'lostxml' => 'application/lost+xml',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'mads' => 'application/mads+xml',
'mrc' => 'application/marc',
'mrcx' => 'application/marcxml+xml',
'ma' => 'application/mathematica',
'mathml' => 'application/mathml+xml',
'mbox' => 'application/mbox',
'mscml' => 'application/mediaservercontrol+xml',
'metalink' => 'application/metalink+xml',
'meta4' => 'application/metalink4+xml',
'mets' => 'application/mets+xml',
'mods' => 'application/mods+xml',
'm21' => 'application/mp21',
'mp4s' => 'application/mp4',
'doc' => 'application/msword',
'mxf' => 'application/mxf',
'bin' => 'application/octet-stream',
'oda' => 'application/oda',
'opf' => 'application/oebps-package+xml',
'ogx' => 'application/ogg',
'omdoc' => 'application/omdoc+xml',
'onetoc' => 'application/onenote',
'oxps' => 'application/oxps',
'xer' => 'application/patch-ops-error+xml',
'pdf' => 'application/pdf',
'pgp' => 'application/pgp-encrypted',
'asc' => 'application/pgp-signature',
'prf' => 'application/pics-rules',
'p10' => 'application/pkcs10',
'p7m' => 'application/pkcs7-mime',
'p7s' => 'application/pkcs7-signature',
'p8' => 'application/pkcs8',
'ac' => 'application/pkix-attr-cert',
'cer' => 'application/pkix-cert',
'crl' => 'application/pkix-crl',
'pkipath' => 'application/pkix-pkipath',
'pki' => 'application/pkixcmp',
'pls' => 'application/pls+xml',
'ai' => 'application/postscript',
'cww' => 'application/prs.cww',
'pskcxml' => 'application/pskc+xml',
'rdf' => 'application/rdf+xml',
'rif' => 'application/reginfo+xml',
'rnc' => 'application/relax-ng-compact-syntax',
'rl' => 'application/resource-lists+xml',
'rld' => 'application/resource-lists-diff+xml',
'rs' => 'application/rls-services+xml',
'gbr' => 'application/rpki-ghostbusters',
'mft' => 'application/rpki-manifest',
'roa' => 'application/rpki-roa',
'rsd' => 'application/rsd+xml',
'rss' => 'application/rss+xml',
'rtf' => 'application/rtf',
'sbml' => 'application/sbml+xml',
'scq' => 'application/scvp-cv-request',
'scs' => 'application/scvp-cv-response',
'spq' => 'application/scvp-vp-request',
'spp' => 'application/scvp-vp-response',
'sdp' => 'application/sdp',
'setpay' => 'application/set-payment-initiation',
'setreg' => 'application/set-registration-initiation',
'shf' => 'application/shf+xml',
'smi' => 'application/smil+xml',
'rq' => 'application/sparql-query',
'srx' => 'application/sparql-results+xml',
'gram' => 'application/srgs',
'grxml' => 'application/srgs+xml',
'sru' => 'application/sru+xml',
'ssdl' => 'application/ssdl+xml',
'ssml' => 'application/ssml+xml',
'tei' => 'application/tei+xml',
'tfi' => 'application/thraud+xml',
'tsd' => 'application/timestamped-data',
'plb' => 'application/vnd.3gpp.pic-bw-large',
'psb' => 'application/vnd.3gpp.pic-bw-small',
'pvb' => 'application/vnd.3gpp.pic-bw-var',
'tcap' => 'application/vnd.3gpp2.tcap',
'pwn' => 'application/vnd.3m.post-it-notes',
'aso' => 'applic