bludit/kernel/url.class.php

187 lines
3.6 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;
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();
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']);
2015-05-05 03:00:01 +02:00
2015-03-27 02:00:01 +01:00
// 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)
{
2015-06-23 06:20:19 +02:00
// getSlugAfterFilter() set the variable $this->slug
2015-03-27 02:00:01 +01:00
$slug = $this->getSlugAfterFilter($filter);
2015-06-23 06:20:19 +02:00
// If the filter is included in the URI.
2015-03-27 02:00:01 +01:00
if($slug!==false)
{
2015-06-23 06:20:19 +02:00
// Where Am I is the filter now, because is in the URI.
$this->whereAmI = $filterKey;
// If the slug empty
2015-03-27 02:00:01 +01:00
if(empty($slug))
{
if($filter==='/')
{
$this->whereAmI = 'home';
break;
}
2015-06-23 06:20:19 +02:00
if($filter===$adminFilter['admin'])
{
$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;
}
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-05-05 03:00:01 +02:00
public function getDomain()
2015-03-08 18:02:59 +01:00
{
2015-05-05 03:00:01 +02:00
if(!empty($_SERVER['HTTPS'])) {
$protocol = 'https://';
}
else {
$protocol = 'http://';
}
2015-03-27 02:00:01 +01:00
2015-05-05 03:00:01 +02:00
$domain = $_SERVER['HTTP_HOST'];
2015-03-27 02:00:01 +01:00
2015-05-05 03:00:01 +02:00
return $protocol.$domain.HTML_PATH_ROOT;
2015-03-27 02:00:01 +01:00
}
2015-03-08 18:02:59 +01:00
// Return the slug after the $filter
2015-06-23 06:20:19 +02:00
// If the filter is not included in the uri, returns FALSE
// If the filter is included 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-05-05 03:00:01 +02:00
if($filter=='/') {
2015-03-14 15:45:30 +01:00
$filter = HTML_PATH_ROOT;
2015-05-05 03:00:01 +02:00
}
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-05-31 03:06:55 +02:00
$position = Text::strpos($this->uri, $filter);
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-05-31 03:06:55 +02:00
$start = $position + Text::length($filter);
2015-03-27 02:00:01 +01:00
$end = $this->uriStrlen;
2015-03-08 18:02:59 +01:00
2015-05-31 03:06:55 +02:00
$slug = Text::cut($this->uri, $start, $end);
2015-05-05 03:00:01 +02:00
$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
}
2015-10-02 06:30:16 +02:00
}