diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php index 3f0f2e02..54d857df 100644 --- a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php +++ b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php @@ -36,6 +36,7 @@ use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\Config\Model\ResourceModel\Config as CoreConfig; use Magento\Framework\App\Cache\TypeListInterface; +use Fastly\Cdn\Model\Snippet\BuiltInSnippetList; /** * Class Upload @@ -90,6 +91,11 @@ class Upload extends Action */ private $typeList; + /** + * @var BuiltInSnippetList + */ + private $builtInSnippetList; + /** * Upload constructor. * @@ -105,6 +111,7 @@ class Upload extends Action * @param Filesystem $filesystem * @param CoreConfig $coreConfig * @param TypeListInterface $typeList + * @param BuiltInSnippetList $builtInSnippetList */ public function __construct( Context $context, @@ -118,7 +125,8 @@ public function __construct( TimezoneInterface $timezone, Filesystem $filesystem, CoreConfig $coreConfig, - TypeListInterface $typeList + TypeListInterface $typeList, + BuiltInSnippetList $builtInSnippetList ) { $this->request = $request; $this->resultJson = $resultJsonFactory; @@ -132,7 +140,7 @@ public function __construct( parent::__construct($context); $this->coreConfig = $coreConfig; $this->typeList = $typeList; - + $this->builtInSnippetList = $builtInSnippetList; } /** @@ -155,6 +163,7 @@ public function execute() $customSnippetPath = $read->getAbsolutePath(Config::CUSTOM_SNIPPET_PATH); $customSnippets = $this->config->getCustomSnippets($customSnippetPath); + $allowedSnippets = []; foreach ($snippets as $key => $value) { $priority = 50; if ($key == 'hash') { @@ -167,6 +176,7 @@ public function execute() 'priority' => $priority, 'content' => $value ]; + $allowedSnippets[] = $snippetData['name']; $this->api->uploadSnippet($clone->number, $snippetData); } @@ -183,9 +193,11 @@ public function execute() 'content' => $value, 'dynamic' => '0' ]; + $allowedSnippets[] = $customSnippetData['name']; $this->api->uploadSnippet($clone->number, $customSnippetData); } + $this->syncSnippets($allowedSnippets, $clone->number); $this->createGzipHeader($clone); $condition = [ @@ -334,4 +346,28 @@ private function createGzipHeader($clone) $this->api->createHeader($clone->number, $headerData); } + + /** + * Remove disabled snippets from current vcl file + * + * @param array $allowedSnippets + * @param int $version + * @throws LocalizedException + */ + private function syncSnippets(array $allowedSnippets, int $version): void + { + $snippets = $this->api->getSnippets($version); + + $currentActiveSnippets = []; + foreach ($snippets as $item) { + $currentActiveSnippets[] = $item->name; + } + $snippetsForDelete = \array_diff($currentActiveSnippets, $allowedSnippets); + + foreach ($snippetsForDelete as $snippetName) { + if (!$this->builtInSnippetList->checkIsBuiltInSnippet($snippetName)) { + $this->api->removeSnippet($version, $snippetName); + } + } + } } diff --git a/Model/Api.php b/Model/Api.php index 853e94e2..99ae8089 100644 --- a/Model/Api.php +++ b/Model/Api.php @@ -18,6 +18,7 @@ * @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com) * @license BSD, see LICENSE_FASTLY_CDN.txt */ + namespace Fastly\Cdn\Model; use Magento\Framework\Exception\LocalizedException; @@ -36,10 +37,10 @@ */ class Api { - const FASTLY_HEADER_AUTH = 'Fastly-Key'; - const FASTLY_HEADER_TOKEN = 'X-Purge-Token'; + const FASTLY_HEADER_AUTH = 'Fastly-Key'; + const FASTLY_HEADER_TOKEN = 'X-Purge-Token'; const FASTLY_HEADER_SOFT_PURGE = 'Fastly-Soft-Purge'; - const PURGE_TIMEOUT = 10; + const PURGE_TIMEOUT = 10; const PURGE_TOKEN_LIFETIME = 30; const FASTLY_MAX_HEADER_KEY_SIZE = 256; @@ -192,7 +193,7 @@ public function cleanBySurrogateKey($keys) $parts = $num / self::FASTLY_MAX_HEADER_KEY_SIZE; $additional = ($parts > (int)$parts) ? 1 : 0; $parts = (int)$parts + (int)$additional; - $chunks = ceil($num/$parts); + $chunks = ceil($num / $parts); $collection = array_chunk($keys, $chunks); } else { $collection = [$keys]; @@ -283,20 +284,20 @@ private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload if ($method == 'PURGE') { // create purge token - $expiration = time() + self::PURGE_TOKEN_LIFETIME; + $expiration = time() + self::PURGE_TOKEN_LIFETIME; $zendUri = \Zend_Uri::factory($uri); $path = $zendUri->getPath(); $stringToSign = $path . $expiration; - $signature = hash_hmac('sha1', $stringToSign, $this->config->getServiceId()); - $token = $expiration . '_' . urlencode($signature); + $signature = hash_hmac('sha1', $stringToSign, $this->config->getServiceId()); + $token = $expiration . '_' . urlencode($signature); $headers = [ self::FASTLY_HEADER_TOKEN . ': ' . $token ]; } else { // set headers $headers = [ - self::FASTLY_HEADER_AUTH . ': ' . $this->config->getApiKey() + self::FASTLY_HEADER_AUTH . ': ' . $this->config->getApiKey() ]; } @@ -337,7 +338,7 @@ private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload } if ($this->config->areWebHooksEnabled() && $this->config->canPublishPurgeChanges()) { - $this->sendWebHook('*initiated ' . $type .'*'); + $this->sendWebHook('*initiated ' . $type . '*'); if ($this->config->canPublishPurgeDebugBacktrace() == false) { return $result; @@ -398,7 +399,7 @@ public function checkServiceDetails($test = false, $serviceId = null, $apiKey = */ public function cloneVersion($curVersion) { - $url = $this->_getApiServiceUri() . 'version/'.rawurlencode($curVersion).'/clone'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($curVersion) . '/clone'; $result = $this->_fetch($url, \Zend_Http_Client::PUT); if (!$result) { @@ -434,7 +435,7 @@ public function addComment($version, $comment) */ public function uploadVcl($version, $vcl) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/vcl'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/vcl'; $result = $this->_fetch($url, 'POST', $vcl); return $result; @@ -450,7 +451,7 @@ public function uploadVcl($version, $vcl) */ public function setVclAsMain($version, $name) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/vcl/' .rawurlencode($name). '/main'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/vcl/' . rawurlencode($name) . '/main'; $result = $this->_fetch($url, 'PUT'); return $result; @@ -464,7 +465,7 @@ public function setVclAsMain($version, $name) */ public function validateServiceVersion($version) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/validate'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/validate'; $result = $this->_fetch($url, 'GET'); if ($result->status == 'error') { @@ -479,7 +480,7 @@ public function validateServiceVersion($version) */ public function containerValidateServiceVersion($version) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/validate'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/validate'; $result = $this->_fetch($url, 'GET'); return $result; @@ -494,7 +495,7 @@ public function containerValidateServiceVersion($version) */ public function activateVersion($version) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/activate'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/activate'; $result = $this->_fetch($url, 'PUT'); return $result; @@ -532,18 +533,18 @@ public function uploadSnippet($version, array $snippet) $snippetName = $snippet['name']; $checkIfExists = $this->hasSnippet($version, $snippetName); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/snippet'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/snippet'; if (!$checkIfExists) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; if (!isset($snippet['dynamic']) || $snippet['dynamic'] != 1) { - $url .= '/'.rawurlencode($snippetName); + $url .= '/' . rawurlencode($snippetName); unset($snippet['name'], $snippet['type'], $snippet['dynamic']); } else { $snippet['name'] = $this->getSnippet($version, $snippetName)->id; - $url = $this->_getApiServiceUri(). 'snippet' . '/'.rawurlencode($snippet['name']); + $url = $this->_getApiServiceUri() . 'snippet' . '/' . rawurlencode($snippet['name']); } } @@ -564,7 +565,7 @@ public function uploadSnippet($version, array $snippet) */ public function getSnippet($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/snippet/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/snippet/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -579,7 +580,7 @@ public function getSnippet($version, $name) */ public function updateSnippet(array $snippet) { - $url = $this->_getApiServiceUri(). 'snippet' . '/'.rawurlencode($snippet['name']); + $url = $this->_getApiServiceUri() . 'snippet' . '/' . rawurlencode($snippet['name']); $result = $this->_fetch($url, \Zend_Http_Client::PUT, $snippet); if (!$result) { @@ -592,8 +593,8 @@ public function updateSnippet(array $snippet) /** * Performs a lookup to determine if VCL snippet exists * - * @param string $version Fastly version - * @param string $name VCL snippet name + * @param string $version Fastly version + * @param string $name VCL snippet name * * @return bool * @throws LocalizedException @@ -619,7 +620,7 @@ public function hasSnippet($version, $name) */ public function removeSnippet($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/snippet/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/snippet/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -635,12 +636,12 @@ public function removeSnippet($version, $name) public function createCondition($version, array $condition) { $checkIfExists = $this->getCondition($version, $condition['name']); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/condition'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/condition'; if (!$checkIfExists) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; - $url .= '/'.rawurlencode($condition['name']); + $url .= '/' . rawurlencode($condition['name']); } $result = $this->_fetch($url, $verb, $condition); @@ -662,7 +663,7 @@ public function createCondition($version, array $condition) */ public function getCondition($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/condition/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/condition/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -679,13 +680,13 @@ public function getCondition($version, $name) public function createHeader($version, array $condition) { $checkIfExists = $this->getHeader($version, $condition['name']); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/header'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/header'; if ($checkIfExists === false) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; - $url .= '/'.rawurlencode($condition['name']); + $url .= '/' . rawurlencode($condition['name']); } $result = $this->_fetch($url, $verb, $condition); @@ -703,7 +704,7 @@ public function createHeader($version, array $condition) */ public function getHeader($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/header/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/header/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -720,12 +721,12 @@ public function getHeader($version, $name) public function createResponse($version, array $response) { $checkIfExists = $this->getResponse($version, $response['name']); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/response_object'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/response_object'; if (!$checkIfExists) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; - $url .= '/'.rawurlencode($response['name']); + $url .= '/' . rawurlencode($response['name']); } $result = $this->_fetch($url, $verb, $response); @@ -743,7 +744,7 @@ public function createResponse($version, array $response) */ public function getResponse($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/response_object/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/response_object/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -758,12 +759,12 @@ public function getResponse($version, $name) public function createRequest($version, $request) { $checkIfExists = $this->getRequest($version, $request['name']); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/request_settings'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/request_settings'; if (!$checkIfExists) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; - $url .= '/'.rawurlencode($request['name']); + $url .= '/' . rawurlencode($request['name']); } $result = $this->_fetch($url, $verb, $request); @@ -776,15 +777,15 @@ public function createRequest($version, $request) /** * Retrieves a specific Request settings object. * - * @param string $version Fastly version - * @param string $name Request name + * @param string $version Fastly version + * @param string $name Request name * * @return bool * @throws LocalizedException */ public function getRequest($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/request_settings/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/request_settings/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET, '', false, null, false); return $result; @@ -797,7 +798,7 @@ public function getRequest($version, $name) */ public function getAllConditions($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/condition'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/condition'; $result = $this->_fetch($url, \Zend_Http_Client::GET, '', false, null, false); return $result; @@ -810,7 +811,7 @@ public function getAllConditions($version) */ public function getAllDomains($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/domain'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/domain'; $result = $this->_fetch($url, \Zend_Http_Client::GET, '', false, null, false); return $result; @@ -824,7 +825,7 @@ public function getAllDomains($version) */ public function deleteDomain($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/domain/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/domain/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -838,7 +839,7 @@ public function deleteDomain($version, $name) */ public function createDomain($version, $data) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/domain'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/domain'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $data); return $result; @@ -852,7 +853,7 @@ public function createDomain($version, $data) */ public function deleteRequest($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/request_settings/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/request_settings/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); if (!$result) { @@ -869,7 +870,7 @@ public function deleteRequest($version, $name) */ public function getBackends($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/backend'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/backend'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -886,7 +887,7 @@ public function getBackends($version) */ public function configureBackend($params, $version, $old_name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/backend/' . rawurlencode($old_name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/backend/' . rawurlencode($old_name); $result = $this->_fetch($url, \Zend_Http_Client::PUT, $params); return $result; @@ -900,7 +901,7 @@ public function configureBackend($params, $version, $old_name) */ public function createBackend($params, $version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/backend'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/backend'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $params); return $result; @@ -930,7 +931,7 @@ public function getAllLogEndpoints($version) $providers = $this->helper->getAvailableLogEndpointProviders(); $results = []; foreach ($providers as $type => $providerName) { - $url = $this->_getApiServiceUri(). 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type); $endpoints = $this->_fetch($url, \Zend_Http_Client::GET); foreach ($endpoints as $endpoint) { $results[] = [ @@ -953,7 +954,7 @@ public function getLogEndpoints($version, $type) { $results = []; $providers = $this->helper->getAvailableLogEndpointProviders(); - $url = $this->_getApiServiceUri(). 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type); $endpoints = $this->_fetch($url, \Zend_Http_Client::GET); foreach ($endpoints as $endpoint) { $results[] = [ @@ -974,7 +975,7 @@ public function getLogEndpoints($version, $type) */ public function getLogEndpoint($version, $type, $name) { - $url = $this->_getApiServiceUri(). 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type) . '/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type) . '/' . rawurlencode($name); return $this->_fetch($url, \Zend_Http_Client::GET); } @@ -1029,16 +1030,16 @@ public function sendWebHook($message) $storeName = $this->helper->getStoreName(); $storeUrl = $this->helper->getStoreUrl(); - $text = $messagePrefix.' user='.$currentUsername.' '.$message.' on <'.$storeUrl.'|Store> | '.$storeName; + $text = $messagePrefix . ' user=' . $currentUsername . ' ' . $message . ' on <' . $storeUrl . '|Store> | ' . $storeName; $headers = [ 'Content-type: application/json' ]; $body = json_encode([ - "text" => $text, + "text" => $text, "username" => "fastly-magento-bot", - "icon_emoji"=> ":airplane:" + "icon_emoji" => ":airplane:" ]); $client = $this->curlFactory->create(); @@ -1049,7 +1050,7 @@ public function sendWebHook($message) $responseCode = \Zend_Http_Response::extractCode($response); if ($responseCode != 200) { - $this->log->log(100, 'Failed to send message to the following Webhook: '.$url); + $this->log->log(100, 'Failed to send message to the following Webhook: ' . $url); } $client->close(); @@ -1065,7 +1066,7 @@ public function sendWebHook($message) */ public function createDictionary($version, $params) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/dictionary'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/dictionary'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $params); return $result; @@ -1081,7 +1082,7 @@ public function createDictionary($version, $params) */ public function deleteDictionary($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/dictionary/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/dictionary/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -1096,7 +1097,7 @@ public function deleteDictionary($version, $name) */ public function dictionaryItemsList($dictionaryId) { - $url = $this->_getApiServiceUri(). 'dictionary/'.rawurlencode($dictionaryId).'/items'; + $url = $this->_getApiServiceUri() . 'dictionary/' . rawurlencode($dictionaryId) . '/items'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1112,7 +1113,7 @@ public function dictionaryItemsList($dictionaryId) */ public function getSingleDictionary($version, $dictionaryName) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/dictionary/' . rawurlencode($dictionaryName); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/dictionary/' . rawurlencode($dictionaryName); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1164,7 +1165,7 @@ public function checkAuthDictionaryPopulation($version) */ public function createDictionaryItems($dictionaryId, $params) { - $url = $this->_getApiServiceUri().'dictionary/'.rawurlencode($dictionaryId).'/items'; + $url = $this->_getApiServiceUri() . 'dictionary/' . rawurlencode($dictionaryId) . '/items'; $result = $this->_fetch($url, \Zend_Http_Client::PATCH, $params); return $result; @@ -1179,7 +1180,7 @@ public function createDictionaryItems($dictionaryId, $params) */ public function getDictionaries($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/dictionary'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/dictionary'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1195,7 +1196,7 @@ public function getDictionaries($version) */ public function deleteDictionaryItem($dictionaryId, $itemKey) { - $url = $this->_getApiServiceUri(). 'dictionary/'. rawurlencode($dictionaryId) . '/item/' . rawurlencode($itemKey); + $url = $this->_getApiServiceUri() . 'dictionary/' . rawurlencode($dictionaryId) . '/item/' . rawurlencode($itemKey); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -1212,7 +1213,7 @@ public function deleteDictionaryItem($dictionaryId, $itemKey) public function upsertDictionaryItem($dictionaryId, $itemKey, $itemValue) { $body = ['item_value' => $itemValue]; - $url = $this->_getApiServiceUri(). 'dictionary/'. rawurlencode($dictionaryId) . '/item/' . rawurlencode($itemKey); + $url = $this->_getApiServiceUri() . 'dictionary/' . rawurlencode($dictionaryId) . '/item/' . rawurlencode($itemKey); $result = $this->_fetch($url, \Zend_Http_Client::PUT, $body); if (!$result) { @@ -1229,7 +1230,7 @@ public function upsertDictionaryItem($dictionaryId, $itemKey, $itemValue) */ public function getSingleAcl($version, $acl) { - $url = $this->_getApiServiceUri(). 'version/'. $version . '/acl/' . $acl; + $url = $this->_getApiServiceUri() . 'version/' . $version . '/acl/' . $acl; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1245,7 +1246,7 @@ public function getSingleAcl($version, $acl) */ public function createAcl($version, $params) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/acl'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/acl'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $params); return $result; @@ -1260,7 +1261,7 @@ public function createAcl($version, $params) */ public function getAcls($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/acl'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/acl'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1276,7 +1277,7 @@ public function getAcls($version) */ public function deleteAcl($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/acl/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/acl/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -1291,7 +1292,7 @@ public function deleteAcl($version, $name) */ public function aclItemsList($aclId) { - $url = $this->_getApiServiceUri() . 'acl/'. rawurlencode($aclId) . '/entries'; + $url = $this->_getApiServiceUri() . 'acl/' . rawurlencode($aclId) . '/entries'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1320,7 +1321,7 @@ public function upsertAclItem($aclId, $itemValue, $negated, $comment = 'Added by $body['subnet'] = $subnet; } - $url = $this->_getApiServiceUri(). 'acl/'. rawurlencode($aclId) . '/entry'; + $url = $this->_getApiServiceUri() . 'acl/' . rawurlencode($aclId) . '/entry'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $body); return $result; @@ -1336,7 +1337,7 @@ public function upsertAclItem($aclId, $itemValue, $negated, $comment = 'Added by */ public function deleteAclItem($aclId, $aclItemId) { - $url = $this->_getApiServiceUri(). 'acl/'. rawurlencode($aclId) . '/entry/' . rawurlencode($aclItemId); + $url = $this->_getApiServiceUri() . 'acl/' . rawurlencode($aclId) . '/entry/' . rawurlencode($aclItemId); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -1366,7 +1367,7 @@ public function updateAclItem($aclId, $aclItemId, $itemValue, $negated, $comment $body['subnet'] = $subnet; } - $url = $this->_getApiServiceUri(). 'acl/'. rawurlencode($aclId) . '/entry/' . rawurlencode($aclItemId); + $url = $this->_getApiServiceUri() . 'acl/' . rawurlencode($aclId) . '/entry/' . rawurlencode($aclItemId); $result = $this->_fetch($url, \Zend_Http_Client::PATCH, json_encode($body)); return $result; @@ -1382,10 +1383,10 @@ public function updateAclItem($aclId, $aclItemId, $itemValue, $negated, $comment public function queryHistoricStats(array $parameters) { $uri = $this->_getHistoricalEndpoint() - . '?region='.rawurlencode($parameters['region']) - . '&from='.rawurlencode($parameters['from']) - . '&to='.rawurlencode($parameters['to']) - . '&by='.rawurlencode($parameters['sample_rate']); + . '?region=' . rawurlencode($parameters['region']) + . '&from=' . rawurlencode($parameters['from']) + . '&to=' . rawurlencode($parameters['to']) + . '&by=' . rawurlencode($parameters['sample_rate']); $result = $this->_fetch($uri); @@ -1423,7 +1424,7 @@ public function getParticularVersion($version) */ public function checkImageOptimizationStatus() { - $url = $this->_getApiServiceUri(). 'dynamic_io_settings'; + $url = $this->_getApiServiceUri() . 'dynamic_io_settings'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1438,7 +1439,7 @@ public function checkImageOptimizationStatus() */ public function getImageOptimizationDefaultConfigOptions($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/io_settings'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/io_settings'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1454,7 +1455,7 @@ public function getImageOptimizationDefaultConfigOptions($version) */ public function configureImageOptimizationDefaultConfigOptions($params, $version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/io_settings'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/io_settings'; $result = $this->_fetch($url, \Zend_Http_Client::PATCH, $params); return $result; @@ -1505,12 +1506,12 @@ public function getLastErrorMessage() /** * Wrapper for API calls towards Fastly service * - * @param string $uri API Endpoint - * @param string $method HTTP Method for request - * @param mixed[]|string $body Content - * @param bool $test Use $testApiKey for request - * @param string $testApiKey API key to be tested - * @param bool $logError When set to false, prevents writing failed requests to log + * @param string $uri API Endpoint + * @param string $method HTTP Method for request + * @param mixed[]|string $body Content + * @param bool $test Use $testApiKey for request + * @param string $testApiKey API key to be tested + * @param bool $logError When set to false, prevents writing failed requests to log * * @return bool|mixed Returns false on failiure * @throws LocalizedException @@ -1532,7 +1533,7 @@ private function _fetch( // Client headers $headers = [ - self::FASTLY_HEADER_AUTH . ': ' . $apiKey, + self::FASTLY_HEADER_AUTH . ': ' . $apiKey, 'Accept: application/json' ]; @@ -1605,7 +1606,7 @@ private function stackTrace($type) } } - $this->sendWebHook('*'. $type .' backtrace:*```' . implode("\n", $trace) . '```'); + $this->sendWebHook('*' . $type . ' backtrace:*```' . implode("\n", $trace) . '```'); } /** @@ -1623,4 +1624,19 @@ private function extractErrorDetails($responseBody, $responseMessage) } return $responseMessage; } + + /** + * Get all VCL snippets + * + * @param int $version + * @return bool|mixed + * @throws LocalizedException + */ + public function getSnippets(int $version) + { + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/snippet'; + $result = $this->_fetch($url, 'GET'); + + return $result; + } } diff --git a/Model/Snippet/BuiltInSnippetList.php b/Model/Snippet/BuiltInSnippetList.php new file mode 100644 index 00000000..4880af17 --- /dev/null +++ b/Model/Snippet/BuiltInSnippetList.php @@ -0,0 +1,44 @@ +predefinedList = $predefinedList; + } + + /** + * Check is builtin snippet + * + * @param string $snippetName + * @return bool + */ + public function checkIsBuiltInSnippet(string $snippetName): bool + { + foreach ($this->predefinedList as $builtinSnippet) { + if (\strpos($snippetName, $builtinSnippet) === 0) { + return true; + } + } + return false; + } +} diff --git a/etc/di.xml b/etc/di.xml index f2ee2e35..2f66f95a 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -91,4 +91,17 @@ + + + + + magentomodule_force_tls + magentomodule_gzip_safety + magentomodule_image_optimization + magentomodule_rate_limiting + magentomodule_error_page + magentomodule_waf + + +