|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_bans\Bans; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_bans\BanException; |
|
7
|
|
|
use kalanis\kw_bans\Interfaces\IIpTypes; |
|
8
|
|
|
use kalanis\kw_bans\Interfaces\IKBTranslations; |
|
9
|
|
|
use kalanis\kw_bans\Sources; |
|
10
|
|
|
use kalanis\kw_bans\Traits\TLang; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Factory |
|
15
|
|
|
* @package kalanis\kw_bans\Bans |
|
16
|
|
|
*/ |
|
17
|
|
|
class Factory |
|
18
|
|
|
{ |
|
19
|
|
|
use TLang; |
|
20
|
|
|
|
|
21
|
|
|
protected const PREG_IP4 = '#[0-9\./\*]+#i'; |
|
22
|
|
|
protected const PREG_IP6 = '#[0-9a-f:/\*]+#i'; |
|
23
|
|
|
protected const PREG_NAME = '#[\*\?\:;\\//]#i'; |
|
24
|
|
|
|
|
25
|
14 |
|
public function __construct(?IKBTranslations $lang = null) |
|
26
|
|
|
{ |
|
27
|
14 |
|
$this->setIKbLang($lang); |
|
28
|
14 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param int $type |
|
32
|
|
|
* @param Sources\ASources $sources |
|
33
|
|
|
* @throws BanException |
|
34
|
|
|
* @return ABan |
|
35
|
|
|
*/ |
|
36
|
13 |
|
public function getBan(int $type, Sources\ASources $sources): ABan |
|
37
|
|
|
{ |
|
38
|
13 |
|
switch ($type) { |
|
39
|
|
|
case IIpTypes::TYPE_NAME: |
|
40
|
6 |
|
return new Clearing($sources, $this->getIKbLang()); |
|
41
|
|
|
case IIpTypes::TYPE_BASIC: |
|
42
|
6 |
|
return new Basic($sources, $this->getIKbLang()); |
|
43
|
|
|
case IIpTypes::TYPE_IP_4: |
|
44
|
5 |
|
return new IP4($sources, $this->getIKbLang()); |
|
45
|
|
|
case IIpTypes::TYPE_IP_6: |
|
46
|
2 |
|
return new IP6($sources, $this->getIKbLang()); |
|
47
|
|
|
case IIpTypes::TYPE_NONE: |
|
48
|
|
|
default: |
|
49
|
1 |
|
throw new BanException($this->getIKbLang()->ikbUnknownType()); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string|array<string>|array<int, string>|Sources\ASources $source |
|
55
|
|
|
* @throws BanException |
|
56
|
|
|
* @return ABan |
|
57
|
|
|
* Filtering has been done by check if there is something left after matching |
|
58
|
|
|
*/ |
|
59
|
9 |
|
public function whichType($source): ABan |
|
60
|
|
|
{ |
|
61
|
9 |
|
$source = $this->determineSource($source); |
|
62
|
8 |
|
if ($this->emptyContent($source)) { |
|
63
|
1 |
|
return $this->getBan(IIpTypes::TYPE_BASIC, $source); |
|
64
|
8 |
|
} elseif ($this->containsIp4($source)) { |
|
65
|
4 |
|
return $this->getBan(IIpTypes::TYPE_IP_4, $source); |
|
66
|
7 |
|
} elseif ($this->containsIp6($source)) { |
|
67
|
1 |
|
return $this->getBan(IIpTypes::TYPE_IP_6, $source); |
|
68
|
6 |
|
} elseif ($this->containsNames($source)) { |
|
69
|
5 |
|
return $this->getBan(IIpTypes::TYPE_NAME, $source); |
|
70
|
|
|
} else { |
|
71
|
4 |
|
return $this->getBan(IIpTypes::TYPE_BASIC, $source); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string|array<string>|array<int, string>|Sources\ASources $source |
|
77
|
|
|
* @throws BanException |
|
78
|
|
|
* @return Sources\ASources |
|
79
|
|
|
*/ |
|
80
|
9 |
|
protected function determineSource($source): Sources\ASources |
|
81
|
|
|
{ |
|
82
|
9 |
|
if ($source instanceof Sources\ASources) { |
|
83
|
1 |
|
return $source; |
|
84
|
|
|
} |
|
85
|
9 |
|
if (is_array($source)) { |
|
86
|
8 |
|
return new Sources\Arrays(array_map('strval', $source)); |
|
87
|
|
|
} |
|
88
|
2 |
|
if (is_string($source) && is_file($source)) { |
|
89
|
1 |
|
return new Sources\File($source); |
|
90
|
|
|
} |
|
91
|
1 |
|
throw new BanException($this->getIKbLang()->ikbUnknownFormat()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
8 |
|
protected function emptyContent(Sources\ASources $sources): bool |
|
95
|
|
|
{ |
|
96
|
8 |
|
return empty( $sources->getRecords() ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
8 |
|
protected function containsIp4(Sources\ASources $sources): bool |
|
100
|
|
|
{ |
|
101
|
8 |
|
return empty( array_filter($sources->getRecords(), [$this, 'checkForNotIp4']) ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
8 |
|
public function checkForNotIp4(string $content): bool |
|
105
|
|
|
{ |
|
106
|
8 |
|
return !empty(preg_replace(static::PREG_IP4, '', $content)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
7 |
|
protected function containsIp6(Sources\ASources $sources): bool |
|
110
|
|
|
{ |
|
111
|
7 |
|
return empty( array_filter($sources->getRecords(), [$this, 'checkForNotIp6']) ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
7 |
|
public function checkForNotIp6(string $content): bool |
|
115
|
|
|
{ |
|
116
|
7 |
|
return !empty(preg_replace(static::PREG_IP6, '', $content)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
6 |
|
protected function containsNames(Sources\ASources $sources): bool |
|
120
|
|
|
{ |
|
121
|
6 |
|
return empty( array_filter($sources->getRecords(), [$this, 'checkForNames']) ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $content |
|
126
|
|
|
* @return bool |
|
127
|
|
|
* Names has no asterisk, question mark or slashes |
|
128
|
|
|
* Who set them there is an idiot |
|
129
|
|
|
*/ |
|
130
|
6 |
|
public function checkForNames(string $content): bool |
|
131
|
|
|
{ |
|
132
|
6 |
|
return (bool) preg_match(static::PREG_NAME, $content); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|