Skip to content

Instantly share code, notes, and snippets.

@volfegan
Created October 10, 2018 03:37
Show Gist options
  • Save volfegan/b797f319a25f8041494ca7bad2d3cfe4 to your computer and use it in GitHub Desktop.
Save volfegan/b797f319a25f8041494ca7bad2d3cfe4 to your computer and use it in GitHub Desktop.
Simple php function to parse an audio or video file and find if it has the right signature (magic number) according to its extension
<?php
/*
* Reads some part of the bytes of an audio|video file and checks if its signature are true|false for that file extension
*
* @param actual $file
* @param string $fileExtension
* @return boolean
**/
function check_AVfile_signature($file, $fileExtension) {
$fileExtension = strtolower($fileExtension);
//https://en.wikipedia.org/wiki/List_of_file_signatures
//https://filesignatures.net
//https://www.garykessler.net/library/file_sigs.html
//signatures encoded in ISO 8859-1 or HEX and written as regex. The signature array may contain different signatureS for the same format e.g.mp3
$videos = array("avi"=>["signature"=>["^(RIFF).*(AVI)"], "offset"=>0, "encode"=>"ISO" ],
"asf"=>["signature"=>["^(3026b2758e66cf11a6d900aa0062ce6c)"], "offset"=>0, "encode"=>"HEX" ],
"flv"=>["signature"=>["^(FLV)"], "offset"=>0, "encode"=>"ISO" ],
"wmv"=>["signature"=>["^(3026b2758e66cf11a6d900aa0062ce6c)"], "offset"=>0, "encode"=>"HEX" ],
"mpeg"=>["signature"=>["^(000001ba)"], "offset"=>0, "encode"=>"HEX" ],
"mpg"=>["signature"=>["^(000001ba)"], "offset"=>0, "encode"=>"HEX" ],
"mp4"=>["signature"=>["(FTYP)"], "offset"=>4, "encode"=>"ISO" ],
"mkv"=>["signature"=>["^(1a45dfa3)"], "offset"=>0, "encode"=>"HEX" ],
"webm"=>["signature"=>["^(1a45dfa3)"], "offset"=>0, "encode"=>"HEX" ]
);
$audios = array("mp3"=>["signature"=>["^(ÿû)", "^(ID3)"], "offset"=>0, "encode"=>"ISO" ],
"wav"=>["signature"=>["^(RIFF).*(WAVE)"], "offset"=>0, "encode"=>"ISO" ],
"ogg"=>["signature"=>["^(OggS)"], "offset"=>0, "encode"=>"ISO" ],
"mid"=>["signature"=>["^(MThd)"], "offset"=>0, "encode"=>"ISO" ],
"wma"=>["signature"=>["^(3026b2758e66cf11a6d900aa0062ce6c)"], "offset"=>0, "encode"=>"HEX" ]
);
$audioVideo = array_merge($audios,$videos);
if ( ! array_key_exists($fileExtension, $audioVideo) ) return false;
foreach ($audioVideo[$fileExtension]["signature"] as $index=>$fileSignature) {
$byte_offset = $audioVideo[$fileExtension]["offset"];
//read 100 bytes after the offset
$byte_lenght = 100;
$filecontent = file_get_contents($file, FALSE, NULL, $byte_offset, $byte_lenght);
if ( $audioVideo[$fileExtension]["encode"] == "ISO" )
//if the web page is in UTF-8, it will convert the file from ISO-8859-1 to UTF-8
$filecontent = mb_convert_encoding($filecontent, "UTF-8", "ISO-8859-1");
if ( $audioVideo[$fileExtension]["encode"] == "HEX" )
$filecontent = bin2hex($filecontent);
if ( preg_match("/$fileSignature/i", $filecontent) ) return true;
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment