PluginProbe
WebTotem Security / 2.4.26
WebTotem Security v2.4.26
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.26, at lib/Interface.php

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