diff --git a/src/client/js/app-edit.js b/src/client/js/app-edit.js index 83e544b..2da0619 100644 --- a/src/client/js/app-edit.js +++ b/src/client/js/app-edit.js @@ -1,7 +1,6 @@ -(function(window, document, console, CodeMirror) { +(function(window, document, slimwiki, console, CodeMirror) { - var slimwiki = window.slimwiki, - editor, + var editor, updatePreviewDelay = 1000, updatePreviewTimeout = null, updatePreviewRunning = false, @@ -31,7 +30,7 @@ updatePreviewRunning = true; var start = new Date().getTime(), articleFilename = slimwiki.settings.articleFilename; - callRpc('editor', 'saveArticle', [ articleFilename, editor.getValue() ], function(result, error) { + slimwiki.Util.callRpc('editor', 'saveArticle', [ articleFilename, editor.getValue() ], function(result, error) { updatePreviewRunning = false; if (error) { @@ -59,34 +58,6 @@ window.scrollTo(0, scrollFactor * (bodyElem.scrollHeight - bodyElem.clientHeight)); } - function callRpc(objectName, methodName, paramArray, done) { - var request = new XMLHttpRequest(), - requestJson; - - request.open('POST', 'rpc/' + objectName, true); - request.onreadystatechange = function () { - if (request.readyState == 4) { - if (request.status != 200) { - done(null, 'Request failed with status ' + request.status); - } else { - try { - var responseJson = JSON.parse(request.responseText); - if (responseJson.error) { - done(null, 'Request failed on server-side: ' + responseJson.error.message); - } else { - done(responseJson.result); - } - } catch (err) { - done(null, 'Request failed: ' + err); - } - } - } - }; - - requestJson = { jsonrpc: '2.0', method: methodName, params: paramArray || [], id: 1 }; - request.send(JSON.stringify(requestJson)); - } - init(); -})(window, document, console, CodeMirror); +})(window, document, slimwiki, console, CodeMirror); diff --git a/src/client/js/app-view.js b/src/client/js/app-view.js index 4d71352..05edf32 100644 --- a/src/client/js/app-view.js +++ b/src/client/js/app-view.js @@ -1,17 +1,81 @@ -(function(window, document, hljs) { +(function(window, document, slimwiki, hljs) { - window.slimwiki = window.slimwiki || {}; - - window.slimwiki.View = { + slimwiki.Util = { + callRpc: callRpc + }; + slimwiki.View = { updateSyntaxHighlighting: updateSyntaxHighlighting }; - updateSyntaxHighlighting(); + init(); - function updateSyntaxHighlighting() { - var blocks = document.getElementById('content').querySelectorAll('pre code'); + function init() { + var mode = slimwiki.settings.mode; + + if (mode == 'view' || mode == 'edit') { + updateSyntaxHighlighting(); + } else if (mode == 'createUser') { + initCreateUserForm(); + } + } + + function initCreateUserForm() { + document.getElementById('showConfigBtn').addEventListener('click', function() { + var user = document.getElementById('user').value, + pass = document.getElementById('password').value; + + callRpc('editor', 'createUserConfig', [ user, pass ], function(result, error) { + if (error) { + console.error('Creating user config failed:', error); + } else { + var resultBoxElem = document.getElementById('result-box'); + resultBoxElem.style.display = 'block'; + + document.getElementById('result').innerHTML = result.replace(/ 'Benutzername', + 'createUser.password' => 'Passwort', + 'createUser.showConfig' => 'Konfiguration anzeigen', + 'createUser.addToConfig' => 'Fügen Sie die folgende Zeile zu Ihrer config.php hinzu:' +); diff --git a/src/server/i18n/en.php b/src/server/i18n/en.php new file mode 100644 index 0000000..71c2362 --- /dev/null +++ b/src/server/i18n/en.php @@ -0,0 +1,8 @@ + 'User name', + 'createUser.password' => 'Password', + 'createUser.showConfig' => 'Show config', + 'createUser.addToConfig' => 'Add the following line to your config.php:' +); diff --git a/src/server/layout/page.php b/src/server/layout/page.php index 4b932db..f2aa859 100644 --- a/src/server/layout/page.php +++ b/src/server/layout/page.php @@ -1,4 +1,8 @@ - + @@ -9,12 +13,12 @@ - + - + -> -
- -
-
- -
-
-
- + - +
+ +
+
+ +
+ +
+
+ + +
+
+ + +
+ +
+ +
+
+
+
+ @@ -89,7 +120,7 @@ - + @@ -97,4 +128,5 @@ + diff --git a/src/server/logic/Context.php b/src/server/logic/Context.php index aff8c98..8963681 100644 --- a/src/server/logic/Context.php +++ b/src/server/logic/Context.php @@ -3,6 +3,7 @@ class Context { private $config; + private $i18n; private $articleBaseDir; private $dataBaseDir; @@ -26,7 +27,8 @@ class Context { // Defaults $config = array( 'wikiName' => 'Slim Wiki', - 'timezone' => 'Europe/Berlin' + 'timezone' => 'Europe/Berlin', + 'lang' => 'en' ); if (file_exists(__DIR__ . '/../../config.php')) { @@ -37,6 +39,18 @@ class Context { return $config; } + public function getI18n() { + if (! is_null($this->i18n)) { + return $this->i18n; + } + + $lang = $this->getConfig()['lang']; + include(__DIR__ . "/../i18n/$lang.php"); + + $this->i18n = $i18n; + return $i18n; + } + public function getArticleBaseDir() { return $this->articleBaseDir; } diff --git a/src/server/logic/EditorService.php b/src/server/logic/EditorService.php index 5265c94..574eb1b 100644 --- a/src/server/logic/EditorService.php +++ b/src/server/logic/EditorService.php @@ -10,7 +10,7 @@ class EditorService { } public function isRpcMethod($methodName) { - return ($methodName == 'saveArticle'); + return ($methodName == 'saveArticle' || $methodName == 'createUserConfig'); } public function saveArticle($articleFilename, $markdownText) { @@ -42,4 +42,12 @@ class EditorService { return $this->context->getRenderService()->renderMarkdown($markdownText, true); } + public function createUserConfig($user, $pass) { + $type = 'sha256'; + $salt = uniqid(mt_rand(), true); + $hash = hash($type, $pass . $salt); + + return "\$config['user.".strtolower($user)."'] = array('type' => '$type', 'salt' => '$salt', 'hash' => '$hash');"; + } + } diff --git a/src/server/logic/Main.php b/src/server/logic/Main.php index 8496866..e2df456 100644 --- a/src/server/logic/Main.php +++ b/src/server/logic/Main.php @@ -73,7 +73,11 @@ class Main { } private function handleGet($baseUrl, $basePath, $requestPathArray, $requestQuery) { - $isEditMode = $requestQuery == 'edit'; + if ($requestQuery == 'edit' || $requestQuery == 'createUser') { + $mode = $requestQuery; + } else { + $mode = 'view'; + } $articleFilename = $this->getArticleFilename($requestPathArray); if ($articleFilename == null) { @@ -86,7 +90,7 @@ class Main { $data = array(); $data['baseUrl'] = $baseUrl; $data['basePath'] = $basePath; - $data['isEditMode'] = $isEditMode; + $data['mode'] = $mode; foreach (array('wikiName', 'footerHtml') as $key) { $data[$key] = $config[$key]; @@ -97,7 +101,7 @@ class Main { $data['articleFilename'] = $articleFilename; $articleMarkdown = file_get_contents($this->context->getArticleBaseDir() . $articleFilename); $data['articleMarkdown'] = $articleMarkdown; - $data['articleHtml'] = $this->context->getRenderService()->renderMarkdown($articleMarkdown, $isEditMode); + $data['articleHtml'] = $this->context->getRenderService()->renderMarkdown($articleMarkdown, $mode == 'edit'); $this->renderPage($data); } @@ -153,6 +157,7 @@ class Main { private function renderPage($data) { header('Content-Type:text/html; charset=utf-8'); + $i18n = $this->context->getI18n(); include(__DIR__ . '/../layout/page.php'); }