-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelper.php
108 lines (91 loc) · 3.67 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
<?php
namespace AndreasMaximilianGerum\DisposableMailDetection;
class Helper
{
private const DISPOSABLE_MAILS_LOOKUP_PATH = __DIR__ . '/generated/disposable-mails-lookup.inc.php';
private const INDENT_STRING = ' ';
/**
* @param array $disposableMailAddresses
*/
public function writeLookup(array $disposableMailAddresses): void
{
if (file_exists(self::DISPOSABLE_MAILS_LOOKUP_PATH)) {
$alreadyExistingLookup = include self::DISPOSABLE_MAILS_LOOKUP_PATH;
$disposableMailAddresses = array_merge($disposableMailAddresses, array_keys($alreadyExistingLookup));
}
file_put_contents(
self::DISPOSABLE_MAILS_LOOKUP_PATH,
'<?php'
. PHP_EOL . '// !!! CAUTION !!!'
. PHP_EOL . '// >>> This file was automatized generated and should not be, in any case, manually modified! <<<'
. PHP_EOL . 'return ['
. PHP_EOL . $this->processIndented(
$this->prepareDisposableMailLookup($disposableMailAddresses)
) . '];'
);
}
/**
* @param array $disposableMailAddresses
*
* @return array
*/
private function prepareDisposableMailLookup(array $disposableMailAddresses): array
{
$whitelistedMailProviders = include __DIR__ . '/disposable-mails-whitelist.inc.php';
$preparedLookup = [];
foreach ($disposableMailAddresses as $disposableMailAddress) {
$disposableMailAddress = strtolower(trim($disposableMailAddress));
$disposableMailAddress = trim($disposableMailAddress, '@');
// Performance is not an issue while generating the lookup, so it is sufficient to just define the providers as value based and thus perform an in_array check
if (!in_array($disposableMailAddress, $whitelistedMailProviders, true)) {
$preparedLookup[$disposableMailAddress] = true;
}
}
ksort($preparedLookup);
return $preparedLookup;
}
/**
* @param array $config
* @param int $indentLevel
*
* @return string
*/
private function processIndented(array $config, &$indentLevel = 1): string
{
$arrayString = '';
foreach ($config as $key => $value) {
$arrayString .= str_repeat(self::INDENT_STRING, $indentLevel);
$arrayString .= (is_int($key) ? $key : "'" . addslashes($key) . "'") . ' => ';
if (is_array($value)) {
if ($value === []) {
$arrayString .= '[]' . ",\n";
} else {
$indentLevel++;
$arrayString .= '[' . "\n" . $this->processIndented($value, $indentLevel) . str_repeat(self::INDENT_STRING, --$indentLevel) . "],\n";
}
} else if (is_object($value) || is_string($value)) {
$arrayString .= var_export($value, true) . ",\n";
} else if (is_bool($value)) {
$arrayString .= ($value ? 'true' : 'false') . ",\n";
} else if ($value === null) {
$arrayString .= "null,\n";
} else {
$arrayString .= $value . ",\n";
}
}
return $arrayString;
}
public function fetchContent($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}