1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 09:14:58 +02:00

Update TemplateFile class with a clearAll() static method and update ProcessPageView to use it when a 404 is thrown. This ensures that a 404 thrown after some output has already been sent can't get interpreted as a non-404 by the client

This commit is contained in:
Ryan Cramer
2021-03-31 10:13:18 -04:00
parent a3616cebfb
commit a16cb528dc
2 changed files with 29 additions and 1 deletions

View File

@@ -138,7 +138,15 @@ class TemplateFile extends WireData {
* DEPRECATED: Variables that will be applied globally to this and all other TemplateFile instances
*
*/
static protected $globals = array();
static protected $globals = array();
/**
* Output buffer starting level, set by first TemplateFile instance that gets created
*
* @var null|int
*
*/
static protected $obStartLevel = null;
/**
* Construct the template file
@@ -147,6 +155,7 @@ class TemplateFile extends WireData {
*
*/
public function __construct($filename = '') {
if(self::$obStartLevel === null) self::$obStartLevel = ob_get_level();
if($filename) $this->setFilename($filename);
}
@@ -561,6 +570,24 @@ class TemplateFile extends WireData {
return self::$renderStack;
}
/**
* Clear out all pending output buffers
*
* @since 3.0.175
* @return int Number of output buffers cleaned
*
*/
public static function clearAll() {
$n = 0;
if(self::$obStartLevel !== null) {
while(ob_get_level() > self::$obStartLevel) {
ob_end_clean();
$n++;
}
}
return $n;
}
/**
* The string value of a TemplateFile is its PHP template filename OR its class name if no filename is set
*

View File

@@ -227,6 +227,7 @@ class ProcessPageView extends Process {
} catch(Wire404Exception $e) {
// 404 exception
TemplateFile::clearAll();
return $this->renderNoPage(array(
'reason404' => '404 thrown during page render',
'exception404' => $e,