From f391773c6562133e9307c2ee58b82db04d709af4 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 1 Mar 2020 08:54:48 +0100 Subject: [PATCH 1/5] generalize date string handling, replacing hardcoded lookups, fixes #586 --- js/privatebin.js | 128 +++++++++++++++++++++++++++++++++++++--------- tpl/bootstrap.php | 2 +- tpl/page.php | 2 +- 3 files changed, 107 insertions(+), 25 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index e76bf98..139ecc7 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -209,6 +209,64 @@ jQuery.PrivateBin = (function($, RawDeflate) { '=': '=' }; + /** + * number of seconds in a minute + * + * @name Helper.minute + * @private + * @enum {number} + * @readonly + */ + const minute = 60; + + /** + * number of seconds in an hour + * + * = 60 * 60 seconds + * + * @name Helper.minute + * @private + * @enum {number} + * @readonly + */ + const hour = 3600; + + /** + * number of seconds in a day + * + * = 60 * 60 * 24 seconds + * + * @name Helper.day + * @private + * @enum {number} + * @readonly + */ + const day = 86400; + + /** + * number of seconds in a month (30 days, an approximation) + * + * = 60 * 60 * 24 * 30 seconds + * + * @name Helper.month + * @private + * @enum {number} + * @readonly + */ + const month = 2592000; + + /** + * number of seconds in a non-leap year + * + * = 60 * 60 * 24 * 365 seconds + * + * @name Helper.year + * @private + * @enum {number} + * @readonly + */ + const year = 31536000; + /** * cache for script location * @@ -229,31 +287,67 @@ jQuery.PrivateBin = (function($, RawDeflate) { me.secondsToHuman = function(seconds) { let v; - if (seconds < 60) + if (seconds < minute) { v = Math.floor(seconds); return [v, 'second']; } - if (seconds < 60 * 60) + if (seconds < hour) { - v = Math.floor(seconds / 60); + v = Math.floor(seconds / minute); return [v, 'minute']; } - if (seconds < 60 * 60 * 24) + if (seconds < day) { - v = Math.floor(seconds / (60 * 60)); + v = Math.floor(seconds / hour); return [v, 'hour']; } // If less than 2 months, display in days: - if (seconds < 60 * 60 * 24 * 60) + if (seconds < (2 * month)) { - v = Math.floor(seconds / (60 * 60 * 24)); + v = Math.floor(seconds / day); return [v, 'day']; } - v = Math.floor(seconds / (60 * 60 * 24 * 30)); + v = Math.floor(seconds / month); return [v, 'month']; }; + /** + * converts a duration string into seconds + * + * The string is expected to be optional digits, followed by a time. + * Supported times are: min, hour, day, month, year, never + * Examples: 5min, 13hour, never + * + * @name Helper.durationToSeconds + * @function + * @param {String} duration + * @return {number} + */ + me.durationToSeconds = function(duration) + { + let pieces = duration.split(/\d+/), + factor = pieces[0] || 0, + timespan = pieces[1] || pieces[0]; + switch (timespan) + { + case 'min': + return factor * minute; + case 'hour': + return factor * hour; + case 'day': + return factor * day; + case 'month': + return factor * month; + case 'year': + return factor * year; + case 'never': + return 0; + default: + return factor; + } + }; + /** * text range selection * @@ -432,22 +526,10 @@ jQuery.PrivateBin = (function($, RawDeflate) { * @return {Date} */ me.calculateExpirationDate = function(initialDate, expirationDisplayStringOrSecondsToExpire) { - let expirationDate = new Date(initialDate); - - const expirationDisplayStringToSecondsDict = { - '5min': 300, - '10min': 600, - '1hour': 3500, - '1day': 86400, - '1week': 604800, - '1month': 2592000, - '1year': 31536000, - 'never': 0 - }; - - let secondsToExpiration = expirationDisplayStringOrSecondsToExpire; + let expirationDate = new Date(initialDate), + secondsToExpiration = expirationDisplayStringOrSecondsToExpire; if (typeof expirationDisplayStringOrSecondsToExpire === 'string') { - secondsToExpiration = expirationDisplayStringToSecondsDict[expirationDisplayStringOrSecondsToExpire]; + secondsToExpiration = me.durationToSeconds(expirationDisplayStringOrSecondsToExpire); } if (typeof secondsToExpiration !== 'number') { diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index faaa977..a5474bb 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -72,7 +72,7 @@ endif; ?> - + diff --git a/tpl/page.php b/tpl/page.php index 8dc9c0d..171f997 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -50,7 +50,7 @@ endif; ?> - + From 879a2a92550b1df3d6b6be62644aea51d4bd30a5 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 1 Mar 2020 08:55:24 +0100 Subject: [PATCH 2/5] tweaking German translation for email strings --- i18n/de.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/i18n/de.json b/i18n/de.json index 31eec78..47e8adc 100644 --- a/i18n/de.json +++ b/i18n/de.json @@ -172,9 +172,9 @@ "Notice:": "Hinweis:", "This link will expire after %s.": - "Dieser Link wird in %s ablaufen.", + "Dieser Link wird um %s ablaufen.", "This link can only be accessed once, do not use back or refresh button in your browser.": - "Dieser Link kann nur einmal geöffnet werden, verwende nicht den \"Zurück\" oder \"Neu laden\" Knopf Deines Browsers.", + "Dieser Link kann nur einmal geöffnet werden, verwende nicht den Zurück- oder Neu-laden-Knopf Deines Browsers.", "Link:": "Link:", "Recipient may become aware of your timezone, convert time to UTC?": From f05e5c2e2988ae1e86b981bceaa03fdb8fa2a1a0 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 1 Mar 2020 16:14:19 +0100 Subject: [PATCH 3/5] documenting change --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e95452..91936c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * **1.4 (not yet released)** * CHANGED: Minimum required PHP version is 5.6, due to a change in the identicon library and to use php's native hash_equals() * CHANGED: Upgrading libraries to: identicon 2.0.0 + * FIXED: Support custom expiration options in email function (#586) * **1.3.3 (2020-02-16)** * CHANGED: Upgrading libraries to: DOMpurify 2.0.8 * CHANGED: Several translations got updated with missing messages From 0564c0e62e56e6b1113bd247a3df7b633800bc66 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Fri, 6 Mar 2020 20:46:51 +0100 Subject: [PATCH 4/5] fixing 'The provided fixer 'method_argument_space' cannot be disabled unless it was already enabled by your preset.' --- .styleci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.styleci.yml b/.styleci.yml index 238f41a..63dc73d 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -17,7 +17,6 @@ disabled: - concat_without_spaces - declare_equal_normalize - heredoc_to_nowdoc - - method_argument_space - new_with_braces - no_alternative_syntax - phpdoc_align From 33bb0c6bd6e5bee609829e1cc5ef9b1a36f89868 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Fri, 6 Mar 2020 20:50:19 +0100 Subject: [PATCH 5/5] trying to recreate former StyleCI behaviour in changed preset --- .styleci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.styleci.yml b/.styleci.yml index 63dc73d..9c2c76c 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -17,6 +17,7 @@ disabled: - concat_without_spaces - declare_equal_normalize - heredoc_to_nowdoc + - method_argument_space_strict - new_with_braces - no_alternative_syntax - phpdoc_align