Created
December 25, 2018 20:13
-
-
Save mladoux/aaca02f35495456d692edebc2c0a44eb to your computer and use it in GitHub Desktop.
URI Parser for DoomHamster Media Group ( Remove namespace from top of script if not using namespaces )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace DHMedia\Input; | |
/** | |
* DHMedia URI Parser | |
* | |
* @author Mark LaDoux <[email protected]> | |
* @copyright Copyright © 2018, DoomHamster Media | |
* @license MIT <https://opensource.org/licenses/MIT> | |
*/ | |
class URI | |
{ | |
/** | |
* URI Data | |
* | |
* @access protected | |
* @var array | |
*/ | |
protected $data = []; | |
/** | |
* Class Constructior | |
* | |
* Parses the current URI and stores it into an array for use. | |
* | |
* @access public | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// Smaller variable that's easier to work with | |
$sd = $_SERVER; | |
// Data Stuff | |
$data['hostname'] = $sd['SERVER_NAME']; | |
$data['secure'] = (isset($sd['HTTPS']) && $sd['HTTPS'] == 'on') ? true : false; | |
$data['port'] = (int) $sd['SERVER_PORT']; | |
$data['document'] = $sd['DOCUMENT_URI']; | |
// Get raw uri | |
$raw_uri = ltrim($sd['REQUEST_URI'], $data['document']); | |
// Ensure that $data['raw_uri'] is set to something. | |
if (empty($raw_uri)) { | |
$raw_uri = '/'; | |
} | |
$data['raw_uri'] = $raw_uri; | |
// Check for uri query string | |
$pos = strpos($data['raw_uri'], '?'); | |
if ($pos === false) { | |
// We don't need to do anything, set the uri and query arrays. | |
$data['uri'] = $data['raw_uri']; | |
$data['query'] = []; | |
} else { | |
// we need to split the query's from the uri | |
$limit = 2; | |
$parts = explode('?', $data['raw_uri'], $limit); | |
$data['uri'] = $parts[0]; | |
$queries = explode('&', $parts[1]); | |
foreach ($queries as $query) { | |
$limit = 2; | |
$keyval = explode('=', $query, $limit); | |
$data['query'][$keyval[0]] = $keyval[1]; | |
} | |
} | |
// Get URI segments | |
$data['segment'] = array_merge([], array_filter(explode('/', $data['uri']))); | |
$this->data = $data; | |
} | |
/** | |
* segment | |
* | |
* Returns the requested uri segment | |
* | |
* @access public | |
* @param int $n Segment to retrieve | |
* @return string Value of segment | |
*/ | |
public function segment(int $n) | |
{ | |
// If the script requests segment 0, we will return the document name | |
// We stripped it out before forming the segments for consistency, so that | |
// it will even be available here if url rewriting is used to hide it. | |
if ($n === 0) { | |
return $this->data['document']; | |
} | |
$key = $n - 1; | |
// if the segment is set, return it, else return false | |
return ($this->data['segment'][$key]) ? $this->data['segment'][$key] : false; | |
} | |
/** | |
* Retrieve value of query | |
* | |
* @access public | |
* @param string $key Key name | |
* @return mixed Key value | |
*/ | |
public function query(string $key) | |
{ | |
return (isset($this->data['query'][$key])) ? $this->data['query'][$key] : false; | |
} | |
/** | |
* Return whether or not the request is using a secure protocol | |
* | |
* @access public | |
* @return bool true if secure, false if not. | |
*/ | |
public function secure() | |
{ | |
return $this->data['secure']; | |
} | |
/** | |
* Return the requested hostname from the URL | |
* | |
* @access public | |
* @return string | |
*/ | |
public function hostname() | |
{ | |
return $this->data['hostname']; | |
} | |
/** | |
* Return the port number site was accessed over | |
* | |
* @access public | |
* @return int | |
*/ | |
public function port() | |
{ | |
return $this->data['port']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment