-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathedit.php
190 lines (189 loc) · 7.6 KB
/
edit.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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Text Editor</title>
<meta name="description" content="Editor">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="styles.css">
</head>
<?php
// first we need to see if we actually GET anything
$gettest = $_GET['p'];
if (isset($gettest)) {
// THEN do this shit
// need to lowercase the filename
$gettest = strtolower($gettest);
// remove all whitespace
$gettest = preg_replace('/\s+/', '', $gettest);
// set file to read
$filename ='txt/'.$gettest.'.txt';
// check if file exists
if (file_exists($filename)) {
// now we check if we are renaming the file
// check if variable has been set
$renamed = @$_GET['renamed'];
if (isset($renamed)) {
// and lowercase filename
$renamed = strtolower($renamed);
// strip whitespace
$renamed = preg_replace('/\s+/', '', $renamed);
// check to see if other files with the same name exist
foreach (glob('txt/*.txt') as $txtfile) {
$txtfile = basename($txtfile, ".txt");
// echo $txtfile;
// echo $renamed;
// if we do find the same name then we have to figure out wtf we're doing with it
if ($renamed == $txtfile) {
// then what? run the rename function again to get a new variable
echo '<script>var renamed = prompt("There is already a file named that! Try again");
var filename = "'.$renamed.'";
if (renamed != "" && renamed != filename) {
var renamed = renamed.toLowerCase();
var renamed = renamed.replace(/\s/g, "");
window.location = "edit.php?p='.$gettext.'&renamed=" + renamed;
} else {
alert("Could not rename file");
window.location = "edit.php";
}
</script>';
$renamed = "";
}
}
if ($renamed != "") {
// make it reference an actual file
$renamedFile = 'txt/'.$renamed.'.txt';
// and rename it
rename($filename, $renamedFile);
// we still have to set this filename
$filename = $renamedFile;
// actually I give up and just want to reload with the new filename
// duhh I think this should work
echo '<script>window.location = "edit.php?p='.$renamed.'"</script>';
}
}
// check if file is writable
//if (is_writable($filename)) {
// echo "The file is writable<br>";
//}
//else {
// echo "The file is not writable<br>";
//}
// now open the file
$fh = @fopen($filename, "r") or die("Unable to open file!<br>");
// read file contents
$data = @fread($fh, filesize($filename));
// then close it
fclose($fh);
$data = base64_decode($data, true);
} else {
// if not, create it
fopen($filename, "a+") or die("Unable to open file!<br>");
// read file contents
$data = @fread($fh, filesize($filename));
$data = base64_decode($data, true);
// close file
@fclose($fh);
}
// and if we got no p variable, take us to the first text file
} else {
foreach (glob('txt/*.txt') as $txtfile) {
// get filename only
$txtfile = basename($txtfile, ".txt");
$gettest = $txtfile;
echo '<script>window.location = "edit.php?p='.$txtfile.'"</script>';
break;
}
}
echo '<body>
<script>document.title = "Edit '.$gettest.'";</script>
<div class="main-container">
<div class="editor-view">
<form action="" method= "post" enctype="multipart/mixed" onsubmit="prettySubmit()">
<textarea rows="25" name="newd" id="newd">'.$data.'</textarea><br />
<input type="submit" value="Save" style="float: right;" id="save">
</form>
</div>
<div style="display: flex; flex-direction: column;">
<div class="menu-view">
<table align="center">';
//<ul id="notes-list">';
// get list of txt files (I think this works)
// loop text files
foreach (glob('txt/*.txt') as $txtfile) {
$txtfile = basename($txtfile, ".txt");
// take out .txt
// put some fucking tables in here
// also we can add direct links to each text file to download
// don't add a link for the currently open file
if ($gettest == $txtfile) {
echo '<tr><td>'.$txtfile.'</td><td align="center"><a href="delete.php?p='.$txtfile.'" onclick="return confirm(\'Are you sure?\')" class="delete"> ✖ </a></td><td><a style="text-decoration: none;" target="_blank" href="view.php?p='.$txtfile.'">🔍</a></td></tr>';
} else {
echo '<tr><td><a href="edit.php?p='.$txtfile.'">'.$txtfile.'</a></td><td align="center"><a href="delete.php?p='.$txtfile.'" onclick="return confirm(\'Are you sure?\')" class="delete"> ✖ </a></td><td><a style="text-decoration: none;" target="_blank" href="view.php?p='.$txtfile.'">🔍</a></td></tr>';
}
}
//</ul>
echo '<script>
function base64EncodeUnicode(str) {
// Firstly, escape the string using encodeURIComponent to get the UTF-8 encoding of the characters,
// Secondly, we convert the percent encodings into raw bytes, and add it to btoa() function.
utf8Bytes = encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode(\'0x\' + p1);
});
return btoa(utf8Bytes);
}
function prettySubmit() {
document.getElementById("newd").style.display = "none";
document.getElementById("save").style.display = "none";
if (newd.value == "") {
newd.value = "EMPTY";
} else {
newd.value = base64EncodeUnicode(newd.value);
}
}
function newFile() {
var txtfile = prompt("Name of new text file");
if (txtfile != "") {
var txtfile = txtfile.toLowerCase();
var txtfile = txtfile.replace(/\s/g, "");
window.location = "edit.php?p=" + txtfile;
}
}
function renameFile() {
var renamed = prompt("New name to rename file");
if (renamed != "") {
var renamed = renamed.toLowerCase();
var renamed = renamed.replace(/\s/g, "");
window.location = "edit.php?p='.$gettest.'&renamed=" + renamed;
}
}</script>
</table>
</div>
<button onclick="newFile()">New File</button>
<button onclick="renameFile()" align="right">Rename '.$gettest.'</button>
</div>
</div>
</body>';
$newdata = $_POST['newd'];
//$content = $_POST['contents'];
//$content = strtr($content, ' ', '+');
//$content = base64_decode($content);
//
if (isset($newdata)) {
$newdata = strtr($newdata, '-_', '+/');
// open file
$fw = fopen($filename, 'w') or die('Could not open file!');
// write to file
if ($newdata == 'EMPTY') {
@ftruncate($fb, 0);
} else {
$fb = fwrite($fw, $newdata) or die('Could not write to file');
}
// close file
fclose($fw);
echo '<script>
location = location
</script> ';
}
?>
</html>