1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-01 20:30:39 +02:00

Minimum viable rewrite of File Inspector frontend

This commit is contained in:
Nick Liu
2020-03-27 00:27:46 -05:00
parent 6f6556178f
commit 6095c94de3
7 changed files with 534 additions and 691 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@@ -193,7 +193,7 @@ class e107
'e_bb_base' => '{e_HANDLER}bbcode_handler.php', 'e_bb_base' => '{e_HANDLER}bbcode_handler.php',
'e_customfields' => '{e_HANDLER}e_customfields_class.php', 'e_customfields' => '{e_HANDLER}e_customfields_class.php',
'e_file' => '{e_HANDLER}file_class.php', 'e_file' => '{e_HANDLER}file_class.php',
'e_file_inspector' => '{e_HANDLER}e_file_inspector_json_phar.php', 'e_file_inspector_json_phar' => '{e_HANDLER}e_file_inspector_json_phar.php',
'e_form' => '{e_HANDLER}form_handler.php', 'e_form' => '{e_HANDLER}form_handler.php',
'e_jshelper' => '{e_HANDLER}js_helper.php', 'e_jshelper' => '{e_HANDLER}js_helper.php',
'e_media' => '{e_HANDLER}media_class.php', 'e_media' => '{e_HANDLER}media_class.php',
@@ -1609,9 +1609,9 @@ class e107
* *
* @return e_file_inspector * @return e_file_inspector
*/ */
public static function getFileInspector() public static function getFileInspector($type = 'core')
{ {
return self::getSingleton('e_file_inspector'); return self::getObject('e_file_inspector_json_phar', e_ADMIN . "core_image.php");
} }
/** /**

View File

@@ -22,6 +22,7 @@ abstract class e_file_inspector implements e_file_inspector_interface
protected $defaultDirsCache; protected $defaultDirsCache;
protected $customDirsCache; protected $customDirsCache;
private $undeterminable = array();
/** /**
* e_file_inspector constructor * e_file_inspector constructor
@@ -32,6 +33,16 @@ abstract class e_file_inspector implements e_file_inspector_interface
{ {
$this->database = $database; $this->database = $database;
$this->loadDatabase(); $this->loadDatabase();
$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",
]
);
} }
/** /**
@@ -165,14 +176,14 @@ abstract class e_file_inspector implements e_file_inspector_interface
return false; return false;
} }
protected function pathToDefaultPath($path) /**
* Convert a custom site path to a default path
* @param string $path Custom path
* @return string
*/
public function customPathToDefaultPath($path)
{ {
if (!$this->customDirsCache) if (!is_array($this->customDirsCache)) $this->populateDirsCache();
{
$this->defaultDirsCache = e107::getInstance()->defaultDirs();
$customDirs = e107::getInstance()->e107_dirs ? e107::getInstance()->e107_dirs : [];
$this->customDirsCache = array_diff_assoc($customDirs, $this->defaultDirsCache);
}
foreach ($this->customDirsCache as $dirType => $customDir) foreach ($this->customDirsCache as $dirType => $customDir)
{ {
if (!isset($this->defaultDirsCache[$dirType])) continue; if (!isset($this->defaultDirsCache[$dirType])) continue;
@@ -186,6 +197,22 @@ abstract class e_file_inspector implements e_file_inspector_interface
return $path; return $path;
} }
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;
}
private function getValidatedBitmask() private function getValidatedBitmask()
{ {
if ($this->validatedBitmask !== null) return $this->validatedBitmask; if ($this->validatedBitmask !== null) return $this->validatedBitmask;
@@ -206,6 +233,13 @@ abstract class e_file_inspector implements e_file_inspector_interface
*/ */
private function isDeterminable($absolutePath) private function isDeterminable($absolutePath)
{ {
return is_file($absolutePath) && is_readable($absolutePath); 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);
} }
} }

View File

@@ -56,7 +56,7 @@ class e_file_inspector_json extends e_file_inspector
*/ */
public function getChecksums($path) public function getChecksums($path)
{ {
$path = $this->pathToDefaultPath($path); $path = $this->customPathToDefaultPath($path);
return isset($this->coreImage[$path]) ? $this->coreImage[$path] : []; return isset($this->coreImage[$path]) ? $this->coreImage[$path] : [];
} }

View File

@@ -62,7 +62,7 @@ class e_file_inspector_sqlphar extends e_file_inspector
*/ */
public function getChecksums($path) public function getChecksums($path)
{ {
$path = $this->pathToDefaultPath($path); $path = $this->customPathToDefaultPath($path);
$statement = $this->coreImage->prepare(" $statement = $this->coreImage->prepare("
SELECT versions.version_string, file_hashes.hash SELECT versions.version_string, file_hashes.hash
FROM file_hashes FROM file_hashes

View File

@@ -64,17 +64,12 @@ class e_file_inspectorTest extends \Codeception\Test\Unit
$this->assertEquals(0, $result & e_file_inspector::VALIDATED_PRESENCE); $this->assertEquals(0, $result & e_file_inspector::VALIDATED_PRESENCE);
} }
/** public function testCustomPathToDefaultPath()
* TODO: Create a stable interface for pathToDefaultPath()
* @throws ReflectionException
*/
public function testPathToDefaultPath()
{ {
/** @var e_file_inspector $object */
$object = $this->make('e_file_inspector'); $object = $this->make('e_file_inspector');
$class = new ReflectionClass(get_class($object)); $class = new ReflectionClass(get_class($object));
$method = $class->getMethod('pathToDefaultPath'); $object->customPathToDefaultPath('populate_cache');
$method->setAccessible(true);
$method->invoke($object, 'populate_cache');
$member = $class->getProperty('customDirsCache'); $member = $class->getProperty('customDirsCache');
$member->setAccessible(true); $member->setAccessible(true);
$customDirs = $member->getValue($object); $customDirs = $member->getValue($object);
@@ -83,7 +78,26 @@ class e_file_inspectorTest extends \Codeception\Test\Unit
$input = "e963_admin/index.php"; $input = "e963_admin/index.php";
$expected = "e107_admin/index.php"; $expected = "e107_admin/index.php";
$actual = $method->invoke($object, $input); $actual = $object->customPathToDefaultPath($input);
$this->assertEquals($expected, $actual);
}
public function testDefaultPathToCustomPath()
{
/** @var e_file_inspector $object */
$object = $this->make('e_file_inspector');
$class = new ReflectionClass(get_class($object));
$object->customPathToDefaultPath('populate_cache');
$member = $class->getProperty('customDirsCache');
$member->setAccessible(true);
$customDirs = $member->getValue($object);
$customDirs['ADMIN_DIRECTORY'] = 'e963_admin/';
$member->setValue($object, $customDirs);
$input = "e107_admin/index.php";
$expected = "e963_admin/index.php";
$actual = $object->defaultPathToCustomPath($input);
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }