| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Encode in Base32 based on RFC 4648. |
| 10 |
* Requires 20% more space than base64 |
| 11 |
* Great for case-insensitive filesystems like Windows and URL's (except for = char which can be excluded using the pad option for urls) |
| 12 |
* |
| 13 |
* @package default |
| 14 |
* @author Bryan Ruiz |
| 15 |
**/ |
| 16 |
class Base32Static { |
| 17 |
|
| 18 |
private static $map = [ |
| 19 |
'A', |
| 20 |
'B', |
| 21 |
'C', |
| 22 |
'D', |
| 23 |
'E', |
| 24 |
'F', |
| 25 |
'G', |
| 26 |
'H', // 7 |
| 27 |
'I', |
| 28 |
'J', |
| 29 |
'K', |
| 30 |
'L', |
| 31 |
'M', |
| 32 |
'N', |
| 33 |
'O', |
| 34 |
'P', // 15 |
| 35 |
'Q', |
| 36 |
'R', |
| 37 |
'S', |
| 38 |
'T', |
| 39 |
'U', |
| 40 |
'V', |
| 41 |
'W', |
| 42 |
'X', // 23 |
| 43 |
'Y', |
| 44 |
'Z', |
| 45 |
'2', |
| 46 |
'3', |
| 47 |
'4', |
| 48 |
'5', |
| 49 |
'6', |
| 50 |
'7', // 31 |
| 51 |
'=', // padding character |
| 52 |
]; |
| 53 |
|
| 54 |
private static $flipped_map = [ |
| 55 |
'A' => '0', |
| 56 |
'B' => '1', |
| 57 |
'C' => '2', |
| 58 |
'D' => '3', |
| 59 |
'E' => '4', |
| 60 |
'F' => '5', |
| 61 |
'G' => '6', |
| 62 |
'H' => '7', |
| 63 |
'I' => '8', |
| 64 |
'J' => '9', |
| 65 |
'K' => '10', |
| 66 |
'L' => '11', |
| 67 |
'M' => '12', |
| 68 |
'N' => '13', |
| 69 |
'O' => '14', |
| 70 |
'P' => '15', |
| 71 |
'Q' => '16', |
| 72 |
'R' => '17', |
| 73 |
'S' => '18', |
| 74 |
'T' => '19', |
| 75 |
'U' => '20', |
| 76 |
'V' => '21', |
| 77 |
'W' => '22', |
| 78 |
'X' => '23', |
| 79 |
'Y' => '24', |
| 80 |
'Z' => '25', |
| 81 |
'2' => '26', |
| 82 |
'3' => '27', |
| 83 |
'4' => '28', |
| 84 |
'5' => '29', |
| 85 |
'6' => '30', |
| 86 |
'7' => '31', |
| 87 |
]; |
| 88 |
|
| 89 |
/** |
| 90 |
* Use padding false when encoding for urls |
| 91 |
* |
| 92 |
* @return base32 encoded string |
| 93 |
* @author Bryan Ruiz |
| 94 |
**/ |
| 95 |
public static function encode( $input, $padding = true ) { |
| 96 |
if ( empty( $input ) ) { |
| 97 |
return ''; |
| 98 |
} |
| 99 |
|
| 100 |
$input = str_split( $input ); |
| 101 |
$binary_string = ''; |
| 102 |
|
| 103 |
for ( $i = 0; $i < count( $input ); $i++ ) { |
| 104 |
$binary_string .= str_pad( base_convert( ord( $input[ $i ] ), 10, 2 ), 8, '0', STR_PAD_LEFT ); |
| 105 |
} |
| 106 |
|
| 107 |
$five_bit_binary_array = str_split( $binary_string, 5 ); |
| 108 |
$base32 = ''; |
| 109 |
$i = 0; |
| 110 |
|
| 111 |
while ( $i < count( $five_bit_binary_array ) ) { |
| 112 |
$base32 .= self::$map[ base_convert( str_pad( $five_bit_binary_array[ $i ], 5, '0' ), 2, 10 ) ]; |
| 113 |
$i++; |
| 114 |
} |
| 115 |
|
| 116 |
if ( $padding && ( $x = strlen( $binary_string ) % 40 ) != 0 ) { |
| 117 |
if ( $x == 8 ) { |
| 118 |
$base32 .= str_repeat( self::$map[32], 6 ); |
| 119 |
} elseif ( $x == 16 ) { |
| 120 |
$base32 .= str_repeat( self::$map[32], 4 ); |
| 121 |
} elseif ( $x == 24 ) { |
| 122 |
$base32 .= str_repeat( self::$map[32], 3 ); |
| 123 |
} elseif ( $x == 32 ) { |
| 124 |
$base32 .= self::$map[32]; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $base32; |
| 129 |
} |
| 130 |
|
| 131 |
public static function decode( $input ) { |
| 132 |
if ( empty( $input ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$padding_char_count = substr_count( $input, self::$map[32] ); |
| 137 |
$allowed_values = [ 6, 4, 3, 1, 0 ]; |
| 138 |
|
| 139 |
if ( ! in_array( $padding_char_count, $allowed_values ) ) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
for ( $i = 0; $i < 4; $i++ ) { |
| 144 |
if ( $padding_char_count == $allowed_values[ $i ] && substr( $input, -( $allowed_values[ $i ] ) ) != str_repeat( self::$map[32], $allowed_values[ $i ] ) ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
$input = str_replace( '=', '', $input ); |
| 150 |
$input = str_split( $input ); |
| 151 |
$binary_string = ''; |
| 152 |
|
| 153 |
for ( $i = 0; $i < count( $input ); $i = $i + 8 ) { |
| 154 |
$x = ''; |
| 155 |
|
| 156 |
if ( ! in_array( $input[ $i ], self::$map ) ) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
for ( $j = 0; $j < 8; $j++ ) { |
| 161 |
$x .= str_pad( base_convert( @self::$flipped_map[ @$input[ $i + $j ] ], 10, 2 ), 5, '0', STR_PAD_LEFT ); |
| 162 |
} |
| 163 |
|
| 164 |
$eight_bits = str_split( $x, 8 ); |
| 165 |
|
| 166 |
for ( $z = 0; $z < count( $eight_bits ); $z++ ) { |
| 167 |
$binary_string .= ( ( $y = chr( base_convert( $eight_bits[ $z ], 2, 10 ) ) ) || ord( $y ) == 48 ) ? $y : ''; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return $binary_string; |
| 172 |
} |
| 173 |
} |
| 174 |
|