<?php
//+-----------------------------------------------------+//
//+ Copyright Floopo.com
//+ router.php
//+ Main router for script
//+ LAST UPDATED : 26 June 2009
//+-----------------------------------------------------+//

 class Router {

    var $component;
    var $get_vars = array();
    var $post_vars = array();
    var $route;
    var $component_index_path;
    

    //______________________________________________________________
    //__Get all get variables_______________________________________

    function Router() {

        //Set all get vars
        if (isset($_GET)) {
            foreach ($_GET as $key => $value) {

                ($key == 'option') ? $this->component = $value:null;

            }
            $this->get_vars = $_GET; //$_GET = NULL;
        }

        //Set all Post vars
        if (isset($_POST)) {
            foreach ($_POST as $key => $value) {

                ($key == 'option') ? $this->component = $value:null;

            }
            $this->post_vars = $_POST; //$_POST = NULL;
        }
      
    }


    function Route() {
        //has component been passes
        $component_index_file = end(explode('com_', $this->component)) . '.php';
        $component_index_path = BASEPATH . '/com/' . $this->component . '/' . $component_index_file;

        //No component
        if (!$this->component){
        $route_array = array('code'=>204, 'get_vars'=>NULL, 'post_vars'=>NULL, 'component'=>NULL);
        return $route_array;           
        }
        
        //Component Found
        if (is_file($component_index_path)) {
        $this->component_index_path = $component_index_path;
        $route_array = array('code'=>200, 'get_vars'=>$this->get_vars, 'post_vars'=>$this->post_vars, 'component'=>$component_index_path);
        return $route_array;          
        }
        
        //Component Not Found
        if (!is_file($component_index_path)) {
        $route_array = array('code'=>404, 'get_vars'=>NULL, 'post_vars'=>NULL, 'component'=>NULL);
        return $route_array;          
        }

}


}



// >> FUNCTIONS
// - Checks if a component called in a Post or Get esists
// - Includes the file

// >> REQUIRES
// - Defined BASEPATH (e.g. define('BASEPATH', dirname(__FILE__));)
?>