From 6238a43863210afe8988ad7cf251e6bfc6c8557c Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Mon, 12 Oct 2020 19:21:50 +0000
Subject: [PATCH] fix: escape generated feed tag values and remove new lines
 from public pages meta description

fixes #57, #46
---
 app/Database/Seeds/CategorySeeder.php | 38 +++++++++++++--------------
 app/Libraries/SimpleRSSElement.php    | 29 +++++++++++++++++---
 app/Views/episode.php                 |  4 +--
 app/Views/podcast.php                 |  4 +--
 4 files changed, 49 insertions(+), 26 deletions(-)

diff --git a/app/Database/Seeds/CategorySeeder.php b/app/Database/Seeds/CategorySeeder.php
index f1c33c136b..0fea4e51e1 100644
--- a/app/Database/Seeds/CategorySeeder.php
+++ b/app/Database/Seeds/CategorySeeder.php
@@ -65,13 +65,13 @@ class CategorySeeder extends Seeder
                 'id' => 6,
                 'code' => 'government',
                 'apple_category' => 'Government',
-                'google_category' => 'Government &amp; Organizations',
+                'google_category' => 'Government & Organizations',
             ],
             [
                 'parent_id' => 0,
                 'id' => 7,
                 'code' => 'health_and_fitness',
-                'apple_category' => 'Health &amp; Fitness',
+                'apple_category' => 'Health & Fitness',
                 'google_category' => 'Health',
             ],
             [
@@ -85,15 +85,15 @@ class CategorySeeder extends Seeder
                 'parent_id' => 0,
                 'id' => 9,
                 'code' => 'kids_and_family',
-                'apple_category' => 'Kids &amp; Family',
-                'google_category' => 'Kids &amp; Family',
+                'apple_category' => 'Kids & Family',
+                'google_category' => 'Kids & Family',
             ],
             [
                 'parent_id' => 0,
                 'id' => 10,
                 'code' => 'leisure',
                 'apple_category' => 'Leisure',
-                'google_category' => 'Games &amp; Hobbies',
+                'google_category' => 'Games & Hobbies',
             ],
             [
                 'parent_id' => 0,
@@ -107,35 +107,35 @@ class CategorySeeder extends Seeder
                 'id' => 12,
                 'code' => 'news',
                 'apple_category' => 'News',
-                'google_category' => 'News &amp; Politics',
+                'google_category' => 'News & Politics',
             ],
             [
                 'parent_id' => 0,
                 'id' => 13,
                 'code' => 'religion_and_spirituality',
-                'apple_category' => 'Religion &amp; Spirituality',
-                'google_category' => 'Religion &amp; Spirituality',
+                'apple_category' => 'Religion & Spirituality',
+                'google_category' => 'Religion & Spirituality',
             ],
             [
                 'parent_id' => 0,
                 'id' => 14,
                 'code' => 'science',
                 'apple_category' => 'Science',
-                'google_category' => 'Science &amp; Medicine',
+                'google_category' => 'Science & Medicine',
             ],
             [
                 'parent_id' => 0,
                 'id' => 15,
                 'code' => 'society_and_culture',
-                'apple_category' => 'Society &amp; Culture',
-                'google_category' => 'Society &amp; Culture',
+                'apple_category' => 'Society & Culture',
+                'google_category' => 'Society & Culture',
             ],
             [
                 'parent_id' => 0,
                 'id' => 16,
                 'code' => 'sports',
                 'apple_category' => 'Sports',
-                'google_category' => 'Sports &amp; Recreation',
+                'google_category' => 'Sports & Recreation',
             ],
             [
                 'parent_id' => 0,
@@ -155,8 +155,8 @@ class CategorySeeder extends Seeder
                 'parent_id' => 0,
                 'id' => 19,
                 'code' => 'tv_and_film',
-                'apple_category' => 'TV &amp; Film',
-                'google_category' => 'TV &amp; Film',
+                'apple_category' => 'TV & Film',
+                'google_category' => 'TV & Film',
             ],
             [
                 'parent_id' => 1,
@@ -176,7 +176,7 @@ class CategorySeeder extends Seeder
                 'parent_id' => 1,
                 'id' => 22,
                 'code' => 'fashion_and_beauty',
-                'apple_category' => 'Fashion &amp; Beauty',
+                'apple_category' => 'Fashion & Beauty',
                 'google_category' => '',
             ],
             [
@@ -372,7 +372,7 @@ class CategorySeeder extends Seeder
                 'parent_id' => 9,
                 'id' => 50,
                 'code' => 'pets_and_animals',
-                'apple_category' => 'Pets &amp; Animals',
+                'apple_category' => 'Pets & Animals',
                 'google_category' => '',
             ],
             [
@@ -386,7 +386,7 @@ class CategorySeeder extends Seeder
                 'parent_id' => 10,
                 'id' => 52,
                 'code' => 'animation_and_manga',
-                'apple_category' => 'Animation &amp; Manga',
+                'apple_category' => 'Animation & Manga',
                 'google_category' => '',
             ],
             [
@@ -428,7 +428,7 @@ class CategorySeeder extends Seeder
                 'parent_id' => 10,
                 'id' => 58,
                 'code' => 'home_and_garden',
-                'apple_category' => 'Home &amp; Garden',
+                'apple_category' => 'Home & Garden',
                 'google_category' => '',
             ],
             [
@@ -645,7 +645,7 @@ class CategorySeeder extends Seeder
                 'parent_id' => 15,
                 'id' => 89,
                 'code' => 'places_and_travel',
-                'apple_category' => 'Places &amp; Travel',
+                'apple_category' => 'Places & Travel',
                 'google_category' => '',
             ],
             [
diff --git a/app/Libraries/SimpleRSSElement.php b/app/Libraries/SimpleRSSElement.php
index 1d70a3944a..3aad896505 100644
--- a/app/Libraries/SimpleRSSElement.php
+++ b/app/Libraries/SimpleRSSElement.php
@@ -14,12 +14,14 @@ class SimpleRSSElement extends SimpleXMLElement
 {
     /**
      * Adds a child with $value inside CDATA
-     * @param unknown $name
-     * @param unknown $value
+     *
+     * @param string $name — The name of the child element to add.
+     * @param string $value — [optional] If specified, the value of the child element.
+     * @param string $namespace [optional] If specified, the namespace to which the child element belongs.
      */
     public function addChildWithCDATA($name, $value = null, $namespace = null)
     {
-        $new_child = $this->addChild($name, null, $namespace);
+        $new_child = parent::addChild($name, null, $namespace);
 
         if ($new_child !== null) {
             $node = dom_import_simplexml($new_child);
@@ -29,4 +31,25 @@ class SimpleRSSElement extends SimpleXMLElement
 
         return $new_child;
     }
+
+    /**
+     * Adds a child element to the XML node with escaped $value if specified.
+     * Override of addChild method as SimpleXMLElement's addChild method doesn't escape ampersand
+     *
+     * @param string $name — The name of the child element to add.
+     * @param string $value — [optional] If specified, the value of the child element.
+     * @param string $namespace [optional] If specified, the namespace to which the child element belongs.
+     */
+    public function addChild($name, $value = null, $namespace = null)
+    {
+        $new_child = parent::addChild($name, null, $namespace);
+
+        if ($new_child !== null) {
+            $node = dom_import_simplexml($new_child);
+            $no = $node->ownerDocument;
+            $node->appendChild($no->createTextNode(esc($value)));
+        }
+
+        return $new_child;
+    }
 }
diff --git a/app/Views/episode.php b/app/Views/episode.php
index c9a8a4afd1..c823519883 100644
--- a/app/Views/episode.php
+++ b/app/Views/episode.php
@@ -5,8 +5,8 @@
 <head>
     <meta charset="UTF-8"/>
     <title><?= $episode->title ?></title>
-    <meta name="description" content="<?= strip_tags(
-        $episode->description_html
+    <meta name="description" content="<?= trim(
+        preg_replace('/\s+/', ' ', strip_tags($episode->description_html))
     ) ?>"/>
     <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
     <link rel="shortcut icon" type="image/png" href="/favicon.ico" />
diff --git a/app/Views/podcast.php b/app/Views/podcast.php
index 5e271c58fd..f64a215783 100644
--- a/app/Views/podcast.php
+++ b/app/Views/podcast.php
@@ -6,8 +6,8 @@
 <head>
     <meta charset="UTF-8"/>
     <title><?= $podcast->title ?></title>
-    <meta name="description" content="<?= strip_tags(
-        $podcast->description_html
+    <meta name="description" content="<?= trim(
+        preg_replace('/\s+/', ' ', strip_tags($podcast->description_html))
     ) ?>"/>
     <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
     <link rel="shortcut icon" type="image/png" href="/favicon.ico" />
-- 
GitLab