bludit/bl-kernel/url.class.php

165 lines
3.3 KiB
PHP
Raw Permalink 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-05-09 00:24:15 +02:00
$filterFull = ltrim($filterURI, '/');
$filterFull = HTML_PATH_ROOT.$filterFull;
$filterLenght = mb_strlen($filterFull, CHARSET);
2015-06-23 06:20:19 +02:00
if (mb_substr($this->uri, 0, $filterLenght, CHARSET)==$filterURI) {
$this->slug = mb_substr($this->uri, $filterLenght+1);
2015-11-30 01:45:30 +01:00
$this->whereAmI = $filterName;
2016-01-21 03:16:32 +01:00
$this->activeFilter = $filterURI;
2015-06-23 06:20:19 +02:00
if (empty($this->slug) && (($filterName=='blog') || ($filterURI=='/')) ) {
$this->whereAmI = 'home';
2015-03-27 02:00:01 +01:00
}
return true;
2015-03-27 02:00:01 +01:00
}
}
$this->setNotFound();
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-07-30 23:15:33 +02:00
$this->whereAmI = '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
}
2015-11-30 01:45:30 +01:00
}