From 4e9fe6048856518497d62e1fce98e520cd9c5b03 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Tue, 3 May 2022 17:19:36 +0000
Subject: [PATCH] do not decode passed links in SkipLinks::addLink(), fixes
 #951

Closes #951

Merge request studip/studip!545
---
 lib/classes/SkipLinks.php  | 51 ++++++++------------------------------
 templates/layouts/base.php |  2 +-
 2 files changed, 12 insertions(+), 41 deletions(-)

diff --git a/lib/classes/SkipLinks.php b/lib/classes/SkipLinks.php
index b906ed5955d..c5738b2af6f 100644
--- a/lib/classes/SkipLinks.php
+++ b/lib/classes/SkipLinks.php
@@ -25,12 +25,6 @@ class SkipLinks
      */
     private static $links = [];
 
-    /**
-     * array of positions of skip links
-     * @var array
-     */
-    private static $position = [];
-
     /**
      * Returns whether skiplinks are enabled.
      *
@@ -59,34 +53,28 @@ class SkipLinks
      * @param string $name the displayed name of the links
      * @param string $url the url of the links
      * @param integer $position the position of the link in the list
-     * @param boolean $overwriteable false if position is not overwritable by another link
      */
-    public static function addLink($name, $url, $position = null, $overwriteable = false)
+    public static function addLink(string $name, string $url, $position = null)
     {
         $position = (!$position || $position < 1) ? count(self::$links) + 100 : (int) $position;
-        $new_link = [
-            'name'          => $name,
-            'url'           => decodeHTML($url),
-            'position'      => $position,
-            'overwriteable' => $overwriteable,
+        self::$links[$url] = [
+            'name'     => $name,
+            'url'      => $url,
+            'position' => $position,
         ];
-        if (self::checkOverwrite($new_link)) {
-            self::$links[$new_link['url']] = $new_link;
-        }
     }
 
     /**
      * Adds a link to an anker on the same page to the list of skip links.
      *
-     * @param string $name the displayed name of the links
-     * @param string $id the id of the anker
+     * @param string  $name     the displayed name of the links
+     * @param string  $id       the id of the anker
      * @param integer $position the position of the link in the list
-     * @param boolean $overwriteable false if position is not overwritable by another link
      */
-    public static function addIndex($name, $id, $position = null, $overwriteable = false)
+    public static function addIndex($name, $id, $position = null)
     {
         $url = '#' . $id;
-        self::addLink($name, $url, $position, $overwriteable);
+        self::addLink($name, $url, $position);
     }
 
     /**
@@ -101,11 +89,11 @@ class SkipLinks
         }
 
         usort(self::$links, function ($a, $b) {
-            return $a['position'] > $b['position'];
+            return $a['position'] - $b['position'];
         });
 
         $navigation = new Navigation('');
-        foreach (self::$links as $index => $link) {
+        foreach (array_values(self::$links) as $index => $link) {
             $navigation->addSubNavigation(
                 "/skiplinks/link-{$index}",
                 new Navigation($link['name'], $link['url'])
@@ -113,21 +101,4 @@ class SkipLinks
         }
         return $GLOBALS['template_factory']->render('skiplinks', compact('navigation'));
     }
-
-    /**
-     * Checks if there is another link at the same position and if it is overwritable.
-     *
-     * @return boolean true if the link at the same position is overwritable
-     */
-    private static function checkOverwrite($link)
-    {
-        if (isset(self::$position[$link['position']])) {
-            return false;
-        }
-        if (!$link['overwrite']) {
-            self::$position[$link['position']] = true;
-        }
-        return true;
-    }
-
 }
diff --git a/templates/layouts/base.php b/templates/layouts/base.php
index 77b7da84658..41e525529f3 100644
--- a/templates/layouts/base.php
+++ b/templates/layouts/base.php
@@ -97,7 +97,7 @@ $getInstalledLanguages = function () {
 <body id="<?= $body_id ?: PageLayout::getBodyElementId() ?>" <? if (SkipLinks::isEnabled()) echo 'class="enable-skiplinks"'; ?>>
 <div id="layout_wrapper">
     <? SkipLinks::insertContainer() ?>
-    <? SkipLinks::addIndex(_('Hauptinhalt'), 'layout_content', 100, true) ?>
+    <? SkipLinks::addIndex(_('Hauptinhalt'), 'layout_content', 100) ?>
     <?= PageLayout::getBodyElements() ?>
 
     <? include 'lib/include/header.php' ?>
-- 
GitLab