improve link building (QUERY_STRING and PATH_INFO are now allowed)
authorStricted <info@stricted.net>
Wed, 20 Jul 2016 06:54:18 +0000 (08:54 +0200)
committerStricted <info@stricted.net>
Wed, 20 Jul 2016 06:54:18 +0000 (08:54 +0200)
lib/system/DNS.class.php
lib/system/RequestHandler.class.php
lib/system/route/Request.class.php
lib/system/template/plugins/block.link.php
templates/default/header.tpl

index 794ead581e344be0441fb23c8d8161190bc62056..dcb641ea3d3696b7a42dfa71ec400bb82d4b8768 100644 (file)
@@ -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));*/
index 156e3df693ce2eaed7968b09151f65630919d73e..f4558943c4ffa3c60842496f66e1f370a07c0de9 100644 (file)
@@ -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;
+       }
 }
index 172100c3664c6a7589d11c48cd21047e1277ee2f..eeb4b827df55bc63f093b8f32365e3359fa95175 100644 (file)
@@ -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;
        }
 }
index a38f202e17e7ee1ae26bf376be9d3bf77dd230ff..a21c57c1229c8b69b17139c18d077dd5b6bf894f 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+use dns\system\RequestHandler;
 
 function smarty_block_link($params, $content, $template, &$repeat) {
        if ($repeat) {
@@ -17,24 +18,5 @@ function smarty_block_link($params, $content, $template, &$repeat) {
                $params['title'] = null;
        }
        
-       $url = 'index.php?'.$params['controller'];
-       
-       if (!empty($params['id'])) {
-               if (!empty($params['title'])) {
-                       $url .= '/'.$params['id'].'-'.$params['title'];
-               }
-               else {
-                       $url .= '/'.$params['id'];
-               }
-       }
-       
-       if (!empty($content)) {
-               if (strpos($content, '&') !== 0) {
-                       $url .= '&';
-               }
-               
-               $url .= $content;
-       }
-       
-       return @htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
+       return RequestHandler::getInstance()->getLink($params, $content);
 }
index fb61f6b735e9126ec14135001cb53930c08a377f..01b7f02cb79a89ad33e38e4bc2bb83e8c89fde93 100644 (file)
@@ -1,6 +1,7 @@
 <!DOCTYPE html>
 <html>
        <head>
+               <base href="{$baseurl}" />
                <meta charset="utf-8">
                <meta http-equiv="X-UA-Compatible" content="IE=edge">
                <meta name="viewport" content="width=device-width, initial-scale=1">