Merge pull request #33 from frozenjava/master

Cabinet 1.1
pull/36/head
Marc 2018-07-24 01:16:15 +01:00 committed by GitHub
commit 2c593370fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 136 additions and 56 deletions

View File

@ -29,10 +29,15 @@ class Cabinet extends Module
case 'createFolder': case 'createFolder':
$this->createFolder(); $this->createFolder();
break; break;
case 'download':
$this->response = $this->download($this->request->filePath);
break;
} }
} }
protected function getDirectoryContents() private function getDirectoryContents()
{ {
$dir = $this->request->directory; $dir = $this->request->directory;
@ -43,7 +48,7 @@ class Cabinet extends Module
$obj = array("name" => $file, "directory" => is_dir($dir . '/' . $file), $obj = array("name" => $file, "directory" => is_dir($dir . '/' . $file),
"path" => realpath($dir . '/' . $file), "path" => realpath($dir . '/' . $file),
"permissions" => substr(sprintf('%o', fileperms($dir . '/' . $file)), -4), "permissions" => substr(sprintf('%o', fileperms($dir . '/' . $file)), -4),
"size" => filesize($dir . '/' . $file)); "size" => $this->readableFileSize($dir . '/' . $file));
array_push($contents, $obj); array_push($contents, $obj);
} }
$success = true; $success = true;
@ -53,7 +58,7 @@ class Cabinet extends Module
} }
protected function getParentDirectory() private function getParentDirectory()
{ {
$dir = $this->request->directory; $dir = $this->request->directory;
$success = false; $success = false;
@ -68,20 +73,13 @@ class Cabinet extends Module
} }
protected function deleteFile() private function deleteFile()
{ {
$f = $this->request->file; $f = $this->request->file;
$success = false; $success = false;
if (file_exists($f)) { if (file_exists($f)) {
if (!is_dir($f)) { exec("rm -rf " . escapeshellarg($f));
unlink($f);
} else {
foreach (preg_grep('/^([^.])/', scandir($f)) as $file) {
unlink($f . '/' . $file);
}
rmdir($f);
}
} }
if (!file_exists($f)) { if (!file_exists($f)) {
@ -92,7 +90,7 @@ class Cabinet extends Module
} }
protected function editFile() private function editFile()
{ {
$f = $this->request->file; $f = $this->request->file;
$data = $this->request->contents; $data = $this->request->contents;
@ -106,22 +104,24 @@ class Cabinet extends Module
$this->response = array("success" => $success); $this->response = array("success" => $success);
} }
protected function getFileContents() private function getFileContents()
{ {
$f = $this->request->file; $f = $this->request->file;
$success = false; $success = false;
$content = ""; $content = "";
$size = "0 Bytes";
if (file_exists($f)) { if (file_exists($f)) {
$success = true; $success = true;
$content = file_get_contents($f); $content = file_get_contents($f);
$size = $this->readableFileSize($f);
} }
$this->response = array("success" => $success, "content" => $content); $this->response = array("success" => $success, "content" => $content, "size" => $size);
} }
protected function createFolder() private function createFolder()
{ {
$dir = $this->request->directory; $dir = $this->request->directory;
$name = $this->request->name; $name = $this->request->name;
@ -135,4 +135,39 @@ class Cabinet extends Module
$this->response = array("success" => $success); $this->response = array("success" => $success);
} }
/**
* Download a file
* @param: The path to the file to download
* @return array : array
*/
private function download($filePath)
{
if (file_exists($filePath)) {
return array("success" => true, "message" => null, "download" => $this->downloadFile($filePath));
} else {
return array("success" => false, "message" => "File does not exist", "download" => null);
}
}
/**
* Get the size of a file and add a unit to the end of it.
* @param $file: The file to get size of
* @return string: File size plus unit. Exp: 3.14M
*/
private function readableFileSize($file) {
$size = filesize($file);
if ($size == null)
return "0 Bytes";
if ($size < 1024) {
return "{$size} Bytes";
} else if ($size >= 1024 && $size < 1024*1024) {
return round($size / 1024, 2) . "K";
} else if ($size >= 1024*1024) {
return round($size / (1024*1024), 2) . "M";
}
return "{$size} Bytes";
}
} }

View File

@ -11,11 +11,11 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope
$scope.showMessage = function(msgTitle, msgBody) { $scope.showMessage = function(msgTitle, msgBody) {
$scope.message = {title: msgTitle, body: msgBody}; $scope.message = {title: msgTitle, body: msgBody};
$('#messageModal').modal("show"); $('#messageModal').modal("show");
} };
$scope.submitChangeDirectory = function(directory) { $scope.submitChangeDirectory = function(directory) {
console.log(directory); console.log(directory);
} };
$scope.getDirectoryContents = function(dir) { $scope.getDirectoryContents = function(dir) {
$api.request({ $api.request({
@ -38,7 +38,7 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope
$scope.showMessage("Error Loading Directory", "There was an error loading directory contents. Please verify that the directory you are navigating to exists."); $scope.showMessage("Error Loading Directory", "There was an error loading directory contents. Please verify that the directory you are navigating to exists.");
} }
}); });
} };
$scope.goToParentDirctory = function() { $scope.goToParentDirctory = function() {
$api.request({ $api.request({
@ -53,14 +53,14 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope
$scope.showMessage("Error Finding Parent Directory", "An error occured while trying to find the parent directory. Please verify that the directory you are navigating to exists."); $scope.showMessage("Error Finding Parent Directory", "An error occured while trying to find the parent directory. Please verify that the directory you are navigating to exists.");
} }
}); });
} };
$scope.requestDeleteFile = function(file) { $scope.requestDeleteFile = function(file) {
$scope.deleteFile.name = file.name; $scope.deleteFile.name = file.name;
$scope.deleteFile.path = file.path; $scope.deleteFile.path = file.path;
$scope.deleteFile.directory = file.directory; $scope.deleteFile.directory = file.directory;
console.log($scope.deleteFile); console.log($scope.deleteFile);
} };
$scope.sendDeleteFile = function() { $scope.sendDeleteFile = function() {
$api.request({ $api.request({
@ -75,7 +75,7 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope
$scope.showMessage("Error Deleting File", "An error occured while trying to delete the file " + $scope.deleteFile.path + ". Please verify that this file exists and you have permission to delete it."); $scope.showMessage("Error Deleting File", "An error occured while trying to delete the file " + $scope.deleteFile.path + ". Please verify that this file exists and you have permission to delete it.");
} }
}); });
} };
$scope.requestEditFile = function(file) { $scope.requestEditFile = function(file) {
$api.request({ $api.request({
@ -84,12 +84,12 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope
file: file.path file: file.path
}, function(response) { }, function(response) {
if (response.success == true) { if (response.success == true) {
$scope.editFile = {name: file.name, path: file.path, content: response.content}; $scope.editFile = {name: file.name, path: file.path, content: response.content, size: response.size};
} else { } else {
$scope.showMessage("Error Loading File Contents", "An error occured while trying to load the file " + file.name + ". Please verify that this file exists and you have permission to edit it."); $scope.showMessage("Error Loading File Contents", "An error occured while trying to load the file " + file.name + ". Please verify that this file exists and you have permission to edit it.");
} }
}); });
} };
$scope.sendEditFile = function() { $scope.sendEditFile = function() {
$api.request({ $api.request({
@ -105,7 +105,7 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope
$scope.showMessage("Error Saving File", "An error occured while trying to save the file " + $scope.editFile.name + ". Please verify that this file exists and you have permission to edit it."); $scope.showMessage("Error Saving File", "An error occured while trying to save the file " + $scope.editFile.name + ". Please verify that this file exists and you have permission to edit it.");
} }
}); });
} };
$scope.createFolder = function() { $scope.createFolder = function() {
$api.request({ $api.request({
@ -121,7 +121,21 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope
$scope.showMessage("Error Creating Directory", "An error occured while trying to create the folder " + $scope.newFolder.name + ". Please verify that you have permission to create new items in this directory."); $scope.showMessage("Error Creating Directory", "An error occured while trying to create the folder " + $scope.newFolder.name + ". Please verify that you have permission to create new items in this directory.");
} }
}); });
} };
$scope.download = function(filePath) {
$api.request({
module: "Cabinet",
action: "download",
filePath: filePath
}, function (response) {
if (!response.success) {
$scope.showMessage("Error", response.message);
return;
}
window.location = "/api/?download=" + response.download;
})
};
$scope.getDirectoryContents($scope.currentDirectory); $scope.getDirectoryContents($scope.currentDirectory);

View File

@ -3,32 +3,34 @@
<div class="col-md-12"> <div class="col-md-12">
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">
<h3 class="panel-title">Cabinet File Manager: Current Directory {{ currentDirectory }}</h3> <h3 class="panel-title">{{ currentDirectory }}</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<form class="form-inline" role="form"> <form class="form-inline pull-right" role="form">
<div class="form-group"> <div class="input-group">
<label for="userDirectory">Change Directory:</label> <input type="text" placeholder="Change Directory" class="form-control" ng-model="userDirectory">
<input type="text" class="form-control" ng-model="userDirectory"> <span class="input-group-btn">
<button type="submit" class="btn btn-default" ng-click="getDirectoryContents(userDirectory)">Go</button>
</span>
</div> </div>
<button type="submit" class="btn btn-default" ng-click="getDirectoryContents(userDirectory)">Go</button>
</form> </form>
<span class="input-group-btn pull-left">
<button type="button" class="btn btn-default btn-sm" ng-show="currentDirectory == '/'" disabled>Back</button>
<button type="button" class="btn btn-default btn-sm" ng-click="goToParentDirctory()" ng-hide="currentDirectory == '/'">Back</button>
<button type="button" class="btn btn-default btn-sm" data-toggle="modal" data-target="#fileModal">New File</button>
<button type="button" class="btn btn-default btn-sm" data-toggle="modal" data-target="#folderModal">New Folder</button>
<button type="button" class="btn btn-default btn-sm" ng-click="getDirectoryContents(currentDirectory)">Refresh</button>
</span>
<br />
<hr/> <hr/>
<form class="form-inline" role="form">
<button type="submit" class="btn btn-default btn-sm" ng-show="currentDirectory == '/'" disabled>Go Back</button>
<button type="submit" class="btn btn-default btn-sm" ng-click="goToParentDirctory()" ng-hide="currentDirectory == '/'">Go Back</button>
<button type="submit" class="btn btn-default btn-sm" data-toggle="modal" data-target="#fileModal">New File</button>
<button type="submit" class="btn btn-default btn-sm" data-toggle="modal" data-target="#folderModal">New Folder</button>
<button type="submit" class="btn btn-default btn-sm" ng-click="getDirectoryContents(currentDirectory)">Refresh</button>
</form>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-striped" align="center"> <table class="table table-striped" align="center">
<thead> <thead>
<th>File Name</th> <th>File Name</th>
<th>Location</th> <th>Location</th>
<th>Permissions</th> <th>Permissions</th>
<th>Bytes</th> <th>Size</th>
<th>Delete</th> <th>Delete</th>
<th>Edit</th> <th>Edit</th>
</thead> </thead>
@ -51,6 +53,36 @@
<!-- Edit file Modal --> <!-- Edit file Modal -->
<div id="fileModal" class="modal fade" role="dialog"> <div id="fileModal" class="modal fade" role="dialog">
<script type="text/javascript">
// Thanks to sud0nick on forums.hak5.org for this snippet
$(document).delegate('#cabinetEditor', 'keydown', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
for (var i = 0; i < 4; i++) {
// get caret position/selection
var start = this.selectionStart;
var end = this.selectionEnd;
var $this = $(this);
var value = $this.val();
// set textarea value to: text before caret + tab + text after caret
$this.val(value.substring(0, start)
+ " "
+ value.substring(end));
// put caret at right position again (add one for the tab)
this.selectionStart = this.selectionEnd = start + 1;
// prevent the focus lose
e.preventDefault();
}
}
});
</script>
<div class="modal-dialog"> <div class="modal-dialog">
<!-- Modal content--> <!-- Modal content-->
@ -60,20 +92,19 @@
<h4 class="modal-title">File Editor {{ editFile.name }}</h4> <h4 class="modal-title">File Editor {{ editFile.name }}</h4>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<form class="form-horizontal"> <label class="control-label">File Name</label>
<div class="form-group"> <input type="text" class="form-control" ng-model="editFile.name" placeholder="Notes.txt">
<label class="control-label">File Name</label> <br />
<input type="text" class="form-control" ng-model="editFile.name" placeholder="Notes.txt"> <label class="control-label">File Contents</label>
</div> <a href="javascript:;" ng-click="editFile.content = ''" class="pull-right">Clear</a>
<div class="form-group"> <textarea class="form-control" rows="10" id="cabinetEditor" ng-model="editFile.content" placeholder="Write some text here"></textarea>
<label class="control-label">File Contents</label> <a href="javascript:;" ng-click="editFile.content = ''">Clear</a> <label class="control-label pull-right">File Size: {{ editFile.size }}</label>
<textarea class="form-control" rows="10" ng-model="editFile.content" placeholder="Write some text here"></textarea> <a href="javascript:;" class="pull-left" ng-click="download(editFile.path)">Download</a>
</div> <br />
</form>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" ng-click="sendEditFile()" class="btn btn-success pull-left" data-dismiss="modal">Save</button> <button type="button" ng-click="sendEditFile()" class="btn btn-success pull-right" data-dismiss="modal">Save</button>
<button type="button" class="btn btn-default pull-right" data-dismiss="modal" ng-click="editFile = {}">Cancel</button> <button type="button" class="btn btn-default pull-left" data-dismiss="modal" ng-click="editFile = {}">Cancel</button>
</div> </div>
</div> </div>
</div> </div>
@ -95,8 +126,8 @@
<p><b>Are you absolutely sure you want to delete {{ deleteFile.name }} located at {{ deleteFile.path }}?</b></p> <p><b>Are you absolutely sure you want to delete {{ deleteFile.name }} located at {{ deleteFile.path }}?</b></p>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-primary pull-left" data-dismiss="modal" ng-click="deleteFile = {}">Cancel</button> <button type="button" class="btn btn-primary pull-right" data-dismiss="modal" ng-click="deleteFile = {}">Cancel</button>
<button type="button" class="btn btn-danger pull-right" data-dismiss="modal" ng-click="sendDeleteFile()">Delete</button> <button type="button" class="btn btn-danger pull-left" data-dismiss="modal" ng-click="sendDeleteFile()">Delete</button>
</div> </div>
</div> </div>
</div> </div>

View File

@ -6,5 +6,5 @@
"tetra" "tetra"
], ],
"title": "Cabinet", "title": "Cabinet",
"version": "1.0" "version": "1.1"
} }