admidio
admidio copied to clipboard
Check file integretry
Calc hashes of file contents an compare it with an official list of file hashes to check if all files are original.
Only show warnings if one file isn't equal because some people want to change files
Function for FileSystemUtils.php to calculate file hashes
/**
* @param string $filePath
* @param string $algorithm
* @throws \RuntimeException Throws if the given path is not in an allowed directory
* @throws \UnexpectedValueException Throws if the file does not exist or is not readable or hash algorithm is not supported
* @return string
* @see https://secure.php.net/manual/en/function.hash-file.php
*/
public static function getFileHash($filePath, $algorithm = 'sha1')
{
self::checkIsInAllowedDirectories($filePath);
$parentDirectoryPath = dirname($filePath);
if (self::isUnix() && !is_executable($parentDirectoryPath))
{
throw new \UnexpectedValueException('Parent directory "' . $parentDirectoryPath . '" is not executable!');
}
if (!is_file($filePath))
{
throw new \UnexpectedValueException('File "' . $filePath . '" does not exist!');
}
if (!is_readable($filePath))
{
throw new \UnexpectedValueException('File "' . $filePath . '" is not readable!');
}
if (!in_array($algorithm, hash_algos(), true))
{
throw new \UnexpectedValueException('Hash algorithm "' . $algorithm . '" is not supported!');
}
return hash_file($algorithm, $filePath);
}