| 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> : 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 |
* Authentication. |
| 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 |
* A safe way to add JavaScript and css files to a WordPress-managed page |
| 282 |
* |
| 283 |
* @return void |
| 284 |
*/ |
| 285 |
public static function enqueueScripts() { |
| 286 |
|
| 287 |
// Adding CSS files. |
| 288 |
wp_register_style( |
| 289 |
'wtotem_flatpickr', |
| 290 |
WEBTOTEM_URL . '/includes/css/flatpickr.min.css', |
| 291 |
[], |
| 292 |
WebTotem::fileVersion('includes/css/flatpickr.min.css') |
| 293 |
); |
| 294 |
wp_enqueue_style('wtotem_flatpickr'); |
| 295 |
|
| 296 |
wp_register_style( |
| 297 |
'wtotem_toastr_css', |
| 298 |
WEBTOTEM_URL . '/includes/css/toastr.min.css', |
| 299 |
[], |
| 300 |
WebTotem::fileVersion('includes/css/toastr.min.css') |
| 301 |
); |
| 302 |
wp_enqueue_style('wtotem_toastr_css'); |
| 303 |
|
| 304 |
wp_register_style( |
| 305 |
'wtotem_main_css', |
| 306 |
WEBTOTEM_URL . '/includes/css/main.css', |
| 307 |
[], |
| 308 |
WebTotem::fileVersion('includes/css/main.css') |
| 309 |
); |
| 310 |
wp_enqueue_style('wtotem_main_css'); |
| 311 |
|
| 312 |
// Adding JS files. |
| 313 |
wp_register_script( |
| 314 |
'wtotem_amplitude', |
| 315 |
WEBTOTEM_URL . '/includes/js/amplitude.js', |
| 316 |
[ 'jquery' ], |
| 317 |
WebTotem::fileVersion('includes/js/amplitude.js'), |
| 318 |
false |
| 319 |
); |
| 320 |
wp_enqueue_script('wtotem_amplitude'); |
| 321 |
|
| 322 |
wp_register_script( |
| 323 |
'wtotem_d3', |
| 324 |
WEBTOTEM_URL . '/includes/js/d3.v4.js', |
| 325 |
['jquery'], |
| 326 |
WebTotem::fileVersion('includes/js/d3.v4.js'), |
| 327 |
true |
| 328 |
); |
| 329 |
wp_enqueue_script('wtotem_d3'); |
| 330 |
|
| 331 |
wp_register_script( |
| 332 |
'wtotem_chart', |
| 333 |
WEBTOTEM_URL . '/includes/js/chart.js', |
| 334 |
['jquery', 'wtotem_d3', 'wtotem_jsdelivr'], |
| 335 |
WebTotem::fileVersion('includes/js/chart.js'), |
| 336 |
true |
| 337 |
); |
| 338 |
wp_enqueue_script('wtotem_chart'); |
| 339 |
|
| 340 |
wp_register_script( |
| 341 |
'wtotem_flatpickr_js', |
| 342 |
WEBTOTEM_URL . '/includes/js/flatpickr.js', |
| 343 |
[ 'jquery', 'wp-i18n' ], |
| 344 |
WebTotem::fileVersion('includes/js/flatpickr.js'), |
| 345 |
true |
| 346 |
); |
| 347 |
wp_set_script_translations( 'wtotem_flatpickr_js', 'wtotem', WEBTOTEM_PLUGIN_PATH . '/lang/'); |
| 348 |
wp_enqueue_script('wtotem_flatpickr_js'); |
| 349 |
|
| 350 |
wp_register_script( |
| 351 |
'wtotem_jsdelivr', |
| 352 |
WEBTOTEM_URL . '/includes/js/jsdelivr_chart.js', |
| 353 |
[ 'jquery' ], |
| 354 |
WebTotem::fileVersion('includes/js/jsdelivr_chart.js'), |
| 355 |
true |
| 356 |
); |
| 357 |
wp_enqueue_script('wtotem_jsdelivr'); |
| 358 |
|
| 359 |
wp_register_script( |
| 360 |
'wtotem_jquery_qrcode', |
| 361 |
WEBTOTEM_URL . '/includes/js/jquery.qrcode.min.js', |
| 362 |
[ 'jquery' ], |
| 363 |
WebTotem::fileVersion('includes/js/jquery.qrcode.min.js'), |
| 364 |
true |
| 365 |
); |
| 366 |
wp_enqueue_script('wtotem_jquery_qrcode'); |
| 367 |
|
| 368 |
wp_register_script( |
| 369 |
'wtotem_progress_bar', |
| 370 |
WEBTOTEM_URL . '/includes/js/progress_bar.js', |
| 371 |
[], |
| 372 |
WebTotem::fileVersion('includes/js/progress_bar.js'), |
| 373 |
true |
| 374 |
); |
| 375 |
wp_enqueue_script('wtotem_progress_bar'); |
| 376 |
|
| 377 |
wp_register_script( |
| 378 |
'wtotem_toastr', |
| 379 |
WEBTOTEM_URL . '/includes/js/toastr.min.js', |
| 380 |
[], |
| 381 |
WebTotem::fileVersion('includes/js/toastr.min.js'), |
| 382 |
true |
| 383 |
); |
| 384 |
wp_enqueue_script('wtotem_toastr'); |
| 385 |
|
| 386 |
$_page = WebTotemRequest::get('page'); |
| 387 |
if($_page === 'wtotem_settings'){ |
| 388 |
wp_register_script( |
| 389 |
'wtotem_country_blocking', |
| 390 |
WEBTOTEM_URL . '/includes/js/country-blocking.js', |
| 391 |
['wp-i18n'], |
| 392 |
WebTotem::fileVersion('includes/js/country-blocking.js'), |
| 393 |
true |
| 394 |
); |
| 395 |
wp_set_script_translations( 'wtotem_country_blocking', 'wtotem' , WEBTOTEM_PLUGIN_PATH . '/lang/'); |
| 396 |
wp_enqueue_script('wtotem_country_blocking'); |
| 397 |
} |
| 398 |
|
| 399 |
wp_register_script( |
| 400 |
'wtotem_main', |
| 401 |
WEBTOTEM_URL . '/includes/js/main.js', |
| 402 |
['jquery'], |
| 403 |
WebTotem::fileVersion('includes/js/main.js'), |
| 404 |
true |
| 405 |
); |
| 406 |
wp_enqueue_script('wtotem_main'); |
| 407 |
} |
| 408 |
} |
| 409 |
|