Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.idea
/composer.lock
/tests/.phpunit.result.cache
/vendor/
98 changes: 98 additions & 0 deletions BotDetectorService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Cleantalk\Common\BotDetectorService;

abstract class BotDetectorService
{
const METHOD_NAME = "get_bot_detector_wrapper_url";

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
*/
abstract public function callAPIMethod($api_key);

/**
* @param string $wrapper_url
* @return bool
*/
abstract public function isWrapperAvailable($wrapper_url);

/**
* @param string $wrapper_url
* @return void
*/
abstract public function saveWrapperURL($wrapper_url);

/**
* @return string
*/
abstract public function loadWrapperURL();
Comment thread
Glomberg marked this conversation as resolved.

/**
* @return string
*/
public function getWrapperURL()
{
if ( $url_from_bd = $this->loadWrapperURL() ) {
Comment thread
Glomberg marked this conversation as resolved.
Outdated
return htmlspecialchars($url_from_bd, ENT_QUOTES);
Comment thread
Glomberg marked this conversation as resolved.
Outdated
}

return htmlspecialchars(
sprintf("https://%s/%s", self::BD_DEFAULT_DOMAIN, self::BD_DEFAULT_SCRIPT_NAME),
ENT_QUOTES
);
Comment thread
Glomberg marked this conversation as resolved.
}

/**
* @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)
{
return (bool) preg_match("/^https?:\/\/.*cleantalk\..*/", $wrapper_url);
}
Comment thread
Glomberg marked this conversation as resolved.

/**
* @param $wrapper_url
* @return bool
*/
private function validateWrapperContent($wrapper_url)
{
// 1) Get content from $wrapper_url
// 2) Validate this
return true;
}
Comment thread
Glomberg marked this conversation as resolved.
}
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"Cleantalk\\Common\\BotDetectorService\\": "/"
}
},
"require": {
"cleantalk/http": "*"
},
Comment thread
Glomberg marked this conversation as resolved.
"require-dev": {
"vimeo/psalm": "^4.8",
"phpunit/phpunit": "^8.5.52",
Expand All @@ -33,4 +36,4 @@
"vendor/bin/psalm --no-cache --config=tests/psalm.xml --taint-analysis"
]
}
}
}
Loading