-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotDetectorService.php
More file actions
131 lines (110 loc) · 3.18 KB
/
Copy pathBotDetectorService.php
File metadata and controls
131 lines (110 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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;
}
}