edit: restrict to whitelisted ip addresses
[minimedit.git] / edit.php
1 <?php
2 function abort($status, $body) {
3         header("HTTP/1.1 $status");
4         print "$body\n";
5         exit;
6 }
7
8 if (!$_POST)
9         abort('405 post error', "niets te doen");
10 if (!isset($_POST['page']))
11         abort('409 input error', "geen bestand aangeleverd");
12
13 $filename = preg_replace('/(?:\.html)?$/', '.html', ltrim($_POST['page'], '/'), 1);
14 if (file_exists($filename) and !is_writable($filename))
15         abort('403 input error', "ongeldige bestandsnaam: $filename");
16
17 $prepend = '<!--#include virtual="common.html" -->'."\n\n";
18 $append  = "\n".'<!--#include virtual="footer.html" -->'."\n";
19 $upload = $_POST['body'];
20
21 if (!$upload)
22         abort('409 input error', "leeg bestand aangeleverd");
23
24 if (!file_put_contents($filename, $prepend . $upload . $append))
25         abort('500 save error', "fout bij schrijven van $filename");
26
27 print "Bestand opgeslagen";
28