From dcf646bfec2c5a19569ba1f043204008cf17e472 Mon Sep 17 00:00:00 2001 From: mattpass Date: Wed, 24 Jun 2020 09:23:28 +0100 Subject: [PATCH] Invalidate OP cache before getting file contents --- classes/File.php | 6 ++++-- classes/System.php | 10 ++++++++++ lib/bug-files-check.php | 1 + lib/indexer.php | 3 +++ lib/settings-common.php | 14 ++++++++++---- lib/settings-update.php | 1 + lib/settings.php | 3 +++ server/run-tasks.php | 4 +++- 8 files changed, 35 insertions(+), 7 deletions(-) diff --git a/classes/File.php b/classes/File.php index 09e0e3c..e6eb776 100644 --- a/classes/File.php +++ b/classes/File.php @@ -351,7 +351,7 @@ class File } public function writeFile() { - global $file, $t, $ICEcoder, $serverType, $doNext, $contents; + global $file, $t, $ICEcoder, $serverType, $doNext, $contents, $systemClass; if (isset($_POST['changes'])) { // Get existing file contents as lines and stitch changes onto it $fileLines = file($file); @@ -369,6 +369,7 @@ class File // Newly created files have the perms set too $setPerms = (!file_exists($file)) ? true : false; + $systemClass->invalidateOPCache($file); $fh = fopen($file, 'w') or die($t['Sorry, cannot save']); // replace \r\n (Windows), \r (old Mac) and \n (Linux) line endings with whatever we chose to be lineEnding @@ -647,7 +648,7 @@ class File } public function compileSass() { - global $docRoot, $fileLoc, $fileName; + global $docRoot, $fileLoc, $fileName, $systemClass; $doNext = ""; @@ -667,6 +668,7 @@ class File if (true === is_writable($docRoot . $fileLoc)) { $scssContent = $scss->compile('@import "' . $fileName . '"'); + $systemClass->invalidateOPCache($docRoot . $fileLoc . "/" . substr($fileName, 0, -4) . "css"); $fh = fopen($docRoot . $fileLoc . "/" . substr($fileName, 0, -4) . "css", 'w'); fwrite($fh, $scssContent); fclose($fh); diff --git a/classes/System.php b/classes/System.php index f9b0e5f..975da61 100644 --- a/classes/System.php +++ b/classes/System.php @@ -59,6 +59,16 @@ class System return $context; } + /** + * @param $path + */ + public function invalidateOPCache($path): void + { + if (function_exists('opcache_invalidate')) { + opcache_invalidate($path, true); + } + } + /** * @param $fileLines * @param $changes diff --git a/lib/bug-files-check.php b/lib/bug-files-check.php index 2744121..16853ba 100644 --- a/lib/bug-files-check.php +++ b/lib/bug-files-check.php @@ -44,6 +44,7 @@ if ("error" !== $result) { $lines = $maxLines + 1 + 1; // 1 (possibly) for end of file and 1 for partial lines // Open the file + $systemClass->invalidateOPCache($filename); $f = fopen($filename, "rb"); // Jump to last character diff --git a/lib/indexer.php b/lib/indexer.php index b565a22..6869b3a 100644 --- a/lib/indexer.php +++ b/lib/indexer.php @@ -11,6 +11,7 @@ $prevIndexData = []; // If we have a data/index.php file if (file_exists($docRoot . $ICEcoderDir . "/data/index.php")) { // Get serialized array back out of PHP file inside a comment block as prevIndexData + $systemClass->invalidateOPCache($docRoot . $ICEcoderDir . "/data/index.php"); $prevIndexData = file_get_contents($docRoot . $ICEcoderDir . "/data/index.php"); if (false !== strpos($prevIndexData, "invalidateOPCache($docRoot . $ICEcoderDir . "/data/git-diff.php"); $gitDiffData = file_get_contents($docRoot . $ICEcoderDir . "/data/git-diff.php"); if (strpos($gitDiffData, "invalidateOPCache($docRoot . $ICEcoderDir . "/data/git-content.php"); $gitContent = file_get_contents($docRoot . $ICEcoderDir . "/data/git-content.php"); if (strpos($gitContent, " $timeout // secs ) )); + $systemClass->invalidateOPCache($url); $data = @file_get_contents($url, false, $context); if (!$data) { $data = @file_get_contents(str_replace("https:", "http:", $url), false, $context); } } elseif (file_exists($url)) { - $data = file_get_contents($url); - } + $systemClass->invalidateOPCache($url); + $data = file_get_contents($url); + } // Return data or die with message if ($data) { return $data; @@ -127,9 +129,10 @@ function getData($url, $type='fopen', $dieMessage = false, $timeout = 60) { // Require a re-index dir/file data next time we index function requireReIndexNextTime() { // If we have a data/index.php file - global $docRoot, $ICEcoderDir; + global $docRoot, $ICEcoderDir, $systemClass; if (true === file_exists($docRoot . $ICEcoderDir . "/data/index.php")) { // Get serialized array back out of PHP file inside a comment block as prevIndexData + $systemClass->invalidateOPCache($docRoot . $ICEcoderDir . "/data/index.php"); $prevIndexData = file_get_contents($docRoot . $ICEcoderDir . "/data/index.php"); if (strpos($prevIndexData, "invalidateOPCache($path); $data = file_get_contents($path); $data = str_replace("<"."?php\n/*\n\n", "", $data); $data = str_replace("\n\n*/\n?".">", "", $data); diff --git a/lib/settings-update.php b/lib/settings-update.php index 0054e2c..1174111 100644 --- a/lib/settings-update.php +++ b/lib/settings-update.php @@ -15,6 +15,7 @@ if (!$demoMode && true === isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] $fh = fopen("../data/" . $settingsFile, 'w'); fwrite($fh, str_replace('"tutorialOnLogin" => true', '"tutorialOnLogin" => false', $settingsContents)); fclose($fh); + $ICEcoder['tutorialOnLogin'] = false; } else { echo ""; } diff --git a/lib/settings.php b/lib/settings.php index 9aae41d..c0a6815 100644 --- a/lib/settings.php +++ b/lib/settings.php @@ -10,6 +10,7 @@ require_once dirname(__FILE__) . "/../classes/System.php"; use ICEcoder\ExtraProcesses; $settingsClass = new \ICEcoder\Settings(); +$systemClass = new \ICEcoder\System(); // Create a new config file if it doesn't exist yet. // The reason we create it, is so it has PHP write permissions, meaning we can update it later @@ -27,6 +28,7 @@ if (false === file_exists(dirname(__FILE__) . "/../data/" . $configSettings)) { } // Load config settings +$systemClass->invalidateOPCache(dirname(__FILE__) . "/../data/" . $configSettings); include dirname(__FILE__) . "/../data/" . $configSettings; // Load common functions @@ -52,6 +54,7 @@ if (false === file_exists(dirname(__FILE__) . "/../data/" . $settingsFile) && $I } // Load user settings +$systemClass->invalidateOPCache(dirname(__FILE__) . "/../data/" . $settingsFile); include dirname(__FILE__) . "/../data/" . $settingsFile; // Remove any previous files that are no longer there diff --git a/server/run-tasks.php b/server/run-tasks.php index f0ff695..bf1893c 100644 --- a/server/run-tasks.php +++ b/server/run-tasks.php @@ -11,6 +11,9 @@ function requireReIndexNextTime() { // If we have a data/index.php file if (file_exists(dirname(__FILE__)."/../data/index.php")) { // Get serialized array back out of PHP file inside a comment block as prevIndexData + if (function_exists('opcache_invalidate')) { + opcache_invalidate(dirname(__FILE__)."/../data/index.php", true); + } $prevIndexData = file_get_contents(dirname(__FILE__)."/../data/index.php"); if (strpos($prevIndexData, " array_filter($diffLines)]; // Store the serialized array in PHP comment block for pick up file_put_contents(dirname(__FILE__)."/../data/git-diff.php", ""); - // Set Git contents $output = []; $paths = array_filter($diffLines);