bludit/kernel/url.class.php

204 lines
3.8 KiB
PHP
Raw Normal View History

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-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.
$this->uri = urldecode($_SERVER['REQUEST_URI']);
// URI Lowercase
//$this->uri = helperText::lowercase($this->uri);
2015-03-27 02:00:01 +01:00
$this->uriStrlen = helperText::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']);
// 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;
}
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;
}
public function setNotFound($error = true)
{
$this->notFound = $error;
}
2015-03-27 02:00:01 +01:00
/*
2015-03-08 18:02:59 +01:00
public function is_tag($filter)
{
2015-03-27 02:00:01 +01:00
$slug = $this->getSlugAfterFilter($filter);
// Check if the filter doesn't match in the uri.
if($slug===false)
2015-03-08 18:02:59 +01:00
return false;
2015-03-27 02:00:01 +01:00
// Check if the slug is empty.
if(empty($slug))
$this->setNotFound(true);
2015-03-08 18:02:59 +01:00
return true;
}
public function is_post($filter)
{
2015-03-27 02:00:01 +01:00
$slug = $this->getSlugAfterFilter($filter);
// Check if the filter doesn't match in the uri.
if($slug===false)
2015-03-08 18:02:59 +01:00
return false;
2015-03-27 02:00:01 +01:00
// Check if the slug is empty.
if(empty($slug))
$this->setNotFound(true);
2015-03-08 18:02:59 +01:00
$this->whereAmI = 'post';
return true;
}
public function is_page($filter)
{
2015-03-27 02:00:01 +01:00
$slug = $this->getSlugAfterFilter($filter);
// Check if the filter doesn't match in the uri.
if($slug===false)
2015-03-08 18:02:59 +01:00
return false;
2015-03-27 02:00:01 +01:00
// Check if the slug is empty.
if(empty($slug))
$this->setNotFound(true);
2015-03-08 18:02:59 +01:00
$this->whereAmI = 'page';
return true;
}
2015-03-27 02:00:01 +01:00
public function isAdmin($filter)
{
$slug = $this->getSlugAfterFilter($filter);
// Check if the filter doesn't match in the uri.
if($slug===false)
return false;
// Check if the slug is empty.
if(empty($slug))
$this->setNotFound(true);
$this->whereAmI = 'admin';
return true;
}
*/
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-03-14 15:45:30 +01:00
if($filter=='/')
$filter = HTML_PATH_ROOT;
2015-03-27 02:00:01 +01:00
// Check if the filter is in the uri.
2015-03-08 18:02:59 +01:00
$position = helperText::strpos($this->uri, $filter);
if($position===false)
return false;
$start = $position + helperText::length($filter);
2015-03-27 02:00:01 +01:00
$end = $this->uriStrlen;
2015-03-08 18:02:59 +01:00
2015-03-27 02:00:01 +01:00
$slug = helperText::cut($this->uri, $start, $end);
$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
}
}