# wt-security/2.4.14/src/PageHandler.php

WebTotem Security, version 2.4.14. 839 lines.

- Page: https://pluginprobe.com/plugins/wt-security/2.4.14/code/src/PageHandler.php
- Raw: https://pluginprobe.com/plugins/wt-security/2.4.14/raw/src/PageHandler.php
- Modified: 2022-09-07T11:53:30+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wt-security/2.4.14/code/src/PageHandler.php#L10-L20`.

```php
<?php

/**
 * Load page and ajax handlers
 */

if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
	if (!headers_sent()) {
		header('HTTP/1.1 403 Forbidden');
	}
	die("Protected By WebTotem!");
}

/**
 * Handles all the AJAX plugin's requests.
 *
 * @return void
 */
function wtotem_ajax_callback() {

    if (WebTotemRequest::post('ajax_action') != NULL) {
        WebTotemAjax::authenticate();
    }

	if (WebTotemRequest::post('ajax_action') != NULL && WebTotemInterface::checkNonce()) {

        $composer_autoload = WEBTOTEM_PLUGIN_PATH . '/vendor/autoload.php';
        if ( file_exists( $composer_autoload ) ) {
            require_once $composer_autoload;
        }

		WebTotemAjax::activation();
		WebTotemAjax::agentsInstallation();
		WebTotemAjax::reinstallAgents();
		WebTotemAjax::chart();
		WebTotemAjax::wafDateFilter();
		WebTotemAjax::ignorePorts();
		WebTotemAjax::lazyLoad();
		WebTotemAjax::antivirus();
		WebTotemAjax::changeThemeMode();
		WebTotemAjax::userTimeZone();
		WebTotemAjax::quarantine();
		WebTotemAjax::reports();
		WebTotemAjax::settings();
		WebTotemAjax::remove();
		WebTotemAjax::reloadPage();
		WebTotemAjax::logout();
		WebTotemAjax::popup();
		WebTotemAjax::multisite();
		WebTotemAjax::twoFactorAuth();
	}

	wp_send_json([
		'success' => false,
		'error' => 'invalid ajax request',
		'notifications' => WebTotemAjax::notifications(),
	], 200);
}

/**
 * Handles all the AJAX plugin's public requests.
 *
 * @return void
 */
function wtotem_public_ajax_callback() {

	if (WebTotemRequest::post('ajax_action') != NULL) {
		WebTotemAjax::authenticate();
	}

	wp_send_json([
		'success' => false,
		'error' => 'invalid ajax request',
	], 200);

}

/**
 * Activation page.
 *
 * @return void
 */
function wtotem_activation_page() {

	$build[] = [
		'variables' => [
			'notifications' => WebTotem::getNotifications(),
			'current_year' => date('Y'),
		],
		'template' => 'activation'
	];

	$template = new WebTotemTemplate();
	echo $template->arrayRender($build);
}

/**
 * All sites page.
 *
 * @return void
 */
function wtotem_all_sites_page() {

	$allSites = WebTotemAPI::getSites();

	// Reset session data.
	WebTotemOption::setSessionOptions([
		'sites_cursor' => $allSites['pageInfo']['endCursor'],
	]);

	$build[] = [
		'variables' => [
			'notifications' => WebTotem::getNotifications(),
			'current_year' => date('Y'),
			'sites' => WebTotem::allSitesData($allSites),
			'theme_mode' => WebTotem::getThemeMode()
		],
		'template' => 'multisite'
	];

	$template = new WebTotemTemplate();
	$page_content = $template->arrayRender($build);
	echo $template->baseTemplate($page_content);
}

/**
 * Error page.
 *
 * @return void
 */
function wtotem_error_page(){
	$template = new WebTotemTemplate();
	$build[] = [
		'template' => 'error',
	];
	$page_content = $template->arrayRender($build);
	echo $template->baseTemplate($page_content);
}

/**
 * Dashboard, main page.
 *
 * @return void
 */
function wtotem_dashboard_page() {

	if(WebTotemRequest::get('hid')){
		$host = WebTotemOption::getHost(WebTotemRequest::get('hid'));
	} else {
		$host = WebTotemAPI::siteInfo();
	}

	$template = new WebTotemTemplate();
	if (!isset($host['id']) or !$host['id']) {
		wtotem_error_page();
		exit();
	}

	// Get data from WebTotem API.
	if($cacheData = WebTotemCache::getdata('getAllData', $host['id'])){
		$data = $cacheData['data'];
	} else {
		$data = WebTotemAPI::getAllData($host['id']);
		WebTotemCache::setData(['getAllData' => $data], $host['id']);
	}

	if (empty($data)) {
		wtotem_error_page();
		exit();
	}

	// MultiSite page header (site name)
	if(WebTotem::isMultiSite() and is_super_admin()){
		// Submenu block.
		$pages['dashboard'] = 'wtotem_page-header__link_active';

		$build[] = [
			'variables' => [
				'is_active' => $pages,
				'site_name' => $host['name'],
				'hid' => $host['id'],
			],
			'template' => 'multisite_submenu',
		];
	}

	// Reset session data.
	WebTotemOption::setSessionOptions([
		'firewall_period' => NULL,
		'ram_period' => NULL,
		'cpu_period' => NULL,
	]);

	// Scoring block.
	$service_data = $data['scoring']['result'];
	$total_score = round($data['scoring']['score']);
	$score_grading = WebTotem::scoreGrading($total_score);
	$build[] = [
		'variables' => [
			"host_id" => $host['id'],
			"total_score" => $total_score . "%",
			"tested_on" => WebTotem::dateFormatter($data['scoring']['lastTest']['time']),
			"server_ip" => $service_data['ip'] ?: ' - ',
			"location" => WebTotem::getCountryName($service_data['country']) ?: ' - ',
			"is_higher_than"  => $service_data['isHigherThan'] . '%',
			"grade" => $score_grading['grade'],
			"color" => $score_grading['color'],
		],
		'template' => 'score',
	];

	// Agents installing process.
	$agents_data = [
		'av' => $data['antivirus']['status'],
		'waf'  => $data['firewall']['status'],
	];

	$agents_statuses = WebTotem::getAgentsStatuses($agents_data);

	if (!$agents_statuses['option_statuses']['av'] or !$agents_statuses['option_statuses']['waf']) {

		$status = [
			'av' => $agents_statuses['process_statuses']['av'] == 'installed',
			'waf' => $agents_statuses['process_statuses']['waf'] == 'installed',
		];

		WebTotemOption::setOptions([
			'av_installed' => $status['av'],
			'waf_installed' => $status['waf'],
		]);

		$build[] = [
			'variables' => [
				"process_status" => $agents_statuses['process_statuses'],
			],
			'template' => 'agents',
		];
	}

	// Firewall header.
	$build[] = [
		'variables' => [
			"title" => __('Firewall activity', 'wtotem'),
		],
		'template' => 'section_header',
	];

	// Firewall stats.
	$service_data = (isset($data['firewall'])) ? $data['firewall'] : [];
	$chart = WebTotem::generateWafChart($service_data['chart']);
	$build[] = [
		'variables' => [
			"is_waf_training" => $data['agentManager'] && WebTotem::isWafTraining( $data['agentManager']['createdAt'] ),
			"most_attacks" => WebTotem::getMostAttacksData($service_data['map']),
			"all_attacks" => $chart['count_attacks'],
			"blocking" => $chart['count_blocks'],
			"not_blocking" => (int) $chart['count_attacks'] - (int) $chart['count_blocks'],
		],
		'template' => 'firewall_stats',
	];

	// Firewall filter form
	$build[] = [
		'template' => 'waf_filter_form',
	];

	// Firewall blocks.
	$build[] = [
		'variables' => [
			"chart" => $chart['chart'],
			"logs"  => WebTotem::wafLogs($service_data['logs']['edges']),
            'host_name' => $host['name'],
		],
		'template' => 'firewall',
	];

	// Display AV and SS data only to the super admin, or it's not a MultiSite network.
	if(!WebTotem::isMultiSite() or is_super_admin()) {

		// Server Status header.
		$build[] = [
			'variables' => [
				"title" => __('Server resources', 'wtotem'),
				"tooltip" => [
					'title' => __('Server resources', 'wtotem'),
					'test' => __('Displays critical data about web-server usage. A large load on a server can slow down the website performance.', 'wtotem'),
				],
			],
			'template' => 'section_header',
		];

		// Server Status RAM.
		$service_data = $data['serverStatus'];
		$build[] = [
			'variables' => [
				"info" => $service_data['info'],
				"ram_chart" => WebTotem::generateChart($service_data['ramChart']),
			],
			'template' => 'server_status_ram',
		];

		// Server Status CPU.
		$build[] = [
			'variables' => [
				"cpu_chart" => WebTotem::generateChart($service_data['cpuChart']),
			],

			'template' => 'server_status_cpu',
		];

		// Antivirus header.
		$build[] = [
			'variables' => [
				"title" => __('Antivirus', 'wtotem'),
			],
			'template' => 'section_header',
		];

		// Antivirus stats blocks.
		$antivirus_stats = $data['antivirus']['stats'];
		$build[] = [
			'variables' => [
				"changes"  => $antivirus_stats['changed'] ?: 0,
				"scanned"  => $antivirus_stats['scanned'] ?: 0,
				"deleted"  => $antivirus_stats['deleted'] ?: 0,
				"infected" => $antivirus_stats["infected"] ?: 0,
			],

			'template' => 'antivirus_stats',
		];
	}

	// Monitoring header.
	$build[] = [
		'variables' => [
			"title" => __('Monitoring', 'wtotem'),
		],
		'template' => 'section_header',
	];

	// Monitoring blocks.
	$build[] = [
		'variables' => [
			"ssl"  => [
				'status' => WebTotem::getStatusData($data['ssl']['status']),
				'days_left' => WebTotem::daysLeft($data['ssl']['expiryDate']),
				'issue_date' => WebTotem::dateFormatter($data['ssl']['issueDate']),
				'expiry_date' => WebTotem::dateFormatter($data['ssl']['expiryDate']),
			],
			"availability"  => [
				'status' => WebTotem::getStatusData($data['availability']['status']),
				"percent" => $data['availability']['percent'],
				"response_time" => ceil($data['availability']['responseTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
				"downtime" => ceil($data['availability']['downTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
				"last_test" => WebTotem::dateFormatter($data['availability']['lastTest']['time']),
			],
			'reputation' => [
				"status" => WebTotem::getStatusData($data['reputation']['status']),
				"blacklists_entries" => WebTotem::blacklistsEntries(
					$data['reputation']['status'],
					$data['reputation']['virusList']),
				"info" => WebTotem::getReputationInfo($data['reputation']['status']),
				"last_test" => WebTotem::dateFormatter($data['reputation']['lastTest']['time']),
			],
		],
		'template' => 'monitoring',
	];

	$build[] = [
		'variables' => [
			"ports" => WebTotemAPI::getAllPortsList($host['id']),
		],
		'template' => 'ports_form',
	];

	// Scanning header.
	$build[] = [
		'variables' => [
			"title" => __('Scanning', 'wtotem'),
		],
		'template' => 'section_header',
	];

	$disc_usage_data = $data['serverStatus']['discUsage'];
	$disc_usage = [
		'total' => $disc_usage_data['total'],
		'free'  => $disc_usage_data['free'],
		'used'  => $disc_usage_data['total'] - $disc_usage_data['free'],
	];

	// Scanning blocks.
	$build[] = [
		'variables' => [
			"ports"  => [
				'status' => WebTotem::getStatusData($data['ports']['status']),
				"ip" => $data['ports']['ip'],
				"number" => count($data['ports']['tcp']),
				"tcp" => $data['ports']['tcp'],
				"ignore_ports" => $data['ports']['ignorePorts'],
				"last_test" => WebTotem::dateFormatter($data['ports']['lastTest']['time']),
			],
			"deface"  => [
				'status' => WebTotem::getStatusData($data['deface']['status']),
				"number" => $data['deface']['count'],
				"words" => !empty($data['deface']['words']) ? implode(",", $data['deface']['words']) : '',
				"last_test" => WebTotem::dateFormatter($data['deface']['lastTest']['time']),
			],
			"disc_usage" => $disc_usage,
			"disc_chart" => json_encode($disc_usage),
		],
		'template' => 'scanning',
	];

	$page_content = $template->arrayRender($build);
	echo $template->baseTemplate($page_content);
}

/** Firewall page.
 *
 * @return void
 */
function wtotem_firewall_page() {

	if(WebTotemRequest::get('hid')){
		$host = WebTotemOption::getHost(WebTotemRequest::get('hid'));
	} else {
		$host = WebTotemAPI::siteInfo();
	}

	$template = new WebTotemTemplate();
	if (!isset($host['id']) or !$host['id']) {
		wtotem_error_page();
		exit();
	}

	// Get data from WebTotem API.
	if($cacheData = WebTotemCache::getdata('getFirewall', $host['id'])){
		$data = $cacheData['data'];
	} else {
		$data = WebTotemAPI::getFirewall($host['id'], 10, NULL, 7);
		WebTotemCache::setData(['getFirewall' => $data], $host['id'], 1);
	}

	if (empty($data)) {
		wtotem_error_page();
		exit();
	}

	$service_data = $data['firewall'];

	// Reset session data.
	WebTotemOption::setSessionOptions([
		'firewall_period' => NULL,
		'firewall_cursor' => $service_data['logs']['pageInfo']['endCursor'],
	]);

	// MultiSite page header (site name)
	if(WebTotem::isMultiSite() and is_super_admin()){
		// Submenu block.
		$pages['firewall'] = 'wtotem_page-header__link_active';

		$build[] = [
			'variables' => [
				'is_active' => $pages,
				'site_name' => $host['name'],
				'hid' => $host['id'],
			],
			'template' => 'multisite_submenu',
		];
	}

	// Start build array for rendering.
	// Firewall header.
	$build[] = [
		'variables' => [
			"title" => __('Firewall activity', 'wtotem'),
		],
		'template' => 'section_header',
	];

	// Attacks map blocks.
	// Get world_map json data
	$world_map_json = WEBTOTEM_URL . '/includes/js/world_map.json';
	$map_data = WebTotem::generateAttacksMapChart($service_data['map']);

	$build[] = [
		'variables' => [
			"attacks_map" => $map_data,
			"world_map_json" => $world_map_json,
		],
		'template' => 'attacks_map',
	];

	$build[] = [
		'variables' => [
			"title" => __('Firewall activity', 'wtotem'),
		],
		'template' => 'section_header',
	];

	// Firewall stats.
	$chart = WebTotem::generateWafChart($service_data['chart']);
	$build[] = [
		'variables' => [
			"is_waf_training" => isset( $data['agentManager']['createdAt'] ) && WebTotem::isWafTraining( $data['agentManager']['createdAt'] ),
			"all_attacks"   => $chart['count_attacks'],
			"blocking"    => $chart['count_blocks'],
			"not_blocking"  => $chart['count_attacks'] - $chart['count_blocks'],
			"most_attacks"  => WebTotem::getMostAttacksData($service_data['map']),
		],
		'template' => 'firewall_stats',
	];

	// Firewall filter form
	$build[] = [
		'template' => 'waf_filter_form',
	];

	// Firewall blocks.
	$build[] = [
		'variables' => [
			"chart" => $chart['chart'],
			"logs"  => WebTotem::wafLogs($service_data['logs']['edges']),
			'has_next_page' => $service_data['logs']['pageInfo']['hasNextPage'],
            'host_name' => $host['name'],
			'page'  => 'firewall',
		],
		'template' => 'firewall',
	];

	$page_content = $template->arrayRender($build);
	echo $template->baseTemplate($page_content);

}

/**
 * Antivirus page.
 *
 * @return void
 */
function wtotem_antivirus_page() {

	$host = WebTotemAPI::siteInfo();

	$template = new WebTotemTemplate();
	if (!isset($host['id']) or !$host['id']) {
		wtotem_error_page();
		exit();
	}

	if(WebTotem::isMultiSite() and !is_super_admin()) {
		echo $template->baseTemplate(__('Sorry, you are not allowed to view this page.', 'wtotem'));
		exit();
	}

	$params = [
		'host_id' => $host['id'],
		'limit' => 10,
		'cursor' => NULL,
		'days' => 365,
		'event' => FALSE,
		'permissions' => FALSE,
	];

	// Get data from WebTotem API.
	if($cacheData = WebTotemCache::getdata('getAntivirus', $host['id'])){

		$data = $cacheData['data'];
	} else {
		$data = WebTotemAPI::getAntivirus($params);
		WebTotemCache::setData(['getAntivirus' => $data], $host['id']);
	}

	if (empty($data)) {
		wtotem_error_page();
		exit();
	}

	// Reset session data.
	WebTotemOption::setSessionOptions([
		'antivirus_event' => NULL,
		'antivirus_permissions' => NULL,
		'antivirus_cursor' => $data['log']['pageInfo']['endCursor'],
	]);

	// MultiSite page header (site name)
	if(WebTotem::isMultiSite() and is_super_admin()){
		// Submenu block.
		$host_ = WebTotemOption::getHost(WebTotemRequest::get('hid'));
		$pages['antivirus'] = 'wtotem_page-header__link_active';

		$build[] = [
			'variables' => [
				'is_active' => $pages,
				'site_name' => $host_['name'],
				'hid' => $host_['id'],
			],
			'template' => 'multisite_submenu',
		];
	}

	// Antivirus header.
	$build[] = [
		'variables' => [
			"title" => __('Antivirus', 'wtotem'),
		],
		'template' => 'section_header',
	];

	// Antivirus stats blocks.
	$stats = $data['stats'];
	$build[] = [
		'variables' => [
			'changes'  => $stats['changed'] ?: 0,
			'scanned'  => $stats['scanned'] ?: 0,
			'deleted'  => $stats['deleted'] ?: 0,
			'infected' => $stats["infected"] ?: 0,
			'page' => 'antivirus',
		],
		'template' => 'antivirus_stats',
	];

	// Quarantine logs blocks.
	$quarantine_logs = $data['quarantine'] ?: [];
	$quarantine_count = count($quarantine_logs);

	$build[] = [
		'variables' => [
			"logs"  => WebTotem::getQuarantineLogs($quarantine_logs) ?: [],
			"count"  => $quarantine_count,
		],
		'template' => 'quarantine',
	];

	// Antivirus filter form.
	$build[] = [
		'template' => 'antivirus_filter_form',
	];

	// Antivirus blocks.
	$build[] = [
		'variables' => [
			"logs" => WebTotem::getAntivirusLogs($data['log']['edges']),
			"has_next_page" => $data['log']['pageInfo']['hasNextPage'],
			'last_scan' => WebTotem::dateFormatter($data['lastTest']['time']),
		],

		'template' => 'antivirus',
	];

	$page_content = $template->arrayRender($build);
	echo $template->baseTemplate($page_content);
}

/**
 * Settings page
 *
 * @return void
 */
function wtotem_settings_page() {

	$host = WebTotemAPI::siteInfo();

	$template = new WebTotemTemplate();
	if (!isset($host['id']) or !$host['id']) {
		wtotem_error_page();
		exit();
	}

	if(WebTotem::isMultiSite() and !is_super_admin()) {
		echo $template->baseTemplate(__('Sorry, you are not allowed to view this page.', 'wtotem'));
		exit();
	}

	// Get data from WebTotem API.
	if($cacheData = WebTotemCache::getdata('getConfigs', $host['id'])){
		$configs_data = $cacheData['data'];
	} else {
		$configs_data = WebTotemAPI::getConfigs($host['id']);
		WebTotemCache::setData(['getConfigs' => $configs_data], $host['id']);
	}

	if($cacheData = WebTotemCache::getdata('getAgentsStatusesFromAPI', $host['id'])){
		$agents_statuses = $cacheData['data'];
	} else {
		$agents_statuses = WebTotemAPI::getAgentsStatusesFromAPI($host['id']);
		WebTotemCache::setData(['getAgentsStatusesFromAPI' => $agents_statuses], $host['id']);
	}

	if($cacheData = WebTotemCache::getdata('getIpLists', $host['id'])){
		$ip_list = $cacheData['data'];
	} else {
		$ip_list = WebTotemAPI::getIpLists($host['id']);
		WebTotemCache::setData(['getIpLists' => $ip_list], $host['id']);
	}

	if($cacheData = WebTotemCache::getdata('getAllowUrlList', $host['id'])){
		$url_list = $cacheData['data'];
	} else {
		$url_list = WebTotemAPI::getAllowUrlList($host['id']) ?: [];
		WebTotemCache::setData(['getAllowUrlList' => $url_list], $host['id']);
	}

	if (empty($configs_data) or
	    empty($agents_statuses) or
	    empty($ip_list)
	) {
		wtotem_error_page();
		exit();
	}

	// MultiSite page header (site name)
	if(WebTotem::isMultiSite() and is_super_admin()){
		// Submenu block.

		$host_ = WebTotemOption::getHost(WebTotemRequest::get('hid'));
		$pages['settings'] = 'wtotem_page-header__link_active';

		$build[] = [
			'variables' => [
				'is_active' => $pages,
				'site_name' => $host_['name'],
				'hid' => $host_['id'],
			],
			'template' => 'multisite_submenu',
		];
	}

	// Settings form.
	$build[] = [
		'variables' => [
			'configs' => WebTotem::getConfigsData($configs_data, 'service'),
			'deny_list' => WebTotem::getIpList($ip_list['blackList'], 'ip_deny'),
			'allow_list' => WebTotem::getIpList($ip_list['whiteList'], 'ip_allow'),
			'url_list' => WebTotem::getUrlAllowList($url_list),
			'av_status' => WebTotem::getStatusData($agents_statuses['av']['status']),
			'waf_status' => WebTotem::getStatusData($agents_statuses['waf']['status']),
			'waf_settings' => WebTotem::getWafSettingData($ip_list['settings']),
			'plugin_settings' => WebTotem::getPluginSettingsData(),
			'two_factor' => WebTotemLogin::getTwoFactorData(),
		],

		'template' => 'settings_form',
	];

	$page_content = $template->arrayRender($build);
	echo $template->baseTemplate($page_content);
}


/**
 * Reports page.
 *
 * @return void
 */
function wtotem_reports_page() {

	if(WebTotemRequest::get('hid')){
		$host = WebTotemOption::getHost(WebTotemRequest::get('hid'));
	} else {
		$host = WebTotemAPI::siteInfo();
	}

	$template = new WebTotemTemplate();
	if (!isset($host['id']) or !$host['id']) {
		wtotem_error_page();
		exit();
	}

	// Get data from WebTotem API.
	if($cacheData = WebTotemCache::getdata('getAllReports', $host['id'])){
		$data = $cacheData['data'];
	} else {
		$data = WebTotemAPI::getAllReports($host['id']);
		WebTotemCache::setData(['getAllReports' => $data], $host['id']);
	}

	if (empty($data)) {
		wtotem_error_page();
		exit();
	}

	WebTotemOption::setSessionOptions([
		'reports_cursor' => $data['pageInfo']['endCursor'],
		'reports_m_cursor' => $data['pageInfo']['endCursor'],
	]);

	// MultiSite page header (site name)
	if(WebTotem::isMultiSite() and is_super_admin()){
		// Submenu block.
		$pages['reports'] = 'wtotem_page-header__link_active';

		$build[] = [
			'variables' => [
				'is_active' => $pages,
				'site_name' => $host['name'],
				'hid' => $host['id'],
			],
			'template' => 'multisite_submenu',
		];
	}

	// Reports form.
	$build[] = [
		'template' => 'reports_form',
	];

	// Reports.
	$build[] = [
		'variables' => [
			"reports" => WebTotem::getReports($data['edges']),
			"has_next_page" => $data['pageInfo']['hasNextPage'],
		],
		'template' => 'reports',
	];

	$page_content = $template->arrayRender($build);
	echo $template->baseTemplate($page_content);

}

/**
 * Information page.
 *
 * @return void
 */
function wtotem_documentation_page() {

	$template = new WebTotemTemplate();

	$build[] = [
		'template' => 'help',
	];

	$page_content = $template->arrayRender($build);
	echo $template->baseTemplate($page_content);
}


```
