GoogleTokenGenerator::generateToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Stichoza\GoogleTranslate\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
     *
20
     * @return mixed A token
21
     */
22
    public function generateToken($source, $target, $text)
23
    {
24
        return $this->TL($text);
25
    }
26
27
    /**
28
     * Generate a valid Google Translate request token.
29
     *
30
     * @param string $a text to translate
31
     *
32
     * @return string
33
     */
34
    private function TL($a)
35
    {
36
        $tkk = $this->TKK();
37
        $b = $tkk[0];
38
39
        for ($d = [], $e = 0, $f = 0; $f < mb_strlen($a, 'UTF-8'); $f++) {
40
            $g = $this->charCodeAt($a, $f);
41
            if (128 > $g) {
42
                $d[$e++] = $g;
43
            } else {
44
                if (2048 > $g) {
45
                    $d[$e++] = $g >> 6 | 192;
46
                } else {
47
                    if (55296 == ($g & 64512) && $f + 1 < mb_strlen($a, 'UTF-8') && 56320 == ($this->charCodeAt($a, $f + 1) & 64512)) {
48
                        $g = 65536 + (($g & 1023) << 10) + ($this->charCodeAt($a, ++$f) & 1023);
49
                        $d[$e++] = $g >> 18 | 240;
50
                        $d[$e++] = $g >> 12 & 63 | 128;
51
                    } else {
52
                        $d[$e++] = $g >> 12 | 224;
53
                    }
54
                    $d[$e++] = $g >> 6 & 63 | 128;
55
                }
56
                $d[$e++] = $g & 63 | 128;
57
            }
58
        }
59
        $a = $b;
60
        for ($e = 0; $e < count($d); $e++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

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:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
61
            $a += $d[$e];
62
            $a = $this->RL($a, '+-a^+6');
63
        }
64
        $a = $this->RL($a, '+-3^+b+-f');
65
        $a ^= $tkk[1] ? $tkk[1] + 0 : 0;
66
        if (0 > $a) {
67
            $a = ($a & 2147483647) + 2147483648;
68
        }
69
        $a = fmod($a, pow(10, 6));
70
71
        return $a.'.'.($a ^ $b);
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    private function TKK()
78
    {
79
        return ['406398', (561666268 + 1526272306)];
80
    }
81
82
    /**
83
     * Process token data by applying multiple operations.
84
     * (Params are safe, no need for multibyte functions)
85
     *
86
     * @param int $a
87
     * @param string $b
88
     *
89
     * @return int
90
     */
91
    private function RL($a, $b)
92
    {
93
        for ($c = 0; $c < strlen($b) - 2; $c += 3) {
94
            $d = $b[$c + 2];
95
            $d = 'a' <= $d ? ord($d[0]) - 87 : intval($d);
96
            $d = '+' == $b[$c + 1] ? $this->unsignedRightShift($a, $d) : $a << $d;
97
            $a = '+' == $b[$c] ? ($a + $d & 4294967295) : $a ^ $d;
98
        }
99
100
        return $a;
101
    }
102
103
    /**
104
     * Unsigned right shift implementation
105
     * https://msdn.microsoft.com/en-us/library/342xfs5s(v=vs.94).aspx
106
     * http://stackoverflow.com/a/43359819/2953830
107
     *
108
     * @param $a
109
     * @param $b
110
     *
111
     * @return number
112
     */
113
    private function unsignedRightShift($a, $b)
114
    {
115
        if ($b >= 32 || $b < -32) {
116
            $m = (int)($b / 32);
117
            $b = $b - ($m * 32);
118
        }
119
120
        if ($b < 0) {
121
            $b = 32 + $b;
122
        }
123
124
        if ($b == 0) {
125
            return (($a >> 1) & 0x7fffffff) * 2 + (($a >> $b) & 1);
126
        }
127
128
        if ($a < 0) {
129
            $a = ($a >> 1);
130
            $a &= 2147483647;
131
            $a |= 0x40000000;
132
            $a = ($a >> ($b - 1));
133
        } else { 
134
            $a = ($a >> $b);
135
        }
136
137
        return $a;
138
    }
139
140
    /**
141
     * Get the Unicode of the character at the specified index in a string.
142
     *
143
     * @param string $str
144
     * @param int    $index
145
     *
146
     * @return null|number
147
     */
148
    private function charCodeAt($str, $index)
149
    {
150
        $char = mb_substr($str, $index, 1, 'UTF-8');
151
        if (mb_check_encoding($char, 'UTF-8')) {
152
            $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
153
            $result = hexdec(bin2hex($ret));
154
155
            return $result;
156
        }
157
158
        return;
159
    }
160
}
161