201247450aa720bd6803710bcfb424ce42046597
[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.  It runs great on the
5 // Linux operating system (and probably elsewhere).  You can use,
6 // copy, and run it under the terms of the GNU General Public
7 // License version 3.  Richard Stallman is an asshole communist.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14
15 // Function to encode data in quoted-printable format
16 // Written by Theriault and Brett Zamir [https://locutus.io/php/quoted_printable_encode/]
17 function quoted_printable_encode(str) {
18         const hexChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
19         const RFC2045Encode1IN = / \r\n|\r\n|[^!-<>-~ ]/gm
20         const RFC2045Encode1OUT = function (sMatch) {
21                 // Encode space before CRLF sequence to prevent spaces from being stripped
22                 // Keep hard line breaks intact; CRLF sequences
23                 if (sMatch.length > 1) {
24                         return sMatch.replace(' ', '=20');
25                 }
26                 // Encode matching character
27                 const chr = sMatch.charCodeAt(0);
28                 return '=' + hexChars[((chr >>> 4) & 15)] + hexChars[(chr & 15)];
29         }
30         // Split lines to 75 characters; the reason it's 75 and not 76 is because softline breaks are
31         // preceeded by an equal sign; which would be the 76th character. However, if the last line/string
32         // was exactly 76 characters, then a softline would not be needed. PHP currently softbreaks
33         // anyway; so this function replicates PHP.
34         const RFC2045Encode2IN = /.{1,72}(?!\r\n)[^=]{0,3}/g
35         const RFC2045Encode2OUT = function (sMatch) {
36                 if (sMatch.substr(sMatch.length - 2) === '\r\n') {
37                         return sMatch;
38                 }
39                 return sMatch + '=\r\n';
40         }
41         str = str.replace(RFC2045Encode1IN, RFC2045Encode1OUT).replace(RFC2045Encode2IN, RFC2045Encode2OUT);
42         // Strip last softline break
43         return str.substr(0, str.length - 3)
44 }
45
46
47 // Generate a random string of the specified length
48 // Useful for generating one-time-use div names
49 function randomString() {
50         return Math.random().toString(36).replace('0.','ctdl_' || '');
51 }
52
53
54 // string escape for html display
55 function escapeHTML(text) {
56         'use strict';
57         return text.replace(/[\"&<>]/g, function (a) {
58                 return {
59                         '"': '&quot;',
60                         '&': '&amp;',
61                         '<': '&lt;',
62                         '>': '&gt;'
63                 }[a];
64         });
65 }
66
67
68 // string escape for html display
69 function escapeHTMLURI(text) {
70         'use strict';
71         return text.replace(/./g, function (a) {
72                 return '%' + a.charCodeAt(0).toString(16);
73         });
74 }
75
76
77 // string escape for JavaScript string
78 //
79 function escapeJS(text) {
80         'use strict';
81         return text.replace(/[\"\']/g, function (a) {
82                 return '\\' + a ;
83         });
84 }
85
86
87 // Convert a UNIX timestamp to the browser's local time
88 // Shamelessly swiped from https://gist.github.com/kmaida/6045266
89 function convertTimestamp(timestamp) {
90         var d = new Date(timestamp * 1000),                     // Convert the passed timestamp to milliseconds
91                 yyyy = d.getFullYear(),
92                 mm = ('0' + (d.getMonth() + 1)).slice(-2),      // Months are zero based. Add leading 0.
93                 dd = ('0' + d.getDate()).slice(-2),             // Add leading 0.
94                 hh = d.getHours(),
95                 h = hh,
96                 min = ('0' + d.getMinutes()).slice(-2),         // Add leading 0.
97                 ampm = 'AM',
98                 time;
99                         
100         if (hh > 12) {
101                 h = hh - 12;
102                 ampm = 'PM';
103         }
104         else if (hh === 12) {
105                 h = 12;
106                 ampm = 'PM';
107         }
108         else if (hh == 0) {
109                 h = 12;
110         }
111         
112         // ie: 2013-02-18, 8:35 AM      
113         time = yyyy + '-' + mm + '-' + dd + ', ' + h + ':' + min + ' ' + ampm;
114                 
115         return time;
116 }