forked from syntaxseed/dokucrypt2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
415 lines (352 loc) · 13.6 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* DOKUWIKI:include_once crypto_high-level.js */
/**
* Handles necessary actions before submitting the 'wikitext' edit form.
*/
var currentSubmitter = null;
function editFormOnSubmit(e) {
if (e && e.submitter) {
let curSub = e.submitter;
if (curSub.name.length==0) return false;
if (curSub.name.includes("cancel")) return true;
// only store the submitter if we got this far
currentSubmitter = curSub;
}
if (async_getKey_active) return false;
if(hasUnencryptedSecrets()) {
askForEncryptPasswordWithVerification();
return false;
} else {
// there is no unencrypted content, so we can prepare submitting the form, now
// Move the original wiki_text element out of the form like we used to do in decryptEditSetup().
// To prevent accidental submission of unencrypted text.
var wikitext=document.getElementById('wiki__text');
var editform=document.getElementById('dw__editform');
editform.parentNode.insertBefore(wikitext,editform);
// Now, get the wikitext content to our hidden field
var hiddentext=document.getElementById('wiki__text_submit');
hiddentext.value=wikitext.value;
return true;
}
}
/**
* Setup the edit form: add the decrypt button and necessary functionality.
*/
function decryptEditSetup(msg) {
//alert('setting up');
var editform=null, wikitext=null, hiddentext=null, preview=null;
if(!(editform=document.getElementById('dw__editform'))) {
// alert("no form dw__editform\n");
return(true);
}
if(!(wikitext=document.getElementById('wiki__text'))) {
// alert("no wiki__text");
return(false);
}
// if there is no preview button, then assume this is a
// "Recover draft" page, dont do anything.
if(!(preview=document.getElementById('edbtn__preview'))) {
return(false);
}
if(!(save=document.getElementById('edbtn__save'))) {
return(false);
}
// Create a hidden element with id 'wiki__text_submit' and
// name wikitext (same as the wiki__text).
if(!(hiddentext=document.createElement('input'))) {
return(false);
}
hiddentext.setAttribute('id', 'wiki__text_submit');
hiddentext.setAttribute('name', 'wikitext');
hiddentext.setAttribute('type','hidden');
editform.insertBefore(hiddentext,null);
if(!(decryptButton=document.createElement('input'))) {
return(false);
}
decryptButton.setAttribute('id', 'decryptButton');
decryptButton.setAttribute('name', 'decryptButton');
decryptButton.setAttribute('type','Button');
decryptButton.setAttribute('value','DecryptSecret');
decryptButton.onclick=decryptButtonOnClick;
decryptButton.setAttribute('class','button');
decryptButton.setAttribute('className','button'); // required for IE
preview.parentNode.insertBefore(decryptButton,preview);
editform.onsubmit = function() {return editFormOnSubmit(event);};
// The following is taken from lib/scripts/locktimer.js (state of 2018-06-08) to make drafts work.
// We override the locktimer refresh function to abort saving of drafts with unencrypted content.
dw_locktimer.refresh = function(){
var now = new Date(),
params = 'call=lock&id=' + dw_locktimer.pageid + '&';
// refresh every half minute only
if(now.getTime() - dw_locktimer.lasttime.getTime() <= 30*1000) {
return;
}
// POST everything necessary for draft saving
if(dw_locktimer.draft && jQuery('#dw__editform').find('textarea[name=wikitext]').length > 0){
// *** BEGIN dokucrypt modified code
// Do not allow saving of a draft, if this page needs some content to be encrypted on save.
// Basically abort saving of drafts if this page has some content that needs encrypting.
if (hasUnencryptedSecrets()) { return(false); }
// *** END dokucrypt modified code
params += jQuery('#dw__editform').find(dw_locktimer.fieldsToSaveAsDraft.join(', ')).serialize();
}
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
params,
dw_locktimer.refreshed,
'json'
);
dw_locktimer.lasttime = now;
};
}
/**
* Checks of there are <SECRET> blocks in the wikitext by trying to encrypt a given text
* with <SECRET>s contained. Works and returns synchronously.
*
* @param string x The text to be encrypted (usually that's the content of the
* textfield containing the wiki pages text source).
*
* @return boolean true, if there are <SECRET> blocks contained. Otherwise false.
*/
function hasUnencryptedSecrets() {
var wikitext=null, hiddentext=null;
if(!(wikitext=document.getElementById('wiki__text'))) {
alert("failed to get wiki__text");
return(false);
}
if (wikitext.value.includes("<" + tag_pt))
return true;
else
return false;
}
/**
* Adds an input dialog to the edit page to ask the user for the encryption password. Works with callbacks and therefore represents an asynchronous workflow.
*/
function askForEncryptPasswordWithVerification() {
var wikitext = document.getElementById('wiki__text');
var hiddentext=document.getElementById('wiki__text_submit');
lock = "default";
// callback manages what to do and where to insert the decrypted text to
// call pw_prompt and let the callback call the next pw_prompt for input verification (repeat passwort)
do_verification = function(key) {
do_encryption = function(key2) {
if (key != key2) {
alert("Die Passwörter stimmen nicht überein!");
return;
}
// important: cache the key first, then try to do the encryption!
setKeyForLock(lock,key);
var encrypted_text = encryptMixedText(wikitext.value);
if (encrypted_text) {
wikitext.value=encrypted_text;
hiddentext.value=encrypted_text;
// retry submit
currentSubmitter.click();
} else {
setKeyForLock(lock,null);
alert("Der Text konnte nicht verschlüsselt werden!");
currentSubmitter = null;
}
};
pw_prompt({
lm:"Bitte Kennwort erneut eingeben", // "Enter passphrase for lock " + lock);
elem:wikitext,
submit_callback:do_encryption
});
};
pw_prompt({
lm:"Bitte Kennwort eingeben", // "Enter passphrase for lock " + lock);
elem:wikitext,
submit_callback:do_verification
});
}
function askForDecryptPassword() {
var wikitext = document.getElementById('wiki__text');
var hiddentext=document.getElementById('wiki__text_submit');
lock = "default";
// callback manages what to do and where to insert the decrypted text to
do_decryption = function(key) {
// important: cache the key first, then try to do the decryption!
setKeyForLock(lock,key);
var decrypted_text = decryptMixedText(wikitext.value);
if (decrypted_text) {
wikitext.value=decrypted_text;
hiddentext.value=decrypted_text;
} else {
setKeyForLock(lock,null);
alert("Der Text konnte nicht entschlüsselt werden!");
}
};
pw_prompt({
lm:"Bitte Kennwort eingeben", // "Enter passphrase for lock " + lock);
elem:wikitext,
submit_callback:do_decryption
});
}
/**
* Handles the actions after clicking the Decrypt button in the edit form. Tries to
* decrypt any <ENCRYPTED> blocks.
*/
function decryptButtonOnClick() {
askForDecryptPassword();
return(true);
}
function toggleElemVisibility(elemid) {
elem=document.getElementById(elemid);
if(elem.style.visibility=="visible") {
elem.style.visibility="hidden";
elem.style.position="absolute";
} else {
elem.style.visibility="visible";
elem.style.position="relative";
}
}
/*
this is called from <A HREF=> links to decrypt the inline html
*/
function toggleCryptDiv(elemid,lock,ctext) {
var elem=null, atab=null, ptext="";
var ctStr="anzeigen", ptStr="verstecken";
elem=document.getElementById(elemid);
atag=document.getElementById(elemid + "_atag");
if(elem===null || atag===null) {
alert("failed to find element id " + elemid);
}
if(atag.innerHTML==ptStr) {
// encrypt text (set back to ctext, and forget key)
elem.innerHTML=ctext;
atag.innerHTML=ctStr;
elem.style.visibility="hidden";
elem.style.position="absolute";
setKeyForLock(lock,undefined);
} else if (atag.innerHTML==ctStr) {
// decrypt text
// callback manages what to do and where to insert the decrypted text to
do_decryption = function(given_key) {
//try the decryption
if(!(ptext=decryptTextString(ctext,given_key))) {
alert("Kein passendes Kennwort eingegeben");
return;
}
elem.innerHTML=ptext;
atag.innerHTML=ptStr;
// make it visible
elem.style.visibility="visible";
elem.style.position="relative";
//store the key that was used
setKeyForLock(lock,given_key);
if (JSINFO["plugin_dokucrypt2_CONFIG_copytoclipboard"] == 1) {
//put it into the clipboard
copyToClipboard(ptext).then(() => {
if (JSINFO['plugin_dokucrypt2_CONFIG_hidepasswordoncopytoclipboard']) {
elem.innerHTML = "{" + JSINFO['plugin_dokucrypt2_TEXT_copied_to_clipboard'] + "}";
} else {
elem.innerHTML += " {" + JSINFO['plugin_dokucrypt2_TEXT_copied_to_clipboard'] + "}";
};
console.log('Das Passwort wurde in die Zwischenablage kopiert.');
}).catch(() => {
console.log('Das Passwort konnte nicht in die Zwischenablage kopiert.');
});
}
};
// now test if there is a key cached for the given lock - if no key can be determined, show password prompt
var key = getKeyForLock(lock);
if(key===false || key===undefined || key === null || !decryptTextString(ctext,key)) {
pw_prompt({
lm:"Bitte Kennwort eingeben", // "Enter passphrase for lock " + lock);
lock:lock,
elem:elem,
submit_callback:do_decryption
});
} else {
do_decryption(key);
}
} else { alert("Broken"); return; }
}
//copy to clipboard from: https://stackoverflow.com/questions/51805395/navigator-clipboard-is-undefined
function copyToClipboard(textToCopy) {
// navigator clipboard api needs a secure context (https)
if (navigator.clipboard && window.isSecureContext) {
// navigator clipboard api method'
return navigator.clipboard.writeText(textToCopy);
} else {
// text area method
let textArea = document.createElement("textarea");
textArea.value = textToCopy;
// make the textarea out of viewport
textArea.style.position = "fixed";
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
return new Promise((res, rej) => {
// here the magic happens
document.execCommand('copy') ? res() : rej();
textArea.remove();
});
}
}
// protected password prompt adapted from: https://stackoverflow.com/a/28461750/19144619
var promptElem = null;
var label = null;
var input = null;
var submit_button = null;
var cancel_button = null;
var submit_event = null;
var cancel_event = null;
var enter_event = null;
var async_getKey_active = false; // tracks whether the user is currently being asked for a key
window.pw_prompt = function(options) {
var lm = options.lm || "Passwort:",
bm = options.bm || "OK",
cm = options.cm || "Abbrechen",
elem = options.elem || document.body,
submit_callback = options.submit_callback;
if(!submit_callback) { // callback manages what to do and where to insert the decrypted text to
alert("No callback function for submitting provided! Please provide one - it should handle the actions after submitting the pw_prompt.")
};
if (promptElem == null) {
promptElem = document.createElement("div");
promptElem.className = "dokucrypt2pw_prompt";
label = document.createElement("label");
label.textContent = lm;
label.for = "pw_prompt_input";
promptElem.appendChild(label);
input = document.createElement("input");
input.id = "pw_prompt_input";
input.type = "password";
promptElem.appendChild(input);
submit_button = document.createElement("button");
promptElem.appendChild(submit_button);
cancel_button = document.createElement("button");
promptElem.appendChild(cancel_button);
} else {
//remove event listeners
submit_button.removeEventListener("click", submit_event);
cancel_button.removeEventListener("click", cancel_event);
}
submit_event = function() {
if (promptElem.parentNode)
promptElem.parentNode.removeChild(promptElem);
async_getKey_active = false;
submit_callback(input.value);
};
cancel_event = function() {
if (promptElem.parentNode)
promptElem.parentNode.removeChild(promptElem);
async_getKey_active = false;
};
label.textContent = lm;
input.value = "";
submit_button.textContent = bm;
submit_button.addEventListener("click", submit_event, false);
cancel_button.textContent = cm;
cancel_button.addEventListener("click", cancel_event, false);
if(elem.nextSibling){
elem.parentNode.insertBefore(promptElem,elem.nextSibling);
} else {
elem.parentNode.appendChild(promptElem);
}
async_getKey_active = true;
input.focus();
};