-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathonecore.php
375 lines (337 loc) · 11.9 KB
/
onecore.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
* onecore - the single file cms
* github.com/sudoremo/onecore
*/
session_start();
ob_start();
?>
<conf>
<flg_password_protected>false</flg_password_protected>
<allowed_file_types>html,xhtml</allowed_file_types>
<username>admin</username>
<password>21232f297a57a5a743894a0e4a801fc3</password>
</conf>
<?php
$configurationXml = ob_get_contents();
ob_end_clean();
class Conf extends SimpleXMLElement
{
function asXmlWithoutXmlHeader()
{
$xml = $this->asXML();
$xml = explode("\n", $xml);
$xml[0] = "";
$xml = trim( implode($xml, "\n") );
return $xml;
}
}
// Read configuration
$configuration = simplexml_load_string($configurationXml, 'Conf');
if ($configuration == false)
{
echo "Error, could not read configuration. Exiting.";
exit;
}
ob_start();
?>
<style type="text/css">
body {
font-family: Verdana, Arial;
font-size: 10pt;
margin-left: 10px;
padding-left: 3px;
padding-bottom: 0px;
}
h2 {
border-bottom: 1px solid darkgray;
}
</style>
<h1>onecore</h1>
<p>This is sample content. To edit check out <a href="?admin">onecore.php?admin</a>.</p>
<p>Made with <a href="http://www.remofritzsche.com/projects/onecore/">onecore</a>.</p>
<?php
$html = ob_get_contents();
ob_end_clean();
// Read current page
$filename = explode("/", $_SERVER['PHP_SELF']);
$filename = $filename[ count($filename) - 1 ];
$pagebuffer = file($filename);
// Get php section A
$i = 0;
for ($i = 0; $i < count($pagebuffer); $i++)
{
if ($pagebuffer[$i] != "?>\n")
{
$phpSectionA .= $pagebuffer[$i];
}
else
{
$phpSectionA .= "?>\n";
break;
}
}
// Get php section B
$read = false;
for ($i = $i+1; $i < count($pagebuffer); $i++)
{
if ($pagebuffer[$i] == "<?php\n")
$read = true;
if ($pagebuffer[$i] != "?>\n")
{
if ($read)
$phpSectionB .= $pagebuffer[$i];
}
else
{
$phpSectionB .= "?>\n";
break;
}
}
// Get php section C
for ($i = (count($pagebuffer)-1); $i >= 0; $i--)
{
if ($pagebuffer[$i] != "<?php\n")
{
$phpSectionC = $pagebuffer[$i] . $phpSectionC;
}
else
{
$phpSectionC = "<?php\n" . $phpSectionC;
break;
}
}
// Check for permissions
if ((isset($_GET['admin']) || isset($_GET['edit']) || isset($_GET['conf']) || isset($_GET['import']) || isset($_GET['export'])) && $configuration->flg_password_protected == "true" &! $_SESSION['ok'] == true)
{
echo 'Error, please <a href="'.$filename.'?login">log in</a> first.';
exit;
}
if (isset($_GET['login']))
{
if (!isset($_GET['do']))
{
echo '<h1>onecore Login: '.$filename.'</h1>';
echo '<form action="' . $filename . '?login&do" method="POST" />';
echo '<table><tr>';
echo '<tr><td>Username</td><td>';
echo '<input type="text" name="username" id="username" />';
echo '</td></tr>';
echo '<tr><td>Password</td><td>';
echo '<input type="password"" name="password"" id="password" />';
echo '</td></tr>';
echo '</table>';
echo '<input type="submit" value="Login" />';
echo '<input type="reset" value="Abort" onClick="history.back()" />';
echo '</form>';
}
else
{
if ($_POST['username'] == $configuration->username && md5($_POST['password']) == $configuration->password)
{
$_SESSION['ok'] = true;
echo 'Thank you for login. Proceed <a href="'.$filename.'?admin">here</a>.';
}
else
{
echo 'Sorry, could not login. <a href="'.$filename.'?login">Try again</a>.';
}
}
}
if (isset($_GET['edit']))
{
echo '<script type="text/javascript">'."\n";
echo 'function insertTab(event,obj) {'."\n";
echo ' var tabKeyCode = 9;'."\n";
echo ' if (event.which) // mozilla'."\n";
echo ' var keycode = event.which;'."\n";
echo ' else // ie'."\n";
echo ' var keycode = event.keyCode;'."\n";
echo ' if (keycode == tabKeyCode) {'."\n";
echo ' if (event.type == "keydown") {'."\n";
echo ' if (obj.setSelectionRange) {'."\n";
echo ' // mozilla'."\n";
echo ' var s = obj.selectionStart;'."\n";
echo ' var e = obj.selectionEnd;'."\n";
echo ' obj.value = obj.value.substring(0, s) + '."\n";
echo ' "\t" + obj.value.substr(e);'."\n";
echo ' obj.setSelectionRange(s + 1, s + 1);'."\n";
echo ' obj.focus();'."\n";
echo ' } else if (obj.createTextRange) {'."\n";
echo ' // ie'."\n";
echo ' document.selection.createRange().text="\t"'."\n";
echo ' obj.onblur = function() { this.focus(); this.onblur = null; };'."\n";
echo ' } else {'."\n";
echo ' // unsupported browsers'."\n";
echo ' }'."\n";
echo ' }'."\n";
echo ' if (event.returnValue) // ie ?'."\n";
echo ' event.returnValue = false;'."\n";
echo ' if (event.preventDefault) // dom'."\n";
echo ' event.preventDefault();'."\n";
echo ' return false; // should work in all browsers'."\n";
echo ' }'."\n";
echo ' return true;'."\n";
echo '}'."\n";
echo '</script>'."\n";
echo '<h1>Edit page</h1>';
echo '<form action="' . $filename . '?save" method="POST" />';
echo '<textarea id="html" onkeydown="return insertTab(event,this);" onkeyup="return insertTab(event,this);" onkeypress="return insertTab(event,this);" name="html" style="width:100%;height:350px;" />'.$html.'</textarea>';
echo '<br />';
echo '<input type="submit" value="Save page" />';
echo '<input type="reset" value="Abort" onClick="history.back()" />';
echo '</form>';
echo '<script type="text/javascript" language="javascript">';
echo ' function getPreview()';
echo ' {';
echo ' if (document.getElementById(\'btnPreview\').value != \'Close preview\')';
echo ' {';
echo ' document.getElementById(\'preview\').innerHTML = \'<hr />\' + document.getElementById(\'html\').value;';
echo ' document.getElementById(\'btnPreview\').value = \'Close preview\';';
echo ' }';
echo ' else';
echo ' {';
echo ' document.getElementById(\'preview\').innerHTML = \' \';';
echo ' document.getElementById(\'btnPreview\').value = \'Preview\';';
echo ' }';
echo ' }';
echo '</script>';
echo '<input type="submit" value="Preview" onClick="getPreview()" id="btnPreview" />';
echo '<div id="preview"> </div>';
}
if (isset($_GET['conf']))
{
if (!isset($_GET['sav']))
{
echo '<h1>onecore Configuration: '.$filename.'</h1>';
echo '<form action="' . $filename . '?conf&sav" method="POST" />';
echo '<table><tr>';
echo '<tr><td>Password protection</td><td>';
$checked = ($configuration->flg_password_protected == 'true') ? 'checked' : '';
echo '<input type="checkbox" name="passwordprotection" id="passwordprotection" ' . $checked . '/>';
echo '</td></tr>';
echo '<tr><td>Username</td><td>';
echo '<input type="text" name="username" id="username" value="'.$configuration->username.'" />';
echo '</td></tr>';
echo '<tr><td>Password<br />(only to set a new)</td><td>';
echo '<input type="password" name="password" id="password" />';
echo '</td></tr>';
echo '<tr><td>Allowed import file extensions<br />Example: \'html,xhtml\'</td><td>';
echo '<input type="text" name="allowed_extensions" id="allowed_extensions" value="'.$configuration->allowed_file_types.'" />';
echo '</td></tr>';
echo '</table>';
echo '<input type="submit" value="Save configuration" />';
echo '<input type="reset" value="Abort" onClick="history.back()" />';
echo '</form>';
}
else
{
if (isset($_POST['passwordprotection']))
{
$configuration->flg_password_protected = 'true';
}
else
{
$configuration->flg_password_protected = 'false';
}
$configuration->username = $_POST['username'];
if (isset($_POST['password']) && $_POST['password'] != "")
{
$configuration->password = md5($_POST['password']);
}
$configuration->allowed_file_types = $_POST['allowed_extensions'];
if (!file_put_contents($filename, trim($phpSectionA . "\n" . $configuration->asXmlWithoutXmlHeader() . "\n" . $phpSectionB . "\n" . stripslashes($html) . "\n" . $phpSectionC)))
{
echo '<p><b>Error: Could not save.</b></p>';
}
else
{
echo '<p><b>Saved.</b></p><p>Continue <a href="'.$filename.'">here</a>.</p>';
$html = $_POST['html'];
}
}
}
if (isset($_GET['save']))
{
if (!file_put_contents($filename, rtrim($phpSectionA . "\n" . $configurationXml . "\n" . $phpSectionB . "\n" . stripslashes($_POST['html']) . "\n" . $phpSectionC)))
{
echo '<p><b>Error: Could not save.</b></p>';
}
else
{
echo '<p><b>Saved.</b></p><p>Continue <a href="'.$filename.'">here</a>.</p>';
$html = $_POST['html'];
}
}
if (isset($_GET['export']))
{
$exportFilename = explode(".", $filename);
$exportFilename = $exportFilename[0] . ".html";
header('Content-type: text/html');
header('Content-Disposition: attachment; filename="'.$exportFilename.'"');
}
if (isset($_GET['admin']))
{
echo '<h1>onecore Administration: '.$filename.'</h1>';
echo '<p><ul>';
echo '<li><a href="'.$filename.'?edit">Edit page</a>';
echo '<li><a href="'.$filename.'?export">Export page</a>';
echo '<li><a href="'.$filename.'?import">Import page</a>';
echo '<li><a href="'.$filename.'?conf">Settings</a>';
echo '<li><a href="'.$filename.'">Back to page</a>';
if ($configuration->flg_password_protected == 'true')
echo '<li><a href="'.$filename.'?logout">Logout</a>';
echo '</ul></p>';
}
if (isset($_GET['logout']))
{
$_SESSION['ok'] = false;
echo "Logged out successfully. Thank you for using onecore<hr />";
}
if (isset($_GET['import']))
{
if (!isset($_GET['do']))
{
echo '<h1>Import HTML File</h1>';
echo '<form enctype="multipart/form-data" action="' . $filename . '?import&do" method="POST">';
echo '<input type="hidden" name="MAX_FILE_SIZE" value="30000" />';
echo '<input name="userfile" type="file" value="Browse" />';
echo '<input type="submit" value="Send file" />';
echo '<input type="reset" value="Abort" onClick="history.back()" />';
echo '</form>';
}
else
{
$extension = explode(".", $_FILES['userfile']['name']);
$extension = $extension[1];
$ALLOWED_EXTENSIONS = explode(",", $configuration->allowed_file_types);
if (!in_array($extension, $ALLOWED_EXTENSIONS))
{
echo '<p><b>Error:</b> The file extension \''.$extension.'\' is not supported. Please use one of these instead:';
echo '<p><ul>';
foreach ($ALLOWED_EXTENSIONS as $ext)
{
echo "<li>.$ext</li>";
}
echo '</ul>';
echo '</p>';
echo '<p><a href="'.$filename.'?import">Try again</a>.</p>';
}
else
{
$html = implode( file($_FILES['userfile']['tmp_name']) , "" );
if (!file_put_contents($filename, trim($phpSectionA . "\n" . $configurationXml . "\n" . $phpSectionB . "\n" . stripslashes($html) . "\n" . $phpSectionC)))
{
echo '<p><b>Error: Could not upload data.</b></p><p>Go <a href="'.$filename.'">back</a>.</p>';
}
else
{
echo '<p><b>Upload successfull.</b></p><p>Continue <a href="'.$filename.'">here</a>.</p>';
$html = $_POST['html'];
}
}
}
}
if (!isset($_GET['save']) &! isset($_GET['edit']) &! isset($_GET['import']) &! isset($_GET['admin']) &! isset($_GET['login']) &! isset($_GET['conf']))
echo stripslashes($html);
?>