-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.php
112 lines (100 loc) · 2.63 KB
/
Helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace mrwadson;
class Helper
{
/**
* Check if passed string has at least one bad word
*
* @param array $words
* @param string $string
*
* @return int
*/
public static function matchBadWords(array $words, string $string): int
{
if (preg_match_all('/\b(' . implode('|', $words) . ')\b/i', $string, $matches)) {
return count(array_unique($matches[0]));
}
return false;
}
/**
* Check if passed value is URL
*
* @param string $url
* @param array $flags
*
* @return bool
*/
public static function isUrl(string $url, array $flags = []): bool
{
return filter_var($url, FILTER_VALIDATE_URL, $flags) !== false;
}
/**
* Check if passed value is domain
*
* @param string $domain
* @param bool $includeTopLevelDomain
*
* @return bool
*/
public static function isDomain(string $domain, bool $includeTopLevelDomain = true): bool
{
return filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) !== false
&& (!$includeTopLevelDomain || strpos($domain, '.', true) !== false);
}
/**
* Get last redirection URL from the passed url parameter.
*
* @param string $url
* @param int $maxRedirects
*
* @return string|null
*/
public static function getRedirectLocation(string $url, int $maxRedirects = 3): ?string
{
$ch = curl_init($url);
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => $maxRedirects,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 120,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_AUTOREFERER => true,
];
curl_setopt_array($ch, $options);
curl_exec($ch);
if ($curlErr = curl_error($ch)) {
echo "$curlErr\n";
return null;
}
$header = curl_getinfo($ch);
curl_close($ch);
return $header['url'] ?? null;
}
/**
* Get the cache data from the file
*
* @param string $file
*
* @return array|null
*/
public static function getCache(string $file): ?array
{
if (file_exists($file)) {
return json_decode(file_get_contents($file), true);
}
return null;
}
/**
* Save the cache data to the file
*
* @param string $file
* @param array $data
*
* @return void
*/
public static function saveCache(string $file, array $data): void
{
file_put_contents($file, json_encode($data));
}
}