-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.php
212 lines (184 loc) · 7.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<html>
<head>
<style>
body {
margin-top: 1px;
margin-left: 20px;
margin-right: 20px;
margin-bottom: 20px;
}
h1, ul, ol, li, p {
font-family: vedana, arial, helvetica, sans-serif;
}
textarea {
font-family: courier new, monospace;
}
.small {
font-size: 8pt;
}
</style>
<script src="http://code.jquery.com/jquery-1.7.2.min.js" language="javascript" type="text/javascript"></script>
<script src="../codemirror-3.18/lib/codemirror-compressed.js"></script>
<link rel="stylesheet" href="../codemirror-3.18/lib/codemirror.css">
</head>
<body>
<?php
// kill magic quotes!
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
foreach ($val as $k => $v) {
unset($process[$key][$k]);
if (is_array($v)) {
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
} else {
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
// HIGH LEVEL PLAN:
// 1. provide list of all the files in the folder (only this folder)
// 2. files can be clicked and if so, then page reloads with editing window
// 3. editing window has three buttons at the top: save, close (save first)
// 4. architecture must prevent writing of files to other directories
// ----------
if(!isset($currentDir)) {
// get the current folder
$currentDir = str_replace("edit.php", "", __FILE__);
}
// -0.5 check if a file needs to be deleted, and if it exists, delete it
if(isset($_GET['delete'])) {
if(file_exists($currentDir.str_replace("/","",str_replace("\\","",$_GET['delete'])))) {
unlink($currentDir.$_GET['delete']);
}
}
// get all the files in the current folder
$files = scandir($currentDir);
// 0. determine if a file has been requested for editing
$fileToRead = "";
if(isset($_GET['file'])) {
// ignore slashes since we're limiting ourselves to this directory
if(file_exists($currentDir.str_replace(array("/","\\"),"",$_GET['file']))) {
$fileToRead = str_replace(array("/","\\"),"",$_GET['file']);
}
}
// 0.25 determine if a file was saved and if so, then save it
if(isset($_POST['contents']) AND $fileToRead AND strtolower($fileToRead) != 'edit.php') {
$fileToSave = fopen($currentDir.$fileToRead, "w");
$i = 0;
while($i <= strlen($_POST['flippedContents'])) { // flip the string back to normal since we reverse it before submitting
$character = substr($_POST['flippedContents'],$i, 1);
if($character != "\r") { // the reverse process in javascript seems to add \r chars to the string??!!
$string = $character . $string;
}
$i++;
}
fwrite($fileToSave, $string); // use $_POST['contents'] and don't neuter it below if you want to avoid the messy flipping stuff, and the removal of the double lines that seem to creep in.
fclose($fileToSave);
}
// 0.5 check if a new file needs to be created, as long as we're not editing something else, and as long as it doesn't exist already
if(isset($_POST['newname']) AND !$fileToRead) {
$fileToRead = str_replace(array("/","\\"),"",$_POST['newname']);
// if the file does not exist then create it
if(!in_array($fileToRead, $files) AND strtolower($fileToRead) != 'edit.php') {
if(!strstr($fileToRead,'.')) { $fileToRead .= '.html'; }
fopen($currentDir.$fileToRead, "w");
}
}
// 1./2. list the files...
if(!$fileToRead) {
// setup the "create a new file box"
?>
<div style="float: right;">
<h1>Type a filename to create a new file</h1>
<form name="newfile" action="edit.php" method="post">
<input type="text" name="newname">
<input type="submit" name="submit" value="Create!">
</form>
<p>Don't forget the extension at the end of the name, ie: .html or .php</p>
</div>
<?php
// draw in the list of existing files
$fileLinks = array();
$unlistedFiles = array('.jpg', '.gif', '.jpg', '.png');
natsort($files);
foreach($files as $file) {
if($file == "." OR $file == ".." OR $file == "edit.php" OR substr($file, 0, 1) == "." OR !strstr($file, ".")) { continue; }
foreach($unlistedFiles as $thisExtension) {
if(strtolower(substr($file, strlen($thisExtension)*-1)) == $thisExtension) {
continue 2; // continue the higher foreach loop
}
}
$safeFileName = strip_tags(htmlspecialchars($file));
$fileLinks[] = "<li><a href=\"edit.php?file=".urlencode($safeFileName)."\">$safeFileName</a> <span class=\"small\"><a href=\"edit.php?delete=".urlencode($safeFileName)."\" onclick=\"javascript:return confirm('Are you sure you want to delete $safeFileName');\">[delete]</a></span></li>\n";
}
print "<div style=\"float: left;\">\n";
print "<h1>Click a filename to edit that file</h1>\n";
if(count($fileLinks)==0) {
$fileLinks[] = "<p>No files have been created yet</p>";
}
print "<ul>\n";
print implode($fileLinks,"");
print "</ul></div>\n";
// 2./3. edit the files...
} else {
$safeFileName = strip_tags(htmlspecialchars($fileToRead));
print "<div style=\"float: left;\">\n";
print "<div style=\"float: right;\"><p><a href=\"$safeFileName\" target=\"_blank\">View this file in a new tab</a></p></div>\n";
print "<h1>Edit the contents of: $safeFileName</h1>\n";
print "<div style=\"float: right;\"><form name=\"closeform\" action=\"edit.php\" method=\"post\">\n";
print "<input type=\"submit\" name=\"close\" value=\"Go back to the file list (don't save)\">\n"; // close button
print "</form></div>\n";
print "<form name=\"editform\" id=\"editform\" action=\"edit.php?file=".urlencode($safeFileName)."\" method=\"post\">\n";
print "<input type=\"hidden\" name=\"flippedContents\" id=\"flippedContents\" value=\"\" />\n";
print "<input type=\"submit\" name=\"save\" value=\"Save your changes\">\n<br>\n"; // save button
$contents = file_get_contents($currentDir.$fileToRead);
print "<textarea id=\"contents\" name=\"contents\">".htmlspecialchars($contents)."</textarea>\n";
print "</form>\n";
print "</div>\n";
?>
<script type="text/javascript">
$(window).load(function() {
setContentsHeightWidth();
});
$(window).resize(function() {
setContentsHeightWidth();
});
$('#editform').submit(function() {
cm.save(); // Call this to update the textarea value for posting
$('#flippedContents').val(reverseString($('#contents').val())); // reverse the string so that we won't trip up any validation/security parsers
$('#contents').val('');
});
function setContentsHeightWidth() {
$("#contents").css('height', ($(window).height()-140));
$("#contents").css('width', ($(window).width()-35));
cm.setSize($(window).width()-35, $(window).height()-140);
}
function reverseString(str) {
var result = '';
for(i=0;i<=str.length;i++) {
result = str.substring(i, i+1) + result;
}
return result;
}
var cm;
$(document).ready(
cm = CodeMirror.fromTextArea( document.getElementById('contents') , {
indentUnit: 4,
indentWithTabs: true,
enterMode: "keep",
tabMode: "shift",
lineNumbers: true,
matchBrackets: true,
mode: 'application/x-httpd-php'
})
);
</script>
<?php
}
?>
</body>
</html>