Guest User

Untitled

a guest
May 26th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4.     define('ABS_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . '/');
  5.  
  6.     require_once ABS_PATH . 'load.php';
  7.    
  8.     $router = new router ();
  9.     $router -> get_router ();
  10.     $router -> load ();
  11.    
  12.     class router
  13.     {
  14.         public $uri;
  15.         public $route;
  16.         public $file;
  17.         public $allowed_routes = array('home', 'login', 'signup', 'account', 'logout', 'blog');
  18.         public $protected_subdomains = array('media');
  19.        
  20.         function __construct ($db)
  21.         {
  22.             if(empty($uri))
  23.             {
  24.                 $this->uri = uri::getInstance();
  25.             }
  26.         }
  27.        
  28.         function get_router ()
  29.         {
  30.             if($this->uri->subdomain() && !in_array($this->uri->subdomain(), $this->protected_subdomains))
  31.             {
  32.                 $this->route = 'blog';
  33.             }
  34.             else if(!$this->uri->fragment(1))
  35.             {
  36.                 $this->route = 'home';
  37.             }
  38.             else
  39.             {
  40.                 $this->route = $this->uri->fragment(1);
  41.             }
  42.            
  43.             if(in_array($this->route, $this->allowed_routes))
  44.             {
  45.                 $this->file = $this->route . '.php';
  46.             }
  47.             else
  48.             {
  49.                 $this->file = '404.php';
  50.             }          
  51.         }
  52.        
  53.         function load ()
  54.         {
  55.             include $this->file;
  56.         }
  57.     }
  58.  
  59.     class uri
  60.     {
  61.         public static $fragments = array();
  62.        
  63.         public static $subdomain;
  64.        
  65.         public static $domain;
  66.    
  67.         private static $instance = null;
  68.    
  69.         public static function getInstance()
  70.         {
  71.              if(is_null(self::$instance))
  72.              {
  73.                  self::$instance = new uri;
  74.              }
  75.             return self::$instance;
  76.         }
  77.    
  78.         private function __construct()
  79.         {
  80.             self::$fragments    = explode('/', $_SERVER['REQUEST_URI']);
  81.             self::$domain       = explode('.', $_SERVER['SERVER_NAME']);
  82.         }
  83.    
  84.         public function fragment($key)
  85.         {
  86.             if(array_key_exists($key, self::$fragments))
  87.             {
  88.                 return self::$fragments[$key];
  89.             }
  90.             return false;
  91.         }
  92.        
  93.         public function subdomain()
  94.         {  
  95.             return self::$subdomain = (array_key_exists(count(self::$domain) - 3, self::$domain)) ? self::$domain[count(self::$domain) - 3] : false;           
  96.         }
  97.    
  98.         private function __clone()
  99.         {
  100.         }
  101.     }
Add Comment
Please, Sign In to add comment