<?php
/**
 * Responsible for the requests.
 *
 * @package    wp2fa
 * @subpackage utils
 * @copyright  2026 Melapress
 * @license    https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link       https://wordpress.org/plugins/wp-2fa/
 * @since      2.0.0
 */

declare(strict_types=1);

namespace WP2FA\Utils;

if ( ! class_exists( '\WP2FA\Utils\Request_Utils' ) ) {

	/**
	 * Utility class to extract info from current request.
	 *
	 * @package WP2FA\Utils
	 * @since 2.0.0
	 */
	class Request_Utils {

		/**
		 * Extracts the IP address for the currently browsing user
		 *
		 * @return string
		 *
		 * @since 2.0.0
		 */
		public static function get_ip(): string {
			$ip_address = '';

			foreach (
				array(
					'HTTP_CLIENT_IP',
					'HTTP_X_FORWARDED_FOR',
					'HTTP_X_FORWARDED',
					'HTTP_X_CLUSTER_CLIENT_IP',
					'HTTP_FORWARDED_FOR',
					'HTTP_FORWARDED',
					'REMOTE_ADDR',
				) as $key
			) {
				if ( array_key_exists( $key, $_SERVER ) ) {
					foreach ( array_map( 'trim', explode( ',', \wp_unslash( $_SERVER[ $key ] ) ) ) as $ip ) {
						if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) !== false ) {
							$ip_address = $ip;
							break 2;
						}
					}
				}
			}

			return $ip_address;
		}

		/**
		 * Extracts the User agent for the current request.
		 *
		 * @return string
		 *
		 * @since 2.0.0
		 */
		public static function get_user_agent(): string {
			return array_key_exists( 'HTTP_USER_AGENT', $_SERVER ) ? trim( (string) \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ) : '';
		}
	}
}
