Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wordfenc.../modules/login-se.../classes/model/crypto
File: base2n.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WordfenceLS\Crypto;
[2] Fix | Delete
/**
[3] Fix | Delete
* Binary-to-text PHP Utilities
[4] Fix | Delete
*
[5] Fix | Delete
* @package binary-to-text-php
[6] Fix | Delete
* @link https://github.com/ademarre/binary-to-text-php
[7] Fix | Delete
* @author Andre DeMarre
[8] Fix | Delete
* @copyright 2009-2013 Andre DeMarre
[9] Fix | Delete
* @license http://opensource.org/licenses/MIT MIT
[10] Fix | Delete
*/
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Class for binary-to-text encoding with a base of 2^n
[14] Fix | Delete
*
[15] Fix | Delete
* The Base2n class is for binary-to-text conversion. It employs a
[16] Fix | Delete
* generalization of the algorithms used by many encoding schemes that
[17] Fix | Delete
* use a fixed number of bits to encode each character. In other words,
[18] Fix | Delete
* the base is a power of 2.
[19] Fix | Delete
*
[20] Fix | Delete
* Earlier versions of this class were named
[21] Fix | Delete
* FixedBitNotation and FixedBitEncoding.
[22] Fix | Delete
*
[23] Fix | Delete
* @package binary-to-text-php
[24] Fix | Delete
*/
[25] Fix | Delete
class Model_Base2n
[26] Fix | Delete
{
[27] Fix | Delete
protected $_chars;
[28] Fix | Delete
protected $_bitsPerCharacter;
[29] Fix | Delete
protected $_radix;
[30] Fix | Delete
protected $_rightPadFinalBits;
[31] Fix | Delete
protected $_padFinalGroup;
[32] Fix | Delete
protected $_padCharacter;
[33] Fix | Delete
protected $_caseSensitive;
[34] Fix | Delete
protected $_charmap;
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Constructor
[38] Fix | Delete
*
[39] Fix | Delete
* @param integer $bitsPerCharacter Bits to use for each encoded character
[40] Fix | Delete
* @param string $chars Base character alphabet
[41] Fix | Delete
* @param boolean $caseSensitive To decode in a case-sensitive manner
[42] Fix | Delete
* @param boolean $rightPadFinalBits How to encode last character
[43] Fix | Delete
* @param boolean $padFinalGroup Add padding to end of encoded output
[44] Fix | Delete
* @param string $padCharacter Character to use for padding
[45] Fix | Delete
*
[46] Fix | Delete
* @throws InvalidArgumentException for incompatible parameters
[47] Fix | Delete
*/
[48] Fix | Delete
public function __construct(
[49] Fix | Delete
$bitsPerCharacter,
[50] Fix | Delete
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_',
[51] Fix | Delete
$caseSensitive = TRUE, $rightPadFinalBits = FALSE,
[52] Fix | Delete
$padFinalGroup = FALSE, $padCharacter = '=')
[53] Fix | Delete
{
[54] Fix | Delete
// Ensure validity of $chars
[55] Fix | Delete
if (!is_string($chars) || ($charLength = strlen($chars)) < 2) {
[56] Fix | Delete
throw new \InvalidArgumentException('$chars must be a string of at least two characters');
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
// Ensure validity of $padCharacter
[60] Fix | Delete
if ($padFinalGroup) {
[61] Fix | Delete
if (!is_string($padCharacter) || !isset($padCharacter[0])) {
[62] Fix | Delete
throw new \InvalidArgumentException('$padCharacter must be a string of one character');
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
if ($caseSensitive) {
[66] Fix | Delete
$padCharFound = strpos($chars, $padCharacter[0]);
[67] Fix | Delete
} else {
[68] Fix | Delete
$padCharFound = stripos($chars, $padCharacter[0]);
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
if ($padCharFound !== FALSE) {
[72] Fix | Delete
throw new \InvalidArgumentException('$padCharacter can not be a member of $chars');
[73] Fix | Delete
}
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
// Ensure validity of $bitsPerCharacter
[77] Fix | Delete
if (!is_int($bitsPerCharacter)) {
[78] Fix | Delete
throw new \InvalidArgumentException('$bitsPerCharacter must be an integer');
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
if ($bitsPerCharacter < 1) {
[82] Fix | Delete
// $bitsPerCharacter must be at least 1
[83] Fix | Delete
throw new \InvalidArgumentException('$bitsPerCharacter can not be less than 1');
[84] Fix | Delete
[85] Fix | Delete
} elseif ($charLength < 1 << $bitsPerCharacter) {
[86] Fix | Delete
// Character length of $chars is too small for $bitsPerCharacter
[87] Fix | Delete
// Find greatest acceptable value of $bitsPerCharacter
[88] Fix | Delete
$bitsPerCharacter = 1;
[89] Fix | Delete
$radix = 2;
[90] Fix | Delete
[91] Fix | Delete
while ($charLength >= ($radix <<= 1) && $bitsPerCharacter < 8) {
[92] Fix | Delete
$bitsPerCharacter++;
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
$radix >>= 1;
[96] Fix | Delete
throw new \InvalidArgumentException(
[97] Fix | Delete
'$bitsPerCharacter can not be more than ' . $bitsPerCharacter
[98] Fix | Delete
. ' given $chars length of ' . $charLength
[99] Fix | Delete
. ' (max radix ' . $radix . ')');
[100] Fix | Delete
[101] Fix | Delete
} elseif ($bitsPerCharacter > 8) {
[102] Fix | Delete
// $bitsPerCharacter must not be greater than 8
[103] Fix | Delete
throw new \InvalidArgumentException('$bitsPerCharacter can not be greater than 8');
[104] Fix | Delete
[105] Fix | Delete
} else {
[106] Fix | Delete
$radix = 1 << $bitsPerCharacter;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
$this->_chars = $chars;
[110] Fix | Delete
$this->_bitsPerCharacter = $bitsPerCharacter;
[111] Fix | Delete
$this->_radix = $radix;
[112] Fix | Delete
$this->_rightPadFinalBits = $rightPadFinalBits;
[113] Fix | Delete
$this->_padFinalGroup = $padFinalGroup;
[114] Fix | Delete
$this->_padCharacter = $padCharacter[0];
[115] Fix | Delete
$this->_caseSensitive = $caseSensitive;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Encode a string
[120] Fix | Delete
*
[121] Fix | Delete
* @param string $rawString Binary data to encode
[122] Fix | Delete
* @return string
[123] Fix | Delete
*/
[124] Fix | Delete
public function encode($rawString)
[125] Fix | Delete
{
[126] Fix | Delete
// Unpack string into an array of bytes
[127] Fix | Delete
$bytes = unpack('C*', $rawString);
[128] Fix | Delete
$byteCount = count($bytes);
[129] Fix | Delete
[130] Fix | Delete
$encodedString = '';
[131] Fix | Delete
$byte = array_shift($bytes);
[132] Fix | Delete
$bitsRead = 0;
[133] Fix | Delete
$oldBits = 0;
[134] Fix | Delete
[135] Fix | Delete
$chars = $this->_chars;
[136] Fix | Delete
$bitsPerCharacter = $this->_bitsPerCharacter;
[137] Fix | Delete
$rightPadFinalBits = $this->_rightPadFinalBits;
[138] Fix | Delete
$padFinalGroup = $this->_padFinalGroup;
[139] Fix | Delete
$padCharacter = $this->_padCharacter;
[140] Fix | Delete
[141] Fix | Delete
$charsPerByte = 8 / $bitsPerCharacter;
[142] Fix | Delete
$encodedLength = $byteCount * $charsPerByte;
[143] Fix | Delete
[144] Fix | Delete
// Generate encoded output; each loop produces one encoded character
[145] Fix | Delete
for ($c = 0; $c < $encodedLength; $c++) {
[146] Fix | Delete
[147] Fix | Delete
// Get the bits needed for this encoded character
[148] Fix | Delete
if ($bitsRead + $bitsPerCharacter > 8) {
[149] Fix | Delete
// Not enough bits remain in this byte for the current character
[150] Fix | Delete
// Save the remaining bits before getting the next byte
[151] Fix | Delete
$oldBitCount = 8 - $bitsRead;
[152] Fix | Delete
$oldBits = $byte ^ ($byte >> $oldBitCount << $oldBitCount);
[153] Fix | Delete
$newBitCount = $bitsPerCharacter - $oldBitCount;
[154] Fix | Delete
[155] Fix | Delete
if (!$bytes) {
[156] Fix | Delete
// Last bits; match final character and exit loop
[157] Fix | Delete
if ($rightPadFinalBits) $oldBits <<= $newBitCount;
[158] Fix | Delete
$encodedString .= $chars[$oldBits];
[159] Fix | Delete
[160] Fix | Delete
if ($padFinalGroup) {
[161] Fix | Delete
// Array of the lowest common multiples of $bitsPerCharacter and 8, divided by 8
[162] Fix | Delete
$lcmMap = array(1 => 1, 2 => 1, 3 => 3, 4 => 1, 5 => 5, 6 => 3, 7 => 7, 8 => 1);
[163] Fix | Delete
$bytesPerGroup = $lcmMap[$bitsPerCharacter];
[164] Fix | Delete
$pads = $bytesPerGroup * $charsPerByte - ceil((strlen($rawString) % $bytesPerGroup) * $charsPerByte);
[165] Fix | Delete
$encodedString .= str_repeat($padCharacter, $pads);
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
break;
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
// Get next byte
[172] Fix | Delete
$byte = array_shift($bytes);
[173] Fix | Delete
$bitsRead = 0;
[174] Fix | Delete
[175] Fix | Delete
} else {
[176] Fix | Delete
$oldBitCount = 0;
[177] Fix | Delete
$newBitCount = $bitsPerCharacter;
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
// Read only the needed bits from this byte
[181] Fix | Delete
$bits = $byte >> 8 - ($bitsRead + ($newBitCount));
[182] Fix | Delete
$bits ^= $bits >> $newBitCount << $newBitCount;
[183] Fix | Delete
$bitsRead += $newBitCount;
[184] Fix | Delete
[185] Fix | Delete
if ($oldBitCount) {
[186] Fix | Delete
// Bits come from seperate bytes, add $oldBits to $bits
[187] Fix | Delete
$bits = ($oldBits << $newBitCount) | $bits;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
$encodedString .= $chars[$bits];
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
return $encodedString;
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
/**
[197] Fix | Delete
* Decode a string
[198] Fix | Delete
*
[199] Fix | Delete
* @param string $encodedString Data to decode
[200] Fix | Delete
* @param boolean $strict Returns NULL if $encodedString contains an undecodable character
[201] Fix | Delete
* @return string
[202] Fix | Delete
*/
[203] Fix | Delete
public function decode($encodedString, $strict = FALSE)
[204] Fix | Delete
{
[205] Fix | Delete
if (!$encodedString || !is_string($encodedString)) {
[206] Fix | Delete
// Empty string, nothing to decode
[207] Fix | Delete
return '';
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
$chars = $this->_chars;
[211] Fix | Delete
$bitsPerCharacter = $this->_bitsPerCharacter;
[212] Fix | Delete
$radix = $this->_radix;
[213] Fix | Delete
$rightPadFinalBits = $this->_rightPadFinalBits;
[214] Fix | Delete
$padFinalGroup = $this->_padFinalGroup;
[215] Fix | Delete
$padCharacter = $this->_padCharacter;
[216] Fix | Delete
$caseSensitive = $this->_caseSensitive;
[217] Fix | Delete
[218] Fix | Delete
// Get index of encoded characters
[219] Fix | Delete
if ($this->_charmap) {
[220] Fix | Delete
$charmap = $this->_charmap;
[221] Fix | Delete
[222] Fix | Delete
} else {
[223] Fix | Delete
$charmap = array();
[224] Fix | Delete
[225] Fix | Delete
for ($i = 0; $i < $radix; $i++) {
[226] Fix | Delete
$charmap[$chars[$i]] = $i;
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
$this->_charmap = $charmap;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
// The last encoded character is $encodedString[$lastNotatedIndex]
[233] Fix | Delete
$lastNotatedIndex = strlen($encodedString) - 1;
[234] Fix | Delete
[235] Fix | Delete
// Remove trailing padding characters
[236] Fix | Delete
if ($padFinalGroup) {
[237] Fix | Delete
while ($encodedString[$lastNotatedIndex] === $padCharacter) {
[238] Fix | Delete
$encodedString = substr($encodedString, 0, $lastNotatedIndex);
[239] Fix | Delete
$lastNotatedIndex--;
[240] Fix | Delete
}
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
$rawString = '';
[244] Fix | Delete
$byte = 0;
[245] Fix | Delete
$bitsWritten = 0;
[246] Fix | Delete
[247] Fix | Delete
// Convert each encoded character to a series of unencoded bits
[248] Fix | Delete
for ($c = 0; $c <= $lastNotatedIndex; $c++) {
[249] Fix | Delete
[250] Fix | Delete
if (!$caseSensitive && !isset($charmap[$encodedString[$c]])) {
[251] Fix | Delete
// Encoded character was not found; try other case
[252] Fix | Delete
if (isset($charmap[$cUpper = strtoupper($encodedString[$c])])) {
[253] Fix | Delete
$charmap[$encodedString[$c]] = $charmap[$cUpper];
[254] Fix | Delete
[255] Fix | Delete
} elseif (isset($charmap[$cLower = strtolower($encodedString[$c])])) {
[256] Fix | Delete
$charmap[$encodedString[$c]] = $charmap[$cLower];
[257] Fix | Delete
}
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
if (isset($charmap[$encodedString[$c]])) {
[261] Fix | Delete
$bitsNeeded = 8 - $bitsWritten;
[262] Fix | Delete
$unusedBitCount = $bitsPerCharacter - $bitsNeeded;
[263] Fix | Delete
[264] Fix | Delete
// Get the new bits ready
[265] Fix | Delete
if ($bitsNeeded > $bitsPerCharacter) {
[266] Fix | Delete
// New bits aren't enough to complete a byte; shift them left into position
[267] Fix | Delete
$newBits = $charmap[$encodedString[$c]] << $bitsNeeded - $bitsPerCharacter;
[268] Fix | Delete
$bitsWritten += $bitsPerCharacter;
[269] Fix | Delete
[270] Fix | Delete
} elseif ($c !== $lastNotatedIndex || $rightPadFinalBits) {
[271] Fix | Delete
// Zero or more too many bits to complete a byte; shift right
[272] Fix | Delete
$newBits = $charmap[$encodedString[$c]] >> $unusedBitCount;
[273] Fix | Delete
$bitsWritten = 8; //$bitsWritten += $bitsNeeded;
[274] Fix | Delete
[275] Fix | Delete
} else {
[276] Fix | Delete
// Final bits don't need to be shifted
[277] Fix | Delete
$newBits = $charmap[$encodedString[$c]];
[278] Fix | Delete
$bitsWritten = 8;
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
$byte |= $newBits;
[282] Fix | Delete
[283] Fix | Delete
if ($bitsWritten === 8 || $c === $lastNotatedIndex) {
[284] Fix | Delete
// Byte is ready to be written
[285] Fix | Delete
$rawString .= pack('C', $byte);
[286] Fix | Delete
[287] Fix | Delete
if ($c !== $lastNotatedIndex) {
[288] Fix | Delete
// Start the next byte
[289] Fix | Delete
$bitsWritten = $unusedBitCount;
[290] Fix | Delete
$byte = ($charmap[$encodedString[$c]] ^ ($newBits << $unusedBitCount)) << 8 - $bitsWritten;
[291] Fix | Delete
}
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
} elseif ($strict) {
[295] Fix | Delete
// Unable to decode character; abort
[296] Fix | Delete
return NULL;
[297] Fix | Delete
}
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
return $rawString;
[301] Fix | Delete
}
[302] Fix | Delete
}
[303] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function