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