diff options
author | jnew-gh <github@hazelden.ca> | 2021-04-18 22:31:50 +0300 |
---|---|---|
committer | jnew-gh <github@hazelden.ca> | 2021-04-18 22:31:50 +0300 |
commit | e2db95a5f9544640c3871fba6b81f99306c740b4 (patch) | |
tree | 5c6004ca98a4344e99170ed0a71b7e38fa2b05e3 | |
parent | c219fb9149e0ef7096cec1e0643985c9593e0dec (diff) |
Cookie variables and functions
All variables and functions necessary to store and retrieve cookies
-rw-r--r-- | dmarcts-report-viewer-common.php | 45 | ||||
-rw-r--r-- | dmarcts-report-viewer-report-data.php | 5 | ||||
-rw-r--r-- | dmarcts-report-viewer-report-list.php | 4 | ||||
-rw-r--r-- | dmarcts-report-viewer.js | 116 | ||||
-rw-r--r-- | dmarcts-report-viewer.php | 6 |
5 files changed, 175 insertions, 1 deletions
diff --git a/dmarcts-report-viewer-common.php b/dmarcts-report-viewer-common.php index 09aa7b7..1791183 100644 --- a/dmarcts-report-viewer-common.php +++ b/dmarcts-report-viewer-common.php @@ -34,6 +34,8 @@ //### variables ###################################################### //#################################################################### +$cookie_name = "dmarcts-options"; + // The order in which the options appear here is the order they appear in the DMARC Results dropdown box $dmarc_result = array( @@ -334,3 +336,46 @@ function format_date($date, $format) { $answer = date($format, strtotime($date)); return $answer; }; + +// Get all configuration options +// -------------------------------------------------------------------------- +function configure() { + + global $cookie_name; + global $cookie_options; + global $options; + + $option = array_keys($options); + $cookie_options = array(); + $cookie_timeout = 60*60*24*365; + + if(!isset($_COOKIE[$cookie_name]) || $_COOKIE[$cookie_name] == "" ) { + // No Cookie + foreach ($option as $option_name) { + if ( $options[$option_name]['option_type'] != "heading" ) { + $cookie_options += array($option_name => $options[$option_name]['option_value']); + // foreach($options[$option_name] as $key=>$value) { + } + } + setcookie($cookie_name, json_encode($cookie_options), time() + $cookie_timeout, "/"); + } else { + // Cookie exists + if ($_SERVER["REQUEST_METHOD"] == "POST") { + // POST + foreach ($option as $option_name) { + if ( $options[$option_name]['option_type'] != "heading" ) { + if ( is_null($_POST[$option_name]) ) { + $cookie_options += array($option_name => ""); + } else { + $cookie_options += array($option_name => $_POST[$option_name]); + } + } + } + setcookie($cookie_name, json_encode($cookie_options), time() + $cookie_timeout, "/"); + header("Location: dmarcts-report-viewer.php"); + exit; + } else { // Not POST + $cookie_options = json_decode($_COOKIE[$cookie_name], true); + } + } +} diff --git a/dmarcts-report-viewer-report-data.php b/dmarcts-report-viewer-report-data.php index 107e9d1..beff179 100644 --- a/dmarcts-report-viewer-report-data.php +++ b/dmarcts-report-viewer-report-data.php @@ -201,6 +201,11 @@ function formatXML($xml) { include "dmarcts-report-viewer-config.php"; include "dmarcts-report-viewer-common.php"; +// Get all configuration options +// -------------------------------------------------------------------------- +configure(); + + // Parameters of GET // -------------------------------------------------------------------------- diff --git a/dmarcts-report-viewer-report-list.php b/dmarcts-report-viewer-report-list.php index 82bf647..78c3de0 100644 --- a/dmarcts-report-viewer-report-list.php +++ b/dmarcts-report-viewer-report-list.php @@ -104,6 +104,10 @@ function tmpl_reportList($reports, $sort) { include "dmarcts-report-viewer-config.php"; include "dmarcts-report-viewer-common.php"; +// Get all configuration options +// -------------------------------------------------------------------------- +configure(); + $dom_select= ''; $org_select= ''; $per_select= ''; diff --git a/dmarcts-report-viewer.js b/dmarcts-report-viewer.js index 86dbd2d..35b3981 100644 --- a/dmarcts-report-viewer.js +++ b/dmarcts-report-viewer.js @@ -37,7 +37,7 @@ var current_report; const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent; const comparer = (idx, asc) => (a, b) => ((v1, v2) => v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2))(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx)); -var report_data_xml_width_last = 0; +var cookie_name = "dmarcts-options"; // ---------------------------------------------------------------------------- //Functions @@ -443,3 +443,117 @@ function makeResizableDiv() { window.removeEventListener('mousemove', resize_vertical); } } + +// Cookie Functions +// ---------------------------------------------------------------------------- +function build_cookie() { + + // Don't allow cookie to be set if there are no reports displayed + if ( document.getElementById('reportlistTbl') != "undefined" && document.getElementById('reportlistTbl') != null ) { + // There are reports showing + + // Build cookie from various sources + + // Change the Period option from the value of the select to (i.e. 'all' or a date) to a boolean, to match the radio button option on the options page + if ( document.getElementById('selPeriod').value == "all" ) { + period = 0; + } else { + period = 1; + } + + // Host lookup + if ( document.getElementsByName('HostLookup')[0].checked ) { + hostlookup = 1; + } else { + hostlookup = 0; + } + + // Sort column and sort direction + if ( document.getElementById('reportlistTbl').getElementsByTagName('thead')[0].getElementsByTagName('tr')[0].getElementsByClassName('desc_triangle').length != 0 ) { + sort_column = document.getElementById('reportlistTbl').getElementsByTagName('thead')[0].getElementsByTagName('tr')[0].getElementsByClassName('desc_triangle')[0].id; + sort = 0; + } else { + sort_column = document.getElementById('reportlistTbl').getElementsByTagName('thead')[0].getElementsByTagName('tr')[0].getElementsByClassName('asc_triangle')[0].id; + sort = 1; + } + + // Create cookie_value object that gets placed into cookie + + // When a new option is added, check the size of the cookie stored. The cookie size should be less than half of the maximum cookie size allowed per domain. + // Less than half because sometimes the cookie is stored twice (once as dmarcts-options and once as dmarcts-options-tmp). Most browsers have a cookie limit of 4KB. + // Currently, the following options generate a cookie of about 0.5KB + cookie_value = { + "cssfile" : document.styleSheets[0].href.split('/').pop() , + "report_list_height_percent" : parseInt( document.getElementById('report_list').offsetHeight/available_height * 100 + 0.5 ) , + "report_data_xml_width_percent" : report_data_xml_width_percent , + "xml_data_open" : xml_data_open , + "HostLookup" : hostlookup , + "Period" : period , + "DMARC" : document.getElementById('selDMARC').value , + "ReportStatus" : document.getElementById('selReportStatus').value , + "Domain" : document.getElementById('selDomain').value , + "Organisation" : document.getElementById('selOrganisation').value , + "sort_column" : sort_column , + "sort" : sort , + // "alignment_unknown" : 0 , + "dmarc_results_matching_only" : 0 , + "report_data_status" : "all" + }; + + cookie_value = JSON.stringify(cookie_value); + setCookie(cookie_name, cookie_value, 365) + } else { + // There are NO reports showing + alert("Settings cannot be saved if there are no reports to display.\n\nChange the filters to show some reports."); + } + setCookie("dmarcts-options-tmp", "", -365) + hideMenu(); +} + +function get_cookie(name) { + + name = name + "="; + var cookie_str = decodeURIComponent(document.cookie); + var cookie_array = cookie_str.split(';'); + for(var i = 0; i <cookie_array.length; i++) { + var c = cookie_array[i]; + while ( c.charAt(0) == " " ) { + c = c.substring(1); + } + if ( c.indexOf(name) == 0 ) { + return c.substring(name.length, c.length); + } + } + return ""; +} + +function setCookie(name, value, exp_days) { + + var d = new Date(); + d.setTime(d.getTime() + (exp_days*24*60*60*1000)); + var expires = "expires="+ d.toUTCString(); + document.cookie = name + "=" + encodeURI(value) + ";" + expires + ";path=/"; +} + +function resetOptions() { + + stored_cookie = get_cookie('dmarcts-options-tmp'); + + if ( stored_cookie == "" || stored_cookie == null ){ + stored_cookie = get_cookie('dmarcts-options'); + setCookie("dmarcts-options-tmp", stored_cookie, 365) + setCookie("dmarcts-options", "", 365) + } + window.location.href = 'dmarcts-report-viewer-options.php'; +} + +function cancelOptions() { + + stored_cookie = get_cookie('dmarcts-options-tmp'); + + if ( stored_cookie ){ + setCookie("dmarcts-options", stored_cookie, 365) + setCookie("dmarcts-options-tmp", "", -365) + } + window.location.href = 'dmarcts-report-viewer.php'; +} diff --git a/dmarcts-report-viewer.php b/dmarcts-report-viewer.php index e0903a1..40c007c 100644 --- a/dmarcts-report-viewer.php +++ b/dmarcts-report-viewer.php @@ -197,6 +197,12 @@ function html ($default_hostlookup = 1, $default_dmarc_result = undef, $default_ include "dmarcts-report-viewer-config.php"; include "dmarcts-report-viewer-common.php"; +// Get all configuration options +// -------------------------------------------------------------------------- +configure(); + +setcookie("dmarcts-options-tmp", "", "01 Jan 1970 00:00:00 UTC", "/"); + // Make a MySQL Connection using mysqli // -------------------------------------------------------------------------- $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport); |