forked from scpwiki/admin-backup-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-backup.user.js
More file actions
985 lines (869 loc) · 29.9 KB
/
admin-backup.user.js
File metadata and controls
985 lines (869 loc) · 29.9 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/*
* Wikidot admin panel backup userscript
* Modified for SCP-CS internal use, for original script visit https://github.com/scpwiki/admin-backup-script
*
* Contact: https://www.wikidot.com/account/messages#/new/4598089
*/
// Data processing
function parseHtml(html) {
const parser = new DOMParser();
return parser.parseFromString(html, 'text/html');
}
function parseUserElement(element) {
if (element.classList.contains('deleted')) {
// 'span.printuser' with "deleted" class -> data-id user ID int
const field = element.getAttribute('data-id');
return parseInt(field);
} else {
// 'span.printuser a' element -> user ID int
const anchor = element.querySelector('a');
const regex = /WIKIDOT\.page\.listeners\.userInfo\((\d+)\)/;
const value = anchor.getAttribute('onclick');
const result = value.match(regex)[1];
return parseInt(result);
}
}
function parseDateElement(element) {
// odate element -> timestamp int
for (const klass of element.classList) {
if (klass.startsWith('time_')) {
return parseInt(klass.substring(5));
}
}
throw new Error('Unable to find timestamp in odate element');
}
function parseRating(value) {
// Special case
if (!value) {
return { enable: 'default' };
}
// Example strings:
// - draP (disabled)
// - raP (default/inherited)
// - ervS (registered, visible votes, five-star)
// - ervM (registered, visible votes, plus/minus)
// - eraM (registered, hidden votes, plus/minus)
// - emvP (site members, visible votes, plus-only)
// - emaP (site members, hidden votes, plus-only)
// Overall status
// d - disabled
// e - enabled
// If neither, then 'default'
// Always the first character
let enable;
switch (value[0]) {
case 'e':
enable = true;
break;
case 'd':
enable = false;
break;
default: // lol
enable = 'default';
}
// Eligible voters
// r - registered wikidot users
// m - site members
let eligibility;
if (value.includes('r')) {
eligibility = 'registered';
} else if (value.includes('m')) {
eligibility = 'members';
} else {
throw new Error(`Invalid vote eligibility in spec str: ${value}`);
}
// Vote visibility
// a - anonymous
// v - visible
const visibility = value.includes('v');
// Vote type
// S - five-star
// M - plus/minus
// P - plus only
let voteType;
if (value.includes('S')) {
voteType = 'fivestar';
} else if (value.includes('M')) {
voteType = 'plusminus';
} else if (value.includes('P')) {
voteType = 'plusonly';
} else {
console.warn(`Using 'plusonly' as default rating (no capital letter in '${value}')`);
voteType = 'plusonly';
}
return { enable, eligibility, visibility, voteType };
}
function parsePagePermissions(enable, value) {
// Example strings:
// - v:armo;e:m;c:m;m:m;d:m;a:m;r:m;z:m;o:rm
// - v:armo;c:;e:;m:;d:;a:;r:;z:;o:
// - v:arm;e:;c:;m:;d:;a:;r:;z:;o:
// - v:armo;c:;e:arm;m:rm;d:rm;a:m;r:o;z:o;o:
if (value === null) {
return null;
}
// Permission action:
// v - View pages
// c - Create pages
// e - Edit pages
// m - Move pages
// d - Delete pages
// a - Add files
// r - Rename files
// z - Replace, move, and delete files
// o - Show page options
function parseAction(value) {
switch (value) {
case 'v': return 'viewPages';
case 'c': return 'createPages';
case 'e': return 'editPages';
case 'm': return 'movePages';
case 'd': return 'deletePages';
case 'a': return 'uploadFiles';
case 'r': return 'renameFiles';
case 'z': return 'replaceDeleteFiles';
case 'o': return 'showPageOptions';
}
}
// User scopes:
// a - Anonymous users (no account)
// r - Registered users (has account)
// m - Site members
// o - Page creators ("owners"), regardless of the above
function parseScope(value) {
const anonymous = value.includes('a');
const registered = value.includes('r');
const members = value.includes('m');
const pageCreators = value.includes('o');
return { anonymous, registered, members, pageCreators };
}
// Parse each permission group
const permissions = { enable };
for (const group of value.split(';')) {
const [perm, scope] = group.split(':');
const action = parseAction(perm);
const options = parseScope(scope);
permissions[action] = options;
}
return permissions;
}
function parseForumPermissions(value) {
// Example strings:
// - t:;p:;e:;s:
// - t:;p:m;e:o;s:
// - t:m;p:rm;e:arm;s:
if (value === null) {
return null;
}
// Permission action:
// t - Create new threads
// p - Add posts to existing threads
// e - Edit posts (and thread metadata)
// s - Split threads (unused)
function parseAction(value) {
switch (value) {
case 't': return 'createThreads';
case 'p': return 'createPosts';
case 'e': return 'editPosts';
case 's': return undefined;
}
}
// User scopes:
// a - Anonymous users (no account)
// r - Registered users (has account)
// m - Site members
// o - Thread creators ("owners"), regardless of the above
function parseScope(value) {
const anonymous = value.includes('a');
const registered = value.includes('r');
const members = value.includes('m');
const threadCreators = value.includes('o');
return { anonymous, registered, members, threadCreators };
}
// Parse each permission group
const permissions = {};
for (const group of value.split(';')) {
const [perm, scope] = group.split(':');
const action = parseAction(perm);
const options = parseScope(scope);
if (action) {
permissions[action] = options;
}
}
return permissions;
}
async function requestModule(moduleName, params=null) {
console.debug('Making an AJAX module request', moduleName, params);
const result = await new Promise((resolve) => {
OZONE.ajax.requestModule(moduleName, params, resolve);
});
if (result['status'] !== 'ok') {
throw new Error(`${moduleName} request failed`);
}
return result;
}
async function requestModuleHtml(moduleName, params=null) {
const result = await requestModule(moduleName, params);
return parseHtml(result['body']);
}
async function requestModuleHtmlPro(moduleName, params=null) {
const result = await requestModule(moduleName, params);
const html = result['body'];
if (html.includes('http://www.wikidot.com/account/upgrade')) {
console.warn(`Module ${moduleName} yielded feature unavailable (needs paid plan)`);
return null;
}
return parseHtml(html);
}
function promptFileDownload(filename, blob) {
const link = document.createElement('a');
link.id = 'backup-download-object';
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click()
}
async function createZip(files) {
console.info('Building output ZIP');
const { BlobReader, BlobWriter, TextReader, ZipWriter } = await import('https://cdn.jsdelivr.net/npm/@zip.js/zip.js/index.js');
const zipFileWriter = new BlobWriter();
const zipWriter = new ZipWriter(zipFileWriter);
for (const file of files) {
const [filename, data] = file;
if (data instanceof Blob) {
const reader = new BlobReader(data);
await zipWriter.add(filename, reader);
} else if (typeof data === 'object') {
const json = JSON.stringify(data);
const reader = new TextReader(json);
await zipWriter.add(filename, reader);
} else {
throw new Error(`No handling for data object: ${data}`);
}
}
await zipWriter.close();
return zipFileWriter.getData();
}
// Backup tasks
async function fetchBasicInfo() {
console.info('Fetching basic site information');
// From variables
const id = WIKIREQUEST.info.siteId;
const slug = WIKIREQUEST.info.domain.replace(/\.wikidot\.com$/, '');
// ^ This is verified to always be the *.wikidot.com domain
const lang = WIKIREQUEST.info.lang;
// From the 'general module'
const html = await requestModuleHtml('managesite/ManageSiteGeneralModule');
const description = html.getElementById('site-description-field').value;
const textFields = html.querySelectorAll('.controls input');
let name, tagline, homePage, welcomePage;
switch (textFields.length) {
case 5:
// first item is the site slug, skip it
name = textFields[1].value;
tagline = textFields[2].value;
homePage = textFields[3].value;
welcomePage = textFields[4].value;
break;
case 4:
// normal distribution
name = textFields[0].value;
tagline = textFields[1].value;
homePage = textFields[2].value;
welcomePage = textFields[3].value;
break;
default:
throw new Error(`Unexpected number of text fields for general site info: ${textFields.length} (wanted 4 or 5)`);
}
return {
dumpGeneratedAt: new Date().toISOString(), // why not, might come in handy
id,
slug,
lang,
description,
name,
tagline,
homePage,
welcomePage,
};
}
async function fetchDomainSettings() {
console.info('Fetching domain settings');
const html = await requestModuleHtml('managesite/ManageSiteDomainModule');
const customDomain = html.getElementById('sm-domain-field').value || null;
const customDomainOnly = html.getElementById('sm-domain-default').checked;
const redirectElements = html.querySelectorAll('#sm-redirects-box input');
const extraDomains = [];
for (const redirectElement of redirectElements) {
if (redirectElement.value) {
extraDomains.push(redirectElement.value);
}
}
return {
customDomain,
customDomainOnly,
extraDomains,
};
}
async function fetchToolbarSettings() {
console.info('Fetching toolbar settings');
const html = await requestModuleHtml('managesite/ManageSiteToolbarsModule');
const showTop = html.getElementById('sm-show-toolbar-input1').checked;
const showBottom = html.getElementById('sm-show-toolbar-input2').checked;
const promoteSite = html.getElementById('sm-promote').checked;
return { showTop, showBottom, promoteSite };
}
async function fetchUserProfileSettings() {
console.info('Fetching user profile settings');
const html = await requestModuleHtmlPro('managesite/ManageSiteProfilePagesModule');
if (!html) {
return null;
}
const enable = html.getElementById('sm-profile-pages-form-enable').checked;
const category = html.querySelector('input[name=category]').value;
const currentTag = html.querySelector('input[name=tag_current]').value;
const formerTag = html.querySelector('input[name=tag_former]').value;
const usePopup = html.querySelector('input[name=popup]').checked;
return { enable, category, currentTag, formerTag, usePopup };
}
async function fetchCustomFooter() {
console.info('Fetching custom footer settings');
const html = await requestModuleHtmlPro('managesite/ManageSiteCustomFooterModule');
if (!html) {
return { enable: false };
}
const enable = html.getElementById('sm-use-custom-footer').checked;
const wikitext = html.getElementById('sm-cutsom-footer-input').innerText;
return { enable, wikitext };
}
async function fetchAccessPolicy() {
console.info('Fetching access policy');
const html = await requestModuleHtml('managesite/ManageSiteAccessPolicyModule');
const accessModeElement = html.querySelector('#sm-private-form input[type=radio][checked]');
const enableApplications = html.getElementById('sm-membership-apply').checked;
const autoAccept = html.getElementById('sm-membership-automatic').value;
const enablePassword = html.getElementById('sm-membership-password').checked;
const passwordValue = html.querySelector('input[name=password]').value;
const allowHotlinks = html.getElementById('sm-allow-hotlinking-checkbox').checked;
// NOTE: private site options are not being saved
const blockClonesElement = html.getElementById('sm-block-clone-checkbox');
const blockClones = blockClonesElement ? blockClonesElement.checked : false;
const blockIncludesElement = html.getElementById('sm-block-csi-checkbox');
const blockIncludes = blockIncludesElement ? blockClonesElement.checked : false;
// ^ cross-site includes
let accessMode;
switch (accessModeElement.id) {
case 'sm-access-open':
accessMode = 'open';
break;
case 'sm-access-closed':
accessMode = 'closed';
break;
case 'sm-access-private':
accessMode = 'private';
break;
default:
throw new Error(`Unknown selected access mode ID: ${accessModeElement.id}`);
}
return {
accessMode,
enableApplications,
autoAccept,
membershipPassword: {
enable: enablePassword,
value: passwordValue,
},
blockClones,
blockIncludes,
allowHotlinks,
};
}
async function fetchHttpsPolicy() {
console.info('Fetching HTTPS settings');
const html = await requestModuleHtmlPro('managesite/ManageSiteSecureAccessModule');
if (!html) {
// if no paid plan, then HTTP only
return { http: true, https: false };
}
const element = html.getElementById('sm-ssl-mode-select');
for (const option of element.children) {
// options are:
// '' - disabled
// 'ssl' - HTTP & HTTPS
// 'ssl_only' - HTTPS only
if (option.selected) {
switch (option.value) {
case '':
return { http: true, https: false };
case 'ssl':
return { http: true, https: true };
case 'ssl_only':
return { http: false, https: true };
default:
throw new Error(`Unknown value in selected option '${option.value}' for secure access mode`);
}
}
}
throw new Error("Couldn't find selected option for secure access mode");
}
async function fetchApiAccess() {
console.info('Fetching API access settings');
const html = await requestModuleHtml('managesite/ManageSiteApiModule');
const memberReadElement = html.querySelector('input[name=read-1]');
const adminReadElement = html.querySelector('input[name=read-2]');
const memberWriteElement = html.querySelector('input[name=write-1]');
const adminWriteElement = html.querySelector('input[name=write-2]');
return {
member: {
read: memberReadElement.checked,
write: memberWriteElement.checked,
},
admin: {
read: adminReadElement.checked,
write: adminWriteElement.checked,
}
};
}
async function fetchUserIconPolicy() {
console.info('Fetching user icon policy');
const html = await requestModuleHtmlPro('managesite/ManageSiteUserIconsModule');
if (!html) {
// default is to show everything
return {
avatar: true,
karma: true,
pro: true,
};
}
const element = html.querySelector('#sm-usericons-form input[checked]');
switch (element.value) {
// "Avatar, Karma, Pro icons"
case 'aks':
return {
avatar: true,
karma: true,
pro: true,
};
// "avatar, Pro icons (skip karma)"
case 'as':
return {
avatar: true,
karma: false,
pro: true,
};
// "avatar, karma (skip Pro icons)"
case 'ak':
return {
avatar: true,
karma: true,
pro: false,
};
// "only avatar"
case 'a':
return {
avatar: true,
karma: false,
pro: false,
};
// "just names, nothing graphical"
case '':
return {
avatar: false,
karma: false,
pro: false,
};
// error
default:
throw new Error(`Unexpected user icon display value: '${element.value}'`);
}
}
async function fetchBlockLinkPolicy() {
console.info('Fetching link block policy');
const html = await requestModuleHtml('managesite/abuse/ManageSiteOptionAbuseModule');
const anonymousElement = html.querySelector('input[name=blockLink]');
const karmaElement = html.querySelector('select[name=karmaLevel] option[selected]');
const blockKarmaLevel = parseInt(karmaElement.value);
if (isNaN(blockKarmaLevel)) {
throw new Error(`Invalid karma level value: ${karmaElement.value}`);
}
return {
blockAnonymous: anonymousElement.checked,
blockKarmaLevel,
};
}
async function fetchIcons() {
console.info('Fetching site icons');
const filenameRegex = /\/local--\w+\/(\w+\.\w+)\?\d+/;
async function fetchIcon(module) {
console.info(`Fetching favicon for module ${module}`);
const html = await requestModuleHtml(module);
const alreadyUploadedElement = html.querySelector('h2');
if (alreadyUploadedElement === null) {
// There is an <h2> with "You have already uploaded a favicon"
// or similar if an icon has been uploaded. So if it's absent,
// then we say no icon has been uploaded and can return null.
return null;
}
const imageElement = html.querySelector('td img');
const filename = imageElement.src.match(filenameRegex)[1];
const response = await fetch(imageElement.src);
if (response.status !== 200) {
throw new Error(`Unable to fetch image, got HTTP ${response.status}`);
}
const blob = await response.blob();
return { filename, blob };
}
return Promise.all([
fetchIcon('managesite/icons/ManageSiteFaviconModule'),
fetchIcon('managesite/icons/ManageSiteIosIconModule'),
fetchIcon('managesite/icons/ManageSiteWindowsIconModule'),
]);
}
async function fetchCategorySettings() {
console.info('Fetching category settings');
// Fetch category JSON
const result = await requestModule('managesite/ManageSiteLicenseModule');
const rawCategories = result['categories'];
console.debug('categories', rawCategories);
// License values
console.info('Fetching license data');
const html = parseHtml(result['body']);
const licenseElements = html.querySelectorAll('#sm-license-lic option');
const licenses = {};
for (const licenseElement of licenseElements) {
const licenseId = licenseElement.value;
const licenseText = licenseElement.innerText;
licenses[licenseId] = licenseText;
}
// Build category data
const categories = {};
for (const raw of rawCategories) {
categories[raw.name] = {
id: raw.categry_id,
name: raw.name,
theme: {
id: raw.theme_id,
default: raw.theme_default,
externalUrl: raw.theme_external_url,
},
layout: {
id: raw.layout_id,
default: raw.layout_default,
},
license: {
id: raw.license_id,
default: raw.license_default,
custom: raw.license_other,
name: licenses[raw.license_id],
},
perPageDiscussion: {
enable: raw.per_page_discussion,
default: raw.per_page_discussion_default,
},
nav: {
default: raw.nav_default,
topBar: raw.top_bar_page_name,
sideBar: raw.side_bar_page_name,
},
template: {
id: raw.template_id,
pageTitle: raw.page_title_template,
},
autonumerate: raw.autonumerate,
rating: parseRating(raw.rating),
permissions: parsePagePermissions(raw.permissions_default, raw.permissions),
};
}
return categories;
}
async function fetchThemesAndLayouts() {
console.info('Fetching Wikidot theme data');
const regex = /WIKIDOT\.modules\.ManageSiteCustomThemesModule\.listeners\.edit(Theme|Layout)\(event, (\d+)\)/;
const html = await requestModuleHtml('managesite/themes/ManageSiteCustomThemesModule');
const themes = [];
const layouts = [];
async function fetchTheme(themeId) {
const html = await requestModuleHtml('managesite/themes/ManageSiteEditCustomThemeModule', { themeId });
const nameElement = html.querySelector('form input[name=name]');
const extendsElement = html.querySelector('form select[name=parentTheme] option[selected]');
const layoutElement = html.querySelector('form select[name=layoutId] option[selected]');
const codeElement = html.getElementById('sm-csscode');
themes.push({
id: themeId,
name: nameElement.value,
code: codeElement.innerText,
layout: {
id: parseInt(layoutElement.value),
name: layoutElement.innerText,
},
extendsTheme: {
id: parseInt(extendsElement.value),
name: extendsElement.innerText,
},
});
}
async function fetchLayout(layoutId) {
const html = await requestModuleHtml('managesite/themes/ManageSiteEditCustomLayoutModule', { layoutId });
const nameElement = html.querySelector('input[name=layout-name]');
const codeElement = html.getElementById('sm-layoutcode');
const usesBootstrap = html.querySelector('input[name=use-bootstrap]').checked;
const bootstrapVersionElement = html.querySelector('select[name=bootstrap-version] option[selected]');
layouts.push({
id: layoutId,
name: nameElement.value,
bootstrap: usesBootstrap
? bootstrapVersionElement.value
: null,
code: codeElement.innerText,
});
}
const editButtons = html.querySelectorAll('table td a.btn-success');
for (const editButton of editButtons) {
if (editButton.classList.contains('disabled')) {
// ignore default layouts
console.warn('Ignoring default item', editButton);
continue;
}
const callback = editButton.getAttribute('onclick');
const match = callback.match(regex);
if (match === null) {
throw new Error(`No regex match for callback: ${callback}`);
}
const id = parseInt(match[2]);
switch (match[1]) {
case 'Theme':
await fetchTheme(id);
break;
case 'Layout':
await fetchLayout(id);
break;
default:
throw new Error(`Somehow got an invalid first group: ${match[1]}`);
}
}
return { themes, layouts };
}
async function fetchUserBans() {
console.info('Fetching user ban data');
const html = await requestModuleHtml('managesite/blocks/ManageSiteUserBlocksModule');
// if this element is present then there are no bans
// however! it could be present but with 'display: none'
const noBansElement = html.querySelector('div.alert');
if (noBansElement !== null && noBansElement.style.display !== 'none') {
console.warn("Found alert element in user bans, shouldn't be any bans here", noBansElement);
return [];
}
const ubans = html.querySelectorAll('table tr');
const bans = [];
// skip the first row, is header
for (let i = 1; i < ubans.length; i++) {
const uban = ubans[i];
const userElement = uban.querySelector('td span.printuser');
const dateElement = uban.querySelector('td span.odate');
const reasonElement = uban.querySelector('td[style]');
bans.push({
userId: parseUserElement(userElement),
timestamp: parseDateElement(dateElement),
reason: reasonElement.innerText.trim(),
});
}
return bans;
}
async function fetchIpBans() {
console.info('Fetching IP ban data');
const html = await requestModuleHtml('managesite/blocks/ManageSiteIpBlocksModule');
// like above, this means no bans
const noBansElement = html.querySelector('div.alert');
if (noBansElement !== null && noBansElement.style.display !== 'none') {
console.warn("Found alert element in user bans, shouldn't be any bans here", noBansElement);
return [];
}
const ibans = html.querySelectorAll('table tr');
// skip the first row, which is a header
const bans = [];
for (let i = 1; i < ibans.length; i++) {
const iban = ibans[i];
const ipElement = iban.querySelector('td');
const dateElement = iban.querySelector('td span.odate');
const reasonElement = iban.querySelector('td[style]');
bans.push({
ip: ipElement.innerText.trim(),
timestamp: parseDateElement(dateElement),
reason: reasonElement.innerText.trim(),
});
}
return bans;
}
async function fetchSiteMembers() {
console.info('Fetching site members');
async function fetchUsers(module) {
console.info(`Requesting user data from module ${module}`);
const users = [];
let page = 1;
let maxPages;
do {
console.debug(`Retrieving page ${page} of ${maxPages || '<unknown>'}`);
const html = await requestModuleHtml(module, { page });
const entries = html.querySelectorAll('table tr');
// skip the first row, is header
for (let i = 1; i < entries.length; i++) {
const entry = entries[i];
const userElement = entry.querySelector('td span.printuser');
const userId = parseUserElement(userElement);
const dateElement = entry.querySelector('td span.odate');
if (dateElement === null) {
// this must be the moderators or admins list
// just emit a list of user IDs
users.push(userId);
} else {
// this must be the regular list of members
// which includes join dates. we want this
// information so we add it to the object
const joined = parseDateElement(dateElement);
users.push({ userId, joined });
}
}
// If there's a pager, there are multiple pages, iterate through each one.
// We only try this on the first iteration, obviously.
if (page === 1) {
const pagerElement = html.querySelector('.pager');
if (pagerElement === null) {
// no more pages
break;
}
// First, get the maximum number of pages.
// The pager is laid out like this:
// [previous] [1] [2] ... [398] [399] [400] [401] [402] ... [998] [999] [next]
//
// Where the page number buttons (and number of them) differ depending on one's position.
// However, it always ends with "next", and second-to-last is the final page number.
// We can use this to get the last page number.
const buttonChildren = pagerElement.querySelectorAll('.target');
const lastButton = buttonChildren[buttonChildren.length - 2];
const lastButtonText = lastButton.innerText
maxPages = parseInt(lastButtonText);
if (isNaN(maxPages)) {
throw new Error(`Invalid value for page index: ${lastButtonText}`);
}
}
page++;
} while (page <= maxPages); // 1-indexing
return users;
}
const [members, moderators, admins] = await Promise.all([
fetchUsers('managesite/members/ManageSiteMembersListModule'),
fetchUsers('managesite/members/ManageSiteModeratorsModule'),
fetchUsers('managesite/members/ManageSiteAdminsModule'),
]);
return { members, moderators, admins };
}
async function fetchForumSettings() {
const html = await requestModuleHtml('managesite/ManageSiteForumSettingsModule');
const noForumElement = html.querySelector('div.lead');
if (noForumElement !== null) {
return null;
}
const nestingLevelSelectElemment = html.getElementById('max-nest-level');
const nestingLevelElement = nestingLevelSelectElemment.querySelector('option[selected]');
const nestingLevel = parseInt(nestingLevelElement.value);
const result = await requestModule('managesite/ManageSiteGetForumLayoutModule');
if (result.groups.length !== result.categories.length) {
throw new Error(`Forum structure mismatch: group count ${result.groups.length} != category[] count ${result.categories.lengths}`);
}
const forum = { nestingLevel, groups: [], categories: [] };
for (let i = 0; i < result.groups.length; i++) {
const { name, description, group_id, visible } = result.groups[i];
const categoryGroup = result.categories[i];
// Add forum group
forum.groups.push({
groupId: group_id,
name,
description,
visible,
});
// Add individual forum categories within that group
for (const category of categoryGroup) {
const {
name,
description,
category_id,
posts,
number_threads,
permissions,
max_nest_level,
} = category;
forum.categories.push({
categoryId: category_id,
groupId: group_id,
name,
description,
maxNestLevel: max_nest_level,
stats: {
posts,
threads: number_threads,
},
permissions: parseForumPermissions(permissions),
});
}
}
return forum;
}
// Main
async function runBackupInner(withSiteMembers) {
// Fetch data
const siteInfo = await fetchBasicInfo();
siteInfo.domains = await fetchDomainSettings();
siteInfo.toolbar = await fetchToolbarSettings();
siteInfo.userProfile = await fetchUserProfileSettings();
siteInfo.customFooter = await fetchCustomFooter();
siteInfo.access = await fetchAccessPolicy();
siteInfo.tls = await fetchHttpsPolicy();
siteInfo.api = await fetchApiAccess();
siteInfo.userIcons = await fetchUserIconPolicy();
siteInfo.blockLinks = await fetchBlockLinkPolicy();
const icons = await fetchIcons();
const categories = await fetchCategorySettings();
const { themes, layouts } = await fetchThemesAndLayouts();
const [userBans, ipBans] = await Promise.all([fetchUserBans(), fetchIpBans()]);
const forum = await fetchForumSettings();
// Build and download ZIP
const zipFiles = [
['site.json', siteInfo],
['categories.json', categories],
['themes.json', themes],
['layouts.json', layouts],
['bans.json', { user: userBans, ip: ipBans }],
];
// If site members are enabled
//
// In full backups this is always collected, but we give an option
// of excluding it in the UI since for large sites it's quite slow.
if (withSiteMembers) {
const members = await fetchSiteMembers();
zipFiles.push(['members.json', members]);
}
// Add forum is enabled
if (forum !== null) {
zipFiles.push(['forum.json', forum]);
}
// Add favicons
for (const icon of icons) {
if (icon !== null) {
zipFiles.push([icon.filename, icon.blob]);
}
}
const zipBlob = await createZip(zipFiles);
promptFileDownload(`${siteInfo.slug}.zip`, zipBlob);
/*
URL objects don't need to be revoked anymore since the browser will close immediately after finishing
URL.revokeObjectURL(zipBlob);
for (const icon of icons) {
if (icon !== null) {
URL.revokeObjectURL(icon.blob);
}
}
*/
}
// Add the function to the global context
// Selenium's driver.execute_script executes scripts as an anonymous function which is immediately called
// The defined functions wouldn't otherwise persist until the backup is started
window.runBackupInner = runBackupInner