New method to generate attributes for <a> tags
authorMarcel Werk <burntime@woltlab.com>
Thu, 6 Aug 2020 20:36:49 +0000 (22:36 +0200)
committerMarcel Werk <burntime@woltlab.com>
Thu, 6 Aug 2020 20:36:49 +0000 (22:36 +0200)
com.woltlab.wcf/templates/quoteMetaCode.tpl
com.woltlab.wcf/templates/userInformationButtons.tpl
com.woltlab.wcf/templates/usersOnlineList.tpl
wcfsetup/install/files/lib/system/template/plugin/AnchorAttributesFunctionTemplatePlugin.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/template/plugin/IpSearchModifierTemplatePlugin.class.php
wcfsetup/install/files/lib/util/StringUtil.class.php

index 1cdd2a6fe46fb91f247160801f6b83fcda2878e4..ebbfe33476b64739fec0db18465e701aae7f7d70 100644 (file)
@@ -11,7 +11,7 @@
                <span class="quoteBoxTitle">
                        {if $quoteAuthor}
                                {if $quoteLink}
-                                       <a href="{@$quoteLink}"{if $isExternalQuoteLink} class="externalURL"{if EXTERNAL_LINK_REL_NOFOLLOW || EXTERNAL_LINK_TARGET_BLANK}rel="{if EXTERNAL_LINK_REL_NOFOLLOW}nofollow{/if} {if EXTERNAL_LINK_TARGET_BLANK}noopener noreferrer{/if}"{/if}{if EXTERNAL_LINK_TARGET_BLANK} target="_blank"{/if}{/if}>{lang}wcf.bbcode.quote.title{/lang}</a>
+                                       <a {anchorAttributes url=$quoteLink}>{lang}wcf.bbcode.quote.title{/lang}</a>
                                {else}
                                        {lang}wcf.bbcode.quote.title{/lang}
                                {/if}
index 7c0eb233dd8e371ef0a663e13f324acc92ef6e57..bd9ed1fcb2fd6c1f97a9f937905a6251b7933392 100644 (file)
@@ -3,7 +3,7 @@
                <ul class="buttonList iconList">
                        {content}
                                {if $user->homepage && $user->homepage != 'http://'}
-                                       <li><a class="jsTooltip" href="{$user->homepage}" title="{lang}wcf.user.option.homepage{/lang}"{if EXTERNAL_LINK_REL_NOFOLLOW || EXTERNAL_LINK_TARGET_BLANK} rel="{if EXTERNAL_LINK_REL_NOFOLLOW}nofollow{/if}{if EXTERNAL_LINK_TARGET_BLANK}{if EXTERNAL_LINK_REL_NOFOLLOW} {/if}noopener noreferrer{/if}"{/if}{if EXTERNAL_LINK_TARGET_BLANK} target="_blank"{/if}><span class="icon icon16 fa-home"></span> <span class="invisible">{lang}wcf.user.option.homepage{/lang}</span></a></li>
+                                       <li><a class="jsTooltip" title="{lang}wcf.user.option.homepage{/lang}" {anchorAttributes url=$user->homepage appendClassname=false}><span class="icon icon16 fa-home"></span> <span class="invisible">{lang}wcf.user.option.homepage{/lang}</span></a></li>
                                {/if}
                                
                                {if $user->userID != $__wcf->user->userID}
index 6885dcac10612fc139066b2f6e19b6b77517b1f1..c4a3a5463c76f53628110f444b1d534d40a9cd93 100644 (file)
                                        
                                        <div class="details userInformation">
                                                <div class="containerHeadline">
-                                                       <h3>{if $user->getSpider()->spiderURL}<a href="{$user->getSpider()->spiderURL}" class="externalURL"{if EXTERNAL_LINK_REL_NOFOLLOW || EXTERNAL_LINK_TARGET_BLANK} rel="{if EXTERNAL_LINK_REL_NOFOLLOW}nofollow{/if}{if EXTERNAL_LINK_TARGET_BLANK}{if EXTERNAL_LINK_REL_NOFOLLOW} {/if}noopener noreferrer{/if}"{/if}{if EXTERNAL_LINK_TARGET_BLANK} target="_blank"{/if}>{$user->getSpider()->spiderName}</a>{else}{$user->getSpider()->spiderName}{/if}</h3>
+                                                       <h3>{if $user->getSpider()->spiderURL}<a {anchorAttributes url=$user->getSpider()->spiderURL}>{$user->getSpider()->spiderName}</a>{else}{$user->getSpider()->spiderName}{/if}</h3>
                                                        {@$locationData}
                                                </div>
                                                
diff --git a/wcfsetup/install/files/lib/system/template/plugin/AnchorAttributesFunctionTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/AnchorAttributesFunctionTemplatePlugin.class.php
new file mode 100644 (file)
index 0000000..bd7f21a
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+namespace wcf\system\template\plugin;
+use wcf\system\application\ApplicationHandler;
+use wcf\system\request\RouteHandler;
+use wcf\system\template\TemplateEngine;
+use wcf\util\StringUtil;
+
+/**
+ * Template function plugin which generates attributes for `a` HTML elements.
+ *
+ * Required parameter:
+ *     `url` (string)
+ * Optional parameter:
+ *      `appendHref` (bool, default true)
+ *     `appendClassname` (bool, default true)
+ *
+ * Usage:
+ *     {anchorAttributes url=$url}
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2020 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Template\Plugin
+ * @since      5.3
+ */
+class AnchorAttributesFunctionTemplatePlugin implements IFunctionTemplatePlugin {
+       /**
+        * @inheritDoc
+        */
+       public function execute($tagArgs, TemplateEngine $tplObj) {
+               if (empty($tagArgs['url'])) {
+                       throw new \InvalidArgumentException("Missing 'url' attribute.");
+               }
+               $url = $tagArgs['url'];
+               $appendClassname = $tagArgs['appendClassname'] ?? true;
+               $appendHref = $tagArgs['appendHref'] ?? true;
+               
+               $external = true;
+               if (ApplicationHandler::getInstance()->isInternalURL($url)) {
+                       $external = false;
+                       $url = preg_replace('~^https?://~', RouteHandler::getProtocol(), $url);
+               }
+               
+               $attributes = '';
+               if ($appendHref) {
+                       $attributes = 'href="' . StringUtil::encodeHTML($url) . '"';
+               }
+               
+               if ($external) {
+                       if ($appendClassname) {
+                               $attributes .= ' class="externalURL"';
+                       }
+                       
+                       $rel = 'nofollow';
+                       if (EXTERNAL_LINK_TARGET_BLANK) {
+                               $rel .= ' noopener noreferrer';
+                               $attributes .= 'target="_blank"';
+                       }
+                       
+                       $attributes .= ' rel="' . $rel . '"';
+               }
+               
+               return StringUtil::trim($attributes);
+       }
+}
index 78c8588d801ff77ee822b371ba73d24a8304ee20..befeb33857799004a584686b8286c7043cb6e3dc 100644 (file)
@@ -35,6 +35,6 @@ class IpSearchModifierTemplatePlugin implements IModifierTemplatePlugin {
                $url = sprintf(IP_ADDRESS_SEARCH_ENGINE ?: self::SEARCH_ENGINE_URL_DEFAULT, $ipAddress);
                $title = WCF::getLanguage()->getDynamicVariable('wcf.user.ipAddress.searchEngine', ['host' => $domain, 'ipAddress' => $ipAddress]);
                
-               return '<a href="' . $url . '"' . (EXTERNAL_LINK_REL_NOFOLLOW ? ' rel="nofollow"' : '') .(EXTERNAL_LINK_TARGET_BLANK ? ' target="_blank"' : '') . ' title="' . $title . '">' . $ipAddress . '</a>';
+               return '<a '. StringUtil::getAnchorTagAttributes($url) .' title="' . $title . '">' . $ipAddress . '</a>';
        }
 }
index d1c047e59ec7af52b2b77cb01a941d84b567bb14..d3708a0d4b6b5cbccec32357f0b01eea20abbc14 100644 (file)
@@ -672,12 +672,6 @@ final class StringUtil {
        public static function getAnchorTag($url, $title = '', $encodeTitle = true) {
                $url = self::trim($url);
                
-               $external = true;
-               if (ApplicationHandler::getInstance()->isInternalURL($url)) {
-                       $external = false;
-                       $url = preg_replace('~^https?://~', RouteHandler::getProtocol(), $url);
-               }
-               
                // cut visible url
                if (empty($title)) {
                        // use URL and remove protocol and www subdomain
@@ -690,7 +684,36 @@ final class StringUtil {
                        if (!$encodeTitle) $title = self::encodeHTML($title);
                }
                
-               return '<a href="'.self::encodeHTML($url).'"'.($external ? (' class="externalURL"'.((EXTERNAL_LINK_REL_NOFOLLOW || EXTERNAL_LINK_TARGET_BLANK) ? (' rel="'.(EXTERNAL_LINK_REL_NOFOLLOW ? 'nofollow' : '').((EXTERNAL_LINK_REL_NOFOLLOW && EXTERNAL_LINK_TARGET_BLANK) ? ' ' : '').(EXTERNAL_LINK_TARGET_BLANK ? 'noopener noreferrer' : '').'"') : '').(EXTERNAL_LINK_TARGET_BLANK ? ' target="_blank"' : '')) : '').'>'.($encodeTitle ? self::encodeHTML($title) : $title).'</a>';
+               return '<a '. self::getAnchorTagAttributes($url) .'>' . ($encodeTitle ? self::encodeHTML($title) : $title) . '</a>';
+       }
+       
+       /**
+        * Generates the attributes for an anchor tag from given URL.
+        *
+        * @param       string          $url
+        * @return      string          attributes
+        * @since       5.3
+        */
+       public static function getAnchorTagAttributes($url) {
+               $external = true;
+               if (ApplicationHandler::getInstance()->isInternalURL($url)) {
+                       $external = false;
+                       $url = preg_replace('~^https?://~', RouteHandler::getProtocol(), $url);
+               }
+               
+               $attributes = 'href="' . self::encodeHTML($url) . '"';
+               if ($external) {
+                       $attributes .= ' class="externalURL"';
+                       $rel = 'nofollow';
+                       if (EXTERNAL_LINK_TARGET_BLANK) {
+                               $rel .= ' noopener noreferrer';
+                               $attributes .= 'target="_blank"';
+                       }
+                       
+                       $attributes .= ' rel="' . $rel . '"';
+               }
+               
+               return $attributes;
        }
        
        /**