2020-03-15 14:57:43 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* e107 website system
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008-2020 e107 Inc (e107.org)
|
|
|
|
* Released under the terms and conditions of the
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once("e_file_inspector_interface.php");
|
|
|
|
|
2020-03-21 19:27:59 -05:00
|
|
|
/**
|
|
|
|
* File Inspector
|
|
|
|
*
|
|
|
|
* Tool to validate application files for consistency by comparing hashes of files with those in a database
|
|
|
|
*/
|
2020-03-15 14:57:43 -05:00
|
|
|
abstract class e_file_inspector implements e_file_inspector_interface
|
|
|
|
{
|
2020-03-23 22:40:28 -05:00
|
|
|
protected $database;
|
2020-03-23 17:04:44 -05:00
|
|
|
protected $currentVersion;
|
2020-03-22 19:54:13 -05:00
|
|
|
private $validatedBitmask;
|
|
|
|
|
2020-03-23 17:04:44 -05:00
|
|
|
protected $defaultDirsCache;
|
|
|
|
protected $customDirsCache;
|
2020-03-27 00:27:46 -05:00
|
|
|
private $undeterminable = array();
|
2020-03-23 17:04:44 -05:00
|
|
|
|
2020-03-23 22:40:28 -05:00
|
|
|
/**
|
|
|
|
* e_file_inspector constructor
|
|
|
|
* @param string $database The database from which integrity data may be read or to which integrity data may be
|
|
|
|
* written. This should be an URL or absolute file path for most implementations.
|
|
|
|
*/
|
|
|
|
public function __construct($database)
|
|
|
|
{
|
|
|
|
$this->database = $database;
|
|
|
|
$this->loadDatabase();
|
2020-03-27 00:27:46 -05:00
|
|
|
|
|
|
|
$appRoot = e107::getInstance()->file_path;
|
|
|
|
$this->undeterminable = array_map(function ($path)
|
|
|
|
{
|
|
|
|
return realpath($path) ? realpath($path) : $path;
|
|
|
|
}, [
|
|
|
|
$appRoot . "e107_config.php",
|
|
|
|
$appRoot . e107::getFolder('admin') . "core_image.php",
|
|
|
|
]
|
|
|
|
);
|
2020-03-23 22:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare the provided database for reading or writing
|
|
|
|
*
|
|
|
|
* Should tolerate a non-existent database and try to create it if a write operation is executed.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
abstract public function loadDatabase();
|
|
|
|
|
2020-03-22 19:54:13 -05:00
|
|
|
/**
|
|
|
|
* Check the integrity of the provided path
|
|
|
|
*
|
|
|
|
* @param $path string Relative path of the file to look up
|
|
|
|
* @param $version string The desired software release to match.
|
|
|
|
* Leave blank for the current version.
|
|
|
|
* Do not prepend the version number with "v".
|
|
|
|
* @return int Validation code (see the constants of this class)
|
|
|
|
*/
|
|
|
|
public function validate($path, $version = null)
|
|
|
|
{
|
|
|
|
if ($version === null) $version = $this->getCurrentVersion();
|
|
|
|
|
|
|
|
$bits = 0x0;
|
|
|
|
$absolutePath = realpath(e_BASE . $path);
|
2020-03-27 01:18:34 -05:00
|
|
|
$dbChecksums = $this->getChecksums($path);
|
2020-03-22 19:54:13 -05:00
|
|
|
$dbChecksum = $this->getChecksum($path, $version);
|
2020-03-27 16:00:39 -05:00
|
|
|
$actualChecksum = !empty($dbChecksums) ? $this->checksumPath($absolutePath) : null;
|
2020-03-22 19:54:13 -05:00
|
|
|
|
2020-03-27 01:18:34 -05:00
|
|
|
if (!empty($dbChecksums)) $bits |= self::VALIDATED_PATH_KNOWN;
|
|
|
|
if ($dbChecksum !== false) $bits |= self::VALIDATED_PATH_VERSION;
|
|
|
|
if (file_exists($absolutePath)) $bits |= self::VALIDATED_FILE_EXISTS;
|
|
|
|
if (!$this->isInsecure($path)) $bits |= self::VALIDATED_FILE_SECURITY;
|
|
|
|
if ($this->isDeterminable($absolutePath)) $bits |= self::VALIDATED_HASH_CALCULABLE;
|
|
|
|
if ($actualChecksum === $dbChecksum) $bits |= self::VALIDATED_HASH_CURRENT;
|
2020-03-22 19:54:13 -05:00
|
|
|
|
2020-03-27 01:18:34 -05:00
|
|
|
foreach ($dbChecksums as $dbVersion => $dbChecksum)
|
2020-03-22 19:54:13 -05:00
|
|
|
{
|
2020-03-27 01:18:34 -05:00
|
|
|
if ($dbChecksum === $actualChecksum) $bits |= self::VALIDATED_HASH_EXISTS;
|
2020-03-22 19:54:13 -05:00
|
|
|
}
|
|
|
|
|
2020-03-23 14:29:26 -05:00
|
|
|
if ($bits + self::VALIDATED === $this->getValidatedBitmask()) $bits |= self::VALIDATED;
|
2020-03-22 19:54:13 -05:00
|
|
|
|
|
|
|
return $bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file integrity hash for the provided path and version
|
|
|
|
*
|
|
|
|
* @param $path string Relative path of the file to look up
|
|
|
|
* @param $version string The software release version corresponding to the file hash.
|
|
|
|
* Leave blank for the current version.
|
|
|
|
* Do not prepend the version number with "v".
|
|
|
|
* @return string|bool The database hash for the path and version specified. FALSE if the record does not exist.
|
|
|
|
*/
|
|
|
|
public function getChecksum($path, $version = null)
|
|
|
|
{
|
2020-03-21 19:27:59 -05:00
|
|
|
if ($version === null) $version = $this->getCurrentVersion();
|
2020-03-22 19:54:13 -05:00
|
|
|
$checksums = $this->getChecksums($path);
|
|
|
|
return isset($checksums[$version]) ? $checksums[$version] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the hash of a path to compare with the hash database
|
|
|
|
*
|
|
|
|
* @param $absolutePath string Absolute path of the file to hash
|
|
|
|
* @return string|bool The actual hash for the path. FALSE if the hash was incalculable.
|
|
|
|
*/
|
|
|
|
public function checksumPath($absolutePath)
|
|
|
|
{
|
2020-03-23 14:29:26 -05:00
|
|
|
if (!$this->isDeterminable($absolutePath)) return false;
|
2020-03-22 19:54:13 -05:00
|
|
|
|
|
|
|
return $this->checksum(file_get_contents($absolutePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the hash of a string, which would be used to compare with the hash database
|
|
|
|
*
|
|
|
|
* @param $content string Full content to hash
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function checksum($content)
|
|
|
|
{
|
|
|
|
return md5(str_replace(array(chr(13), chr(10)), "", $content));
|
|
|
|
}
|
2020-03-15 14:57:43 -05:00
|
|
|
|
2020-03-21 19:27:59 -05:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getVersions($path)
|
|
|
|
{
|
|
|
|
return array_keys($this->getChecksums($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getCurrentVersion()
|
|
|
|
{
|
2020-03-23 17:04:44 -05:00
|
|
|
if ($this->currentVersion) return $this->currentVersion;
|
|
|
|
|
2020-03-21 19:27:59 -05:00
|
|
|
$checksums = $this->getChecksums("index.php");
|
|
|
|
$versions = array_keys($checksums);
|
|
|
|
usort($versions, 'version_compare');
|
2020-03-23 17:04:44 -05:00
|
|
|
return $this->currentVersion = array_pop($versions);
|
2020-03-21 19:27:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-22 19:54:13 -05:00
|
|
|
* Get the matching version of the provided path
|
|
|
|
*
|
|
|
|
* Useful for looking up the versions of old files that no longer exist in the latest image
|
|
|
|
*
|
|
|
|
* @param $path string Relative path of the file to look up
|
|
|
|
* @return string|bool PHP-standardized version of the file. FALSE if there is no match.
|
|
|
|
*/
|
|
|
|
public function getVersion($path)
|
|
|
|
{
|
|
|
|
$actualChecksum = $this->checksumPath($path);
|
|
|
|
foreach ($this->getChecksums($path) as $dbVersion => $dbChecksum)
|
|
|
|
{
|
|
|
|
if ($actualChecksum === $dbChecksum) return $dbVersion;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-21 19:27:59 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isInsecure($path)
|
|
|
|
{
|
|
|
|
# TODO
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-22 19:54:13 -05:00
|
|
|
|
2020-03-27 00:27:46 -05:00
|
|
|
/**
|
|
|
|
* Convert a custom site path to a default path
|
|
|
|
* @param string $path Custom path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function customPathToDefaultPath($path)
|
2020-03-23 17:04:44 -05:00
|
|
|
{
|
2020-03-27 00:27:46 -05:00
|
|
|
if (!is_array($this->customDirsCache)) $this->populateDirsCache();
|
2020-03-23 17:04:44 -05:00
|
|
|
foreach ($this->customDirsCache as $dirType => $customDir)
|
|
|
|
{
|
|
|
|
if (!isset($this->defaultDirsCache[$dirType])) continue;
|
|
|
|
|
|
|
|
$defaultDir = $this->defaultDirsCache[$dirType];
|
|
|
|
if ($customDir === $defaultDir) continue;
|
|
|
|
|
|
|
|
if (substr($path, 0, strlen($customDir)) === $customDir)
|
|
|
|
$path = $defaultDir . substr($path, strlen($customDir));
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2020-03-27 00:27:46 -05:00
|
|
|
public function defaultPathToCustomPath($path)
|
|
|
|
{
|
|
|
|
if (!is_array($this->customDirsCache)) $this->populateDirsCache();
|
|
|
|
foreach ($this->customDirsCache as $dirType => $customDir)
|
|
|
|
{
|
|
|
|
if (!isset($this->defaultDirsCache[$dirType])) continue;
|
|
|
|
|
|
|
|
$defaultDir = $this->defaultDirsCache[$dirType];
|
|
|
|
if ($customDir === $defaultDir) continue;
|
|
|
|
|
|
|
|
if (substr($path, 0, strlen($defaultDir)) === $defaultDir)
|
|
|
|
$path = $customDir . substr($path, strlen($defaultDir));
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2020-03-22 19:54:13 -05:00
|
|
|
private function getValidatedBitmask()
|
|
|
|
{
|
|
|
|
if ($this->validatedBitmask !== null) return $this->validatedBitmask;
|
|
|
|
$constants = (new ReflectionClass(self::class))->getConstants();
|
|
|
|
$validated_constants = array_filter($constants, function ($key)
|
|
|
|
{
|
|
|
|
$str = 'VALIDATED_';
|
|
|
|
return substr($key, 0, strlen($str)) === $str;
|
|
|
|
}, ARRAY_FILTER_USE_KEY);
|
|
|
|
|
|
|
|
$this->validatedBitmask = (max($validated_constants) << 0x1) - 0x1;
|
|
|
|
return $this->validatedBitmask;
|
|
|
|
}
|
2020-03-23 14:29:26 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $absolutePath
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isDeterminable($absolutePath)
|
|
|
|
{
|
2020-03-27 00:27:46 -05:00
|
|
|
return is_file($absolutePath) && is_readable($absolutePath) && !in_array($absolutePath, $this->undeterminable);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function populateDirsCache()
|
|
|
|
{
|
|
|
|
$this->defaultDirsCache = e107::getInstance()->defaultDirs();
|
|
|
|
$customDirs = e107::getInstance()->e107_dirs ? e107::getInstance()->e107_dirs : [];
|
|
|
|
$this->customDirsCache = array_diff_assoc($customDirs, $this->defaultDirsCache);
|
2020-03-23 14:29:26 -05:00
|
|
|
}
|
2020-03-15 14:57:43 -05:00
|
|
|
}
|