forked from 8l4ckSh33p/passwords
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passwords.js
executable file
·3648 lines (3246 loc) · 124 KB
/
passwords.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
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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(OC, window, $, undefined) {
'use strict';
$(document).ready(function() {
$(document.body).click(function (e){
var $command_box = $('#commands_popup');
if (!$command_box.has(e.target).length) { // if the click was not within $command_box
//$command_box.hide();
$command_box.slideUp(100);
$('.activerow').removeClass('activerow');
}
});
$('#settingsbtn').text(t('core', 'Settings'));
// this passwords object holds all our passwords
var Passwords = function(baseUrl) {
this._baseUrl = baseUrl;
this._passwords = [];
this._activePassword = undefined;
};
Passwords.prototype = {
load: function(id) {
var self = this;
this._passwords.forEach(function(password) {
if (password.id === id) {
password.active = true;
self._activePassword = password;
} else {
password.active = false;
}
});
},
getActive: function() {
return this._activePassword;
},
removeActive: function() {
var index;
var deferred = $.Deferred();
var id = this._activePassword.id;
this._passwords.forEach(function(password, counter) {
if (password.id === id) {
index = counter;
}
});
if (index !== undefined) {
// delete cached active password if necessary
if (this._activePassword === this._passwords[index]) {
delete this._activePassword;
}
this._passwords.splice(index, 1);
$.ajax({
url: this._baseUrl + '/' + id,
method: 'DELETE'
}).done(function() {
deferred.resolve();
}).fail(function() {
deferred.reject();
});
} else {
deferred.reject();
}
return deferred.promise();
},
removeByID: function(id) {
var index = id;
var deferred = $.Deferred();
if (index !== undefined) {
// delete cached active password if necessary
if (this._activePassword === this._passwords[index]) {
delete this._activePassword;
}
this._passwords.splice(index, 1);
$.ajax({
url: this._baseUrl + '/' + id,
method: 'DELETE'
}).done(function() {
deferred.resolve();
}).fail(function() {
deferred.reject();
});
} else {
deferred.reject();
}
return deferred.promise();
},
create: function(password) {
var deferred = $.Deferred();
$.ajax({
url: this._baseUrl,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(password)
}).done(function(password) {
deferred.resolve();
}).fail(function() {
deferred.reject();
});
return deferred.promise();
},
sendmail: function(kind, website, sharewith, domain, fullurl, instancename) {
var sharewithArr = [];
if ($.isArray(sharewith)) {
sharewithArr = sharewith;
} else if (!sharewith) {
sharewithArr = "";
} else {
sharewithArr = sharewith.split(', ');
}
// this._baseUrl ends with /passwords as this is in its Prototype, not /mail as it should be (in routes.php)
// so use generateUrl('/mail')
var result = false;
var request = $.ajax({
url: generateUrl('/mail'),
data: {
'kind' : kind,
'website' : website,
'sharewith' : sharewithArr,
'domain' : domain,
'fullurl' : fullurl,
'instancename' : instancename
},
method: 'POST',
async: false
});
request.done(function(msg) {
// will be true or false;
result = msg;
});
request.fail(function( jqXHR, textStatus ) {
//alert( "Error while authenticating: " + textStatus );
});
return result;
},
getAll: function() {
return this._passwords;
},
loadAll: function() {
var deferred = $.Deferred();
var self = this;
$.get(this._baseUrl).done(function(passwords) {
self._activePassword = undefined;
self._passwords = passwords;
deferred.resolve();
}).fail(function() {
deferred.reject();
});
return deferred.promise();
},
updateActive: function(index, loginname, website, address, pass, notes, sharewith, category, deleted, changedDate) {
var sharewithArr = [];
if ($.isArray(sharewith)) {
sharewithArr = sharewith;
} else if (!sharewith) {
sharewithArr = "";
} else {
sharewithArr = sharewith.split(', ');
}
if (changedDate == undefined) {
// this needs to stay here for users who are updating from <= v16.2; creation dates are used as source
var d = new Date();
// date as YYYY-MM-DD
var changedDate = d.getFullYear()
+ '-' + ('0' + (d.getMonth() + 1)).slice(-2)
+ '-' + ('0' + d.getDate()).slice(-2);
}
if (!pass) {
pass = ' ';
}
var password = {
'id' : index,
'website': website,
'pass': pass,
'loginname': loginname,
'address': address,
'category': category,
'notes': notes,
'sharewith' : sharewithArr,
'deleted': deleted,
'datechanged' : changedDate
};
return $.ajax({
url: this._baseUrl + '/' + password.id,
method: 'PUT',
contentType: 'application/json',
data: JSON.stringify(password)
});
}
};
// this categories object holds all our categories
var Categories = function(baseUrl) {
this._baseUrl = baseUrl;
this._categories = [];
};
Categories.prototype = {
load: function(id) {
var self = this;
this._categories.forEach(function(category) {
if (category.id === id) {
category.active = true;
self._activecategory = category;
} else {
category.active = false;
}
});
},
removeByID: function(id) {
var index = id;
var deferred = $.Deferred();
if (index !== undefined) {
this._categories.splice(index, 1);
$.ajax({
url: this._baseUrl + '/' + id,
method: 'DELETE'
}).done(function() {
deferred.resolve();
}).fail(function() {
deferred.reject();
});
} else {
deferred.reject();
}
return deferred.promise();
},
create: function(category) {
var deferred = $.Deferred();
$.ajax({
url: this._baseUrl,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(category)
}).done(function(category) {
deferred.resolve();
}).fail(function() {
deferred.reject();
});
return deferred.promise();
},
getAll: function() {
return this._categories;
},
loadAll: function() {
var deferred = $.Deferred();
var self = this;
$.get(this._baseUrl).done(function(categories) {
self._categories = categories;
deferred.resolve();
}).fail(function() {
deferred.reject();
});
return deferred.promise();
}
};
// this holds our settings
var Settings = function(baseUrl) {
this._baseUrl = baseUrl;
this._settings = [];
};
Settings.prototype = {
load: function() {
var deferred = $.Deferred();
var self = this;
$.ajax({
url: this._baseUrl,
method: 'GET',
async: false
}).done(function( settings ) {
self._settings = settings;
}).fail(function() {
deferred.reject();
});
return deferred.promise();
},
setUserKey: function(key, value) {
var deferred = $.Deferred();
$.ajax({
url: this._baseUrl + '/' + key + '/' + value,
method: 'POST'
}).done(function( data ) {
deferred.resolve(data);
}).fail(function() {
alert(t('passwords', 'Error while saving field') + ' ' + key + '!');
deferred.reject();
});
},
getKey: function(key) {
for (var k in this._settings)
{
if (k == key)
return this._settings[k];
}
},
getAll: function() {
return this._settings;
}
};
// this will be the view that is used to update the html
var View = function(passwords) {
this._passwords = passwords;
};
View.prototype = {
renderContent: function() {
var source_passwords = $('#template-passwords-old').html();
var template_passwords = Handlebars.compile(source_passwords);
var html_passwords = template_passwords({
passwords: this._passwords.getAll()
});
$('#PasswordsTableTestOld').html(html_passwords);
// check for legacy versions, where loginname and notes (and so forth) weren't included in the db properties column yet
// prior to v17
var table = document.getElementById('PasswordsTableTestOld');
if (table) {
for (var i = 0; i < table.rows.length; i++) {
resetTimer(true);
// test for login names (= [1]), should not exist since they're serialized in properties column
// but if they do, website (= [0]) must be filled too, gives error on >= v18 otherwise
if (table.rows[i].cells[0].textContent != '' && table.rows[i].cells[1].textContent != '') {
var updateReq = true;
}
}
if (updateReq) {
$('#update_start_btn').click(function() {
updateStart(passwords);
});
updateRequired();
return false;
} else {
$('#PasswordsTableTestOld').hide();
// following doesn't work on IE7
$('#PasswordsTableTestOld').remove();
}
}
// building new table
source_passwords = $('#template-passwords-serialize').html();
template_passwords = Handlebars.compile(source_passwords);
var html_passwords_serialize = template_passwords({
passwords: this._passwords.getAll()
});
// decode HTML: convert (") to (") and (&) to (&) and so forth
html_passwords_serialize = $('<textarea/>').html(html_passwords_serialize).text();
var rows = html_passwords_serialize.split('<br>');
formatTable(false, rows);
$('tr').click(function(event) {
$('tr').removeClass('activerow');
});
$('.btn_commands_open').click(function(event) {
event.stopPropagation(); // or else this will hide the box
$('tr').removeClass('activerow');
if ($('#commands_popup').css('display') == 'block') {
$('#commands_popup').slideUp(150);
return false;
}
var $row = $(this).closest('tr');
var $cell = $(this).closest('td');
$row.addClass('activerow');
$('#commands_popup').hide();
// set values
$('#cmd_id').val($row.attr('attr_id'));
$('#cmd_type').val($cell.attr('type'));
$('#cmd_value').val($row.attr('attr_' + $cell.attr('type')));
$('#cmd_website').val($row.attr('attr_website'));
$('#cmd_address').val($row.attr('attr_address'));
$('#cmd_loginname').val($row.attr('attr_loginname'));
$('#cmd_pass').val($row.attr('attr_pass'));
$('#cmd_notes').val($row.attr('attr_notes'));
$('#cmd_sharedwith').val($row.attr('attr_sharedwith'));
$('#cmd_category').val($row.attr('attr_category'));
$('#cmd_deleted').val($row.hasClass('is_deleted'));
if ($row.hasClass('is_sharedby')) {
$('#btn_edit').hide();
$('#btn_share').hide();
$('#btn_view').show();
$('#commands_popup input').css('width', '170px');
} else {
$('#btn_edit').show();
$('#btn_share').show();
$('#btn_view').hide();
$('#commands_popup input').css('width', '80px');
}
$('#btn_copy').attr('data-clipboard-text', $('#cmd_value').val());
if ($('#app-navigation').css('position') == 'absolute') {
var left = $(this).position().left;
} else {
var left = $(this).position().left + $('#app-navigation').width();
}
left = left + $('.btn_commands_open').width() - ($('#commands_popup').width() / 2);
var top = $(this).position().top + $('#header').height() + 25;
$('#commands_popup').css('left', left + 'px');
$('#commands_popup').css('top', top + 'px');
$('#commands_popup').slideDown(150);
});
// colour picker from the great https://github.com/bgrins/spectrum
$("#colorpicker").spectrum({
color: '#eeeeee',
showInput: true,
showButtons: false,
showInitial: false,
allowEmpty: false,
showAlpha: false,
showPalette: true,
showPaletteOnly: false,
togglePaletteOnly: false,
showSelectionPalette: false,
clickoutFiresChange: true,
hideAfterPaletteSelect: true,
containerClassName: 'category_colorpicker',
replacerClassName: 'category_colorpicker',
preferredFormat: 'hex',
palette: [
["#000000","#444444","#666666","#999999","#cccccc","#eeeeee","#f3f3f3","#ffffff"],
["#ff0000","#ff9900","#ffff00","#00ff00","#00ffff","#0000ff","#9900ff","#ff00ff"],
["#f4cccc","#fce5cd","#fff2cc","#d9ead3","#d0e0e3","#cfe2f3","#d9d2e9","#ead1dc"],
["#ea9999","#f9cb9c","#ffe599","#b6d7a8","#a2c4c9","#9fc5e8","#b4a7d6","#d5a6bd"],
["#e06666","#f6b26b","#ffd966","#93c47d","#76a5af","#6fa8dc","#8e7cc3","#c27ba0"],
["#cc0000","#e69138","#f1c232","#6aa84f","#45818e","#3d85c6","#674ea7","#a64d79"],
["#990000","#b45f06","#bf9000","#38761d","#134f5c","#0b5394","#351c75","#741b47"],
["#660000","#783f04","#7f6000","#274e13","#0c343d","#073763","#20124d","#4c1130"]
],
move: function(color) {
$('#cat_colour').val(color.toHexString());
},
change: function(color) {
$('#cat_colour').val(color.toHexString());
},
hide: function(color) {
$('#cat_colour').val(color.toHexString());
}
});
$('#btn_copy').click(function() {
var clipboard = new Clipboard('#btn_copy');
clipboard.on('success', function(e) {
$('#zeroclipboard_copied').slideDown(50);
setTimeout(function() {
$('#zeroclipboard_copied').slideUp(100);
}, 1500);
e.clearSelection();
});
clipboard.on('error', function(e) {
var typeTitle = '';
switch ($('#cmd_type').val()) {
case 'website':
typeTitle = t('passwords', 'Website or company');
break;
case 'loginname':
typeTitle = t('passwords', 'Login name');
break;
case 'pass':
typeTitle = t('passwords', 'Password');
break;
}
$('#commands_popup').slideUp(100);
$('.activerow').removeClass('activerow');
setTimeout(function() {
window.prompt(typeTitle + ':', $('#cmd_value').val());
e.stopPropagation;
}, 100);
});
});
$('#btn_invalid_sharekey').click(function() {
OCdialogs.alert(t('passwords', 'You do not have a valid share key, to decrypt this password. Ask the user that shared this password with you, to reshare it.'), t('passwords', 'Invalid share key'), function() { return false; }, true);
});
$('#btn_view').click(function() {
var typeVar = '';
var typeTitle = '';
switch ($('#cmd_type').val()) {
case 'website':
typeVar = 'website';
typeTitle = t('passwords', 'Website or company');
break;
case 'loginname':
typeVar = 'loginname';
typeTitle = t('passwords', 'Login name');
break;
case 'pass':
typeVar = 'password';
typeTitle = t('passwords', 'Password');
break;
}
popUp(typeTitle, $('#cmd_value').val(), 'view', $('#cmd_address').val(), $('#cmd_website').val(), $('#cmd_loginname').val());
});
$('#btn_edit').click(function() {
var typeVar = '';
var typeTitle = '';
switch ($('#cmd_type').val()) {
case 'website':
typeVar = 'website';
typeTitle = t('passwords', 'Website or company');
break;
case 'loginname':
typeVar = 'loginname';
typeTitle = t('passwords', 'Login name');
break;
case 'pass':
typeVar = 'password';
typeTitle = t('passwords', 'Password');
break;
}
popUp(typeTitle, $('#cmd_value').val(), typeVar, $('#cmd_address').val(), $('#cmd_website').val(), $('#cmd_loginname').val());
$('#accept').click(function() {
var newvalue = $('#new_value_popup').val();
if (typeVar == 'website') {
if ($('#new_address_popup').val() != ''
&& $('#new_address_popup').val().substring(0,7).toLowerCase() != 'http://'
&& $('#new_address_popup').val().substring(0,8).toLowerCase() != 'https://'
&& $('#new_address_popup').val().substring(0,4).toLowerCase() != 'www.')
{
if (isUrl($('#new_address_popup').val())) {
// valid URL, so add http
$('#new_address_popup').val('http://' + $('#new_address_popup').val());
// now check if valid
if (!isUrl($('#new_address_popup').val())) {
$('#popupInvalid').show();
$('#new_address_popup').select();
return false;
}
} else {
$('#popupInvalid').show();
$('#new_address_popup').select();
return false;
}
}
var newaddress = $('#new_address_popup').val();
}
var passwords = new Passwords(generateUrl('/passwords'));
if ($('#keep_old_popup').prop('checked') == true) {
// save row to trash bin first
var pass_old = $('#cmd_pass').val();
var password = {
'website': $('#cmd_website').val(),
'pass': pass_old,
'loginname': $('#cmd_loginname').val(),
'address': $('#cmd_address').val(),
'category': $('#cmd_category').val(),
'notes': $('#cmd_notes').val(),
'deleted': '1'
};
passwords.create(password).done(function() {
var passwords2 = new Passwords(generateUrl('/passwords'));
var view = new View(passwords2);
passwords2.loadAll().done(function() {
view.renderContent();
});
}).fail(function() {
OCdialogs.alert(t('passwords', 'Error: Could not create password.'), t('passwords', 'Save'), function() { return false; }, true);
return false;
});
}
// overwrite proper field(s) with new value(s)
switch (typeVar) {
case 'website':
$('#cmd_website').val(newvalue);
$('#cmd_address').val(newaddress);
break;
case 'loginname':
$('#cmd_loginname').val(newvalue);
break;
case 'password':
$('#cmd_pass').val(newvalue);
break;
}
var success = passwords.updateActive($('#cmd_id').val(), $('#cmd_loginname').val(), $('#cmd_website').val(), $('#cmd_address').val(), $('#cmd_pass').val(), $('#cmd_notes').val(), $('#cmd_sharedwith').val(), $('#cmd_category').val(), $('#cmd_deleted').val());
if (success) {
var passwords = new Passwords(generateUrl('/passwords'));
var view = new View(passwords);
passwords.loadAll().done(function() {
view.renderContent();
})
// building new table
var source_passwords = $('#template-passwords-serialize').html();
var template_passwords = Handlebars.compile(source_passwords);
var html_passwords_serialize = template_passwords({
passwords: passwords.getAll()
});
html_passwords_serialize = html_passwords_serialize.replace(/"/g, '"');
var rows = html_passwords_serialize.split('<br>');
formatTable(false, rows);
} else {
OCdialogs.alert(t('passwords', 'Error: Could not update password.'), t('passwords', 'Save'), function() { return false; }, true);
}
removePopup();
});
return false;
});
$('#btn_share').click(function() {
var id = $('#cmd_id').val();
var row = findRow(id);
$(row).find('.icon-share').click();
});
$('#btn_clone').click(function() {
$('#new_address').attr('value', $('#cmd_address').val());
$('#new_notes').text($('#cmd_notes').val());
$('#new_website').attr('value', $('#cmd_website').val());
$('#new_username').attr('value', $('#cmd_loginname').val());
$('#new_password').attr('value', $('#cmd_pass').val());
webimg2new();
strength_str($('#cmd_pass').val(), false);
$('#add_new').click();
});
$('#app-settings-content').hide();
// reset timer on hover (for touch screens)
$('#idleTimer').mouseenter(function(event) {
resetTimer(true);
});
$('#countSec').mouseenter(function(event) {
resetTimer(true);
});
$('#back_to_passwords').click(function() {
$('#section_table').show(200);
$('#section_categories').hide(100);
});
$("#CategoriesTableContent").on("click", "td", function() {
var $cell = $(this);
var $row = $cell.closest('tr');
var rows = $row.closest('table').find('tr').length;
var cat_id = $row.find('.catTable_id').text();
var cat_name = $row.find('.catTable_name').text();
var is_trash = $cell.hasClass('icon-delete');
if (is_trash) {
OCdialogs.confirm(t('passwords', 'This will delete the category') + " '" + cat_name + "'. " + t('passwords', 'Are you sure?'), t('passwords', 'Category'), function(res) {
if (res) {
var categories = new Passwords(generateUrl('/categories'));
categories.removeByID(cat_id).done(function() {
setTimeout(function() {
categories = new Categories(generateUrl('/categories'));
categories.loadAll().done(function() {
renderCategories(categories);
}).fail(function() {
OCdialogs.alert(t('passwords', 'Error: Could not load categories.'), t('passwords', 'Passwords'), function() { return false; }, true);
});
}, 500);
}).fail(function() {
OCdialogs.alert(t('passwords', 'Error: Could not delete category.'), t('passwords', 'Category'), function() { return false; }, true);
});
}
});
}
});
$('#cat_add').click(function() {
if ($('#cat_name').val().length == 0) {
return false;
}
var categories = new Categories(generateUrl('/categories'));
var cat_name = $('#cat_name').val();
var cat_colour = $('#cat_colour').val();
cat_colour = cat_colour.replace('#', '');
var category = {
categoryName: cat_name,
categoryColour: cat_colour
};
var success = categories.create(category);
if (success) {
$('#cat_name').val('');
$('#cat_colour').val('#eeeeee');
$('#colorpicker').spectrum('set', 'eeeeee');
setTimeout(function() {
categories = new Categories(generateUrl('/categories'));
categories.loadAll().done(function() {
renderCategories(categories);
}).fail(function() {
OCdialogs.alert(t('passwords', 'Error: Could not load categories.'), t('passwords', 'Passwords'), function() { return false; }, true);
});
}, 500);
} else {
OCdialogs.alert(t('passwords', 'Error: Could not create category.'), t('passwords', 'Categories'), function() { return false; }, true);
}
});
$('#sidebarClose').click(function() {
$('#sidebarRow').val('');
$('#app-content-wrapper').attr('class', '');
$('#app-sidebar-wrapper').hide(100);
});
$('#delete_trashbin').click(function(event) {
OCdialogs.confirm(t('passwords', 'This will permanently delete all passwords in this trash bin.') + ' ' + t('passwords', 'Are you sure?'), t('passwords', 'Trash bin'),
function(confirmed) {
if (confirmed) {
$('#PasswordsTableContent tr').each(function() {
var $row = $(this);
var is_deleted = $row.hasClass('is_deleted');
var id = $row.attr('attr_id');
var passwords = new Passwords(generateUrl('/passwords'));
if (is_deleted) {
passwords.removeByID(id).done(function() {
$row.remove();
formatTable(true); // reset counters and show 'trash is empty'
}).fail(function() {
});
}
});
setTimeout(function() {
alert(t('passwords', 'Deletion of all trashed passwords done.'));
}, 3000);
}
}, true);
});
$('#PasswordsTableContent td').click(function(event) {
var $cell = $(this);
var $row = $cell.closest('tr');
var is_strength = $cell.hasClass('cell_strength');
var is_date = $cell.hasClass('cell_datechanged');
var is_notes = $cell.hasClass('icon-notes');
var is_category = $cell.hasClass('cell_category');
var is_info = $cell.hasClass('icon-info');
var is_share = $cell.hasClass('icon-share');
var is_sharedby = $cell.hasClass('icon-shared');;
var is_sharedto = $cell.hasClass('icon-public');
var is_trash = $cell.hasClass('icon-delete');
var is_restore = $cell.hasClass('icon-history');
var active_table = $('#app-settings').attr("active-table");
var loginname_pass = $cell.attr('type') == 'loginname' || $cell.attr('type') == 'pass';
var $cmd_buttons = $cell.find('.btn_commands_open');
if (loginname_pass) {
$cmd_buttons.click();
$('#btn_copy').click();
$(document.body).click();
return false;
}
var passwords = new Passwords(generateUrl('/passwords'));
// popUp function works with parameters:
// popUp(title, value, type, address_value, website, username);
if (is_category) {
popUp(t('passwords', 'Category'), $row.attr('attr_category'), 'category', '', $row.attr('attr_website'), $row.attr('attr_loginname'));
$('#accept').click(function() {
$row.attr('attr_category', $('#new_value_popup select').val());
var success = passwords.updateActive($row.attr('attr_id'), $row.attr('attr_loginname'), $row.attr('attr_website'), $row.attr('attr_address'), $row.attr('attr_pass'), $row.attr('attr_notes'), $row.attr('attr_sharedwith'), $row.attr('attr_category'), $row.hasClass('is_deleted'));
if (success) {
var view = new View(passwords);
passwords.loadAll().done(function() {
view.renderContent();
})
// building new table
var source_passwords = $('#template-passwords-serialize').html();
var template_passwords = Handlebars.compile(source_passwords);
var html_passwords_serialize = template_passwords({
passwords: passwords.getAll()
});
html_passwords_serialize = html_passwords_serialize.replace(/"/g, '"');
var rows = html_passwords_serialize.split('<br>');
formatTable(false, rows);
} else {
OCdialogs.alert(t('passwords', 'Error: Could not update password.'), t('passwords', 'Save'), function() { return false; }, true);
}
removePopup();
});
return false;
}
if (is_share || is_sharedto) {
var website = $row.attr('attr_website');
popUp(t('passwords', 'Share'), $row.attr('attr_sharedwith'), 'share', '', website, $row.attr('attr_loginname'));
$('#accept').click(function() {
var sharearray = [];
$("#new_value_popup input:checked").each(function() {
sharearray.push($(this).val());
});
var success = passwords.updateActive($row.attr('attr_id'), $row.attr('attr_loginname'), website, $row.attr('attr_address'), $row.attr('attr_pass'), $row.attr('attr_notes'), sharearray, $row.attr('attr_category'), $row.hasClass('is_deleted'));
if (success) {
if (sharearray.length == 0) {
$row.attr('attr_sharedwith', '');
$cell.removeClass('icon-public');
$cell.addClass('icon-share');
$cell.find('div').removeClass('is_sharedto');
$cell.find('div span').text(0);
} else {
$row.attr('attr_sharedwith', sharearray.join(','));
$cell.removeClass('icon-share');
$cell.addClass('icon-public');
$cell.find('div').addClass('is_sharedto');
$cell.find('div span').text(sharearray.length);
}
} else {
OCdialogs.alert(t('passwords', 'Error: Could not update password.'), t('passwords', 'Save'), function() { return false; }, true);
}
removePopup();
});
$('#stop').click(function() {
var usernames = [];
var displaynames = [];
$("#new_value_popup input:checked").each(function() {
usernames.push($(this).val());
displaynames.push($(this).siblings('span').text());
});
$('#new_value_popup input').each(function() {
this.checked = false;
});
$('#accept').click();
OCdialogs.confirm(t('passwords', 'Would you like to send a notification by email to %s?').replace('%s', displaynames.join(', ')), t('passwords', 'Send email'), function(res) {
if (res) {
var success = passwords.sendmail('stop', website, usernames, URLtoDomain(window.location.href), window.location.href, $('#app-settings').attr('instance-name'));
if (success) {
OCdialogs.info(t('passwords', 'The email has been sent to %s users.').replace('%s', usernames.length), t('passwords', 'Send email'), function() { return false; }, true);
} else {
OCdialogs.alert(t('passwords', 'Error: Could not send email.'), t('passwords', 'Send email'), function() { return false; }, true);
}
}
});
});
$('#mail').click(function() {
var usernames = [];
var displaynames = [];
$("#new_value_popup input:checked").each(function() {
usernames.push($(this).val());
displaynames.push($(this).siblings('span').text());
});
OCdialogs.confirm(t('passwords', 'This will send a notification email for %s to the following users').replace('%s', website) + ": " + displaynames.join(', ') + ". " + t('passwords', 'The email will not contain your password.') + ' ' + t('passwords', 'Are you sure?'), t('passwords', 'Send email'), function(res) {
if (res) {
var success = passwords.sendmail('start', website, usernames, URLtoDomain(window.location.href), window.location.href, $('#app-settings').attr('instance-name'));
if (success) {
OCdialogs.info(t('passwords', 'The email has been sent to %s users.').replace('%s', usernames.length), t('passwords', 'Send email'), function() { return false; }, true);
} else {
OCdialogs.alert(t('passwords', 'Error: Could not send email.'), t('passwords', 'Send email'), function() { return false; }, true);
}
}
});
});
return false;
}
if (is_sharedby) {
OCdialogs.info(t('passwords', 'Shared by %s').replace('%s', uid2displayname($row.attr('sharedby'))) + '.', t('passwords', 'Share'), function() { return false; }, true);
return false;
}
if (is_notes) {
popUp(t('passwords', 'Notes'), $row.attr('attr_notes'), 'notes', '', $row.attr('attr_website'), $row.attr('attr_loginname'), $row.hasClass('is_sharedby'));
$('#accept').click(function() {
$row.attr('attr_notes', $('#new_value_popup').val());
var success = passwords.updateActive($row.attr('attr_id'), $row.attr('attr_loginname'), $row.attr('attr_website'), $row.attr('attr_address'), $row.attr('attr_pass'), $row.attr('attr_notes'), $row.attr('attr_sharedwith'), $row.attr('attr_category'), $row.hasClass('is_deleted'));
if (success) {
if ($row.attr('attr_notes').length == 0) {
$cell.removeClass('has-note');
} else {
$cell.addClass('has-note');
}
} else {
OCdialogs.alert(t('passwords', 'Error: Could not update password.'), t('passwords', 'Save'), function() { return false; }, true);
}
removePopup();
});
return false;
}
if (is_info || is_strength || is_date) {
if ($('#sidebarRow').val() == $row.attr('attr_id')) {
$('#sidebarRow').val('');
$('#app-content-wrapper').attr('class', '');
$('#app-sidebar-wrapper').hide(100);
} else {
showSidebar($row);
}
return false;
}
if (is_trash) {
if (active_table == 'active') {
// no sharedwith, so a share will be stopped when the owner deletes the password
var success = passwords.updateActive($row.attr('attr_id'), $row.attr('attr_loginname'), $row.attr('attr_website'), $row.attr('attr_address'), $row.attr('attr_pass'), $row.attr('attr_notes'), '', $row.attr('attr_category'), "1");
if (success) {
var wasShared = $row.attr('attr_sharedwith') != null && $row.attr('attr_sharedwith') != '';
$row.addClass('is_deleted');
$row.hide();
$row.find('.icon-info').removeClass('icon-info').addClass('icon-history');
// remove shared state
$row.attr('attr_sharedwith', '');
$row.find('.icon-public').removeClass('icon-public').addClass('icon-share');
$row.find('is_sharedto span').text(0);
$row.find('.is_sharedto').removeClass('is_sharedto');
formatTable(true);
if (wasShared) {
OCdialogs.info(t('passwords', 'The password was moved to the trash bin.') + ' ' + t('passwords', 'This password is no longer shared anymore.'), t('passwords', 'Trash bin'), function() { return false; }, true);
} else {
OCdialogs.info(t('passwords', 'The password was moved to the trash bin.'), t('passwords', 'Trash bin'), function() { return false; }, true);
}
} else {
OCdialogs.alert(t('passwords', 'Error: Could not update password.'), t('passwords', 'Trash bin'), function() { return false; }, true);
}
// from trash, remove from database
} else {
OCdialogs.confirm(t('passwords', 'This will delete the password for') + " '" + $row.attr('attr_website') + "' " + t('passwords', "with user name") + " '" + $row.attr('attr_loginname') + "'. " + t('passwords', 'Are you sure?'), t('passwords', 'Trash bin'), function(res) {
if (res) {
var view = new View(passwords);
passwords.removeByID($row.attr('attr_id')).done(function() {
// now removed from db, so delete from DOM
$row.remove();
formatTable(true);
}).fail(function() {
OCdialogs.alert(t('passwords', 'Error: Could not delete password.'), t('passwords', 'Trash bin'), function() { return false; }, true);
});
}
});
}
}
if (is_restore) {
var success = passwords.updateActive($row.attr('attr_id'), $row.attr('attr_loginname'), $row.attr('attr_website'), $row.attr('attr_address'), $row.attr('attr_pass'), $row.attr('attr_notes'), '', $row.attr('attr_category'), "0");
if (success) {
$cell.removeClass('icon-history').addClass('icon-info');
$row.removeClass('is_deleted');
$row.hide();
formatTable(true);
} else {
OCdialogs.alert(t('passwords', 'Error: Could not update password.'), t('passwords', 'Trash bin'), function() { return false; }, true);
}
}
});
},
renderNavigation: function() {
// lock down app
$('#lock_btn').click(function() {
// remove cookie
document.cookie = "oc_passwords_auth=" + SHA512($('head').attr('data-user')) + "; expires=Thu, 01 Jan 1970 00:00:00 UTC";
// and reload page, the auth will initiate
window.location = window.location;
});
// set settings
var settings = new Settings(generateUrl('/settings'));
settings.load();
if ((settings.getKey('backup_allowed').toLowerCase() == 'true') == false) {
// was already hidden with CSS at default for IE7 and lower
// Now remove it from DOM (doesn't work on IE7 and lower)