edit: page editor and php save handler
authorMischa POSLAWSKY <perl@shiar.org>
Tue, 8 Jul 2014 13:28:23 +0000 (15:28 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 8 Jul 2014 22:48:57 +0000 (00:48 +0200)
Inline CKEditor to alter static contents in <div class="article">.
Assumes wanted v4.1.2 ckeditor.js in ckeditor/.

edit.js [new file with mode: 0644]
edit.php [new file with mode: 0644]
footer.html

diff --git a/edit.js b/edit.js
new file mode 100644 (file)
index 0000000..fda6bf9
--- /dev/null
+++ b/edit.js
@@ -0,0 +1,50 @@
+CKEDITOR.plugins.add('inlinesave', {
+       init: function(editor) {
+               editor.addCommand( 'inlinesave', {
+                       exec: function (editor) {
+                               var pagename = window.location.pathname.replace(/^\//, '') || 'index';
+                               var data = 'page='+encodeURIComponent(pagename)+'&body='+encodeURIComponent(editor.getData());
+                               ajaxpost = new XMLHttpRequest();
+                               ajaxpost.open('POST', 'edit.php', true);
+                               ajaxpost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+                               ajaxpost.onreadystatechange = function () {
+                                       if (ajaxpost.readyState != 4)
+                                               return; // not done yet
+                                       if (ajaxpost.status != 200)
+                                               alert('Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText);
+                                       //else alert('ok: '+ajaxpost.responseText);
+                               };
+                               ajaxpost.send(data);
+                       },
+               });
+               editor.ui.addButton( 'Inlinesave', {
+                       command: 'inlinesave',
+                       label: editor.lang.save.toolbar,
+                       icon: this.path + '../save/icons/save.png',
+               });
+       }
+});
+
+CKEDITOR.on('instanceCreated', function (event) {
+       var editor = event.editor;
+       editor.on('configLoaded', function () {
+               var config = editor.config;
+               config.language = 'nl';
+               config.extraPlugins = 'sourcedialog,inlinesave';
+               config.format_tags = 'h2;h3;h4;p';
+               config.allowedContent = true;
+               config.entities = false; // keep unicode
+               config.toolbar = [
+                       ['Inlinesave', '-', 'ShowBlocks', 'Sourcedialog', '-', 'Undo', 'Redo'],
+                       ['Format'],
+                       ['BulletedList', 'NumberedList', '-', 'Blockquote'],
+                       ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Link'],
+                       ['HorizontalRule', 'Table', 'Image'],
+               ];
+               config.enterMode = CKEDITOR.ENTER_BR; // results in <p>; ENTER_P does it twice
+       });
+});
+
+var pagebody = document.getElementsByClassName('article')[0];
+pagebody.setAttribute('contenteditable', 'true');
+
diff --git a/edit.php b/edit.php
new file mode 100644 (file)
index 0000000..14bfde0
--- /dev/null
+++ b/edit.php
@@ -0,0 +1,24 @@
+<?php
+function abort($status, $body) {
+       header("HTTP/1.1 $status");
+       print "$body\n";
+       exit;
+}
+
+$filename = $_POST['page'];
+$filename = ltrim($filename, '/');
+if (!preg_match('/^[a-z]+\.html$/', $filename))
+       abort('403 input error', "Ongeldige bestandsnaam: $filename");
+
+$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 (!file_put_contents($filename, $prepend . $upload . $append))
+       abort('500 save error', "kon bestand niet overschrijven");
+
+print "Bestand opgeslagen";
+
index 7e5febca8059395aa5d1511e155fe8e88ee2c5c0..bd4ca9712e7e553ddec5ad579eb6128477d17345 100644 (file)
@@ -1,3 +1,5 @@
 </div>
 <hr style="clear:both" />
+<script src="/ckeditor/ckeditor.js"></script>
+<script src="/edit.js"></script>
 </body></html>