Skip to content

Commit 33de84c

Browse files
authored
Merge pull request #582 from favicode/bbutkovic/fix-blocking-php80
Fix blocking issues on php 8.0 and up.
2 parents 654dd2b + 97d3039 commit 33de84c

File tree

6 files changed

+158
-257
lines changed

6 files changed

+158
-257
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Blocking;
4+
5+
use Fastly\Cdn\Model\Config;
6+
use Magento\Backend\App\Action;
7+
use Magento\Backend\App\Action\Context;
8+
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
9+
10+
abstract class AbstractBlocking extends Action
11+
{
12+
protected ConfigWriter $configWriter;
13+
14+
public function __construct(
15+
Context $context,
16+
ConfigWriter $configWriter
17+
) {
18+
$this->configWriter = $configWriter;
19+
20+
parent::__construct($context);
21+
}
22+
23+
/**
24+
* @param string[] $countryCodes
25+
* @param string[] $acls
26+
* @param int $blockingType
27+
* @return string
28+
*/
29+
protected function prepareBlockedItems(array $countryCodes, array $acls, int $blockingType): string
30+
{
31+
$list = [];
32+
foreach ($countryCodes as $countryCode) {
33+
$list[] = sprintf('client.geo.country_code == "%s"', $countryCode);
34+
}
35+
36+
foreach ($acls as $acl) {
37+
$list[] = sprintf('req.http.Fastly-Client-Ip ~ %s', $acl);
38+
}
39+
40+
$result = implode(' || ', $list);
41+
if ($blockingType === 1 && !empty($result)) {
42+
$result = sprintf('!(%s)', $result);
43+
}
44+
45+
return $result;
46+
}
47+
48+
protected function storeConfigArray(string $path, array $data): void
49+
{
50+
$this->configWriter->save(
51+
$path,
52+
implode(',', $data),
53+
'default',
54+
'0'
55+
);
56+
}
57+
58+
protected function getParamArray(string $param): array
59+
{
60+
$request = $this->getRequest();
61+
62+
$data = $request->getParam($param);
63+
if (empty($data)) {
64+
return [];
65+
}
66+
67+
return array_map(static function ($row) {
68+
return $row['value'];
69+
}, $data);
70+
}
71+
}

Diff for: Controller/Adminhtml/FastlyCdn/Blocking/Blocking.php

+11-89
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Blocking
3737
*/
38-
class Blocking extends Action
38+
class Blocking extends AbstractBlocking
3939
{
4040
/**
4141
* @var Http
@@ -57,10 +57,6 @@ class Blocking extends Action
5757
* @var Vcl
5858
*/
5959
private $vcl;
60-
/**
61-
* @var ConfigWriter
62-
*/
63-
private $configWriter;
6460

6561
/**
6662
* Blocking constructor.
@@ -87,8 +83,7 @@ public function __construct(
8783
$this->config = $config;
8884
$this->api = $api;
8985
$this->vcl = $vcl;
90-
$this->configWriter = $configWriter;
91-
parent::__construct($context);
86+
parent::__construct($context, $configWriter);
9287
}
9388

9489
/**
@@ -114,11 +109,13 @@ public function execute()
114109
Config::VCL_BLOCKING_SNIPPET
115110
);
116111

117-
$country_codes = $this->prepareCountryCodes($this->getRequest()->getParam('countries'));
118-
$acls = $this->prepareAcls($this->getRequest()->getParam('acls'));
112+
$countryCodes = $this->getParamArray('countries');
113+
$this->storeConfigArray(Config::XML_FASTLY_BLOCK_BY_COUNTRY, $countryCodes);
119114

120-
$blockedItems = $country_codes . $acls;
121-
$strippedBlockedItems = substr($blockedItems, 0, strrpos($blockedItems, '||', -1));
115+
$acls = $this->getParamArray('acls');
116+
$this->storeConfigArray(Config::XML_FASTLY_BLOCK_BY_ACL, $acls);
117+
118+
$blockedItems = $this->prepareBlockedItems($countryCodes, $acls, (int) $blockingType);
122119

123120
$this->configWriter->save(
124121
Config::XML_FASTLY_BLOCKING_TYPE,
@@ -136,12 +133,9 @@ public function execute()
136133
continue;
137134
}
138135

139-
if ($strippedBlockedItems === '') {
140-
$value = '';
141-
} else {
142-
$strippedBlockedItems = $this->config->processBlockedItems($strippedBlockedItems, $blockingType);
143-
$value = str_replace('####BLOCKED_ITEMS####', $strippedBlockedItems, $value);
144-
}
136+
$value = $blockedItems !== '' ?
137+
str_replace('####BLOCKED_ITEMS####', $blockedItems, $value) :
138+
'';
145139

146140
$snippetData = [
147141
'name' => $name,
@@ -188,78 +182,6 @@ public function execute()
188182
}
189183
}
190184

191-
/**
192-
* Prepares ACL VCL snippets
193-
*
194-
* @param $blockedAcls
195-
* @return string
196-
*/
197-
private function prepareAcls($blockedAcls)
198-
{
199-
$result = '';
200-
$aclsArray = [];
201-
$acls = '';
202-
203-
if ($blockedAcls != null) {
204-
foreach ($blockedAcls as $key => $value) {
205-
$aclsArray[] = $value['value'];
206-
}
207-
$acls = implode(',', $aclsArray);
208-
}
209-
210-
$this->configWriter->save(
211-
Config::XML_FASTLY_BLOCK_BY_ACL,
212-
$acls,
213-
'default',
214-
'0'
215-
);
216-
217-
if ($acls != '') {
218-
$blockedAclsPieces = explode(",", $acls);
219-
foreach ($blockedAclsPieces as $acl) {
220-
$result .= ' req.http.Fastly-Client-Ip ~ ' . $acl . ' ||';
221-
}
222-
}
223-
224-
return $result;
225-
}
226-
227-
/**
228-
* Prepares blocked countries VCL snippet
229-
*
230-
* @param $blockedCountries
231-
* @return string
232-
*/
233-
private function prepareCountryCodes($blockedCountries)
234-
{
235-
$result = '';
236-
$countriesArray = [];
237-
$countries = '';
238-
239-
if ($blockedCountries != null) {
240-
foreach ($blockedCountries as $key => $value) {
241-
$countriesArray[] = $value['value'];
242-
}
243-
$countries = implode(',', $countriesArray);
244-
}
245-
246-
$this->configWriter->save(
247-
Config::XML_FASTLY_BLOCK_BY_COUNTRY,
248-
$countries,
249-
'default',
250-
'0'
251-
);
252-
253-
if ($countries != '') {
254-
$blockedCountriesPieces = explode(",", $countries);
255-
foreach ($blockedCountriesPieces as $code) {
256-
$result .= ' client.geo.country_code == "' . $code . '" ||';
257-
}
258-
}
259-
260-
return $result;
261-
}
262-
263185
private function sendWebhook($checkIfSettingExists, $clone)
264186
{
265187
if ($this->config->areWebHooksEnabled() && $this->config->canPublishConfigChanges()) {

Diff for: Controller/Adminhtml/FastlyCdn/Blocking/UpdateBlocking.php

+22-97
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@
2020
*/
2121
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Blocking;
2222

23-
use Magento\Backend\App\Action;
23+
use Fastly\Cdn\Helper\Vcl;
24+
use Fastly\Cdn\Model\Api;
25+
use Fastly\Cdn\Model\Config;
2426
use Magento\Backend\App\Action\Context;
27+
use Magento\Config\App\Config\Type\System as SystemConfig;
28+
use Magento\Framework\App\Cache\TypeListInterface as CacheTypeList;
29+
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
2530
use Magento\Framework\App\Request\Http;
2631
use Magento\Framework\Controller\Result\JsonFactory;
27-
use Fastly\Cdn\Model\Config;
28-
use Fastly\Cdn\Model\Api;
29-
use Fastly\Cdn\Helper\Vcl;
30-
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
31-
use Magento\Framework\App\Cache\TypeListInterface as CacheTypeList;
32-
use Magento\Config\App\Config\Type\System as SystemConfig;
3332
use Magento\Framework\Exception\LocalizedException;
3433

3534
/**
3635
* Class UpdateBlocking
3736
*
3837
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Blocking
3938
*/
40-
class UpdateBlocking extends Action
39+
class UpdateBlocking extends AbstractBlocking
4140
{
4241
/**
4342
* @var Http
@@ -59,10 +58,6 @@ class UpdateBlocking extends Action
5958
* @var Vcl
6059
*/
6160
private $vcl;
62-
/**
63-
* @var ConfigWriter
64-
*/
65-
private $configWriter;
6661
/**
6762
* @var CacheTypeList
6863
*/
@@ -104,7 +99,7 @@ public function __construct(
10499
$this->configWriter = $configWriter;
105100
$this->cacheTypeList = $cacheTypeList;
106101
$this->systemConfig = $systemConfig;
107-
parent::__construct($context);
102+
parent::__construct($context, $configWriter);
108103
}
109104

110105
/**
@@ -118,34 +113,36 @@ public function execute()
118113
try {
119114
$service = $this->api->checkServiceDetails();
120115
$currActiveVersion = $this->vcl->determineVersions($service->versions);
116+
if (!isset($currActiveVersion['active_version'])) {
117+
throw new LocalizedException(__('No version is currently active.'));
118+
}
121119

122120
$snippet = $this->config->getVclSnippets(
123121
Config::VCL_BLOCKING_PATH,
124122
Config::VCL_BLOCKING_SNIPPET
125123
);
126124

127-
$country_codes = $this->prepareCountryCodes($this->request->getParam('countries'));
128-
$acls = $this->prepareAcls($this->request->getParam('acls'));
129-
130-
$blockedItems = $country_codes . $acls;
131-
$strippedBlockedItems = substr($blockedItems, 0, strrpos($blockedItems, '||', -1));
132125
$blockingType = $this->request->getParam('blocking_type');
133-
134126
$this->configWriter->save(
135127
Config::XML_FASTLY_BLOCKING_TYPE,
136128
$blockingType,
137129
'default',
138130
'0'
139131
);
140132

133+
$countryCodes = $this->getParamArray('countries');
134+
$this->storeConfigArray(Config::XML_FASTLY_BLOCK_BY_COUNTRY, $countryCodes);
135+
136+
$acls = $this->getParamArray('acls');
137+
$this->storeConfigArray(Config::XML_FASTLY_BLOCK_BY_ACL, $acls);
138+
139+
$blockedItems = $this->prepareBlockedItems($countryCodes, $acls, (int) $blockingType);
140+
141141
// Add blocking snippet
142142
foreach ($snippet as $key => $value) {
143-
if ($strippedBlockedItems === '') {
144-
$value = '';
145-
} else {
146-
$strippedBlockedItems = $this->config->processBlockedItems($strippedBlockedItems, $blockingType);
147-
$value = str_replace('####BLOCKED_ITEMS####', $strippedBlockedItems, $value);
148-
}
143+
$value = $blockedItems !== '' ?
144+
str_replace('####BLOCKED_ITEMS####', $blockedItems, $value) :
145+
'';
149146

150147
$snippetName = Config::FASTLY_MAGENTO_MODULE . '_blocking_' . $key;
151148
$snippetId = $this->api->getSnippet($currActiveVersion['active_version'], $snippetName);
@@ -176,76 +173,4 @@ public function execute()
176173
]);
177174
}
178175
}
179-
180-
/**
181-
* Prepares ACL VCL snippets
182-
*
183-
* @param $blockedAcls
184-
* @return string
185-
*/
186-
private function prepareAcls($blockedAcls)
187-
{
188-
$result = '';
189-
$aclsArray = [];
190-
$acls = '';
191-
192-
if ($blockedAcls != null) {
193-
foreach ($blockedAcls as $key => $value) {
194-
$aclsArray[] = $value['value'];
195-
}
196-
$acls = implode(',', $aclsArray);
197-
}
198-
199-
$this->configWriter->save(
200-
Config::XML_FASTLY_BLOCK_BY_ACL,
201-
$acls,
202-
'default',
203-
'0'
204-
);
205-
206-
if ($acls != '') {
207-
$blockedAclsPieces = explode(",", $acls);
208-
foreach ($blockedAclsPieces as $acl) {
209-
$result .= ' req.http.Fastly-Client-Ip ~ ' . $acl . ' ||';
210-
}
211-
}
212-
213-
return $result;
214-
}
215-
216-
/**
217-
* Prepares blocked countries VCL snippet
218-
*
219-
* @param $blockedCountries
220-
* @return string
221-
*/
222-
private function prepareCountryCodes($blockedCountries)
223-
{
224-
$result = '';
225-
$countriesArray = [];
226-
$countries = '';
227-
228-
if ($blockedCountries != null) {
229-
foreach ($blockedCountries as $key => $value) {
230-
$countriesArray[] = $value['value'];
231-
}
232-
$countries = implode(',', $countriesArray);
233-
}
234-
235-
$this->configWriter->save(
236-
Config::XML_FASTLY_BLOCK_BY_COUNTRY,
237-
$countries,
238-
'default',
239-
'0'
240-
);
241-
242-
if ($countries != '') {
243-
$blockedCountriesPieces = explode(",", $countries);
244-
foreach ($blockedCountriesPieces as $code) {
245-
$result .= ' client.geo.country_code == "' . $code . '" ||';
246-
}
247-
}
248-
249-
return $result;
250-
}
251176
}

0 commit comments

Comments
 (0)