123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527 |
- <?php
-
- $web = 'index.php';
-
- if (in_array('phar', stream_get_wrappers()) && class_exists('Phar', 0)) {
- Phar::interceptFileFuncs();
- set_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());
- Phar::webPhar(null, $web);
- include 'phar://' . __FILE__ . '/' . Extract_Phar::START;
- return;
- }
-
- if (@(isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST'))) {
- Extract_Phar::go(true);
- $mimes = array(
- 'phps' => 2,
- 'c' => 'text/plain',
- 'cc' => 'text/plain',
- 'cpp' => 'text/plain',
- 'c++' => 'text/plain',
- 'dtd' => 'text/plain',
- 'h' => 'text/plain',
- 'log' => 'text/plain',
- 'rng' => 'text/plain',
- 'txt' => 'text/plain',
- 'xsd' => 'text/plain',
- 'php' => 1,
- 'inc' => 1,
- 'avi' => 'video/avi',
- 'bmp' => 'image/bmp',
- 'css' => 'text/css',
- 'gif' => 'image/gif',
- 'htm' => 'text/html',
- 'html' => 'text/html',
- 'htmls' => 'text/html',
- 'ico' => 'image/x-ico',
- 'jpe' => 'image/jpeg',
- 'jpg' => 'image/jpeg',
- 'jpeg' => 'image/jpeg',
- 'js' => 'application/x-javascript',
- 'midi' => 'audio/midi',
- 'mid' => 'audio/midi',
- 'mod' => 'audio/mod',
- 'mov' => 'movie/quicktime',
- 'mp3' => 'audio/mp3',
- 'mpg' => 'video/mpeg',
- 'mpeg' => 'video/mpeg',
- 'pdf' => 'application/pdf',
- 'png' => 'image/png',
- 'swf' => 'application/shockwave-flash',
- 'tif' => 'image/tiff',
- 'tiff' => 'image/tiff',
- 'wav' => 'audio/wav',
- 'xbm' => 'image/xbm',
- 'xml' => 'text/xml',
- );
-
- header("Cache-Control: no-cache, must-revalidate");
- header("Pragma: no-cache");
-
- $basename = basename(__FILE__);
- if (!strpos($_SERVER['REQUEST_URI'], $basename)) {
- chdir(Extract_Phar::$temp);
- include $web;
- return;
- }
- $pt = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $basename) + strlen($basename));
- if (!$pt || $pt == '/') {
- $pt = $web;
- header('HTTP/1.1 301 Moved Permanently');
- header('Location: ' . $_SERVER['REQUEST_URI'] . '/' . $pt);
- exit;
- }
- $a = realpath(Extract_Phar::$temp . DIRECTORY_SEPARATOR . $pt);
- if (!$a || strlen(dirname($a)) < strlen(Extract_Phar::$temp)) {
- header('HTTP/1.0 404 Not Found');
- echo "<html>\n <head>\n <title>File Not Found<title>\n </head>\n <body>\n <h1>404 - File Not Found</h1>\n </body>\n</html>";
- exit;
- }
- $b = pathinfo($a);
- if (!isset($b['extension'])) {
- header('Content-Type: text/plain');
- header('Content-Length: ' . filesize($a));
- readfile($a);
- exit;
- }
- if (isset($mimes[$b['extension']])) {
- if ($mimes[$b['extension']] === 1) {
- include $a;
- exit;
- }
- if ($mimes[$b['extension']] === 2) {
- highlight_file($a);
- exit;
- }
- header('Content-Type: ' .$mimes[$b['extension']]);
- header('Content-Length: ' . filesize($a));
- readfile($a);
- exit;
- }
- }
-
- class Extract_Phar
- {
- static $temp;
- static $origdir;
- const GZ = 0x1000;
- const BZ2 = 0x2000;
- const MASK = 0x3000;
- const START = 'index.php';
- const LEN = 6643;
-
- static function go($return = false)
- {
- $fp = fopen(__FILE__, 'rb');
- fseek($fp, self::LEN);
- $L = unpack('V', $a = fread($fp, 4));
- $m = '';
-
- do {
- $read = 8192;
- if ($L[1] - strlen($m) < 8192) {
- $read = $L[1] - strlen($m);
- }
- $last = fread($fp, $read);
- $m .= $last;
- } while (strlen($last) && strlen($m) < $L[1]);
-
- if (strlen($m) < $L[1]) {
- die('ERROR: manifest length read was "' .
- strlen($m) .'" should be "' .
- $L[1] . '"');
- }
-
- $info = self::_unpack($m);
- $f = $info['c'];
-
- if ($f & self::GZ) {
- if (!function_exists('gzinflate')) {
- die('Error: zlib extension is not enabled -' .
- ' gzinflate() function needed for zlib-compressed .phars');
- }
- }
-
- if ($f & self::BZ2) {
- if (!function_exists('bzdecompress')) {
- die('Error: bzip2 extension is not enabled -' .
- ' bzdecompress() function needed for bz2-compressed .phars');
- }
- }
-
- $temp = self::tmpdir();
-
- if (!$temp || !is_writable($temp)) {
- $sessionpath = session_save_path();
- if (strpos ($sessionpath, ";") !== false)
- $sessionpath = substr ($sessionpath, strpos ($sessionpath, ";")+1);
- if (!file_exists($sessionpath) || !is_dir($sessionpath)) {
- die('Could not locate temporary directory to extract phar');
- }
- $temp = $sessionpath;
- }
-
- $temp .= '/pharextract/'.basename(__FILE__, '.phar');
- self::$temp = $temp;
- self::$origdir = getcwd();
- @mkdir($temp, 0777, true);
- $temp = realpath($temp);
-
- if (!file_exists($temp . DIRECTORY_SEPARATOR . md5_file(__FILE__))) {
- self::_removeTmpFiles($temp, getcwd());
- @mkdir($temp, 0777, true);
- @file_put_contents($temp . '/' . md5_file(__FILE__), '');
-
- foreach ($info['m'] as $path => $file) {
- $a = !file_exists(dirname($temp . '/' . $path));
- @mkdir(dirname($temp . '/' . $path), 0777, true);
- clearstatcache();
-
- if ($path[strlen($path) - 1] == '/') {
- @mkdir($temp . '/' . $path, 0777);
- } else {
- file_put_contents($temp . '/' . $path, self::extractFile($path, $file, $fp));
- @chmod($temp . '/' . $path, 0666);
- }
- }
- }
-
- chdir($temp);
-
- if (!$return) {
- include self::START;
- }
- }
-
- static function tmpdir()
- {
- if (strpos(PHP_OS, 'WIN') !== false) {
- if ($var = getenv('TMP') ? getenv('TMP') : getenv('TEMP')) {
- return $var;
- }
- if (is_dir('/temp') || mkdir('/temp')) {
- return realpath('/temp');
- }
- return false;
- }
- if ($var = getenv('TMPDIR')) {
- return $var;
- }
- return realpath('/tmp');
- }
-
- static function _unpack($m)
- {
- $info = unpack('V', substr($m, 0, 4));
- $l = unpack('V', substr($m, 10, 4));
- $m = substr($m, 14 + $l[1]);
- $s = unpack('V', substr($m, 0, 4));
- $o = 0;
- $start = 4 + $s[1];
- $ret['c'] = 0;
-
- for ($i = 0; $i < $info[1]; $i++) {
- $len = unpack('V', substr($m, $start, 4));
- $start += 4;
- $savepath = substr($m, $start, $len[1]);
- $start += $len[1];
- $ret['m'][$savepath] = array_values(unpack('Va/Vb/Vc/Vd/Ve/Vf', substr($m, $start, 24)));
- $ret['m'][$savepath][3] = sprintf('%u', $ret['m'][$savepath][3]
- & 0xffffffff);
- $ret['m'][$savepath][7] = $o;
- $o += $ret['m'][$savepath][2];
- $start += 24 + $ret['m'][$savepath][5];
- $ret['c'] |= $ret['m'][$savepath][4] & self::MASK;
- }
- return $ret;
- }
-
- static function extractFile($path, $entry, $fp)
- {
- $data = '';
- $c = $entry[2];
-
- while ($c) {
- if ($c < 8192) {
- $data .= @fread($fp, $c);
- $c = 0;
- } else {
- $c -= 8192;
- $data .= @fread($fp, 8192);
- }
- }
-
- if ($entry[4] & self::GZ) {
- $data = gzinflate($data);
- } elseif ($entry[4] & self::BZ2) {
- $data = bzdecompress($data);
- }
-
- if (strlen($data) != $entry[0]) {
- die("Invalid internal .phar file (size error " . strlen($data) . " != " .
- $stat[7] . ")");
- }
-
- if ($entry[3] != sprintf("%u", crc32($data) & 0xffffffff)) {
- die("Invalid internal .phar file (checksum error)");
- }
-
- return $data;
- }
-
- static function _removeTmpFiles($temp, $origdir)
- {
- chdir($temp);
-
- foreach (glob('*') as $f) {
- if (file_exists($f)) {
- is_dir($f) ? @rmdir($f) : @unlink($f);
- if (file_exists($f) && is_dir($f)) {
- self::_removeTmpFiles($f, getcwd());
- }
- }
- }
-
- @rmdir($temp);
- clearstatcache();
- chdir($origdir);
- }
- }
-
- Extract_Phar::go();
- __HALT_COMPILER(); ?>��
- ����������random_compat.phar�������byte_safe_strings.phpU��´BÅZU��
- ãƶ���������cast_to_int.phpF
- ��´BÅZF
- ��:)û%¶���������error_polyfill.php¡��´BÅZ¡��"]¶������ ���index.phpM��´BÅZM��t:•Ä¶���������random_bytes_com_dotnet.phpW��´BÅZW��¡@¶���������random_bytes_dev_urandom.php@��´BÅZ@��Ù*‘ã¶���������random_bytes_libsodium.php��´BÅZ��Ñ;[c¶������!���random_bytes_libsodium_legacy.phpN��´BÅZN��ƒ^L¶���������random_bytes_mcrypt.phpÙ ��´BÅZÙ ��¯BS7¶���������random_int.phpv��´BÅZv��ôÍâ¶������<?php
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- if (!is_callable('RandomCompat_strlen')) {
- if (
- defined('MB_OVERLOAD_STRING') &&
- ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
- ) {
- /**
- * strlen() implementation that isn't brittle to mbstring.func_overload
- *
- * This version uses mb_strlen() in '8bit' mode to treat strings as raw
- * binary rather than UTF-8, ISO-8859-1, etc
- *
- * @param string $binary_string
- *
- * @throws TypeError
- *
- * @return int
- */
- function RandomCompat_strlen($binary_string)
- {
- if (!is_string($binary_string)) {
- throw new TypeError(
- 'RandomCompat_strlen() expects a string'
- );
- }
-
- return (int) mb_strlen($binary_string, '8bit');
- }
-
- } else {
- /**
- * strlen() implementation that isn't brittle to mbstring.func_overload
- *
- * This version just used the default strlen()
- *
- * @param string $binary_string
- *
- * @throws TypeError
- *
- * @return int
- */
- function RandomCompat_strlen($binary_string)
- {
- if (!is_string($binary_string)) {
- throw new TypeError(
- 'RandomCompat_strlen() expects a string'
- );
- }
- return (int) strlen($binary_string);
- }
- }
- }
-
- if (!is_callable('RandomCompat_substr')) {
-
- if (
- defined('MB_OVERLOAD_STRING')
- &&
- ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
- ) {
- /**
- * substr() implementation that isn't brittle to mbstring.func_overload
- *
- * This version uses mb_substr() in '8bit' mode to treat strings as raw
- * binary rather than UTF-8, ISO-8859-1, etc
- *
- * @param string $binary_string
- * @param int $start
- * @param int $length (optional)
- *
- * @throws TypeError
- *
- * @return string
- */
- function RandomCompat_substr($binary_string, $start, $length = null)
- {
- if (!is_string($binary_string)) {
- throw new TypeError(
- 'RandomCompat_substr(): First argument should be a string'
- );
- }
-
- if (!is_int($start)) {
- throw new TypeError(
- 'RandomCompat_substr(): Second argument should be an integer'
- );
- }
-
- if ($length === null) {
- /**
- * mb_substr($str, 0, NULL, '8bit') returns an empty string on
- * PHP 5.3, so we have to find the length ourselves.
- */
- $length = RandomCompat_strlen($binary_string) - $start;
- } elseif (!is_int($length)) {
- throw new TypeError(
- 'RandomCompat_substr(): Third argument should be an integer, or omitted'
- );
- }
-
- // Consistency with PHP's behavior
- if ($start === RandomCompat_strlen($binary_string) && $length === 0) {
- return '';
- }
- if ($start > RandomCompat_strlen($binary_string)) {
- return '';
- }
-
- return (string) mb_substr($binary_string, $start, $length, '8bit');
- }
-
- } else {
-
- /**
- * substr() implementation that isn't brittle to mbstring.func_overload
- *
- * This version just uses the default substr()
- *
- * @param string $binary_string
- * @param int $start
- * @param int $length (optional)
- *
- * @throws TypeError
- *
- * @return string
- */
- function RandomCompat_substr($binary_string, $start, $length = null)
- {
- if (!is_string($binary_string)) {
- throw new TypeError(
- 'RandomCompat_substr(): First argument should be a string'
- );
- }
-
- if (!is_int($start)) {
- throw new TypeError(
- 'RandomCompat_substr(): Second argument should be an integer'
- );
- }
-
- if ($length !== null) {
- if (!is_int($length)) {
- throw new TypeError(
- 'RandomCompat_substr(): Third argument should be an integer, or omitted'
- );
- }
-
- return (string) substr($binary_string, $start, $length);
- }
-
- return (string) substr($binary_string, $start);
- }
- }
- }
- <?php
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- if (!is_callable('RandomCompat_intval')) {
-
- /**
- * Cast to an integer if we can, safely.
- *
- * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX)
- * (non-inclusive), it will sanely cast it to an int. If you it's equal to
- * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats
- * lose precision, so the <= and => operators might accidentally let a float
- * through.
- *
- * @param int|float $number The number we want to convert to an int
- * @param bool $fail_open Set to true to not throw an exception
- *
- * @return float|int
- * @psalm-suppress InvalidReturnType
- *
- * @throws TypeError
- */
- function RandomCompat_intval($number, $fail_open = false)
- {
- if (is_int($number) || is_float($number)) {
- $number += 0;
- } elseif (is_numeric($number)) {
- $number += 0;
- }
-
- if (
- is_float($number)
- &&
- $number > ~PHP_INT_MAX
- &&
- $number < PHP_INT_MAX
- ) {
- $number = (int) $number;
- }
-
- if (is_int($number)) {
- return (int) $number;
- } elseif (!$fail_open) {
- throw new TypeError(
- 'Expected an integer.'
- );
- }
- return $number;
- }
- }
- <?php
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- if (!class_exists('Error', false)) {
- // We can't really avoid making this extend Exception in PHP 5.
- class Error extends Exception
- {
-
- }
- }
-
- if (!class_exists('TypeError', false)) {
- if (is_subclass_of('Error', 'Exception')) {
- class TypeError extends Error
- {
-
- }
- } else {
- class TypeError extends Exception
- {
-
- }
- }
- }
- <?php
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * @version 2.0.10
- * @released 2017-03-13
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- if (!defined('PHP_VERSION_ID')) {
- // This constant was introduced in PHP 5.2.7
- $RandomCompatversion = array_map('intval', explode('.', PHP_VERSION));
- define(
- 'PHP_VERSION_ID',
- $RandomCompatversion[0] * 10000
- + $RandomCompatversion[1] * 100
- + $RandomCompatversion[2]
- );
- $RandomCompatversion = null;
- }
-
- /**
- * PHP 7.0.0 and newer have these functions natively.
- */
- if (PHP_VERSION_ID >= 70000) {
- return;
- }
-
- if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
- define('RANDOM_COMPAT_READ_BUFFER', 8);
- }
-
- $RandomCompatDIR = dirname(__FILE__);
-
- require_once $RandomCompatDIR . '/byte_safe_strings.php';
- require_once $RandomCompatDIR . '/cast_to_int.php';
- require_once $RandomCompatDIR . '/error_polyfill.php';
-
- if (!is_callable('random_bytes')) {
- /**
- * PHP 5.2.0 - 5.6.x way to implement random_bytes()
- *
- * We use conditional statements here to define the function in accordance
- * to the operating environment. It's a micro-optimization.
- *
- * In order of preference:
- * 1. Use libsodium if available.
- * 2. fread() /dev/urandom if available (never on Windows)
- * 3. mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM)
- * 4. COM('CAPICOM.Utilities.1')->GetRandom()
- *
- * See RATIONALE.md for our reasoning behind this particular order
- */
- if (extension_loaded('libsodium')) {
- // See random_bytes_libsodium.php
- if (PHP_VERSION_ID >= 50300 && is_callable('\\Sodium\\randombytes_buf')) {
- require_once $RandomCompatDIR . '/random_bytes_libsodium.php';
- } elseif (method_exists('Sodium', 'randombytes_buf')) {
- require_once $RandomCompatDIR . '/random_bytes_libsodium_legacy.php';
- }
- }
-
- /**
- * Reading directly from /dev/urandom:
- */
- if (DIRECTORY_SEPARATOR === '/') {
- // DIRECTORY_SEPARATOR === '/' on Unix-like OSes -- this is a fast
- // way to exclude Windows.
- $RandomCompatUrandom = true;
- $RandomCompat_basedir = ini_get('open_basedir');
-
- if (!empty($RandomCompat_basedir)) {
- $RandomCompat_open_basedir = explode(
- PATH_SEPARATOR,
- strtolower($RandomCompat_basedir)
- );
- $RandomCompatUrandom = (array() !== array_intersect(
- array('/dev', '/dev/', '/dev/urandom'),
- $RandomCompat_open_basedir
- ));
- $RandomCompat_open_basedir = null;
- }
-
- if (
- !is_callable('random_bytes')
- &&
- $RandomCompatUrandom
- &&
- @is_readable('/dev/urandom')
- ) {
- // Error suppression on is_readable() in case of an open_basedir
- // or safe_mode failure. All we care about is whether or not we
- // can read it at this point. If the PHP environment is going to
- // panic over trying to see if the file can be read in the first
- // place, that is not helpful to us here.
-
- // See random_bytes_dev_urandom.php
- require_once $RandomCompatDIR . '/random_bytes_dev_urandom.php';
- }
- // Unset variables after use
- $RandomCompat_basedir = null;
- } else {
- $RandomCompatUrandom = false;
- }
-
- /**
- * mcrypt_create_iv()
- *
- * We only want to use mcypt_create_iv() if:
- *
- * - random_bytes() hasn't already been defined
- * - the mcrypt extensions is loaded
- * - One of these two conditions is true:
- * - We're on Windows (DIRECTORY_SEPARATOR !== '/')
- * - We're not on Windows and /dev/urandom is readabale
- * (i.e. we're not in a chroot jail)
- * - Special case:
- * - If we're not on Windows, but the PHP version is between
- * 5.6.10 and 5.6.12, we don't want to use mcrypt. It will
- * hang indefinitely. This is bad.
- * - If we're on Windows, we want to use PHP >= 5.3.7 or else
- * we get insufficient entropy errors.
- */
- if (
- !is_callable('random_bytes')
- &&
- // Windows on PHP < 5.3.7 is broken, but non-Windows is not known to be.
- (DIRECTORY_SEPARATOR === '/' || PHP_VERSION_ID >= 50307)
- &&
- // Prevent this code from hanging indefinitely on non-Windows;
- // see https://bugs.php.net/bug.php?id=69833
- (
- DIRECTORY_SEPARATOR !== '/' ||
- (PHP_VERSION_ID <= 50609 || PHP_VERSION_ID >= 50613)
- )
- &&
- extension_loaded('mcrypt')
- ) {
- // See random_bytes_mcrypt.php
- require_once $RandomCompatDIR . '/random_bytes_mcrypt.php';
- }
- $RandomCompatUrandom = null;
-
- /**
- * This is a Windows-specific fallback, for when the mcrypt extension
- * isn't loaded.
- */
- if (
- !is_callable('random_bytes')
- &&
- extension_loaded('com_dotnet')
- &&
- class_exists('COM')
- ) {
- $RandomCompat_disabled_classes = preg_split(
- '#\s*,\s*#',
- strtolower(ini_get('disable_classes'))
- );
-
- if (!in_array('com', $RandomCompat_disabled_classes)) {
- try {
- $RandomCompatCOMtest = new COM('CAPICOM.Utilities.1');
- if (method_exists($RandomCompatCOMtest, 'GetRandom')) {
- // See random_bytes_com_dotnet.php
- require_once $RandomCompatDIR . '/random_bytes_com_dotnet.php';
- }
- } catch (com_exception $e) {
- // Don't try to use it.
- }
- }
- $RandomCompat_disabled_classes = null;
- $RandomCompatCOMtest = null;
- }
-
- /**
- * throw new Exception
- */
- if (!is_callable('random_bytes')) {
- /**
- * We don't have any more options, so let's throw an exception right now
- * and hope the developer won't let it fail silently.
- *
- * @param mixed $length
- * @psalm-suppress MissingReturnType
- * @throws Exception
- * @return string
- */
- function random_bytes($length)
- {
- unset($length); // Suppress "variable not used" warnings.
- throw new Exception(
- 'There is no suitable CSPRNG installed on your system'
- );
- return '';
- }
- }
- }
-
- if (!is_callable('random_int')) {
- require_once $RandomCompatDIR . '/random_int.php';
- }
-
- $RandomCompatDIR = null;
- <?php
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- if (!is_callable('random_bytes')) {
- /**
- * Windows with PHP < 5.3.0 will not have the function
- * openssl_random_pseudo_bytes() available, so let's use
- * CAPICOM to work around this deficiency.
- *
- * @param int $bytes
- *
- * @throws Exception
- *
- * @return string
- */
- function random_bytes($bytes)
- {
- try {
- $bytes = RandomCompat_intval($bytes);
- } catch (TypeError $ex) {
- throw new TypeError(
- 'random_bytes(): $bytes must be an integer'
- );
- }
-
- if ($bytes < 1) {
- throw new Error(
- 'Length must be greater than 0'
- );
- }
-
- $buf = '';
- if (!class_exists('COM')) {
- throw new Error(
- 'COM does not exist'
- );
- }
- $util = new COM('CAPICOM.Utilities.1');
- $execCount = 0;
-
- /**
- * Let's not let it loop forever. If we run N times and fail to
- * get N bytes of random data, then CAPICOM has failed us.
- */
- do {
- $buf .= base64_decode($util->GetRandom($bytes, 0));
- if (RandomCompat_strlen($buf) >= $bytes) {
- /**
- * Return our random entropy buffer here:
- */
- return RandomCompat_substr($buf, 0, $bytes);
- }
- ++$execCount;
- } while ($execCount < $bytes);
-
- /**
- * If we reach here, PHP has failed us.
- */
- throw new Exception(
- 'Could not gather sufficient random data'
- );
- }
- }<?php
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
- define('RANDOM_COMPAT_READ_BUFFER', 8);
- }
-
- if (!is_callable('random_bytes')) {
- /**
- * Unless open_basedir is enabled, use /dev/urandom for
- * random numbers in accordance with best practices
- *
- * Why we use /dev/urandom and not /dev/random
- * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers
- *
- * @param int $bytes
- *
- * @throws Exception
- *
- * @return string
- */
- function random_bytes($bytes)
- {
- static $fp = null;
- /**
- * This block should only be run once
- */
- if (empty($fp)) {
- /**
- * We use /dev/urandom if it is a char device.
- * We never fall back to /dev/random
- */
- $fp = fopen('/dev/urandom', 'rb');
- if (!empty($fp)) {
- $st = fstat($fp);
- if (($st['mode'] & 0170000) !== 020000) {
- fclose($fp);
- $fp = false;
- }
- }
-
- if (!empty($fp)) {
- /**
- * stream_set_read_buffer() does not exist in HHVM
- *
- * If we don't set the stream's read buffer to 0, PHP will
- * internally buffer 8192 bytes, which can waste entropy
- *
- * stream_set_read_buffer returns 0 on success
- */
- if (is_callable('stream_set_read_buffer')) {
- stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER);
- }
- if (is_callable('stream_set_chunk_size')) {
- stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER);
- }
- }
- }
-
- try {
- $bytes = RandomCompat_intval($bytes);
- } catch (TypeError $ex) {
- throw new TypeError(
- 'random_bytes(): $bytes must be an integer'
- );
- }
-
- if ($bytes < 1) {
- throw new Error(
- 'Length must be greater than 0'
- );
- }
-
- /**
- * This if() block only runs if we managed to open a file handle
- *
- * It does not belong in an else {} block, because the above
- * if (empty($fp)) line is logic that should only be run once per
- * page load.
- */
- if (!empty($fp)) {
- /**
- * @var int
- */
- $remaining = $bytes;
-
- /**
- * @var string|bool
- */
- $buf = '';
-
- /**
- * We use fread() in a loop to protect against partial reads
- */
- do {
- /**
- * @var string|bool
- */
- $read = fread($fp, $remaining);
- if (!is_string($read)) {
- if ($read === false) {
- /**
- * We cannot safely read from the file. Exit the
- * do-while loop and trigger the exception condition
- *
- * @var string|bool
- */
- $buf = false;
- break;
- }
- }
- /**
- * Decrease the number of bytes returned from remaining
- */
- $remaining -= RandomCompat_strlen($read);
- /**
- * @var string|bool
- */
- $buf = $buf . $read;
- } while ($remaining > 0);
-
- /**
- * Is our result valid?
- */
- if (is_string($buf)) {
- if (RandomCompat_strlen($buf) === $bytes) {
- /**
- * Return our random entropy buffer here:
- */
- return $buf;
- }
- }
- }
-
- /**
- * If we reach here, PHP has failed us.
- */
- throw new Exception(
- 'Error reading from source device'
- );
- }
- }
- <?php
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- if (!is_callable('random_bytes')) {
- /**
- * If the libsodium PHP extension is loaded, we'll use it above any other
- * solution.
- *
- * libsodium-php project:
- * @ref https://github.com/jedisct1/libsodium-php
- *
- * @param int $bytes
- *
- * @throws Exception
- *
- * @return string
- */
- function random_bytes($bytes)
- {
- try {
- $bytes = RandomCompat_intval($bytes);
- } catch (TypeError $ex) {
- throw new TypeError(
- 'random_bytes(): $bytes must be an integer'
- );
- }
-
- if ($bytes < 1) {
- throw new Error(
- 'Length must be greater than 0'
- );
- }
-
- /**
- * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
- * generated in one invocation.
- */
- if ($bytes > 2147483647) {
- $buf = '';
- for ($i = 0; $i < $bytes; $i += 1073741824) {
- $n = ($bytes - $i) > 1073741824
- ? 1073741824
- : $bytes - $i;
- $buf .= \Sodium\randombytes_buf($n);
- }
- } else {
- $buf = \Sodium\randombytes_buf($bytes);
- }
-
- if ($buf !== false) {
- if (RandomCompat_strlen($buf) === $bytes) {
- return $buf;
- }
- }
-
- /**
- * If we reach here, PHP has failed us.
- */
- throw new Exception(
- 'Could not gather sufficient random data'
- );
- }
- }
- <?php
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- if (!is_callable('random_bytes')) {
- /**
- * If the libsodium PHP extension is loaded, we'll use it above any other
- * solution.
- *
- * libsodium-php project:
- * @ref https://github.com/jedisct1/libsodium-php
- *
- * @param int $bytes
- *
- * @throws Exception
- *
- * @return string
- */
- function random_bytes($bytes)
- {
- try {
- $bytes = RandomCompat_intval($bytes);
- } catch (TypeError $ex) {
- throw new TypeError(
- 'random_bytes(): $bytes must be an integer'
- );
- }
-
- if ($bytes < 1) {
- throw new Error(
- 'Length must be greater than 0'
- );
- }
-
- /**
- * @var string
- */
- $buf = '';
-
- /**
- * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
- * generated in one invocation.
- */
- if ($bytes > 2147483647) {
- for ($i = 0; $i < $bytes; $i += 1073741824) {
- $n = ($bytes - $i) > 1073741824
- ? 1073741824
- : $bytes - $i;
- $buf .= Sodium::randombytes_buf((int) $n);
- }
- } else {
- $buf .= Sodium::randombytes_buf((int) $bytes);
- }
-
- if (is_string($buf)) {
- if (RandomCompat_strlen($buf) === $bytes) {
- return $buf;
- }
- }
-
- /**
- * If we reach here, PHP has failed us.
- */
- throw new Exception(
- 'Could not gather sufficient random data'
- );
- }
- }
- <?php
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- if (!is_callable('random_bytes')) {
- /**
- * Powered by ext/mcrypt (and thankfully NOT libmcrypt)
- *
- * @ref https://bugs.php.net/bug.php?id=55169
- * @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386
- *
- * @param int $bytes
- *
- * @throws Exception
- *
- * @return string
- */
- function random_bytes($bytes)
- {
- try {
- $bytes = RandomCompat_intval($bytes);
- } catch (TypeError $ex) {
- throw new TypeError(
- 'random_bytes(): $bytes must be an integer'
- );
- }
-
- if ($bytes < 1) {
- throw new Error(
- 'Length must be greater than 0'
- );
- }
-
- $buf = @mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
- if (
- $buf !== false
- &&
- RandomCompat_strlen($buf) === $bytes
- ) {
- /**
- * Return our random entropy buffer here:
- */
- return $buf;
- }
-
- /**
- * If we reach here, PHP has failed us.
- */
- throw new Exception(
- 'Could not gather sufficient random data'
- );
- }
- }
- <?php
-
- if (!is_callable('random_int')) {
- /**
- * Random_* Compatibility Library
- * for using the new PHP 7 random_* API in PHP 5 projects
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- /**
- * Fetch a random integer between $min and $max inclusive
- *
- * @param int $min
- * @param int $max
- *
- * @throws Exception
- *
- * @return int
- */
- function random_int($min, $max)
- {
- /**
- * Type and input logic checks
- *
- * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX)
- * (non-inclusive), it will sanely cast it to an int. If you it's equal to
- * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats
- * lose precision, so the <= and => operators might accidentally let a float
- * through.
- */
-
- try {
- $min = RandomCompat_intval($min);
- } catch (TypeError $ex) {
- throw new TypeError(
- 'random_int(): $min must be an integer'
- );
- }
-
- try {
- $max = RandomCompat_intval($max);
- } catch (TypeError $ex) {
- throw new TypeError(
- 'random_int(): $max must be an integer'
- );
- }
-
- /**
- * Now that we've verified our weak typing system has given us an integer,
- * let's validate the logic then we can move forward with generating random
- * integers along a given range.
- */
- if ($min > $max) {
- throw new Error(
- 'Minimum value must be less than or equal to the maximum value'
- );
- }
-
- if ($max === $min) {
- return (int) $min;
- }
-
- /**
- * Initialize variables to 0
- *
- * We want to store:
- * $bytes => the number of random bytes we need
- * $mask => an integer bitmask (for use with the &) operator
- * so we can minimize the number of discards
- */
- $attempts = $bits = $bytes = $mask = $valueShift = 0;
-
- /**
- * At this point, $range is a positive number greater than 0. It might
- * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to
- * a float and we will lose some precision.
- */
- $range = $max - $min;
-
- /**
- * Test for integer overflow:
- */
- if (!is_int($range)) {
-
- /**
- * Still safely calculate wider ranges.
- * Provided by @CodesInChaos, @oittaa
- *
- * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435
- *
- * We use ~0 as a mask in this case because it generates all 1s
- *
- * @ref https://eval.in/400356 (32-bit)
- * @ref http://3v4l.org/XX9r5 (64-bit)
- */
- $bytes = PHP_INT_SIZE;
- $mask = ~0;
-
- } else {
-
- /**
- * $bits is effectively ceil(log($range, 2)) without dealing with
- * type juggling
- */
- while ($range > 0) {
- if ($bits % 8 === 0) {
- ++$bytes;
- }
- ++$bits;
- $range >>= 1;
- $mask = $mask << 1 | 1;
- }
- $valueShift = $min;
- }
-
- $val = 0;
- /**
- * Now that we have our parameters set up, let's begin generating
- * random integers until one falls between $min and $max
- */
- do {
- /**
- * The rejection probability is at most 0.5, so this corresponds
- * to a failure probability of 2^-128 for a working RNG
- */
- if ($attempts > 128) {
- throw new Exception(
- 'random_int: RNG is broken - too many rejections'
- );
- }
-
- /**
- * Let's grab the necessary number of random bytes
- */
- $randomByteString = random_bytes($bytes);
-
- /**
- * Let's turn $randomByteString into an integer
- *
- * This uses bitwise operators (<< and |) to build an integer
- * out of the values extracted from ord()
- *
- * Example: [9F] | [6D] | [32] | [0C] =>
- * 159 + 27904 + 3276800 + 201326592 =>
- * 204631455
- */
- $val &= 0;
- for ($i = 0; $i < $bytes; ++$i) {
- $val |= ord($randomByteString[$i]) << ($i * 8);
- }
-
- /**
- * Apply mask
- */
- $val &= $mask;
- $val += $valueShift;
-
- ++$attempts;
- /**
- * If $val overflows to a floating point number,
- * ... or is larger than $max,
- * ... or smaller than $min,
- * then try again.
- */
- } while (!is_int($val) || $val > $max || $val < $min);
-
- return (int) $val;
- }
- }
- 0e1�é¶hìUúc\H7€ÉlOS‘=ð˜©áÊ2ìE³!¼ù ÈS$5¿øåË#Œ Åà4‡”0!”Ôf‹O‡¥©ËZG©ÕáŒy>(ê$Òwšº¾Û(\�ÔH°Z-”íݬ‡¯-vÖg������GBMB
|