PluginProbe
WebTotem Security / 2.4.16
WebTotem Security v2.4.16
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.16, at lib/Interface.php

401 lines 11.6 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 $sapi = @php_sapi_name();
73 if( ! current_user_can( 'publish_posts' ) and $sapi != "cli" ) {
74 if ($waf = WebTotemOption::getOption(("waf_installed_file"))) {
75 $include_waf_file = ABSPATH . '/_include_' . $waf;
76
77 if (is_file($include_waf_file) && is_readable($include_waf_file)) {
78 include_once $include_waf_file;
79 }
80 }
81 }
82 }
83
84 /**
85 * Checking whether agents are installed, if they are not installed, then install.
86 */
87 private static function checkAgents(){
88
89 $api_key = WebTotemOption::getOption('api_key');
90
91 // Check if the plugin version has changed.
92 WebTotemAgentManager::checkVersion();
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
142 /**
143 * Add 2fa to the profile form.
144 *
145 * @return void
146 */
147 public static function add2faProfileForm(){
148
149 if(!WebTotemLogin::isTwoFactorEnabled()){ return; }
150
151 if ( isset( $_GET['user_id'] ) ) {
152 if( !current_user_can( 'manage_options' ) ){
153 return;
154 }
155 $user_id = (int) $_GET['user_id'];
156 $user = get_user_by( 'id', $user_id );
157 } else {
158 $user = wp_get_current_user();
159 }
160
161 $current_user = wp_get_current_user();
162
163 if ( ! is_a( $user, '\WP_User' ) || ! is_a( $current_user, '\WP_User' ) ) {
164 return;
165 }
166
167 $composer_autoload = WEBTOTEM_PLUGIN_PATH . '/vendor/autoload.php';
168 if ( file_exists( $composer_autoload ) ) {
169 require_once $composer_autoload;
170 }
171
172 $template = new WebTotemTemplate();
173
174 $build[] = [
175 'template' => 'two_factor_user_profile_modal',
176 'variables' => [
177 'two_factor' => WebTotemLogin::getTwoFactorData($user),
178 'user_id' => $user_id ?? $user->ID,
179 'can_manage_options' => current_user_can( 'manage_options' )
180 ],
181 ];
182
183 $page_content = $template->arrayRender($build);
184 echo $page_content;
185 }
186
187 /**
188 * Authentication.
189 *
190 * @return mixed
191 */
192 public static function wt_authenticate($user, $username = null, $password = null) {
193
194
195 if(WebTotemCaptcha::isEnabled()) {
196 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
197 return $user;
198 }
199 $token = WebTotemCaptcha::get_token();
200 $score = WebTotemCaptcha::score($token, WebTotemOption::getPluginSettings('recaptcha_v3_secret'));
201
202 if($score < 0.5) {
203 return new WP_Error('authentication_failed', __('<strong>ERROR</strong>&nbsp;: Please check the ReCaptcha box or try to reload page.','wtotem'));
204 }
205 }
206
207 if(isset($_POST['wtotem-token']) && is_string($_POST['wtotem-token']) && !empty($_POST['wtotem-token'])){
208 if ( is_object( $user ) && ( $user instanceof \WP_User ) ) {
209 if ( WebTotemLogin::hasUser2faActivated( $user ) ) {
210 $check2faCode = WebTotemLogin::check2faCode( $user, $_POST['wtotem-token']);
211
212 if ( ! $check2faCode ) {
213 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() ) ) );
214 }
215 }
216 }
217 }
218
219 return WebTotemBFProtection::checkBruteForceAttempts($user, $username);
220 }
221
222 /**
223 * Authentication.
224 *
225 * @return mixed
226 */
227 public static function wt_lost_password($errors = null, $user_data = null) {
228 return WebTotemBFProtection::lostPassword($errors);
229 }
230
231 /**
232 * Restore and then hide the readme file when updating the WordPress.
233 *
234 * @param string $string
235 * @return string
236 */
237 public static function restoreReadmeWhenUpdating($string) {
238 static $didRun;
239 if (!isset($didRun)) {
240 $didRun = true;
241 WebTotemOption::restoreReadme();
242 register_shutdown_function('WebTotemOption::hideReadme');
243 }
244
245 return $string;
246 }
247
248 /**
249 * Login Page
250 */
251 public static function loginEnqueueScripts() {
252
253 $recaptcha_enabled = WebTotemCaptcha::isEnabled();
254 if ($recaptcha_enabled) {
255 $recaptcha_site_key = WebTotemOption::getPluginSettings('recaptcha_v3_site_key');
256 wp_enqueue_script('wtotem_recaptcha', 'https://www.google.com/recaptcha/api.js?render=' . $recaptcha_site_key);
257 }
258
259 wp_register_script(
260 'wtotem_login',
261 WEBTOTEM_URL . '/includes/js/login.js',
262 ['jquery'],
263 WebTotem::fileVersion('includes/js/login.js'),
264 false
265 );
266 wp_enqueue_script('wtotem_login');
267
268 wp_register_style(
269 'wtotem_login',
270 WEBTOTEM_URL . '/includes/css/login.css',
271 [],
272 WebTotem::fileVersion('includes/css/login.css')
273 );
274 wp_enqueue_style('wtotem_login');
275
276 wp_localize_script('wtotem_login', 'wtotem_login_vars', [
277 'recaptcha_site_key' => WebTotemCaptcha::_siteKey(),
278 'recaptcha_is_enabled' => $recaptcha_enabled,
279 'ajaxurl' => admin_url('admin-ajax.php', 'relative'),
280 'two_factor_is_enabled' => WebTotemLogin::isTwoFactorEnabled() and WebTotemLogin::anyTwoFactorActivated(),
281 ]);
282 }
283
284 /**
285 * A safe way to add JavaScript and css files to a WordPress-managed page
286 *
287 * @return void
288 */
289 public static function enqueueScripts() {
290 // Adding CSS files.
291 wp_register_style(
292 'wtotem_flatpickr',
293 WEBTOTEM_URL . '/includes/css/flatpickr.min.css',
294 [],
295 WebTotem::fileVersion('includes/css/flatpickr.min.css')
296 );
297 wp_enqueue_style('wtotem_flatpickr');
298
299 wp_register_style(
300 'wtotem_toastr_css',
301 WEBTOTEM_URL . '/includes/css/toastr.min.css',
302 [],
303 WebTotem::fileVersion('includes/css/toastr.min.css')
304 );
305 wp_enqueue_style('wtotem_toastr_css');
306
307 wp_register_style(
308 'wtotem_main_css',
309 WEBTOTEM_URL . '/includes/css/main.css',
310 [],
311 WebTotem::fileVersion('includes/css/main.css')
312 );
313 wp_enqueue_style('wtotem_main_css');
314
315 // Adding JS files.
316 wp_register_script(
317 'wtotem_amplitude',
318 WEBTOTEM_URL . '/includes/js/amplitude.js',
319 [ 'jquery' ],
320 WebTotem::fileVersion('includes/js/amplitude.js'),
321 false
322 );
323 wp_enqueue_script('wtotem_amplitude');
324
325 wp_register_script(
326 'wtotem_d3',
327 WEBTOTEM_URL . '/includes/js/d3.v4.js',
328 ['jquery'],
329 WebTotem::fileVersion('includes/js/d3.v4.js'),
330 true
331 );
332 wp_enqueue_script('wtotem_d3');
333
334 wp_register_script(
335 'wtotem_chart',
336 WEBTOTEM_URL . '/includes/js/chart.js',
337 ['jquery', 'wtotem_d3', 'wtotem_jsdelivr'],
338 WebTotem::fileVersion('includes/js/chart.js'),
339 true
340 );
341 wp_enqueue_script('wtotem_chart');
342
343 wp_register_script(
344 'wtotem_flatpickr_js',
345 WEBTOTEM_URL . '/includes/js/flatpickr.js',
346 [ 'jquery', 'wp-i18n' ],
347 WebTotem::fileVersion('includes/js/flatpickr.js'),
348 true
349 );
350 wp_enqueue_script('wtotem_flatpickr_js');
351 wp_set_script_translations( 'wtotem_flatpickr_js', 'wtotem' );
352
353 wp_register_script(
354 'wtotem_jsdelivr',
355 WEBTOTEM_URL . '/includes/js/jsdelivr_chart.js',
356 [ 'jquery' ],
357 WebTotem::fileVersion('includes/js/jsdelivr_chart.js'),
358 true
359 );
360 wp_enqueue_script('wtotem_jsdelivr');
361
362 wp_register_script(
363 'wtotem_jquery_qrcode',
364 WEBTOTEM_URL . '/includes/js/jquery.qrcode.min.js',
365 [ 'jquery' ],
366 WebTotem::fileVersion('includes/js/jquery.qrcode.min.js'),
367 true
368 );
369 wp_enqueue_script('wtotem_jquery_qrcode');
370
371 wp_register_script(
372 'wtotem_progress_bar',
373 WEBTOTEM_URL . '/includes/js/progress_bar.js',
374 [],
375 WebTotem::fileVersion('includes/js/progress_bar.js'),
376 true
377 );
378 wp_enqueue_script('wtotem_progress_bar');
379
380 wp_register_script(
381 'wtotem_toastr',
382 WEBTOTEM_URL . '/includes/js/toastr.min.js',
383 [],
384 WebTotem::fileVersion('includes/js/toastr.min.js'),
385 true
386 );
387 wp_enqueue_script('wtotem_toastr');
388
389 wp_register_script(
390 'wtotem_main',
391 WEBTOTEM_URL . '/includes/js/main.js',
392 ['jquery'],
393 WebTotem::fileVersion('includes/js/main.js'),
394 true
395 );
396 wp_enqueue_script('wtotem_main');
397 }
398
399
400 }
401