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;
|
2016-01-21 03:16:32 +01:00
|
|
|
private $activeFilter;
|
2017-07-28 19:37:06 +02:00
|
|
|
private $httpCode;
|
|
|
|
private $httpMessage;
|
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
|
|
|
|
2017-05-09 00:24:15 +02:00
|
|
|
// Remove parameters GET, I don't use parse_url because has problem with utf-8
|
2015-07-26 02:25:13 +02:00
|
|
|
$explode = explode('?', $decode);
|
|
|
|
$this->uri = $explode[0];
|
2015-05-15 00:07:45 +02:00
|
|
|
$this->parameters = $_GET;
|
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;
|
2015-06-23 06:20:19 +02:00
|
|
|
$this->slug = '';
|
2015-03-27 02:00:01 +01:00
|
|
|
$this->filters = array();
|
2016-01-21 03:16:32 +01:00
|
|
|
$this->activeFilter = '';
|
2017-07-28 19:37:06 +02:00
|
|
|
$this->httpCode = 200;
|
|
|
|
$this->httpMessage = 'OK';
|
2015-03-14 15:45:30 +01:00
|
|
|
}
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-11-30 01:45:30 +01:00
|
|
|
// Filters change for different languages
|
2015-03-14 15:45:30 +01:00
|
|
|
// Ex (Spanish): Array('post'=>'/publicacion/', 'tag'=>'/etiqueta/', ....)
|
|
|
|
// Ex (English): Array('post'=>'/post/', 'tag'=>'/tag/', ....)
|
|
|
|
public function checkFilters($filters)
|
|
|
|
{
|
2017-09-15 20:02:53 +02:00
|
|
|
// Put the "admin" filter first
|
2015-03-27 02:00:01 +01:00
|
|
|
$adminFilter['admin'] = $filters['admin'];
|
|
|
|
unset($filters['admin']);
|
|
|
|
uasort($filters, array($this, 'sortByLength'));
|
2017-09-15 20:02:53 +02:00
|
|
|
$this->filters = $adminFilter + $filters;
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2017-09-15 20:02:53 +02:00
|
|
|
foreach ($this->filters as $filterName=>$filterURI) {
|
|
|
|
// $filterName = 'category'
|
|
|
|
// $filterURI = '/category/'
|
2017-09-20 16:51:54 +02:00
|
|
|
// $filterURIwoSlash = '/category'
|
|
|
|
$filterURIwoSlash = rtrim($filterURI, '/');
|
2017-05-09 00:24:15 +02:00
|
|
|
|
2017-09-20 16:51:54 +02:00
|
|
|
// $filterFull = '/base_url/category/'
|
2017-09-15 20:02:53 +02:00
|
|
|
$filterFull = ltrim($filterURI, '/');
|
|
|
|
$filterFull = HTML_PATH_ROOT.$filterFull;
|
2017-09-20 16:51:54 +02:00
|
|
|
$filterFullLenght = mb_strlen($filterFull, CHARSET);
|
2015-06-23 06:20:19 +02:00
|
|
|
|
2017-09-20 16:51:54 +02:00
|
|
|
// $filterFullwoSlash = '/base_url/category'
|
|
|
|
$filterFullwoSlash = ltrim($filterURIwoSlash, '/');
|
|
|
|
$filterFullwoSlash = HTML_PATH_ROOT.$filterURIwoSlash;
|
|
|
|
|
|
|
|
$subString = mb_substr($this->uri, 0, $filterFullLenght, CHARSET);
|
|
|
|
|
|
|
|
// Check coincidence without the last slash at the end, this case is notfound
|
2017-09-20 17:15:49 +02:00
|
|
|
if (($subString==$filterURIwoSlash) && ($filterName!='admin')) {
|
2017-09-20 16:51:54 +02:00
|
|
|
$this->setNotFound();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check coincidence with complete filterURI
|
2017-09-22 23:11:08 +02:00
|
|
|
if ($subString==$filterFull) {
|
2017-09-20 16:54:47 +02:00
|
|
|
$this->slug = mb_substr($this->uri, $filterFullLenght);
|
2017-09-21 20:42:03 +02:00
|
|
|
$this->setWhereAmI($filterName);
|
2016-01-21 03:16:32 +01:00
|
|
|
$this->activeFilter = $filterURI;
|
2015-06-23 06:20:19 +02:00
|
|
|
|
2017-09-21 20:42:03 +02:00
|
|
|
if (empty($this->slug) && ($filterName=='blog')) {
|
2017-10-19 00:05:02 +02:00
|
|
|
$this->setWhereAmI('blog');
|
2017-09-20 16:51:54 +02:00
|
|
|
} elseif (!empty($this->slug) && ($filterName=='blog')) {
|
|
|
|
$this->setNotFound();
|
|
|
|
return false;
|
2017-09-21 20:42:03 +02:00
|
|
|
} elseif (empty($this->slug) && ($filterURI=='/')) {
|
|
|
|
$this->setWhereAmI('home');
|
|
|
|
} elseif (!empty($this->slug) && ($filterURI=='/')) {
|
|
|
|
$this->setWhereAmI('page');
|
2017-09-20 17:15:49 +02:00
|
|
|
} elseif ($filterName=='admin') {
|
|
|
|
$this->slug = ltrim($this->slug, '/');
|
2015-03-27 02:00:01 +01:00
|
|
|
}
|
|
|
|
|
2017-09-15 20:02:53 +02:00
|
|
|
return true;
|
2015-03-27 02:00:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-15 20:02:53 +02:00
|
|
|
|
|
|
|
$this->setNotFound();
|
2017-09-21 20:42:03 +02:00
|
|
|
return false;
|
2015-03-08 18:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function slug()
|
|
|
|
{
|
|
|
|
return $this->slug;
|
|
|
|
}
|
|
|
|
|
2017-07-11 23:53:53 +02:00
|
|
|
public function setSlug($slug)
|
|
|
|
{
|
|
|
|
$this->slug = $slug;
|
|
|
|
}
|
|
|
|
|
2016-01-21 03:16:32 +01:00
|
|
|
public function activeFilter()
|
|
|
|
{
|
|
|
|
return $this->activeFilter;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-28 18:33:37 +02:00
|
|
|
// Return the filter filter by type
|
2015-09-08 02:51:48 +02:00
|
|
|
public function filters($type, $trim=true)
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
2015-09-08 02:51:48 +02:00
|
|
|
$filter = $this->filters[$type];
|
2018-07-28 18:33:37 +02:00
|
|
|
if ($trim) {
|
2015-09-08 02:51:48 +02:00
|
|
|
$filter = trim($filter, '/');
|
|
|
|
}
|
|
|
|
return $filter;
|
2015-03-08 18:02:59 +01:00
|
|
|
}
|
|
|
|
|
2017-05-09 00:24:15 +02:00
|
|
|
// Returns where is the user, home, pages, categories, tags..
|
2015-03-08 18:02:59 +01:00
|
|
|
public function whereAmI()
|
|
|
|
{
|
|
|
|
return $this->whereAmI;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setWhereAmI($where)
|
|
|
|
{
|
2017-05-21 22:01:44 +02:00
|
|
|
$GLOBALS['WHERE_AM_I'] = $where;
|
2015-03-08 18:02:59 +01:00
|
|
|
$this->whereAmI = $where;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function notFound()
|
|
|
|
{
|
|
|
|
return $this->notFound;
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:07:45 +02:00
|
|
|
public function pageNumber()
|
|
|
|
{
|
2018-03-02 16:37:17 +01:00
|
|
|
if (isset($this->parameters['page'])) {
|
2017-05-24 00:48:29 +02:00
|
|
|
return (int)$this->parameters['page'];
|
2015-05-15 00:07:45 +02:00
|
|
|
}
|
2017-05-24 00:48:29 +02:00
|
|
|
return 1;
|
2015-05-15 00:07:45 +02:00
|
|
|
}
|
|
|
|
|
2018-03-02 16:37:17 +01:00
|
|
|
public function parameter($field)
|
|
|
|
{
|
|
|
|
if (isset($this->parameters[$field])) {
|
|
|
|
return $this->parameters[$field];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-10 22:14:12 +01:00
|
|
|
public function setNotFound()
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
2017-09-21 20:42:03 +02:00
|
|
|
$this->setWhereAmI('page');
|
2017-12-10 22:14:12 +01:00
|
|
|
$this->notFound = true;
|
|
|
|
$this->httpCode = 404;
|
|
|
|
$this->httpMessage = 'Not Found';
|
2017-07-28 19:37:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function httpCode()
|
|
|
|
{
|
|
|
|
return $this->httpCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHttpCode($code = 200)
|
|
|
|
{
|
|
|
|
$this->httpCode = $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function httpMessage()
|
|
|
|
{
|
|
|
|
return $this->httpMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHttpMessage($msg = 'OK')
|
|
|
|
{
|
|
|
|
$this->httpMessage = $msg;
|
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
|
|
|
}
|
|
|
|
|
2017-09-20 16:51:54 +02:00
|
|
|
}
|