Completed
Push — master ( bf72bf...5075c7 )
by Elf
04:00
created

Helper::gravatar()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 4
dl 0
loc 16
ccs 0
cts 14
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support;
4
5
use Illuminate\Http\Request;
6
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
7
8
class Helper
9
{
10
    /**
11
     * Trim strings for the request's inputs.
12
     *
13
     * @see https://gist.github.com/drakakisgeo/3bba2a2600b4c554f836#gistcomment-1744670
14
     *
15
     * @param  \Illuminate\Http\Request  $request
16
     * @return \Illuminate\Http\Request
17
     */
18
    public static function trimRequestInputs(Request $request)
19
    {
20
        $inputs = $request->input();
21
22
        array_walk_recursive($inputs, function (&$value) {
23
            $value = trim($value);
24
        });
25
26
        $request->merge($inputs);
27
28
        return $request;
29
    }
30
31
    /**
32
     * Get file extension for MIME type.
33
     *
34
     * @param  string  $mimeType
35
     * @param  string  $prefix
36
     * @return string|null
37
     */
38
    public static function fileExtensionForMimeType($mimeType, $prefix = '')
39
    {
40
        $extension = ExtensionGuesser::getInstance()->guess($mimeType);
41
42
        if (! is_null($extension)) {
43
            if ('jpeg' == $extension) {
44
                $extension = 'jpg';
45
            }
46
47
            return $prefix.$extension;
48
        }
49
    }
50
51
    /**
52
     * Convert an iOS platform to the device model name.
53
     *
54
     * @see https://www.theiphonewiki.com/wiki/Models
55
     * @see https://support.hockeyapp.net/kb/client-integration-ios-mac-os-x-tvos/ios-device-types
56
     *
57
     * @param  string  $platform
58
     * @return string
59
     */
60
    public static function iDeviceModel($platform)
61
    {
62
        $list = [
63
            'iPhone1,1' => 'iPhone',
64
            'iPhone1,2' => 'iPhone 3G',
65
            'iPhone2,1' => 'iPhone 3GS',
66
            'iPhone3,1' => 'iPhone 4',
67
            'iPhone3,2' => 'iPhone 4',
68
            'iPhone3,3' => 'iPhone 4',
69
            'iPhone4,1' => 'iPhone 4S',
70
            'iPhone5,1' => 'iPhone 5',
71
            'iPhone5,2' => 'iPhone 5',
72
            'iPhone5,3' => 'iPhone 5c',
73
            'iPhone5,4' => 'iPhone 5c',
74
            'iPhone6,1' => 'iPhone 5s',
75
            'iPhone6,2' => 'iPhone 5s',
76
            'iPhone7,1' => 'iPhone 6 Plus',
77
            'iPhone7,2' => 'iPhone 6',
78
            'iPhone8,1' => 'iPhone 6s',
79
            'iPhone8,2' => 'iPhone 6s Plus',
80
            'iPhone8,4' => 'iPhone SE',
81
            'iPhone9,1' => 'iPhone 7',
82
            'iPhone9,2' => 'iPhone 7 Plus',
83
            'iPhone9,3' => 'iPhone 7',
84
            'iPhone9,4' => 'iPhone 7 Plus',
85
86
            'iPod1,1' => 'iPod touch',
87
            'iPod2,1' => 'iPod touch 2G',
88
            'iPod3,1' => 'iPod touch 3G',
89
            'iPod4,1' => 'iPod touch 4G',
90
            'iPod5,1' => 'iPod touch 5G',
91
            'iPod7,1' => 'iPod touch 6G',
92
93
            'iPad1,1' => 'iPad',
94
            'iPad2,1' => 'iPad 2',
95
            'iPad2,2' => 'iPad 2',
96
            'iPad2,3' => 'iPad 2',
97
            'iPad2,4' => 'iPad 2',
98
            'iPad2,5' => 'iPad mini',
99
            'iPad2,6' => 'iPad mini',
100
            'iPad2,7' => 'iPad mini',
101
            'iPad3,1' => 'iPad 3',
102
            'iPad3,2' => 'iPad 3',
103
            'iPad3,3' => 'iPad 3',
104
            'iPad3,4' => 'iPad 4',
105
            'iPad3,5' => 'iPad 4',
106
            'iPad3,6' => 'iPad 4',
107
            'iPad4,1' => 'iPad Air',
108
            'iPad4,2' => 'iPad Air',
109
            'iPad4,3' => 'iPad Air',
110
            'iPad4,4' => 'iPad mini 2',
111
            'iPad4,5' => 'iPad mini 2',
112
            'iPad4,6' => 'iPad mini 2',
113
            'iPad4,7' => 'iPad mini 3',
114
            'iPad4,8' => 'iPad mini 3',
115
            'iPad4,9' => 'iPad mini 3',
116
            'iPad5,1' => 'iPad mini 4',
117
            'iPad5,2' => 'iPad mini 4',
118
            'iPad5,3' => 'iPad Air 2',
119
            'iPad5,4' => 'iPad Air 2',
120
            'iPad6,3' => 'iPad Pro',
121
            'iPad6,4' => 'iPad Pro',
122
            'iPad6,7' => 'iPad Pro',
123
            'iPad6,8' => 'iPad Pro',
124
125
            'AppleTV2,1' => 'Apple TV 2G',
126
            'AppleTV3,1' => 'Apple TV 3G',
127
            'AppleTV3,2' => 'Apple TV 3G',
128
            'AppleTV5,3' => 'Apple TV 4G',
129
130
            'Watch1,1' => 'Apple Watch',
131
            'Watch1,2' => 'Apple Watch',
132
            'Watch2,6' => 'Apple Watch Series 1',
133
            'Watch2,7' => 'Apple Watch Series 1',
134
            'Watch2,3' => 'Apple Watch Series 2',
135
            'Watch2,4' => 'Apple Watch Series 2',
136
137
            'i386' => 'Simulator',
138
            'x86_64' => 'Simulator',
139
        ];
140
141
        return isset($list[$platform]) ? $list[$platform] : $platform;
142
    }
143
144
    /**
145
     * Get the mail homepage.
146
     *
147
     * @param  string  $address
148
     * @return string|null
149
     */
150
    public static function mailHomepage($address)
151
    {
152
        if (preg_match('#@([a-z0-9.-]+)#i', $address, $match)) {
153
            $list = [
154
                'qq.com' => 'https://mail.qq.com',
155
                'vip.qq.com' => 'https://mail.qq.com',
156
                '126.com' => 'http://www.126.com',
157
                'gmail.com' => 'https://mail.google.com',
158
                '139.com' => 'http://mail.10086.cn',
159
                'wo.cn' => 'http://mail.wo.com.cn',
160
                'sina.com' => 'http://mail.sina.com.cn',
161
                'sina.cn' => 'http://mail.sina.com.cn',
162
                'vip.sina.com' => 'http://mail.sina.com.cn',
163
            ];
164
165
            $domain = strtolower($match[1]);
166
167
            return isset($list[$domain]) ? $list[$domain] : 'http://mail.'.$domain;
168
        }
169
    }
170
171
    /**
172
     * Encrypt ASCII string via XOR.
173
     *
174
     * @param  string  $text
175
     * @param  string|null  $key
176
     * @return string
177
     */
178
    public static function sampleEncrypt($text, $key = null)
179
    {
180
        $text = (string) $text;
181
        if (is_null($key)) {
182
            $key = app('encrypter')->getKey();
183
        }
184
185
        // 生成随机字符串
186
        $random = str_random(strlen($text));
187
188
        // 按字符拼接:随机字符串 + 随机字符串异或原文
189
        $tmp = static::sampleEncryption($text, $random, function ($a, $b) {
190
            return $b.($a ^ $b);
191
        });
192
193
        // 异或 $tmp 和 $key
194
        $result = static::sampleEncryption($tmp, $key);
195
196
        return urlsafe_base64_encode($result);
197
    }
198
199
    /**
200
     * Decrypt string via XOR.
201
     *
202
     * @param  string  $text
203
     * @param  string|null  $key
204
     * @return string
205
     */
206
    public static function sampleDecrypt($text, $key = null)
207
    {
208
        if (is_null($key)) {
209
            $key = app('encrypter')->getKey();
210
        }
211
212
        $tmp = static::sampleEncryption(urlsafe_base64_decode($text), $key);
213
        $tmpLength = strlen($tmp);
214
        $result = '';
215
        for ($i = 0; $i < $tmpLength, ($i + 1) < $tmpLength; $i += 2) {
216
            $result .= $tmp[$i] ^ $tmp[$i + 1];
217
        }
218
219
        return $result;
220
    }
221
222
    /**
223
     * Do a sample XOR encryption.
224
     *
225
     * @param  string  $text
226
     * @param  string  $key
227
     * @param  \Closure|null  $callback `($a, $b, $index)`
228
     * @return string
229
     */
230
    protected static function sampleEncryption($text, $key, $callback = null)
231
    {
232
        // 对 $text 和 $key 的每个字符进行运算。
233
        // $callback 为 null 时默认进行异或运算。
234
        // $callback 参数: 第 i 位 $text, 第 i 位 $key, 下标 i
235
        // $callback 返回 false 时,结束字符循环.
236
237
        $text = (string) $text;
238
        $key = (string) $key;
239
        $keyLength = strlen($key);
240
        if (is_null($callback)) {
241
            $callback = function ($a, $b) {
242
                return $a ^ $b;
243
            };
244
        }
245
246
        $result = '';
247
        $textLength = strlen($text);
248
        for ($i = 0; $i < $textLength; $i++) {
249
            $tmp = $callback($text[$i], $key[$i % $keyLength], $i);
250
            if (false === $tmp) {
251
                break;
252
            }
253
            $result .= $tmp;
254
        }
255
256
        return $result;
257
    }
258
259
    /**
260
     * Convert an integer to a string.
261
     * It is useful to shorten a database ID to URL keyword, e.g. 54329 to 'xkE'.
262
     *
263
     * @param  int $number
264
     * @param  string|null  $characters
265
     * @return string
266
     */
267
    public static function int2string($number, string $characters = null)
268
    {
269
        $number = (int) $number;
270
        if ($number < 0) {
271
            return '';
272
        }
273
274
        if (is_null($characters)) {
275
            $characters = config('support.int2string');
276
        }
277
278
        $charactersLength = strlen($characters);
279
        $string = '';
280
281
        while ($number >= $charactersLength) {
282
            $mod = (int) bcmod($number, $charactersLength);
283
            $number = (int) bcdiv($number, $charactersLength);
284
            $string = $characters[$mod].$string;
285
        }
286
287
        return $characters[$number].$string;
288
    }
289
290
    /**
291
     * Convert an `int2string` generated string back to an integer.
292
     *
293
     * @param  string  $string
294
     * @param  string|null  $characters
295
     * @return int
296
     */
297
    public static function string2int($string, string $characters = null)
298
    {
299
        $string = strrev($string);
300
        $stringLength = strlen($string);
301
302
        if (is_null($characters)) {
303
            $characters = config('support.int2string');
304
        }
305
        $charactersLength = strlen($characters);
306
307
        $number = 0;
308
        for ($i = 0; $i < $stringLength; $i++) {
309
            $index = strpos($characters, $string[$i]);
310
            $number = (int) bcadd($number, bcmul($index, bcpow($charactersLength, $i)));
311
        }
312
313
        return $number;
314
    }
315
}
316