[TikiWiki-commits] [Git][tikiwiki/tiki][tiki-fix-tracker-url-wikisyntax] [ENH] Url.php: enhance URL validation and add wiki link handling; update...

"Sammy Ndabo \(@ndabosam084\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <698b9e1e35db9_3b5d5cc945417b@gitlab-sidekiq-low-urgency-cpu-bound-v2-6c7f7654c9-xzdzj.mail>

Sammy Ndabo pushed to branch tiki-fix-tracker-url-wikisyntax at Tiki Wiki CMS Groupware / Tiki


Commits:
a9cf9e12 by Sammy Ndabo at 2026-02-10T23:07:14+02:00
[ENH] Url.php: enhance URL validation and add wiki link handling; update url.tpl for wiki support syntax warnings

- - - - -


2 changed files:

- lib/core/Tracker/Field/Url.php
- templates/trackerinput/url.tpl


Changes:

=====================================
lib/core/Tracker/Field/Url.php
=====================================
@@ -83,7 +83,7 @@ class Tracker_Field_Url extends \Tracker\Field\AbstractItemField implements \Tra
             // Intentionally limited wiki-syntax detection for URL fields.
             // Supports only full-value wrappers like ((PageName)) or [url|text].
             // TODO: For full wiki parsing consistency (escaping, multilingual behavior, shared parsing path),
-            // consider refactoring URL to inherit Tracker_Field_Text in a separate, non-backport change.
+            // consider refactoring URL to inherit Tracker_Field_Text.
             return TikiLib::lib('parser')->parse_data_simple($url);
         } elseif ($this->getOption('linkToURL') == 2) { // Site title as link
             return smarty_function_object_link(
@@ -140,6 +140,25 @@ class Tracker_Field_Url extends \Tracker\Field\AbstractItemField implements \Tra
             return tr('Invalid wiki link syntax. Use complete wrappers like ((PageName)) or [url|text].');
         }
 
+        if ($trimmed === '') {
+            return true;
+        }
+
+        if (self::isWikiSyntaxLink($trimmed)) {
+            $resolvedHref = self::extractFirstHrefFromParsedWikiLink($trimmed);
+            if ($resolvedHref === null) {
+                return tr('Invalid wiki link syntax. The link target could not be resolved.');
+            }
+            if (! self::isSyntacticallyValidUrl($resolvedHref)) {
+                return tr('Invalid wiki link syntax. The resolved link target is not a valid URL.');
+            }
+            return true;
+        }
+
+        if (! self::isSyntacticallyValidUrl($trimmed)) {
+            return tr('Invalid URL syntax.');
+        }
+
         return true;
     }
 
@@ -161,7 +180,22 @@ class Tracker_Field_Url extends \Tracker\Field\AbstractItemField implements \Tra
 
     public function renderInput($context = [])
     {
-        return $this->renderTemplate("trackerinput/url.tpl", $context);
+        $value = trim((string) $this->getConfiguration('value'));
+        $templateData = [
+            'wikiSyntaxInfo' => tr('You can also use complete wiki-link syntax: ((PageName)) or [url|label].'),
+            'wikiSyntaxWarning' => '',
+        ];
+
+        if (self::isWikiSyntaxLink($value)) {
+            $target = self::extractWikiLinkTarget($value);
+            if ($target !== null && ! self::looksLikeExternalUrl($target)) {
+                if (! TikiLib::lib('tiki')->page_exists($target)) {
+                    $templateData['wikiSyntaxWarning'] = tr('Warning: Target wiki page "%0" does not exist yet.', $target);
+                }
+            }
+        }
+
+        return $this->renderTemplate("trackerinput/url.tpl", $context, $templateData);
     }
 
     public function importRemote($value)
@@ -197,4 +231,65 @@ class Tracker_Field_Url extends \Tracker\Field\AbstractItemField implements \Tra
 
         return $schema;
     }
+
+    protected static function extractFirstHrefFromParsedWikiLink(string $value): ?string
+    {
+        $parsed = TikiLib::lib('parser')->parse_data_simple($value);
+
+        if (! preg_match('/<a\b[^>]*\bhref=(["\'])(.*?)\1/i', $parsed, $matches)) {
+            return null;
+        }
+
+        return html_entity_decode($matches[2], ENT_QUOTES, 'UTF-8');
+    }
+
+    protected static function isSyntacticallyValidUrl(string $url): bool
+    {
+        if ($url === '') {
+            return true;
+        }
+
+        if (filter_var($url, FILTER_VALIDATE_URL)) {
+            return true;
+        }
+
+        if (str_starts_with($url, '/')) {
+            return ! preg_match('/\s/', $url);
+        }
+
+        $parsed = parse_url($url);
+        if ($parsed === false) {
+            return false;
+        }
+
+        if (isset($parsed['scheme'])) {
+            return (bool) preg_match('/^[a-z][a-z0-9+.-]*$/i', $parsed['scheme']) && ! preg_match('/\s/', $url);
+        }
+
+        return ! preg_match('/\s/', $url);
+    }
+
+    protected static function extractWikiLinkTarget(string $value): ?string
+    {
+        if (str_starts_with($value, '((') && str_ends_with($value, '))')) {
+            return trim(substr($value, 2, -2));
+        }
+
+        if (str_starts_with($value, '[') && str_ends_with($value, ']')) {
+            $inside = trim(substr($value, 1, -1));
+            if ($inside === '') {
+                return null;
+            }
+
+            $parts = explode('|', $inside, 2);
+            return trim($parts[0]);
+        }
+
+        return null;
+    }
+
+    protected static function looksLikeExternalUrl(string $value): bool
+    {
+        return (bool) preg_match('/^(https?:\/\/|ftp:\/\/|mailto:|news:)/i', $value);
+    }
 }


=====================================
templates/trackerinput/url.tpl
=====================================
@@ -9,3 +9,9 @@
         </span>
     {/if}
 </div>
+{if !empty($wikiSyntaxInfo)}
+    <div class="form-text text-muted">{$wikiSyntaxInfo|escape}</div>
+{/if}
+{if !empty($wikiSyntaxWarning)}
+    <div class="form-text text-warning">{$wikiSyntaxWarning|escape}</div>
+{/if}



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/a9cf9e12990bf2a95887bc0cfa45f5ee2855cb83

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/a9cf9e12990bf2a95887bc0cfa45f5ee2855cb83
You're receiving this email because of your account on gitlab.com.

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.