mirror of
https://github.com/hak5/nano-tetra-modules.git
synced 2025-10-29 16:58:09 +00:00
Cabinet 1.1
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user