<?php
/**
 * Cache module — Host Detector
 *
 * Identifies the underlying hosting provider so the Cache module can adapt:
 * SiteGround, Kinsta, WP Engine, Cloudways, Pressable, O2Switch, Infomaniak,
 * LiteSpeed-powered hosts. Detection is constant- and path-based — fast and
 * 100 % offline.
 *
 * @package BlaminhorEssentials
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Blaminhor_Essentials_Cache_Host_Detector {

	/** @var string|null memoised */
	private $detected = null;

	/**
	 * @return string Slug of the detected host, or 'unknown'.
	 */
	public function detect() {
		if ( null !== $this->detected ) {
			return $this->detected;
		}

		// SiteGround.
		if ( defined( 'SG_OPTIMIZER_VERSION' ) || defined( 'SiteGround_Optimizer\\VERSION' ) ) {
			return $this->detected = 'siteground';
		}
		$server_software = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( (string) $_SERVER['SERVER_SOFTWARE'] ) ) : '';
		if ( '' !== $server_software && false !== stripos( $server_software, 'SiteGround' ) ) {
			return $this->detected = 'siteground';
		}

		// Kinsta.
		if ( defined( 'KINSTA_CACHE_ZONE' ) || defined( 'KINSTAMU_VERSION' ) ) {
			return $this->detected = 'kinsta';
		}

		// WP Engine.
		if ( defined( 'WPE_PLUGIN_BASE' ) || defined( 'WPE_APIKEY' ) ) {
			return $this->detected = 'wpengine';
		}

		// Cloudways.
		if ( defined( 'CLOUDWAYS_API_KEY' ) || ! empty( $_SERVER['cw_allowed_ip'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
			return $this->detected = 'cloudways';
		}

		// Pressable.
		if ( defined( 'IS_PRESSABLE' ) ) {
			return $this->detected = 'pressable';
		}

		// Pantheon.
		if ( defined( 'PANTHEON_ENVIRONMENT' ) ) {
			return $this->detected = 'pantheon';
		}

		// LiteSpeed-powered host.
		if ( '' !== $server_software && false !== stripos( $server_software, 'LiteSpeed' ) ) {
			return $this->detected = 'litespeed';
		}

		// Infomaniak (mutualisé): server hostname pattern + characteristic path.
		$hostname = function_exists( 'gethostname' ) ? (string) @gethostname() : '';
		if ( '' !== $hostname && preg_match( '/\.infomaniak\.(com|ch)\b|infomaniak-/i', $hostname ) ) {
			return $this->detected = 'infomaniak';
		}
		if ( false !== strpos( __DIR__, '/home/clients/' ) && is_dir( '/altroot' ) ) {
			return $this->detected = 'infomaniak';
		}

		// o2switch.
		if ( '' !== $hostname && preg_match( '/o2switch|nodalix/i', $hostname ) ) {
			return $this->detected = 'o2switch';
		}
		if ( false !== strpos( ABSPATH, '/home/' ) && is_dir( '/home/cpaneluser' ) ) {
			// generic cPanel — not 100% o2switch but a fair guess for FR market.
			return $this->detected = 'cpanel';
		}

		return $this->detected = 'unknown';
	}

	/**
	 * @return string Pretty label for the admin UI.
	 */
	public function label() {
		$slug = $this->detect();
		$labels = array(
			'siteground' => 'SiteGround',
			'kinsta'     => 'Kinsta',
			'wpengine'   => 'WP Engine',
			'cloudways'  => 'Cloudways',
			'pressable'  => 'Pressable',
			'pantheon'   => 'Pantheon',
			'litespeed'  => 'LiteSpeed',
			'infomaniak' => 'Infomaniak',
			'o2switch'   => 'o2switch',
			'cpanel'     => 'cPanel-based host',
			'unknown'    => __( 'Unknown', 'blaminhor-essentials' ),
		);
		return $labels[ $slug ] ?? 'Unknown';
	}
}
