1 <?php |
1 <?php |
2 |
2 |
3 if (!is_callable('sodiumCompatAutoloader')) { |
3 if (PHP_VERSION_ID < 70000) { |
4 /** |
4 if (!is_callable('sodiumCompatAutoloader')) { |
5 * Sodium_Compat autoloader. |
5 /** |
6 * |
6 * Sodium_Compat autoloader. |
7 * @param string $class Class name to be autoloaded. |
7 * |
8 * |
8 * @param string $class Class name to be autoloaded. |
9 * @return bool Stop autoloading? |
9 * |
10 */ |
10 * @return bool Stop autoloading? |
11 function sodiumCompatAutoloader($class) |
11 */ |
12 { |
12 function sodiumCompatAutoloader($class) |
13 $namespace = 'ParagonIE_Sodium_'; |
13 { |
14 // Does the class use the namespace prefix? |
14 $namespace = 'ParagonIE_Sodium_'; |
15 $len = strlen($namespace); |
15 // Does the class use the namespace prefix? |
16 if (strncmp($namespace, $class, $len) !== 0) { |
16 $len = strlen($namespace); |
17 // no, move to the next registered autoloader |
17 if (strncmp($namespace, $class, $len) !== 0) { |
|
18 // no, move to the next registered autoloader |
|
19 return false; |
|
20 } |
|
21 |
|
22 // Get the relative class name |
|
23 $relative_class = substr($class, $len); |
|
24 |
|
25 // Replace the namespace prefix with the base directory, replace namespace |
|
26 // separators with directory separators in the relative class name, append |
|
27 // with .php |
|
28 $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php'; |
|
29 // if the file exists, require it |
|
30 if (file_exists($file)) { |
|
31 require_once $file; |
|
32 return true; |
|
33 } |
18 return false; |
34 return false; |
19 } |
35 } |
20 |
36 |
21 // Get the relative class name |
37 // Now that we have an autoloader, let's register it! |
22 $relative_class = substr($class, $len); |
38 spl_autoload_register('sodiumCompatAutoloader'); |
23 |
|
24 // Replace the namespace prefix with the base directory, replace namespace |
|
25 // separators with directory separators in the relative class name, append |
|
26 // with .php |
|
27 $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php'; |
|
28 // if the file exists, require it |
|
29 if (file_exists($file)) { |
|
30 require_once $file; |
|
31 return true; |
|
32 } |
|
33 return false; |
|
34 } |
39 } |
35 |
40 } else { |
36 // Now that we have an autoloader, let's register it! |
41 require_once dirname(__FILE__) . '/autoload-php7.php'; |
37 spl_autoload_register('sodiumCompatAutoloader'); |
|
38 } |
42 } |
39 |
43 |
40 require_once dirname(__FILE__) . '/src/SodiumException.php'; |
44 /* Explicitly, always load the Compat class: */ |
|
45 require_once dirname(__FILE__) . '/src/Compat.php'; |
|
46 |
|
47 if (!class_exists('SodiumException', false)) { |
|
48 require_once dirname(__FILE__) . '/src/SodiumException.php'; |
|
49 } |
41 if (PHP_VERSION_ID >= 50300) { |
50 if (PHP_VERSION_ID >= 50300) { |
42 // Namespaces didn't exist before 5.3.0, so don't even try to use this |
51 // Namespaces didn't exist before 5.3.0, so don't even try to use this |
43 // unless PHP >= 5.3.0 |
52 // unless PHP >= 5.3.0 |
44 require_once dirname(__FILE__) . '/lib/namespaced.php'; |
53 require_once dirname(__FILE__) . '/lib/namespaced.php'; |
45 require_once dirname(__FILE__) . '/lib/sodium_compat.php'; |
54 require_once dirname(__FILE__) . '/lib/sodium_compat.php'; |
53 if (PHP_VERSION_ID >= 70000) { |
62 if (PHP_VERSION_ID >= 70000) { |
54 assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?'); |
63 assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?'); |
55 } else { |
64 } else { |
56 assert(class_exists('ParagonIE_Sodium_Compat')); |
65 assert(class_exists('ParagonIE_Sodium_Compat')); |
57 } |
66 } |
58 require_once (dirname(__FILE__) . '/lib/php72compat.php'); |
67 require_once(dirname(__FILE__) . '/lib/php72compat.php'); |
|
68 } elseif (!function_exists('sodium_crypto_stream_xchacha20_xor')) { |
|
69 // Older versions of {PHP, ext/sodium} will not define these |
|
70 require_once(dirname(__FILE__) . '/lib/php72compat.php'); |
59 } |
71 } |
|
72 require_once(dirname(__FILE__) . '/lib/ristretto255.php'); |