From 75e18e0cf5191a9ddca4beacac5cc7312326bad6 Mon Sep 17 00:00:00 2001 From: rMalie Date: Thu, 26 Jan 2012 12:49:22 +0000 Subject: [PATCH] // Fix potential issue on autoload if class_index is corrupted --- cache/class_index.php | 2 +- classes/Autoload.php | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/cache/class_index.php b/cache/class_index.php index 55cc932d8..3402e677c 100644 --- a/cache/class_index.php +++ b/cache/class_index.php @@ -644,4 +644,4 @@ 'WebserviceSpecificManagementSearchCore' => 'classes/webservice/WebserviceSpecificManagementSearch.php', 'Zone' => 'override/classes/Zone.php', 'ZoneCore' => 'classes/Zone.php', -); \ No newline at end of file +); ?> \ No newline at end of file diff --git a/classes/Autoload.php b/classes/Autoload.php index 85f3d60c7..b70b10603 100644 --- a/classes/Autoload.php +++ b/classes/Autoload.php @@ -54,7 +54,7 @@ class Autoload { $this->root_dir = dirname(dirname(__FILE__)).'/'; if (file_exists($this->root_dir.Autoload::INDEX_FILE)) - $this->index = include_once($this->root_dir.Autoload::INDEX_FILE); + $this->index = include($this->root_dir.Autoload::INDEX_FILE); } /** @@ -129,12 +129,34 @@ class Autoload $this->getClassesFromDir('override/controllers/') ); ksort($classes); - $content = ''; // Write classes index on disc to cache it $filename = $this->root_dir.Autoload::INDEX_FILE; if ((file_exists($filename) && is_writable($filename)) || is_writable(dirname($filename))) - file_put_contents($filename, $content); + { + // Let's write index content in cache file + // In order to be sure that this file is correctly written, a check is done on the file content + $loop_protection = 0; + do + { + $integrity_is_ok = false; + file_put_contents($filename, $content); + if ($loop_protection++ > 10) + break; + + // If the file content end with PHP tag, integrity of the file is ok + if (preg_match('#\?>\s*$#', file_get_contents($filename))) + $integrity_is_ok = true; + } + while (!$integrity_is_ok); + + if (!$integrity_is_ok) + { + file_put_contents($filename, ''); + throw new Exception('Your file '.$filename.' is corrupted. Please remove this file, a new one will be regenerated automatically'); + } + } else throw new Exception($filename.' is not writable!');