login/visits: user details moved to /profile
[minimedit.git] / edit.php
1 <?php
2 ob_clean();
3
4 if (empty($User['admin']))
5         abort("geen beheersrechten", '401 unauthorised');
6
7 if ($_FILES) {
8         $img = @$_FILES['upload'];
9         if (!$img or $img['error'] !== UPLOAD_ERR_OK)
10                 abort('bestand niet goed ontvangen: '.$img['error'], '409 upload error');
11
12         $datadir = 'data/' . date('Y');
13         $target = $datadir.'/'.$img['name'];
14         move_uploaded_file($img['tmp_name'], $target);
15
16         switch (@$_GET['output']) {
17         case 'ckescript':
18                 printf('<script>window.parent.CKEDITOR.tools.callFunction(%s)</script>',
19                         "{$_GET['CKEditorFuncNum']}, '$target'"
20                 );
21                 break;
22         default:
23                 abort($target);
24         }
25         exit;
26 }
27
28 if (!$_POST)
29         abort("niets te doen", '405 post error');
30 if (!$Args)
31         abort("geen bestand aangeleverd", '409 input error');
32
33 $filename = ltrim($Args, '/').'.html';
34 if (preg_match('{^\.}', $filename))
35         abort("ongeldige bestandsnaam: $filename", '403 input error');
36 if (file_exists($filename) and !is_writable($filename))
37         abort("onwijzigbaar bestand: $filename", '403 input error');
38
39 if (!isset($_POST['body']))
40         abort("geen inhoud aangeleverd", '409 input error');
41
42 $upload = $_POST['body'];
43
44 if (!strlen($upload)) {
45         if (file_exists($filename) and !unlink($filename))
46                 abort("fout bij het verwijderen van $filename", '500 delete error');
47
48         abort("Bestand verwijderd");
49 }
50
51 if (!file_exists(dirname($filename)) and !mkdir(dirname($filename), 0777, TRUE))
52         abort("fout bij aanmaken van map voor $filename", '500 save error');
53
54 if (!file_put_contents($filename, $upload))
55         abort("fout bij schrijven van $filename", '500 save error');
56
57 if (is_writable('../.git')) {
58         $gitmsg = preg_replace('/\.html$/', '', $filename).": edit from {$_SERVER['REMOTE_ADDR']}";
59         $gitcmd = 'git';
60         $gitcmd .= ' -c user.name='.escapeshellarg($User['name']);
61         $gitcmd .= ' -c user.email='.escapeshellarg("{$User['name']}@lijtweg.nl");
62         $gitcmd .= ' commit -q';
63         $gitcmd .= ' -m '.escapeshellarg($gitmsg);
64         $gitcmd .= ' -- '.escapeshellarg($filename);
65         exec("$gitcmd 2>&1", $gitlog, $gitstatus);
66         if ($gitstatus) {
67                 trigger_error("git commit failure $gitstatus: ".implode("\n", $gitlog), E_USER_WARNING);
68         }
69 }
70
71 abort("Bestand opgeslagen");
72