Refactored code and logic plus fixed PHPSESSIONID issue.

This commit is contained in:
Maxim Stewart 2018-04-22 20:43:19 -05:00
parent eaacecfd37
commit f336a5ec81
16 changed files with 266 additions and 285 deletions

View File

@ -21,7 +21,7 @@ Notes:
1. Allow for move and copy.
3. Fix the ' and & naming issue.
4. Implement themes functionality.
5. Look to refactor code.
# Images
![1 Home](Images/1.png)

View File

@ -20,9 +20,9 @@
<input type="text" id="DIRPATHUL" name="DIRPATHUL" value="">
</form>
<br/>
<input type="text" id="NewItem" style="text-align: center; background-color: #ffffff; color: #000000;" value="" />
<input type="button" value="New Dir" onclick="createDir()">
<input type="button" value="New File" onclick="createFile()">
<input type="text" id="NewItem" value="" />
<input type="button" value="New Dir" onclick="createItem('dir')">
<input type="button" value="New File" onclick="createItem('file')">
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;Path:&nbsp;&nbsp;<span id="path"></span>
</h2>
@ -46,11 +46,11 @@
</menu>
<script type="text/javascript" src="resources/js/ajax.js" charset="utf-8"></script>
<script type="text/javascript" src="resources/js/cookieHandler.js" charset="utf-8"></script>
<script type="text/javascript" src="resources/js/xmlParser.js" charset="utf-8"></script>
<script type="text/javascript" src="resources/js/uiControls.js" charset="utf-8"></script>
<script type="text/javascript" src="resources/js/fileSystemControls.js" charset="utf-8"></script>
<script type="text/javascript" src="resources/js/ajax.js" charset="utf-8"></script>
<script type="text/javascript" src="resources/js/uiActions.js" charset="utf-8"></script>
<script type="text/javascript" src="resources/js/filesystemActions.js" charset="utf-8"></script>
<script type="text/javascript" src="resources/js/uiEvents.js" charset="utf-8"></script>
<script type="text/javascript" src="resources/js/dragContainerEvents.js" charset="utf-8"></script>
</body>
</html>

View File

@ -82,6 +82,11 @@
#imgView { overflow: hidden; left: 15em; }
#NewItem {
background-color: #ffffff;
color: #000000;
text-align: center;
}
/* Classes */
.imgViewImg {

View File

@ -1,24 +1,37 @@
var pathNodes = [];
// SSE events if supported
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("resources/php/sse.php");
source.onmessage = function(event) {
if (event.data === "updateListing") {
getDir("./");
}
};
} else {
console.log("SSE Not Supported In Browser...");
}
function getDir(query) {
var formULPTH = document.getElementById("DIRPATHUL");
var formUlPth = document.getElementById("DIRPATHUL");
var path = "";
var temp = "";
var cookies = "";
var dirCookie = "";
// push or pop to path list
if (query === "/") {
// Process path from cookie and set to array/list
if (document.cookie) {
temp = document.cookie.replace("dirQuery=", "");
temp = temp.split("/");
// Subtract one b/c paths end with / and create empty slot
var size = temp.length - 1;
dirCookie = getCookie("dirQuery");
if (dirCookie != "" && dirCookie != "./") {
dirCookie = dirCookie.split("/");
dirCookie.pop(); // account for ending empty slot
var size = dirCookie.length;
for (var i = 0; i < size; i++) {
pathNodes.push(temp[i] + "/");
pathNodes.push(dirCookie[i] + "/");
}
// If no cookie, setup path from root
} else {
pathNodes = [];
pathNodes.push("." + query);
}
} else if (query === "../") {
@ -34,18 +47,11 @@ function getDir(query) {
// Create path from array of items
for (pathNode of pathNodes) {
path += pathNode; console.log(pathNode);
path += pathNode;
}
// For some reason, PHPSESSID= gets inserted when in sub dir.
// temp work arround is to trim it.
if (path.includes("PHPSESSID=")) {
path = path.split("; ").pop();
}
formULPTH.value = path; // Setup upload path for form
formUlPth.value = path; // Setup upload path for form
path = "dirQuery=" + path;
console.log("Path : " + path);
process(path);
}
@ -66,7 +72,7 @@ function process(path) {
}
}
};
xhttp.open("POST", "resources/php/process.php", true); // Open the connection
xhttp.open("POST", "resources/php/getDirList.php", true); // Open the connection
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.overrideMimeType('application/xml'); // Force return to be XML
xhttp.send(path); // Start the process

View File

@ -0,0 +1,15 @@
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}

View File

@ -1,43 +0,0 @@
function dragContainer(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
elmnt.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
pauseEvent(e);
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
pauseEvent(e);
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement(e) {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
function pauseEvent(e) {
if(e.stopPropagation) e.stopPropagation();
if(e.preventDefault) e.preventDefault();
e.cancelBubble=true;
e.returnValue=false;
return false;
}
}

View File

@ -0,0 +1,59 @@
function renameItem(obj) {
var path = document.getElementById("path").innerHTML;
var oldName = formerFileName;
var newName = obj.value;
var formData = "renameItem=true&oldName=" + oldName + "&newName=" + newName + "&path=" + path;
var xhttp = new XMLHttpRequest();
console.log("Old name: " + oldName);
console.log("New name: " + newName);
xhttp.open("POST", "resources/php/filesystemActions.php", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(formData);
}
function createItem(type) {
var path = document.getElementById("path").innerHTML;
var newItem = document.getElementById("NewItem");
var fullPth = path + newItem.value;
var xhttp = new XMLHttpRequest();
newItem.value = "";
xhttp.open("POST", "resources/php/filesystemActions.php", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("createItem=true&item=" + fullPth + "&type=" + type + "");
}
function startDeleteItem(item) {
// Get the item name
itemObj = item;
}
function deleteItem(item) {
var path = document.getElementById("path").innerHTML;
// Clicked yes to delete and there is an item
if (itemObj != undefined && itemObj != null) {
var fullPth = path + itemObj;
var answer = confirm("Are you sure you want to delete: " + fullPth);
if (answer == true) {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "resources/php/filesystemActions.php", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("deleteItem=true&item=" + fullPth);
console.log("Deleted: " + fullPth);
itemObj = null;
}
}
}
function openInLocalProg(media) {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "resources/php/filesystemActions.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("media=" + media);
}

View File

@ -1,5 +1,19 @@
var formerFileName = "";
function enableEdit(obj) {
obj.style.backgroundColor = "#ffffffff";
obj.style.color = '#000000ff';
obj.readOnly = '';
formerFileName = obj.value;
}
function disableEdits(obj) {
obj.style.backgroundColor = "#ffffff00";
obj.style.color = '#ffffff';
obj.value = formerFileName;
obj.readOnly = "true";
}
function showImg(imgLoc) {
var path = document.getElementById("path").innerHTML;
var imgView = document.getElementById("imgView");
@ -37,88 +51,40 @@ function showMedia(media) {
}
}
function openInLocalProg(media) {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "resources/php/open.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("media=" + media);
function closeImg() {
var imgView = document.getElementById("imgView");
imgView.style.display = "none";
}
function enableEdit(obj) {
obj.style.backgroundColor = "#ffffffff";
obj.style.color = '#000000ff';
obj.readOnly = '';
formerFileName = obj.value;
function closeMedia() {
var mediaView = document.getElementById("fileView");
mediaView.style.display = "none";
mediaView.children[3].src = "";
}
function disableEdits(obj) {
obj.style.backgroundColor = "#ffffff00";
obj.style.color = '#ffffff';
obj.value = formerFileName;
obj.readOnly = "true";
function clearDirCookie() {
var expireDate = "Thu, 01 Jan 1970 00:00:00 UTC";
document.cookie = "dirQuery=; expires=" + expireDate;
getDir("/");
}
function renameItem(obj) {
var path = document.getElementById("path").innerHTML;
var oldName = formerFileName;
var newName = obj.value;
var formData = "oldName=" + oldName + "&newName=" + newName + "&path=" + path;
var xhttp = new XMLHttpRequest();
console.log("Old name: " + oldName);
console.log("New name: " + newName);
xhttp.open("POST", "resources/php/rename.php", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(formData);
}
function createDir() {
var path = document.getElementById("path").innerHTML;
var newItem = document.getElementById("NewItem");
var fullPth = path + newItem.value;
var xhttp = new XMLHttpRequest();
newItem.value = "";
xhttp.open("POST", "resources/php/newFileOrDir.php", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("item=" + fullPth + "&isDir=dir");
}
function createFile() {
var path = document.getElementById("path").innerHTML;
var newItem = document.getElementById("NewItem");
var fullPth = path + newItem.value;
var xhttp = new XMLHttpRequest();
newItem.value = "";
xhttp.open("POST", "resources/php/newFileOrDir.php", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("item=" + fullPth + "&isFile=file");
}
function startDeleteItem(item) {
// Get the item name
itemObj = item;
}
function deleteItem(item) {
var path = document.getElementById("path").innerHTML;
// Clicked yes to delete and there is an item
if (itemObj != undefined && itemObj != null) {
var fullPth = path + itemObj;
var answer = confirm("Are you sure you want to delete: " + fullPth);
if (answer == true) {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "resources/php/delete.php", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("item=" + fullPth);
console.log("Deleted: " + fullPth);
itemObj = null;
}
function tgglServerMsgView() {
var serverMsgView = document.getElementById("toggleServerMsg");
if (serverMsgView.style.display == "none") {
serverMsgView.style.display = "block";
} else {
serverMsgView.style.display = "none";
}
}
function clearDlList() {
document.getElementById("CLEARBTTN").click();
}
function onloadSetBG() {
updateBG("resources/images/backgrounds/000.jpg");
}
function updateBG(bgImg) {
document.getElementById("bg").src = bgImg;
}

View File

@ -1,46 +0,0 @@
function tgglServerMsgView() {
var serverMsgView = document.getElementById("toggleServerMsg");
if (serverMsgView.style.display == "none") {
serverMsgView.style.display = "block";
} else {
serverMsgView.style.display = "none";
}
}
function closeImg() {
var imgView = document.getElementById("imgView");
imgView.style.display = "none";
}
function closeMedia() {
var mediaView = document.getElementById("fileView");
mediaView.style.display = "none";
mediaView.children[3].src = "";
}
function clearDirCookie() {
var expireDate = "Thu, 01 Jan 1970 00:00:00 UTC";
document.cookie = "dirQuery=; expires=" + expireDate;
location.reload();
}
function clearDlList() {
document.getElementById("CLEARBTTN").click();
}
function onloadSetBG() { updateBG("resources/images/backgrounds/000.jpg"); }
function updateBG(bgImg) { document.getElementById("bg").src = bgImg; }
// SSE events if supported
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("resources/php/sse.php");
source.onmessage = function(event) {
if (event.data === "updateListing") {
getDir("./");
}
};
} else {
console.log("SSE Not Supported In Browser...");
}

View File

@ -35,6 +35,7 @@ document.onclick = function (event) {
}
}
// Mainly for rename event
document.onkeydown = function (event) {
var obj = event.target;
var callingID = event.target.id;
@ -47,3 +48,48 @@ document.onkeydown = function (event) {
}
}
}
// Drage event for container
function dragContainer(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
elmnt.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
pauseEvent(e);
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
pauseEvent(e);
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement(e) {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
function pauseEvent(e) {
if(e.stopPropagation) e.stopPropagation();
if(e.preventDefault) e.preventDefault();
e.cancelBubble=true;
e.returnValue=false;
return false;
}
}

View File

@ -1,23 +0,0 @@
<?php
session_start();
function deleteItem($FILE) {
if (is_dir($FILE)){
//GLOB_MARK adds a slash to directories returned
$files = glob($FILE . '*', GLOB_MARK);
foreach ($files as $file) {
deleteItem($file);
}
rmdir($FILE);
} else if (is_file($FILE)) {
unlink($FILE);
}
$_SESSION["refreshState"] = "updateListing";
}
chdir("../../");
if (isset($_POST["item"])) {
deleteItem($_POST["item"]);
}
?>

View File

@ -1,7 +1,39 @@
<?php
session_start();
// Create file or folder
function createItem($FILE, $TYPE) {
if ($TYPE == "dir"){
mkdir($FILE, 0755);
} else if ($TYPE == "file") {
$myfile = fopen($FILE, "w");
fclose($myfile);
}
$_SESSION["refreshState"] = "updateListing";
}
// File or folder delition
function deleteItem($FILE) {
if (is_dir($FILE)){
//GLOB_MARK adds a slash to directories returned
$files = glob($FILE . '*', GLOB_MARK);
foreach ($files as $file) {
deleteItem($file);
}
rmdir($FILE);
} else if (is_file($FILE)) {
unlink($FILE);
}
$_SESSION["refreshState"] = "updateListing";
}
// Rename file or folder
function renameItem($OLDFILE, $NEWNAME, $PATH) {
rename($PATH . $OLDFILE, $PATH . $NEWNAME);
$_SESSION["refreshState"] = "updateListing";
}
// Uploader
function uploadFiles($targetDir) {
echo "<!DOCTYPE html>"
. "<head>"
@ -61,12 +93,42 @@ function uploadFiles($targetDir) {
echo "</body></html>";
}
// Check access type.
// Local program file access
function openFile($FILE) {
include 'config.php';
$EXTNSN = strtolower(pathinfo($FILE, PATHINFO_EXTENSION));
if (preg_match('(mkv|avi|flv|mov|m4v|mpg|wmv|mpeg|mp4|webm)', $EXTNSN) === 1) {
shell_exec($MEDIAPLAYER . $MPLAYER_WH . "\"" . $FILE . "\" > /dev/null &");
} else if (preg_match('(png|jpg|jpeg|gif)', $EXTNSN) === 1) {
shell_exec($IMGVIEWER . ' "' . $FILE . '" > /dev/null &');
} else if (preg_match('(psf|mp3|ogg|flac)', $EXTNSN) === 1) {
shell_exec($MUSICPLAYER . ' "' . $FILE . '" > /dev/null &');
} else if (preg_match('(odt|doc|docx|rtf)', $EXTNSN) === 1) {
shell_exec($OFFICEPROG . ' "' . $FILE . '" > /dev/null &');
} else if (preg_match('(txt)', $EXTNSN) === 1) {
shell_exec($TEXTVIEWER . ' "' . $FILE . '" > /dev/null &');
} else if (preg_match('(pdf)', $EXTNSN) === 1) {
shell_exec($PDFVIEWER . ' "' . $FILE . '" > /dev/null &');
}
}
chdir("../../");
if(isset($_POST["UploadFiles"]) && isset($_POST["DIRPATHUL"])) {
if (isset($_POST["createItem"]) && isset($_POST["item"]) && isset($_POST["type"])) {
createItem($_POST["item"], $_POST["type"]);
} else if (isset($_POST["deleteItem"]) && isset($_POST["item"])) {
deleteItem($_POST["item"]);
} else if (isset($_POST["renameItem"]) && isset($_POST["oldName"]) && isset($_POST["newName"]) && isset($_POST["path"])) {
renameItem($_POST["oldName"], $_POST["newName"], $_POST["path"]);
} else if(isset($_POST["UploadFiles"]) && isset($_POST["DIRPATHUL"])) {
uploadFiles($_POST["DIRPATHUL"]);
} else if (isset($_POST["media"])) {
openFile($_POST["media"]);
} else {
echo "<span style='color:rgb(255, 0, 0);'>Server: [Error] --> Incorrect access attempt!</span>";
}
?>

View File

@ -1,23 +0,0 @@
<?php
session_start();
function createItem($FILE, $TYPE) {
if ($TYPE == "dir"){
mkdir($FILE, 0755);
} else if ($TYPE == "file") {
$myfile = fopen($FILE, "w");
fclose($myfile);
}
}
chdir("../../");
if (isset($_POST["item"])) {
if (isset($_POST["isDir"])) {
createItem($_POST["item"], $_POST["isDir"]);
} else if (isset($_POST["isFile"])) {
createItem($_POST["item"], $_POST["isFile"]);
}
$_SESSION["refreshState"] = "updateListing";
}
?>

View File

@ -1,28 +0,0 @@
<?php
function openFile($FILE) {
include 'config.php';
$EXTNSN = strtolower(pathinfo($FILE, PATHINFO_EXTENSION));
if (preg_match('(mkv|avi|flv|mov|m4v|mpg|wmv|mpeg|mp4|webm)', $EXTNSN) === 1) {
shell_exec($MEDIAPLAYER . $MPLAYER_WH . "\"" . $FILE . "\" > /dev/null &");
} else if (preg_match('(png|jpg|jpeg|gif)', $EXTNSN) === 1) {
shell_exec($IMGVIEWER . ' "' . $FILE . '" > /dev/null &');
} else if (preg_match('(psf|mp3|ogg|flac)', $EXTNSN) === 1) {
shell_exec($MUSICPLAYER . ' "' . $FILE . '" > /dev/null &');
} else if (preg_match('(odt|doc|docx|rtf)', $EXTNSN) === 1) {
shell_exec($OFFICEPROG . ' "' . $FILE . '" > /dev/null &');
} else if (preg_match('(txt)', $EXTNSN) === 1) {
shell_exec($TEXTVIEWER . ' "' . $FILE . '" > /dev/null &');
} else if (preg_match('(pdf)', $EXTNSN) === 1) {
shell_exec($PDFVIEWER . ' "' . $FILE . '" > /dev/null &');
}
}
chdir("../../");
if (isset($_POST["media"])) {
openFile($_POST["media"]);
}
?>

View File

@ -1,15 +0,0 @@
<?php
session_start();
function renameItem($OLDFILE, $NEWNAME, $PATH) {
rename($PATH . $OLDFILE, $PATH . $NEWNAME);
$_SESSION["refreshState"] = "updateListing";
}
chdir("../../");
if (isset($_POST["oldName"]) && isset($_POST["newName"]) && isset($_POST["path"])) {
renameItem($_POST["oldName"], $_POST["newName"], $_POST["path"]);
}
?>