<?php
namespace WPUltimateSecurity\UltimateSecurity\Modules\Monitoring\SiteHealth\Collectors;

/**
 * Server Information Collector
 *
 * Collects server environment information
 */
class ServerCollector extends AbstractCollector
{
	/**
	 * Collect server information
	 *
	 * @return array
	 */
	public function collect(): array
	{
		global $wpdb;

		return [
			$this->item(__('PHP Version', 'ultimate-security'), phpversion()),
			$this->item(__('MySQL Version', 'ultimate-security'), $wpdb->db_version()),
			$this->item(__('Server Software', 'ultimate-security'), $this->getServerSoftware()),
			$this->item(__('User Agent', 'ultimate-security'), $this->getUserAgent()),
			$this->item(__('PHP Memory Limit', 'ultimate-security'), ini_get('memory_limit')),
			$this->item(__('PHP Max Execution Time', 'ultimate-security'), ini_get('max_execution_time') . ' ' . __('seconds', 'ultimate-security')),
			$this->item(__('PHP Max Input Vars', 'ultimate-security'), ini_get('max_input_vars')),
			$this->item(__('PHP Post Max Size', 'ultimate-security'), ini_get('post_max_size')),
			$this->item(__('PHP Max Upload Size', 'ultimate-security'), ini_get('upload_max_filesize')),
			$this->item(__('PHP cURL Enabled', 'ultimate-security'), function_exists('curl_version')),
			$this->item(__('PHP GD Library', 'ultimate-security'), extension_loaded('gd')),
			$this->item(__('PHP Mbstring', 'ultimate-security'), extension_loaded('mbstring')),
			$this->item(__('PHP OpenSSL', 'ultimate-security'), extension_loaded('openssl')),
			$this->item(__('PHP XML Support', 'ultimate-security'), extension_loaded('xml')),
			$this->item(__('PHP Zip Extension', 'ultimate-security'), extension_loaded('zip')),
		];
	}

	/**
	 * Get server software
	 *
	 * @return string
	 */
	private function getServerSoftware(): string
	{
		return isset($_SERVER['SERVER_SOFTWARE'])
			? sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE']))
			: __('Not available', 'ultimate-security');
	}

	/**
	 * Get user agent
	 *
	 * @return string
	 */
	private function getUserAgent(): string
	{
		return isset($_SERVER['HTTP_USER_AGENT'])
			? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT']))
			: __('Not available', 'ultimate-security');
	}

	/**
	 * Get collector name
	 *
	 * @return string
	 */
	public function getName(): string
	{
		return 'server';
	}

	/**
	 * Get collector label
	 *
	 * @return string
	 */
	public function getLabel(): string
	{
		return __('Server', 'ultimate-security');
	}
}
