Added ConfigContainer. Routing.php is not yet fully converted.

This commit is contained in:
Markus Birth 2022-02-14 02:49:49 +01:00
parent 8ebf5e6865
commit 7e8dcb0cb6
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
9 changed files with 194 additions and 89 deletions

View File

@ -37,7 +37,7 @@ if (!empty($_REQUEST['c']) && !empty($_REQUEST['hash'])) {
'subscribe_confirm_error' => !$res, 'subscribe_confirm_error' => !$res,
'subscribe_confirm_success' => $res, 'subscribe_confirm_success' => $res,
); );
$pg = new PageGenerator($serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
$serendipity['smarty']->display(serendipity_getTemplateFile('index.tpl', 'serendipityPath')); $serendipity['smarty']->display(serendipity_getTemplateFile('index.tpl', 'serendipityPath'));
exit; exit;
@ -52,7 +52,7 @@ if (!empty($_REQUEST['optin'])) {
'subscribe_confirm_error' => !$res, 'subscribe_confirm_error' => !$res,
'subscribe_confirm_success' => $res, 'subscribe_confirm_success' => $res,
); );
$pg = new PageGenerator($serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
$serendipity['smarty']->display(serendipity_getTemplateFile('index.tpl', 'serendipityPath')); $serendipity['smarty']->display(serendipity_getTemplateFile('index.tpl', 'serendipityPath'));
exit; exit;

View File

@ -6,7 +6,7 @@ if (IN_serendipity !== true) {
die ("Don't hack!"); die ("Don't hack!");
} }
$serendipity = array(); #$serendipity = array();
if (!defined('PATH_SEPARATOR')) { if (!defined('PATH_SEPARATOR')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { 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); return html_entity_decode($string, $flags, $encoding);
} }
/* vim: set sts=4 ts=4 expandtab : */ /* vim: set sts=4 ts=4 expandtab : */

View File

@ -6,5 +6,5 @@
use Serendipity\PageGenerator; use Serendipity\PageGenerator;
$pg = new PageGenerator($serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();

View File

@ -69,7 +69,7 @@ if (preg_match(PAT_APPROVE, $uri, $res) && $serendipity['serendipityAuthedUser']
define('DATA_TRACKBACK_APPROVED', false); 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'])) { if (preg_match(PAT_ARCHIVES, $uri, $matches) || isset($serendipity['GET']['range']) && is_numeric($serendipity['GET']['range'])) {
$routing->serveArchives(); $routing->serveArchives();
} else if (preg_match(PAT_PERMALINK, $uri, $matches) || } else if (preg_match(PAT_PERMALINK, $uri, $matches) ||

View File

@ -0,0 +1,99 @@
<?php
// Serendipity
// See LICENSE file for license information.
namespace Serendipity;
use ArrayAccess;
class ConfigContainer implements ArrayAccess
{
protected static $instance = null;
protected $serendipity;
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct()
{
// FIXME: Compatibility with old style
global $serendipity;
if (!is_array($serendipity)) {
$serendipity = [];
}
$this->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;
}
}

View File

@ -5,6 +5,7 @@
namespace Serendipity; namespace Serendipity;
use Serendipity\ConfigContainer;
use voku\cache\Cache; use voku\cache\Cache;
use voku\cache\CacheAdapterAutoManager; use voku\cache\CacheAdapterAutoManager;
use voku\cache\AdapterArray; use voku\cache\AdapterArray;
@ -23,14 +24,14 @@ class ContentCache
} }
return self::$instance; return self::$instance;
} }
private function __construct() private function __construct()
{ {
// Configure voku/simple-cache to use templates_c as directory for the opcache files, the fallback // Configure voku/simple-cache to use templates_c as directory for the opcache files, the fallback
// when Memcached and Redis are not used. // when Memcached and Redis are not used.
// FIXME: Bad hack - remove when no longer needed!
global $serendipity; $cfg = ConfigContainer::getInstance();
$cacheDir = $serendipity['serendipityPath'] . '/templates_c/simple_cache'; $cacheDir = $cfg->get('serendipityPath') . '/templates_c/simple_cache';
$this->cache_manager = new CacheAdapterAutoManager(); $this->cache_manager = new CacheAdapterAutoManager();
$this->cache_manager->addAdapter( $this->cache_manager->addAdapter(
@ -63,12 +64,12 @@ class ContentCache
{ {
return $this->cache->setItem($key, $item, $ttl); return $this->cache->setItem($key, $item, $ttl);
} }
public function getItem($key) public function getItem($key)
{ {
return $this->cache->getItem($key); return $this->cache->getItem($key);
} }
public function clearCache() public function clearCache()
{ {
return $this->cache->removeAll(); return $this->cache->removeAll();

View File

@ -5,19 +5,17 @@
namespace Serendipity; namespace Serendipity;
use Serendipity\ConfigContainer;
class PageGenerator class PageGenerator
{ {
protected $serendipity; public function __construct()
public function __construct(&$serendipity)
{ {
$this->serendipity =& $serendipity;
} }
public function render() public function render()
{ {
// TODO: REMOVE ME! $cfg = ConfigContainer::getInstance();
$serendipity =& $this->serendipity;
include_once('serendipity_config.inc.php'); include_once('serendipity_config.inc.php');
include_once(S9Y_INCLUDE_PATH . 'include/plugin_api.inc.php'); include_once(S9Y_INCLUDE_PATH . 'include/plugin_api.inc.php');
@ -25,53 +23,55 @@ class PageGenerator
$uri_addData = array( $uri_addData = array(
'startpage' => false, 'startpage' => false,
'uriargs' => implode('/', serendipity_getUriArguments($uri, true)), 'uriargs' => implode('/', serendipity_getUriArguments($uri, true)),
'view' => $this->serendipity['view'], 'view' => $cfg->get('view'),
'viewtype' => isset($this->serendipity['viewtype']) ? $this->serendipity['viewtype'] : '' '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; $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_plugin_api::hook_event('genpage', $uri, $uri_addData);
serendipity_smarty_init(); serendipity_smarty_init();
if (count($this->serendipity['plugindata']['smartyvars']) > 0) { $smarty = $cfg->get('smarty');
$this->serendipity['smarty']->assign($this->serendipity['plugindata']['smartyvars']); if (count($plugindata['smartyvars']) > 0) {
$smarty->assign($plugindata['smartyvars']);
} }
$leftSidebarElements = \serendipity_plugin_api::count_plugins('left'); $leftSidebarElements = \serendipity_plugin_api::count_plugins('left');
$rightSidebarElements = \serendipity_plugin_api::count_plugins('right'); $rightSidebarElements = \serendipity_plugin_api::count_plugins('right');
$this->serendipity['smarty']->assignByRef('leftSidebarElements', $leftSidebarElements); $smarty->assignByRef('leftSidebarElements', $leftSidebarElements);
$this->serendipity['smarty']->assignByRef('rightSidebarElements', $rightSidebarElements); $smarty->assignByRef('rightSidebarElements', $rightSidebarElements);
switch ($this->serendipity['GET']['action']) { switch ($cfg->get('GET')['action']) {
// User wants to read the diary // User wants to read the diary
case 'read': case 'read':
if (isset($this->serendipity['GET']['id'])) { if (isset($cfg->get('GET')['id'])) {
$entry = array(serendipity_fetchEntry('id', $this->serendipity['GET']['id'])); $entry = array(serendipity_fetchEntry('id', $cfg->get('GET')['id']));
if (!is_array($entry) || count($entry) < 1 || !is_array($entry[0])) { if (!is_array($entry) || count($entry) < 1 || !is_array($entry[0])) {
unset($this->serendipity['GET']['id']); unset($cfg->get('GET')['id']);
$entry = array(array()); $entry = array(array());
$this->serendipity['head_subtitle'] = ''; $cfg->set('head_subtitle', '');
$this->serendipity['smarty']->assign('head_subtitle', $this->serendipity['head_subtitle']); $smarty->assign('head_subtitle', $cfg->get('head_subtitle'));
$this->serendipity['view'] = '404'; $cfg->set('view', '404');
$this->serendipity['content_message'] = URL_NOT_FOUND; $cfg->set('content_message', URL_NOT_FOUND);
serendipity_header('HTTP/1.0 404 Not found'); serendipity_header('HTTP/1.0 404 Not found');
serendipity_header('Status: 404 Not found'); serendipity_header('Status: 404 Not found');
} }
serendipity_printEntries($entry, 1); serendipity_printEntries($entry, 1);
} else { } 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; break;
// User searches // User searches
case 'search': case 'search':
$r = serendipity_searchEntries($this->serendipity['GET']['searchTerm']); $r = serendipity_searchEntries($cfg->get('GET')['searchTerm']);
if (strlen($this->serendipity['GET']['searchTerm']) <= 3) { if (strlen($cfg->get('GET')['searchTerm']) <= 3) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'content_message' => SEARCH_TOO_SHORT, 'content_message' => SEARCH_TOO_SHORT,
'searchresult_tooShort' => true 'searchresult_tooShort' => true
@ -81,28 +81,28 @@ class PageGenerator
} }
if (is_string($r) && $r !== true) { if (is_string($r) && $r !== true) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'content_message' => sprintf(SEARCH_ERROR, $this->serendipity['dbPrefix'], $r), 'content_message' => sprintf(SEARCH_ERROR, $cfg->get('dbPrefix'), $r),
'searchresult_error' => true 'searchresult_error' => true
) )
); );
break; break;
} elseif ($r === true) { } elseif ($r === true) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'content_message' => sprintf(NO_ENTRIES_BLAHBLAH, '<span class="searchterm">' . $this->serendipity['GET']['searchTerm'] . '</span>'), 'content_message' => sprintf(NO_ENTRIES_BLAHBLAH, '<span class="searchterm">' . $cfg->get('GET')['searchTerm'] . '</span>'),
'searchresult_noEntries' => true 'searchresult_noEntries' => true
) )
); );
break; break;
} }
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'content_message' => sprintf(YOUR_SEARCH_RETURNED_BLAHBLAH, '<span class="searchterm">' . $this->serendipity['GET']['searchTerm'] . '</span>', '<span class="searchresults">' . serendipity_getTotalEntries() . '</span>'), 'content_message' => sprintf(YOUR_SEARCH_RETURNED_BLAHBLAH, '<span class="searchterm">' . $cfg->get('GET')['searchTerm'] . '</span>', '<span class="searchresults">' . serendipity_getTotalEntries() . '</span>'),
'searchresult_results' => true, '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 // Show the archive
case 'archives': case 'archives':
$this->serendipity['head_subtitle'] = ARCHIVES; $cfg->set('head_subtitle', ARCHIVES);
$this->serendipity['smarty']->assign('head_subtitle', $this->serendipity['head_subtitle']); $smarty->assign('head_subtitle', $cfg->get('head_subtitle'));
serendipity_printArchives(); serendipity_printArchives();
break; break;
case 'custom': case 'custom':
if ($this->serendipity['smarty_custom_vars']) { if ($cfg->get('smarty_custom_vars')) {
$this->serendipity['smarty']->assign($this->serendipity['smarty_custom_vars']); $smarty->assign($cfg->get('smarty_custom_vars'));
} }
break; break;
@ -134,58 +134,58 @@ class PageGenerator
// Welcome screen or whatever // Welcome screen or whatever
default: default:
serendipity_printEntries(serendipity_fetchEntries(null, true, $this->serendipity['fetchLimit'])); serendipity_printEntries(serendipity_fetchEntries(null, true, $cfg->get('fetchLimit')));
break; break;
} }
if ($this->serendipity['GET']['action'] != 'search' && !empty($this->serendipity['content_message'])) { if ($cfg->get('GET')['action'] != 'search' && !empty($cfg->get('content_message'))) {
$this->serendipity['smarty']->assign('content_message', $this->serendipity['content_message']); $smarty->assign('content_message', $cfg->get('content_message'));
} }
if ($this->serendipity['smarty']->getTemplateVars('searchresult_tooShort') == null) { if ($smarty->getTemplateVars('searchresult_tooShort') == null) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'searchresult_tooShort' => false 'searchresult_tooShort' => false
) )
); );
} }
if ($this->serendipity['smarty']->getTemplateVars('searchresult_error') == null) { if ($smarty->getTemplateVars('searchresult_error') == null) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'searchresult_error' => false 'searchresult_error' => false
) )
); );
} }
if ($this->serendipity['smarty']->getTemplateVars('searchresult_noEntries') == null) { if ($smarty->getTemplateVars('searchresult_noEntries') == null) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'searchresult_noEntries' => false 'searchresult_noEntries' => false
) )
); );
} }
if ($this->serendipity['smarty']->getTemplateVars('searchresult_results') == null) { if ($smarty->getTemplateVars('searchresult_results') == null) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'searchresult_results' => false 'searchresult_results' => false
) )
); );
} }
if ($this->serendipity['smarty']->getTemplateVars('content_message') == null) { if ($smarty->getTemplateVars('content_message') == null) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'content_message' => false 'content_message' => false
) )
); );
} }
if ($this->serendipity['smarty']->getTemplateVars('ARCHIVES') == null) { if ($smarty->getTemplateVars('ARCHIVES') == null) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'ARCHIVES' => '' 'ARCHIVES' => ''
) )
); );
} }
if ($this->serendipity['smarty']->getTemplateVars('ENTRIES') == null) { if ($smarty->getTemplateVars('ENTRIES') == null) {
$this->serendipity['smarty']->assign( $smarty->assign(
array( array(
'ENTRIES' => '' 'ENTRIES' => ''
) )
@ -193,6 +193,6 @@ class PageGenerator
} }
serendipity_smarty_fetch('CONTENT', 'content.tpl'); serendipity_smarty_fetch('CONTENT', 'content.tpl');
$this->serendipity['smarty']->assign('ENTRIES', ''); $smarty->assign('ENTRIES', '');
} }
} }

View File

@ -5,40 +5,45 @@
namespace Serendipity; namespace Serendipity;
use Serendipity\ConfigContainer;
use Serendipity\PageGenerator; use Serendipity\PageGenerator;
class Routing class Routing
{ {
protected $serendipity; 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() public function serveIndex()
{ {
$this->serendipity['view'] = 'start'; $cfg = ConfigContainer::getInstance();
$cfg->set('view', 'start');
if ($this->serendipity['GET']['action'] == 'search') { if ($cfg->get('GET')['action'] == 'search') {
$this->serendipity['view'] = 'search'; $cfg->set('view', 'search');
$this->serendipity['uriArguments'] = array(PATH_SEARCH, urlencode($this->serendipity['GET']['searchTerm'])); $cfg->set('uriArguments', array(PATH_SEARCH, urlencode($this->serendipity['GET']['searchTerm'])));
} else { } else {
$this->serendipity['uriArguments'][] = PATH_ARCHIVES; $cfg->getByRef('uriArguments')[] = PATH_ARCHIVES;
} }
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
public function serve404() public function serve404()
{ {
$this->serendipity['view'] = '404'; $cfg = ConfigContainer::getInstance();
$this->serendipity['viewtype'] = '404_4'; $cfg->set('view', '404');
$this->serendipity['content_message'] = URL_NOT_FOUND; $cfg->set('viewtype', '404_4');
$cfg->set('content_message', URL_NOT_FOUND);
header('HTTP/1.0 404 Not found'); header('HTTP/1.0 404 Not found');
header('Status: 404 Not found'); header('Status: 404 Not found');
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
@ -136,7 +141,7 @@ class Routing
$this->serendipity['head_subtitle'] = $this->serendipity['blogTitle']; $this->serendipity['head_subtitle'] = $this->serendipity['blogTitle'];
$this->serendipity['GET']['action'] = 'comments'; $this->serendipity['GET']['action'] = 'comments';
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
@ -159,7 +164,7 @@ class Routing
// the fix below // the fix below
$this->serendipity['GET']['action'] = 'empty'; $this->serendipity['GET']['action'] = 'empty';
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
// HOTFIX: The staticpage plugin spews out a 404 error in the genpage hook, // 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']['action'] = 'search';
$this->serendipity['GET']['searchTerm'] = urldecode(serendipity_specialchars(strip_tags(implode(' ', $search)))); $this->serendipity['GET']['searchTerm'] = urldecode(serendipity_specialchars(strip_tags(implode(' ', $search))));
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
@ -245,7 +250,7 @@ class Routing
$this->serendipity['head_subtitle'] = $this->serendipity['blogTitle']; $this->serendipity['head_subtitle'] = $this->serendipity['blogTitle'];
} }
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
@ -290,7 +295,7 @@ class Routing
$this->serendipity['head_subtitle'] = $this->serendipity['blogTitle']; $this->serendipity['head_subtitle'] = $this->serendipity['blogTitle'];
} }
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
@ -301,7 +306,7 @@ class Routing
$this->locateHiddenVariables($this->serendipity['uriArguments']); $this->locateHiddenVariables($this->serendipity['uriArguments']);
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
@ -319,7 +324,7 @@ class Routing
$this->serendipity['view'] = 'plugin'; $this->serendipity['view'] = 'plugin';
if (strpos($matches[2], 'admin/') !== false) { if (strpos($matches[2], 'admin/') !== false) {
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
@ -415,7 +420,7 @@ class Routing
header('Status: 404 Not found'); header('Status: 404 Not found');
} }
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
@ -509,7 +514,7 @@ class Routing
$this->serendipity['head_subtitle'] .= sprintf(ENTRIES_FOR, $date); $this->serendipity['head_subtitle'] .= sprintf(ENTRIES_FOR, $date);
} }
$pg = new PageGenerator($this->serendipity); $pg = new PageGenerator();
$pg->render(); $pg->render();
} }
} }

View File

@ -39,7 +39,7 @@ if (isset($serendipity['GET']['adminModule']) && $serendipity['GET']['adminModul
serendipity_login(true); serendipity_login(true);
if (serendipity_userLoggedIn()) { if (serendipity_userLoggedIn()) {
// login with external authentication - reload page to set language settings correct for user // login with external authentication - reload page to set language settings correct for user
$routing = new Routing($serendipity); $routing = new Routing();
$routing->gotoAdmin(); $routing->gotoAdmin();
return true; return true;
} }