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_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;

View File

@ -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 : */
/* vim: set sts=4 ts=4 expandtab : */

View File

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

View File

@ -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) ||

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;
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();

View File

@ -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, '<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
)
);
break;
}
$this->serendipity['smarty']->assign(
$smarty->assign(
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_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', '');
}
}

View File

@ -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();
}
}

View File

@ -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;
}