From d550adcdcdece7beba20d08cc634736d8e888ebb Mon Sep 17 00:00:00 2001
From: Markus Birth <mbirth@gmail.com>
Date: Sat, 12 Feb 2022 15:50:22 +0100
Subject: [PATCH] Moved genpage.inc.php into PageGenerator class.

---
 include/genpage.inc.php           | 191 +---------------------------
 lib/Serendipity/PageGenerator.php | 198 ++++++++++++++++++++++++++++++
 lib/Serendipity/Routing.php       |  61 +++++----
 3 files changed, 232 insertions(+), 218 deletions(-)
 create mode 100644 lib/Serendipity/PageGenerator.php

diff --git a/include/genpage.inc.php b/include/genpage.inc.php
index 398f440d..c489bfa5 100644
--- a/include/genpage.inc.php
+++ b/include/genpage.inc.php
@@ -1,189 +1,10 @@
 <?php
-# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
-# All rights reserved.  See LICENSE file for licensing details
 
-if (IN_serendipity !== true) {
-    die ("Don't hack!");
-}
+// Serendipity
+// See LICENSE file for license information.
+// TODO: THIS FILE IS FOR BACKWARDS COMPATIBILITY - REMOVE WHEN NO LONGER NEEDED
 
-include_once('serendipity_config.inc.php');
+use Serendipity\PageGenerator;
 
-include_once(S9Y_INCLUDE_PATH . 'include/plugin_api.inc.php');
-
-$uri = $_SERVER['REQUEST_URI'];  // need to define this again here, as index.php (2.1) no longer includes this file
-$uri_addData = array(
-    'startpage' => false,
-    'uriargs'   => implode('/', serendipity_getUriArguments($uri, true)),
-    'view'      => $serendipity['view'],
-    'viewtype'  => isset($serendipity['viewtype']) ? $serendipity['viewtype'] : ''
-);
-
-if ((empty($uri_addData['uriargs']) || trim($uri_addData['uriargs']) == $serendipity['indexFile']) && empty($serendipity['GET']['subpage'])) {
-    $uri_addData['startpage'] = true;
-}
-
-$serendipity['plugindata']['smartyvars'] = $uri_addData; // Plugins can change this global variable
-serendipity_plugin_api::hook_event('genpage', $uri, $uri_addData);
-serendipity_smarty_init();
-if (count($serendipity['plugindata']['smartyvars']) > 0) {
-    $serendipity['smarty']->assign($serendipity['plugindata']['smartyvars']);
-}
-
-$leftSidebarElements  = serendipity_plugin_api::count_plugins('left');
-$rightSidebarElements = serendipity_plugin_api::count_plugins('right');
-$serendipity['smarty']->assignByRef('leftSidebarElements', $leftSidebarElements);
-$serendipity['smarty']->assignByRef('rightSidebarElements', $rightSidebarElements);
-
-switch ($serendipity['GET']['action']) {
-    // User wants to read the diary
-    case 'read':
-        if (isset($serendipity['GET']['id'])) {
-            $entry = array(serendipity_fetchEntry('id', $serendipity['GET']['id']));
-            if (!is_array($entry) || count($entry) < 1 || !is_array($entry[0])) {
-                unset($serendipity['GET']['id']);
-                $entry = array(array());
-                $serendipity['head_subtitle'] = '';
-                $serendipity['smarty']->assign('head_subtitle', $serendipity['head_subtitle']);
-                $serendipity['view'] = '404';
-                $serendipity['content_message'] = URL_NOT_FOUND;
-                serendipity_header('HTTP/1.0 404 Not found');
-                serendipity_header('Status: 404 Not found');
-            }
-            
-            serendipity_printEntries($entry, 1);
-        } else {
-            serendipity_printEntries(serendipity_fetchEntries($serendipity['range'] ?? null, true, $serendipity['fetchLimit']));
-        }
-        break;
-
-    // User searches
-    case 'search':
-        $r = serendipity_searchEntries($serendipity['GET']['searchTerm']);
-        if (strlen($serendipity['GET']['searchTerm']) <= 3) {
-            $serendipity['smarty']->assign(
-                array(
-                    'content_message'       => SEARCH_TOO_SHORT,
-                    'searchresult_tooShort' => true
-                )
-            );
-            break;
-        }
-
-        if (is_string($r) && $r !== true) {
-            $serendipity['smarty']->assign(
-                array(
-                    'content_message'    => sprintf(SEARCH_ERROR, $serendipity['dbPrefix'], $r),
-                    'searchresult_error' => true
-                )
-            );
-            break;
-        } elseif ($r === true) {
-            $serendipity['smarty']->assign(
-                array(
-                    'content_message'        => sprintf(NO_ENTRIES_BLAHBLAH, '<span class="searchterm">' . $serendipity['GET']['searchTerm'] . '</span>'),
-                    'searchresult_noEntries' => true
-                )
-            );
-            break;
-        }
-
-        $serendipity['smarty']->assign(
-            array(
-                'content_message'      => sprintf(YOUR_SEARCH_RETURNED_BLAHBLAH, '<span class="searchterm">' . $serendipity['GET']['searchTerm'] . '</span>', '<span class="searchresults">' . serendipity_getTotalEntries() . '</span>'),
-                'searchresult_results' => true,
-                'searchresult_fullentry' => $serendipity['GET']['fullentry'] ?? null
-            )
-        );
-
-        serendipity_printEntries($r);
-        break;
-
-    // Show the comments
-    case 'comments':
-        serendipity_printCommentsByAuthor();
-        // use 'content_message' for pagination?
-        break;
-
-    // Show the archive
-    case 'archives':
-        $serendipity['head_subtitle'] = ARCHIVES;
-        $serendipity['smarty']->assign('head_subtitle', $serendipity['head_subtitle']);
-        serendipity_printArchives();
-        break;
-
-
-    case 'custom':
-        if ($serendipity['smarty_custom_vars']) {
-            $serendipity['smarty']->assign($serendipity['smarty_custom_vars']);
-        }
-        break;
-
-    case 'empty':
-        break;
-
-    // Welcome screen or whatever
-    default:
-        serendipity_printEntries(serendipity_fetchEntries(null, true, $serendipity['fetchLimit']));
-        break;
-}
-
-if ($serendipity['GET']['action'] != 'search' && !empty($serendipity['content_message'])) {
-    $serendipity['smarty']->assign('content_message', $serendipity['content_message']);
-}
-
-
-if ($serendipity['smarty']->getTemplateVars('searchresult_tooShort') == null) {
-    $serendipity['smarty']->assign(
-                array(
-                    'searchresult_tooShort' => false
-                )
-            );    
-}
-if ($serendipity['smarty']->getTemplateVars('searchresult_error') == null) {
-    $serendipity['smarty']->assign(
-                array(
-                    'searchresult_error' => false
-                )
-            );    
-}
-if ($serendipity['smarty']->getTemplateVars('searchresult_noEntries') == null) {
-    $serendipity['smarty']->assign(
-                array(
-                    'searchresult_noEntries' => false
-                )
-            );    
-}
-if ($serendipity['smarty']->getTemplateVars('searchresult_results') == null) {
-    $serendipity['smarty']->assign(
-                array(
-                    'searchresult_results' => false
-                )
-            );    
-}
-if ($serendipity['smarty']->getTemplateVars('content_message') == null) {
-    $serendipity['smarty']->assign(
-                array(
-                    'content_message' => false
-                )
-            );    
-}
-if ($serendipity['smarty']->getTemplateVars('ARCHIVES') == null) {
-    $serendipity['smarty']->assign(
-                array(
-                    'ARCHIVES' => ''
-                )
-            );    
-}
-if ($serendipity['smarty']->getTemplateVars('ENTRIES') == null) {
-    $serendipity['smarty']->assign(
-                array(
-                    'ENTRIES' => ''
-                )
-            );    
-}
-
-serendipity_smarty_fetch('CONTENT', 'content.tpl');
-$serendipity['smarty']->assign('ENTRIES', '');
-
-
-/* vim: set sts=4 ts=4 expandtab : */
+$pg = new PageGenerator($serendipity);
+$pg->render();
diff --git a/lib/Serendipity/PageGenerator.php b/lib/Serendipity/PageGenerator.php
new file mode 100644
index 00000000..2cb8127f
--- /dev/null
+++ b/lib/Serendipity/PageGenerator.php
@@ -0,0 +1,198 @@
+<?php
+
+// Serendipity
+// See LICENSE file for license information.
+
+namespace Serendipity;
+
+class PageGenerator
+{
+    protected $serendipity;
+
+    public function __construct(&$serendipity)
+    {
+        $this->serendipity =& $serendipity;
+    }
+
+    public function render()
+    {
+        // TODO: REMOVE ME!
+        $serendipity =& $this->serendipity;
+        include_once('serendipity_config.inc.php');
+        include_once(S9Y_INCLUDE_PATH . 'include/plugin_api.inc.php');
+
+        $uri = $_SERVER['REQUEST_URI'];  // need to define this again here, as index.php (2.1) no longer includes this file
+        $uri_addData = array(
+            'startpage' => false,
+            'uriargs'   => implode('/', serendipity_getUriArguments($uri, true)),
+            'view'      => $this->serendipity['view'],
+            'viewtype'  => isset($this->serendipity['viewtype']) ? $this->serendipity['viewtype'] : ''
+        );
+
+        if ((empty($uri_addData['uriargs']) || trim($uri_addData['uriargs']) == $this->serendipity['indexFile']) && empty($this->serendipity['GET']['subpage'])) {
+            $uri_addData['startpage'] = true;
+        }
+
+        $this->serendipity['plugindata']['smartyvars'] = $uri_addData; // Plugins can change this global variable
+        \serendipity_plugin_api::hook_event('genpage', $uri, $uri_addData);
+        serendipity_smarty_init();
+        if (count($this->serendipity['plugindata']['smartyvars']) > 0) {
+            $this->serendipity['smarty']->assign($this->serendipity['plugindata']['smartyvars']);
+        }
+
+        $leftSidebarElements  = \serendipity_plugin_api::count_plugins('left');
+        $rightSidebarElements = \serendipity_plugin_api::count_plugins('right');
+        $this->serendipity['smarty']->assignByRef('leftSidebarElements', $leftSidebarElements);
+        $this->serendipity['smarty']->assignByRef('rightSidebarElements', $rightSidebarElements);
+
+        switch ($this->serendipity['GET']['action']) {
+            // User wants to read the diary
+            case 'read':
+                if (isset($this->serendipity['GET']['id'])) {
+                    $entry = array(serendipity_fetchEntry('id', $this->serendipity['GET']['id']));
+                    if (!is_array($entry) || count($entry) < 1 || !is_array($entry[0])) {
+                        unset($this->serendipity['GET']['id']);
+                        $entry = array(array());
+                        $this->serendipity['head_subtitle'] = '';
+                        $this->serendipity['smarty']->assign('head_subtitle', $this->serendipity['head_subtitle']);
+                        $this->serendipity['view'] = '404';
+                        $this->serendipity['content_message'] = URL_NOT_FOUND;
+                        serendipity_header('HTTP/1.0 404 Not found');
+                        serendipity_header('Status: 404 Not found');
+                    }
+
+                    serendipity_printEntries($entry, 1);
+                } else {
+                    serendipity_printEntries(serendipity_fetchEntries($this->serendipity['range'] ?? null, true, $this->serendipity['fetchLimit']));
+                }
+                break;
+
+            // User searches
+            case 'search':
+                $r = serendipity_searchEntries($this->serendipity['GET']['searchTerm']);
+                if (strlen($this->serendipity['GET']['searchTerm']) <= 3) {
+                    $this->serendipity['smarty']->assign(
+                        array(
+                            'content_message'       => SEARCH_TOO_SHORT,
+                            'searchresult_tooShort' => true
+                        )
+                    );
+                    break;
+                }
+
+                if (is_string($r) && $r !== true) {
+                    $this->serendipity['smarty']->assign(
+                        array(
+                            'content_message'    => sprintf(SEARCH_ERROR, $this->serendipity['dbPrefix'], $r),
+                            'searchresult_error' => true
+                        )
+                    );
+                    break;
+                } elseif ($r === true) {
+                    $this->serendipity['smarty']->assign(
+                        array(
+                            'content_message'        => sprintf(NO_ENTRIES_BLAHBLAH, '<span class="searchterm">' . $this->serendipity['GET']['searchTerm'] . '</span>'),
+                            'searchresult_noEntries' => true
+                        )
+                    );
+                    break;
+                }
+
+                $this->serendipity['smarty']->assign(
+                    array(
+                        'content_message'      => sprintf(YOUR_SEARCH_RETURNED_BLAHBLAH, '<span class="searchterm">' . $this->serendipity['GET']['searchTerm'] . '</span>', '<span class="searchresults">' . serendipity_getTotalEntries() . '</span>'),
+                        'searchresult_results' => true,
+                        'searchresult_fullentry' => $this->serendipity['GET']['fullentry'] ?? null
+                    )
+                );
+
+                serendipity_printEntries($r);
+                break;
+
+            // Show the comments
+            case 'comments':
+                serendipity_printCommentsByAuthor();
+                // use 'content_message' for pagination?
+                break;
+
+            // Show the archive
+            case 'archives':
+                $this->serendipity['head_subtitle'] = ARCHIVES;
+                $this->serendipity['smarty']->assign('head_subtitle', $this->serendipity['head_subtitle']);
+                serendipity_printArchives();
+                break;
+
+
+            case 'custom':
+                if ($this->serendipity['smarty_custom_vars']) {
+                    $this->serendipity['smarty']->assign($this->serendipity['smarty_custom_vars']);
+                }
+                break;
+
+            case 'empty':
+                break;
+
+            // Welcome screen or whatever
+            default:
+                serendipity_printEntries(serendipity_fetchEntries(null, true, $this->serendipity['fetchLimit']));
+                break;
+        }
+
+        if ($this->serendipity['GET']['action'] != 'search' && !empty($this->serendipity['content_message'])) {
+            $this->serendipity['smarty']->assign('content_message', $this->serendipity['content_message']);
+        }
+
+        if ($this->serendipity['smarty']->getTemplateVars('searchresult_tooShort') == null) {
+            $this->serendipity['smarty']->assign(
+                        array(
+                            'searchresult_tooShort' => false
+                        )
+                    );
+        }
+        if ($this->serendipity['smarty']->getTemplateVars('searchresult_error') == null) {
+            $this->serendipity['smarty']->assign(
+                        array(
+                            'searchresult_error' => false
+                        )
+                    );
+        }
+        if ($this->serendipity['smarty']->getTemplateVars('searchresult_noEntries') == null) {
+            $this->serendipity['smarty']->assign(
+                        array(
+                            'searchresult_noEntries' => false
+                        )
+                    );
+        }
+        if ($this->serendipity['smarty']->getTemplateVars('searchresult_results') == null) {
+            $this->serendipity['smarty']->assign(
+                        array(
+                            'searchresult_results' => false
+                        )
+                    );
+        }
+        if ($this->serendipity['smarty']->getTemplateVars('content_message') == null) {
+            $this->serendipity['smarty']->assign(
+                        array(
+                            'content_message' => false
+                        )
+                    );    
+        }
+        if ($this->serendipity['smarty']->getTemplateVars('ARCHIVES') == null) {
+            $this->serendipity['smarty']->assign(
+                        array(
+                            'ARCHIVES' => ''
+                        )
+                    );
+        }
+        if ($this->serendipity['smarty']->getTemplateVars('ENTRIES') == null) {
+            $this->serendipity['smarty']->assign(
+                        array(
+                            'ENTRIES' => ''
+                        )
+                    );
+        }
+
+        serendipity_smarty_fetch('CONTENT', 'content.tpl');
+        $this->serendipity['smarty']->assign('ENTRIES', '');
+    }
+}
diff --git a/lib/Serendipity/Routing.php b/lib/Serendipity/Routing.php
index b72413bb..63f86a74 100644
--- a/lib/Serendipity/Routing.php
+++ b/lib/Serendipity/Routing.php
@@ -5,6 +5,8 @@
 
 namespace Serendipity;
 
+use Serendipity\PageGenerator;
+
 class Routing
 {
     protected $serendipity;
@@ -25,9 +27,8 @@ class Routing
             $this->serendipity['uriArguments'][] = PATH_ARCHIVES;
         }
 
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
     }
 
     public function serve404()
@@ -37,9 +38,8 @@ class Routing
         $this->serendipity['content_message'] = URL_NOT_FOUND;
         header('HTTP/1.0 404 Not found');
         header('Status: 404 Not found');
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
     }
 
     /* Attempt to locate hidden variables within the URI */
@@ -135,9 +135,9 @@ class Routing
         }
         $this->serendipity['head_subtitle'] = $this->serendipity['blogTitle'];
         $this->serendipity['GET']['action']     = 'comments';
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
     }
 
     public function serveJS($js_mode)
@@ -159,9 +159,8 @@ class Routing
         // the fix below
         $this->serendipity['GET']['action'] = 'empty';
 
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
 
         // HOTFIX: The staticpage plugin spews out a 404 error in the genpage hook,
         // because it assumes that all "normal" content pages could belong to it.
@@ -215,9 +214,9 @@ class Routing
 
         $this->serendipity['GET']['action']     = 'search';
         $this->serendipity['GET']['searchTerm'] = urldecode(serendipity_specialchars(strip_tags(implode(' ', $search))));
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
     }
 
     public function serveAuthorPage($matches)
@@ -246,9 +245,8 @@ class Routing
             $this->serendipity['head_subtitle'] = $this->serendipity['blogTitle'];
         }
 
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
     }
 
     public function serveCategory($matches)
@@ -292,9 +290,8 @@ class Routing
             $this->serendipity['head_subtitle'] = $this->serendipity['blogTitle'];
         }
 
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
     }
 
     public function serveArchive()
@@ -304,9 +301,8 @@ class Routing
 
         $this->locateHiddenVariables($this->serendipity['uriArguments']);
 
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
     }
 
     public function gotoAdmin()
@@ -322,12 +318,13 @@ class Routing
     public function servePlugin($matches) {
         $this->serendipity['view'] = 'plugin';
 
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
         if (strpos($matches[2], 'admin/')  !== false) {
-            include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+            $pg = new PageGenerator($this->serendipity);
+            $pg->render();
         }
 
+        // TODO: REMOVE - BACKWARDS COMPATIBILITY
+        $serendipity =& $this->serendipity;
         \serendipity_plugin_api::hook_event('external_plugin', $matches[2]);
     }
 
@@ -418,9 +415,8 @@ class Routing
             header('Status: 404 Not found');
         }
 
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
     }
 
     public function serveArchives()
@@ -513,8 +509,7 @@ class Routing
             $this->serendipity['head_subtitle'] .= sprintf(ENTRIES_FOR, $date);
         }
 
-        // TODO: REMOVE - BACKWARDS COMPATIBILITY
-        $serendipity =& $this->serendipity;
-        include(S9Y_INCLUDE_PATH . 'include/genpage.inc.php');
+        $pg = new PageGenerator($this->serendipity);
+        $pg->render();
     }
 }