password.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @name password.js
  3. * @author Philipp Maurer
  4. * @author Tobias Reich
  5. * @copyright 2013 by Philipp Maurer, Tobias Reich
  6. *
  7. * Password Module
  8. * Controls the access to password-protected albums and photos.
  9. */
  10. password = {
  11. value: "",
  12. set: function(albumID) {
  13. var buttons,
  14. params;
  15. buttons = [
  16. ["Set Password", function() {
  17. if (visible.album()) album.json.password = true;
  18. params = "setAlbumPassword&albumID=" + albumID + "&password=" + hex_md5($("input.password").val());
  19. lychee.api(params, "text", function(data) {
  20. if (!data) loadingBar.show("error");
  21. });
  22. }],
  23. ["Cancel", function() {}]
  24. ];
  25. modal.show("Set Password", "Set a password to protect '" + album.json.title + "' from unauthorized viewers. Only people with this password can view this album. <input class='password' type='password' placeholder='password' value=''>", buttons);
  26. },
  27. get: function(albumID, callback) {
  28. var passwd = $("input.password").val(),
  29. params;
  30. if (!lychee.publicMode) callback();
  31. else if (album.json&&album.json.password==false) callback();
  32. else if (albums.json&&albums.json.content[albumID].password==false) callback();
  33. else if (!albums.json&&!album.json) {
  34. // Continue without password
  35. album.json = {password: true};
  36. callback("");
  37. } else if (passwd==undefined) {
  38. // Request password
  39. password.getDialog(albumID, callback);
  40. } else {
  41. // Check password
  42. params = "checkAlbumAccess&albumID=" + albumID + "&password=" + hex_md5(passwd);
  43. lychee.api(params, "text", function(data) {
  44. if (data) {
  45. password.value = hex_md5(passwd);
  46. callback();
  47. } else {
  48. lychee.goto("");
  49. loadingBar.show("error", "Error", "Access denied. Wrong password!");
  50. }
  51. });
  52. }
  53. },
  54. getDialog: function(albumID, callback) {
  55. var buttons;
  56. buttons = [
  57. ["Enter", function() { password.get(albumID, callback) }],
  58. ["Cancel", lychee.goto]
  59. ];
  60. modal.show("<a class='icon-lock'></a> Enter Password", "This album is protected with a password. Enter the password below to view the photos of this album: <input class='password' type='password' placeholder='password' value=''>", buttons);
  61. },
  62. remove: function(albumID) {
  63. var params;
  64. if (visible.album()) album.json.password = false;
  65. params = "setAlbumPassword&albumID=" + albumID + "&password=";
  66. lychee.api(params, "text", function(data) {
  67. if (!data) loadingBar.show("error");
  68. });
  69. }
  70. }