<commit_before><?php

/**
 * Utility methods.
 */
class Utils {
    /**
     * Use this to sanitize data that somes in via $_GET or $_POST params;
     * this is to avoid injection attacks.
     */
    public static function sanitizeUrlParam($in) {
        return trim(preg_replace('/[^[:alnum:]\.,@_-\s]/', '', $in));
    }
    
    /**
     * Even more restrictive sanitization: will only accept integer values
     * between $min and $max.
     * @param mixed $in
     * @param integer $min
     * @param integer $max
     * @return mixed the sanitized input or null if it's invalid.
     */
    public static function sanitizeUrlParamIntRange($in, $min, $max) {
         if (!is_numeric($in)) return null;
         $numVal = intval($in);
         if ($numVal > $max) return null;
         if ($numVal < $in) return null;
         return $numVal;
    }
    
    /**
     * Sanitizes things that are intended to be used as paths or URLs.
     * @param type $in
     * @return type
     */
    public static function sanitizeUrlOrPath($in) {
        return trim(preg_replace('/[^[:alnum:]\.,@_-\s\/\:\?&]/', '', $in));
    }
    
    public static function getBaseUrl() {
        $topLevelPath = str_replace(
            Utils::sanitizeUrlOrPath($_SERVER['DOCUMENT_ROOT']), 
            "", 
            Utils::getBasePath());
        
        $httpsOn = (   isset($_SERVER["HTTPS"]) 
                    && 0 == stricmp($_SERVER["HTTPS"], "on"));
        
        $pageURL = $httpsOn ? 'https://' : 'http://';
        $pageURL .= Utils::sanitizeUrlOrPath($_SERVER["SERVER_NAME"]);
        
        $port = Utils::sanitizeUrlParamIntRange(
            $_SERVER["SERVER_PORT"], 
            0, 
            65535);
        if (   (!$httpsOn && $port != "80")
            || ($httpsOn && $port != "443")) {
            
            $pageURL .= ":$port";
        }
        
        return Utils::concatenatePaths($pageURL, $topLevelPath);
    }
    
    public static function getBasePath() {
        return realpath(Utils::concatenatePaths(__DIR__, '..'));
    }
    
    /**
     * Concatenates a set of paths, ensuring that double separators are not
     * retained.
     *
     * This is a variable-arity function.  Pass in as many tokens as you like;
     * they will be concatenated in the order passed. 
     */
    public static function concatenatePaths() {
        $args = func_get_args();
        $path = "";
        foreach ($args as $arg) {
            $path .= $arg;
            $path .= DIRECTORY_SEPARATOR;
        }
        
        if (0 == strcmp("/", DIRECTORY_SEPARATOR)) {
            $path = str_replace("http://", "http:////", $path);
            $path = str_replace("https://", "https:////", $path);
        }
        
        $processed = str_replace(
            DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, 
            DIRECTORY_SEPARATOR, 
            $path);
        
        $endSlashRemoved = strrchr($path, DIRECTORY_SEPARATOR);
        if (false == $endSlashRemoved) {
            return $processed;
        }
        
        $len = strlen($processed);
        return ($len <= 0 ? $processed : substr($processed, 0, $len - 1));
    }
}
?><commit_msg>Added netbeans project, minor tweaks.<commit_after><?php

/**
 * Utility methods.
 */
class Utils {
    /**
     * Use this to sanitize data that comes in via external params;
     * this is to avoid injection attacks.
     */
    public static function sanitizeUrlParam($in) {
        return trim(preg_replace('/[^[:alnum:]\.,@_-\s]/', '', $in));
    }

    /**
     * Even more restrictive sanitization: will only accept integer values
     * between $min and $max.
     *
     * - If $in is not numeric, returns null.
     * - If $in is greater than $max, returns $max
     * - If $in is less than $min, returns $min.
     *
     * @param mixed $in
     * @param integer $min
     * @param integer $max
     * @return mixed the sanitized input or null if it's invalid.
     */
    public static function sanitizeUrlParamIntRange($in, $min, $max) {
         if (!is_numeric($in)) return null;
         $numVal = intval($in);
         if ($numVal > $max) return $max;
         if ($numVal < $in) return $min;
         return $numVal;
    }

    /**
     * Sanitizes things that are intended to be used as paths or URLs.
     * @param type $in
     * @return type
     */
    public static function sanitizeUrlOrPath($in) {
        return trim(preg_replace('/[^[:alnum:]\.,@_-\s\/\:\?&]/', '', $in));
    }

    /*
     * Gets the base URL of the HTTP server where the script is being run.
     */
    public static function getBaseUrl() {
        $httpsOn = (   isset($_SERVER["HTTPS"])
                    && 0 == stricmp($_SERVER["HTTPS"], "on"));

        $pageURL = $httpsOn ? 'https://' : 'http://';
        $pageURL .= Utils::sanitizeUrlOrPath($_SERVER["SERVER_NAME"]);

        $port = Utils::sanitizeUrlParamIntRange(
            $_SERVER["SERVER_PORT"],
            0,
            65535);
        if (   (!$httpsOn && $port != "80")
            || ($httpsOn && $port != "443")) {

            $pageURL .= ":$port";
        }

        return $pageURL;
    }

    /*
     * Gets the server-side base path of the server where the script is
     * being run.
     */
    public static function getBasePath() {
        // Only works in Apache.
        return realpath(Utils::sanitizeUrlOrPath($_SERVER['DOCUMENT_ROOT']));
    }

    /**
     * Concatenates a set of paths, ensuring that double separators are not
     * retained.
     *
     * This is a variable-arity function.  Pass in as many tokens as you like;
     * they will be concatenated in the order passed.
     */
    public static function concatenatePaths() {
        $args = func_get_args();
        $path = "";
        foreach ($args as $arg) {
            $path .= $arg;
            $path .= DIRECTORY_SEPARATOR;
        }

        if (0 == strcmp("/", DIRECTORY_SEPARATOR)) {
            $path = str_replace("http://", "http:////", $path);
            $path = str_replace("https://", "https:////", $path);
        }

        $processed = str_replace(
            DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,
            DIRECTORY_SEPARATOR,
            $path);

        $endSlashRemoved = strrchr($path, DIRECTORY_SEPARATOR);
        if (false == $endSlashRemoved) {
            return $processed;
        }

        $len = strlen($processed);
        return ($len <= 0 ? $processed : substr($processed, 0, $len - 1));
    }
}
?>
{'fraction_non_alphanumeric': 0.14614197530864198, 'fraction_numerical': 0.0049382716049382715, 'mean_word_length': 1.9075818752803948, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4344769', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1915, 'n_words': 524}