bludit/bl-kernel/url.class.php

192 lines
4.2 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-05-15 00:07:45 +02:00
private $parameters;
2016-01-21 03:16:32 +01:00
private $activeFilter;
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 = '';
$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)
{
// 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'));
$this->filters = $adminFilter + $filters;
2015-03-08 18:02:59 +01: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/'
$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')) {
$this->setWhereAmI('home');
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
}
return true;
2015-03-27 02:00:01 +01: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;
}
// Return the filter used
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];
if($trim) {
$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)
{
$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()
{
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
}
public function setNotFound()
2015-03-08 18:02:59 +01:00
{
2017-09-21 20:42:03 +02:00
$this->setWhereAmI('page');
$this->notFound = true;
$this->httpCode = 404;
$this->httpMessage = 'Not Found';
}
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
}