/** * Generate the Google XML sitemaps * * @param integer $intId The root page ID */ public function generateSitemap($intId = 0) { $time = \Date::floorToMinute(); $objDatabase = \Database::getInstance(); $this->purgeXmlFiles(); // Only root pages should have sitemap names $objDatabase->execute("UPDATE tl_page SET createSitemap='', sitemapName='' WHERE type!='root'"); // Get a particular root page if ($intId > 0) { do { $objRoot = $objDatabase->prepare("SELECT * FROM tl_page WHERE id=?")->limit(1)->execute($intId); if ($objRoot->numRows < 1) { break; } $intId = $objRoot->pid; } while ($objRoot->type != 'root' && $intId > 0); // Make sure the page is published if (!$objRoot->published || $objRoot->start != '' && $objRoot->start > $time || $objRoot->stop != '' && $objRoot->stop <= $time + 60) { return; } // Check the sitemap name if (!$objRoot->createSitemap || !$objRoot->sitemapName) { return; } $objRoot->reset(); } else { $objRoot = $objDatabase->execute("SELECT id, language, sitemapName FROM tl_page WHERE type='root' AND createSitemap='1' AND sitemapName!='' AND (start='' OR start<='{$time}') AND (stop='' OR stop>'" . ($time + 60) . "') AND published='1'"); } // Return if there are no pages if ($objRoot->numRows < 1) { return; } // Create the XML file while ($objRoot->next()) { $objFile = new \File('web/share/' . $objRoot->sitemapName . '.xml'); $objFile->truncate(); $objFile->append('<?xml version="1.0" encoding="UTF-8"?>'); $objFile->append('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'); // Find the searchable pages $arrPages = \Backend::findSearchablePages($objRoot->id, '', true); // HOOK: take additional pages if (isset($GLOBALS['TL_HOOKS']['getSearchablePages']) && is_array($GLOBALS['TL_HOOKS']['getSearchablePages'])) { foreach ($GLOBALS['TL_HOOKS']['getSearchablePages'] as $callback) { $this->import($callback[0]); $arrPages = $this->{$callback[0]}->{$callback[1]}($arrPages, $objRoot->id, true, $objRoot->language); } } // Add pages foreach ($arrPages as $strUrl) { $strUrl = rawurlencode($strUrl); $strUrl = str_replace(array('%2F', '%3F', '%3D', '%26', '%3A//'), array('/', '?', '=', '&', '://'), $strUrl); $strUrl = ampersand($strUrl, true); $objFile->append(' <url><loc>' . $strUrl . '</loc></url>'); } $objFile->append('</urlset>'); $objFile->close(); // Add a log entry $this->log('Generated sitemap "' . $objRoot->sitemapName . '.xml"', __METHOD__, TL_CRON); } }
/** * Generate the combined file and return its path * * @param string $strUrl An optional URL to prepend * * @return string The path to the combined file */ public function getCombinedFile($strUrl = null) { if ($strUrl === null) { $strUrl = TL_ASSETS_URL; } $strTarget = substr($this->strMode, 1); $strKey = substr(md5($this->strKey), 0, 12); // Do not combine the files in debug mode (see #6450) if (\Config::get('debugMode')) { $return = array(); foreach ($this->arrFiles as $arrFile) { $content = file_get_contents(TL_ROOT . '/' . $arrFile['name']); // Compile SCSS/LESS files into temporary files if ($arrFile['extension'] == self::SCSS || $arrFile['extension'] == self::LESS) { $strPath = 'assets/' . $strTarget . '/' . str_replace('/', '_', $arrFile['name']) . $this->strMode; $objFile = new \File($strPath, true); $objFile->write($this->handleScssLess($content, $arrFile)); $objFile->close(); $return[] = $strPath; } else { $name = $arrFile['name']; // Add the media query (see #7070) if ($arrFile['media'] != '' && $arrFile['media'] != 'all' && strpos($content, '@media') === false) { $name .= '" media="' . $arrFile['media']; } $return[] = $name; } } if ($this->strMode == self::JS) { return implode('"></script><script src="', $return); } else { return implode('"><link rel="stylesheet" href="', $return); } } // Load the existing file if (file_exists(TL_ROOT . '/assets/' . $strTarget . '/' . $strKey . $this->strMode)) { return $strUrl . 'assets/' . $strTarget . '/' . $strKey . $this->strMode; } // Create the file $objFile = new \File('assets/' . $strTarget . '/' . $strKey . $this->strMode, true); $objFile->truncate(); foreach ($this->arrFiles as $arrFile) { $content = file_get_contents(TL_ROOT . '/' . $arrFile['name']); // HOOK: modify the file content if (isset($GLOBALS['TL_HOOKS']['getCombinedFile']) && is_array($GLOBALS['TL_HOOKS']['getCombinedFile'])) { foreach ($GLOBALS['TL_HOOKS']['getCombinedFile'] as $callback) { $this->import($callback[0]); $content = $this->{$callback}[0]->{$callback}[1]($content, $strKey, $this->strMode, $arrFile); } } if ($arrFile['extension'] == self::CSS) { $content = $this->handleCss($content, $arrFile); } elseif ($arrFile['extension'] == self::SCSS || $arrFile['extension'] == self::LESS) { $content = $this->handleScssLess($content, $arrFile); } $objFile->append($content); } unset($content); $objFile->close(); // Create a gzipped version if (\Config::get('gzipScripts') && function_exists('gzencode')) { \File::putContent('assets/' . $strTarget . '/' . $strKey . $this->strMode . '.gz', gzencode(file_get_contents(TL_ROOT . '/assets/' . $strTarget . '/' . $strKey . $this->strMode), 9)); } return $strUrl . 'assets/' . $strTarget . '/' . $strKey . $this->strMode; }
/** * 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'); } } }
/** * Generate the combined file and return its path * * @param string $strUrl An optional URL to prepend * * @return string The path to the combined file */ protected function getCombinedFileUrl($strUrl = null) { if ($strUrl === null) { $strUrl = TL_ASSETS_URL; } $strTarget = substr($this->strMode, 1); $strKey = substr(md5($this->strKey), 0, 12); // Load the existing file if (file_exists(TL_ROOT . '/assets/' . $strTarget . '/' . $strKey . $this->strMode)) { return $strUrl . 'assets/' . $strTarget . '/' . $strKey . $this->strMode; } // Create the file $objFile = new \File('assets/' . $strTarget . '/' . $strKey . $this->strMode); $objFile->truncate(); foreach ($this->arrFiles as $arrFile) { $content = file_get_contents(TL_ROOT . '/' . $arrFile['name']); // HOOK: modify the file content if (isset($GLOBALS['TL_HOOKS']['getCombinedFile']) && is_array($GLOBALS['TL_HOOKS']['getCombinedFile'])) { foreach ($GLOBALS['TL_HOOKS']['getCombinedFile'] as $callback) { $this->import($callback[0]); $content = $this->{$callback[0]}->{$callback[1]}($content, $strKey, $this->strMode, $arrFile); } } if ($arrFile['extension'] == self::CSS) { $content = $this->handleCss($content, $arrFile); } elseif ($arrFile['extension'] == self::SCSS || $arrFile['extension'] == self::LESS) { $content = $this->handleScssLess($content, $arrFile); } $objFile->append($content); } unset($content); $objFile->close(); // Create a gzipped version if (\Config::get('gzipScripts') && function_exists('gzencode')) { \File::putContent('assets/' . $strTarget . '/' . $strKey . $this->strMode . '.gz', gzencode(file_get_contents(TL_ROOT . '/assets/' . $strTarget . '/' . $strKey . $this->strMode), 9)); } return $strUrl . 'assets/' . $strTarget . '/' . $strKey . $this->strMode; }