1 // Copyright (c) 2016-2023 by the citadel.org team
3 // This program is open source software. Use, duplication, or
4 // disclosure are subject to the GNU General Public License v3.
7 // // (We don't need this anymore because we encode on the server side)
8 // // Function to encode data in quoted-printable format
9 // // Written by Theriault and Brett Zamir [https://locutus.io/php/quoted_printable_encode/]
10 // function quoted_printable_encode(str) {
11 // const hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
12 // const RFC2045Encode1IN = / \r\n|\r\n|[^!-<>-~ ]/gm
13 // const RFC2045Encode1OUT = function (sMatch) {
14 // // Encode space before CRLF sequence to prevent spaces from being stripped
15 // // Keep hard line breaks intact; CRLF sequences
16 // if (sMatch.length > 1) {
17 // return sMatch.replace(' ', '=20');
19 // // Encode matching character
20 // const chr = sMatch.charCodeAt(0);
21 // return '=' + hexChars[((chr >>> 4) & 15)] + hexChars[(chr & 15)];
23 // // Split lines to 75 characters; the reason it's 75 and not 76 is because softline breaks are
24 // // preceeded by an equal sign; which would be the 76th character. However, if the last line/string
25 // // was exactly 76 characters, then a softline would not be needed. PHP currently softbreaks
26 // // anyway; so this function replicates PHP.
27 // const RFC2045Encode2IN = /.{1,72}(?!\r\n)[^=]{0,3}/g
28 // const RFC2045Encode2OUT = function (sMatch) {
29 // if (sMatch.substr(sMatch.length - 2) === '\r\n') {
32 // return sMatch + '=\r\n';
34 // str = str.replace(RFC2045Encode1IN, RFC2045Encode1OUT).replace(RFC2045Encode2IN, RFC2045Encode2OUT);
35 // // Strip last softline break
36 // return str.substr(0, str.length - 3)
40 // generate a random string -- mainly used for generating one-time-use div names
41 function randomString() {
42 return Math.random().toString(36).replace('0.','ctdl_' || '');
46 // string escape for html display
47 function escapeHTML(text) {
49 return text.replace(/[\"&<>]/g, function (a) {
60 // string escape for html display FIXME can this be replaced with encodeURI() from the standard library?
61 function escapeHTMLURI(text) {
63 return text.replace(/./g, function (a) {
64 return '%' + a.charCodeAt(0).toString(16);
69 // string escape for JavaScript string
70 function escapeJS(text) {
72 return text.replace(/[\"\']/g, function (a) {
78 // Convert a UNIX timestamp to the browser's local time
79 // See also: https://timestamp.online/article/how-to-convert-timestamp-to-datetime-in-javascript
80 // format should be: 0=full (date+time), 1=brief (date if not today, time if today)
81 function string_timestamp(timestamp, format) {
82 var ts = new Date(timestamp * 1000);
84 var now_ts = new Date(Date.now());
85 if (ts.toLocaleDateString() == now_ts.toLocaleDateString()) {
86 return(ts.toLocaleTimeString());
89 return(ts.toLocaleDateString());
93 return(ts.toLocaleString());
98 // An old version of string_timestamp() did it the hard way.
99 // It used https://gist.github.com/kmaida/6045266 as a reference.
100 // check git history prior to 2022-jul-03 if you want to see it.
103 // Get the value of a cookie from the HTTP session
104 // Shamelessly swiped from https://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript
105 const getCookieValue = (name) => (
106 document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || ''