nieuws: treat years before 2000 as single page
[minimedit.git] / edit.php
index 14bfde092a06c0384fa76d1ade619e037e519026..25f9f1be5454506c137cdc85a698357380810159 100644 (file)
--- a/edit.php
+++ b/edit.php
@@ -1,24 +1,72 @@
 <?php
-function abort($status, $body) {
-       header("HTTP/1.1 $status");
-       print "$body\n";
+ob_clean();
+
+if (empty($User['admin']))
+       abort("geen beheersrechten", '401 unauthorised');
+
+if ($_FILES) {
+       $img = @$_FILES['upload'];
+       if (!$img or $img['error'] !== UPLOAD_ERR_OK)
+               abort('bestand niet goed ontvangen: '.$img['error'], '409 upload error');
+
+       $datadir = 'data/' . date('Y');
+       $target = $datadir.'/'.$img['name'];
+       move_uploaded_file($img['tmp_name'], $target);
+
+       switch (@$_GET['output']) {
+       case 'ckescript':
+               printf('<script>window.parent.CKEDITOR.tools.callFunction(%s)</script>',
+                       "{$_GET['CKEditorFuncNum']}, '$target'"
+               );
+               break;
+       default:
+               abort($target);
+       }
        exit;
 }
 
-$filename = $_POST['page'];
-$filename = ltrim($filename, '/');
-if (!preg_match('/^[a-z]+\.html$/', $filename))
-       abort('403 input error', "Ongeldige bestandsnaam: $filename");
+if (!$_POST)
+       abort("niets te doen", '405 post error');
+if (!$Args)
+       abort("geen bestand aangeleverd", '409 input error');
+
+$filename = ltrim($Args, '/').'.html';
+if (preg_match('{^\.}', $filename))
+       abort("ongeldige bestandsnaam: $filename", '403 input error');
+if (file_exists($filename) and !is_writable($filename))
+       abort("onwijzigbaar bestand: $filename", '403 input error');
+
+if (!isset($_POST['body']))
+       abort("geen inhoud aangeleverd", '409 input error');
 
-$prepend = '<!--#include virtual="common.html" -->'."\n\n";
-$append  = "\n\n".'<!--#include virtual="footer.html" -->'."\n";
 $upload = $_POST['body'];
 
-if (!$upload)
-       abort('409 input error', "leeg bestand aangeleverd");
+if (!strlen($upload)) {
+       if (file_exists($filename) and !unlink($filename))
+               abort("fout bij het verwijderen van $filename", '500 delete error');
 
-if (!file_put_contents($filename, $prepend . $upload . $append))
-       abort('500 save error', "kon bestand niet overschrijven");
+       abort("Bestand verwijderd");
+}
+
+if (!file_exists(dirname($filename)) and !mkdir(dirname($filename), 0777, TRUE))
+       abort("fout bij aanmaken van map voor $filename", '500 save error');
+
+if (!file_put_contents($filename, $upload))
+       abort("fout bij schrijven van $filename", '500 save error');
+
+if (is_writable('../.git')) {
+       $gitmsg = preg_replace('/\.html$/', '', $filename).": edit from {$_SERVER['REMOTE_ADDR']}";
+       $gitcmd = 'git';
+       $gitcmd .= ' -c user.name='.escapeshellarg($User['name']);
+       $gitcmd .= ' -c user.email='.escapeshellarg("{$User['name']}@lijtweg.nl");
+       $gitcmd .= ' commit -q';
+       $gitcmd .= ' -m '.escapeshellarg($gitmsg);
+       $gitcmd .= ' -- '.escapeshellarg($filename);
+       exec("$gitcmd 2>&1", $gitlog, $gitstatus);
+       if ($gitstatus) {
+               trigger_error("git commit failure $gitstatus: ".implode("\n", $gitlog), E_USER_WARNING);
+       }
+}
 
-print "Bestand opgeslagen";
+abort("Bestand opgeslagen");