Skip to content

Commit c766eb6

Browse files
committed
Update index.php
-Upload file/s. -Add new file & folder. -Fix bugs.
1 parent c807159 commit c766eb6

File tree

1 file changed

+80
-101
lines changed

1 file changed

+80
-101
lines changed

phpboard/index.php

+80-101
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,47 @@
99
$act = $pData->act ?? '';
1010
if ($act == 'del') {
1111
$path = $pData->path ?? '';
12-
if (!empty($path) and file_exists(DIR_ROOT . $path)) {
13-
ajaxDone(removeDirFile(DIR_ROOT . $path));
12+
if (!empty($path) and file_exists(PATH_ROOT . $path)) {
13+
ajaxDone(removeDirFile(PATH_ROOT . $path));
1414
} else {
1515
ajaxDone(false);
1616
}
1717
} else if ($act == 'zip') {
1818
$path = $pData->path ?? '';
19-
if (!empty($path) and file_exists(DIR_ROOT . $path)) {
20-
ajaxDone(zipDir(DIR_ROOT . $path));
19+
if (!empty($path) and file_exists(PATH_ROOT . $path)) {
20+
ajaxDone(zipDir(PATH_ROOT . $path));
2121
} else {
2222
ajaxDone(false);
2323
}
24+
} else if ($act == 'addFile') {
25+
$directory = $pData->dir ?? '';
26+
$name = $pData->name ?? '';
27+
if (empty($name)) {
28+
ajaxDone(false, "File name is empty!");
29+
} if (file_exists(PATH_ROOT . $directory .$name)) {
30+
ajaxDone(false, "Duplicated file name!");
31+
} else {
32+
ajaxDone(fopen(PATH_ROOT . $directory .$name, "w")!==false);
33+
}
34+
} else if ($act == 'addFolder') {
35+
$directory = $pData->dir ?? '';
36+
$name = $pData->name ?? '';
37+
if (empty($name)) {
38+
ajaxDone(false, "Folder name is empty!");
39+
} if (file_exists(PATH_ROOT . $directory .$name)) {
40+
ajaxDone(false, "Duplicated folder name!");
41+
} else {
42+
ajaxDone(mkdir(PATH_ROOT . $directory .$name, 0777, true), "w");
43+
}
2444
}
2545

2646
ajaxInvalid();
47+
} else if (isset($_POST['upload'])) {
48+
$path = $_POST['path'];
49+
foreach ($_FILES as $file) {
50+
@move_uploaded_file($file["tmp_name"], PATH_ROOT . $path . $file["name"]);
51+
}
52+
ajaxDone(true);
2753
}
2854

2955

@@ -36,12 +62,12 @@
3662
if (@$_GET['act'] == 'download') {
3763
if ($_GET['targetf'] ?? false) {
3864
$targetFile = $_GET['targetf'];
39-
if (file_exists(DIR_ROOT . $directory . $targetFile)) {
65+
if (file_exists(PATH_ROOT . $directory . $targetFile)) {
4066
header("Content-Type:application/octet-stream");
4167
header("Accept-Ranges: bytes");
42-
header("Content-Length: " . filesize(DIR_ROOT . $directory . $targetFile));
68+
header("Content-Length: " . filesize(PATH_ROOT . $directory . $targetFile));
4369
header("Content-Disposition: attachment; filename=" . $targetFile);
44-
readfile(DIR_ROOT . $directory . $targetFile);
70+
readfile(PATH_ROOT . $directory . $targetFile);
4571
exit;
4672
}
4773
} else if ($_GET['targetd'] ?? false) {
@@ -50,7 +76,7 @@
5076
$download = $targetDir . '.zip';
5177
$zip->open($download, ZipArchive::CREATE);
5278

53-
addDirToZip($zip, $targetDir, DIR_ROOT . $directory . $targetDir);
79+
addDirToZip($zip, $targetDir, PATH_ROOT . $directory . $targetDir);
5480

5581
$zip->close();
5682
header('Content-Type: application/zip');
@@ -62,15 +88,16 @@
6288
exit("<script>window.close()</script>");
6389
}
6490

65-
91+
$isInProjectPath = strpos(PATH_PROJECT, PATH_ROOT . $directory) !== false && strlen($directory) > 0;
92+
$isInRootDir = PATH_ROOT . $directory == PATH_ROOT ? true : false;
6693

6794
$dirs_list = "";
6895
$files_list = "";
69-
if ($files = array_diff(scandir(DIR_ROOT . $directory), ['.', '..'])) {
96+
if ($files = array_diff(scandir(PATH_ROOT . $directory), ['.', '..'])) {
7097
$index = 0;
7198
foreach ($files as $entry) {
7299
if ($entry != "." && $entry != "..") {
73-
$path = DIR_ROOT . $directory . $entry;
100+
$path = PATH_ROOT . $directory . $entry;
74101
// print decoct(fileperms($file) & 0777);
75102
$stat = stat($path);
76103
if (is_dir($path)) {
@@ -85,7 +112,7 @@
85112
<td>' . getPermDescription($path) . '</td>
86113
<td>'
87114
. (true ? '<button type="button" class="btn btn-default btn-sm btn-dirtozip" ng-click="zipDir($event, ' . $index . ')">Create Zip File</button> ' : ' ')
88-
. (is_writable($path) ? '<input type="button" class="btn btn-danger btn-sm" ng-click="deleteFileDir($event, ' . $index . ')" value="Delete" title="Delete without confirmation!"> ' : ' ') .
115+
. makeDeleteButtonForDir($path, $entry, $index) .
89116
'</td>
90117
</tr>';
91118
} else {
@@ -100,7 +127,7 @@
100127
<td>' . getPermDescription($path) . '</td>
101128
<td>'
102129
. (true ? '<input type="button" class="btn btn-default btn-sm" ng-click="downloadFile($event, ' . $index . ')" value="Download"> ' : ' ')
103-
. (is_writable($path) ? '<input type="button" class="btn btn-danger btn-sm" ng-click="deleteFileDir($event, ' . $index . ')" value="Delete"> ' : ' ') .
130+
. makeDeleteButtonForFile($path, $entry, $index) .
104131
'</td>
105132
</tr>';
106133
}
@@ -121,20 +148,54 @@ function getPermDescription($path)
121148
return implode('+', $desc);
122149
}
123150

151+
function makeDeleteButtonForDir($path, $entry, $index)
152+
{
153+
global $isInProjectPath, $isInRootDir;
154+
if (!is_writable($path)) {
155+
return '<input type="button" class="btn btn-danger btn-sm" value="Delete" title="Permission Denied!" disabled="disabled"> ';
156+
} else if ($isInProjectPath || ($isInRootDir && $entry == DIR_PROJECT)) {
157+
return '<input type="button" class="btn btn-danger btn-sm" value="Delete" title="Not Allowed!" disabled="disabled"> ';
158+
}
159+
return '<input type="button" class="btn btn-danger btn-sm" ng-click="deleteFileDir($event, ' . $index . ')" value="Delete" title="Delete without confirmation!"> ';
160+
}
161+
162+
function makeDeleteButtonForFile($path, $entry, $index)
163+
{
164+
global $project_root_files, $isInProjectPath, $isInRootDir;
165+
if (!is_writable($path)) {
166+
return '<input type="button" class="btn btn-danger btn-sm" value="Delete" title="Permission Denied!" disabled="disabled"> ';
167+
} else if ($isInProjectPath || ($isInRootDir && in_array($entry, $project_root_files))) {
168+
return '<input type="button" class="btn btn-danger btn-sm" value="Delete" title="Not Allowed!" disabled="disabled"> ';
169+
}
170+
return '<input type="button" class="btn btn-danger btn-sm" ng-click="deleteFileDir($event, ' . $index . ')" value="Delete" title="Delete without confirmation!"> ';
171+
}
172+
124173

125174

126175
getHeader();
127176
?>
128-
129177
<!-- Page Content -->
130178
<div class="container">
131-
<div class="row">
179+
<div class="row" ng-app="theApp">
132180
<div class="col-lg-12">
133181
<h1 class="page-header">Your Host <br><span class="details"><?php print getWebServerDetails(); ?>
134182
</h1>
135183
<?php
136184
showBreadCrumb($directory);
137185
?>
186+
<div class="file-upload">
187+
<ul ng-controller="addNewCtl">
188+
<li ng-click="showUploadBox($event)"><span class="glyphicon glyphicon-upload"></span> Upload</li>
189+
<li ng-click="addNewFile($event)"><span class="glyphicon glyphicon-plus"></span> <span class="m-hidden-xs">New </span>File</li>
190+
<li ng-click="addNewFolder($event)"><span class="glyphicon glyphicon-plus"></span> <span class="m-hidden-xs">New </span>Folder</li>
191+
<li>
192+
<input style="display:none" type="text" ng-model="newfileName" id="newfileName" name="newfileName" placeholder="File/Folder Name">
193+
<input style="display:none" type="button" ng-click="addNewFileFolderHide($event)" class="btn btn-default btn-xs m-hidden-xs" value=" Cancel ">
194+
<input style="display:none" type="button" ng-click="addNewFileFolder($event)" class="btn btn-primary btn-xs" value=" Add ">
195+
</li>
196+
</ul>
197+
<upload id="filedrop" to="index.php"></upload>
198+
</div>
138199
<div class="table-responsive" style="border: 1px solid #efefef;">
139200
<table class="table filestable">
140201
<thead>
@@ -143,10 +204,10 @@ function getPermDescription($path)
143204
<th>Size</th>
144205
<th>Modified</th>
145206
<th>Permissions</th>
146-
<th>Actions</th>
207+
<th style="min-width: 180px;">Actions</th>
147208
</tr>
148209
</thead>
149-
<tbody ng-app="filesApp" ng-controller="filesCtl">
210+
<tbody ng-controller="filesCtl">
150211
<?php
151212
if (empty($dirs_list) and empty($files_list)) {
152213
print '<tr>
@@ -267,91 +328,9 @@ function getMenuPosition(mouse, direction, scrollDir) {
267328
</script>
268329
<script>
269330
var rootUrl = "<?php print URL_ROOT; ?>";
270-
angular.module('filesApp', [])
271-
.controller('filesCtl', function($scope, $http, $element, $timeout, $window) {
272-
$scope.downloadFile = function(event, id) {
273-
var trItem = $element.find('#tr-' + id);
274-
var path = trItem.attr("data-href");
275-
var file = trItem.attr("data-file");
276-
var url = window.location.href;
277-
url += (url.search('/?') > 0 ? '&' : '?');
278-
279-
$window.open(url + "act=download&targetf=" + file, '_blank');
280-
$window.focus();
281-
}
282-
$scope.zipDir = function(event, id) {
283-
var zipBtn = angular.element(event.target);
284-
zipBtn.html('<span class="spinner-grow spinner-grow-sm" style="margin:0 5px 0 -2px;" role="status" aria-hidden="true"></span> Zipping ...');
285-
angular.element(".btn-dirtozip").attr("disabled", true);
286-
287-
288-
var trItem = $element.find('#tr-' + id);
289-
var path = trItem.attr("data-href");
290-
291-
$http({
292-
method: 'POST',
293-
url: "index.php",
294-
headers: {
295-
'Content-Type': 'application/x-www-form-urlencoded'
296-
},
297-
data: {
298-
act: 'zip',
299-
path: path
300-
}
301-
}).then(function successCallback(response) {
302-
console.log(response.data);
303-
if (response.data.status === true) {
304-
zipBtn.html('Create Zip File');
305-
angular.element(".btn-dirtozip").attr("disabled", false);
306-
307-
$timeout(function() {
308-
$window.location.reload();
309-
}, 250);
310-
} else {
311-
trItem.addClass("dangerHighlight");
312-
$timeout(function() {
313-
trItem.addClass("highlightOut");
314-
}, 1000);
315-
}
316-
317-
}, function errorCallback(response) {
318-
319-
});
320-
}
321-
$scope.deleteFileDir = function(event, id) {
322-
var trItem = $element.find('#tr-' + id);
323-
var path = trItem.attr("data-href");
324-
trItem.removeClass();
325-
$http({
326-
method: 'DELETE',
327-
url: "index.php",
328-
headers: {
329-
'Content-Type': 'application/x-www-form-urlencoded'
330-
},
331-
data: {
332-
act: 'del',
333-
path: path
334-
}
335-
}).then(function successCallback(response) {
336-
if (response.data.status === true) {
337-
trItem.hide(250);
338-
$timeout(function() {
339-
trItem.remove();
340-
}, 250);
341-
} else {
342-
trItem.addClass("dangerHighlight");
343-
$timeout(function() {
344-
trItem.addClass("highlightOut");
345-
}, 1000);
346-
}
347-
348-
}, function errorCallback(response) {
349-
350-
});
351-
}
352-
353-
});
331+
var directory = "<?php print $directory; ?>";
354332
</script>
333+
<script src="theme/assets/js/app.js"></script>
355334
<?php
356335
getFooter();
357336
?>

0 commit comments

Comments
 (0)