From 7e8dcb0cb6b7af2ce7911a050b8e3d82958e448c Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Mon, 14 Feb 2022 02:49:49 +0100 Subject: [PATCH] Added ConfigContainer. Routing.php is not yet fully converted. --- comment.php | 4 +- include/compat.inc.php | 4 +- include/genpage.inc.php | 2 +- index.php | 2 +- lib/Serendipity/ConfigContainer.php | 99 +++++++++++++++++++++++++ lib/Serendipity/ContentCache.php | 13 ++-- lib/Serendipity/PageGenerator.php | 110 ++++++++++++++-------------- lib/Serendipity/Routing.php | 47 ++++++------ serendipity_admin.php | 2 +- 9 files changed, 194 insertions(+), 89 deletions(-) create mode 100644 lib/Serendipity/ConfigContainer.php diff --git a/comment.php b/comment.php index 366bced4..3d0b3c37 100644 --- a/comment.php +++ b/comment.php @@ -37,7 +37,7 @@ if (!empty($_REQUEST['c']) && !empty($_REQUEST['hash'])) { 'subscribe_confirm_error' => !$res, 'subscribe_confirm_success' => $res, ); - $pg = new PageGenerator($serendipity); + $pg = new PageGenerator(); $pg->render(); $serendipity['smarty']->display(serendipity_getTemplateFile('index.tpl', 'serendipityPath')); exit; @@ -52,7 +52,7 @@ if (!empty($_REQUEST['optin'])) { 'subscribe_confirm_error' => !$res, 'subscribe_confirm_success' => $res, ); - $pg = new PageGenerator($serendipity); + $pg = new PageGenerator(); $pg->render(); $serendipity['smarty']->display(serendipity_getTemplateFile('index.tpl', 'serendipityPath')); exit; diff --git a/include/compat.inc.php b/include/compat.inc.php index fb43792c..6dfd8e90 100644 --- a/include/compat.inc.php +++ b/include/compat.inc.php @@ -6,7 +6,7 @@ if (IN_serendipity !== true) { die ("Don't hack!"); } -$serendipity = array(); +#$serendipity = array(); if (!defined('PATH_SEPARATOR')) { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { @@ -534,4 +534,4 @@ function serendipity_entity_decode($string, $flags = null, $encoding = LANG_CHAR return html_entity_decode($string, $flags, $encoding); } -/* vim: set sts=4 ts=4 expandtab : */ \ No newline at end of file +/* vim: set sts=4 ts=4 expandtab : */ diff --git a/include/genpage.inc.php b/include/genpage.inc.php index c489bfa5..ff264875 100644 --- a/include/genpage.inc.php +++ b/include/genpage.inc.php @@ -6,5 +6,5 @@ use Serendipity\PageGenerator; -$pg = new PageGenerator($serendipity); +$pg = new PageGenerator(); $pg->render(); diff --git a/index.php b/index.php index 08f8d612..c1aebf25 100644 --- a/index.php +++ b/index.php @@ -69,7 +69,7 @@ if (preg_match(PAT_APPROVE, $uri, $res) && $serendipity['serendipityAuthedUser'] define('DATA_TRACKBACK_APPROVED', false); } -$routing = new Routing($serendipity); +$routing = new Routing(); if (preg_match(PAT_ARCHIVES, $uri, $matches) || isset($serendipity['GET']['range']) && is_numeric($serendipity['GET']['range'])) { $routing->serveArchives(); } else if (preg_match(PAT_PERMALINK, $uri, $matches) || diff --git a/lib/Serendipity/ConfigContainer.php b/lib/Serendipity/ConfigContainer.php new file mode 100644 index 00000000..66212625 --- /dev/null +++ b/lib/Serendipity/ConfigContainer.php @@ -0,0 +1,99 @@ +serendipity =& $serendipity; + } + + public function offsetExists($offset): bool + { + return isset($this->serendipity[$offset]); + } + + public function offsetGet($offset) + { + return isset($this->serendipity[$offset]) ? $this->serendipity[$offset] : null; + } + + public function offsetSet($offset, $value): void + { + if (is_null($offset)) { + $this->serendipity[] = $value; + } else { + $this->serendipity[$offset] = $value; + } + } + + public function offsetUnset($offset): void + { + unset($this->serendipity[$offset]); + } + + public function has($key): bool + { + return isset($this->serendipity[$key]); + } + + public function set($key, $value) + { + if (is_null($key)) { + $this->serendipity[] = $value; + } else { + $this->serendipity[$key] = $value; + } + } + + public function setByRef($key, &$value) + { + if (is_null($key)) { + $this->serendipity[] =& $value; + } else { + $this->serendipity[$key] =& $value; + } + } + + public function get($key) + { + return isset($this->serendipity[$key]) ? $this->serendipity[$key] : null; + } + + public function &getByRef($key) + { + return isset($this->serendipity[$key]) ? $this->serendipity[$key] : null; + } + + public function del($key): void + { + unset($this->serendipity[$key]); + } + + public function &getSerendipity() + { + return $this->serendipity; + } +} diff --git a/lib/Serendipity/ContentCache.php b/lib/Serendipity/ContentCache.php index 5623b4c2..ecaec6d6 100644 --- a/lib/Serendipity/ContentCache.php +++ b/lib/Serendipity/ContentCache.php @@ -5,6 +5,7 @@ namespace Serendipity; +use Serendipity\ConfigContainer; use voku\cache\Cache; use voku\cache\CacheAdapterAutoManager; use voku\cache\AdapterArray; @@ -23,14 +24,14 @@ class ContentCache } return self::$instance; } - + private function __construct() { // Configure voku/simple-cache to use templates_c as directory for the opcache files, the fallback // when Memcached and Redis are not used. - // FIXME: Bad hack - remove when no longer needed! - global $serendipity; - $cacheDir = $serendipity['serendipityPath'] . '/templates_c/simple_cache'; + + $cfg = ConfigContainer::getInstance(); + $cacheDir = $cfg->get('serendipityPath') . '/templates_c/simple_cache'; $this->cache_manager = new CacheAdapterAutoManager(); $this->cache_manager->addAdapter( @@ -63,12 +64,12 @@ class ContentCache { return $this->cache->setItem($key, $item, $ttl); } - + public function getItem($key) { return $this->cache->getItem($key); } - + public function clearCache() { return $this->cache->removeAll(); diff --git a/lib/Serendipity/PageGenerator.php b/lib/Serendipity/PageGenerator.php index 2cb8127f..ef4dcfc0 100644 --- a/lib/Serendipity/PageGenerator.php +++ b/lib/Serendipity/PageGenerator.php @@ -5,19 +5,17 @@ namespace Serendipity; +use Serendipity\ConfigContainer; + class PageGenerator { - protected $serendipity; - - public function __construct(&$serendipity) + public function __construct() { - $this->serendipity =& $serendipity; } public function render() { - // TODO: REMOVE ME! - $serendipity =& $this->serendipity; + $cfg = ConfigContainer::getInstance(); include_once('serendipity_config.inc.php'); include_once(S9Y_INCLUDE_PATH . 'include/plugin_api.inc.php'); @@ -25,53 +23,55 @@ class PageGenerator $uri_addData = array( 'startpage' => false, 'uriargs' => implode('/', serendipity_getUriArguments($uri, true)), - 'view' => $this->serendipity['view'], - 'viewtype' => isset($this->serendipity['viewtype']) ? $this->serendipity['viewtype'] : '' + 'view' => $cfg->get('view'), + 'viewtype' => $cfg->get('viewtype') ?? '' ); - if ((empty($uri_addData['uriargs']) || trim($uri_addData['uriargs']) == $this->serendipity['indexFile']) && empty($this->serendipity['GET']['subpage'])) { + if ((empty($uri_addData['uriargs']) || trim($uri_addData['uriargs']) == $cfg->get('indexFile')) && empty($cfg->get('GET')['subpage'])) { $uri_addData['startpage'] = true; } - $this->serendipity['plugindata']['smartyvars'] = $uri_addData; // Plugins can change this global variable + $plugindata = $cfg->getByRef('plugindata'); + $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']); + $smarty = $cfg->get('smarty'); + if (count($plugindata['smartyvars']) > 0) { + $smarty->assign($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); + $smarty->assignByRef('leftSidebarElements', $leftSidebarElements); + $smarty->assignByRef('rightSidebarElements', $rightSidebarElements); - switch ($this->serendipity['GET']['action']) { + switch ($cfg->get('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 (isset($cfg->get('GET')['id'])) { + $entry = array(serendipity_fetchEntry('id', $cfg->get('GET')['id'])); if (!is_array($entry) || count($entry) < 1 || !is_array($entry[0])) { - unset($this->serendipity['GET']['id']); + unset($cfg->get('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; + $cfg->set('head_subtitle', ''); + $smarty->assign('head_subtitle', $cfg->get('head_subtitle')); + $cfg->set('view', '404'); + $cfg->set('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'])); + serendipity_printEntries(serendipity_fetchEntries($cfg->get('range') ?? null, true, $cfg->get('fetchLimit'))); } break; // User searches case 'search': - $r = serendipity_searchEntries($this->serendipity['GET']['searchTerm']); - if (strlen($this->serendipity['GET']['searchTerm']) <= 3) { - $this->serendipity['smarty']->assign( + $r = serendipity_searchEntries($cfg->get('GET')['searchTerm']); + if (strlen($cfg->get('GET')['searchTerm']) <= 3) { + $smarty->assign( array( 'content_message' => SEARCH_TOO_SHORT, 'searchresult_tooShort' => true @@ -81,28 +81,28 @@ class PageGenerator } if (is_string($r) && $r !== true) { - $this->serendipity['smarty']->assign( + $smarty->assign( array( - 'content_message' => sprintf(SEARCH_ERROR, $this->serendipity['dbPrefix'], $r), + 'content_message' => sprintf(SEARCH_ERROR, $cfg->get('dbPrefix'), $r), 'searchresult_error' => true ) ); break; } elseif ($r === true) { - $this->serendipity['smarty']->assign( + $smarty->assign( array( - 'content_message' => sprintf(NO_ENTRIES_BLAHBLAH, '' . $this->serendipity['GET']['searchTerm'] . ''), + 'content_message' => sprintf(NO_ENTRIES_BLAHBLAH, '' . $cfg->get('GET')['searchTerm'] . ''), 'searchresult_noEntries' => true ) ); break; } - $this->serendipity['smarty']->assign( + $smarty->assign( array( - 'content_message' => sprintf(YOUR_SEARCH_RETURNED_BLAHBLAH, '' . $this->serendipity['GET']['searchTerm'] . '', '' . serendipity_getTotalEntries() . ''), + 'content_message' => sprintf(YOUR_SEARCH_RETURNED_BLAHBLAH, '' . $cfg->get('GET')['searchTerm'] . '', '' . serendipity_getTotalEntries() . ''), 'searchresult_results' => true, - 'searchresult_fullentry' => $this->serendipity['GET']['fullentry'] ?? null + 'searchresult_fullentry' => $cfg->get('GET')['fullentry'] ?? null ) ); @@ -117,15 +117,15 @@ class PageGenerator // Show the archive case 'archives': - $this->serendipity['head_subtitle'] = ARCHIVES; - $this->serendipity['smarty']->assign('head_subtitle', $this->serendipity['head_subtitle']); + $cfg->set('head_subtitle', ARCHIVES); + $smarty->assign('head_subtitle', $cfg->get('head_subtitle')); serendipity_printArchives(); break; case 'custom': - if ($this->serendipity['smarty_custom_vars']) { - $this->serendipity['smarty']->assign($this->serendipity['smarty_custom_vars']); + if ($cfg->get('smarty_custom_vars')) { + $smarty->assign($cfg->get('smarty_custom_vars')); } break; @@ -134,58 +134,58 @@ class PageGenerator // Welcome screen or whatever default: - serendipity_printEntries(serendipity_fetchEntries(null, true, $this->serendipity['fetchLimit'])); + serendipity_printEntries(serendipity_fetchEntries(null, true, $cfg->get('fetchLimit'))); break; } - if ($this->serendipity['GET']['action'] != 'search' && !empty($this->serendipity['content_message'])) { - $this->serendipity['smarty']->assign('content_message', $this->serendipity['content_message']); + if ($cfg->get('GET')['action'] != 'search' && !empty($cfg->get('content_message'))) { + $smarty->assign('content_message', $cfg->get('content_message')); } - if ($this->serendipity['smarty']->getTemplateVars('searchresult_tooShort') == null) { - $this->serendipity['smarty']->assign( + if ($smarty->getTemplateVars('searchresult_tooShort') == null) { + $smarty->assign( array( 'searchresult_tooShort' => false ) ); } - if ($this->serendipity['smarty']->getTemplateVars('searchresult_error') == null) { - $this->serendipity['smarty']->assign( + if ($smarty->getTemplateVars('searchresult_error') == null) { + $smarty->assign( array( 'searchresult_error' => false ) ); } - if ($this->serendipity['smarty']->getTemplateVars('searchresult_noEntries') == null) { - $this->serendipity['smarty']->assign( + if ($smarty->getTemplateVars('searchresult_noEntries') == null) { + $smarty->assign( array( 'searchresult_noEntries' => false ) ); } - if ($this->serendipity['smarty']->getTemplateVars('searchresult_results') == null) { - $this->serendipity['smarty']->assign( + if ($smarty->getTemplateVars('searchresult_results') == null) { + $smarty->assign( array( 'searchresult_results' => false ) ); } - if ($this->serendipity['smarty']->getTemplateVars('content_message') == null) { - $this->serendipity['smarty']->assign( + if ($smarty->getTemplateVars('content_message') == null) { + $smarty->assign( array( 'content_message' => false ) ); } - if ($this->serendipity['smarty']->getTemplateVars('ARCHIVES') == null) { - $this->serendipity['smarty']->assign( + if ($smarty->getTemplateVars('ARCHIVES') == null) { + $smarty->assign( array( 'ARCHIVES' => '' ) ); } - if ($this->serendipity['smarty']->getTemplateVars('ENTRIES') == null) { - $this->serendipity['smarty']->assign( + if ($smarty->getTemplateVars('ENTRIES') == null) { + $smarty->assign( array( 'ENTRIES' => '' ) @@ -193,6 +193,6 @@ class PageGenerator } serendipity_smarty_fetch('CONTENT', 'content.tpl'); - $this->serendipity['smarty']->assign('ENTRIES', ''); + $smarty->assign('ENTRIES', ''); } } diff --git a/lib/Serendipity/Routing.php b/lib/Serendipity/Routing.php index 63f86a74..7b4e9513 100644 --- a/lib/Serendipity/Routing.php +++ b/lib/Serendipity/Routing.php @@ -5,40 +5,45 @@ namespace Serendipity; +use Serendipity\ConfigContainer; use Serendipity\PageGenerator; class Routing { protected $serendipity; - public function __construct(&$serendipity) + public function __construct() { - $this->serendipity =& $serendipity; + // FIXME: Temporary workaround while fixing this class + $cfg = ConfigContainer::getInstance(); + $this->serendipity =& $cfg->getSerendipity(); } public function serveIndex() { - $this->serendipity['view'] = 'start'; + $cfg = ConfigContainer::getInstance(); + $cfg->set('view', 'start'); - if ($this->serendipity['GET']['action'] == 'search') { - $this->serendipity['view'] = 'search'; - $this->serendipity['uriArguments'] = array(PATH_SEARCH, urlencode($this->serendipity['GET']['searchTerm'])); + if ($cfg->get('GET')['action'] == 'search') { + $cfg->set('view', 'search'); + $cfg->set('uriArguments', array(PATH_SEARCH, urlencode($this->serendipity['GET']['searchTerm']))); } else { - $this->serendipity['uriArguments'][] = PATH_ARCHIVES; + $cfg->getByRef('uriArguments')[] = PATH_ARCHIVES; } - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } public function serve404() { - $this->serendipity['view'] = '404'; - $this->serendipity['viewtype'] = '404_4'; - $this->serendipity['content_message'] = URL_NOT_FOUND; + $cfg = ConfigContainer::getInstance(); + $cfg->set('view', '404'); + $cfg->set('viewtype', '404_4'); + $cfg->set('content_message', URL_NOT_FOUND); header('HTTP/1.0 404 Not found'); header('Status: 404 Not found'); - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } @@ -136,7 +141,7 @@ class Routing $this->serendipity['head_subtitle'] = $this->serendipity['blogTitle']; $this->serendipity['GET']['action'] = 'comments'; - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } @@ -159,7 +164,7 @@ class Routing // the fix below $this->serendipity['GET']['action'] = 'empty'; - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); // HOTFIX: The staticpage plugin spews out a 404 error in the genpage hook, @@ -215,7 +220,7 @@ class Routing $this->serendipity['GET']['action'] = 'search'; $this->serendipity['GET']['searchTerm'] = urldecode(serendipity_specialchars(strip_tags(implode(' ', $search)))); - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } @@ -245,7 +250,7 @@ class Routing $this->serendipity['head_subtitle'] = $this->serendipity['blogTitle']; } - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } @@ -290,7 +295,7 @@ class Routing $this->serendipity['head_subtitle'] = $this->serendipity['blogTitle']; } - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } @@ -301,7 +306,7 @@ class Routing $this->locateHiddenVariables($this->serendipity['uriArguments']); - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } @@ -319,7 +324,7 @@ class Routing $this->serendipity['view'] = 'plugin'; if (strpos($matches[2], 'admin/') !== false) { - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } @@ -415,7 +420,7 @@ class Routing header('Status: 404 Not found'); } - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } @@ -509,7 +514,7 @@ class Routing $this->serendipity['head_subtitle'] .= sprintf(ENTRIES_FOR, $date); } - $pg = new PageGenerator($this->serendipity); + $pg = new PageGenerator(); $pg->render(); } } diff --git a/serendipity_admin.php b/serendipity_admin.php index 9c292ca1..a71ff076 100644 --- a/serendipity_admin.php +++ b/serendipity_admin.php @@ -39,7 +39,7 @@ if (isset($serendipity['GET']['adminModule']) && $serendipity['GET']['adminModul serendipity_login(true); if (serendipity_userLoggedIn()) { // login with external authentication - reload page to set language settings correct for user - $routing = new Routing($serendipity); + $routing = new Routing(); $routing->gotoAdmin(); return true; }