From cff58c55b16237085f3ffd25b30c2fd49f384efa Mon Sep 17 00:00:00 2001 From: Stricted Date: Wed, 20 Jul 2016 08:54:18 +0200 Subject: [PATCH] improve link building (QUERY_STRING and PATH_INFO are now allowed) --- lib/system/DNS.class.php | 3 +- lib/system/RequestHandler.class.php | 63 +++++++++++++++++++++- lib/system/route/Request.class.php | 16 +++--- lib/system/template/plugins/block.link.php | 22 +------- templates/default/header.tpl | 1 + 5 files changed, 77 insertions(+), 28 deletions(-) diff --git a/lib/system/DNS.class.php b/lib/system/DNS.class.php index 794ead5..dcb641e 100644 --- a/lib/system/DNS.class.php +++ b/lib/system/DNS.class.php @@ -363,7 +363,8 @@ class DNS { /* assign language variables */ self::getTPL()->assign(array( "isReseller" => User::isReseller(), - "isAdmin" => User::isAdmin() + "isAdmin" => User::isAdmin(), + 'baseurl' => RequestHandler::getInstance()->getBaseUrl() )); /*self::getTPL()->assign("version", mb_substr(sha1(DNS_VERSION), 0, 8));*/ diff --git a/lib/system/RequestHandler.class.php b/lib/system/RequestHandler.class.php index 156e3df..f455894 100644 --- a/lib/system/RequestHandler.class.php +++ b/lib/system/RequestHandler.class.php @@ -44,7 +44,7 @@ class RequestHandler extends SingletonFactory { $routes = []; foreach ($controllers as $name => $data) { - $routes[$name] = Segment::factory([ 'route' => $name.'[/][/:id[-:title]]', 'constraints' => [ 'id' => '[0-9]+', 'title' => '[a-zA-Z0-9_.-/]+' ], 'defaults' => [ 'controller' => $data ] ]); + $routes[$name] = Segment::factory([ 'route' => '[/]' . $name . '[/][/:id[-:title]]', 'constraints' => [ 'id' => '[0-9]+', 'title' => '[a-zA-Z0-9_.-/]+' ], 'defaults' => [ 'controller' => $data ] ]); } $this->router->setRoutes($routes); @@ -142,4 +142,65 @@ class RequestHandler extends SingletonFactory { $_REQUEST[$key] = $value; } } + + public function getBaseUrl () { + $protocol = 'http://'; + + if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443 || (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) { + $protocol = 'https://'; + } + + return $protocol . $_SERVER['HTTP_HOST'] . '/'; + } + + public function getLink (array $params = [], $query = '') { + $path_info = true; // TODO: add config constant for that + + $url = $this->getBaseUrl() . 'index.php'; + if ($path_info) { + $url .= '/'; + } + else { + $url .= '?'; + } + + if (!empty($params['controller'])) { + $url .= $params['controller']; + } + else { + // TODO: InvalidArgumentException? + } + + if (!empty($params['id'])) { + if (!empty($params['title'])) { + $url .= '/'.$params['id'].'-'.$params['title']; + } + else { + $url .= '/'.$params['id']; + } + } + + if (!empty($query)) { + if ($path_info) { + if (strpos($query, '&') === 0) { + $query = '/' . substr($query, 1); + } + else if (strpos($query, '/') !== 0) { + $query = '/' . $query; + } + } + else { + if (strpos($query, '/') === 0) { + $query = '&' . substr($query, 1); + } + else if (strpos($query, '&') !== 0) { + $query = '&' . $query; + } + } + + $url .= $query; + } + + return $url; + } } diff --git a/lib/system/route/Request.class.php b/lib/system/route/Request.class.php index 172100c..eeb4b82 100644 --- a/lib/system/route/Request.class.php +++ b/lib/system/route/Request.class.php @@ -4,13 +4,17 @@ use Zend\Stdlib\Request as BaseRequest; class Request extends BaseRequest { public function getPath() { - $queryString = $_SERVER['QUERY_STRING']; - - if (strpos($queryString, '&') !== false) { - $pos = strpos($queryString, '&'); - $queryString = substr($queryString, 0, $pos); + if (isset($_SERVER['PATH_INFO']) && !empty($_SERVER['PATH_INFO'])) { + $queryString = $_SERVER['PATH_INFO']; + } + else { + $queryString = $_SERVER['QUERY_STRING']; + + if (strpos($queryString, '&') !== false) { + $pos = strpos($queryString, '&'); + $queryString = substr($queryString, 0, $pos); + } } - return $queryString; } } diff --git a/lib/system/template/plugins/block.link.php b/lib/system/template/plugins/block.link.php index a38f202..a21c57c 100644 --- a/lib/system/template/plugins/block.link.php +++ b/lib/system/template/plugins/block.link.php @@ -1,4 +1,5 @@ getLink($params, $content); } diff --git a/templates/default/header.tpl b/templates/default/header.tpl index fb61f6b..01b7f02 100644 --- a/templates/default/header.tpl +++ b/templates/default/header.tpl @@ -1,6 +1,7 @@ + -- 2.20.1