Completed
Push — master ( 8925a7...a06fb4 )
by Elf
07:09
created

Helper::string2int()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 2
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support;
4
5
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
6
7
class Helper
8
{
9
    /**
10
     * Get file extension for MIME type.
11
     *
12
     * @param  string  $mimeType
13
     * @param  string  $prefix
14
     * @return string|null
15
     */
16
    public static function fileExtensionForMimeType($mimeType, $prefix = '')
17
    {
18
        $extension = ExtensionGuesser::getInstance()->guess($mimeType);
19
20
        if (! is_null($extension)) {
21
            if ('jpeg' == $extension) {
22
                $extension = 'jpg';
23
            }
24
25
            return $prefix.$extension;
26
        }
27
    }
28
29
    /**
30
     * Convert an iOS platform to the device model name.
31
     *
32
     * @see https://www.theiphonewiki.com/wiki/Models
33
     * @see https://support.hockeyapp.net/kb/client-integration-ios-mac-os-x-tvos/ios-device-types
34
     *
35
     * @param  string  $platform
36
     * @return string
37
     */
38
    public static function iDeviceModel($platform)
39
    {
40
        static $iDeviceModels = null;
41
42
        if (is_null($iDeviceModels)) {
43
            $iDeviceModels = [
44
                'i386' => 'Simulator',
45
                'x86_64' => 'Simulator',
46
47
                'iPhone1,1' => 'iPhone',
48
                'iPhone1,2' => 'iPhone 3G',
49
                'iPhone2,1' => 'iPhone 3GS',
50
                'iPhone3,1' => 'iPhone 4',
51
                'iPhone3,2' => 'iPhone 4',
52
                'iPhone3,3' => 'iPhone 4',
53
                'iPhone4,1' => 'iPhone 4S',
54
                'iPhone5,1' => 'iPhone 5',
55
                'iPhone5,2' => 'iPhone 5',
56
                'iPhone5,3' => 'iPhone 5c',
57
                'iPhone5,4' => 'iPhone 5c',
58
                'iPhone6,1' => 'iPhone 5s',
59
                'iPhone6,2' => 'iPhone 5s',
60
                'iPhone7,1' => 'iPhone 6 Plus',
61
                'iPhone7,2' => 'iPhone 6',
62
                'iPhone8,1' => 'iPhone 6s',
63
                'iPhone8,2' => 'iPhone 6s Plus',
64
                'iPhone8,4' => 'iPhone SE',
65
                'iPhone9,1' => 'iPhone 7',
66
                'iPhone9,2' => 'iPhone 7 Plus',
67
                'iPhone9,3' => 'iPhone 7',
68
                'iPhone9,4' => 'iPhone 7 Plus',
69
70
                'iPod1,1' => 'iPod touch',
71
                'iPod2,1' => 'iPod touch 2G',
72
                'iPod3,1' => 'iPod touch 3G',
73
                'iPod4,1' => 'iPod touch 4G',
74
                'iPod5,1' => 'iPod touch 5G',
75
                'iPod7,1' => 'iPod touch 6G',
76
77
                'iPad1,1' => 'iPad',
78
                'iPad2,1' => 'iPad 2',
79
                'iPad2,2' => 'iPad 2',
80
                'iPad2,3' => 'iPad 2',
81
                'iPad2,4' => 'iPad 2',
82
                'iPad2,5' => 'iPad mini',
83
                'iPad2,6' => 'iPad mini',
84
                'iPad2,7' => 'iPad mini',
85
                'iPad3,1' => 'iPad 3',
86
                'iPad3,2' => 'iPad 3',
87
                'iPad3,3' => 'iPad 3',
88
                'iPad3,4' => 'iPad 4',
89
                'iPad3,5' => 'iPad 4',
90
                'iPad3,6' => 'iPad 4',
91
                'iPad4,1' => 'iPad Air',
92
                'iPad4,2' => 'iPad Air',
93
                'iPad4,3' => 'iPad Air',
94
                'iPad4,4' => 'iPad mini 2',
95
                'iPad4,5' => 'iPad mini 2',
96
                'iPad4,6' => 'iPad mini 2',
97
                'iPad4,7' => 'iPad mini 3',
98
                'iPad4,8' => 'iPad mini 3',
99
                'iPad4,9' => 'iPad mini 3',
100
                'iPad5,1' => 'iPad mini 4',
101
                'iPad5,2' => 'iPad mini 4',
102
                'iPad5,3' => 'iPad Air 2',
103
                'iPad5,4' => 'iPad Air 2',
104
                'iPad6,3' => 'iPad Pro',
105
                'iPad6,4' => 'iPad Pro',
106
                'iPad6,7' => 'iPad Pro',
107
                'iPad6,8' => 'iPad Pro',
108
109
                'AppleTV2,1' => 'Apple TV 2G',
110
                'AppleTV3,1' => 'Apple TV 3G',
111
                'AppleTV3,2' => 'Apple TV 3G',
112
                'AppleTV5,3' => 'Apple TV 4G',
113
114
                'Watch1,1' => 'Apple Watch',
115
                'Watch1,2' => 'Apple Watch',
116
                'Watch2,6' => 'Apple Watch Series 1',
117
                'Watch2,7' => 'Apple Watch Series 1',
118
                'Watch2,3' => 'Apple Watch Series 2',
119
                'Watch2,4' => 'Apple Watch Series 2',
120
            ];
121
        }
122
123
        return isset($iDeviceModels[$platform]) ? $iDeviceModels[$platform] : $platform;
124
    }
125
126
    /**
127
     * Get the mail homepage.
128
     *
129
     * @param  string  $address
130
     * @return string|null
131
     */
132
    public static function mailHomepage($address)
133
    {
134
        if (preg_match('#@([a-z0-9.-]+)#i', $address, $match)) {
135
            $list = [
136
                'qq.com' => 'https://mail.qq.com',
137
                'vip.qq.com' => 'https://mail.qq.com',
138
                '126.com' => 'http://www.126.com',
139
                'gmail.com' => 'https://mail.google.com',
140
                '139.com' => 'http://mail.10086.cn',
141
                'wo.cn' => 'http://mail.wo.com.cn',
142
                'sina.com' => 'http://mail.sina.com.cn',
143
                'sina.cn' => 'http://mail.sina.com.cn',
144
                'vip.sina.com' => 'http://mail.sina.com.cn',
145
            ];
146
147
            $domain = strtolower($match[1]);
148
149
            return isset($list[$domain]) ? $list[$domain] : 'http://mail.'.$domain;
150
        }
151
    }
152
153
    /**
154
     * Encrypt ASCII string via XOR.
155
     *
156
     * @param  string  $text
157
     * @param  string|null  $key
158
     * @return string
159
     */
160
    public static function sampleEncrypt($text, $key = null)
161
    {
162
        $text = (string) $text;
163
        if (is_null($key)) {
164
            $key = app('encrypter')->getKey();
165
        }
166
167
        // 生成随机字符串
168
        $random = str_random(strlen($text));
169
170
        // 按字符拼接:随机字符串 + 随机字符串异或原文
171
        $tmp = static::sampleEncryption($text, $random, function ($a, $b) {
172
            return $b.($a ^ $b);
173
        });
174
175
        // 异或 $tmp 和 $key
176
        $result = static::sampleEncryption($tmp, $key);
177
178
        return urlsafe_base64_encode($result);
179
    }
180
181
    /**
182
     * Decrypt string via XOR.
183
     *
184
     * @param  string  $text
185
     * @param  string|null  $key
186
     * @return string
187
     */
188
    public static function sampleDecrypt($text, $key = null)
189
    {
190
        if (is_null($key)) {
191
            $key = app('encrypter')->getKey();
192
        }
193
194
        $tmp = static::sampleEncryption(urlsafe_base64_decode($text), $key);
195
        $tmpLength = strlen($tmp);
196
        $result = '';
197
        for ($i = 0; $i < $tmpLength, ($i + 1) < $tmpLength; $i += 2) {
198
            $result .= $tmp[$i] ^ $tmp[$i + 1];
199
        }
200
201
        return $result;
202
    }
203
204
    /**
205
     * Do a sample XOR encryption.
206
     *
207
     * @param  string  $text
208
     * @param  string  $key
209
     * @param  \Closure|null  $callback `($a, $b, $index)`
210
     * @return string
211
     */
212
    protected static function sampleEncryption($text, $key, $callback = null)
213
    {
214
        // 对 $text 和 $key 的每个字符进行运算。
215
        // $callback 为 null 时默认进行异或运算。
216
        // $callback 参数: 第 i 位 $text, 第 i 位 $key, 下标 i
217
        // $callback 返回 false 时,结束字符循环.
218
219
        $text = (string) $text;
220
        $key = (string) $key;
221
        $keyLength = strlen($key);
222
        if (is_null($callback)) {
223
            $callback = function ($a, $b) {
224
                return $a ^ $b;
225
            };
226
        }
227
228
        $result = '';
229
        $textLength = strlen($text);
230
        for ($i = 0; $i < $textLength; $i++) {
231
            $tmp = $callback($text[$i], $key[$i % $keyLength], $i);
232
            if (false === $tmp) {
233
                break;
234
            }
235
            $result .= $tmp;
236
        }
237
238
        return $result;
239
    }
240
}
241