]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/random_compat/random.php
WordPress 4.4
[autoinstalls/wordpress.git] / wp-includes / random_compat / random.php
1 <?php
2 /**
3  * Random_* Compatibility Library 
4  * for using the new PHP 7 random_* API in PHP 5 projects
5  * 
6  * The MIT License (MIT)
7  * 
8  * Copyright (c) 2015 Paragon Initiative Enterprises
9  * 
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  */
28
29 if (!defined('PHP_VERSION_ID')) {
30     // This constant was introduced in PHP 5.2.7
31     $RandomCompatversion = explode('.', PHP_VERSION);
32     define('PHP_VERSION_ID', ($RandomCompatversion[0] * 10000 + $RandomCompatversion[1] * 100 + $RandomCompatversion[2]));
33     $RandomCompatversion = null;
34 }
35 if (PHP_VERSION_ID < 70000) {
36     if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
37         define('RANDOM_COMPAT_READ_BUFFER', 8);
38     }
39     $RandomCompatDIR = dirname(__FILE__);
40     require_once $RandomCompatDIR.'/byte_safe_strings.php';
41     require_once $RandomCompatDIR.'/cast_to_int.php';
42     require_once $RandomCompatDIR.'/error_polyfill.php';
43     if (!function_exists('random_bytes')) {
44         /**
45          * PHP 5.2.0 - 5.6.x way to implement random_bytes()
46          * 
47          * We use conditional statements here to define the function in accordance
48          * to the operating environment. It's a micro-optimization.
49          * 
50          * In order of preference:
51          *   1. Use libsodium if available.
52          *   2. fread() /dev/urandom if available (never on Windows)
53          *   3. mcrypt_create_iv($bytes, MCRYPT_CREATE_IV)
54          *   4. COM('CAPICOM.Utilities.1')->GetRandom()
55          *   5. openssl_random_pseudo_bytes() (absolute last resort)
56          * 
57          * See ERRATA.md for our reasoning behind this particular order
58          */
59         if (extension_loaded('libsodium')) {
60             // See random_bytes_libsodium.php
61             require_once $RandomCompatDIR.'/random_bytes_libsodium.php';
62         }
63         if (
64             !function_exists('random_bytes') && 
65             DIRECTORY_SEPARATOR === '/' &&
66             @is_readable('/dev/urandom')
67         ) {
68             // DIRECTORY_SEPARATOR === '/' on Unix-like OSes -- this is a fast
69             // way to exclude Windows.
70             // 
71             // Error suppression on is_readable() in case of an open_basedir or 
72             // safe_mode failure. All we care about is whether or not we can 
73             // read it at this point. If the PHP environment is going to panic 
74             // over trying to see if the file can be read in the first place,
75             // that is not helpful to us here.
76             
77             // See random_bytes_dev_urandom.php
78             require_once $RandomCompatDIR.'/random_bytes_dev_urandom.php';
79         }
80         if (
81             !function_exists('random_bytes') &&
82             PHP_VERSION_ID >= 50307 &&
83             extension_loaded('mcrypt')
84         ) {
85             // See random_bytes_mcrypt.php
86             require_once $RandomCompatDIR.'/random_bytes_mcrypt.php';
87         }
88         if (
89             !function_exists('random_bytes') && 
90             extension_loaded('com_dotnet') &&
91             class_exists('COM')
92         ) {
93             try {
94                 $RandomCompatCOMtest = new COM('CAPICOM.Utilities.1');
95                 if (method_exists($RandomCompatCOMtest, 'GetRandom')) {
96                     // See random_bytes_com_dotnet.php
97                     require_once $RandomCompatDIR.'/random_bytes_com_dotnet.php';
98                 }
99             } catch (com_exception $e) {
100                 // Don't try to use it.
101             }
102             $RandomCompatCOMtest = null;
103         }
104         if (
105             !function_exists('random_bytes') && 
106             extension_loaded('openssl') &&
107             (
108                 // Unix-like with PHP >= 5.3.0 or
109                 (
110                     DIRECTORY_SEPARATOR === '/' &&
111                     PHP_VERSION_ID >= 50300
112                 ) ||
113                 // Windows with PHP >= 5.4.1
114                 PHP_VERSION_ID >= 50401
115             )
116         ) {
117             // See random_bytes_openssl.php
118             require_once $RandomCompatDIR.'/random_bytes_openssl.php';
119         }
120         if (!function_exists('random_bytes')) {
121             /**
122              * We don't have any more options, so let's throw an exception right now
123              * and hope the developer won't let it fail silently.
124              */
125             function random_bytes()
126             {
127                 throw new Exception(
128                     'There is no suitable CSPRNG installed on your system'
129                 );
130             }
131         }
132     }
133     if (!function_exists('random_int')) {
134         require_once $RandomCompatDIR.'/random_int.php';
135     }
136     $RandomCompatDIR = null;
137 }