Completed
Pull Request — master (#57)
by
unknown
04:58
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
use DateTime;
6
7
/**
8
 * Google Token Generator.
9
 *
10
 * Thanks to @helen5106 and @tehmaestro and few other cool guys
11
 * at https://github.com/Stichoza/google-translate-php/issues/32
12
 */
13
class GoogleTokenGenerator implements TokenProviderInterface
14
{
15
    /**
16
     * Generate and return a token.
17
     *
18
     * @param string $source Source language
19
     * @param string $target Target language
20
     * @param string $text   Text to translate
21
     *
22
     * @return mixed A token
23
     */
24
    public function generateToken($source, $target, $text)
25
    {
26
        return $this->TL($text);
27
    }
28
29
    /**
30
     * Generate a valid Google Translate request token.
31
     *
32
     * @param string $a text to translate
33
     *
34
     * @return string
35
     */
36
    private function TL($a)
37
    {
38
        $tkk = $this->TKK();
39
        $b = $tkk[0];
40
41
        for ($d = [], $e = 0, $f = 0; $f < mb_strlen($a, 'UTF-8'); $f++) {
42
            $g = $this->charCodeAt($a, $f);
43
            if (128 > $g) {
44
                $d[$e++] = $g;
45
            } else {
46
                if (2048 > $g) {
47
                    $d[$e++] = $g >> 6 | 192;
48
                } else {
49
                    if (55296 == ($g & 64512) && $f + 1 < mb_strlen($a, 'UTF-8') && 56320 == ($this->charCodeAt($a, $f + 1) & 64512)) {
50
                        $g = 65536 + (($g & 1023) << 10) + ($this->charCodeAt($a, ++$f) & 1023);
51
                        $d[$e++] = $g >> 18 | 240;
52
                        $d[$e++] = $g >> 12 & 63 | 128;
53
                    } else {
54
                        $d[$e++] = $g >> 12 | 224;
55
                        $d[$e++] = $g >> 6 & 63 | 128;
56
                    }
57
                }
58
                $d[$e++] = $g & 63 | 128;
59
            }
60
        }
61
        $a = $b;
62
        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...
63
            $a += $d[$e];
64
            $a = $this->RL($a, '+-a^+6');
65
        }
66
        $a = $this->RL($a, '+-3^+b+-f');
67
        $a ^= $tkk[1];
68
        if (0 > $a) {
69
            $a = ($a & 2147483647) + 2147483648;
70
        }
71
        $a = fmod($a, pow(10, 6));
72
73
        return $a.'.'.($a ^ $b);
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    private function TKK()
80
    {
81
        return ['406398', (561666268 + 1526272306)];
82
    }
83
84
    /**
85
     * Process token data by applying multiple operations.
86
     *
87
     * @param $a
88
     * @param $b
89
     *
90
     * @return int
91
     */
92
    private function RL($a, $b)
93
    {
94
        for ($c = 0; $c < strlen($b) - 2; $c += 3) {
95
            $d = $b{$c + 2};
96
            $d = $d >= 'a' ? $this->charCodeAt($d, 0) - 87 : intval($d);
97
            $d = $b{$c + 1}
98
            == '+' ? $this->shr32($a, $d) : $a << $d;
99
            $a = $b{$c}
100
            == '+' ? ($a + $d & 4294967295) : $a ^ $d;
101
        }
102
103
        return $a;
104
    }
105
106
    /**
107
     * Crypto function.
108
     *
109
     * @param $x
110
     * @param $bits
111
     *
112
     * @return number
113
     */
114
    private function shr32($x, $bits)
115
    {
116
        if ($bits <= 0) {
117
            return $x;
118
        }
119
        if ($bits >= 32) {
120
            return 0;
121
        }
122
        $bin = decbin($x);
123
        $l = strlen($bin);
124
        if ($l > 32) {
125
            $bin = substr($bin, $l - 32, 32);
126
        } elseif ($l < 32) {
127
            $bin = str_pad($bin, 32, '0', STR_PAD_LEFT);
128
        }
129
130
        return bindec(str_pad(substr($bin, 0, 32 - $bits), 32, '0', STR_PAD_LEFT));
131
    }
132
133
    /**
134
     * Get the Unicode of the character at the specified index in a string.
135
     *
136
     * @param string $str
137
     * @param int    $index
138
     *
139
     * @return null|number
140
     */
141
    private function charCodeAt($str, $index)
142
    {
143
        $char = mb_substr($str, $index, 1, 'UTF-8');
144
        if (mb_check_encoding($char, 'UTF-8')) {
145
            $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
146
            $result = hexdec(bin2hex($ret));
147
148
            return $result;
149
        }
150
151
        return null;
152
    }
153
}
154