From 46f922721d552b2c1ada10b20c31dc39969c6f21 Mon Sep 17 00:00:00 2001 From: Niko Date: Wed, 9 Feb 2022 22:22:41 +0100 Subject: [PATCH] Done login flow to obtain session cookie + more base code * Added templating system for .php files inside _templates/ * Use templating system with tpl* functions in base.php * Added templating to include scripts and other properties for tpl .php files * Added http.js, a regular JS helper for xhr-related operations * Added COOKIE check in PHP to redirect to APP_DIR --- web/src/_templates/pub/footer.php | 2 + web/src/_templates/pub/header.php | 14 +++++ web/src/app/js/http.js | 87 +++++++++++++++++++++++++++++++ web/src/base.php | 31 +++++++++++ web/src/index.php | 16 +++--- web/src/public/login.php | 33 ++++++++++-- 6 files changed, 172 insertions(+), 11 deletions(-) create mode 100644 web/src/_templates/pub/footer.php create mode 100644 web/src/_templates/pub/header.php create mode 100644 web/src/app/js/http.js diff --git a/web/src/_templates/pub/footer.php b/web/src/_templates/pub/footer.php new file mode 100644 index 0000000..308b1d0 --- /dev/null +++ b/web/src/_templates/pub/footer.php @@ -0,0 +1,2 @@ + + diff --git a/web/src/_templates/pub/header.php b/web/src/_templates/pub/header.php new file mode 100644 index 0000000..8a1a78b --- /dev/null +++ b/web/src/_templates/pub/header.php @@ -0,0 +1,14 @@ + + + <?php echo $title ?> + + + + + diff --git a/web/src/app/js/http.js b/web/src/app/js/http.js new file mode 100644 index 0000000..688efa7 --- /dev/null +++ b/web/src/app/js/http.js @@ -0,0 +1,87 @@ + +function isVisible(element) { + const rect = element.getBoundingClientRect(); + return ( + rect.top >= 0 && + rect.left >= 0 && + rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && + rect.right <= (window.innerWidth || document.documentElement.clientWidth) + ); +} + +function html2text(html) { + var e = document.createElement('div'); + e.innerHTML = html; + const result = e.innerText; + e.remove(); + return result; +} + +function JSON_to_URLEncoded(element,key,list) { + var list = list || []; + if (typeof(element) == 'object') { + for (var idx in element) + JSON_to_URLEncoded(element[idx],key?key+'['+idx+']':idx,list); + } else { + list.push(key+'='+encodeURIComponent(element)); + } + return list.join('&'); +} + +// source: https://www.geeksforgeeks.org/how-to-get-the-javascript-function-parameter-names-values-dynamically/ +function _get_func_params(func) { + // String representaation of the function code + var str = func.toString(); + // Remove comments of the form /* ... */ + // Removing comments of the form // + // Remove body of the function { ... } + // removing '=>' if func is arrow function + str = str.replace(/\/\*[\s\S]*?\*\//g, '') + .replace(/\/\/(.)*/g, '') + .replace(/{[\s\S]*}/, '') + .replace(/=>/g, '') + .trim(); + // Start parameter names after first '(' + var start = str.indexOf("(") + 1; + // End parameter names is just before last ')' + var end = str.length - 1; + var result = str.substring(start, end).split(", "); + var params = []; + result.forEach(element => { + // Removing any default value + element = element.replace(/=[\s\S]*/g, '').trim(); + if(element.length > 0) + params.push(element); + }); + return params; +} + +const http = { + request: function(method, path, payload, callbk) { + payload = payload || null; + callbk = callbk || null; + const oReq = new XMLHttpRequest(); + oReq.addEventListener("load", function() { + if (callbk) { + const ps = _get_func_params(callbk); + if (ps.includes('data') || ps.includes('text') || + ps.includes('html') || ps.includes('plain')) + callbk(this.responseText); + else if (ps.includes('json') || ps.includes('js')) { + try { callbk(JSON.parse(this.responseText)) } + catch (SyntaxError) { callbk(undefined) } + } + } + }); + oReq.open(method, path); + oReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + oReq.send(JSON_to_URLEncoded(payload)); + }, + get: function(path, payload, callbk) { + return http.request('GET', path, payload, callbk); + }, + post: function(path, payload, callbk) { + return http.request('POST', path, payload, callbk); + }, +}; + diff --git a/web/src/base.php b/web/src/base.php index 7272ec2..b20bc1b 100644 --- a/web/src/base.php +++ b/web/src/base.php @@ -3,3 +3,34 @@ function s($name) { echo $name; } + +function tpl($ns, $args=[]) { + $ns = str_replace('.','/',$ns); + $__tfile = "_templates/$ns.php"; + if (file_exists($__tfile)) { + unset($ns); + extract($args); + unset($args); + require $__tfile; + } +} + +function tpl_script($script) { + $suffix = ''; + if (getenv('env') === DEV) + $suffix = '?_='.microtime(); + echo APP_DIR.'/js/'.$script.'.js'.$suffix; +} +function tpl_scripts($scripts) { + foreach ($scripts as $script) { + $script = str_replace('.', '/', $script); + ?> - + ' s(login)', + 'scripts' => ['http'] +]) ?> -?> -
- Email + +
- Password +
+ + +