Cabinet 1.1
parent
af3ed0d98c
commit
17f462a497
|
@ -29,10 +29,15 @@ class Cabinet extends Module
|
|||
case 'createFolder':
|
||||
$this->createFolder();
|
||||
break;
|
||||
|
||||
case 'download':
|
||||
$this->response = $this->download($this->request->filePath);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected function getDirectoryContents()
|
||||
private function getDirectoryContents()
|
||||
{
|
||||
$dir = $this->request->directory;
|
||||
|
||||
|
@ -43,7 +48,7 @@ class Cabinet extends Module
|
|||
$obj = array("name" => $file, "directory" => is_dir($dir . '/' . $file),
|
||||
"path" => realpath($dir . '/' . $file),
|
||||
"permissions" => substr(sprintf('%o', fileperms($dir . '/' . $file)), -4),
|
||||
"size" => filesize($dir . '/' . $file));
|
||||
"size" => $this->readableFileSize($dir . '/' . $file));
|
||||
array_push($contents, $obj);
|
||||
}
|
||||
$success = true;
|
||||
|
@ -53,7 +58,7 @@ class Cabinet extends Module
|
|||
|
||||
}
|
||||
|
||||
protected function getParentDirectory()
|
||||
private function getParentDirectory()
|
||||
{
|
||||
$dir = $this->request->directory;
|
||||
$success = false;
|
||||
|
@ -68,20 +73,13 @@ class Cabinet extends Module
|
|||
|
||||
}
|
||||
|
||||
protected function deleteFile()
|
||||
private function deleteFile()
|
||||
{
|
||||
$f = $this->request->file;
|
||||
$success = false;
|
||||
|
||||
if (file_exists($f)) {
|
||||
if (!is_dir($f)) {
|
||||
unlink($f);
|
||||
} else {
|
||||
foreach (preg_grep('/^([^.])/', scandir($f)) as $file) {
|
||||
unlink($f . '/' . $file);
|
||||
}
|
||||
rmdir($f);
|
||||
}
|
||||
exec("rm -rf " . escapeshellarg($f));
|
||||
}
|
||||
|
||||
if (!file_exists($f)) {
|
||||
|
@ -92,7 +90,7 @@ class Cabinet extends Module
|
|||
|
||||
}
|
||||
|
||||
protected function editFile()
|
||||
private function editFile()
|
||||
{
|
||||
$f = $this->request->file;
|
||||
$data = $this->request->contents;
|
||||
|
@ -106,22 +104,24 @@ class Cabinet extends Module
|
|||
$this->response = array("success" => $success);
|
||||
}
|
||||
|
||||
protected function getFileContents()
|
||||
private function getFileContents()
|
||||
{
|
||||
$f = $this->request->file;
|
||||
$success = false;
|
||||
$content = "";
|
||||
$size = "0 Bytes";
|
||||
|
||||
if (file_exists($f)) {
|
||||
$success = true;
|
||||
$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;
|
||||
$name = $this->request->name;
|
||||
|
@ -135,4 +135,39 @@ class Cabinet extends Module
|
|||
$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";
|
||||
}
|
||||
|
||||
}
|
|
@ -11,11 +11,11 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope
|
|||
$scope.showMessage = function(msgTitle, msgBody) {
|
||||
$scope.message = {title: msgTitle, body: msgBody};
|
||||
$('#messageModal').modal("show");
|
||||
}
|
||||
};
|
||||
|
||||
$scope.submitChangeDirectory = function(directory) {
|
||||
console.log(directory);
|
||||
}
|
||||
};
|
||||
|
||||
$scope.getDirectoryContents = function(dir) {
|
||||
$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.goToParentDirctory = function() {
|
||||
$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.requestDeleteFile = function(file) {
|
||||
$scope.deleteFile.name = file.name;
|
||||
$scope.deleteFile.path = file.path;
|
||||
$scope.deleteFile.directory = file.directory;
|
||||
console.log($scope.deleteFile);
|
||||
}
|
||||
};
|
||||
|
||||
$scope.sendDeleteFile = function() {
|
||||
$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.requestEditFile = function(file) {
|
||||
$api.request({
|
||||
|
@ -84,12 +84,12 @@ registerController("CabinetController", ['$api', '$scope', function($api, $scope
|
|||
file: file.path
|
||||
}, function(response) {
|
||||
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 {
|
||||
$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() {
|
||||
$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.createFolder = function() {
|
||||
$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.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);
|
||||
|
||||
|
|
|
@ -3,32 +3,34 @@
|
|||
<div class="col-md-12">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">Cabinet File Manager: Current Directory {{ currentDirectory }}</h3>
|
||||
<h3 class="panel-title">{{ currentDirectory }}</h3>
|
||||
</div>
|
||||
|
||||
<div class="panel-body">
|
||||
<form class="form-inline" role="form">
|
||||
<div class="form-group">
|
||||
<label for="userDirectory">Change Directory:</label>
|
||||
<input type="text" class="form-control" ng-model="userDirectory">
|
||||
<form class="form-inline pull-right" role="form">
|
||||
<div class="input-group">
|
||||
<input type="text" placeholder="Change Directory" 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>
|
||||
<button type="submit" class="btn btn-default" ng-click="getDirectoryContents(userDirectory)">Go</button>
|
||||
</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/>
|
||||
<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">
|
||||
<table class="table table-striped" align="center">
|
||||
<thead>
|
||||
<th>File Name</th>
|
||||
<th>Location</th>
|
||||
<th>Permissions</th>
|
||||
<th>Bytes</th>
|
||||
<th>Size</th>
|
||||
<th>Delete</th>
|
||||
<th>Edit</th>
|
||||
</thead>
|
||||
|
@ -51,6 +53,36 @@
|
|||
|
||||
<!-- Edit file Modal -->
|
||||
<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">
|
||||
|
||||
<!-- Modal content-->
|
||||
|
@ -60,20 +92,19 @@
|
|||
<h4 class="modal-title">File Editor {{ editFile.name }}</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form class="form-horizontal">
|
||||
<div class="form-group">
|
||||
<label class="control-label">File Name</label>
|
||||
<input type="text" class="form-control" ng-model="editFile.name" placeholder="Notes.txt">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="control-label">File Contents</label> <a href="javascript:;" ng-click="editFile.content = ''">Clear</a>
|
||||
<textarea class="form-control" rows="10" ng-model="editFile.content" placeholder="Write some text here"></textarea>
|
||||
</div>
|
||||
</form>
|
||||
<label class="control-label">File Name</label>
|
||||
<input type="text" class="form-control" ng-model="editFile.name" placeholder="Notes.txt">
|
||||
<br />
|
||||
<label class="control-label">File Contents</label>
|
||||
<a href="javascript:;" ng-click="editFile.content = ''" class="pull-right">Clear</a>
|
||||
<textarea class="form-control" rows="10" id="cabinetEditor" ng-model="editFile.content" placeholder="Write some text here"></textarea>
|
||||
<label class="control-label pull-right">File Size: {{ editFile.size }}</label>
|
||||
<a href="javascript:;" class="pull-left" ng-click="download(editFile.path)">Download</a>
|
||||
<br />
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" ng-click="sendEditFile()" class="btn btn-success pull-left" 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" ng-click="sendEditFile()" class="btn btn-success pull-right" data-dismiss="modal">Save</button>
|
||||
<button type="button" class="btn btn-default pull-left" data-dismiss="modal" ng-click="editFile = {}">Cancel</button>
|
||||
</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>
|
||||
</div>
|
||||
<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-danger pull-right" data-dismiss="modal" ng-click="sendDeleteFile()">Delete</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-left" data-dismiss="modal" ng-click="sendDeleteFile()">Delete</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -6,5 +6,5 @@
|
|||
"tetra"
|
||||
],
|
||||
"title": "Cabinet",
|
||||
"version": "1.0"
|
||||
"version": "1.1"
|
||||
}
|
Loading…
Reference in New Issue