<commit_before><?php
/**
 * Request.class.php
 * @author <NAME>
 */

namespace Hawk;

/**
 * This class define methods to get HTTP request information 
 * @package Core
 */
class Request{
    /**
     * The clientIp, registered as static variable, to avoid to calculate it each time
     * @static
     */
    private static $clientIp,

    /**
     * The request headers
     */
    $headers;

    /**
     * Get the HTTP request method
     * @static
     * @return string the HTTP request method
     */
    public static function getMethod(){        
        return strtolower(getenv('REQUEST_METHOD'));
    }
    

    /**
     * Get the HTTP request URI
     * @static
     * @return string The HTTP request URI 
     */
    public static function getUri(){
        return getenv('REQUEST_URI');
    }
    

    /**
     * Check if the request is an AJAX request
     * @static
     * @return true if the request is an AJAX request else false
     */
    public static function isAjax(){
        return strtolower(self::getHeaders('X-Requested-With')) === 'xmlhttprequest';
    }
    

    /**
     * Get the client IP address.
     * @static
     * @return string The IPV4 address of the client that performed the HTTP request
     */    
    public static function clientIp(){
        if(isset(self::$clientIp)){
            return self::$clientIp;
        }

        if (self::getHeaders('X-Forwarded-For')) {
            // The user is behind a proxy that transmit HTTP_X_FORWARDED_FOR header
            if ( ! preg_match('![0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}!', self::getHeaders('X-Forwarded-For')) ){
                // The format of HTTP_X_FORWARDED_FOR header is not correct
                self::$clientIp = getenv('REMOTE_ADDR');
                return self::$clientIp;
            }
            else{
                // Get the last public IP in HTTP_X_FORWARDED_FOR header
                $chain = explode(',', preg_replace('/[^0-9,.]/', '', self::getHeaders('X-Forwarded-For')));
                for($i = 0 ; $i <= count($chain); $i ++){
                    $ip = $chain[$i];

                    if((!preg_match("!^(192\.168|10\.0\.0)!", $ip) && $ip != "127.0.0.1") || $i == count($chain) - 1){
                        self::$clientIp = $ip;
                        return self::$clientIp;                        
                    }
                }
            }
        }
    
        // X-Forwarded-For header has not been transmitted, get the REMOTE_ADDR header
        self::$clientIp = getenv('REMOTE_ADDR');
        return self::$clientIp;
    }


    /**
     * This function returns the value of the variable $name in the request body, or all the body if $name is not provided
     * @param string $name The variable name
     * @return string|array The parameter value or all the body
     */
    public static function getBody($name = ""){
        if($name){
            return isset($_POST[$name]) ? $_POST[$name] : null;
        }
        else{            
            return $_POST;
        }
    }

    /**
     * Get the request uploaded files for the given name, or all files if $name is not provided
     * @param string $name The key in $_FILES to get
     * @return string|array The file or all files
     */
    public static function getFiles($name = ''){
        if($name){
            return isset($_FILES[$name]) ? $_FILES[$name] : array();
        }
        else{<commit_msg>add json support for Request class<commit_after><?php
/**
 * Request.class.php
 * @author <NAME>
 */

namespace Hawk;

/**
 * This class define methods to get HTTP request information 
 * @package Core
 */
class Request{
    /**
     * The clientIp, registered as static variable, to avoid to calculate it each time
     * @static
     */
    private static $clientIp,

    /**
     * The request headers
     */
    $headers,

    /**
     * The request body
     */
    $body;

    /**
     * Get the HTTP request method
     * @static
     * @return string the HTTP request method
     */
    public static function getMethod(){        
        return strtolower(getenv('REQUEST_METHOD'));
    }
    

    /**
     * Get the HTTP request URI
     * @static
     * @return string The HTTP request URI 
     */
    public static function getUri(){
        return getenv('REQUEST_URI');
    }
    

    /**
     * Check if the request is an AJAX request
     * @static
     * @return true if the request is an AJAX request else false
     */
    public static function isAjax(){
        return strtolower(self::getHeaders('X-Requested-With')) === 'xmlhttprequest';
    }
    

    /**
     * Get the client IP address.
     * @static
     * @return string The IPV4 address of the client that performed the HTTP request
     */    
    public static function clientIp(){
        if(isset(self::$clientIp)){
            return self::$clientIp;
        }

        if (self::getHeaders('X-Forwarded-For')) {
            // The user is behind a proxy that transmit HTTP_X_FORWARDED_FOR header
            if ( ! preg_match('![0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}!', self::getHeaders('X-Forwarded-For')) ){
                // The format of HTTP_X_FORWARDED_FOR header is not correct
                self::$clientIp = getenv('REMOTE_ADDR');
                return self::$clientIp;
            }
            else{
                // Get the last public IP in HTTP_X_FORWARDED_FOR header
                $chain = explode(',', preg_replace('/[^0-9,.]/', '', self::getHeaders('X-Forwarded-For')));
                for($i = 0 ; $i <= count($chain); $i ++){
                    $ip = $chain[$i];

                    if((!preg_match("!^(192\.168|10\.0\.0)!", $ip) && $ip != "127.0.0.1") || $i == count($chain) - 1){
                        self::$clientIp = $ip;
                        return self::$clientIp;                        
                    }
                }
            }
        }
    
        // X-Forwarded-For header has not been transmitted, get the REMOTE_ADDR header
        self::$clientIp = getenv('REMOTE_ADDR');
        return self::$clientIp;
    }


    /**
     * This function returns the value of the variable $name in the request body, or all the body if $name is not provided
     * @param string $name The variable name
     * @return string|array The parameter value or all the body
     */
    public static function getBody($name = ""){
        if(!self::$body){
            if(self::getHeaders('Content-Type') === 'application/json'){
                self::$body = json_decode(file_get_contents('php://input'), true);
            }
            else{
                self::$body = $_POST;
            }
        }
        if($name){
            return isset(self::$body[$name]) ? self::$body[$name] : null;
        }
        else{            
            return self::$body;
        }
    }

    /**
     * Get the request uploaded files for the given name, or all files if $name is not provided
     * @param string $name The key in $_FILES to get
     * @return string|array The file or all files
     */
    public static function getFiles($name = ''){
        if($name){
            return isset($_FILES[$name]) ? $_FILES[$name] : array();
        }
        else{
{'fraction_non_alphanumeric': 0.13152804642166344, 'fraction_numerical': 0.010223818734457032, 'mean_word_length': 1.7767548906789412, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '196751', 'n_tokens_mistral': 2100, 'n_tokens_neox': 2016, 'n_words': 603}