<commit_before><?php
/**
 * Application
 */

require(__DIR__."/../../vendor/torophp/torophp/src/Toro.php");
require(__DIR__."/cache.php");
require(__DIR__."/actions.php");
require(__DIR__."/validator.php");

class AppOrigin {

	public $loginKey = "";

	protected $routes = array(
		    "/" => "IndexHandler",
		    "/:alpha" => "IndexHandler",
		    "/index/:alpha/:alpha" => "IndexHandler"
		);

	protected $routesAdmin = array(
			"/admin/history" => "AdminHistoryHandler",
		    "/admin/login" => "AdminLoginHandler",
		    "/admin/logout" => "AdminLogoutHandler");

	/**
	 * Fonction de parsage de l'URI courante
	 * @param  string $extra ajouter un / pour accéder au dossier
	 * @return array        explosion de chaine
	 */
	public function getRouteInfos($extra = null) {

		$routes = $this->routes;

		//from ToroPHP (:23:43)
		
		$request_method = strtolower($_SERVER['REQUEST_METHOD']);
        $path_info = '/';
        if (!empty($_SERVER['PATH_INFO'])) {
            $path_info = $_SERVER['PATH_INFO'];
        }
        else if (!empty($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] !== '/index.php') {
            $path_info = $_SERVER['ORIG_PATH_INFO'];
        }
        else {
            if (!empty($_SERVER['REQUEST_URI'])) {
                $path_info = (strpos($_SERVER['REQUEST_URI'], '?') > 0) ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'];
            }
        }

		$discovered_handler = null;
        $regex_matches = array();

        if (isset($routes[$path_info])) {
            $discovered_handler = $routes[$path_info];
        }
        else if ($routes) {
            $tokens = array(
                ':string' => '([a-zA-Z]+)',
                ':number' => '([0-9]+)',
                ':alpha'  => '([a-zA-Z0-9-_]+)'
            );
            foreach ($routes as $pattern => $handler_name) {
                $pattern = strtr($pattern, $tokens);
                if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) {
                    $discovered_handler = $handler_name;
                    $regex_matches = $matches;
                    break;
                }
            }
        }
        $modelEx = explode("Handler",$discovered_handler);
        $model = strtolower($modelEx[0]);

        if(count($regex_matches) >= 2) {
        	$id = $regex_matches[1];
        } else {
        	$id = "";
        }

        $v = new AdminValidator();
        $v->validator($id,'model_id',$model);

		return array('model' => $model, 'id' => $extra.$id);
	}

	/**
	 * Demarrage du router 
	 */
	
	public function startRoutes() {

		ToroHook::add("404", function() {
		    $this->error404();
		});

		$routes = $this->routes;
		$routesAdmin = $this->routesAdmin;
		
		if( defined('ADMIN') ) { 
			$routes = array_merge($routes, $routesAdmin);
		}

		Toro::serve($routes);
	}

	public function startCache() {

		ToroHook::add("before_handler", function($vars) { <commit_msg>fix autoload vendor + php5.3 static bug<commit_after><?php
/**
 * Application
 */

require(__DIR__."/../../vendor/autoload.php");
require(__DIR__."/cache.php");
require(__DIR__."/actions.php");
require(__DIR__."/validator.php");

class AppOrigin {

	public $loginKey = "";

	protected $routes = array(
		    "/" => "IndexHandler",
		    "/:alpha" => "IndexHandler",
		    "/index/:alpha/:alpha" => "IndexHandler"
		);

	protected $routesAdmin = array(
			"/admin/history" => "AdminHistoryHandler",
		    "/admin/login" => "AdminLoginHandler",
		    "/admin/logout" => "AdminLogoutHandler");

	/**
	 * Fonction de parsage de l'URI courante
	 * @param  string $extra ajouter un / pour accéder au dossier
	 * @return array        explosion de chaine
	 */
	public function getRouteInfos($extra = null) {

		$routes = $this->routes;

		//from ToroPHP (:23:43)
		
		$request_method = strtolower($_SERVER['REQUEST_METHOD']);
        $path_info = '/';
        if (!empty($_SERVER['PATH_INFO'])) {
            $path_info = $_SERVER['PATH_INFO'];
        }
        else if (!empty($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] !== '/index.php') {
            $path_info = $_SERVER['ORIG_PATH_INFO'];
        }
        else {
            if (!empty($_SERVER['REQUEST_URI'])) {
                $path_info = (strpos($_SERVER['REQUEST_URI'], '?') > 0) ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'];
            }
        }

		$discovered_handler = null;
        $regex_matches = array();

        if (isset($routes[$path_info])) {
            $discovered_handler = $routes[$path_info];
        }
        else if ($routes) {
            $tokens = array(
                ':string' => '([a-zA-Z]+)',
                ':number' => '([0-9]+)',
                ':alpha'  => '([a-zA-Z0-9-_]+)'
            );
            foreach ($routes as $pattern => $handler_name) {
                $pattern = strtr($pattern, $tokens);
                if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) {
                    $discovered_handler = $handler_name;
                    $regex_matches = $matches;
                    break;
                }
            }
        }
        $modelEx = explode("Handler",$discovered_handler);
        $model = strtolower($modelEx[0]);

        if(count($regex_matches) >= 2) {
        	$id = $regex_matches[1];
        } else {
        	$id = "";
        }

        $v = new AdminValidator();
        $v->validator($id,'model_id',$model);

		return array('model' => $model, 'id' => $extra.$id);
	}

	/**
	 * Demarrage du router 
	 */
	
	public function startRoutes() {

		ToroHook::add("404", function() {
		    header('HTTP/1.0 404 Not Found');
    		echo "Not found"; 
    		exit;  
		});

		$routes = $this->routes;
		$routesAdmin = $this->routesAdmin;
		
		if( defined('ADMIN') ) { 
			$routes = array_merge($routes, $routesAdmin);
		}

		Toro::serve($routes);
	}

	public function startCache() {

		ToroHook::add("before_handler", function($vars) { 
{'fraction_non_alphanumeric': 0.1857190747569561, 'fraction_numerical': 0.00670465973851827, 'mean_word_length': 2.260655737704918, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, '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': '2044250', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1896, 'n_words': 330}