HEX
Server: LiteSpeed
System: Linux baciro.idweb.host 4.18.0-553.5.1.lve.1.el8.x86_64 #1 SMP Fri Jun 14 15:38:45 UTC 2024 x86_64
User: yastidua (1245)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /home/yastidua/httpdocs/wp-content/plugins/jetformbuilder/includes/classes/http/http-tools.php
<?php


namespace Jet_Form_Builder\Classes\Http;

use Jet_Form_Builder\Plugin;

class Http_Tools {

	public static function get_site_host() {
		return str_ireplace( 'www.', '', wp_parse_url( home_url(), PHP_URL_HOST ) );
	}

	/**
	 * Get current user IP Address.
	 *
	 * Taken from:
	 * https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-geolocation.php#L80-L91
	 * (WC_Geolocation::get_ip_address)
	 *
	 * @return string
	 */
	public static function get_ip_address(): string {
		if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) {
			return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) );
		} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
			// Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2
			// Make sure we always only send through the first IP in the list which should always be the client IP.
			return (string) rest_is_ip_address( trim( current( preg_split( '/,/', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) ) ) );
		} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
			return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
		}

		return '';
	}

	public static function get_user_agent(): string {
		return sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) );
	}

	public static function replace_path_args( string $path, array $path_args ): string {
		$patterns = array();

		foreach ( $path_args as $key => $value ) {
			$patterns[ "#\(\?P<$key\>\S+\)#" ] = function ( $matches ) use ( $value ) {
				return (string) $value;
			};
		}

		return implode(
			'/',
			preg_replace_callback_array(
				$patterns,
				explode( '/', $path )
			)
		);
	}

	/**
	 * Returns form action url
	 *
	 * @param array $args
	 *
	 * @return string [type] [description]
	 */
	public static function get_form_action_url( array $args = array() ): string {
		global $wp;

		$args = array_merge(
			array(
				Plugin::instance()->form_handler->hook_key => Plugin::instance()->form_handler->hook_val,
				'method'                                   => 'reload',
			),
			$args
		);

		$action = add_query_arg(
			$args,
			trailingslashit(
				home_url( $wp->request )
			)
		);

		return apply_filters( 'jet-form-builder/form-action-url', $action );
	}

	/**
	 * Returns form refer url
	 *
	 * @return [type] [description]
	 */
	public static function get_form_refer_url(): string {

		global $wp;

		$refer = home_url( $wp->request );

		if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
			$refer = trailingslashit( $refer ) . '?' . $_SERVER['QUERY_STRING'];
		}

		return apply_filters( 'jet-form-builder/form-refer-url', $refer );
	}

}