/**
  * Add new jquery Version
  */
 public function changeVersion($objPage, $objLayout, $self)
 {
     // check if jswitcher is enabled
     if (!$this->jswitcher) {
         return;
     }
     // jQuery scripts
     if ($objLayout->jSource == 'j_googleapis' || $objLayout->jSource == 'j_fallback') {
         $blnXhtml = $objPage->outputFormat == 'xhtml';
         $self->Template->mooScripts .= \Template::generateScriptTag((\Environment::get('ssl') ? 'https://' : 'http://') . 'code.jquery.com/jquery-' . $objLayout->jQueryVersion . '.min.js', $blnXhtml) . "\n";
         // Local fallback (thanks to DyaGa)
         if ($objLayout->jSource == 'j_fallback') {
             $self->Template->mooScripts .= \Template::generateInlineScript('window.jQuery || document.write(\'<script src="' . $GLOBALS['TL_JQUERY_VERSIONS'][$objLayout->jQueryVersion]['local'] . '">\\x3C/script>\')', $blnXhtml) . "\n";
         }
     } else {
         $arrAppendJs = array($GLOBALS['TL_JQUERY_VERSIONS'][$objLayout->jQueryVersion]['local'] . '|static');
         $GLOBALS['TL_JAVASCRIPT'] = array_merge($arrAppendJs, $GLOBALS['TL_JAVASCRIPT']);
     }
     //remove mootools-request.js
     $findMooReq = array_search('assets/mootools/core/' . MOOTOOLS . '/mootools-request.js|static', $GLOBALS['TL_JAVASCRIPT']);
     if ($findMooReq !== false) {
         unset($GLOBALS['TL_JAVASCRIPT'][$findMooReq]);
     }
     //restore old addJquery variable
     $objLayout->addJQuery = $objLayout->addJQueryTmp;
 }
 /**
  * Parse the template file, add the TinyMCE configuration and print it to the screen
  *
  * @throws \Exception
  */
 public function output()
 {
     // User agent class (see #3074 and #6277)
     $this->ua = \Environment::get('agent')->class;
     // Style sheets
     if (!empty($GLOBALS['TL_CSS']) && is_array($GLOBALS['TL_CSS'])) {
         $strStyleSheets = '';
         foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             $strStyleSheets .= \Template::generateStyleTag($this->addStaticUrlTo($stylesheet), $options->media);
         }
         $this->stylesheets = $strStyleSheets;
     }
     // Add the debug style sheet
     if (\Config::get('debugMode')) {
         $this->stylesheets .= '<link rel="stylesheet" href="' . $this->addStaticUrlTo('assets/contao/css/debug.css') . '">' . "\n";
     }
     // JavaScripts
     if (!empty($GLOBALS['TL_JAVASCRIPT']) && is_array($GLOBALS['TL_JAVASCRIPT'])) {
         $strJavaScripts = '';
         foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript) {
             $options = \StringUtil::resolveFlaggedUrl($javascript);
             $strJavaScripts .= \Template::generateScriptTag($this->addStaticUrlTo($javascript), false, $options->async) . "\n";
         }
         $this->javascripts = $strJavaScripts;
     }
     // MooTools scripts (added at the page bottom)
     if (!empty($GLOBALS['TL_MOOTOOLS']) && is_array($GLOBALS['TL_MOOTOOLS'])) {
         $strMootools = '';
         foreach (array_unique($GLOBALS['TL_MOOTOOLS']) as $script) {
             $strMootools .= "\n" . trim($script) . "\n";
         }
         $this->mootools = $strMootools;
     }
     $strBuffer = $this->parse();
     // HOOK: add custom output filter
     if (isset($GLOBALS['TL_HOOKS']['outputBackendTemplate']) && is_array($GLOBALS['TL_HOOKS']['outputBackendTemplate'])) {
         foreach ($GLOBALS['TL_HOOKS']['outputBackendTemplate'] as $callback) {
             $this->import($callback[0]);
             $strBuffer = $this->{$callback[0]}->{$callback[1]}($strBuffer, $this->strTemplate);
         }
     }
     $this->strBuffer = $strBuffer;
     parent::output();
 }
 /**
  * Compile the template
  *
  * @internal
  */
 protected function compile()
 {
     // User agent class (see #3074 and #6277)
     $this->ua = \Environment::get('agent')->class;
     // Style sheets
     if (!empty($GLOBALS['TL_CSS']) && is_array($GLOBALS['TL_CSS'])) {
         $strStyleSheets = '';
         foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             $strStyleSheets .= \Template::generateStyleTag($this->addStaticUrlTo($stylesheet), $options->media);
         }
         $this->stylesheets = $strStyleSheets;
     }
     // JavaScripts
     if (!empty($GLOBALS['TL_JAVASCRIPT']) && is_array($GLOBALS['TL_JAVASCRIPT'])) {
         $strJavaScripts = '';
         foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript) {
             $options = \StringUtil::resolveFlaggedUrl($javascript);
             $strJavaScripts .= \Template::generateScriptTag($this->addStaticUrlTo($javascript), $options->async) . "\n";
         }
         $this->javascripts = $strJavaScripts;
     }
     // MooTools scripts (added at the page bottom)
     if (!empty($GLOBALS['TL_MOOTOOLS']) && is_array($GLOBALS['TL_MOOTOOLS'])) {
         $strMootools = '';
         foreach (array_unique($GLOBALS['TL_MOOTOOLS']) as $script) {
             $strMootools .= "\n" . trim($script) . "\n";
         }
         $this->mootools = $strMootools;
     }
     $strBuffer = $this->parse();
     $strBuffer = static::replaceOldBePaths($strBuffer);
     // HOOK: add custom output filter
     if (isset($GLOBALS['TL_HOOKS']['outputBackendTemplate']) && is_array($GLOBALS['TL_HOOKS']['outputBackendTemplate'])) {
         foreach ($GLOBALS['TL_HOOKS']['outputBackendTemplate'] as $callback) {
             $this->import($callback[0]);
             $strBuffer = $this->{$callback}[0]->{$callback}[1]($strBuffer, $this->strTemplate);
         }
     }
     $this->strBuffer = $strBuffer;
     parent::compile();
 }
Example #4
0
 /**
  * Create a new template
  *
  * @param \PageModel   $objPage
  * @param \LayoutModel $objLayout
  */
 protected function createTemplate($objPage, $objLayout)
 {
     $blnXhtml = $objPage->outputFormat == 'xhtml';
     /** @var \FrontendTemplate|object $objTemplate */
     $objTemplate = new \FrontendTemplate($objPage->template);
     $this->Template = $objTemplate;
     // Generate the DTD
     if ($blnXhtml) {
         if ($objPage->outputVariant == 'strict') {
             $this->Template->doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n";
         } else {
             $this->Template->doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";
         }
     }
     $arrFramework = deserialize($objLayout->framework);
     $this->Template->viewport = '';
     $this->Template->framework = '';
     // Generate the CSS framework
     if (is_array($arrFramework) && in_array('layout.css', $arrFramework)) {
         $strFramework = '';
         if (in_array('responsive.css', $arrFramework)) {
             $this->Template->viewport = '<meta name="viewport" content="width=device-width,initial-scale=1.0"' . ($blnXhtml ? ' />' : '>') . "\n";
         }
         // Wrapper
         if ($objLayout->static) {
             $arrSize = deserialize($objLayout->width);
             if (isset($arrSize['value']) && $arrSize['value'] != '' && $arrSize['value'] >= 0) {
                 $arrMargin = array('left' => '0 auto 0 0', 'center' => '0 auto', 'right' => '0 0 0 auto');
                 $strFramework .= sprintf('#wrapper{width:%s;margin:%s}', $arrSize['value'] . $arrSize['unit'], $arrMargin[$objLayout->align]);
             }
         }
         // Header
         if ($objLayout->rows == '2rwh' || $objLayout->rows == '3rw') {
             $arrSize = deserialize($objLayout->headerHeight);
             if (isset($arrSize['value']) && $arrSize['value'] != '' && $arrSize['value'] >= 0) {
                 $strFramework .= sprintf('#header{height:%s}', $arrSize['value'] . $arrSize['unit']);
             }
         }
         $strContainer = '';
         // Left column
         if ($objLayout->cols == '2cll' || $objLayout->cols == '3cl') {
             $arrSize = deserialize($objLayout->widthLeft);
             if (isset($arrSize['value']) && $arrSize['value'] != '' && $arrSize['value'] >= 0) {
                 $strFramework .= sprintf('#left{width:%s;right:%s}', $arrSize['value'] . $arrSize['unit'], $arrSize['value'] . $arrSize['unit']);
                 $strContainer .= sprintf('padding-left:%s;', $arrSize['value'] . $arrSize['unit']);
             }
         }
         // Right column
         if ($objLayout->cols == '2clr' || $objLayout->cols == '3cl') {
             $arrSize = deserialize($objLayout->widthRight);
             if (isset($arrSize['value']) && $arrSize['value'] != '' && $arrSize['value'] >= 0) {
                 $strFramework .= sprintf('#right{width:%s}', $arrSize['value'] . $arrSize['unit']);
                 $strContainer .= sprintf('padding-right:%s;', $arrSize['value'] . $arrSize['unit']);
             }
         }
         // Main column
         if ($strContainer != '') {
             $strFramework .= sprintf('#container{%s}', substr($strContainer, 0, -1));
         }
         // Footer
         if ($objLayout->rows == '2rwf' || $objLayout->rows == '3rw') {
             $arrSize = deserialize($objLayout->footerHeight);
             if (isset($arrSize['value']) && $arrSize['value'] != '' && $arrSize['value'] >= 0) {
                 $strFramework .= sprintf('#footer{height:%s}', $arrSize['value'] . $arrSize['unit']);
             }
         }
         // Add the layout specific CSS
         if ($strFramework != '') {
             $this->Template->framework = \Template::generateInlineStyle($strFramework, $blnXhtml) . "\n";
         }
     }
     // Overwrite the viewport tag (see #6251)
     if ($objLayout->viewport != '') {
         $this->Template->viewport = '<meta name="viewport" content="' . $objLayout->viewport . '"' . ($blnXhtml ? ' />' : '>') . "\n";
     }
     $this->Template->mooScripts = '';
     // Make sure TL_JAVASCRIPT exists (see #4890)
     if (isset($GLOBALS['TL_JAVASCRIPT']) && is_array($GLOBALS['TL_JAVASCRIPT'])) {
         $arrAppendJs = $GLOBALS['TL_JAVASCRIPT'];
         $GLOBALS['TL_JAVASCRIPT'] = array();
     } else {
         $arrAppendJs = array();
         $GLOBALS['TL_JAVASCRIPT'] = array();
     }
     // jQuery scripts
     if ($objLayout->addJQuery) {
         if ($objLayout->jSource == 'j_googleapis' || $objLayout->jSource == 'j_fallback') {
             $this->Template->mooScripts .= \Template::generateScriptTag('//code.jquery.com/jquery-' . $GLOBALS['TL_ASSETS']['JQUERY'] . '.min.js', $blnXhtml) . "\n";
             // Local fallback (thanks to DyaGa)
             if ($objLayout->jSource == 'j_fallback') {
                 $this->Template->mooScripts .= \Template::generateInlineScript('window.jQuery || document.write(\'<script src="' . TL_ASSETS_URL . 'assets/jquery/core/' . $GLOBALS['TL_ASSETS']['JQUERY'] . '/jquery.min.js">\\x3C/script>\')', $blnXhtml) . "\n";
             }
         } else {
             $GLOBALS['TL_JAVASCRIPT'][] = 'assets/jquery/core/' . $GLOBALS['TL_ASSETS']['JQUERY'] . '/jquery.min.js|static';
         }
     }
     // MooTools scripts
     if ($objLayout->addMooTools) {
         if ($objLayout->mooSource == 'moo_googleapis' || $objLayout->mooSource == 'moo_fallback') {
             $this->Template->mooScripts .= \Template::generateScriptTag('//ajax.googleapis.com/ajax/libs/mootools/' . $GLOBALS['TL_ASSETS']['MOOTOOLS'] . '/mootools-yui-compressed.js', $blnXhtml) . "\n";
             // Local fallback (thanks to DyaGa)
             if ($objLayout->mooSource == 'moo_fallback') {
                 $this->Template->mooScripts .= \Template::generateInlineScript('window.MooTools || document.write(\'<script src="' . TL_ASSETS_URL . 'assets/mootools/core/' . $GLOBALS['TL_ASSETS']['MOOTOOLS'] . '/mootools-core.js">\\x3C/script>\')', $blnXhtml) . "\n";
             }
             $GLOBALS['TL_JAVASCRIPT'][] = 'assets/mootools/core/' . $GLOBALS['TL_ASSETS']['MOOTOOLS'] . '/mootools-more.js|static';
             $GLOBALS['TL_JAVASCRIPT'][] = 'assets/mootools/core/' . $GLOBALS['TL_ASSETS']['MOOTOOLS'] . '/mootools-mobile.js|static';
         } else {
             $GLOBALS['TL_JAVASCRIPT'][] = 'assets/mootools/core/' . $GLOBALS['TL_ASSETS']['MOOTOOLS'] . '/mootools.js|static';
         }
     }
     // Load MooTools core for the debug bar (see #5195)
     if (\Config::get('debugMode') && !$objLayout->addMooTools) {
         $GLOBALS['TL_JAVASCRIPT'][] = 'assets/mootools/core/' . $GLOBALS['TL_ASSETS']['MOOTOOLS'] . '/mootools-core.js|static';
     }
     // Picturefill
     if ($objLayout->picturefill) {
         $GLOBALS['TL_JAVASCRIPT'][] = 'assets/respimage/' . $GLOBALS['TL_ASSETS']['RESPIMAGE'] . '/respimage.min.js|static';
     }
     // Check whether TL_APPEND_JS exists (see #4890)
     if (!empty($arrAppendJs)) {
         $GLOBALS['TL_JAVASCRIPT'] = array_merge($GLOBALS['TL_JAVASCRIPT'], $arrAppendJs);
     }
     // Initialize the sections
     $this->Template->header = '';
     $this->Template->left = '';
     $this->Template->main = '';
     $this->Template->right = '';
     $this->Template->footer = '';
     // Initialize the custom layout sections
     $this->Template->sections = array();
     $this->Template->sPosition = $objLayout->sPosition;
     // Default settings
     $this->Template->layout = $objLayout;
     $this->Template->language = $GLOBALS['TL_LANGUAGE'];
     $this->Template->charset = \Config::get('characterSet');
     $this->Template->base = \Environment::get('base');
     $this->Template->disableCron = \Config::get('disableCron');
     $this->Template->cronTimeout = $this->getCronTimeout();
     $this->Template->isRTL = false;
 }
 /**
  * Replace the dynamic script tags (see #4203)
  *
  * @param string $strBuffer The string with the tags to be replaced
  *
  * @return string The string with the replaced tags
  */
 public static function replaceDynamicScriptTags($strBuffer)
 {
     // HOOK: add custom logic
     if (isset($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags']) && is_array($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'])) {
         foreach ($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'] as $callback) {
             $strBuffer = static::importStatic($callback[0])->{$callback[1]}($strBuffer);
         }
     }
     /** @var \PageModel $objPage */
     global $objPage;
     $arrReplace = array();
     $blnXhtml = $objPage->outputFormat == 'xhtml';
     $strScripts = '';
     // Add the internal jQuery scripts
     if (!empty($GLOBALS['TL_JQUERY']) && is_array($GLOBALS['TL_JQUERY'])) {
         foreach (array_unique($GLOBALS['TL_JQUERY']) as $script) {
             $strScripts .= "\n" . trim($script) . "\n";
         }
     }
     $arrReplace['[[TL_JQUERY]]'] = $strScripts;
     $strScripts = '';
     // Add the internal MooTools scripts
     if (!empty($GLOBALS['TL_MOOTOOLS']) && is_array($GLOBALS['TL_MOOTOOLS'])) {
         foreach (array_unique($GLOBALS['TL_MOOTOOLS']) as $script) {
             $strScripts .= "\n" . trim($script) . "\n";
         }
     }
     $arrReplace['[[TL_MOOTOOLS]]'] = $strScripts;
     $strScripts = '';
     // Add the internal <body> tags
     if (!empty($GLOBALS['TL_BODY']) && is_array($GLOBALS['TL_BODY'])) {
         foreach (array_unique($GLOBALS['TL_BODY']) as $script) {
             $strScripts .= trim($script) . "\n";
         }
     }
     // Add the syntax highlighter scripts
     if (!empty($GLOBALS['TL_HIGHLIGHTER']) && is_array($GLOBALS['TL_HIGHLIGHTER'])) {
         $objCombiner = new \Combiner();
         foreach (array_unique($GLOBALS['TL_HIGHLIGHTER']) as $script) {
             $objCombiner->add($script);
         }
         $strScripts .= "\n" . \Template::generateScriptTag($objCombiner->getCombinedFile(), $blnXhtml);
         $strScripts .= "\n" . \Template::generateInlineScript('SyntaxHighlighter.defaults.toolbar=false;SyntaxHighlighter.all()', $blnXhtml) . "\n";
     }
     // Command scheduler
     if (!\Config::get('disableCron')) {
         $strScripts .= "\n" . \Template::generateInlineScript('setTimeout(function(){var e=function(e,t){try{var n=new XMLHttpRequest}catch(r){return}n.open("GET",e,!0),n.onreadystatechange=function(){this.readyState==4&&this.status==200&&typeof t=="function"&&t(this.responseText)},n.send()},t="system/cron/cron.";e(t+"txt",function(n){parseInt(n||0)<Math.round(+(new Date)/1e3)-' . \Frontend::getCronTimeout() . '&&e(t+"php")})},5e3);', $blnXhtml) . "\n";
     }
     $arrReplace['[[TL_BODY]]'] = $strScripts;
     $strScripts = '';
     $objCombiner = new \Combiner();
     // Add the CSS framework style sheets
     if (!empty($GLOBALS['TL_FRAMEWORK_CSS']) && is_array($GLOBALS['TL_FRAMEWORK_CSS'])) {
         foreach (array_unique($GLOBALS['TL_FRAMEWORK_CSS']) as $stylesheet) {
             $objCombiner->add($stylesheet);
         }
     }
     // Add the internal style sheets
     if (!empty($GLOBALS['TL_CSS']) && is_array($GLOBALS['TL_CSS'])) {
         foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             if ($options->static) {
                 if ($options->mtime === null) {
                     $options->mtime = filemtime(TL_ROOT . '/' . $stylesheet);
                 }
                 $objCombiner->add($stylesheet, $options->mtime, $options->media);
             } else {
                 $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media, $blnXhtml) . "\n";
             }
         }
     }
     // Add the user style sheets
     if (!empty($GLOBALS['TL_USER_CSS']) && is_array($GLOBALS['TL_USER_CSS'])) {
         foreach (array_unique($GLOBALS['TL_USER_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             if ($options->static) {
                 if ($options->mtime === null) {
                     $options->mtime = filemtime(TL_ROOT . '/' . $stylesheet);
                 }
                 $objCombiner->add($stylesheet, $options->mtime, $options->media);
             } else {
                 $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media, $blnXhtml) . "\n";
             }
         }
     }
     // Create the aggregated style sheet
     if ($objCombiner->hasEntries()) {
         $strScripts .= \Template::generateStyleTag($objCombiner->getCombinedFile(), 'all', $blnXhtml) . "\n";
     }
     $arrReplace['[[TL_CSS]]'] = $strScripts;
     $strScripts = '';
     // Add the internal scripts
     if (!empty($GLOBALS['TL_JAVASCRIPT']) && is_array($GLOBALS['TL_JAVASCRIPT'])) {
         $objCombiner = new \Combiner();
         $objCombinerAsync = new \Combiner();
         foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript) {
             $options = \StringUtil::resolveFlaggedUrl($javascript);
             if ($options->static) {
                 if ($options->mtime === null) {
                     $options->mtime = filemtime(TL_ROOT . '/' . $javascript);
                 }
                 $options->async ? $objCombinerAsync->add($javascript, $options->mtime) : $objCombiner->add($javascript, $options->mtime);
             } else {
                 $strScripts .= \Template::generateScriptTag(static::addStaticUrlTo($javascript), $blnXhtml, $options->async) . "\n";
             }
         }
         // Create the aggregated script and add it before the non-static scripts (see #4890)
         if ($objCombiner->hasEntries()) {
             $strScripts = \Template::generateScriptTag($objCombiner->getCombinedFile(), $blnXhtml) . "\n" . $strScripts;
         }
         if ($objCombinerAsync->hasEntries()) {
             $strScripts = \Template::generateScriptTag($objCombinerAsync->getCombinedFile(), $blnXhtml, true) . "\n" . $strScripts;
         }
     }
     // Add the internal <head> tags
     if (!empty($GLOBALS['TL_HEAD']) && is_array($GLOBALS['TL_HEAD'])) {
         foreach (array_unique($GLOBALS['TL_HEAD']) as $head) {
             $strScripts .= trim($head) . "\n";
         }
     }
     $arrReplace['[[TL_HEAD]]'] = $strScripts;
     return str_replace(array_keys($arrReplace), array_values($arrReplace), $strBuffer);
 }