]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/util.js
new license declaration text
[citadel.git] / webcit-ng / static / js / util.js
1 //
2 // Copyright (c) 2016-2022 by the citadel.org team
3 //
4 // This program is open source software.  Use, duplication, or
5 // disclosure are subject to the GNU General Public License v3.
6
7
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');
18                 }
19                 // Encode matching character
20                 const chr = sMatch.charCodeAt(0);
21                 return '=' + hexChars[((chr >>> 4) & 15)] + hexChars[(chr & 15)];
22         }
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') {
30                         return sMatch;
31                 }
32                 return sMatch + '=\r\n';
33         }
34         str = str.replace(RFC2045Encode1IN, RFC2045Encode1OUT).replace(RFC2045Encode2IN, RFC2045Encode2OUT);
35         // Strip last softline break
36         return str.substr(0, str.length - 3)
37 }
38
39
40 // Generate a random string of the specified length
41 // Useful for generating one-time-use div names
42 function randomString() {
43         return Math.random().toString(36).replace('0.','ctdl_' || '');
44 }
45
46
47 // string escape for html display
48 function escapeHTML(text) {
49         'use strict';
50         return text.replace(/[\"&<>]/g, function (a) {
51                 return {
52                         '"': '&quot;',
53                         '&': '&amp;',
54                         '<': '&lt;',
55                         '>': '&gt;'
56                 }[a];
57         });
58 }
59
60
61 // string escape for html display
62 function escapeHTMLURI(text) {
63         'use strict';
64         return text.replace(/./g, function (a) {
65                 return '%' + a.charCodeAt(0).toString(16);
66         });
67 }
68
69
70 // string escape for JavaScript string
71 //
72 function escapeJS(text) {
73         'use strict';
74         return text.replace(/[\"\']/g, function (a) {
75                 return '\\' + a ;
76         });
77 }
78
79
80 // Convert a UNIX timestamp to the browser's local time
81 // Shamelessly swiped from https://gist.github.com/kmaida/6045266
82 function convertTimestamp(timestamp) {
83         var d = new Date(timestamp * 1000),                     // Convert the passed timestamp to milliseconds
84                 yyyy = d.getFullYear(),
85                 mm = ('0' + (d.getMonth() + 1)).slice(-2),      // Months are zero based. Add leading 0.
86                 dd = ('0' + d.getDate()).slice(-2),             // Add leading 0.
87                 hh = d.getHours(),
88                 h = hh,
89                 min = ('0' + d.getMinutes()).slice(-2),         // Add leading 0.
90                 ampm = 'AM',
91                 time;
92                         
93         if (hh > 12) {
94                 h = hh - 12;
95                 ampm = 'PM';
96         }
97         else if (hh === 12) {
98                 h = 12;
99                 ampm = 'PM';
100         }
101         else if (hh == 0) {
102                 h = 12;
103         }
104         
105         // ie: 2013-02-18, 8:35 AM      
106         time = yyyy + '-' + mm + '-' + dd + ', ' + h + ':' + min + ' ' + ampm;
107                 
108         return time;
109 }
110
111
112 // Get the value of a cookie from the HTTP session
113 // Shamelessly swiped from https://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript
114 const getCookieValue = (name) => (
115         document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || ''
116 )
117
118