PluginProbe
WebTotem Security / 2.4.36
WebTotem Security v2.4.36
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / lib / Interface.php

Interface.php in WebTotem Security 2.4.36, at lib/Interface.php

487 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
4 if (!headers_sent()) {
5 header('HTTP/1.1 403 Forbidden');
6 }
7 die("Protected By WebTotem!");
8 }
9
10 /**
11 * Plugin initializer.
12 *
13 */
14 class WebTotemInterface extends WebTotem {
15
16 /**
17 * Execute pre-checks before every page.
18 *
19 * @return void
20 */
21 public static function startupChecks() {
22
23 /** Hide or show WP version */
24 if (WebTotemOption::getPluginSettings('hide_wp_version')) {
25 WebTotemOption::hideWPVersion();
26
27 add_filter('style_loader_src', 'WebTotemOption::replaceVersion');
28 add_filter('script_loader_src', 'WebTotemOption::replaceVersion');
29
30 }
31
32 /** Disable user enumeration */
33 if (WebTotemOption::getPluginSettings('disable_user_enumeration')) {
34 if (!is_admin()) {
35 // default URL format
36 if (preg_match('/author=([0-9]*)/i', $_SERVER['QUERY_STRING'])) {
37 header("Location: " . get_home_url());
38 die();
39 }
40 add_filter('redirect_canonical', 'wtsec_check_enum', 10, 2);
41
42 }
43
44 function wtsec_check_enum($redirect, $request) {
45 // permalink URL format
46 if (preg_match('/\?author=([0-9]*)(\/*)/i', $request)) {
47 header("Location: " . get_home_url());
48 die();
49 } else {
50 return $redirect;
51 }
52 }
53
54 }
55
56
57 $_page = WebTotemRequest::get('page');
58 if(strpos($_page, 'wtotem') === 0 ) {
59 $composer_autoload = WEBTOTEM_PLUGIN_PATH . '/vendor/autoload.php';
60 if ( file_exists( $composer_autoload ) ) {
61 require_once $composer_autoload;
62 }
63 }
64
65 $_page = WebTotemRequest::get('page');
66 if(strpos($_page, 'wtotem') === 0){
67
68 if(!WebTotemOption::isActivated()){
69 // Checking the old version of options.
70 WebTotemOption::checkOldOptions();
71 }
72
73 WebTotemOption::multisiteCheck();
74
75 if(!WebTotemOption::isActivated() and $_page !== 'wtotem_activation') {
76 // If the plugin is not activated by the API key, then redirect to the activation page.
77 wp_safe_redirect( WebTotem::adminURL('admin.php?page=wtotem_activation') );
78 exit;
79 }
80 elseif (WebTotemOption::isActivated() and ($_page === 'wtotem_activation' or $_page === 'wtotem')){
81 // If the plugin is activated by the API key, then redirect to the main page.
82 if(self::isMultiSite() and is_super_admin()){
83 // Main page is all sites page.
84 wp_safe_redirect( WebTotem::adminURL('admin.php?page=wtotem_all_sites') );
85 } else {
86 // Main page is dashboard page.
87 wp_safe_redirect( WebTotem::adminURL('admin.php?page=wtotem_dashboard') );
88 }
89 exit;
90 }
91 elseif(WebTotemOption::isActivated()) {
92 // Checking whether agents are installed, if they are not installed, then install.
93 self::checkAgents();
94 self::checkSiteAddedDate();
95 }
96 }
97
98 // Check if the plugin version has changed.
99 WebTotemAgentManager::checkVersion();
100
101 $sapi = @php_sapi_name();
102 if( $sapi != "cli" ) {
103 if ($waf = WebTotemOption::getOption("waf_file")) {
104 $include_waf_file = ABSPATH . '_include_' . $waf;
105
106 if (is_file($include_waf_file) && is_readable($include_waf_file)) {
107 include_once $include_waf_file;
108 }
109 }
110 }
111 }
112
113 /**
114 * Checking whether agents are installed, if they are not installed, then install.
115 */
116 private static function checkAgents(){
117
118 $api_key = WebTotemOption::getOption('api_key');
119
120 $host = WebTotemAPI::siteInfo();
121
122 if ($api_key && array_key_exists('id', $host)) {
123
124 // Install Agent Manager if it was not previously installed.
125 $am_installed = WebTotemAgentManager::checkInstalledService('am');
126 if (!$am_installed['file_status']) {
127
128 $am_was_installed = WebTotemAgentManager::amInstall();
129
130 if (!$am_was_installed) {
131 WebTotemOption::setOptions(['am_installed' => FALSE]);
132 }
133 }
134
135 }
136 }
137
138 /**
139 * Checking whether the site added date has been saved, if not, then requests the data and saves.
140 */
141 private static function checkSiteAddedDate(){
142 if (WebTotemRequest::get('hid')) {
143 $host = WebTotemOption::getHost(WebTotemRequest::get('hid'));
144 } else {
145 $host = WebTotemAPI::siteInfo();
146 }
147
148 $site_created_at = WebTotemOption::getOption('site_created_at');
149
150 $site_created_at = $site_created_at ? json_decode($site_created_at, true) : [];
151
152 if(!array_key_exists($host['name'], $site_created_at)){
153 $createdAt = WebTotemAPI::getGetSiteAddedDate($host['name']);
154 if($createdAt){
155 WebTotemOption::setOptions(['site_created_at' => [ $host['name'] => $createdAt] ]);
156 }
157 }
158 }
159
160 /**
161 * When adding a new site, add it to the WebTotem platform.
162 */
163 public static function addNewSite($new_site){
164 $domain = untrailingslashit($new_site->domain . $new_site->path);
165
166 WebTotemAPI::addMultiSiteNewSites([$domain]);
167 }
168
169 /**
170 * Verify the nonce of the previous page after a form submission.
171 *
172 * @return bool True if the nonce is valid, false otherwise.
173 */
174 public static function checkNonce() {
175 if (!empty($_POST)) {
176 $name = 'wtotem_page_nonce';
177 $value = WebTotemRequest::post($name);
178
179 if (!$value || !wp_verify_nonce($value, $name)) {
180 WebTotemOption::setNotification('error', __('The WordPress CSRF check failed. The submitted form is missing an important unique code. Go back and try again.', 'wtotem'));
181 return false;
182 }
183 }
184
185 return true;
186 }
187
188 /**
189 * Add 2fa to the profile form.
190 *
191 * @return void
192 */
193 public static function add2faProfileForm(){
194
195 if(!WebTotemLogin::isTwoFactorEnabled()){ return; }
196
197 if ( isset( $_GET['user_id'] ) ) {
198 if( !current_user_can( 'manage_options' ) ){
199 return;
200 }
201 $user_id = (int) $_GET['user_id'];
202 $user = get_user_by( 'id', $user_id );
203 } else {
204 $user = wp_get_current_user();
205 }
206
207 $current_user = wp_get_current_user();
208
209 if ( ! is_a( $user, '\WP_User' ) || ! is_a( $current_user, '\WP_User' ) ) {
210 return;
211 }
212
213 $composer_autoload = WEBTOTEM_PLUGIN_PATH . '/vendor/autoload.php';
214 if ( file_exists( $composer_autoload ) ) {
215 require_once $composer_autoload;
216 }
217
218 $template = new WebTotemTemplate();
219
220 $build[] = [
221 'template' => 'two_factor_user_profile_modal',
222 'variables' => [
223 'two_factor' => WebTotemLogin::getTwoFactorData($user),
224 'user_id' => $user_id ?? $user->ID,
225 'can_manage_options' => current_user_can( 'manage_options' )
226 ],
227 ];
228
229 $page_content = $template->arrayRender($build);
230 echo $page_content;
231 }
232
233 /**
234 * Authentication.
235 *
236 * @return mixed
237 */
238 public static function wt_authenticate($user, $username = null, $password = null) {
239
240 if(WebTotemCaptcha::isEnabled()) {
241 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
242 return $user;
243 }
244 $token = WebTotemCaptcha::get_token();
245 $score = WebTotemCaptcha::score($token, WebTotemOption::getPluginSettings('recaptcha_v3_secret'));
246 if($score < 0.5) {
247 return new \WP_Error('authentication_failed', __('<strong>ERROR</strong>&nbsp;: Please check the ReCaptcha box or try to reload page.','wtotem'));
248 }
249 }
250
251 if(isset($_POST['wtotem-token']) && is_string($_POST['wtotem-token']) && !empty($_POST['wtotem-token'])){
252 if ( is_object( $user ) && ( $user instanceof \WP_User ) ) {
253 if ( WebTotemLogin::hasUser2faActivated( $user ) ) {
254 $check2faCode = WebTotemLogin::check2faCode( $user, $_POST['wtotem-token']);
255
256 if ( ! $check2faCode ) {
257 return new \WP_Error( 'wtotem_two_factor_failed', wp_kses( __( '<strong>CODE INVALID</strong>: The 2FA code provided is either expired or invalid. Please try again.', 'wtotem' ), array( 'strong' => array() ) ) );
258 }
259 }
260 }
261 }
262
263 return WebTotemBFProtection::checkBruteForceAttempts($user, $username);
264 }
265
266 /**
267 * Password brute force protection.
268 *
269 * @return mixed
270 */
271 public static function wt_lost_password($errors = null, $user_data = null) {
272 return WebTotemBFProtection::lostPassword($errors);
273 }
274
275 /**
276 * Restore and then hide the readme file when updating the WordPress.
277 *
278 * @param string $string
279 * @return string
280 */
281 public static function restoreReadmeWhenUpdating($string) {
282 static $didRun;
283 if (!isset($didRun)) {
284 $didRun = true;
285 WebTotemOption::restoreReadme();
286 register_shutdown_function('WebTotemOption::hideReadme');
287 }
288
289 return $string;
290 }
291
292 /**
293 * Login Page
294 */
295 public static function loginEnqueueScripts() {
296
297 $recaptcha_enabled = WebTotemCaptcha::isEnabled();
298 if ($recaptcha_enabled) {
299 $recaptcha_site_key = WebTotemOption::getPluginSettings('recaptcha_v3_site_key');
300 wp_enqueue_script('wtotem_recaptcha', 'https://www.google.com/recaptcha/api.js?render=' . $recaptcha_site_key);
301 }
302
303 wp_register_script(
304 'wtotem_login',
305 WEBTOTEM_URL . '/includes/js/login.js',
306 ['jquery'],
307 WebTotem::fileVersion('includes/js/login.js'),
308 false
309 );
310 wp_enqueue_script('wtotem_login');
311
312 wp_register_style(
313 'wtotem_login',
314 WEBTOTEM_URL . '/includes/css/login.css',
315 [],
316 WebTotem::fileVersion('includes/css/login.css')
317 );
318 wp_enqueue_style('wtotem_login');
319
320 wp_localize_script('wtotem_login', 'wtotem_login_vars', [
321 'recaptcha_site_key' => WebTotemCaptcha::_siteKey(),
322 'recaptcha_is_enabled' => $recaptcha_enabled,
323 'ajaxurl' => admin_url('admin-ajax.php', 'relative'),
324 'two_factor_is_enabled' => WebTotemLogin::isTwoFactorEnabled() and WebTotemLogin::anyTwoFactorActivated(),
325 ]);
326 }
327
328 /**
329 * Added a pop-up window to the plugins page
330 */
331 public static function registerDeletePrompt() {
332 wp_register_style(
333 'wtotem_prompt_css',
334 WEBTOTEM_URL . '/includes/css/prompt.css',
335 [],
336 WebTotem::fileVersion('includes/css/prompt.css')
337 );
338 wp_enqueue_style('wtotem_prompt_css');
339
340 $composer_autoload = WEBTOTEM_PLUGIN_PATH . '/vendor/autoload.php';
341 if ( file_exists( $composer_autoload ) ) {
342 require_once $composer_autoload;
343 }
344
345 $template = new WebTotemTemplate();
346 $build[] = [
347 'variables' => [
348 'message' => __('Are you sure you want to deactivate the plugin?<br>Don\'t worry, even after removing the plugin, our system will continue to protect your site: <ul class="confirmation-dialog__list"><li>the current host will remain in the account</li><li>agents will stay on the current site</li><li>accumulated history, monitoring, agent management and all other functions are available in the account on the <a href="https://wtotem.com" target="_blank">site</a></li></ul>', 'wtotem'),
349 'action' => 'reinstall_agents',
350 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
351 ],
352 'template' => 'prompt',
353 ];
354
355 echo $template->arrayRender($build);
356 }
357
358 /**
359 * A safe way to add JavaScript and css files to a WordPress-managed page
360 *
361 * @return void
362 */
363 public static function enqueueScripts() {
364
365 // Adding CSS files.
366 wp_register_style(
367 'wtotem_flatpickr',
368 WEBTOTEM_URL . '/includes/css/flatpickr.min.css',
369 [],
370 WebTotem::fileVersion('includes/css/flatpickr.min.css')
371 );
372 wp_enqueue_style('wtotem_flatpickr');
373
374 wp_register_style(
375 'wtotem_toastr_css',
376 WEBTOTEM_URL . '/includes/css/toastr.min.css',
377 [],
378 WebTotem::fileVersion('includes/css/toastr.min.css')
379 );
380 wp_enqueue_style('wtotem_toastr_css');
381
382 wp_register_style(
383 'wtotem_main_css',
384 WEBTOTEM_URL . '/includes/css/main.css',
385 [],
386 WebTotem::fileVersion('includes/css/main.css')
387 );
388 wp_enqueue_style('wtotem_main_css');
389
390 // Adding JS files.
391 wp_register_script(
392 'wtotem_amplitude',
393 WEBTOTEM_URL . '/includes/js/amplitude.js',
394 [ 'jquery' ],
395 WebTotem::fileVersion('includes/js/amplitude.js'),
396 false
397 );
398 wp_enqueue_script('wtotem_amplitude');
399
400 wp_register_script(
401 'wtotem_d3',
402 WEBTOTEM_URL . '/includes/js/d3.v4.js',
403 ['jquery'],
404 WebTotem::fileVersion('includes/js/d3.v4.js'),
405 true
406 );
407 wp_enqueue_script('wtotem_d3');
408
409 wp_register_script(
410 'wtotem_chart',
411 WEBTOTEM_URL . '/includes/js/chart.js',
412 ['jquery', 'wtotem_d3', 'wtotem_jsdelivr'],
413 WebTotem::fileVersion('includes/js/chart.js'),
414 true
415 );
416 wp_enqueue_script('wtotem_chart');
417
418 wp_register_script(
419 'wtotem_flatpickr_js',
420 WEBTOTEM_URL . '/includes/js/flatpickr.js',
421 [ 'jquery', 'wp-i18n' ],
422 WebTotem::fileVersion('includes/js/flatpickr.js'),
423 true
424 );
425 wp_set_script_translations( 'wtotem_flatpickr_js', 'wtotem', WEBTOTEM_PLUGIN_PATH . '/lang/');
426 wp_enqueue_script('wtotem_flatpickr_js');
427
428 wp_register_script(
429 'wtotem_jsdelivr',
430 WEBTOTEM_URL . '/includes/js/jsdelivr_chart.js',
431 [ 'jquery' ],
432 WebTotem::fileVersion('includes/js/jsdelivr_chart.js'),
433 true
434 );
435 wp_enqueue_script('wtotem_jsdelivr');
436
437 wp_register_script(
438 'wtotem_jquery_qrcode',
439 WEBTOTEM_URL . '/includes/js/jquery.qrcode.min.js',
440 [ 'jquery' ],
441 WebTotem::fileVersion('includes/js/jquery.qrcode.min.js'),
442 true
443 );
444 wp_enqueue_script('wtotem_jquery_qrcode');
445
446 wp_register_script(
447 'wtotem_progress_bar',
448 WEBTOTEM_URL . '/includes/js/progress_bar.js',
449 [],
450 WebTotem::fileVersion('includes/js/progress_bar.js'),
451 true
452 );
453 wp_enqueue_script('wtotem_progress_bar');
454
455 wp_register_script(
456 'wtotem_toastr',
457 WEBTOTEM_URL . '/includes/js/toastr.min.js',
458 [],
459 WebTotem::fileVersion('includes/js/toastr.min.js'),
460 true
461 );
462 wp_enqueue_script('wtotem_toastr');
463
464 $_page = WebTotemRequest::get('page');
465 if($_page === 'wtotem_settings'){
466 wp_register_script(
467 'wtotem_country_blocking',
468 WEBTOTEM_URL . '/includes/js/country-blocking.js',
469 ['wp-i18n'],
470 WebTotem::fileVersion('includes/js/country-blocking.js'),
471 true
472 );
473 wp_set_script_translations( 'wtotem_country_blocking', 'wtotem' , WEBTOTEM_PLUGIN_PATH . '/lang/');
474 wp_enqueue_script('wtotem_country_blocking');
475 }
476
477 wp_register_script(
478 'wtotem_main',
479 WEBTOTEM_URL . '/includes/js/main.js',
480 ['jquery'],
481 WebTotem::fileVersion('includes/js/main.js'),
482 true
483 );
484 wp_enqueue_script('wtotem_main');
485 }
486 }
487