1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\Util; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class offering Base64 encoding and decoding. |
8
|
|
|
*/ |
9
|
|
|
class Base64 |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Encode a string using base64url variant. |
13
|
|
|
* |
14
|
|
|
* @link https://en.wikipedia.org/wiki/Base64#URL_applications |
15
|
|
|
* @param string $data |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
164 |
|
public static function urlEncode($data) { |
19
|
164 |
|
return strtr(rtrim(self::encode($data), "="), "+/", "-_"); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Decode a string using base64url variant. |
24
|
|
|
* |
25
|
|
|
* @link https://en.wikipedia.org/wiki/Base64#URL_applications |
26
|
|
|
* @param string $data |
27
|
|
|
* @throws \UnexpectedValueException |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
130 |
|
public static function urlDecode($data) { |
31
|
130 |
|
$data = strtr($data, "-_", "+/"); |
32
|
130 |
|
switch (strlen($data) % 4) { |
33
|
130 |
|
case 0: |
34
|
78 |
|
break; |
35
|
112 |
|
case 2: |
36
|
80 |
|
$data .= "=="; |
37
|
80 |
|
break; |
38
|
68 |
|
case 3: |
39
|
67 |
|
$data .= "="; |
40
|
67 |
|
break; |
41
|
1 |
|
default: |
42
|
1 |
|
throw new \UnexpectedValueException("Malformed base64url encoding."); |
43
|
1 |
|
} |
44
|
129 |
|
return self::decode($data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check whether string is validly base64url encoded. |
49
|
|
|
* |
50
|
|
|
* @link https://en.wikipedia.org/wiki/Base64#URL_applications |
51
|
|
|
* @param string $data |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
112 |
|
public static function isValidURLEncoding($data) { |
55
|
112 |
|
return preg_match('#^[A-Za-z0-9\-_]*$#', $data) == 1; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Encode a string in base64. |
60
|
|
|
* |
61
|
|
|
* @link https://tools.ietf.org/html/rfc4648#section-4 |
62
|
|
|
* @param string $data |
63
|
|
|
* @throws \RuntimeException If encoding fails |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
167 |
View Code Duplication |
public static function encode($data) { |
|
|
|
|
67
|
167 |
|
$ret = @base64_encode($data); |
68
|
167 |
|
if (!is_string($ret)) { |
69
|
1 |
|
$err = error_get_last(); |
70
|
1 |
|
$msg = isset($err) ? $err["message"] : "base64_encode() failed."; |
71
|
1 |
|
throw new \RuntimeException($msg); |
72
|
|
|
} |
73
|
166 |
|
return $ret; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Decode a string from base64 encoding. |
78
|
|
|
* |
79
|
|
|
* @link https://tools.ietf.org/html/rfc4648#section-4 |
80
|
|
|
* @param string $data |
81
|
|
|
* @throws \RuntimeException If decoding fails |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
131 |
|
public static function decode($data) { |
85
|
131 |
|
$ret = base64_decode($data, true); |
86
|
131 |
|
if (!is_string($ret)) { |
87
|
1 |
|
$err = error_get_last(); |
88
|
1 |
|
$msg = isset($err) ? $err["message"] : "base64_decode() failed."; |
89
|
1 |
|
throw new \RuntimeException($msg); |
90
|
|
|
} |
91
|
130 |
|
return $ret; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check whether string is validly base64 encoded. |
96
|
|
|
* |
97
|
|
|
* @link https://tools.ietf.org/html/rfc4648#section-4 |
98
|
|
|
* @param string $data |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
4 |
|
public static function isValid($data) { |
102
|
4 |
|
return preg_match('#^[A-Za-z0-9+/]*={0,2}$#', $data) == 1; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.