[PHP-WEBMASTER] [web-news] master: Canonicalises feed base URL handling (#41)

Author: NickSdot (NickSdot)
Committer: GitHub (web-flow)
Pusher: derickr
Date: 2026-08-10T16:21:05+01:00

Commit: Canonicalises feed base URL handling (#41) · php/web-news@3242085 · GitHub
Raw diff: <?php echo $host; ?>: <?php echo $group?>

Canonicalises feed base URL handling (#41)

* Canonicalise feed base URL handling

* review: standardise to news-web.php.net

Changed paths:
  M README.md
  M group.php
  M index.php
  M lib/config.php

Diff:

diff --git a/README.md b/README.md
index 70262b3..6ac1429 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ for local development.

 git clone https://github.com/php/web-news.git
 cd web-news/
-NNTP_HOST=news.php.net php -S localhost:8080 .router.php
+NNTP_HOST=news-web.php.net php -S localhost:8080 .router.php

-----
diff --git a/group.php b/group.php
index 4884b1c..005941e 100644
--- a/group.php
+++ b/group.php
@@ -29,15 +29,18 @@
     error($e->getMessage());
}

-$host = htmlspecialchars($_SERVER['HTTP_HOST'], ENT_QUOTES, "UTF-8");
+$cleanBaseUrl = clean($NEWS_WEB_BASE_URL);
+$baseUrlParts = parse_url($NEWS_WEB_BASE_URL);
+$cleanBaseHost = clean($baseUrlParts['host'] . (isset($baseUrlParts['port']) ? ':' . $baseUrlParts['port'] : ''));
+$cleanGroupUrl = urlencode($group);
switch ($format) {
     case 'rss':
         header("Content-type: text/xml");
         echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";?>
<rss version="2.0">
  <channel>
- <title><?php echo $host; ?>: <?php echo $group?></title>
- <link>http://<?php echo $host; ?>/group.php?group=<?php echo $group?></link>
+ <title><?php echo $cleanBaseHost; ?>: <?php echo $group?></title>
+ <link><?php echo $cleanBaseUrl; ?>/group.php?group=<?php echo $cleanGroupUrl?></link>
   <description></description>
         <?php
         break;
@@ -49,8 +52,8 @@
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;
         xmlns="http://my.netscape.com/rdf/simple/0.9/&quot;&gt;
  <channel>
- <title><?php echo $host; ?>: <?php echo $group?></title>
- <link>http://<?php echo $host; ?>/group.php?group=<?php echo $group?></link>
+ <title><?php echo $cleanBaseHost; ?>: <?php echo $group?></title>
+ <link><?php echo $cleanBaseUrl; ?>/group.php?group=<?php echo $cleanGroupUrl?></link>
   <description><?php echo $group?> Newsgroup at <?php echo $NNTP_HOST; ?></description>
   <language>en-US</language>
  </channel>
@@ -146,11 +149,13 @@
foreach ($overview['articles'] as $articleNumber => $details) {
     /* $date = date("H:i:s M/d/y", strtotime($odate)); */
     $date822 = date("r", strtotime($details['date']));
+ $cleanArticlePath = "/$cleanGroupUrl/" . urlencode((string) $articleNumber);
+ $cleanArticleLink = "$cleanBaseUrl$cleanArticlePath";

     switch ($format) {
         case 'rss':
             echo " <item>\n";
- echo " <link>http://$host/$group/$articleNumber&lt;/link&gt;\\n&quot;;
+ echo " <link>$cleanArticleLink</link>\n";
             echo " <title>", format_subject($details['subject'], $charset), "</title>\n";
             echo " <description>",
                 htmlspecialchars(format_author($details['author'], $charset), ENT_QUOTES, "UTF-8"),
@@ -161,7 +166,7 @@
         case 'rdf':
             echo " <item>\n";
             echo " <title>", format_subject($details['subject'], $charset), "</title>\n";
- echo " <link>http://$host/$group/$articleNumber&lt;/link&gt;\\n&quot;;
+ echo " <link>$cleanArticleLink</link>\n";
             echo " <description>",
                 htmlspecialchars(format_author($details['author'], $charset), ENT_QUOTES, "UTF-8"),
                 "</description>\n";
diff --git a/index.php b/index.php
index 1c9d1e3..20bd3e2 100644
--- a/index.php
+++ b/index.php
@@ -17,7 +17,7 @@

head();

-$DISPLAY_NNTP_HOST = htmlspecialchars(($NNTP_HOST == 'localhost') ? 'news.php.net' : $NNTP_HOST);
+$DISPLAY_NNTP_HOST = htmlspecialchars(($NNTP_HOST == 'localhost') ? 'news-web.php.net' : $NNTP_HOST);
?>

<nav class="secondary-nav">
diff --git a/lib/config.php b/lib/config.php
index 71a3835..4a4c523 100644
--- a/lib/config.php
+++ b/lib/config.php
@@ -4,3 +4,10 @@
if (getenv('NNTP_HOST')) {
     $NNTP_HOST = getenv('NNTP_HOST');
}
+
+$NEWS_WEB_BASE_URL = 'https://news-web.php.net';
+if (getenv('NEWS_WEB_BASE_URL')) {
+ $NEWS_WEB_BASE_URL = rtrim(getenv('NEWS_WEB_BASE_URL'), '/');
+} elseif (PHP_SAPI == 'cli-server') {
+ $NEWS_WEB_BASE_URL = 'http://' . $_SERVER['HTTP_HOST'];
+}