function testArgsInUrls() { // Clear previous requirements Requirements::clear(); // clearing all previously generated requirements (just in case) Requirements::clear_combined_files(); Requirements::delete_combined_files('RequirementsTest_bc.js'); Requirements::javascript(SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.js?test=1&test=2&test=3'); Requirements::css(SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.css?test=1&test=2&test=3'); $html = Requirements::includeInHTML(false, self::$html_template); /* Javascript has correct path */ $this->assertTrue((bool) preg_match('/src=".*\\/RequirementsTest_a\\.js\\?m=\\d\\d+&test=1&test=2&test=3/', $html), 'javascript has correct path'); /* CSS has correct path */ $this->assertTrue((bool) preg_match('/href=".*\\/RequirementsTest_a\\.css\\?m=\\d\\d+&test=1&test=2&test=3/', $html), 'css has correct path'); }
/** * The process() method handles the "meat" of the template processing. * It takes care of caching the output (via {@link SS_Cache}), * as well as replacing the special "$Content" and "$Layout" * placeholders with their respective subtemplates. * The method injects extra HTML in the header via {@link Requirements::includeInHTML()}. * * Note: You can call this method indirectly by {@link ViewableData->renderWith()}. * * @param ViewableData $item * @param SS_Cache $cache Optional cache backend * @return String Parsed template output. */ public function process($item, $arguments = null) { SSViewer::$topLevel[] = $item; if ($arguments && $arguments instanceof Zend_Cache_Core) { Deprecation::notice('3.0', 'Use setPartialCacheStore to override the partial cache storage backend, ' . 'the second argument to process is now an array of variables.'); $this->setPartialCacheStore($arguments); $arguments = null; } if (isset($this->chosenTemplates['main'])) { $template = $this->chosenTemplates['main']; } else { $keys = array_keys($this->chosenTemplates); $key = reset($keys); $template = $this->chosenTemplates[$key]; } $cacheFile = TEMP_FOLDER . "/.cache" . str_replace(array('\\', '/', ':'), '.', Director::makeRelative(realpath($template))); $lastEdited = filemtime($template); if (!file_exists($cacheFile) || filemtime($cacheFile) < $lastEdited || isset($_GET['flush'])) { $content = file_get_contents($template); $content = SSViewer::parseTemplateContent($content, $template); $fh = fopen($cacheFile, 'w'); fwrite($fh, $content); fclose($fh); } $underlay = array('I18NNamespace' => basename($template)); // Makes the rendered sub-templates available on the parent item, // through $Content and $Layout placeholders. foreach (array('Content', 'Layout') as $subtemplate) { if (isset($this->chosenTemplates[$subtemplate])) { $subtemplateViewer = new SSViewer($this->chosenTemplates[$subtemplate]); $subtemplateViewer->setPartialCacheStore($this->getPartialCacheStore()); $underlay[$subtemplate] = $subtemplateViewer->process($item, $arguments); } } $val = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, $underlay); $output = Requirements::includeInHTML($template, $val); array_pop(SSViewer::$topLevel); // If we have our crazy base tag, then fix # links referencing the current page. if ($this->rewriteHashlinks && self::$options['rewriteHashlinks']) { if (strpos($output, '<base') !== false) { if (SSViewer::$options['rewriteHashlinks'] === 'php') { $thisURLRelativeToBase = "<?php echo strip_tags(\$_SERVER['REQUEST_URI']); ?>"; } else { $thisURLRelativeToBase = strip_tags($_SERVER['REQUEST_URI']); } $output = preg_replace('/(<a[^>]+href *= *)"#/i', '\\1"' . $thisURLRelativeToBase . '#', $output); } } return $output; }
/** * The process() method handles the "meat" of the template processing. * * It takes care of caching the output (via {@link SS_Cache}), as well as * replacing the special "$Content" and "$Layout" placeholders with their * respective subtemplates. * * The method injects extra HTML in the header via {@link Requirements::includeInHTML()}. * * Note: You can call this method indirectly by {@link ViewableData->renderWith()}. * * @param ViewableData $item * @param array|null $arguments - arguments to an included template * @param Object $inheritedScope - the current scope of a parent template including a sub-template * * @return HTMLText Parsed template output. */ public function process($item, $arguments = null, $inheritedScope = null) { SSViewer::$topLevel[] = $item; if (isset($this->chosenTemplates['main'])) { $template = $this->chosenTemplates['main']; } else { $keys = array_keys($this->chosenTemplates); $key = reset($keys); $template = $this->chosenTemplates[$key]; } $cacheFile = TEMP_FOLDER . "/.cache" . str_replace(array('\\', '/', ':'), '.', Director::makeRelative(realpath($template))); $lastEdited = filemtime($template); if (!file_exists($cacheFile) || filemtime($cacheFile) < $lastEdited) { $content = file_get_contents($template); $content = $this->parseTemplateContent($content, $template); $fh = fopen($cacheFile, 'w'); fwrite($fh, $content); fclose($fh); } $underlay = array('I18NNamespace' => basename($template)); // Makes the rendered sub-templates available on the parent item, // through $Content and $Layout placeholders. foreach (array('Content', 'Layout') as $subtemplate) { if (isset($this->chosenTemplates[$subtemplate])) { $subtemplateViewer = new SSViewer($this->chosenTemplates[$subtemplate], $this->parser); $subtemplateViewer->includeRequirements(false); $subtemplateViewer->setPartialCacheStore($this->getPartialCacheStore()); $underlay[$subtemplate] = $subtemplateViewer->process($item, $arguments); } } $output = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, $underlay, $inheritedScope); if ($this->includeRequirements) { $output = Requirements::includeInHTML($template, $output); } array_pop(SSViewer::$topLevel); // If we have our crazy base tag, then fix # links referencing the current page. $rewrite = Config::inst()->get('SSViewer', 'rewrite_hash_links'); if ($this->rewriteHashlinks && $rewrite) { if (strpos($output, '<base') !== false) { if ($rewrite === 'php') { $thisURLRelativeToBase = "<?php echo Convert::raw2att(preg_replace(\"/^(\\\\/)+/\", \"/\", \$_SERVER['REQUEST_URI'])); ?>"; } else { $thisURLRelativeToBase = Convert::raw2att(preg_replace("/^(\\/)+/", "/", $_SERVER['REQUEST_URI'])); } $output = preg_replace('/(<a[^>]+href *= *)"#/i', '\\1"' . $thisURLRelativeToBase . '#', $output); } } return DBField::create_field('HTMLText', $output, null, array('shortcodes' => false)); }
/** * The process() method handles the "meat" of the template processing. */ public function process($item) { SSViewer::$topLevel[] = $item; if(isset($this->chosenTemplates['main'])) { $template = $this->chosenTemplates['main']; } else { $template = $this->chosenTemplates[ reset($dummy = array_keys($this->chosenTemplates)) ]; } if(isset($_GET['debug_profile'])) Profiler::mark("SSViewer::process", " for $template"); $cacheFile = TEMP_FOLDER . "/.cache" . str_replace(array('\\','/',':'),'.',realpath($template)); $lastEdited = filemtime($template); if(!file_exists($cacheFile) || filemtime($cacheFile) < $lastEdited || isset($_GET['flush'])) { if(isset($_GET['debug_profile'])) Profiler::mark("SSViewer::process - compile", " for $template"); $content = file_get_contents($template); $content = SSViewer::parseTemplateContent($content, $template); $fh = fopen($cacheFile,'w'); fwrite($fh, $content); fclose($fh); if(isset($_GET['debug_profile'])) Profiler::unmark("SSViewer::process - compile", " for $template"); } if(isset($_GET['showtemplate']) && !Director::isLive()) { $lines = file($cacheFile); echo "<h2>Template: $cacheFile</h2>"; echo "<pre>"; foreach($lines as $num => $line) { echo str_pad($num+1,5) . htmlentities($line); } echo "</pre>"; } foreach(array('Content', 'Layout') as $subtemplate) { if(isset($this->chosenTemplates[$subtemplate])) { $subtemplateViewer = new SSViewer($this->chosenTemplates[$subtemplate]); $item = $item->customise(array( $subtemplate => $subtemplateViewer->process($item) )); } } $itemStack = array(); $val = ""; include($cacheFile); $output = $val; $output = Requirements::includeInHTML($template, $output); array_pop(SSViewer::$topLevel); if(isset($_GET['debug_profile'])) Profiler::unmark("SSViewer::process", " for $template"); // If we have our crazy base tag, then fix # links referencing the current page. if(strpos($output, '<base') !== false) { $thisURLRelativeToBase = Director::makeRelative(Director::absoluteURL($_SERVER['REQUEST_URI'])); $output = preg_replace('/(<a[^>+]href *= *")#/i', '\\1' . $thisURLRelativeToBase . '#', $output); } return $output; }
/** * The process() method handles the "meat" of the template processing. * It takes care of caching the output (via {@link SS_Cache}), * as well as replacing the special "$Content" and "$Layout" * placeholders with their respective subtemplates. * The method injects extra HTML in the header via {@link Requirements::includeInHTML()}. * * Note: You can call this method indirectly by {@link ViewableData->renderWith()}. * * @param ViewableData $item * @param SS_Cache $cache Optional cache backend * @return String Parsed template output. */ public function process($item, $cache = null) { SSViewer::$topLevel[] = $item; if (!$cache) { $cache = SS_Cache::factory('cacheblock'); } if (isset($this->chosenTemplates['main'])) { $template = $this->chosenTemplates['main']; } else { $template = $this->chosenTemplates[reset($dummy = array_keys($this->chosenTemplates))]; } if (isset($_GET['debug_profile'])) { Profiler::mark("SSViewer::process", " for {$template}"); } $cacheFile = TEMP_FOLDER . "/.cache" . str_replace(array('\\', '/', ':'), '.', realpath($template)); $lastEdited = filemtime($template); if (!file_exists($cacheFile) || filemtime($cacheFile) < $lastEdited || isset($_GET['flush'])) { if (isset($_GET['debug_profile'])) { Profiler::mark("SSViewer::process - compile", " for {$template}"); } $content = file_get_contents($template); $content = SSViewer::parseTemplateContent($content, $template); $fh = fopen($cacheFile, 'w'); fwrite($fh, $content); fclose($fh); if (isset($_GET['debug_profile'])) { Profiler::unmark("SSViewer::process - compile", " for {$template}"); } } if (isset($_GET['showtemplate']) && !Director::isLive()) { $lines = file($cacheFile); echo "<h2>Template: {$cacheFile}</h2>"; echo "<pre>"; foreach ($lines as $num => $line) { echo str_pad($num + 1, 5) . htmlentities($line); } echo "</pre>"; } // Makes the rendered sub-templates available on the parent item, // through $Content and $Layout placeholders. foreach (array('Content', 'Layout') as $subtemplate) { if (isset($this->chosenTemplates[$subtemplate])) { $subtemplateViewer = new SSViewer($this->chosenTemplates[$subtemplate]); $item = $item->customise(array($subtemplate => $subtemplateViewer->process($item, $cache))); } } $itemStack = array(); $val = ""; $valStack = array(); include $cacheFile; $output = $val; $output = Requirements::includeInHTML($template, $output); array_pop(SSViewer::$topLevel); if (isset($_GET['debug_profile'])) { Profiler::unmark("SSViewer::process", " for {$template}"); } // If we have our crazy base tag, then fix # links referencing the current page. if ($this->rewriteHashlinks && self::$options['rewriteHashlinks']) { if (strpos($output, '<base') !== false) { if (SSViewer::$options['rewriteHashlinks'] === 'php') { $thisURLRelativeToBase = "<?php echo \$_SERVER['REQUEST_URI']; ?>"; } else { $thisURLRelativeToBase = Director::makeRelative(Director::absoluteURL($_SERVER['REQUEST_URI'])); } $output = preg_replace('/(<a[^>]+href *= *)"#/i', '\\1"' . $thisURLRelativeToBase . '#', $output); } } return $output; }
function testBlockedCombinedJavascript() { $combinedFilePath = Director::baseFolder() . '/' . 'RequirementsTest_bc.js'; /* BLOCKED COMBINED FILES ARE NOT INCLUDED */ $this->setupCombinedRequirements(); Requirements::block('RequirementsTest_bc.js'); Requirements::delete_combined_files('RequirementsTest_bc.js'); clearstatcache(); // needed to get accurate file_exists() results $html = Requirements::includeInHTML(false, self::$html_template); $this->assertFalse((bool)preg_match('/src=".*\/RequirementsTest_bc\.js/', $html), 'blocked combined files are not included '); Requirements::unblock('RequirementsTest_bc.js'); /* BLOCKED UNCOMBINED FILES ARE NOT INCLUDED */ // need to re-add requirements, as Requirements::process_combined_includes() alters the // original arrays grml... $this->setupCombinedRequirements(); Requirements::block('sapphire/tests/forms/RequirementsTest_b.js'); Requirements::delete_combined_files('RequirementsTest_bc.js'); clearstatcache(); // needed to get accurate file_exists() results $html = Requirements::includeInHTML(false, self::$html_template); $this->assertFalse((strpos(file_get_contents($combinedFilePath), "alert('b')") !== false), 'blocked uncombined files are not included'); Requirements::unblock('RequirementsTest_b.js'); /* A SINGLE FILE CAN'T BE INCLUDED IN TWO COMBINED FILES */ $this->setupCombinedRequirements(); clearstatcache(); // needed to get accurate file_exists() results // This throws a notice-level error, so we prefix with @ @Requirements::combine_files( 'RequirementsTest_ac.js', array( 'sapphire/tests/forms/RequirementsTest_a.js', 'sapphire/tests/forms/RequirementsTest_c.js' ) ); $combinedFiles = Requirements::get_combine_files(); $this->assertEquals( array_keys($combinedFiles), array('RequirementsTest_bc.js'), "A single file can't be included in two combined files" ); Requirements::delete_combined_files('RequirementsTest_bc.js'); }