/** * Gets the label for a language, optionally replacing insert tags. * * @param string $language * @param bool $replaceInsertTags * * @return string */ public function get($language, $replaceInsertTags = true) { $language = strtolower($language); if (empty($this->map[$language])) { return strtoupper($language); } $value = $this->map[$language]; if ($replaceInsertTags) { $value = Controller::replaceInsertTags($value); } return $value; }
/** * Generate the page title with ##title## as placeholder for dynamic title * * @param PageModel $pageModel * * @return string */ protected function generatePageTitle(PageModel $pageModel) { $pageModel->loadDetails(); $layoutModel = LayoutModel::findByPk($pageModel->layout); if ($layoutModel === null) { return '##title##'; } $title = $layoutModel->titleTag ?: '{{page::pageTitle}} - {{page::rootPageTitle}}'; $title = str_replace('{{page::pageTitle}}', '##title##', $title); // Fake the global page object $GLOBALS['objPage'] = $pageModel; // Replace the insert tags $title = Controller::replaceInsertTags($title, false); // Remove the faked global page object unset($GLOBALS['objPage']); return $title; }
/** * 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; }
/** * @param $db * @param $doTable * @param $table * @return null|string */ public function setTitle($db, $doTable, $table) { // sorted by priority if (!$doTable) { return null; } $colsForTitle = $this->modules[$doTable]['title']; array_unshift($colsForTitle, 'ps_title'); $colsForTitle = $colsForTitle ? $colsForTitle : array(); // hook for custom title if ($this->modules[$doTable] && is_array($this->modules[$doTable]['setCustomTitle'])) { foreach ($this->modules[$doTable]['setCustomTitle'] as $callable) { $this->import($callable[0]); return $this->{$callable[0]}->{$callable[1]}($table, $db, $colsForTitle, $doTable); } } foreach ($colsForTitle as $title) { if (!$db[$title]) { continue; } $ct = deserialize($db[$title]); // check if value is serialize if (is_array($ct) && !empty($ct)) { $meta = Helper::parseStrForMeta($db[$title]); $db[$title] = $meta; } if ($db[$title] && (is_string($db[$title]) || is_numeric($db[$title]))) { $return = $db[$title]; $return = Controller::replaceInsertTags($return); $return = strip_tags($return); $return = trim($return); return $return; break; } } return null; }