/* ----------------------------------------------------------------------------- * Pi.Alert * Open Source Network Guard / WIFI & LAN intrusion detector * * pialert_common.js - Front module. Common Javascript functions *------------------------------------------------------------------------------- # Puche 2021 / 2022+ jokob jokob@duck.com GNU GPLv3 ----------------------------------------------------------------------------- */ // ----------------------------------------------------------------------------- var timerRefreshData = '' var modalCallbackFunction = ''; // urlParams = new Proxy(new URLSearchParams(window.location.search), { // get: (searchParams, prop) => searchParams.get(prop.toString()), // }); function getCache(key) { // check cache if(sessionStorage.getItem(key)) { return sessionStorage.getItem(key); } else { return ""; } } function setCache(key, data) { sessionStorage.setItem(key, data); } // ----------------------------------------------------------------------------- function handleVersion(){ release_timestamp = getCookie("release_timestamp") if(release_timestamp != "") { build_timestamp = parseInt($('#version').attr("data-build-time").match( /\d+/g ).join('')) // if the release_timestamp is older by 10 min or more as the build timestamp then there is a new release available if(release_timestamp > build_timestamp + 600 ) { console.log("New release!") $('#version').attr("class", $('#version').attr("class").replace("myhidden", "")) } else{ console.log("All up-to-date!") $('#current-version-text').attr("class", $('#current-version-text').attr("class").replace("myhidden", "")) // new-version-text current-version-text } } } //-------------------------------------------------------------- function getVersion() { release_timestamp = getCookie("release_timestamp") // no cached value available if(release_timestamp == "") { // get parameter value $.get('https://api.github.com/repos/jokob-sk/Pi.Alert/releases', function(data) { var releases = data; if(releases.length > 0) { release_datetime = releases[0].published_at; release_timestamp = new Date(release_datetime).getTime() / 1000; // cache value setCookie("release_timestamp", release_timestamp ,5); handleVersion(); } }); } else { // cache is available, just call the handler handleVersion() } } // ----------------------------------------------------------------------------- function setCookie (cookie, value, expirationMinutes='') { // Calc expiration date var expires = ''; if (typeof expirationMinutes === 'number') { expires = ';expires=' + new Date(Date.now() + expirationMinutes *60*1000).toUTCString(); } // Save Cookie document.cookie = cookie + "=" + value + expires; } // ----------------------------------------------------------------------------- function getCookie (cookie) { // Array of cookies var allCookies = document.cookie.split(';'); // For each cookie for (var i = 0; i < allCookies.length; i++) { var currentCookie = allCookies[i].trim(); // If the current cookie is the correct cookie if (currentCookie.indexOf (cookie +'=') == 0) { // Return value return currentCookie.substring (cookie.length+1); } } // Return empty (not found) return ""; } // ----------------------------------------------------------------------------- function deleteCookie (cookie) { document.cookie = cookie + '=;expires=Thu, 01 Jan 1970 00:00:00 UTC'; } // ----------------------------------------------------------------------------- function deleteAllCookies() { // Array of cookies var allCookies = document.cookie.split(";"); // For each cookie for (var i = 0; i < allCookies.length; i++) { var cookie = allCookies[i].trim(); var eqPos = cookie.indexOf("="); var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 UTC"; } } // ----------------------------------------------------------------------------- function showModalOk (title, message, callbackFunction = null) { // set captions $('#modal-ok-title').html (title); $('#modal-ok-message').html (message); if(callbackFunction!= null) { $("#modal-ok-OK").click(function() { callbackFunction() }); } // Show modal $('#modal-ok').modal('show'); } function showModalDefault (title, message, btnCancel, btnOK, callbackFunction) { // set captions $('#modal-default-title').html (title); $('#modal-default-message').html (message); $('#modal-default-cancel').html (btnCancel); $('#modal-default-OK').html (btnOK); modalCallbackFunction = callbackFunction; // Show modal $('#modal-default').modal('show'); } // ----------------------------------------------------------------------------- function showModalWarning (title, message, btnCancel, btnOK, callbackFunction) { // set captions $('#modal-warning-title').html (title); $('#modal-warning-message').html (message); $('#modal-warning-cancel').html (btnCancel); $('#modal-warning-OK').html (btnOK); modalCallbackFunction = callbackFunction; // Show modal $('#modal-warning').modal('show'); } // ----------------------------------------------------------------------------- function modalDefaultOK () { // Hide modal $('#modal-default').modal('hide'); // timer to execute function window.setTimeout( function() { window[modalCallbackFunction](); }, 100); } // ----------------------------------------------------------------------------- function modalWarningOK () { // Hide modal $('#modal-warning').modal('hide'); // timer to execute function window.setTimeout( function() { window[modalCallbackFunction](); }, 100); } // ----------------------------------------------------------------------------- function sanitize(data) { return data.replace(/(\r\n|\n|\r)/gm,"").replace(/[^\x00-\x7F]/g, "") } // ----------------------------------------------------------------------------- function showMessage (textMessage="") { if (textMessage.toLowerCase().includes("error") ) { // show error alert (textMessage); } else { // show temporal notification $("#alert-message").html (textMessage); $("#notification").fadeIn(1, function () { window.setTimeout( function() { $("#notification").fadeOut(500) }, 3000); } ); } } // ----------------------------------------------------------------------------- function setParameter (parameter, value) { // Retry $.get('php/server/parameters.php?action=set¶meter=' + parameter + '&value='+ value, function(data) { if (data != "OK") { // Retry sleep (200); $.get('php/server/parameters.php?action=set¶meter=' + parameter + '&value='+ value, function(data) { if (data != "OK") { // alert (data); } else { // alert ("OK. Second attempt"); }; } ); }; } ); } // ----------------------------------------------------------------------------- function sleep(milliseconds) { const date = Date.now(); let currentDate = null; do { currentDate = Date.now(); } while (currentDate - date < milliseconds); } // ----------------------------------------------------------------------------- function translateHTMLcodes (text) { if (text == null) { return null; } var text2 = text.replace(new RegExp(' ', 'g'), " "); text2 = text2.replace(new RegExp('<', 'g'), "<"); return text2; } // ----------------------------------------------------------------------------- function stopTimerRefreshData () { try { clearTimeout (timerRefreshData); } catch (e) {} } // ----------------------------------------------------------------------------- function newTimerRefreshData (refeshFunction) { timerRefreshData = setTimeout (function() { refeshFunction(); }, 60000); } // ----------------------------------------------------------------------------- function debugTimer () { $('#pageTitle').html (new Date().getSeconds()); } // ----------------------------------------------------------------------------- function openInNewTab (url) { window.open(url, "_blank"); }