Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
\spl_autoload_register(function ($class) {
// Project-specific namespace prefix
$prefix = 'Defuse\\Crypto';
$prefix = 'Defuse\\Crypto\\';

// Base directory for the namespace prefix
$base_dir = __DIR__.'/src/';
Expand All @@ -18,19 +18,46 @@

// Get the relative class name
$relative_class = \substr($class, $len);

// Replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir.
\str_replace(
['\\', '_'],
'/',
$relative_class
).'.php';

// If the file exists, require it
if (\file_exists($file)) {
require $file;

/**
* unserialize() -> autoloader -> LFI hardening
*/
$classmap = array(
'Config' =>
'Config.php',
'Core' =>
'Core.php',
'Crypto' =>
'Crypto.php',
'Encoding' =>
'Encoding.php',
'ExceptionHandler' =>
'ExceptionHandler.php',
'File' =>
'File.php',
'FileConfig' =>
'FileConfig.php',
'Key' =>
'Key.php',
'KeyConfig' =>
'KeyConfig.php',
'RuntimeTests' =>
'RuntimeTests.php',
'StreamInterface' =>
'StreamInterface.php',
// Exceptions:
'Exception\\CannotPerformOperationException' =>
'Exception/CannotPerformOperationException.php',
'Exception\\CryptoException' =>
'Exception/CryptoException.php',
'Exception\\CryptoTestFailedException' =>
'Exception/CryptoTestFailedException.php',
'Exception\\InvalidCiphertextException' =>
'Exception/InvalidCiphertextException.php',
);
foreach ($classmap as $classname => $file) {
if ($classname === $relative_class) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isset($classmap[$relative_class]) seems to do the work just as well or am I missing something?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it really doesn't need to be a loop. But I'm going to accept it and open a ticket to fix it later.

require $base_dir.$file;
}
}
});