Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace pb;
  3. if ( ! defined('PB_ENGINE_PATH')) exit('No direct script access allowed');
  4. /**
  5.  * @package phpbull
  6.  * @author Piyush Mishra<me[at]piyushmishra[dot]com>
  7.  */
  8.  
  9. class uri
  10. {
  11.     private $_segments;
  12.     private $_request_uri;
  13.     public $site_url;
  14.     function __construct($apps)
  15.     {
  16.         $scheme= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']!='off') ? 'https':'http';
  17.         $host_parts = explode(':', $_SERVER['HTTP_HOST'], 2);
  18.         $http_host = array_shift($host_parts);
  19.         $http_port = array_shift($host_parts);
  20.         $request_uri=$this->request_uri();
  21.         if(filter_var($scheme."://".$http_host.$request_uri, FILTER_VALIDATE_URL))
  22.         {
  23.             if(isset($apps) && is_array($apps))
  24.             foreach($apps as $url=>$relpath)
  25.             {
  26.                 $urla=parse_url($url);
  27.                 if($urla['scheme']==$scheme && $urla['host']==$http_host && $urla['port'] == $http_port)
  28.                 {
  29.                     if(strpos($request_uri,$urla['path'])===0)
  30.                     {
  31.                         $this->site_url=$url;
  32.                         $this->populate($urla['path']);
  33.                         define('PB_APP_PATH',realpath($relpath).'/');
  34.                         break;
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     }
  40.     public function segment($id,$false=false)
  41.     {
  42.         if(array_key_exists($id,$this->_segments))
  43.             return $this->_segments[$id];
  44.         return $false;
  45.     }
  46.     public function total_segments()
  47.     {
  48.         return count($this->_segments);
  49.     }
  50.     public function segment_array()
  51.     {
  52.         return $this->_segments;
  53.     }
  54.     private function populate($path)
  55.     {
  56.         $relative=str_replace($path,"",$this->_request_uri);
  57.         $segments=explode('/',$relative);
  58.         $this->_segments=$segments;
  59.         array_pop($this->_segments);
  60.         array_unshift($this->_segments,'');
  61.         unset($this->_segments[0]);
  62.     }
  63.     private function request_uri()
  64.     {
  65.         $this->_request_uri=rtrim($_SERVER['REQUEST_URI'],'/').'/';
  66.         return $this->_request_uri;
  67.     }
  68. }
  69.  
  70. /**
  71.  * End of file Uri
  72.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement