]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/random_compat/random.php
WordPress 4.4.2-scripts
[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 (PHP_VERSION_ID >= 50300 && extension_loaded('libsodium') && function_exists('\\Sodium\\randombytes_buf')) {
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             $RandomCompat_disabled_classes = preg_split(
94                 '#\s*,\s*#', 
95                 strtolower(ini_get('disable_classes'))
96             );
97             
98             if (!in_array('com', $RandomCompat_disabled_classes)) {
99                 try {
100                     $RandomCompatCOMtest = new COM('CAPICOM.Utilities.1');
101                     if (method_exists($RandomCompatCOMtest, 'GetRandom')) {
102                         // See random_bytes_com_dotnet.php
103                         require_once $RandomCompatDIR.'/random_bytes_com_dotnet.php';
104                     }
105                 } catch (com_exception $e) {
106                     // Don't try to use it.
107                 }
108             }
109             $RandomCompat_disabled_classes = null;
110             $RandomCompatCOMtest = null;
111         }
112         if (
113             !function_exists('random_bytes') && 
114             extension_loaded('openssl') &&
115             (
116                 // Unix-like with PHP >= 5.3.0 or
117                 (
118                     DIRECTORY_SEPARATOR === '/' &&
119                     PHP_VERSION_ID >= 50300
120                 ) ||
121                 // Windows with PHP >= 5.4.1
122                 PHP_VERSION_ID >= 50401
123             )
124         ) {
125             // See random_bytes_openssl.php
126             require_once $RandomCompatDIR.'/random_bytes_openssl.php';
127         }
128         if (!function_exists('random_bytes')) {
129             /**
130              * We don't have any more options, so let's throw an exception right now
131              * and hope the developer won't let it fail silently.
132              */
133             function random_bytes()
134             {
135                 throw new Exception(
136                     'There is no suitable CSPRNG installed on your system'
137                 );
138             }
139         }
140     }
141     if (!function_exists('random_int')) {
142         require_once $RandomCompatDIR.'/random_int.php';
143     }
144     $RandomCompatDIR = null;
145 }