From f196bb9305478ada6f724c10bd3def9bd7e8e67a Mon Sep 17 00:00:00 2001 From: mattpass Date: Tue, 21 Jul 2020 08:13:23 +0100 Subject: [PATCH] Fixing Save As issue plus a few PHP notices --- classes/File.php | 14 ++++++++------ lib/file-control.php | 13 ++++++------- terminal.php | 4 +++- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/classes/File.php b/classes/File.php index e902785..ba18a71 100644 --- a/classes/File.php +++ b/classes/File.php @@ -353,9 +353,9 @@ class File $fileLines = file($file); $contents = $this->systemClass->stitchChanges($fileLines, $_POST['changes']); - // get old file contents, and count stats on usage \n and \r there - // in this case we can keep line endings, which file had before, without - // making code version control systems going crazy about line endings change in whole file. + // Get old file contents, and count stats on usage \n and \r\n + // We use this info shortly to standardise the file to the same line endings + // throughout, whichever is greater $oldContents = file_exists($file) ? getData($file) : ''; $unixNewLines = preg_match_all('/[^\r][\n]/u', $oldContents); $windowsNewLines = preg_match_all('/[\r][\n]/u', $oldContents); @@ -368,11 +368,13 @@ class File $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 + // Replace \r\n (Windows), \r (old Mac) and \n (Linux) line endings with whatever we chose to be lineEnding $contents = str_replace("\r\n", $ICEcoder["lineEnding"], $contents); $contents = str_replace("\r", $ICEcoder["lineEnding"], $contents); $contents = str_replace("\n", $ICEcoder["lineEnding"], $contents); - if (isset($_POST['changes']) && (0 < $unixNewLines) || (0 < $windowsNewLines)) { + // Finally, replace the line endings with whatever what greatest in the file before + // (We do this to help avoid a huge number of unnecessary changes simply on line endings) + if (isset($_POST['changes']) && (0 < $unixNewLines || 0 < $windowsNewLines)) { if ($unixNewLines > $windowsNewLines){ $contents = str_replace($ICEcoder["lineEnding"], "\n", $contents); } elseif ($windowsNewLines > $unixNewLines){ @@ -735,7 +737,7 @@ class File } else { $itemAbsPath = $file; $itemPath = dirname($file); - $itemBytes = is_dir($file) ? null : filesize($file); + $itemBytes = is_dir($file) || !file_exists($file) ? null : filesize($file); $itemType = (file_exists($file) ? (is_dir($file) ? "dir" : "file") : "unknown"); $itemExists = (file_exists($file) ? "true" : "false"); } diff --git a/lib/file-control.php b/lib/file-control.php index 242d6d2..81de883 100644 --- a/lib/file-control.php +++ b/lib/file-control.php @@ -136,13 +136,12 @@ if (!$error && "save" === $_GET['action']) { if (!$demoMode && (isset($ftpSite) || (file_exists($file) && is_writable($file)) || isset($_POST['newFileName']) && "" != $_POST['newFileName'])) { - $filemtime = !isset($ftpSite) && "Windows" !== $serverType ? filemtime($file) : "1000000"; + $filemtime = !isset($ftpSite) && "Windows" !== $serverType && file_exists($file) ? filemtime($file) : "1000000"; - // ======================= - // MDT'S MATCH, WRITE FILE - // ======================= - - if (!(isset($_GET['fileMDT'])) || $filemtime == $_GET['fileMDT']) { + // ================================================== + // MDT'S MATCH (OR WE'RE SAVING NEW FILE), WRITE FILE + // ================================================== + if (!(isset($_GET['fileMDT'])) || $filemtime == $_GET['fileMDT'] || isset($_POST['newFileName'])) { // FTP Saving if (isset($ftpSite)) { @@ -488,7 +487,7 @@ if (!isset($ftpSite) && !$error && "checkExists" === $_GET['action']) { // No $filemtime yet? Get it now! if (false === isset($filemtime) && !is_dir($file)) { - $filemtime = "Windows" !== $serverType ? filemtime($file) : 1000000; + $filemtime = "Windows" !== $serverType && file_exists($file) ? filemtime($file) : 1000000; } if (false === isset($filemtime)) { $filemtime = 1000000; diff --git a/terminal.php b/terminal.php index b4937b7..8307797 100644 --- a/terminal.php +++ b/terminal.php @@ -93,7 +93,9 @@ sendCmd = function(command) {