<?php
namespace WPD_Platform\Utils;

use WPD_Platform\Utils\HostTypeInterface;

/**
 * Host Resolver. Used to identify the hosting service. Thanks to WP Rocket for the foundation.
 *
 * @since 3.6.3
 */
class HostResolver implements HostTypeInterface {

	/**
	 * Name of the current host service.
	 *
	 * @var string
	 */
	private static $hostname = '';

	/**
	 * Get the name of an identifiable hosting service.
	 *
	 * @since 3.6.3
	 *
	 * @param bool $ignore_cached_hostname (optional) Don't use cached hostname when true.
	 *
	 * @return string Name of the hosting service or '' if no service is recognized.
	 */
	public static function get_host_service( $ignore_cached_hostname = false ) {

		if ( ! $ignore_cached_hostname && ! empty( self::$hostname ) ) {
			return self::$hostname;
		}

		if ( isset( $_SERVER['GROUPONE_BRAND_NAME'] ) ) {
			$group_one_brand_name = strtolower( sanitize_text_field( wp_unslash( $_SERVER['GROUPONE_BRAND_NAME'] ) ) );

			switch ( $group_one_brand_name ) {
				case 'one.com':
					self::$hostname = self::ONECOM;
					return self::ONECOM;
				case 'proisp.no':
					self::$hostname = self::PROISP;
					return self::PROISP;
			}
		}

		if ( isset( $_SERVER['cw_allowed_ip'] ) ) {
			self::$hostname = self::CLOUDWAYS;
			return self::CLOUDWAYS;
		}

		if ( defined( 'IS_PRESSABLE' ) && IS_PRESSABLE ) {
			self::$hostname = self::PRESSABLE;
			return self::PRESSABLE;
		}

		if ( getenv( 'SPINUPWP_CACHE_PATH' ) ) {
			self::$hostname = self::SPINUPWP;
			return self::SPINUPWP;
		}

		if ( class_exists( 'WpeCommon' ) && function_exists( 'wpe_param' ) ) {
			self::$hostname = self::WPENGINE;
			return self::WPENGINE;
		}

		if ( defined( 'O2SWITCH_VARNISH_PURGE_KEY' ) && O2SWITCH_VARNISH_PURGE_KEY ) {
			self::$hostname = self::O2SWITCH;
			return self::O2SWITCH;
		}

        if ( class_exists( 'FlywheelNginxCompat' ) ) {
            self::$hostname = self::FLYWHEEL;
            return self::FLYWHEEL;
        }

		if ( defined( 'WPCOMSH_VERSION' ) && WPCOMSH_VERSION ) {
			self::$hostname = self::WORDPRESSCOM;
			return self::WORDPRESSCOM;
		}

		if ( defined( 'PANTHEON_ENVIRONMENT' ) && PANTHEON_ENVIRONMENT ) {
			self::$hostname = self::PANTHEON;
			return self::PANTHEON;
		}

		if (
			defined( '\Savvii\CacheFlusherPlugin::NAME_FLUSH_NOW' ) && \Savvii\CacheFlusherPlugin::NAME_FLUSH_NOW
			&&
			defined( '\Savvii\CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW' ) && \Savvii\CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW
		) {
			self::$hostname = self::SAVVII;
			return self::SAVVII;
		}

		if ( self::is_dreampress() ) {
			self::$hostname = self::DREAMPRESS;
			return self::DREAMPRESS;
		}

		if ( isset( $_SERVER['HTTP_WPXCLOUD'] ) ) {
			self::$hostname = self::WPXCLOUD;
			return self::WPXCLOUD;
		}

		if ( isset( $_SERVER['X-LSCACHE'] ) ) {
			self::$hostname = self::LITESPEED;
			return self::LITESPEED;
		}

		if ( class_exists( '\WPaas\Plugin' ) ) {
			self::$hostname = self::GODADDY;
			return self::GODADDY;
		}

		if ( isset( $_SERVER['KINSTA_CACHE_ZONE'] ) ) {
			self::$hostname = self::KINSTA;
			return self::KINSTA;
		}

		return self::UNKNOWN;
	}

	/**
	 * Checks if the current host is DreamPress
	 *
	 * @since 3.7.2
	 *
	 * @return boolean
	 */
	private static function is_dreampress() {
		if ( ! isset( $_SERVER['DH_USER'] ) ) {
			return false;
		}

		if (
			( defined( 'WP_ROCKET_IS_TESTING' ) && ! WP_ROCKET_IS_TESTING )
			&&
			'dp-' !== substr( gethostname(), 0, 3 )
		) {
			return false;
		}

		return 'wp_' === substr( sanitize_key( wp_unslash( $_SERVER['DH_USER'] ) ), 0, 3 );
	}
}
