diff --git a/.gitignore b/.gitignore index 485dee6..ecbdba1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .idea +/composer.lock +/tests/.phpunit.result.cache +/vendor/ diff --git a/Api/Api.php b/Api/Api.php new file mode 100644 index 0000000..4ab314b --- /dev/null +++ b/Api/Api.php @@ -0,0 +1,22 @@ + self::METHOD_NAME, + 'auth_key' => $api_key, + ); + + return self::sendRequest($request);*/ + } +} diff --git a/BotDetectorService.php b/BotDetectorService.php new file mode 100644 index 0000000..fd41282 --- /dev/null +++ b/BotDetectorService.php @@ -0,0 +1,131 @@ +setUrl($wrapper_url) + ->setPresets(['get_code']) + ->request(); + + return 200 === $response; + } + + /** + * @param string $wrapper_url + * @return void + */ + abstract public function saveWrapperURL($wrapper_url); + + /** + * @return string|false + */ + abstract public function loadWrapperURL(); + + /** + * @return string + */ + public function getWrapperURL() + { + $url_from_bd = $this->loadWrapperURL(); + if ( $url_from_bd ) { + return htmlspecialchars($url_from_bd, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + } + + return htmlspecialchars( + sprintf("https://%s/%s", self::BD_DEFAULT_DOMAIN, self::BD_DEFAULT_SCRIPT_NAME), + ENT_QUOTES | ENT_SUBSTITUTE, + 'UTF-8' + ); + } + + /** + * @param string $api_key + * @return void + */ + public function updateWrapperURL($api_key) + { + $url_from_api = $this->callAPIMethod($api_key); + if ( ! $this->validateWrapperURL($url_from_api) ) { + return; + } + if ( ! $this->isWrapperAvailable($url_from_api) ) { + return; + } + if ( ! $this->validateWrapperContent($url_from_api) ) { + return; + } + + $url_from_bd = $this->loadWrapperURL(); + + if ( $url_from_api !== $url_from_bd ) { + $this->saveWrapperURL($url_from_api); + } + } + + /** + * @param string $wrapper_url + * @return bool + */ + private function validateWrapperURL($wrapper_url) + { + if ( ! is_string($wrapper_url) || $wrapper_url === '' ) { + return false; + } + $parts = parse_url($wrapper_url); + if ( empty($parts['scheme']) || empty($parts['host']) ) { + return false; + } + $scheme = strtolower($parts['scheme']); + if ( $scheme !== 'https' && $scheme !== 'http' ) { + return false; + } + $host = strtolower($parts['host']); + + // Allow cleantalk. and subdomains like fd.cleantalk.org + return (bool) preg_match('/(^|\.)cleantalk\.[a-z0-9-]{2,}$/', $host); + } + + /** + * @param $wrapper_url + * @return bool + */ + private function validateWrapperContent($wrapper_url) + { + // 1) Get content from $wrapper_url + // 2) Validate this + return true; + } +} diff --git a/composer.json b/composer.json index 8e98efb..e4893f2 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,13 @@ "Cleantalk\\Common\\BotDetectorService\\": "/" } }, + "require": { + "cleantalk/http": "*", + "cleantalk/templates": "*", + "cleantalk/api": "*", + "cleantalk/mloader": "*" + + }, "require-dev": { "vimeo/psalm": "^4.8", "phpunit/phpunit": "^8.5.52", @@ -33,4 +40,4 @@ "vendor/bin/psalm --no-cache --config=tests/psalm.xml --taint-analysis" ] } -} \ No newline at end of file +}