PluginProbe
WebTotem Security / 2.4.29
WebTotem Security v2.4.29
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.29, at lib/Interface.php

464 lines 14.0 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 }
95 }
96
97 // Check if the plugin version has changed.
98 WebTotemAgentManager::checkVersion();
99
100 $sapi = @php_sapi_name();
101 if( $sapi != "cli" ) {
102 if ($waf = WebTotemOption::getOption("waf_file")) {
103 $include_waf_file = ABSPATH . '/_include_' . $waf;
104
105 if (is_file($include_waf_file) && is_readable($include_waf_file)) {
106 include_once $include_waf_file;
107 }
108 }
109 }
110 }
111
112 /**
113 * Checking whether agents are installed, if they are not installed, then install.
114 */
115 private static function checkAgents(){
116
117 $api_key = WebTotemOption::getOption('api_key');
118
119 $host = WebTotemAPI::siteInfo();
120
121 if ($api_key && array_key_exists('id', $host)) {
122
123 // Install Agent Manager if it was not previously installed.
124 $am_installed = WebTotemAgentManager::checkInstalledService('am');
125 if (!$am_installed['file_status']) {
126
127 $am_was_installed = WebTotemAgentManager::amInstall();
128
129 if (!$am_was_installed) {
130 WebTotemOption::setOptions(['am_installed' => FALSE]);
131 }
132 }
133
134 }
135 }
136
137 /**
138 * When adding a new site, add it to the WebTotem platform.
139 */
140 public static function addNewSite($new_site){
141 $domain = untrailingslashit($new_site->domain . $new_site->path);
142
143 WebTotemAPI::addMultiSiteNewSites([$domain]);
144 }
145
146 /**
147 * Verify the nonce of the previous page after a form submission.
148 *
149 * @return bool True if the nonce is valid, false otherwise.
150 */
151 public static function checkNonce() {
152 if (!empty($_POST)) {
153 $name = 'wtotem_page_nonce';
154 $value = WebTotemRequest::post($name);
155
156 if (!$value || !wp_verify_nonce($value, $name)) {
157 WebTotemOption::setNotification('error', __('The WordPress CSRF check failed. The submitted form is missing an important unique code. Go back and try again.', 'wtotem'));
158 return false;
159 }
160 }
161
162 return true;
163 }
164
165 /**
166 * Add 2fa to the profile form.
167 *
168 * @return void
169 */
170 public static function add2faProfileForm(){
171
172 if(!WebTotemLogin::isTwoFactorEnabled()){ return; }
173
174 if ( isset( $_GET['user_id'] ) ) {
175 if( !current_user_can( 'manage_options' ) ){
176 return;
177 }
178 $user_id = (int) $_GET['user_id'];
179 $user = get_user_by( 'id', $user_id );
180 } else {
181 $user = wp_get_current_user();
182 }
183
184 $current_user = wp_get_current_user();
185
186 if ( ! is_a( $user, '\WP_User' ) || ! is_a( $current_user, '\WP_User' ) ) {
187 return;
188 }
189
190 $composer_autoload = WEBTOTEM_PLUGIN_PATH . '/vendor/autoload.php';
191 if ( file_exists( $composer_autoload ) ) {
192 require_once $composer_autoload;
193 }
194
195 $template = new WebTotemTemplate();
196
197 $build[] = [
198 'template' => 'two_factor_user_profile_modal',
199 'variables' => [
200 'two_factor' => WebTotemLogin::getTwoFactorData($user),
201 'user_id' => $user_id ?? $user->ID,
202 'can_manage_options' => current_user_can( 'manage_options' )
203 ],
204 ];
205
206 $page_content = $template->arrayRender($build);
207 echo $page_content;
208 }
209
210 /**
211 * Authentication.
212 *
213 * @return mixed
214 */
215 public static function wt_authenticate($user, $username = null, $password = null) {
216
217 if(WebTotemCaptcha::isEnabled()) {
218 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
219 return $user;
220 }
221 $token = WebTotemCaptcha::get_token();
222 $score = WebTotemCaptcha::score($token, WebTotemOption::getPluginSettings('recaptcha_v3_secret'));
223 if($score < 0.5) {
224 return new \WP_Error('authentication_failed', __('<strong>ERROR</strong>&nbsp;: Please check the ReCaptcha box or try to reload page.','wtotem'));
225 }
226 }
227
228 if(isset($_POST['wtotem-token']) && is_string($_POST['wtotem-token']) && !empty($_POST['wtotem-token'])){
229 if ( is_object( $user ) && ( $user instanceof \WP_User ) ) {
230 if ( WebTotemLogin::hasUser2faActivated( $user ) ) {
231 $check2faCode = WebTotemLogin::check2faCode( $user, $_POST['wtotem-token']);
232
233 if ( ! $check2faCode ) {
234 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() ) ) );
235 }
236 }
237 }
238 }
239
240 return WebTotemBFProtection::checkBruteForceAttempts($user, $username);
241 }
242
243 /**
244 * Password brute force protection.
245 *
246 * @return mixed
247 */
248 public static function wt_lost_password($errors = null, $user_data = null) {
249 return WebTotemBFProtection::lostPassword($errors);
250 }
251
252 /**
253 * Restore and then hide the readme file when updating the WordPress.
254 *
255 * @param string $string
256 * @return string
257 */
258 public static function restoreReadmeWhenUpdating($string) {
259 static $didRun;
260 if (!isset($didRun)) {
261 $didRun = true;
262 WebTotemOption::restoreReadme();
263 register_shutdown_function('WebTotemOption::hideReadme');
264 }
265
266 return $string;
267 }
268
269 /**
270 * Login Page
271 */
272 public static function loginEnqueueScripts() {
273
274 $recaptcha_enabled = WebTotemCaptcha::isEnabled();
275 if ($recaptcha_enabled) {
276 $recaptcha_site_key = WebTotemOption::getPluginSettings('recaptcha_v3_site_key');
277 wp_enqueue_script('wtotem_recaptcha', 'https://www.google.com/recaptcha/api.js?render=' . $recaptcha_site_key);
278 }
279
280 wp_register_script(
281 'wtotem_login',
282 WEBTOTEM_URL . '/includes/js/login.js',
283 ['jquery'],
284 WebTotem::fileVersion('includes/js/login.js'),
285 false
286 );
287 wp_enqueue_script('wtotem_login');
288
289 wp_register_style(
290 'wtotem_login',
291 WEBTOTEM_URL . '/includes/css/login.css',
292 [],
293 WebTotem::fileVersion('includes/css/login.css')
294 );
295 wp_enqueue_style('wtotem_login');
296
297 wp_localize_script('wtotem_login', 'wtotem_login_vars', [
298 'recaptcha_site_key' => WebTotemCaptcha::_siteKey(),
299 'recaptcha_is_enabled' => $recaptcha_enabled,
300 'ajaxurl' => admin_url('admin-ajax.php', 'relative'),
301 'two_factor_is_enabled' => WebTotemLogin::isTwoFactorEnabled() and WebTotemLogin::anyTwoFactorActivated(),
302 ]);
303 }
304
305 /**
306 * Added a pop-up window to the plugins page
307 */
308 public static function registerDeletePrompt() {
309 wp_register_style(
310 'wtotem_prompt_css',
311 WEBTOTEM_URL . '/includes/css/prompt.css',
312 [],
313 WebTotem::fileVersion('includes/css/prompt.css')
314 );
315 wp_enqueue_style('wtotem_prompt_css');
316
317 $composer_autoload = WEBTOTEM_PLUGIN_PATH . '/vendor/autoload.php';
318 if ( file_exists( $composer_autoload ) ) {
319 require_once $composer_autoload;
320 }
321
322 $template = new WebTotemTemplate();
323 $build[] = [
324 'variables' => [
325 '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'),
326 'action' => 'reinstall_agents',
327 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
328 ],
329 'template' => 'prompt',
330 ];
331
332 echo $template->arrayRender($build);
333 }
334
335 /**
336 * A safe way to add JavaScript and css files to a WordPress-managed page
337 *
338 * @return void
339 */
340 public static function enqueueScripts() {
341
342 // Adding CSS files.
343 wp_register_style(
344 'wtotem_flatpickr',
345 WEBTOTEM_URL . '/includes/css/flatpickr.min.css',
346 [],
347 WebTotem::fileVersion('includes/css/flatpickr.min.css')
348 );
349 wp_enqueue_style('wtotem_flatpickr');
350
351 wp_register_style(
352 'wtotem_toastr_css',
353 WEBTOTEM_URL . '/includes/css/toastr.min.css',
354 [],
355 WebTotem::fileVersion('includes/css/toastr.min.css')
356 );
357 wp_enqueue_style('wtotem_toastr_css');
358
359 wp_register_style(
360 'wtotem_main_css',
361 WEBTOTEM_URL . '/includes/css/main.css',
362 [],
363 WebTotem::fileVersion('includes/css/main.css')
364 );
365 wp_enqueue_style('wtotem_main_css');
366
367 // Adding JS files.
368 wp_register_script(
369 'wtotem_amplitude',
370 WEBTOTEM_URL . '/includes/js/amplitude.js',
371 [ 'jquery' ],
372 WebTotem::fileVersion('includes/js/amplitude.js'),
373 false
374 );
375 wp_enqueue_script('wtotem_amplitude');
376
377 wp_register_script(
378 'wtotem_d3',
379 WEBTOTEM_URL . '/includes/js/d3.v4.js',
380 ['jquery'],
381 WebTotem::fileVersion('includes/js/d3.v4.js'),
382 true
383 );
384 wp_enqueue_script('wtotem_d3');
385
386 wp_register_script(
387 'wtotem_chart',
388 WEBTOTEM_URL . '/includes/js/chart.js',
389 ['jquery', 'wtotem_d3', 'wtotem_jsdelivr'],
390 WebTotem::fileVersion('includes/js/chart.js'),
391 true
392 );
393 wp_enqueue_script('wtotem_chart');
394
395 wp_register_script(
396 'wtotem_flatpickr_js',
397 WEBTOTEM_URL . '/includes/js/flatpickr.js',
398 [ 'jquery', 'wp-i18n' ],
399 WebTotem::fileVersion('includes/js/flatpickr.js'),
400 true
401 );
402 wp_set_script_translations( 'wtotem_flatpickr_js', 'wtotem', WEBTOTEM_PLUGIN_PATH . '/lang/');
403 wp_enqueue_script('wtotem_flatpickr_js');
404
405 wp_register_script(
406 'wtotem_jsdelivr',
407 WEBTOTEM_URL . '/includes/js/jsdelivr_chart.js',
408 [ 'jquery' ],
409 WebTotem::fileVersion('includes/js/jsdelivr_chart.js'),
410 true
411 );
412 wp_enqueue_script('wtotem_jsdelivr');
413
414 wp_register_script(
415 'wtotem_jquery_qrcode',
416 WEBTOTEM_URL . '/includes/js/jquery.qrcode.min.js',
417 [ 'jquery' ],
418 WebTotem::fileVersion('includes/js/jquery.qrcode.min.js'),
419 true
420 );
421 wp_enqueue_script('wtotem_jquery_qrcode');
422
423 wp_register_script(
424 'wtotem_progress_bar',
425 WEBTOTEM_URL . '/includes/js/progress_bar.js',
426 [],
427 WebTotem::fileVersion('includes/js/progress_bar.js'),
428 true
429 );
430 wp_enqueue_script('wtotem_progress_bar');
431
432 wp_register_script(
433 'wtotem_toastr',
434 WEBTOTEM_URL . '/includes/js/toastr.min.js',
435 [],
436 WebTotem::fileVersion('includes/js/toastr.min.js'),
437 true
438 );
439 wp_enqueue_script('wtotem_toastr');
440
441 $_page = WebTotemRequest::get('page');
442 if($_page === 'wtotem_settings'){
443 wp_register_script(
444 'wtotem_country_blocking',
445 WEBTOTEM_URL . '/includes/js/country-blocking.js',
446 ['wp-i18n'],
447 WebTotem::fileVersion('includes/js/country-blocking.js'),
448 true
449 );
450 wp_set_script_translations( 'wtotem_country_blocking', 'wtotem' , WEBTOTEM_PLUGIN_PATH . '/lang/');
451 wp_enqueue_script('wtotem_country_blocking');
452 }
453
454 wp_register_script(
455 'wtotem_main',
456 WEBTOTEM_URL . '/includes/js/main.js',
457 ['jquery'],
458 WebTotem::fileVersion('includes/js/main.js'),
459 true
460 );
461 wp_enqueue_script('wtotem_main');
462 }
463 }
464