-
Notifications
You must be signed in to change notification settings - Fork 0
New. Code. Library BotDetectorService implementation.
#1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bcf60ae
New. Code. Implementing the library main class.
Glomberg 4c5f339
Fix. Code. Code style fixed.
Glomberg 4083a86
Fix. Code. Copilot review fixed #1.
Glomberg b2e4e52
Fix. Code. Copilot review fixed #2.
Glomberg 033b188
Fix. Code. Copilot review fixed #3.
Glomberg 2d0147b
Fix. Code. Review notice fixed.
Glomberg 666b221
Fix. API. API calling implemented.
Glomberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| .idea | ||
| /composer.lock | ||
| /tests/.phpunit.result.cache | ||
| /vendor/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| namespace Cleantalk\Common\BotDetectorService\Api; | ||
|
|
||
| use Cleantalk\Common\BotDetectorService\BotDetectorService; | ||
|
|
||
| class Api extends \Cleantalk\Common\Api\Api | ||
| { | ||
| const METHOD_NAME = "get_bot_detector_wrapper_url"; | ||
|
|
||
| public static function methodGetBotDetectorWrapperUrl($api_key) | ||
| { | ||
| return false; | ||
|
|
||
| /*$request = array( | ||
| 'method_name' => self::METHOD_NAME, | ||
| 'auth_key' => $api_key, | ||
| ); | ||
| return self::sendRequest($request);*/ | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| <?php | ||
|
|
||
| namespace Cleantalk\Common\BotDetectorService; | ||
|
|
||
| use Cleantalk\Common\BotDetectorService\Api\Api; | ||
| use Cleantalk\Common\Mloader\Mloader; | ||
| use Cleantalk\Common\Templates\Singleton; | ||
|
|
||
| abstract class BotDetectorService | ||
| { | ||
| use Singleton; | ||
|
|
||
| const BD_DEFAULT_DOMAIN = "fd.cleantalk.org"; | ||
|
|
||
| const BD_DEFAULT_SCRIPT_NAME = "ct-bot-detector-wrapper.js"; | ||
|
|
||
| const CALL_PERIOD = 3600; | ||
|
|
||
| const OPTION_NAME = "bot_detector_wrapper_url"; | ||
|
|
||
| /** | ||
| * @param string $api_key | ||
| * @return string|false | ||
| */ | ||
| public function callAPIMethod($api_key) | ||
| { | ||
| return Api::methodGetBotDetectorWrapperUrl($api_key); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $wrapper_url | ||
| * @return bool | ||
| */ | ||
| public function isWrapperAvailable($wrapper_url) | ||
| { | ||
| $request_class = Mloader::get('Http\Request'); | ||
| $http = new $request_class(); | ||
|
|
||
| $response = $http->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.<domain> 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; | ||
| } | ||
|
Glomberg marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.