bludit/bl-kernel/url.class.php

207 lines
4.1 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;
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-07-26 02:25:13 +02:00
// remove parameters GET, do not use parse_url because has problem with utf-8.
$explode = explode('?', $decode);
$this->uri = $explode[0];
2015-05-15 00:07:45 +02:00
$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;
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 = '';
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)
{
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-11-30 01:45:30 +01:00
// Sort filters by length
2015-03-27 02:00:01 +01:00
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;
$this->filters = $filters;
2015-03-08 18:02:59 +01:00
2015-11-30 01:45:30 +01:00
foreach($filters as $filterName=>$filterURI)
2015-03-27 02:00:01 +01:00
{
2015-11-30 01:45:30 +01:00
// $slug will be FALSE if the filter is not included in the URI.
$slug = $this->getSlugAfterFilter($filterURI);
2015-06-23 06:20:19 +02:00
2015-03-27 02:00:01 +01:00
if($slug!==false)
{
2015-11-30 01:45:30 +01:00
$this->slug = $slug;
$this->whereAmI = $filterName;
2016-01-21 03:16:32 +01:00
$this->activeFilter = $filterURI;
2015-06-23 06:20:19 +02:00
2015-11-30 01:45:30 +01:00
// If the slug is empty
if(Text::isEmpty($slug))
2015-03-27 02:00:01 +01:00
{
2015-11-30 01:45:30 +01:00
if($filterURI==='/')
2015-03-27 02:00:01 +01:00
{
$this->whereAmI = 'home';
break;
}
2016-01-08 21:12:17 +01:00
if($filterURI===$filters['blog'])
{
$this->whereAmI = 'blog';
break;
}
2015-11-30 01:45:30 +01:00
if($filterURI===$adminFilter['admin'])
2015-06-23 06:20:19 +02:00
{
$this->whereAmI = 'admin';
2015-06-27 06:28:13 +02:00
$this->slug = 'dashboard';
2015-06-23 06:20:19 +02:00
break;
}
2015-03-27 02:00:01 +01:00
$this->setNotFound(true);
}
break;
}
}
2015-03-08 18:02:59 +01:00
}
public function slug()
{
return $this->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
}
// 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-11-30 01:45:30 +01:00
// Returns the slug after the $filter, the slug could be an empty string
2015-06-23 06:20:19 +02:00
// If the filter is not included in the uri, returns FALSE
2015-03-08 18:02:59 +01:00
// ex: http://domain.com/cms/$filter/slug123 => slug123
2015-11-30 01:45:30 +01:00
// ex: http://domain.com/cms/$filter/name/lastname => name/lastname
// ex: http://domain.com/cms/$filter/ => empty string
// ex: http://domain.com/cms/$filter => empty string
2015-03-27 02:00:01 +01:00
private function getSlugAfterFilter($filter)
2015-03-08 18:02:59 +01:00
{
2015-11-30 01:45:30 +01:00
// Remove both slash from the filter
$filter = trim($filter, '/');
// Add to the filter the root directory
$filter = HTML_PATH_ROOT.$filter;
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-11-30 01:45:30 +01:00
$position = Text::stringPosition($this->uri, $filter);
// If the position is FALSE, the filter isn't in the URI.
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-11-30 01:45:30 +01:00
// Start position to cut
2015-05-31 03:06:55 +02:00
$start = $position + Text::length($filter);
2015-11-30 01:45:30 +01:00
// End position to cut
2015-03-27 02:00:01 +01:00
$end = $this->uriStrlen;
2015-03-08 18:02:59 +01:00
2015-11-30 01:45:30 +01:00
// Get the slug from the URI
2015-05-31 03:06:55 +02:00
$slug = Text::cut($this->uri, $start, $end);
2015-03-08 18:02:59 +01:00
2015-11-30 01:45:30 +01:00
if(Text::isEmpty($slug)) {
return '';
}
if($slug[0]=='/') {
return ltrim($slug, '/');
}
if($filter==HTML_PATH_ROOT) {
return $slug;
}
return false;
2015-03-27 02:00:01 +01:00
}
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
}