Completed
Pull Request — master (#57)
by
unknown
02:20
created

GoogleTokenGenerator::TL()   D

Complexity

Conditions 9
Paths 20

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 1
Metric Value
c 4
b 3
f 1
dl 0
loc 39
rs 4.909
cc 9
eloc 29
nc 20
nop 1
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
                        $d[$e++] = $g >> 6 & 63 | 128;
54
                    }
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];
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
     *
85
     * @param $a
86
     * @param $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 = $d >= 'a' ? $this->charCodeAt($d, 0) - 87 : intval($d);
95
            $d = $b{$c + 1}
96
            == '+' ? $this->shr32($a, $d) : $a << $d;
97
            $a = $b{$c}
98
            == '+' ? ($a + $d & 4294967295) : $a ^ $d;
99
        }
100
101
        return $a;
102
    }
103
104
    /**
105
     * Crypto function.
106
     *
107
     * @param $x
108
     * @param $bits
109
     *
110
     * @return number
111
     */
112
    private function shr32($x, $bits)
113
    {
114
        if ($bits <= 0) {
115
            return $x;
116
        }
117
        if ($bits >= 32) {
118
            return 0;
119
        }
120
        $bin = decbin($x);
121
        $l = strlen($bin);
122
        if ($l > 32) {
123
            $bin = substr($bin, $l - 32, 32);
124
        } elseif ($l < 32) {
125
            $bin = str_pad($bin, 32, '0', STR_PAD_LEFT);
126
        }
127
128
        return bindec(str_pad(substr($bin, 0, 32 - $bits), 32, '0', STR_PAD_LEFT));
129
    }
130
131
    /**
132
     * Get the Unicode of the character at the specified index in a string.
133
     *
134
     * @param string $str
135
     * @param int    $index
136
     *
137
     * @return null|number
138
     */
139
    private function charCodeAt($str, $index)
140
    {
141
        $char = mb_substr($str, $index, 1, 'UTF-8');
142
        if (mb_check_encoding($char, 'UTF-8')) {
143
            $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
144
            $result = hexdec(bin2hex($ret));
145
146
            return $result;
147
        }
148
149
        return;
150
    }
151
}
152