Skip to content

Commit

Permalink
Merge pull request #117 from paragonie/php74-preload-fix
Browse files Browse the repository at this point in the history
Proposed fix for #116
  • Loading branch information
paragonie-security authored Mar 20, 2020
2 parents 3b95310 + fa4115d commit bbade40
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 31 deletions.
31 changes: 31 additions & 0 deletions autoload-php7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
This file should only ever be loaded on PHP 7+
*/
if (PHP_VERSION_ID < 70000) {
return;
}

spl_autoload_register(function ($class) {
$namespace = 'ParagonIE_Sodium_';
// Does the class use the namespace prefix?
$len = strlen($namespace);
if (strncmp($namespace, $class, $len) !== 0) {
// no, move to the next registered autoloader
return false;
}

// 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 = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require_once $file;
return true;
}
return false;
});
66 changes: 35 additions & 31 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
<?php

if (!is_callable('sodiumCompatAutoloader')) {
/**
* Sodium_Compat autoloader.
*
* @param string $class Class name to be autoloaded.
*
* @return bool Stop autoloading?
*/
function sodiumCompatAutoloader($class)
{
$namespace = 'ParagonIE_Sodium_';
// Does the class use the namespace prefix?
$len = strlen($namespace);
if (strncmp($namespace, $class, $len) !== 0) {
// no, move to the next registered autoloader
return false;
}
if (PHP_VERSION_ID < 70000) {
if (!is_callable('sodiumCompatAutoloader')) {
/**
* Sodium_Compat autoloader.
*
* @param string $class Class name to be autoloaded.
*
* @return bool Stop autoloading?
*/
function sodiumCompatAutoloader($class)
{
$namespace = 'ParagonIE_Sodium_';
// Does the class use the namespace prefix?
$len = strlen($namespace);
if (strncmp($namespace, $class, $len) !== 0) {
// no, move to the next registered autoloader
return false;
}

// Get the relative class name
$relative_class = substr($class, $len);
// 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 = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require_once $file;
return true;
// Replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require_once $file;
return true;
}
return false;
}
return false;
}

// Now that we have an autoloader, let's register it!
spl_autoload_register('sodiumCompatAutoloader');
// Now that we have an autoloader, let's register it!
spl_autoload_register('sodiumCompatAutoloader');
}
} else {
require_once dirname(__FILE__) . '/autoload-php7.php';
}

if (!class_exists('SodiumException', false)) {
Expand Down

0 comments on commit bbade40

Please sign in to comment.