2015-03-08 18:02:59 +01:00
|
|
|
<?php defined('BLUDIT') or die('Bludit CMS.');
|
|
|
|
|
|
|
|
class Url
|
|
|
|
{
|
|
|
|
private $uri;
|
2015-03-27 02:00:01 +01:00
|
|
|
private $uriStrlen;
|
2015-03-08 18:02:59 +01:00
|
|
|
private $whereAmI;
|
|
|
|
private $slug;
|
|
|
|
private $filters; // Filters for the URI
|
|
|
|
private $notFound;
|
2015-05-15 00:07:45 +02:00
|
|
|
private $parameters;
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-03-14 15:45:30 +01:00
|
|
|
function __construct()
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
|
|
|
// Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
|
2015-05-15 00:07:45 +02:00
|
|
|
$decode = urldecode($_SERVER['REQUEST_URI']);
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-05-15 00:07:45 +02:00
|
|
|
// Parse, http://php.net/parse_url
|
|
|
|
$parse = parse_url($decode);
|
|
|
|
|
|
|
|
$this->uri = $parse['path'];
|
|
|
|
|
|
|
|
$this->parameters = $_GET;
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-05-31 03:06:55 +02:00
|
|
|
$this->uriStrlen = Text::length($this->uri);
|
2015-03-08 18:02:59 +01:00
|
|
|
|
|
|
|
$this->whereAmI = 'home';
|
|
|
|
|
|
|
|
$this->notFound = false;
|
|
|
|
|
|
|
|
$this->slug = false;
|
2015-03-27 02:00:01 +01:00
|
|
|
|
|
|
|
$this->filters = array();
|
2015-03-14 15:45:30 +01:00
|
|
|
}
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-03-14 15:45:30 +01:00
|
|
|
// Filters may be changed for different languages
|
|
|
|
// Ex (Spanish): Array('post'=>'/publicacion/', 'tag'=>'/etiqueta/', ....)
|
|
|
|
// Ex (English): Array('post'=>'/post/', 'tag'=>'/tag/', ....)
|
|
|
|
public function checkFilters($filters)
|
|
|
|
{
|
2015-03-27 02:00:01 +01:00
|
|
|
// Get the admin filter
|
|
|
|
$adminFilter['admin'] = $filters['admin'];
|
|
|
|
unset($filters['admin']);
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2015-03-27 02:00:01 +01:00
|
|
|
// Sort by filter length
|
|
|
|
uasort($filters, array($this, 'sortByLength'));
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-03-27 02:00:01 +01:00
|
|
|
// Push the admin filter first
|
|
|
|
$filters = $adminFilter + $filters;
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-03-27 02:00:01 +01:00
|
|
|
$this->filters = $filters;
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-03-27 02:00:01 +01:00
|
|
|
foreach($filters as $filterKey=>$filter)
|
|
|
|
{
|
|
|
|
$slug = $this->getSlugAfterFilter($filter);
|
|
|
|
if($slug!==false)
|
|
|
|
{
|
|
|
|
if(empty($slug))
|
|
|
|
{
|
|
|
|
if($filter==='/')
|
|
|
|
{
|
|
|
|
$this->whereAmI = 'home';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setNotFound(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->whereAmI = $filterKey;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-03-08 18:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function slug()
|
|
|
|
{
|
|
|
|
return $this->slug;
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
public function explodeSlug($delimiter="/")
|
|
|
|
{
|
|
|
|
return explode($delimiter, $this->slug);
|
|
|
|
}
|
|
|
|
|
2015-03-08 18:02:59 +01:00
|
|
|
public function uri()
|
|
|
|
{
|
|
|
|
return $this->uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the filter used
|
|
|
|
public function filters($type)
|
|
|
|
{
|
|
|
|
return $this->filters[$type];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return: home, tag, post
|
|
|
|
public function whereAmI()
|
|
|
|
{
|
|
|
|
return $this->whereAmI;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setWhereAmI($where)
|
|
|
|
{
|
|
|
|
$this->whereAmI = $where;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function notFound()
|
|
|
|
{
|
|
|
|
return $this->notFound;
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:07:45 +02:00
|
|
|
public function pageNumber()
|
|
|
|
{
|
|
|
|
if(isset($this->parameters['page'])) {
|
|
|
|
return $this->parameters['page'];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-08 18:02:59 +01:00
|
|
|
public function setNotFound($error = true)
|
|
|
|
{
|
|
|
|
$this->notFound = $error;
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
public function getDomain()
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
2015-05-05 03:00:01 +02:00
|
|
|
if(!empty($_SERVER['HTTPS'])) {
|
|
|
|
$protocol = 'https://';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$protocol = 'http://';
|
|
|
|
}
|
2015-03-27 02:00:01 +01:00
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
$domain = $_SERVER['HTTP_HOST'];
|
2015-03-27 02:00:01 +01:00
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
return $protocol.$domain.HTML_PATH_ROOT;
|
2015-03-27 02:00:01 +01:00
|
|
|
}
|
|
|
|
|
2015-03-08 18:02:59 +01:00
|
|
|
// Return the slug after the $filter
|
2015-03-27 02:00:01 +01:00
|
|
|
// If the filter is not contain in the uri, returns FALSE
|
|
|
|
// If the filter is contain in the uri and the slug is not empty, returns the slug
|
2015-03-08 18:02:59 +01:00
|
|
|
// ex: http://domain.com/cms/$filter/slug123 => slug123
|
2015-03-27 02:00:01 +01:00
|
|
|
private function getSlugAfterFilter($filter)
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
2015-05-05 03:00:01 +02:00
|
|
|
if($filter=='/') {
|
2015-03-14 15:45:30 +01:00
|
|
|
$filter = HTML_PATH_ROOT;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
2015-03-14 15:45:30 +01:00
|
|
|
|
2015-03-27 02:00:01 +01:00
|
|
|
// Check if the filter is in the uri.
|
2015-05-31 03:06:55 +02:00
|
|
|
$position = Text::strpos($this->uri, $filter);
|
2015-05-05 03:00:01 +02:00
|
|
|
if($position===false) {
|
2015-03-08 18:02:59 +01:00
|
|
|
return false;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-05-31 03:06:55 +02:00
|
|
|
$start = $position + Text::length($filter);
|
2015-03-27 02:00:01 +01:00
|
|
|
$end = $this->uriStrlen;
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-05-31 03:06:55 +02:00
|
|
|
$slug = Text::cut($this->uri, $start, $end);
|
2015-05-05 03:00:01 +02:00
|
|
|
$this->slug = trim($slug, '/');
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-03-27 02:00:01 +01:00
|
|
|
return $slug;
|
|
|
|
}
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-03-27 02:00:01 +01:00
|
|
|
private function sortByLength($a, $b)
|
|
|
|
{
|
|
|
|
return strlen($b)-strlen($a);
|
2015-03-08 18:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|