From 5e4e73067ef47c7ee16e0d97f5d4b3538092d844 Mon Sep 17 00:00:00 2001 From: mattpass Date: Fri, 8 May 2020 09:01:09 +0100 Subject: [PATCH] Tidy lib/file-control-xhr.php --- lib/file-control-xhr.php | 560 ++++++++++++++++++++------------------- 1 file changed, 285 insertions(+), 275 deletions(-) diff --git a/lib/file-control-xhr.php b/lib/file-control-xhr.php index 5b27136..96a327d 100644 --- a/lib/file-control-xhr.php +++ b/lib/file-control-xhr.php @@ -1,7 +1,7 @@ 0||$saveType=="saveAs") { - $finalAction = strpos($fileOrig,"[NEW]")>0 ? "save as" : "save"; + if (0 < strpos($fileOrig, "[NEW]") || "saveAs" === $saveType) { + $finalAction = 0 < strpos($fileOrig, "[NEW]") ? "save as" : "save"; $fileURL = isset($file) ? $file : ""; - $fileMDTURLPart = isset($_GET["fileMDT"]) && $_GET["fileMDT"]!="undefined" ? "&fileMDT=".numClean($_GET['fileMDT']) : ""; - $fileVersionURLPart = isset($_GET["fileVersion"]) && $_GET["fileVersion"]!="undefined" ? "&fileVersion=".numClean($_GET['fileVersion']) : ""; + $fileMDTURLPart = isset($_GET["fileMDT"]) && "undefined" !== $_GET["fileMDT"] ? "&fileMDT=" . numClean($_GET['fileMDT']) : ""; + $fileVersionURLPart = isset($_GET["fileVersion"]) && "undefined" !== $_GET["fileVersion"] ? "&fileVersion=" . numClean($_GET['fileVersion']) : ""; $doNext .= ' ICEcoder.serverMessage(); - fileLoc = "'.$fileLoc.'"; + fileLoc = "' . $fileLoc . '"; overwriteOK = false; noConflictSave = false; - newFileName = ICEcoder.getInput("'.$t['Enter filename to...'].' "+(fileLoc!="" ? fileLoc : "/"),""); + newFileName = ICEcoder.getInput("' . $t['Enter filename to...'] . ' " + (fileLoc!="" ? fileLoc : "/"), ""); if (newFileName) { - if (newFileName.substr(0,1)!="/") {newFileName = "/" + newFileName}; + if ("/" !== newFileName.substr(0,1)) {newFileName = "/" + newFileName}; newFileName = fileLoc + newFileName; /* Check if file/dir exists */ ICEcoder.lastFileDirCheckStatusObj = false; ICEcoder.checkExists(newFileName); var thisInt = setInterval(function() { - if (ICEcoder.lastFileDirCheckStatusObj != false) { + if (false != ICEcoder.lastFileDirCheckStatusObj) { clearInterval(thisInt); if (ICEcoder.lastFileDirCheckStatusObj.file && ICEcoder.lastFileDirCheckStatusObj.file.exists) { - overwriteOK = ICEcoder.ask("'.$t['That file exists...'].'"); + overwriteOK = ICEcoder.ask("' . $t['That file exists...'] . '"); } else { noConflictSave = true; }; /* Saving under conditions: Confirmation of overwrite or there is no filename conflict, it is a new file, in either case we can save */ if (overwriteOK || noConflictSave) { - newFileName = "'.(isset($ftpSite) ? "" : $docRoot).'" + newFileName; - saveURL = "lib/file-control-xhr.php?action=save'.$fileMDTURLPart.$fileVersionURLPart.'&csrf='.$_GET["csrf"].'"; + newFileName = "' . (isset($ftpSite) ? "" : $docRoot) . '" + newFileName; + saveURL = "lib/file-control-xhr.php?action=save' . $fileMDTURLPart . $fileVersionURLPart . '&csrf=' . $_GET["csrf"] . '"; var xhr = ICEcoder.xhrObj(); xhr.onreadystatechange=function() { - if (xhr.readyState==4 && xhr.status==200) { + if (4 === xhr.readyState && 200 === xhr.status) { /* console.log(xhr.responseText); */ var statusObj = JSON.parse(xhr.responseText); /* Set the actions end time and time taken in JSON object */ @@ -232,11 +238,11 @@ if (!$error && $_GET['action']=="save") { } }; - /* console.log(\'Calling \'+saveURL+\' via XHR\'); */ + /* console.log(\'Calling \' + saveURL + \' via XHR\'); */ xhr.open("POST",saveURL,true); xhr.setRequestHeader(\'Content-type\', \'application/x-www-form-urlencoded\'); - xhr.send(\'timeStart='.numClean($_POST["timeStart"]).'&file='.$fileURL.'&newFileName=\'+newFileName.replace(/\\\+/g,"%2B")+\'&contents=\'+encodeURIComponent(ICEcoder.saveAsContent)); - ICEcoder.serverMessage("'.$t['Saving'].'
" + "'.($finalAction == "Save" ? "newFileName" : "'".$fileName."'").'"); + xhr.send(\'timeStart=' . numClean($_POST["timeStart"]) . '&file=' . $fileURL . '&newFileName=\' + newFileName.replace(/\\\+/g, "%2B") + \'&contents=\' + encodeURIComponent(ICEcoder.saveAsContent)); + ICEcoder.serverMessage("' . $t['Saving'] . '
" + "'.("Save" === $finalAction ? "newFileName" : "'" . $fileName . "'") . '"); } } },10); @@ -245,7 +251,7 @@ if (!$error && $_GET['action']=="save") { /* UI dialog cancelling and saving contents for save as looparound */ if (!newFileName || newFileName && !overwriteOK) { ICEcoder.saveAsContent = document.getElementById(\'saveTemp1\').value; - ICEcoder.serverMessage();ICEcoder.serverQueue("del",0); + ICEcoder.serverMessage();ICEcoder.serverQueue("del", 0); }'; // =================== @@ -259,28 +265,28 @@ if (!$error && $_GET['action']=="save") { // FILE IS WRITEABLE // ================= - if (!$demoMode && (isset($ftpSite) || (file_exists($file) && is_writable($file)) || isset($_POST['newFileName']) && $_POST['newFileName']!="")) { + if (!$demoMode && (isset($ftpSite) || (file_exists($file) && is_writable($file)) || isset($_POST['newFileName']) && "" != $_POST['newFileName'])) { - $filemtime = !isset($ftpSite) && $serverType=="Linux" ? filemtime($file) : "1000000"; + $filemtime = !isset($ftpSite) && "Linux" === $serverType ? filemtime($file) : "1000000"; // ======================= // MDT'S MATCH, WRITE FILE // ======================= - if (!(isset($_GET['fileMDT']))||$filemtime==$_GET['fileMDT']) { + if (!(isset($_GET['fileMDT'])) || $filemtime == $_GET['fileMDT']) { // FTP Saving if (isset($ftpSite)) { - $ftpFilepath = ltrim($fileLoc."/".$fileName,"/"); + $ftpFilepath = ltrim($fileLoc . "/" . $fileName, "/"); if (isset($_POST['changes'])) { // Get existing file contents as lines - $loadedFile = toUTF8noBOM(ftpGetContents($ftpConn, $ftpRoot.$fileLoc."/".$fileName, $ftpMode),false); - $fileLines = explode("\n",str_replace("\r","",$loadedFile)); + $loadedFile = toUTF8noBOM(ftpGetContents($ftpConn, $ftpRoot . $fileLoc . "/" . $fileName, $ftpMode), false); + $fileLines = explode("\n", str_replace("\r", "", $loadedFile)); // Need to add a new line at the end of each because explode will lose them, // want want to end up with same array that 'file($file)' produces for a local file // - it keeps the line endings at the end of each array item - for ($i=0; $i 0) || ($windowsNewLines > 0)){ + if (isset($_POST['changes']) && ($unixNewLines > 0) || ($windowsNewLines > 0)) { if ($unixNewLines > $windowsNewLines){ $contents = str_replace($ICEcoder["lineEnding"], "\n", $contents); } elseif ($windowsNewLines > $unixNewLines){ @@ -309,10 +315,10 @@ if (!$error && $_GET['action']=="save") { } // Write our file contents if (!ftpWriteFile($ftpConn, $ftpFilepath, $contents, $ftpMode)) { - $doNext .= 'ICEcoder.message("Sorry, could not write '.$ftpFilepath.' at '.$ftpHost.'");'; + $doNext .= 'ICEcoder.message("Sorry, could not write ' . $ftpFilepath . ' at ' . $ftpHost . '");'; } else { - $doNext .= 'ICEcoder.openFileMDTs[ICEcoder.selectedTab-1]="'.$filemtime.'";'; - $doNext .= '(function() {var x=ICEcoder.openFileVersions; var y=ICEcoder.selectedTab-1; x[y] = "undefined" != typeof x[y] ? x[y]+1 : 1})();ICEcoder.updateVersionsDisplay();'; + $doNext .= 'ICEcoder.openFileMDTs[ICEcoder.selectedTab - 1]="' . $filemtime . '";'; + $doNext .= '(function() {var x = ICEcoder.openFileVersions; var y = ICEcoder.selectedTab-1; x[y] = "undefined" != typeof x[y] ? x[y] + 1 : 1})(); ICEcoder.updateVersionsDisplay();'; } // Local saving } else { @@ -324,7 +330,7 @@ if (!$error && $_GET['action']=="save") { // 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. - $oldContents = file_exists($file)?getData($file):''; + $oldContents = file_exists($file) ? getData($file) : ''; $unixNewLines = preg_match_all('/[^\r][\n]/u', $oldContents); $windowsNewLines = preg_match_all('/[\r][\n]/u', $oldContents); } else { @@ -339,7 +345,7 @@ if (!$error && $_GET['action']=="save") { $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']) && ($unixNewLines > 0) || ($windowsNewLines > 0)){ + if (isset($_POST['changes']) && (0 < $unixNewLines) || (0 < $windowsNewLines)) { if ($unixNewLines > $windowsNewLines){ $contents = str_replace($ICEcoder["lineEnding"], "\n", $contents); } elseif ($windowsNewLines > $unixNewLines){ @@ -351,12 +357,12 @@ if (!$error && $_GET['action']=="save") { fclose($fh); if ($setPerms) { - chmod($file,octdec($ICEcoder['newFilePerms'])); + chmod($file, octdec($ICEcoder['newFilePerms'])); } clearstatcache(); - $filemtime = $serverType=="Linux" ? filemtime($file) : "1000000"; - $doNext .= 'ICEcoder.openFileMDTs[ICEcoder.selectedTab-1]="'.$filemtime.'";'; - $doNext .= '(function() {var x=ICEcoder.openFileVersions; var y=ICEcoder.selectedTab-1; x[y] = "undefined" != typeof x[y] ? x[y]+1 : 1})();ICEcoder.updateVersionsDisplay();'; + $filemtime = "Linux" === $serverType ? filemtime($file) : "1000000"; + $doNext .= 'ICEcoder.openFileMDTs[ICEcoder.selectedTab - 1] = "' . $filemtime . '";'; + $doNext .= '(function() {var x = ICEcoder.openFileVersions; var y = ICEcoder.selectedTab-1; x[y] = "undefined" != typeof x[y] ? x[y] + 1 : 1})(); ICEcoder.updateVersionsDisplay();'; } // Save a version controlled backup source of the file @@ -364,113 +370,113 @@ if (!$error && $_GET['action']=="save") { $backupDirFormat = "Y-m-d"; // Establish the base, host and date dir parts... - $backupDirBase = str_replace("\\","/",dirname(__FILE__))."/../data/backups/"; - $backupDirHost = isset($ftpSite) ? parse_url($ftpSite,PHP_URL_HOST) : "localhost"; + $backupDirBase = str_replace("\\", "/", dirname(__FILE__)) . "/../data/backups/"; + $backupDirHost = isset($ftpSite) ? parse_url($ftpSite, PHP_URL_HOST) : "localhost"; $backupDirDate = date($backupDirFormat); // Establish an array of dirs from base to our file location - $subDirsArray = explode("/",ltrim($fileLoc,"/")); - array_unshift($subDirsArray,$backupDirHost,$backupDirDate); + $subDirsArray = explode("/", ltrim($fileLoc, "/")); + array_unshift($subDirsArray, $backupDirHost, $backupDirDate); // Make any dirs that don't exist if full path isn't there - if (!is_dir($backupDirBase.implode("/",$subDirsArray))) { + if (!is_dir($backupDirBase . implode("/", $subDirsArray))) { $pathIncr = ""; - for ($i=0; $i","",$loadedFile)))).'"; - var refreshFile = ICEcoder.ask("'.$t['Sorry, this file...'].'\\\n'.$file.'\\\n\\\n'.$t['Reload this file...'].'"); + loadedFile.value = "' . str_replace('"', '\\\"', str_replace("\r", "\\\\r", str_replace("\n", "\\\\n", str_replace("", "", $loadedFile)))).'"; + var refreshFile = ICEcoder.ask("' . $t['Sorry, this file...'] . '\\\n' . $file . '\\\n\\\n' . $t['Reload this file...'] . '"); if (refreshFile) { var cM = ICEcoder.getcMInstance(); var thisTab = ICEcoder.selectedTab; var userVersionFile = cM.getValue(); /* Revert back to original */ cM.setValue(loadedFile.value); - ICEcoder.savedPoints[thisTab-1] = cM.changeGeneration(); - ICEcoder.savedContents[thisTab-1] = cM.getValue(); - ICEcoder.openFileMDTs[ICEcoder.selectedTab-1] = "'.$filemtime.'"; - ICEcoder.openFileVersions[ICEcoder.selectedTab-1] = "'.$fileCountInfo['count'].'"; + ICEcoder.savedPoints[thisTab - 1] = cM.changeGeneration(); + ICEcoder.savedContents[thisTab - 1] = cM.getValue(); + ICEcoder.openFileMDTs[ICEcoder.selectedTab - 1] = "' . $filemtime . '"; + ICEcoder.openFileVersions[ICEcoder.selectedTab - 1] = "' . $fileCountInfo['count'] . '"; cM.clearHistory(); /* Now for the new version in the diff pane */ ICEcoder.setSplitPane(\'on\'); @@ -531,9 +537,9 @@ if (!$error && $_GET['action']=="save") { } else { $finalAction = "nothing"; - $doNext .= "ICEcoder.message('".$t['Sorry, cannot save']."\\\\n".$file."');"; + $doNext .= "ICEcoder.message('" . $t['Sorry, cannot save'] . "\\\\n" . $file . "');"; } - $doNext .= 'ICEcoder.serverMessage();ICEcoder.serverQueue("del",0);'; + $doNext .= 'ICEcoder.serverMessage(); ICEcoder.serverQueue("del", 0);'; } }; @@ -541,14 +547,14 @@ if (!$error && $_GET['action']=="save") { // NEW FOLDER // ========== -if (!$error && $_GET['action']=="newFolder") { +if (!$error && "newFolder" === $_GET['action']) { if (!$demoMode && (isset($ftpSite) || is_writable($docRoot.$fileLoc))) { $updateFM = false; // FTP if (isset($ftpSite)) { - $ftpFilepath = ltrim($fileLoc."/".$fileName,"/"); + $ftpFilepath = ltrim($fileLoc . "/" . $fileName, "/"); if (!ftpMkDir($ftpConn, octdec($ICEcoder['newDirPerms']), $ftpFilepath)) { - $doNext .= 'ICEcoder.message("Sorry, could not create dir '.$ftpFilepath.' at '.$ftpHost.'");'; + $doNext .= 'ICEcoder.message("Sorry, could not create dir '.$ftpFilepath.' at ' . $ftpHost . '");'; } else { $updateFM = true; } @@ -560,29 +566,29 @@ if (!$error && $_GET['action']=="newFolder") { } // Update file manager on success if ($updateFM) { - $doNext .= 'ICEcoder.selectedFiles=[];ICEcoder.updateFileManagerList(\'add\',\''.$fileLoc.'\',\''.$fileName.'\',false,false,false,\'folder\');'; + $doNext .= 'ICEcoder.selectedFiles=[]; ICEcoder.updateFileManagerList(\'add\', \'' . $fileLoc . '\', \'' . $fileName . '\', false, false, false, \'folder\');'; } $finalAction = "newFolder"; // Run our custom processes - include_once("../processes/on-new-dir.php"); + include_once "../processes/on-new-dir.php"; } else { - $doNext .= "ICEcoder.message('".$t['Sorry, cannot create...']."\\\\n".$fileLoc."');"; + $doNext .= "ICEcoder.message('" . $t['Sorry, cannot create...'] . "\\\\n" . $fileLoc . "');"; $finalAction = "nothing"; } - $doNext .= 'ICEcoder.serverMessage();ICEcoder.serverQueue("del",0);'; + $doNext .= 'ICEcoder.serverMessage(); ICEcoder.serverQueue("del", 0);'; }; // ================ // MOVE FILE/FOLDER // ================ -if (!$error && $_GET['action']=="move") { +if (!$error && "move" === $_GET['action']) { if (isset($ftpSite)) { - $srcDir = ltrim(str_replace("|","/",$_GET['oldFileName']),"/"); - $tgtDir = ltrim($fileLoc."/".$fileName,"/"); + $srcDir = ltrim(str_replace("|", "/", $_GET['oldFileName']), "/"); + $tgtDir = ltrim($fileLoc . "/" . $fileName, "/"); } else { - $srcDir = $docRoot.$iceRoot.str_replace("|","/",$_GET['oldFileName']); - $tgtDir = $docRoot.$fileLoc."/".$fileName; + $srcDir = $docRoot . $iceRoot . str_replace("|", "/", $_GET['oldFileName']); + $tgtDir = $docRoot . $fileLoc . "/" . $fileName; } if ($srcDir != $tgtDir && $fileLoc != "") { if (!$demoMode && (isset($ftpSite) || is_writable($srcDir))) { @@ -590,90 +596,90 @@ if (!$error && $_GET['action']=="move") { // FTP if (isset($ftpSite)) { if (!ftpRename($ftpConn, $srcDir, $tgtDir)) { - $doNext .= 'ICEcoder.message("Sorry, could not rename '.$srcDir.' to '.$tgtDir.'");'; + $doNext .= 'ICEcoder.message("Sorry, could not rename ' . $srcDir . ' to ' . $tgtDir . '");'; } else { - $ftpFileDirInfo = ftpGetFileInfo($ftpConn, ltrim($fileLoc,"/"), $fileName); - $fileOrFolder = $ftpFileDirInfo['type'] == "directory" ? "folder" : "file"; + $ftpFileDirInfo = ftpGetFileInfo($ftpConn, ltrim($fileLoc, "/"), $fileName); + $fileOrFolder = "directory" === $ftpFileDirInfo['type'] ? "folder" : "file"; $updateFM = true; } // Local } else { - if(rename($srcDir,$tgtDir)) { + if(rename($srcDir, $tgtDir)) { // Is a dir or file (needed to create new item in file manager) - $fileOrFolder = is_dir($docRoot.$fileLoc."/".$fileName) ? "folder" : "file"; + $fileOrFolder = is_dir($docRoot . $fileLoc . "/" . $fileName) ? "folder" : "file"; $updateFM = true; } } // Update file manager on success if ($updateFM) { - $doNext .= 'ICEcoder.selectedFiles=[];ICEcoder.updateFileManagerList(\'move\',\''.$fileLoc.'\',\''.$fileName.'\',\'\',\''.str_replace($iceRoot,"",str_replace("|","/",$_GET['oldFileName'])).'\',false,\''.$fileOrFolder.'\');'; + $doNext .= 'ICEcoder.selectedFiles=[]; ICEcoder.updateFileManagerList(\'move\', \'' . $fileLoc . '\', \'' . $fileName . '\', \'\', \'' .str_replace($iceRoot, "", str_replace("|", "/", $_GET['oldFileName'])) . '\', false, \'' . $fileOrFolder . '\');'; } $finalAction = "move"; // Run our custom processes - include_once("../processes/on-file-dir-move.php"); + include_once "../processes/on-file-dir-move.php"; } else { - $doNext .= "ICEcoder.message('".$t['Sorry, cannot move']."\\\\n".str_replace("|","/",$_GET['oldFileName'])."\\\\n\\\\n".$t['Maybe public write...']."');"; + $doNext .= "ICEcoder.message('" . $t['Sorry, cannot move'] . "\\\\n" . str_replace("|", "/", $_GET['oldFileName']) . "\\\\n\\\\n" . $t['Maybe public write...'] . "');"; $finalAction = "nothing"; } } else { $doNext .= ""; $finalAction = "nothing"; } - $doNext .= 'ICEcoder.serverMessage();ICEcoder.serverQueue("del",0);'; + $doNext .= 'ICEcoder.serverMessage(); ICEcoder.serverQueue("del", 0);'; }; // ================== // RENAME FILE/FOLDER // ================== -if (!$error && $_GET['action']=="rename") { - if (!$demoMode && (isset($ftpSite) || is_writable($docRoot.$iceRoot.str_replace("|","/",$_GET['oldFileName'])))) { +if (!$error && "rename" === $_GET['action']) { + if (!$demoMode && (isset($ftpSite) || is_writable($docRoot.$iceRoot.str_replace("|", "/", $_GET['oldFileName'])))) { $updateFM = false; // FTP if (isset($ftpSite)) { - $ftpFilepath = ltrim($fileLoc."/".$fileName,"/"); - if (!ftpRename($ftpConn, ltrim($_GET['oldFileName'],"/"), $ftpFilepath)) { - $doNext .= 'ICEcoder.message("Sorry, could not rename '.ltrim($_GET['oldFileName'],"/").' to '.$ftpFilepath.'");'; + $ftpFilepath = ltrim($fileLoc . "/" . $fileName, "/"); + if (!ftpRename($ftpConn, ltrim($_GET['oldFileName'], "/"), $ftpFilepath)) { + $doNext .= 'ICEcoder.message("Sorry, could not rename ' . ltrim($_GET['oldFileName'], "/") . ' to ' . $ftpFilepath . '");'; } else { $updateFM = true; } // Local } else { - rename($docRoot.$iceRoot.str_replace("|","/",$_GET['oldFileName']),$docRoot.$fileLoc."/".$fileName); + rename($docRoot.$iceRoot.str_replace("|", "/", $_GET['oldFileName']), $docRoot . $fileLoc . "/" . $fileName); $updateFM = true; } // Update file manager on success if ($updateFM) { - $doNext .= 'ICEcoder.selectedFiles=[];ICEcoder.updateFileManagerList(\'rename\',\''.$fileLoc.'\',\''.$fileName.'\',\'\',\''.str_replace($iceRoot,"",$_GET['oldFileName']).'\');'; + $doNext .= 'ICEcoder.selectedFiles=[]; ICEcoder.updateFileManagerList(\'rename\', \'' . $fileLoc . '\', \'' . $fileName . '\', \'\', \'' . str_replace($iceRoot, "", $_GET['oldFileName']) . '\');'; } $finalAction = "rename"; // Run our custom processes - include_once("../processes/on-file-dir-rename.php"); + include_once "../processes/on-file-dir-rename.php"; } else { - $doNext .= "ICEcoder.message('".$t['Sorry, cannot rename']."\\\\n".$_GET['oldFileName']."\\\\n\\\\n".$t['Maybe public write...']."');"; + $doNext .= "ICEcoder.message('".$t['Sorry, cannot rename'] . "\\\\n" . $_GET['oldFileName'] . "\\\\n\\\\n" . $t['Maybe public write...'] . "');"; $finalAction = "nothing"; } - $doNext .= 'ICEcoder.serverMessage();ICEcoder.serverQueue("del",0);'; + $doNext .= 'ICEcoder.serverMessage(); ICEcoder.serverQueue("del", 0);'; }; // ================= // PASTE FILE/FOLDER // ================= -if (!isset($ftpSite) && !$error && $_GET['action']=="paste") { +if (!isset($ftpSite) && !$error && "paste" === $_GET['action']) { $source = $file; - $dest = str_replace("//","/",$docRoot.$iceRoot.str_replace("|","/",$_GET['location'])."/".basename($source)); + $dest = str_replace("//", "/", $docRoot . $iceRoot . str_replace("|", "/", $_GET['location']) . "/" . basename($source)); if (!$demoMode && is_writable(dirname($dest))) { if (is_dir($source)) { $fileOrFolder = "folder"; if (!is_dir($dest)) { mkdir($dest, octdec($ICEcoder['newDirPerms'])); } else { - for ($i=2; $i<1000000000; $i++) { - if (!is_dir($dest." (".$i.")")) { - $dest = $dest." (".$i.")"; + for ($i = 2; $i < 1000000000; $i++) { + if (!is_dir($dest . " (" . $i . ")")) { + $dest = $dest." (" . $i . ")"; mkdir($dest, octdec($ICEcoder['newDirPerms'])); - $i=1000000000; + $i = 1000000000; } } } @@ -682,9 +688,9 @@ if (!isset($ftpSite) && !$error && $_GET['action']=="paste") { RecursiveIteratorIterator::SELF_FIRST) as $item ) { if ($item->isDir()) { - mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName(), octdec($ICEcoder['newDirPerms'])); + mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), octdec($ICEcoder['newDirPerms'])); } else { - copy($item, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName()); + copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); } } } else { @@ -692,60 +698,60 @@ if (!isset($ftpSite) && !$error && $_GET['action']=="paste") { if (!file_exists($dest)) { copy($source, $dest); } else { - for ($i=2; $i<1000000000; $i++) { - if (!file_exists($dest." (".$i.")")) { - $dest = $dest." (".$i.")"; + for ($i = 2; $i < 1000000000; $i++) { + if (!file_exists($dest . " (" . $i . ")")) { + $dest = $dest . " (" . $i . ")"; copy($source, $dest); - $i=1000000000; + $i = 1000000000; } } } } // Reload file manager - $doNext .= 'ICEcoder.updateFileManagerList(\'add\',\''.str_replace("|","/",$_GET['location']).'\',\''.basename($dest).'\',false,false,false,\''.$fileOrFolder.'\');'; + $doNext .= 'ICEcoder.updateFileManagerList(\'add\', \'' . str_replace("|", "/", $_GET['location']) . '\', \'' . basename($dest) . '\', false, false, false, \'' . $fileOrFolder . '\');'; $finalAction = "pasteFile"; // Run our custom processes - include_once("../processes/on-file-dir-paste.php"); + include_once "../processes/on-file-dir-paste.php"; } else { - $doNext .= "ICEcoder.message('".$t['Sorry, cannot copy']." \\\\n".str_replace($docRoot,"",$source)."\\\\n ".$t['into']." \\\\n".str_replace($docRoot,"",$dest)."');"; + $doNext .= "ICEcoder.message('" . $t['Sorry, cannot copy'] . " \\\\n" . str_replace($docRoot, "", $source) . "\\\\n " . $t['into'] . " \\\\n" . str_replace($docRoot, "", $dest) . "');"; $finalAction = "nothing"; } - $doNext .= 'ICEcoder.serverMessage();ICEcoder.serverQueue("del",0);'; + $doNext .= 'ICEcoder.serverMessage(); ICEcoder.serverQueue("del", 0);'; }; // ============== // UPLOAD FILE(S) // ============== -if (!isset($ftpSite) && !$error && $_GET['action']=="upload") { +if (!isset($ftpSite) && !$error && "upload" === $_GET['action']) { if (!$demoMode) { class fileUploader { public function __construct($uploads) { - global $docRoot,$iceRoot,$ICEcoder,$doNext; - $uploadDir=$docRoot.$iceRoot.str_replace("..","",str_replace("|","/",$_POST['folder']."/")); + global $docRoot, $iceRoot, $ICEcoder, $doNext; + $uploadDir = $docRoot . $iceRoot . str_replace("..", "", str_replace("|", "/", $_POST['folder'] . "/")); foreach($uploads as $current) { - $this->uploadFile=$uploadDir.$current->name; + $this->uploadFile = $uploadDir . $current->name; $fileName = $current->name; // Get & set existing perms for existing files, or set to newFilePerms setting for new files if (file_exists($this->uploadFile)) { $chmodInfo = substr(sprintf('%o', fileperms($this->uploadFile)), -4); - $setPerms = substr($chmodInfo,1,3); // reduces 0755 down to 755 + $setPerms = substr($chmodInfo, 1, 3); // reduces 0755 down to 755 } else { $setPerms = $ICEcoder['newFilePerms']; } - if ($this->upload($current,$this->uploadFile,$setPerms)) { - $doNext .= 'ICEcoder.updateFileManagerList(\'add\',ICEcoder.selectedFiles[ICEcoder.selectedFiles.length-1].replace(/\|/g,\'/\'),\''.str_replace("'","\'",$fileName).'\',false,false,true,\'file\'); ICEcoder.serverMessage("'.$t['Uploaded file(s) OK'].'");setTimeout(function(){ICEcoder.serverMessage();},2000);'; + if ($this->upload($current, $this->uploadFile, $setPerms)) { + $doNext .= 'parent.parent.ICEcoder.updateFileManagerList(\'add\', parent.parent.ICEcoder.selectedFiles[parent.parent.ICEcoder.selectedFiles.length - 1].replace(/\|/g, \'/\'), \'' . str_replace("'", "\'", $fileName) . '\', false, false, true, \'file\'); parent.parent.ICEcoder.serverMessage("' . $t['Uploaded file(s) OK'] . '");setTimeout(function(){parent.parent.ICEcoder.serverMessage();}, 2000);'; $finalAction = "upload"; } else { - $doNext .= "ICEcoder.message('".$t['Sorry, cannot upload']." \\\\n".$fileName."\\\\n ".$t['into']." \\\\n'+ICEcoder.selectedFiles[ICEcoder.selectedFiles.length-1].replace(/\|/g,'/'));"; + $doNext .= "parent.parent.ICEcoder.message('" . $t['Sorry, cannot upload'] . " \\\\n" . $fileName . "\\\\n " . $t['into'] . " \\\\n' + parent.parent.ICEcoder.selectedFiles[parent.parent.ICEcoder.selectedFiles.length - 1].replace(/\|/g, '/'));"; $finalAction = "nothing"; } } } - public function upload($current,$uploadFile,$setPerms){ - if(move_uploaded_file($current->tmp_name,$uploadFile)){ - chmod($uploadFile,octdec($setPerms)); + public function upload($current, $uploadFile, $setPerms){ + if (move_uploaded_file($current->tmp_name, $uploadFile)){ + chmod($uploadFile, octdec($setPerms)); return true; } } @@ -753,26 +759,26 @@ if (!isset($ftpSite) && !$error && $_GET['action']=="upload") { function getDetails($fileArr) { foreach($fileArr['name'] as $keyee => $info) { - $uploads[$keyee]->name=xssClean($fileArr['name'][$keyee],"html"); - $uploads[$keyee]->type=$fileArr['type'][$keyee]; - $uploads[$keyee]->tmp_name=$fileArr['tmp_name'][$keyee]; - $uploads[$keyee]->error=$fileArr['error'][$keyee]; + $uploads[$keyee]->name = xssClean($fileArr['name'][$keyee], "html"); + $uploads[$keyee]->type = $fileArr['type'][$keyee]; + $uploads[$keyee]->tmp_name = $fileArr['tmp_name'][$keyee]; + $uploads[$keyee]->error = $fileArr['error'][$keyee]; } return $uploads; } - if($_FILES['filesInput']){ + if ($_FILES['filesInput']){ $uploads = getDetails($_FILES['filesInput']); - $fileUploader=new fileUploader($uploads); + $fileUploader = new fileUploader($uploads); } // Run our custom processes - include_once("../processes/on-file-upload.php"); + include_once "../processes/on-file-upload.php"; } else { - $doNext .= "ICEcoder.message('".$t['Sorry, cannot upload...']."');"; + $doNext .= "parent.parent.ICEcoder.message('" . $t['Sorry, cannot upload...'] . "');"; $finalAction = "nothing"; } - $doNext .= "ICEcoder.hideFileMenu();document.getElementById('fileInput').value='';ICEcoder.showHide('hide',document.getElementById('loadingMask'));"; + $doNext .= "parent.parent.ICEcoder.hideFileMenu(); parent.parent.document.getElementById('fileInput').value = ''; parent.parent.ICEcoder.showHide('hide', parent.parent.document.getElementById('loadingMask'));"; // Upload is not handled by XHR methods, but form post, so we need to manually trigger $doNext in a script tag echo ""; @@ -782,62 +788,66 @@ if (!isset($ftpSite) && !$error && $_GET['action']=="upload") { // DELETE FILE(S)/FOLDER(S) // ======================== -if (!$error && $_GET['action']=="delete") { - $filesArray = explode(";",$file); // May contain more than one file here +if (!$error && "delete" === $_GET['action']) { + $filesArray = explode(";", $file); // May contain more than one file here // FTP if (isset($ftpSite)) { - if (count($filesArray) == 1) { - $ftpFileDirInfo = ftpGetFileInfo($ftpConn, ltrim($fileLoc,"/"), $fileName); - $itemType = $ftpFileDirInfo['type'] == "directory" ? "dir" : "file"; - $itemPath = ltrim($fileLoc."/".$fileName,"/"); - if (!$demoMode && ftpDelete($ftpConn,$itemType,$itemPath)) { - if ($fileLoc=="" || $fileLoc=="\\") {$fileLoc="/";}; + if (1 === count($filesArray)) { + $ftpFileDirInfo = ftpGetFileInfo($ftpConn, ltrim($fileLoc, "/"), $fileName); + $itemType = "directory" === $ftpFileDirInfo['type'] ? "dir" : "file"; + $itemPath = ltrim($fileLoc . "/" . $fileName, "/"); + if (!$demoMode && ftpDelete($ftpConn, $itemType, $itemPath)) { + if ($fileLoc=="" || "\\" === $fileLoc) { + $fileLoc = "/"; + }; // Reload file manager - $doNext .= 'ICEcoder.selectedFiles=[];ICEcoder.updateFileManagerList(\'delete\',\''.$fileLoc.'\',\''.$fileName.'\');'; + $doNext .= 'ICEcoder.selectedFiles = []; ICEcoder.updateFileManagerList(\'delete\', \'' . $fileLoc . '\', \'' . $fileName . '\');'; $finalAction = "delete"; // Run our custom processes - include_once("../processes/on-file-dir-delete.php"); + include_once "../processes/on-file-dir-delete.php"; } else { - $doNext .= "ICEcoder.message('".$t['Sorry, cannot delete']."\\\\n".$fileLoc."/".$fileName."');"; + $doNext .= "ICEcoder.message('" . $t['Sorry, cannot delete'] . "\\\\n" . $fileLoc . "/" . $fileName . "');"; $finalAction = "nothing"; } } else { - $doNext .= "ICEcoder.message('".$t['Sorry, cannot delete more...']."');"; + $doNext .= "ICEcoder.message('" . $t['Sorry, cannot delete more...'] . "');"; $finalAction = "nothing"; } // Local } else { - for ($i=0;$i