1 | <?php |
||
8 | class Helper |
||
9 | { |
||
10 | /** |
||
11 | * Generate a Gravatar URL. |
||
12 | * |
||
13 | * @see https://gravatar.com/site/implement/images/ |
||
14 | * |
||
15 | * @param string $email |
||
16 | * @param int $size |
||
17 | * @param string $default |
||
18 | * @param string $rating |
||
19 | * @return string |
||
20 | */ |
||
21 | public static function gravatar($email, $size = 120, $default = null, $rating = null) |
||
41 | |||
42 | /** |
||
43 | * Add JSON type to the "Accept" header for the current request. |
||
44 | * |
||
45 | * @see https://laravel-china.org/topics/3430/modify-request-headers-incomplete-raiders |
||
46 | * |
||
47 | * @param callable $determination |
||
48 | * @param callable $callback |
||
49 | * @return mixed |
||
50 | */ |
||
51 | public static function addAcceptableJsonType($determination = null, $callback = null) |
||
52 | { |
||
53 | return app()->rebinding('request', function ($app, $request) use ($determination, $callback) { |
||
54 | if (is_null($determination) || call_user_func($determination, $request)) { |
||
55 | $accept = $request->headers->get('Accept'); |
||
56 | |||
57 | if (! str_contains($accept, ['/json', '+json'])) { |
||
58 | $accept = rtrim('application/json,'.$accept, ','); |
||
59 | |||
60 | $request->headers->set('Accept', $accept); |
||
61 | $request->server->set('HTTP_ACCEPT', $accept); |
||
62 | $_SERVER['HTTP_ACCEPT'] = $accept; |
||
63 | |||
64 | if ($callback) { |
||
65 | call_user_func($callback, $request); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | }); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Get file extension for MIME type. |
||
74 | * |
||
75 | * @param string $mimeType |
||
76 | * @param string $prefix |
||
77 | * @return string|null |
||
78 | */ |
||
79 | public static function fileExtensionForMimeType($mimeType, $prefix = '') |
||
91 | |||
92 | /** |
||
93 | * Convert an iOS platform to the device model name. |
||
94 | * |
||
95 | * @see https://www.theiphonewiki.com/wiki/Models |
||
96 | * @see https://support.hockeyapp.net/kb/client-integration-ios-mac-os-x-tvos/ios-device-types |
||
97 | * |
||
98 | * @param string $platform |
||
99 | * @return string |
||
100 | */ |
||
101 | public static function iDeviceModel($platform) |
||
188 | |||
189 | /** |
||
190 | * Get the mail homepage. |
||
191 | * |
||
192 | * @param string $address |
||
193 | * @return string|null |
||
194 | */ |
||
195 | public static function mailHomepage($address) |
||
215 | |||
216 | /** |
||
217 | * Encrypt ASCII string via XOR. |
||
218 | * |
||
219 | * @param string $text |
||
220 | * @param string|null $key |
||
221 | * @return string |
||
222 | */ |
||
223 | public static function sampleEncrypt($text, $key = null) |
||
224 | { |
||
225 | $text = (string) $text; |
||
226 | if (is_null($key)) { |
||
227 | $key = app('encrypter')->getKey(); |
||
228 | } |
||
229 | |||
230 | // 生成随机字符串 |
||
231 | $random = str_random(strlen($text)); |
||
232 | |||
233 | // 按字符拼接:随机字符串 + 随机字符串异或原文 |
||
234 | $tmp = static::sampleEncryption($text, $random, function ($a, $b) { |
||
235 | return $b.($a ^ $b); |
||
236 | }); |
||
237 | |||
238 | // 异或 $tmp 和 $key |
||
239 | $result = static::sampleEncryption($tmp, $key); |
||
240 | |||
241 | return urlsafe_base64_encode($result); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Decrypt string via XOR. |
||
246 | * |
||
247 | * @param string $text |
||
248 | * @param string|null $key |
||
249 | * @return string |
||
250 | */ |
||
251 | public static function sampleDecrypt($text, $key = null) |
||
266 | |||
267 | /** |
||
268 | * Do a sample XOR encryption. |
||
269 | * |
||
270 | * @param string $text |
||
271 | * @param string $key |
||
272 | * @param \Closure|null $callback `($a, $b, $index)` |
||
273 | * @return string |
||
274 | */ |
||
275 | protected static function sampleEncryption($text, $key, $callback = null) |
||
303 | |||
304 | /** |
||
305 | * Convert an integer to a string. |
||
306 | * It is useful to shorten a database ID to URL keyword, e.g. 54329 to 'xkE'. |
||
307 | * |
||
308 | * @param int $number |
||
309 | * @param string|null $characters |
||
310 | * @return string |
||
311 | */ |
||
312 | public static function int2string($number, string $characters = null) |
||
334 | |||
335 | /** |
||
336 | * Convert an `int2string` generated string back to an integer. |
||
337 | * |
||
338 | * @param string $string |
||
339 | * @param string|null $characters |
||
340 | * @return int |
||
341 | */ |
||
342 | public static function string2int($string, string $characters = null) |
||
360 | } |
||
361 |