1 | <?php |
||||
2 | |||||
3 | namespace Translation\Tokens; |
||||
4 | |||||
5 | /** |
||||
6 | * Google Token Generator. |
||||
7 | * |
||||
8 | * Thanks to @helen5106 and @tehmaestro and few other cool guys |
||||
9 | * at https://github.com/Stichoza/google-translate-php/issues/32 |
||||
10 | */ |
||||
11 | class GoogleTokenGenerator implements TokenProviderInterface |
||||
12 | { |
||||
13 | /** |
||||
14 | * Generate and return a token. |
||||
15 | * |
||||
16 | * @param string $source Source language |
||||
17 | * @param string $target Target language |
||||
18 | * @param string $text Text to translate |
||||
19 | * @return string Token |
||||
20 | */ |
||||
21 | public function generateToken(string $source, string $target, string $text) : string |
||||
22 | { |
||||
23 | return $this->TL($text); |
||||
24 | } |
||||
25 | |||||
26 | /** |
||||
27 | * Generate a valid Google Translate request token. |
||||
28 | * |
||||
29 | * @param string $a text to translate |
||||
30 | * |
||||
31 | * @return string |
||||
32 | */ |
||||
33 | private function TL($a) |
||||
34 | { |
||||
35 | $tkk = $this->TKK(); |
||||
36 | $b = $tkk[0]; |
||||
37 | |||||
38 | for ($d = [], $e = 0, $f = 0; $f < $this->JS_length($a); $f++) { |
||||
39 | $g = $this->JS_charCodeAt($a, $f); |
||||
40 | if (128 > $g) { |
||||
41 | $d[$e++] = $g; |
||||
42 | } else { |
||||
43 | if (2048 > $g) { |
||||
44 | $d[$e++] = $g >> 6 | 192; |
||||
45 | } else { |
||||
46 | if (55296 == ($g & 64512) && $f + 1 < $this->JS_length($a) && 56320 == ($this->JS_charCodeAt($a, $f + 1) & 64512)) { |
||||
47 | $g = 65536 + (($g & 1023) << 10) + ($this->JS_charCodeAt($a, ++$f) & 1023); |
||||
48 | $d[$e++] = $g >> 18 | 240; |
||||
49 | $d[$e++] = $g >> 12 & 63 | 128; |
||||
50 | } else { |
||||
51 | $d[$e++] = $g >> 12 | 224; |
||||
52 | } |
||||
53 | $d[$e++] = $g >> 6 & 63 | 128; |
||||
54 | } |
||||
55 | $d[$e++] = $g & 63 | 128; |
||||
56 | } |
||||
57 | } |
||||
58 | $a = $b; |
||||
59 | for ($e = 0; $e < count($d); $e++) { |
||||
0 ignored issues
–
show
|
|||||
60 | $a += $d[$e]; |
||||
61 | $a = $this->RL($a, '+-a^+6'); |
||||
62 | } |
||||
63 | $a = $this->RL($a, '+-3^+b+-f'); |
||||
0 ignored issues
–
show
It seems like
$a can also be of type string ; however, parameter $a of Translation\Tokens\GoogleTokenGenerator::RL() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
64 | $a ^= $tkk[1] ? $tkk[1] + 0 : 0; |
||||
65 | if (0 > $a) { |
||||
66 | $a = ($a & 2147483647) + 2147483648; |
||||
67 | } |
||||
68 | $a = fmod($a, pow(10, 6)); |
||||
69 | |||||
70 | return $a.'.'.($a ^ $b); |
||||
71 | } |
||||
72 | |||||
73 | /** |
||||
74 | * @return array |
||||
75 | */ |
||||
76 | private function TKK() |
||||
77 | { |
||||
78 | return ['406398', (561666268 + 1526272306)]; |
||||
79 | } |
||||
80 | |||||
81 | /** |
||||
82 | * Process token data by applying multiple operations. |
||||
83 | * (Params are safe, no need for multibyte functions) |
||||
84 | * |
||||
85 | * @param int $a |
||||
86 | * @param string $b |
||||
87 | * |
||||
88 | * @return int |
||||
89 | */ |
||||
90 | private function RL($a, $b) |
||||
91 | { |
||||
92 | for ($c = 0; $c < strlen($b) - 2; $c += 3) { |
||||
93 | $d = $b[$c + 2]; |
||||
94 | $d = 'a' <= $d ? ord($d[0]) - 87 : intval($d); |
||||
95 | $d = '+' == $b[$c + 1] ? $this->unsignedRightShift($a, $d) : $a << $d; |
||||
96 | $a = '+' == $b[$c] ? ($a + $d & 4294967295) : $a ^ $d; |
||||
97 | } |
||||
98 | |||||
99 | return $a; |
||||
100 | } |
||||
101 | |||||
102 | /** |
||||
103 | * Unsigned right shift implementation |
||||
104 | * https://msdn.microsoft.com/en-us/library/342xfs5s(v=vs.94).aspx |
||||
105 | * http://stackoverflow.com/a/43359819/2953830 |
||||
106 | * |
||||
107 | * @param $a |
||||
108 | * @param $b |
||||
109 | * |
||||
110 | * @return number |
||||
111 | */ |
||||
112 | private function unsignedRightShift(int $a, int $b) |
||||
113 | { |
||||
114 | if ($b >= 32 || $b < -32) { |
||||
115 | $m = (int)($b / 32); |
||||
116 | $b = $b - ($m * 32); |
||||
117 | } |
||||
118 | |||||
119 | if ($b < 0) { |
||||
120 | $b = 32 + $b; |
||||
121 | } |
||||
122 | |||||
123 | if ($b == 0) { |
||||
124 | return (($a >> 1) & 0x7fffffff) * 2 + (($a >> $b) & 1); |
||||
125 | } |
||||
126 | |||||
127 | if ($a < 0) { |
||||
128 | $a = ($a >> 1); |
||||
129 | $a &= 2147483647; |
||||
130 | $a |= 0x40000000; |
||||
131 | $a = ($a >> ($b - 1)); |
||||
132 | } else { |
||||
133 | $a = ($a >> $b); |
||||
134 | } |
||||
135 | |||||
136 | return $a; |
||||
137 | } |
||||
138 | |||||
139 | /** |
||||
140 | * Get JS charCodeAt equivalent result with UTF-16 encoding |
||||
141 | * |
||||
142 | * @param string $str |
||||
143 | * @param int $index |
||||
144 | * |
||||
145 | * @return number |
||||
146 | */ |
||||
147 | private function JS_charCodeAt($str, $index) |
||||
148 | { |
||||
149 | $utf16 = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8'); |
||||
150 | return ord($utf16[$index*2]) + (ord($utf16[$index*2+1]) << 8); |
||||
151 | } |
||||
152 | |||||
153 | /** |
||||
154 | * Get JS equivalent string length with UTF-16 encoding |
||||
155 | * |
||||
156 | * @param string $str |
||||
157 | * |
||||
158 | * @return number |
||||
159 | */ |
||||
160 | private function JS_length($str) |
||||
161 | { |
||||
162 | $utf16 = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8'); |
||||
163 | return strlen($utf16)/2; |
||||
0 ignored issues
–
show
It seems like
$utf16 can also be of type array ; however, parameter $string of strlen() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
164 | } |
||||
165 | } |
||||
166 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: