edit: save changes as git commit
[minimedit.git] / edit.php
index 1b81ccf2aa8f4242dbcdddeaecd7d7636c5e17f7..9ba0d04c02c34d2265851839eedd045cdcbfba95 100644 (file)
--- a/edit.php
+++ b/edit.php
@@ -10,6 +10,27 @@ function abort($body, $status = NULL) {
 if (!@$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;
+}
+
 if (!$_POST)
        abort("niets te doen", '405 post error');
 if (!$Args)
@@ -39,5 +60,19 @@ if (!file_exists(dirname($filename)) and !mkdir(dirname($filename)))
 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);
+       }
+}
+
 abort("Bestand opgeslagen");