Completed
Push — master ( 1740a9...c9fbfb )
by Elf
03:29
created

Support::fakeApiToken()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
285
286
            $agent->setUserAgent(
287
                $app['request']->header('User-Agent')." client($client)"
288
            );
289
        });
290
    }
291
292
    /**
293
     * Set faked api token for the current request.
294
     *
295
     * @param  string|null  $appKey
296
     * @return void
297
     */
298
    public function fakeApiToken($appKey = null)
299
    {
300
        app()->rebinding('request', function ($app, $request) use ($appKey) {
301
            if ($request->hasHeader('X-API-TOKEN') || $request->has('_token')) {
302
                return;
303
            }
304
305
            $appKey = $appKey ?: app('api.client')->defaultAppKey();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $appKey, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
306
307
            foreach (app('api.token')->generateDataForKey($appKey) as $key => $value) {
308
                $request->headers->set('X-API-'.strtoupper($key), $value);
309
            }
310
        });
311
    }
312
}
313