PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / model / setting / user-agent-lockout.php

user-agent-lockout.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at src/model/setting/user-agent-lockout.php

207 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender\Model\Setting;
4
5 use Calotes\Model\Setting;
6
7 class User_Agent_Lockout extends Setting {
8 /**
9 * Option name.
10 * @var string
11 */
12 public $table = 'wd_user_agent_settings';
13
14 /**
15 * @var bool
16 * @defender_property
17 */
18 public $enabled = false;
19 /**
20 * @var string
21 * @defender_property
22 */
23 public $blacklist = '';
24 /**
25 * @var string
26 * @defender_property
27 */
28 public $whitelist = '';
29 /**
30 * @var string
31 * @defender_property
32 */
33 public $message = '';
34 /**
35 * @var bool
36 * @defender_property
37 */
38 public $empty_headers = false;
39
40 protected $rules = array(
41 array( array( 'enabled', 'empty_headers' ), 'boolean' ),
42 );
43
44 /**
45 * @return array
46 */
47 public function get_default_values() {
48 // Allowled User Agents.
49 $whitelist = "a6-indexer\nadsbot-google\naolbuild\napis-google\nbaidu\nbingbot\nbingpreview";
50 $whitelist .= "\nbutterfly\ncloudflare\nchrome\nduckduckbot\nembedly\nfacebookexternalhit\nfacebot\ngoogle page speed";
51 $whitelist .= "\ngooglebot\nia_archiver\nlinkedinbot\nmediapartners-google\nmsnbot\nnetcraftsurvey";
52 $whitelist .= "\noutbrain\npinterest\nquora\nslackbot\nslurp\ntweetmemebot\ntwitterbot\nuptimerobot";
53 $whitelist .= "\nurlresolver\nvkshare\nw3c_validator\nwordpress\nwp rocket\nyandex";
54
55 return array(
56 'message' => __( 'You have been blocked from accessing this website.', 'wpdef' ),
57 'whitelist' => $whitelist,
58 // Blocked User Agents.
59 'blacklist' => "MJ12Bot\nAhrefsBot\nSEMrushBot\nDotBot",
60 );
61 }
62
63 protected function before_load() {
64 $default_values = $this->get_default_values();
65 $this->message = $default_values['message'];
66 $this->whitelist = $default_values['whitelist'];
67 $this->blacklist = $default_values['blacklist'];
68 }
69
70 /**
71 * @return bool
72 */
73 public function is_active() {
74
75 return apply_filters(
76 'wd_user_agents_enable',
77 $this->enabled
78 );
79 }
80
81 /**
82 * Get list of blocklisted or allowlisted data.
83 *
84 * @param string $type blocklist|allowlist
85 * @param bool $lower
86 *
87 * @return array
88 */
89 public function get_lockout_list( $type = 'blocklist', $lower = true ) {
90 $data = ( 'blocklist' === $type ) ? $this->blacklist : $this->whitelist;
91 $arr = is_array( $data ) ? $data : array_filter( explode( PHP_EOL, $data ) );
92 $arr = array_map( 'trim', $arr );
93 if ( $lower ) {
94 $arr = array_map( 'strtolower', $arr );
95 }
96
97 return $arr;
98 }
99
100 /**
101 * Define labels for settings key.
102 *
103 * @param string|null $key
104 *
105 * @return string|array|null
106 */
107 public function labels( $key = null ) {
108 $labels = array(
109 'enabled' => __( 'User Agent Banning', 'wpdef' ),
110 'message' => __( 'Message', 'wpdef' ),
111 'blacklist' => __( 'Blocklist', 'wpdef' ),
112 'whitelist' => __( 'Allowlist', 'wpdef' ),
113 'empty_headers' => __( 'Empty Headers', 'wpdef' ),
114 );
115
116 if ( ! is_null( $key ) ) {
117 return $labels[ $key ] ?? null;
118 }
119
120 return $labels;
121 }
122
123 /**
124 * Get the access status of this UA.
125 *
126 * @param string $ua
127 *
128 * @return array
129 */
130 public function get_access_status( $ua ) {
131 $blocklist = $this->get_lockout_list( 'blocklist' );
132 $allowlist = $this->get_lockout_list( 'allowlist' );
133 if ( ! in_array( $ua, $blocklist, true ) && ! in_array( $ua, $allowlist, true ) ) {
134
135 return array( 'na' );
136 }
137
138 $result = array();
139 if ( in_array( $ua, $this->get_lockout_list( 'blocklist' ), true ) ) {
140 $result[] = 'banned';
141 }
142 if ( in_array( $ua, $this->get_lockout_list( 'allowlist' ), true ) ) {
143 $result[] = 'allowlist';
144 }
145
146 return $result;
147 }
148
149 /**
150 * @param string $ua
151 * @param string $list blocklist|allowlist
152 *
153 * @return bool
154 */
155 public function is_ua_in_list( $ua, $list ) {
156 $arr = $this->get_lockout_list( $list );
157
158 return in_array( $ua, $arr, true );
159 }
160
161 /**
162 * Remove User Agent from a list.
163 *
164 * @param string $ua
165 * @param string $list blocklist|allowlist
166 *
167 * @return void
168 */
169 public function remove_from_list( $ua, $list ) {
170 $arr = $this->get_lockout_list( $list );
171 // Array can contain uppercase.
172 $orig_arr = $this->get_lockout_list( $list, false );
173 $key = array_search( $ua, $arr, true );
174 if ( false !== $key ) {
175 unset( $orig_arr[ $key ] );
176 if ( 'blocklist' === $list ) {
177 $this->blacklist = implode( PHP_EOL, $orig_arr );
178 } elseif ( 'allowlist' === $list ) {
179 $this->whitelist = implode( PHP_EOL, $orig_arr );
180 }
181
182 $this->save();
183 }
184 }
185
186 /**
187 * Add an UA to the list.
188 *
189 * @param string $ua
190 * @param string $list blocklist|allowlist
191 *
192 * @return void
193 */
194 public function add_to_list( $ua, $list ) {
195 $arr = $this->get_lockout_list( $list, false );
196 $arr[] = trim( $ua );
197 $arr = array_unique( $arr );
198 if ( 'blocklist' === $list ) {
199 $this->blacklist = implode( PHP_EOL, $arr );
200 } elseif ( 'allowlist' === $list ) {
201 $this->whitelist = implode( PHP_EOL, $arr );
202 }
203
204 $this->save();
205 }
206 }
207