Check if there are any messages
public static hasMessages ( string $strScope = TL_MODE ) : boolean | ||
$strScope | string | An optional message scope |
Résultat | boolean | True if there are messages |
/** * Add the template output to the cache and add the cache headers */ protected function addToCache() { /** @var PageModel $objPage */ global $objPage; $intCache = 0; // Decide whether the page shall be cached if (!isset($_GET['file']) && !isset($_GET['token']) && empty($_POST) && !BE_USER_LOGGED_IN && !FE_USER_LOGGED_IN && !$_SESSION['DISABLE_CACHE'] && !isset($_SESSION['LOGIN_ERROR']) && !\Message::hasMessages() && intval($objPage->cache) > 0 && !$objPage->protected) { $intCache = time() + intval($objPage->cache); } // Server-side cache if ($intCache > 0 && (\Config::get('cacheMode') == 'both' || \Config::get('cacheMode') == 'server')) { // If the request string is empty, use a special cache tag which considers the page language if (\Environment::get('relativeRequest') == '') { $strCacheKey = \Environment::get('host') . '/empty.' . $objPage->language; } else { $strCacheKey = \Environment::get('host') . '/' . \Environment::get('relativeRequest'); } // HOOK: add custom logic if (isset($GLOBALS['TL_HOOKS']['getCacheKey']) && is_array($GLOBALS['TL_HOOKS']['getCacheKey'])) { foreach ($GLOBALS['TL_HOOKS']['getCacheKey'] as $callback) { $this->import($callback[0]); $strCacheKey = $this->{$callback[0]}->{$callback[1]}($strCacheKey); } } // Add a suffix if there is a mobile layout (see #7826) if ($objPage->mobileLayout > 0) { if (\Input::cookie('TL_VIEW') == 'mobile' || \Environment::get('agent')->mobile && \Input::cookie('TL_VIEW') != 'desktop') { $strCacheKey .= '.mobile'; } else { $strCacheKey .= '.desktop'; } } // Replace insert tags for caching $strBuffer = $this->replaceInsertTags($this->strBuffer); $strBuffer = $this->replaceDynamicScriptTags($strBuffer); // see #4203 // Add the cache file header $strHeader = sprintf("<?php /* %s */ \$expire = %d; \$content = %s; \$type = %s; \$files = %s; \$assets = %s; ?>\n", $strCacheKey, (int) $intCache, var_export($this->strContentType, true), var_export($objPage->type, true), var_export(TL_FILES_URL, true), var_export(TL_ASSETS_URL, true)); $strCachePath = str_replace(TL_ROOT . '/', '', \System::getContainer()->getParameter('kernel.cache_dir')); // Create the cache file $strMd5CacheKey = md5($strCacheKey); $objFile = new \File($strCachePath . '/contao/html/' . substr($strMd5CacheKey, 0, 1) . '/' . $strMd5CacheKey . '.html'); $objFile->write($strHeader); $objFile->append($this->minifyHtml($strBuffer), ''); $objFile->close(); } // Client-side cache if (!headers_sent()) { if ($intCache > 0 && (\Config::get('cacheMode') == 'both' || \Config::get('cacheMode') == 'browser')) { header('Cache-Control: private, max-age=' . ($intCache - time())); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); header('Expires: ' . gmdate('D, d M Y H:i:s', $intCache) . ' GMT'); } else { header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Expires: Fri, 06 Jun 1975 15:10:00 GMT'); } } }
/** * Check whether there is a cached version of the page and return a response object * * @return Response|null */ public static function getResponseFromCache() { // Build the page if a user is (potentially) logged in or there is POST data if (!empty($_POST) || \Input::cookie('BE_USER_AUTH') || \Input::cookie('FE_USER_AUTH') || \Input::cookie('FE_AUTO_LOGIN') || $_SESSION['DISABLE_CACHE'] || isset($_SESSION['LOGIN_ERROR']) || \Message::hasMessages() || \Config::get('debugMode')) { return null; } $strCacheDir = \System::getContainer()->getParameter('kernel.cache_dir'); // Try to map the empty request if (\Environment::get('relativeRequest') == '') { // Return if the language is added to the URL and the empty domain will be redirected if (\Config::get('addLanguageToUrl') && !\Config::get('doNotRedirectEmpty')) { return null; } $strCacheKey = null; $arrLanguage = \Environment::get('httpAcceptLanguage'); $strMappingFile = $strCacheDir . '/contao/config/mapping.php'; // Try to get the cache key from the mapper array if (file_exists($strMappingFile)) { $arrMapper = (include $strMappingFile); $arrPaths = array(\Environment::get('host'), '*'); // Try the language specific keys foreach ($arrLanguage as $strLanguage) { foreach ($arrPaths as $strPath) { $strKey = $strPath . '/empty.' . $strLanguage; if (isset($arrMapper[$strKey])) { $strCacheKey = $arrMapper[$strKey]; break; } } } // Try the fallback key if ($strCacheKey === null) { foreach ($arrPaths as $strPath) { $strKey = $strPath . '/empty.fallback'; if (isset($arrMapper[$strKey])) { $strCacheKey = $arrMapper[$strKey]; break; } } } } // Fall back to the first accepted language if ($strCacheKey === null) { $strCacheKey = \Environment::get('host') . '/empty.' . $arrLanguage[0]; } } else { $strCacheKey = \Environment::get('host') . '/' . \Environment::get('relativeRequest'); } // HOOK: add custom logic if (isset($GLOBALS['TL_HOOKS']['getCacheKey']) && is_array($GLOBALS['TL_HOOKS']['getCacheKey'])) { foreach ($GLOBALS['TL_HOOKS']['getCacheKey'] as $callback) { $strCacheKey = \System::importStatic($callback[0])->{$callback[1]}($strCacheKey); } } $blnFound = false; $strCacheFile = null; // Check for a mobile layout if (\Input::cookie('TL_VIEW') == 'mobile' || \Environment::get('agent')->mobile && \Input::cookie('TL_VIEW') != 'desktop') { $strMd5CacheKey = md5($strCacheKey . '.mobile'); $strCacheFile = $strCacheDir . '/contao/html/' . substr($strMd5CacheKey, 0, 1) . '/' . $strMd5CacheKey . '.html'; if (file_exists($strCacheFile)) { $blnFound = true; } } else { $strMd5CacheKey = md5($strCacheKey . '.desktop'); $strCacheFile = $strCacheDir . '/contao/html/' . substr($strMd5CacheKey, 0, 1) . '/' . $strMd5CacheKey . '.html'; if (file_exists($strCacheFile)) { $blnFound = true; } } // Check for a regular layout if (!$blnFound) { $strMd5CacheKey = md5($strCacheKey); $strCacheFile = $strCacheDir . '/contao/html/' . substr($strMd5CacheKey, 0, 1) . '/' . $strMd5CacheKey . '.html'; if (file_exists($strCacheFile)) { $blnFound = true; } } // Return if the file does not exist if (!$blnFound) { return null; } $expire = null; $content = null; $type = null; $files = null; $assets = null; // Include the file ob_start(); require_once $strCacheFile; // The file has expired if ($expire < time()) { ob_end_clean(); return null; } // Define the static URL constants (see #7914) define('TL_FILES_URL', $files); define('TL_ASSETS_URL', $assets); // Read the buffer $strBuffer = ob_get_clean(); /** @var AttributeBagInterface $session */ $session = \System::getContainer()->get('session')->getBag('contao_frontend'); // Session required to determine the referer $data = $session->all(); // Set the new referer if (!isset($_GET['pdf']) && !isset($_GET['file']) && !isset($_GET['id']) && $data['referer']['current'] != \Environment::get('requestUri')) { $data['referer']['last'] = $data['referer']['current']; $data['referer']['current'] = substr(\Environment::get('requestUri'), strlen(\Environment::get('path')) + 1); } // Store the session data $session->replace($data); // Load the default language file (see #2644) \System::loadLanguageFile('default'); // Replace the insert tags and then re-replace the request_token tag in case a form element has been loaded via insert tag $strBuffer = \Controller::replaceInsertTags($strBuffer, false); $strBuffer = str_replace(array('{{request_token}}', '[{]', '[}]'), array(REQUEST_TOKEN, '{{', '}}'), $strBuffer); // HOOK: allow to modify the compiled markup (see #4291 and #7457) if (isset($GLOBALS['TL_HOOKS']['modifyFrontendPage']) && is_array($GLOBALS['TL_HOOKS']['modifyFrontendPage'])) { foreach ($GLOBALS['TL_HOOKS']['modifyFrontendPage'] as $callback) { $strBuffer = \System::importStatic($callback[0])->{$callback[1]}($strBuffer, null); } } // Content type if (!$content) { $content = 'text/html'; } $response = new Response($strBuffer); // Send the status header (see #6585) if ($type == 'error_403') { $response->setStatusCode(Response::HTTP_FORBIDDEN); } elseif ($type == 'error_404') { $response->setStatusCode(Response::HTTP_NOT_FOUND); } $response->headers->set('Vary', 'User-Agent', false); $response->headers->set('Content-Type', $content . '; charset=' . \Config::get('characterSet')); // Send the cache headers if ($expire !== null && (\Config::get('cacheMode') == 'both' || \Config::get('cacheMode') == 'browser')) { $response->headers->set('Cache-Control', 'public, max-age=' . ($expire - time())); $response->headers->set('Pragma', 'public'); $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s', time()) . ' GMT'); $response->headers->set('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); } else { $response->headers->set('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); $response->headers->set('Pragma', 'no-cache'); $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT'); $response->headers->set('Expires', 'Fri, 06 Jun 1975 15:10:00 GMT'); } return $response; }