diff --git a/classes/FTP.php b/classes/FTP.php index a504711..c94dd1e 100644 --- a/classes/FTP.php +++ b/classes/FTP.php @@ -19,7 +19,7 @@ class FTP $ftpFilepath = ltrim($fileLoc . "/" . $fileName, "/"); if (isset($_POST['changes'])) { // Get existing file contents as lines - $loadedFile = toUTF8noBOM(ftpGetContents($ftpConn, $ftpRoot . $fileLoc . "/" . $fileName, $ftpMode), false); + $loadedFile = toUTF8noBOM($this->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 @@ -53,11 +53,136 @@ class FTP } } // Write our file contents - if (!ftpWriteFile($ftpConn, $ftpFilepath, $contents, $ftpMode)) { + if (!$this->ftpWriteFile($ftpConn, $ftpFilepath, $contents, $ftpMode)) { $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();'; } } + + // Start a FTP connection + function ftpStart() + { + global $ftpConn, $ftpLogin, $ftpHost, $ftpUser, $ftpPass, $ftpPasv; + + // Establish connection, login and maybe use pasv + $ftpConn = ftp_connect($ftpHost); + $ftpLogin = ftp_login($ftpConn, $ftpUser, $ftpPass); + if ($ftpPasv) { + ftp_pasv($ftpConn, true); + } + } + + // End a FTP connection + function ftpEnd() + { + global $ftpConn; + + ftp_close($ftpConn); + } + + // Get dir/file lists (simple and detailed) from FTP detailed rawlist response + function ftpGetList($ftpConn, $directory = '.') + { + $simpleList = $detailedList = array(); + // If we have a FTP rawlist to work with + if (is_array($rows = @ftp_rawlist($ftpConn, $directory))) { + foreach ($rows as $row) { + // Split row up by spaces and set keys on $item array + $chunks = preg_split("/\s+/", $row); + list($item['rights'], $item['number'], $item['user'], $item['group'], $item['size'], $item['month'], $item['day'], $item['time']) = $chunks; + // Also set if this is a dir or file + $item['type'] = $chunks[0][0] === 'd' ? 'directory' : 'file'; + // Splice the array and finally work out $simpleList and $detailedList + array_splice($chunks, 0, 8); + $detailedList[implode(" ", $chunks)] = $item; + $simpleList[] = implode(" ", $chunks); + } + // Return simple array list and detailed items list also + return array('simpleList' => $simpleList, 'detailedList' => $detailedList); + } + return false; + } + + // Get detailed info on a file from returned info from ftpGetList + function ftpGetFileInfo($ftpConn, $directory = '.', $fileName) + { + // Get both sets of arrays back and get our detailed list + $ftpListArrays = $this->ftpGetList($ftpConn, $directory); + $detailedList = $ftpListArrays['detailedList']; + + // Now get the file info for our file + $fileInfo = $detailedList[$fileName]; + + // Return the info + return $fileInfo; + } + + // Get contents over FTP + function ftpGetContents($ftpConn, $filepath, $ftpMode) + { + // Create temp handler, this type needed for extended char set + $tempHandle = fopen('php://temp', 'r+'); + + // Get file from FTP assuming that it exists + ftp_fget($ftpConn, $tempHandle, $filepath, $ftpMode, 0); + + // Return our content + return stream_get_contents($tempHandle, -1, 0); + } + + // Write file contents over FTP + function ftpWriteFile($ftpConn, $filepath, $contents, $ftpMode) + { + // Create temp handler, this type needed for extended char set + $tempHandle = fopen('php://temp', 'r+'); + + // Write contents to handle and rewind head + fwrite($tempHandle, $contents); + rewind($tempHandle); + + // Write our content and return true/false + return ftp_fput($ftpConn, $filepath, $tempHandle, $ftpMode, 0); + } + + // Make a new dir over FTP + function ftpMkDir($ftpConn, $perms, $dir) + { + // Create the new dir + if (!ftp_mkdir($ftpConn, $dir)) { + return false; + } else { + // Also then set perms (we must be able to do that if we created dir, so can always return true) + $this->ftpPerms($ftpConn, $perms, $dir); + return true; + } + } + + // Rename a dir/dile over FTP + function ftpRename($ftpConn, $oldPath, $newPath) + { + // Return success status of rename + return ftp_rename($ftpConn, $oldPath, $newPath); + } + + // Change dir/file perms over FTP + function ftpPerms($ftpConn, $perms, $filePath) + { + // Return success status of perms change + return ftp_chmod($ftpConn, $perms, $filePath); + } + + // Delete dir/file over FTP + function ftpDelete($ftpConn, $type, $path) + { + if ($type == "file") { + // Delete our file and return true/false + return ftp_delete($ftpConn, $path); + } else { + // Delete our dir and return true/false + return ftp_rmdir($ftpConn, $path); + } + } + } diff --git a/classes/File.php b/classes/File.php index 976e5e3..42853b6 100644 --- a/classes/File.php +++ b/classes/File.php @@ -4,15 +4,17 @@ namespace ICEcoder; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; +use ICEcoder\FTP; use ICEcoder\System; class File { - + private $ftpClass; private $system; public function __construct() { + $this->ftpClass = new FTP(); $this->system = new System(); } @@ -145,14 +147,14 @@ class File // Get file over FTP? if (isset($ftpSite)) { - ftpStart(); + $this->ftpClass->ftpStart(); // Show user warning if no good connection if (!$ftpConn || !$ftpLogin) { die('parent.parent.ICEcoder.message("Sorry, no FTP connection to ' . $ftpHost . ' for user ' . $ftpUser . '");parent.parent.ICEcoder.serverMessage();parent.parent.ICEcoder.serverQueue("del",0);'); } // Get our file contents and close the FTP connection - $loadedFile = toUTF8noBOM(ftpGetContents($ftpConn, $ftpRoot . $fileLoc . "/" . $fileName, $ftpMode), false); - ftpEnd(); + $loadedFile = toUTF8noBOM($this->ftpClass->ftpGetContents($ftpConn, $ftpRoot . $fileLoc . "/" . $fileName, $ftpMode), false); + $this->ftpClass->ftpEnd(); // Get local file } else { $loadedFile = toUTF8noBOM(getData($file), true); @@ -640,9 +642,9 @@ class File if (isset($ftpSite)) { // Get info on dir/file now - $ftpFileDirInfo = ftpGetFileInfo($ftpConn, ltrim($fileLoc, "/"), $fileName); + $ftpFileDirInfo = $this->ftpClass->ftpGetFileInfo($ftpConn, ltrim($fileLoc, "/"), $fileName); // End the connection - ftpEnd(); + $this->ftpClass->ftpEnd(); // Then set info $itemAbsPath = $ftpRoot . $fileLoc . '/' . $fileName; $itemPath = dirname($ftpRoot.$fileLoc . '/' . $fileName); diff --git a/lib/file-control.php b/lib/file-control.php index 9421c7b..ea5096f 100644 --- a/lib/file-control.php +++ b/lib/file-control.php @@ -58,7 +58,7 @@ if (false === $error) { $doNext = ""; // If we're in FTP mode, start a connection and leave open for FTP actions if (isset($ftpSite)) { - ftpStart(); + $ftpClass->ftpStart(); // Show user warning if no good connection if (!$ftpConn || !$ftpLogin) { $doNext .= 'ICEcoder.message("Sorry, no FTP connection to ' . $ftpHost . ' for user ' . $ftpUser . '");'; @@ -184,7 +184,7 @@ if (!$error && "newFolder" === $_GET['action']) { // FTP if (isset($ftpSite)) { $ftpFilepath = ltrim($fileLoc . "/" . $fileName, "/"); - if (!ftpMkDir($ftpConn, octdec($ICEcoder['newDirPerms']), $ftpFilepath)) { + if (!$ftpClass->ftpMkDir($ftpConn, octdec($ICEcoder['newDirPerms']), $ftpFilepath)) { $doNext .= 'ICEcoder.message("Sorry, could not create dir '.$ftpFilepath.' at ' . $ftpHost . '");'; } else { $fileClass->updateFileManager('add', $fileLoc, $fileName, '', '', '', 'folder'); @@ -221,10 +221,10 @@ if (!$error && "move" === $_GET['action']) { if (!$demoMode && (isset($ftpSite) || is_writable($srcDir))) { // FTP if (isset($ftpSite)) { - if (!ftpRename($ftpConn, $srcDir, $tgtDir)) { + if (!$ftpClass->ftpRename($ftpConn, $srcDir, $tgtDir)) { $doNext .= 'ICEcoder.message("Sorry, could not rename ' . $srcDir . ' to ' . $tgtDir . '");'; } else { - $ftpFileDirInfo = ftpGetFileInfo($ftpConn, ltrim($fileLoc, "/"), $fileName); + $ftpFileDirInfo = $ftpClass->ftpGetFileInfo($ftpConn, ltrim($fileLoc, "/"), $fileName); $fileOrFolder = "directory" === $ftpFileDirInfo['type'] ? "folder" : "file"; $fileClass->updateFileManager('move', $fileLoc, $fileName, '', str_replace($iceRoot, "", str_replace("|", "/", $_GET['oldFileName'])), '', $fileOrFolder); } @@ -260,7 +260,7 @@ if (!$error && "rename" === $_GET['action']) { // FTP if (isset($ftpSite)) { $ftpFilepath = ltrim($fileLoc . "/" . $fileName, "/"); - if (!ftpRename($ftpConn, ltrim($_GET['oldFileName'], "/"), $ftpFilepath)) { + if (!$ftpClass->ftpRename($ftpConn, ltrim($_GET['oldFileName'], "/"), $ftpFilepath)) { $doNext .= 'ICEcoder.message("Sorry, could not rename ' . ltrim($_GET['oldFileName'], "/") . ' to ' . $ftpFilepath . '");'; } else { $fileClass->updateFileManager('rename', $fileLoc, $fileName, '', str_replace($iceRoot, "", $_GET['oldFileName']), '', ''); @@ -340,10 +340,10 @@ if (!$error && "delete" === $_GET['action']) { // FTP if (isset($ftpSite)) { if (1 === count($filesArray)) { - $ftpFileDirInfo = ftpGetFileInfo($ftpConn, ltrim($fileLoc, "/"), $fileName); + $ftpFileDirInfo = $ftpClass->ftpGetFileInfo($ftpConn, ltrim($fileLoc, "/"), $fileName); $itemType = "directory" === $ftpFileDirInfo['type'] ? "dir" : "file"; $itemPath = ltrim($fileLoc . "/" . $fileName, "/"); - if (!$demoMode && ftpDelete($ftpConn, $itemType, $itemPath)) { + if (!$demoMode && $ftpClass->ftpDelete($ftpConn, $itemType, $itemPath)) { if ($fileLoc=="" || "\\" === $fileLoc) { $fileLoc = "/"; }; @@ -427,7 +427,7 @@ if (!$error && "perms" === $_GET['action']) { // FTP if (isset($ftpSite)) { $ftpFilepath = ltrim($fileLoc . "/" . $fileName, "/"); - if (!ftpPerms($ftpConn, octdec(numClean($_GET['perms'])), $ftpFilepath)) { + if (!$ftpClass->ftpPerms($ftpConn, octdec(numClean($_GET['perms'])), $ftpFilepath)) { $doNext .= 'ICEcoder.message("Sorry, could not set perms on ' . $ftpFilepath . ' at ' . $ftpHost . '");'; } else { $fileClass->updateFileManager('chmod', $fileLoc, $fileName, numClean($_GET['perms']), '', '', ''); diff --git a/lib/get-branch.php b/lib/get-branch.php index 61806be..7271aa4 100644 --- a/lib/get-branch.php +++ b/lib/get-branch.php @@ -1,9 +1,9 @@ ftpStart(); // Show user warning if no good connection if (!$ftpConn || !$ftpLogin) { die(''); exit; } // Get our simple and detailed lists and close the FTP connection - $ftpList = ftpGetList($ftpConn, $ftpRoot . $location); + $ftpList = $ftpClass->ftpGetList($ftpConn, $ftpRoot . $location); $finalArray = $ftpList['simpleList']; $ftpItems = $ftpList['detailedList']; - ftpEnd(); + $ftpClass->ftpEnd(); // or get local list } else { $finalArray = scanDir($scanDir . $location); diff --git a/lib/icecoder.php b/lib/icecoder.php index 1b4d144..b02f192 100644 --- a/lib/icecoder.php +++ b/lib/icecoder.php @@ -10,6 +10,3 @@ require_once "../classes/URL.php"; // Headers & Settings require_once "headers.php"; require_once "settings.php"; - -// FTP Control -require_once "ftp-control.php";