-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup.php
More file actions
195 lines (168 loc) · 6.91 KB
/
Copy pathSetup.php
File metadata and controls
195 lines (168 loc) · 6.91 KB
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
191
192
193
194
195
<?php
/**
* IP Block Protection - a XenForo 2 add-on.
*
* @copyright 2026 ip-block.com
* @license GNU General Public License v2.0 only
*/
namespace IpBlock\Protection;
use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerInstallTrait;
use XF\AddOn\StepRunnerUninstallTrait;
use XF\AddOn\StepRunnerUpgradeTrait;
/**
* Install / upgrade / uninstall handler.
*
* Creates the "IP Block" option group, all options and their master phrases so
* the add-on is configurable from Admin CP -> Options immediately after install,
* then removes everything cleanly on uninstall.
*
* (The controller_pre_dispatch code-event listener is registered declaratively
* in _data/code_event_listeners.xml, imported automatically on install.)
*/
class Setup extends AbstractSetup
{
use StepRunnerInstallTrait;
use StepRunnerUpgradeTrait;
use StepRunnerUninstallTrait;
const GROUP_ID = 'ipBlock';
const ADDON_ID = 'IpBlock/Protection';
/**
* Option definitions: id => [editFormat, dataType, defaultValue, editFormatParams].
*
* block_action uses a plain textbox that accepts "redirect" or "message";
* the Checker treats any value other than "message" as "redirect", so the
* behaviour is always well defined.
*
* @return array
*/
protected function optionDefs()
{
return [
'ipBlockEnabled' => ['onoff', 'boolean', '0', ''],
'ipBlockSiteId' => ['textbox', 'string', '', ''],
'ipBlockApiKey' => ['textbox', 'string', '', ''],
'ipBlockApiUrl' => ['textbox', 'string', 'https://api.ip-block.com/v1/check', ''],
'ipBlockFailOpen' => ['onoff', 'boolean', '1', ''],
'ipBlockCacheTtl' => ['spinbox', 'posint', '300', 'min=0'],
'ipBlockBehindProxy' => ['onoff', 'boolean', '0', ''],
'ipBlockBlockAction' => ['textbox', 'string', 'redirect', ''],
'ipBlockBlockMessage' => ['textarea', 'string', 'Access to this site has been denied.', ''],
'ipBlockWhitelist' => ['textarea', 'string', '', ''],
];
}
// -------------------------------------------------------------------------
// Install
// -------------------------------------------------------------------------
public function installStep1()
{
// Option group + its master title phrase.
/** @var \XF\Entity\OptionGroup $group */
$group = $this->app->em()->create('XF:OptionGroup');
$group->group_id = self::GROUP_ID;
$group->display_order = 1000;
$group->debug_only = false;
$group->advanced = false;
$group->addon_id = self::ADDON_ID;
$group->save();
$this->createPhrase('option_group.' . self::GROUP_ID, 'IP Block');
}
public function installStep2()
{
$displayOrder = 10;
foreach ($this->optionDefs() as $optionId => $def)
{
list($editFormat, $dataType, $default, $params) = $def;
/** @var \XF\Entity\Option $option */
$option = $this->app->em()->create('XF:Option');
$option->option_id = $optionId;
$option->option_value = $default;
$option->default_value = $default;
$option->edit_format = $editFormat;
$option->edit_format_params = $params;
$option->data_type = $dataType;
$option->sub_options = [];
$option->advanced = false;
$option->addon_id = self::ADDON_ID;
$option->setRelations([self::GROUP_ID => $displayOrder]);
$option->save();
$this->createPhrase('option.' . $optionId, $this->titleFor($optionId));
$this->createPhrase('option_explain.' . $optionId, $this->explainFor($optionId));
$displayOrder += 10;
}
}
// -------------------------------------------------------------------------
// Upgrade - nothing to migrate for 1.0.0
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// Uninstall
// -------------------------------------------------------------------------
public function uninstallStep1()
{
$db = $this->db();
$optionIds = array_keys($this->optionDefs());
$quoted = $db->quote($optionIds);
$db->delete('xf_option_group_relation', "option_id IN ($quoted)");
$db->delete('xf_option', "option_id IN ($quoted)");
$db->delete('xf_option_group', 'group_id = ?', self::GROUP_ID);
}
public function uninstallStep2()
{
$db = $this->db();
$titles = ['option_group.' . self::GROUP_ID];
foreach (array_keys($this->optionDefs()) as $optionId)
{
$titles[] = 'option.' . $optionId;
$titles[] = 'option_explain.' . $optionId;
}
$db->delete('xf_phrase', 'title IN (' . $db->quote($titles) . ') AND language_id = 0');
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
/**
* Create a master (language_id 0) phrase owned by this add-on.
*/
protected function createPhrase($title, $text)
{
/** @var \XF\Entity\Phrase $phrase */
$phrase = $this->app->em()->create('XF:Phrase');
$phrase->language_id = 0;
$phrase->title = $title;
$phrase->phrase_text = $text;
$phrase->addon_id = self::ADDON_ID;
$phrase->save();
}
protected function titleFor($optionId)
{
$titles = [
'ipBlockEnabled' => 'Enable IP screening',
'ipBlockSiteId' => 'Site ID',
'ipBlockApiKey' => 'API key',
'ipBlockApiUrl' => 'API URL',
'ipBlockFailOpen' => 'Fail open',
'ipBlockCacheTtl' => 'Cache lifetime (seconds)',
'ipBlockBehindProxy' => 'Behind a proxy / CDN',
'ipBlockBlockAction' => 'Block action (redirect | message)',
'ipBlockBlockMessage' => 'Block message',
'ipBlockWhitelist' => 'IP whitelist',
];
return isset($titles[$optionId]) ? $titles[$optionId] : $optionId;
}
protected function explainFor($optionId)
{
$explains = [
'ipBlockEnabled' => 'Screen every front-end request against ip-block.com. The Admin CP is never screened.',
'ipBlockSiteId' => 'Your ip-block.com site identifier.',
'ipBlockApiKey' => 'Your ip-block.com API key. Sent in the request body.',
'ipBlockApiUrl' => 'The screening endpoint. Leave as default unless instructed otherwise.',
'ipBlockFailOpen' => 'If the API times out or errors, allow the visitor (recommended). Disable to block on failure.',
'ipBlockCacheTtl' => 'How long a decision is cached (keyed by IP + user agent + referrer). 0 = check every request.',
'ipBlockBehindProxy' => 'Enable when behind Cloudflare or another reverse proxy so the real IP is read from CF-Connecting-IP / X-Forwarded-For.',
'ipBlockBlockAction' => 'Enter "redirect" to send blocked visitors to ip-block.com, or "message" to return an HTTP 403 with the message below.',
'ipBlockBlockMessage' => 'Message returned with the 403 response when the block action is "message".',
'ipBlockWhitelist' => 'One IP address per line. Whitelisted IPs are always allowed and never sent to the API.',
];
return isset($explains[$optionId]) ? $explains[$optionId] : '';
}
}