Commit 27bece0b authored by Tino Goratsch's avatar Tino Goratsch

That's a big one:

- Introduced the FosHttpCache and used it
- made the HTTP cache user context aware
- Cache most of the more or less static frontend pages
- changed the form token from to be by request to by session, as it isn't less save, but reduced complexity and make caching easier
- minor fixes and improvements
parent 819df0e4
......@@ -7,20 +7,46 @@
namespace ACP3\Core\Application\Bootstrap;
use ACP3\Core\Session\SessionHandlerInterface;
use ACP3\Core\View\Renderer\Smarty\Filters\MoveToBottom;
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
use FOS\HttpCache\SymfonyCache\RefreshSubscriber;
use FOS\HttpCache\SymfonyCache\UserContextSubscriber;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Class HttpCache
* @package ACP3\Core\Application\Bootstrap
*/
class HttpCache extends \Symfony\Component\HttpKernel\HttpCache\HttpCache
class HttpCache extends EventDispatchingHttpCache
{
const JAVASCRIPTS_REGEX_PATTERN = MoveToBottom::ELEMENT_CATCHER_REGEX_PATTERN;
const PLACEHOLDER = '</body>';
/**
* @inheritdoc
*/
public function __construct(
HttpKernelInterface $kernel,
StoreInterface $store,
SurrogateInterface $surrogate = null,
array $options = array())
{
parent::__construct($kernel, $store, $surrogate, $options);
$this->addSubscriber(new UserContextSubscriber([
'user_hash_uri' => '/widget/users/index/hash/',
'session_name_prefix' => SessionHandlerInterface::SESSION_NAME
]));
$this->addSubscriber(new PurgeSubscriber());
$this->addSubscriber(new RefreshSubscriber());
}
/**
* {@inheritdoc}
*/
......
......@@ -38,12 +38,13 @@ trait CacheResponseTrait
{
$response = $this->getResponse();
if ($this->getUser()->isAuthenticated() || $this->getApplicationMode() === ApplicationMode::DEVELOPMENT) {
if ($this->getApplicationMode() === ApplicationMode::DEVELOPMENT) {
$response->setPrivate();
$lifetime = null;
}
$response
->setVary('X-User-Context-Hash')
->setPublic()
->setMaxAge($lifetime)
->setSharedMaxAge($lifetime);
......
......@@ -35,45 +35,36 @@ class FormToken
/**
* Generates and renders the form token
*
* @param string $path
*
* @return string
*/
public function renderFormToken($path = '')
public function renderFormToken()
{
$tokenName = Core\Session\SessionHandlerInterface::XSRF_TOKEN_NAME;
$sessionTokens = $this->sessionHandler->get($tokenName, []);
$path = empty($path) ? $this->request->getQuery() : $path;
$path = $path . (!preg_match('/\/$/', $path) ? '/' : '');
$sessionToken = $this->sessionHandler->get($tokenName);
if (!isset($sessionTokens[$path])) {
$sessionTokens[$path] = sha1(uniqid(mt_rand(), true));
$this->sessionHandler->set($tokenName, $sessionTokens);
if (empty($sessionToken)) {
$sessionToken = sha1(uniqid(mt_rand(), true));
$this->sessionHandler->set($tokenName, $sessionToken);
}
return '<input type="hidden" name="' . $tokenName . '" value="' . $sessionTokens[$path] . '" />';
return '<input type="hidden" name="' . $tokenName . '" value="' . $sessionToken . '" />';
}
/**
* Removed the form token from the session
* Removes the form token from the session
*
* @param string $path
* @param string $token
*/
public function unsetFormToken($path = '', $token = '')
public function unsetFormToken($token = '')
{
$path = empty($path) ? $this->request->getQuery() : $path;
$tokenName = Core\Session\SessionHandlerInterface::XSRF_TOKEN_NAME;
if (empty($token) && $this->request->getPost()->has($tokenName)) {
$token = $this->request->getPost()->get($tokenName, '');
}
if (!empty($token)) {
$sessionTokens = $this->sessionHandler->get($tokenName, []);
if (isset($sessionTokens[$path])) {
unset($sessionTokens[$path]);
$this->sessionHandler->set($tokenName, $sessionTokens);
$sessionToken = $this->sessionHandler->get($tokenName);
if (!empty($sessionToken)) {
$this->sessionHandler->remove($tokenName);
}
}
}
......
......@@ -147,8 +147,6 @@ class Create extends AbstractFormAction
$this->createOrUpdateMenuItem($formData, $articleId);
$this->formTokenHelper->unsetFormToken();
return $articleId;
});
}
......
......@@ -195,8 +195,6 @@ class Edit extends AbstractFormAction
$this->insertUriAlias($formData, $articleId);
$this->createOrUpdateMenuItem($formData, $articleId);
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -15,6 +15,8 @@ use ACP3\Modules\ACP3\Articles;
*/
class Details extends Core\Controller\FrontendAction
{
use Core\Cache\CacheResponseTrait;
/**
* @var \ACP3\Core\Date
*/
......@@ -65,6 +67,8 @@ class Details extends Core\Controller\FrontendAction
public function execute($id)
{
if ($this->articleRepository->resultExists($id, $this->date->getCurrentDateTime()) === true) {
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
$article = $this->articlesCache->getCache($id);
$this->breadcrumb->replaceAncestor($article['title'], '', true);
......
......@@ -15,6 +15,8 @@ use ACP3\Modules\ACP3\Articles;
*/
class Index extends Core\Controller\FrontendAction
{
use Core\Cache\CacheResponseTrait;
/**
* @var \ACP3\Core\Date
*/
......@@ -51,6 +53,8 @@ class Index extends Core\Controller\FrontendAction
public function execute()
{
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
$time = $this->date->getCurrentDateTime();
$articles = $this->articleRepository->getAll($time, POS, $this->user->getEntriesPerPage());
$this->pagination->setTotalResults($this->articleRepository->countAll($time));
......
......@@ -55,7 +55,7 @@ class Index extends Core\Controller\WidgetAction
*/
public function execute($template = '')
{
$this->setCacheResponseCacheable();
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
$this->setTemplate($template !== '' ? $template : 'Articles/Widget/index.index.tpl');
......
......@@ -56,7 +56,7 @@ class Single extends Core\Controller\WidgetAction
public function execute($id)
{
if ($this->articleRepository->resultExists((int)$id, $this->date->getCurrentDateTime()) === true) {
$this->setCacheResponseCacheable();
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
return [
'sidebar_article' => $this->articlesCache->getCache($id)
......
......@@ -117,8 +117,6 @@ class Create extends Core\Controller\AdminAction
$this->categoriesCache->saveCache(strtolower($formData['module']));
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -114,8 +114,6 @@ class Edit extends Core\Controller\AdminAction
$this->categoriesCache->saveCache($this->categoryRepository->getModuleNameFromCategoryId($id));
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -75,8 +75,6 @@ class Settings extends Core\Controller\AdminAction
'filesize' => (int)$formData['filesize'],
];
$this->formTokenHelper->unsetFormToken();
return $this->config->setSettings($data, 'categories');
});
}
......
......@@ -141,8 +141,6 @@ class Edit extends Core\Controller\AdminAction
$bool = $this->commentRepository->update($updateValues, $id);
$this->formTokenHelper->unsetFormToken();
return $bool;
},
'acp/comments/details/index/id_' . $moduleId
......
......@@ -86,8 +86,6 @@ class Settings extends Core\Controller\AdminAction
'emoticons' => $formData['emoticons'],
];
$this->formTokenHelper->unsetFormToken();
return $this->config->setSettings($data, 'comments');
});
}
......
......@@ -123,11 +123,11 @@ class Create extends AbstractFrontendAction
$bool = $this->commentRepository->insert($insertValues);
$this->formTokenHelper->unsetFormToken();
return $this->redirectMessages()->setMessage($bool,
return $this->redirectMessages()->setMessage(
$bool,
$this->translator->t('system', $bool !== false ? 'create_success' : 'create_error'),
$this->request->getQuery());
$this->request->getQuery()
);
}
);
}
......
......@@ -79,8 +79,6 @@ class Index extends Core\Controller\AdminAction
'vat_id' => $this->get('core.helpers.secure')->strEncode($formData['vat_id'], true),
];
$this->formTokenHelper->unsetFormToken();
return $this->config->setSettings($data, 'contact');
});
}
......
......@@ -16,11 +16,15 @@ use ACP3\Modules\ACP3\Contact;
*/
class Imprint extends Core\Controller\FrontendAction
{
use Core\Cache\CacheResponseTrait;
/**
* @return array
*/
public function execute()
{
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
return [
'imprint' => $this->config->getSettings('contact'),
'powered_by' => $this->translator->t(
......
......@@ -133,11 +133,8 @@ class Index extends Core\Controller\FrontendAction
$this->sendEmailHelper->execute($formData['name'], $formData['mail'], $settings['mail'], $subjectCopy, $bodyCopy);
}
$this->formTokenHelper->unsetFormToken();
$this->setTemplate($this->get('core.helpers.alerts')->confirmBox(
$bool === true ? $this->translator->t('contact',
'send_mail_success') : $this->translator->t('contact', 'send_mail_error'),
$this->translator->t('contact', $bool === true ? 'send_mail_success' : 'send_mail_error'),
$this->router->route('contact')
));
}
......
......@@ -15,11 +15,15 @@ use ACP3\Modules\ACP3\Contact;
*/
class Index extends Core\Controller\WidgetAction
{
use Core\Cache\CacheResponseTrait;
/**
* @return array
*/
public function execute()
{
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
return [
'sidebar_contact' => $this->config->getSettings('contact')
];
......
......@@ -101,8 +101,6 @@ class Create extends Core\Controller\AdminAction
$this->emoticonsCache->saveCache();
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -113,8 +113,6 @@ class Edit extends Core\Controller\AdminAction
$this->emoticonsCache->saveCache();
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -73,8 +73,6 @@ class Settings extends Core\Controller\AdminAction
'filesize' => (int)$formData['filesize'],
];
$this->formTokenHelper->unsetFormToken();
return $this->config->setSettings($data, 'emoticons');
});
}
......
......@@ -86,8 +86,6 @@ class Index extends Core\Controller\AdminAction
'feed_type' => $formData['feed_type']
];
$this->formTokenHelper->unsetFormToken();
return $this->config->setSettings($data, 'feeds');
});
}
......
......@@ -15,6 +15,8 @@ use ACP3\Modules\ACP3\Feeds;
*/
class Index extends Core\Controller\FrontendAction
{
use Core\Cache\CacheResponseTrait;
/**
* @var \ACP3\Modules\ACP3\Feeds\Helper\FeedGenerator
*/
......@@ -42,6 +44,8 @@ class Index extends Core\Controller\FrontendAction
public function execute($feed)
{
if ($this->acl->hasPermission('frontend/' . $feed) === true) {
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
$module = $this->request->getParameters()->get('feed', '');
$this->feedGenerator
->setTitle($this->config->getSettings('seo')['title'])
......
......@@ -157,8 +157,6 @@ class Create extends AbstractFormAction
$this->insertUriAlias($formData, $lastId);
$this->formTokenHelper->unsetFormToken();
return $lastId;
});
}
......
......@@ -170,8 +170,6 @@ class Edit extends AbstractFormAction
$this->filesCache->saveCache($fileId);
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -112,8 +112,6 @@ class Settings extends Core\Controller\AdminAction
$data['comments'] = $formData['comments'];
}
$this->formTokenHelper->unsetFormToken();
return $this->config->setSettings($data, 'files');
});
}
......
......@@ -17,6 +17,8 @@ use Symfony\Component\HttpFoundation\ResponseHeaderBag;
*/
class Details extends Core\Controller\FrontendAction
{
use Core\Cache\CacheResponseTrait;
/**
* @var \ACP3\Core\Date
*/
......@@ -66,6 +68,8 @@ class Details extends Core\Controller\FrontendAction
public function execute($id, $action = '')
{
if ($this->filesRepository->resultExists($id, $this->date->getCurrentDateTime()) === true) {
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
$file = $this->filesCache->getCache($id);
if ($action === 'download') {
......
......@@ -16,6 +16,8 @@ use ACP3\Modules\ACP3\Files as FilesModule;
*/
class Files extends Core\Controller\FrontendAction
{
use Core\Cache\CacheResponseTrait;
/**
* @var \ACP3\Core\Date
*/
......@@ -59,6 +61,8 @@ class Files extends Core\Controller\FrontendAction
public function execute($cat)
{
if ($this->categoryRepository->resultExists($cat) === true) {
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
$category = $this->categoryRepository->getOneById($cat);
$this->breadcrumb
......
......@@ -15,6 +15,8 @@ use ACP3\Modules\ACP3\Categories;
*/
class Index extends Core\Controller\FrontendAction
{
use Core\Cache\CacheResponseTrait;
/**
* @var \ACP3\Modules\ACP3\Categories\Cache
*/
......@@ -40,6 +42,8 @@ class Index extends Core\Controller\FrontendAction
*/
public function execute()
{
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
return [
'categories' => $this->categoriesCache->getCache('files')
];
......
......@@ -50,7 +50,7 @@ class Index extends Core\Controller\WidgetAction
*/
public function execute($categoryId = 0, $template = '')
{
$this->setCacheResponseCacheable();
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
$settings = $this->config->getSettings('files');
......
......@@ -100,8 +100,6 @@ class Create extends AbstractFormAction
$this->insertUriAlias($formData, $lastId);
$this->formTokenHelper->unsetFormToken();
return $lastId;
});
}
......
......@@ -199,8 +199,6 @@ class Edit extends AbstractFormAction
$this->generatePictureAliases($galleryId);
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -98,8 +98,6 @@ class Settings extends Core\Controller\AdminAction
$data['comments'] = (int)$formData['comments'];
}
$this->formTokenHelper->unsetFormToken();
$bool = $this->config->setSettings($data, 'gallery');
if ($formData['thumbwidth'] !== $settings['thumbwidth']
......
......@@ -181,8 +181,6 @@ class Create extends AbstractFormAction
$this->galleryCache->saveCache($id);
$this->formTokenHelper->unsetFormToken();
return $lastId && $bool2;
},
'acp/gallery/index/edit/id_' . $id
......
......@@ -144,8 +144,6 @@ class Edit extends AbstractFormAction
$this->galleryCache->saveCache($picture['gallery_id']);
$this->formTokenHelper->unsetFormToken();
return $bool;
},
'acp/gallery/index/edit/id_' . $picture['gallery_id']
......
......@@ -49,7 +49,7 @@ class Index extends Core\Controller\WidgetAction
*/
public function execute()
{
$this->setCacheResponseCacheable();
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
$settings = $this->config->getSettings('gallery');
......
......@@ -130,8 +130,6 @@ class Edit extends Core\Controller\AdminAction
$bool = $this->guestbookRepository->update($updateValues, $id);
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -105,8 +105,6 @@ class Settings extends Core\Controller\AdminAction
'newsletter_integration' => $formData['newsletter_integration'],
];
$this->formTokenHelper->unsetFormToken();
return $this->config->setSettings($data, 'guestbook');
});
}
......
......@@ -203,10 +203,10 @@ class Create extends AbstractAction
$this->newsletterSubscribeHelper->subscribeToNewsletter($formData['mail']);
}
$this->formTokenHelper->unsetFormToken();
return $this->redirectMessages()->setMessage($lastId,
$this->translator->t('system', $lastId !== false ? 'create_success' : 'create_error'));
return $this->redirectMessages()->setMessage(
$lastId,
$this->translator->t('system', $lastId !== false ? 'create_success' : 'create_error')
);
}
);
}
......
......@@ -17,6 +17,8 @@ use ACP3\Modules\ACP3\Newsletter;
*/
class Index extends AbstractAction
{
use Core\Cache\CacheResponseTrait;
/**
* @var \ACP3\Core\Pagination
*/
......@@ -49,6 +51,8 @@ class Index extends AbstractAction
*/
public function execute()
{
$this->setCacheResponseCacheable($this->config->getSettings('system')['cache_minify']);
$guestbook = $this->guestbookRepository->getAll($this->guestbookSettings['notify'], POS, $this->user->getEntriesPerPage());
$cGuestbook = count($guestbook);
......
......@@ -82,8 +82,6 @@ class Create extends Core\Controller\AdminAction
$lastId = $this->menuRepository->insert($insertValues);
$this->formTokenHelper->unsetFormToken();
return $lastId;
});
}
......
......@@ -104,8 +104,6 @@ class Edit extends Core\Controller\AdminAction
$this->menusCache->saveMenusCache();
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -156,10 +156,11 @@ class Create extends AbstractFormAction
$this->menusCache->saveMenusCache();
$this->formTokenHelper->unsetFormToken();
return $this->redirectMessages()->setMessage($bool,
$this->translator->t('system', $bool !== false ? 'create_success' : 'create_error'), 'acp/menus');
return $this->redirectMessages()->setMessage(
$bool,
$this->translator->t('system', $bool !== false ? 'create_success' : 'create_error'),
'acp/menus'
);
},
'acp/menus'
);
......
......@@ -181,10 +181,11 @@ class Edit extends AbstractFormAction
$this->menusCache->saveMenusCache();
$this->formTokenHelper->unsetFormToken();
return $this->redirectMessages()->setMessage($bool,
$this->translator->t('system', $bool !== false ? 'edit_success' : 'edit_error'), 'acp/menus');
return $this->redirectMessages()->setMessage(
$bool,
$this->translator->t('system', $bool !== false ? 'edit_success' : 'edit_error'),
'acp/menus'
);
},
'acp/menus'
);
......
......@@ -124,8 +124,6 @@ class Create extends AbstractFormAction
$this->insertUriAlias($formData, $newsId);
$this->formTokenHelper->unsetFormToken();
return $newsId;
});
}
......
......@@ -139,8 +139,6 @@ class Edit extends AbstractFormAction
$this->newsCache->saveCache($newsId);
$this->formTokenHelper->unsetFormToken();
return $bool;
});
}
......
......@@ -100,8 +100,6 @@ class Settings extends Core\Controller\AdminAction
$data['comments'] = $formData['comments'];
}
$this->formTokenHelper->unsetFormToken();