-
Notifications
You must be signed in to change notification settings - Fork 18
/
BotDetector.php
135 lines (112 loc) · 3.62 KB
/
BotDetector.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/*
* This file is part of the VipxBotDetect library.
*
* (c) Lennart Hildebrandt <http://github.com/lennerd>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Vipx\BotDetect;
use InvalidArgumentException;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\ConfigCache;
use Vipx\BotDetect\Metadata\Dumper\MetadataDumper;
use Vipx\BotDetect\Metadata\Dumper\PhpMetadataDumper;
use Vipx\BotDetect\Metadata\Metadata;
use Vipx\BotDetect\Metadata\MetadataCollection;
class BotDetector implements BotDetectorInterface
{
private $metadatas;
private $loader;
private $resource;
private $options;
/**
* @param LoaderInterface $loader
* @param $resource
* @param array $options
*/
public function __construct(LoaderInterface $loader, $resource, array $options = [])
{
$this->loader = $loader;
$this->resource = $resource;
$this->setOptions($options);
}
/**
* @param array $options
* @throws InvalidArgumentException
*/
public function setOptions(array $options): void
{
$this->options = [
'cache_dir' => null,
'debug' => false,
'metadata_cache_file' => 'project_vipx_bot_detect_metadata.php',
'metadata_dumper_class' => PhpMetadataDumper::class,
];
// check option names and live merge, if errors are encountered Exception will be thrown
$invalid = [];
foreach ($options as $key => $value) {
if (array_key_exists($key, $this->options)) {
$this->options[$key] = $value;
} else {
$invalid[] = $key;
}
}
if ($invalid) {
throw new InvalidArgumentException(
sprintf('The BotDetector does not support the following options: "%s".', implode('\', \'', $invalid))
);
}
}
/**
* Returns the detector options
*
* @return array
*/
public function getOptions(): array
{
return $this->options;
}
/**
* @return Metadata[]
*/
public function getMetadatas(): array
{
if (null !== $this->metadatas) {
return $this->metadatas;
}
if (null === $this->options['cache_dir'] || null === $this->options['metadata_cache_file']) {
$metadataCollection = $this->loader->load($this->resource);
return $this->metadatas = $metadataCollection->getMetadatas();
}
$cache = new ConfigCache(
$this->options['cache_dir'].'/'.$this->options['metadata_cache_file'],
$this->options['debug']
);
if ($cache->isFresh()) {
return $this->metadatas = require $cache->getPath();
}
/** @var $metadataCollection MetadataCollection */
$metadataCollection = $this->loader->load($this->resource);
$dumperClass = $this->options['metadata_dumper_class'];
$metadatas = $metadataCollection->getMetadatas();
/** @var $dumper MetadataDumper */
$dumper = new $dumperClass($metadatas);
$cache->write($dumper->dump(), $metadataCollection->getResources());
return $this->metadatas = $metadatas;
}
/**
* {@inheritdoc}
*/
public function detect(string $agent, string $ip): ?Metadata
{
$agent = trim($agent);
foreach ($this->getMetadatas() as $metadata) {
if ($metadata->match($agent, $ip)) {
return $metadata;
}
}
return null;
}
}