<commit_before><?php if(!defined('CAFFEINE_ROOT')) die ('No direct script access allowed.');
/**
 * =============================================================================
 * Router
 * @author <NAME> <<EMAIL>>
 * @version 1.0
 *
 * The Router library is used to provide information about the URL. It parses
 * out segments and allows the application to make use of them as well as
 * providing easy ways for constructing URL's and redirecting.
 *
 * NOTE: Trailing slashes are ALWAYS stripped from URL's.
 *
 * @event modify_segments
 *      Allows modification of parsed segments before finishing.
 * =============================================================================
 */
class Router {

    protected static $_path     = ''; // Stores current path
    protected static $_segments = array(); // Stores parsed segments
    protected static $_base     = null; // Stores the applications base URL
    
    /**
     * -------------------------------------------------------------------------
     * Returns the current URL path. This does NOT include the base URL.
     * -------------------------------------------------------------------------
     */
    public static function current_path() {
        return self::$_path;
    }
    
    /**
     * -------------------------------------------------------------------------
     * Returns the full current URL.
     * -------------------------------------------------------------------------
     */
    public static function current_url() {
        return self::url(self::$_path);
    }
    
    /**
     * -------------------------------------------------------------------------
     * Returns the full base URL.
     * -------------------------------------------------------------------------
     */
    public static function base() {
        return self::$_base;
    }

    /**
     * -------------------------------------------------------------------------
     * TODO
     * -------------------------------------------------------------------------
     */
	public static function subdomain($tld_count = ROUTER_TLD_COUNT) 
	{
		$host_bits = explode('.', 
			str_ireplace('www.', '', $_SERVER['HTTP_HOST']), 2); 

		if(substr_count($host_bits[1], '.') == $tld_count)
			return $host_bits[0];

		return null;
	}
    
    /**
     * -------------------------------------------------------------------------
     * Combines a given path with the base path and returns the full URL. This
     * allows an application to use full URL paths without being dependant on 
     * a specific domain.
     *
     * @param $path
     *      The path you want combined with the base URL.
     *
     * @param $args
     *      An array of arguments to be replaced in the URL. Check PHP's
     *      sprintf function for syntax.
     *
     * @return
     *      Returns a full URL of the base path and given path combined.
     */
    public static function url() 
    {
        $args = func_get_args();
        
        return sprintf('%s/%s', self::$_base, 
            rtrim(call_user_func_array('sprintf', $args), '/'));
    }
    
    /**
     * -------------------------------------------------------------------------
     * Gets the value of the given segment number. Segment numbers are based on
     * an array, so the first value is always 0.
     *
     * @param $num
     *      The segment number you want a value for.
     *
     * @return
     *      Returns the segment value if it exists, false otherwise.
     * -------------------------------------------------------------------------
     */
    public static function segment($num) 
    {
        if(isset(self::$_segments[$num]))
            return self::$_segments[$num];
        return false;
    }
    
    /**
     * -------------------------------------------------------------------------
     * TODO
     * -------------------------------------------------------------------------
     */
    public static function segments() {
        return self::$_segments;
    }
    
    /**
     * -------------------------------------------------------------------------
     * Redirects the page to a given path or URL. If the given path doesn't
     * start with 'http', its assumed an application path and is combined with
     * the base URL.
     *
     * @param $path
     *      The full URL or application path you want to be redirected to.
     * -------------------------------------------------------------------------
     */
    public static function redirect($path) 
    {
        if(substr(strtolower($path), 0, 4) != 'http')
            $path = self::url($path);
            
        header('Location: ' . $path);
    }

    /**
     * -------------------------------------------------------------------------
     * Parses segments out of the URL and provides them to the application
     * through events for use.
     * -------------------------------------------------------------------------
     */
    protected static function _parse_segments()
    {
        Debug::log('Router', 'Parsing URL segments');
        $host = $_SERVER['HTTP_HOST'];
        $bits = explode('index.php', $_SERVER['SCRIPT_NAME']);
        $dir = '';
               
        if(isset($bits[0]))
            $dir = $bits[0];

		$http = (isset($_SERVER['HTTPS'])) ? 'https://' : 'http://';

        self::$_base = rtrim($http . $host . $dir, '/');
        self::$_path = isset($_GET['q']) ? $_GET['q'] : '';
        
        self::$_segments = strlen(self::$_path) ? explode('/', self::$_path) : array();
    }

}
<commit_msg>Updated Router module to use relative URLs. Provides better support for SSL.<commit_after><?php if(!defined('CAFFEINE_ROOT')) die ('No direct script access allowed.');
/**
 * =============================================================================
 * Router
 * @author <NAME> <<EMAIL>>
 * @version 1.0
 *
 * The Router library is used to provide information about the URL. It parses
 * out segments and allows the application to make use of them as well as
 * providing easy ways for constructing URL's and redirecting.
 *
 * NOTE: Trailing slashes are ALWAYS stripped from URL's.
 *
 * @event modify_segments
 *      Allows modification of parsed segments before finishing.
 * =============================================================================
 */
class Router {

    protected static $_path     = ''; // Stores current path
    protected static $_segments = array(); // Stores parsed segments
    protected static $_base     = null; // Stores the applications base URL
    
    /**
     * -------------------------------------------------------------------------
     * Returns the current URL path. This does NOT include the base URL.
     * -------------------------------------------------------------------------
     */
    public static function current_path() {
        return self::$_path;
    }
    
    /**
     * -------------------------------------------------------------------------
     * Returns the full current URL.
     * -------------------------------------------------------------------------
     */
    public static function current_url() {
        return self::url(self::$_path);
    }
    
    /**
     * -------------------------------------------------------------------------
     * Returns the full base URL.
     * -------------------------------------------------------------------------
     */
    public static function base() {
        return self::$_base;
    }

    /**
     * -------------------------------------------------------------------------
     * TODO
     * -------------------------------------------------------------------------
     */
	public static function subdomain($tld_count = ROUTER_TLD_COUNT) 
	{
		$host_bits = explode('.', 
			str_ireplace('www.', '', $_SERVER['HTTP_HOST']), 2); 

		if(substr_count($host_bits[1], '.') == $tld_count)
			return $host_bits[0];

		return null;
	}
    
    /**
     * -------------------------------------------------------------------------
     * Combines a given path with the base path and returns the full URL. This
     * allows an application to use full URL paths without being dependant on 
     * a specific domain.
     *
     * @param $path
     *      The path you want combined with the base URL.
     *
     * @param $args
     *      An array of arguments to be replaced in the URL. Check PHP's
     *      sprintf function for syntax.
     *
     * @return
     *      Returns a full URL of the base path and given path combined.
     */
    public static function url() 
    {
        $args = func_get_args();

        return sprintf('%s/%s', self::$_base, 
            rtrim(call_user_func_array('sprintf', $args), '/'));
    }
    
    /**
     * -------------------------------------------------------------------------
     * Gets the value of the given segment number. Segment numbers are based on
     * an array, so the first value is always 0.
     *
     * @param $num
     *      The segment number you want a value for.
     *
     * @return
     *      Returns the segment value if it exists, false otherwise.
     * -------------------------------------------------------------------------
     */
    public static function segment($num) 
    {
        if(isset(self::$_segments[$num]))
            return self::$_segments[$num];
        return false;
    }
    
    /**
     * -------------------------------------------------------------------------
     * TODO
     * -------------------------------------------------------------------------
     */
    public static function segments() {
        return self::$_segments;
    }
    
    /**
     * -------------------------------------------------------------------------
     * Redirects the page to a given path or URL. If the given path doesn't
     * start with 'http', its assumed an application path and is combined with
     * the base URL.
     *
     * @param $path
     *      The full URL or application path you want to be redirected to.
     * -------------------------------------------------------------------------
     */
    public static function redirect($path) 
    {
        if(substr(strtolower($path), 0, 4) != 'http')
            $path = self::url($path);
            
        header('Location: ' . $path);
    }

    /**
     * -------------------------------------------------------------------------
     * Parses segments out of the URL and provides them to the application
     * through events for use.
     * -------------------------------------------------------------------------
     */
    protected static function _parse_segments()
    {
        Debug::log('Router', 'Parsing URL segments');
        $host = $_SERVER['HTTP_HOST'];
        $bits = explode('index.php', $_SERVER['SCRIPT_NAME']);
        $dir = '';
               
        if(isset($bits[0]))
            $dir = $bits[0];

		$http = (isset($_SERVER['HTTPS'])) ? 'https://' : 'http://';

        //self::$_base = rtrim($http . $host . $dir, '/');
        self::$_base = rtrim($dir, '/'); // Relative
        self::$_path = isset($_GET['q']) ? $_GET['q'] : '';
        
        self::$_segments = strlen(self::$_path) ? explode('/', self::$_path) : array();
    }

}

{'fraction_non_alphanumeric': 0.3427146419665123, 'fraction_numerical': 0.0017812611328820805, 'mean_word_length': 1.103991006183249, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3276056', 'n_tokens_mistral': 2555, 'n_tokens_neox': 2475, 'n_words': 796}