-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setup.php
190 lines (158 loc) · 5.16 KB
/
Setup.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Hampel\KnownBots;
use Hampel\KnownBots\SubContainer\Api;
use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerUpgradeTrait;
use XF\Db\Schema\Alter;
use XF\Db\Schema\Create;
use XF\Util\File;
class Setup extends AbstractSetup
{
use StepRunnerUpgradeTrait;
const BOTPATH = 'internal-data://knownbots.json';
public function install(array $stepParams = [])
{
$this->createAgentTable();
}
public function postInstall(array &$stateChanges)
{
$fs = $this->app->fs();
if (!$fs->has(self::BOTPATH))
{
$sourceFile = sprintf("%s/knownbots.json", $this->addOn->getAddOnDirectory());
if (file_exists($sourceFile))
{
File::copyFileToAbstractedPath($sourceFile, self::BOTPATH);
$this->loadBots();
}
else // we may be installing from _output rather than from the zip archive, so fetch the latest bots directly
{
$this->getApi()->fetchBots(true);
}
}
$this->randomizeFetchCron();
$this->randomizeSendCron();
}
// ################################ UPGRADE ##################
public function upgrade5000031Step1()
{
$this->createAgentTable();
}
public function postUpgrade($previousVersion, array &$stateChanges)
{
$this->loadBots();
$this->randomizeFetchCron();
$this->randomizeSendCron();
// remove [email protected] email addresses when upgrading to v6
if ($previousVersion < 6000030)
{
$this->removeKnownBotsEmail();
}
}
// ################################ UNINSTALL ##################
public function uninstall(array $stepParams = [])
{
$fs = $this->app->fs();
if ($fs->has(self::BOTPATH))
{
$fs->delete(self::BOTPATH);
}
// remove code cache files
foreach (['maps', 'bots', 'complex', 'generic', 'ignored', 'browsers'] as $type)
{
$path = "code-cache://known_bots/{$type}.php";
if ($fs->has($path))
{
$fs->delete($path);
}
}
$fs->deleteDir("code-cache://known_bots");
$this->schemaManager()->dropTable('xf_knownbots_agent');
}
// ################################ Helpers ##################
protected function createAgentTable()
{
$this->schemaManager()->createTable('xf_knownbots_agent', function (Create $table) {
$table->addColumn('user_agent', 'varbinary', 512)->primaryKey();
$table->addColumn('robot_key', 'varchar', 25)->nullable();
$table->addColumn('last_updated', 'int')->setDefault(0);
$table->addColumn('sent', 'bool')->setDefault(0);
$table->addKey('last_updated');
});
}
protected function loadBots()
{
if ($this->app->fs()->has(self::BOTPATH))
{
$fetcher = $this->getApi();
$fetcher->updateBots($fetcher->loadBots());
}
}
protected function randomizeFetchCron()
{
// randomize cron run time
$cron = $this->app()->find('XF:CronEntry', 'hampelKnownBotsFetchBots');
if ($cron)
{
$hours = rand(0, 23);
$minutes = rand(0, 59);
$rules = $cron->run_rules;
$rules['hours'] = [$hours];
$rules['minutes'] = [$minutes];
$cron->run_rules = $rules;
$cron->save();
}
}
protected function randomizeSendCron()
{
// randomize cron run time
$cron = $this->app()->find('XF:CronEntry', 'hampelKnownBotsUserAgents');
if ($cron)
{
$hours = rand(0, 23);
$minutes = rand(0, 59);
$rules = $cron->run_rules;
$rules['hours'] = [$hours];
$rules['minutes'] = [$minutes];
$cron->run_rules = $rules;
$cron->save();
}
}
protected function removeKnownBotsEmail()
{
$emailOption = \XF::options()->knownbotsEmailUserAgents;
if (isset($emailOption['email']) && !empty($emailOption['email']))
{
$addresses = array_map('trim', explode(',', $emailOption['email']));
$initialCount = count($addresses);
foreach ($addresses as $key => $address)
{
if ($address == '[email protected]')
{
unset($addresses[$key]);
}
}
if (count($addresses) < $initialCount)
{
if (count($addresses) == 0)
{
$emailOption['enabled'] = 0;
$emailOption['email'] = '';
}
else
{
// just take the first email address remaining in the list
$emailOption['email'] = array_shift($addresses);
}
\XF::repository('XF:Option')->updateOption('knownbotsEmailUserAgents', $emailOption);
}
}
}
/**
* @return Api
*/
protected function getApi()
{
return $this->app['knownbots.api'];
}
}