github.com/merlinepedra/gophish1@v0.9.0/static/js/src/app/settings.js (about)

     1  $(document).ready(function () {
     2      $('[data-toggle="tooltip"]').tooltip();
     3      $("#apiResetForm").submit(function (e) {
     4          api.reset()
     5              .success(function (response) {
     6                  user.api_key = response.data
     7                  successFlash(response.message)
     8                  $("#api_key").val(user.api_key)
     9              })
    10              .error(function (data) {
    11                  errorFlash(data.message)
    12              })
    13          return false
    14      })
    15      $("#settingsForm").submit(function (e) {
    16          $.post("/settings", $(this).serialize())
    17              .done(function (data) {
    18                  successFlash(data.message)
    19              })
    20              .fail(function (data) {
    21                  errorFlash(data.responseJSON.message)
    22              })
    23          return false
    24      })
    25      //$("#imapForm").submit(function (e) {
    26      $("#savesettings").click(function() {
    27          var imapSettings = {}
    28          imapSettings.host = $("#imaphost").val()
    29          imapSettings.port = $("#imapport").val()
    30          imapSettings.username = $("#imapusername").val()
    31          imapSettings.password = $("#imappassword").val()
    32          imapSettings.enabled = $('#use_imap').prop('checked')
    33          imapSettings.tls = $('#use_tls').prop('checked')
    34  
    35          //Advanced settings
    36          imapSettings.folder = $("#folder").val()
    37          imapSettings.imap_freq = $("#imapfreq").val()
    38          imapSettings.restrict_domain = $("#restrictdomain").val()
    39          imapSettings.delete_reported_campaign_email = $('#deletecampaign').prop('checked')
    40          
    41          //To avoid unmarshalling error in controllers/api/imap.go. It would fail gracefully, but with a generic error.
    42          if (imapSettings.host == ""){
    43              errorFlash("No IMAP Host specified")
    44              document.body.scrollTop = 0;
    45              document.documentElement.scrollTop = 0;
    46              return false
    47          }
    48          if (imapSettings.port == ""){
    49              errorFlash("No IMAP Port specified")
    50              document.body.scrollTop = 0;
    51              document.documentElement.scrollTop = 0;
    52              return false
    53          }
    54          if (isNaN(imapSettings.port) || imapSettings.port <1 || imapSettings.port > 65535  ){ 
    55              errorFlash("Invalid IMAP Port")
    56              document.body.scrollTop = 0;
    57              document.documentElement.scrollTop = 0;
    58              return false
    59          }
    60          if (imapSettings.imap_freq == ""){
    61              imapSettings.imap_freq = "60"
    62          }
    63  
    64          api.IMAP.post(imapSettings).done(function (data) {
    65                  if (data.success == true) {
    66                      successFlashFade("Successfully updated IMAP settings.", 2)
    67                  } else {
    68                      errorFlash("Unable to update IMAP settings.")
    69                  }
    70              })
    71              .success(function (data){
    72                  loadIMAPSettings()
    73              })
    74              .fail(function (data) {
    75                  errorFlash(data.responseJSON.message)
    76              })
    77              .always(function (data){
    78                  document.body.scrollTop = 0;
    79                  document.documentElement.scrollTop = 0;
    80              })
    81          
    82          return false
    83      })
    84  
    85      $("#validateimap").click(function() {
    86  
    87          // Query validate imap server endpoint
    88          var server = {}
    89          server.host = $("#imaphost").val()
    90          server.port = $("#imapport").val()
    91          server.username = $("#imapusername").val()
    92          server.password = $("#imappassword").val()
    93          server.tls = $('#use_tls').prop('checked')
    94  
    95          //To avoid unmarshalling error in controllers/api/imap.go. It would fail gracefully, but with a generic error. 
    96          if (server.host == ""){
    97              errorFlash("No IMAP Host specified")
    98              document.body.scrollTop = 0;
    99              document.documentElement.scrollTop = 0;
   100              return false
   101          }
   102          if (server.port == ""){
   103              errorFlash("No IMAP Port specified")
   104              document.body.scrollTop = 0;
   105              document.documentElement.scrollTop = 0;
   106              return false
   107          }
   108          if (isNaN(server.port) || server.port <1 || server.port > 65535  ){
   109              errorFlash("Invalid IMAP Port")
   110              document.body.scrollTop = 0;
   111              document.documentElement.scrollTop = 0;
   112              return false
   113          }
   114  
   115          var oldHTML = $("#validateimap").html();
   116          // Disable inputs and change button text
   117          $("#imaphost").attr("disabled", true);
   118          $("#imapport").attr("disabled", true);
   119          $("#imapusername").attr("disabled", true);
   120          $("#imappassword").attr("disabled", true);
   121          $("#use_imap").attr("disabled", true);
   122          $("#use_tls").attr("disabled", true);
   123          $("#folder").attr("disabled", true);
   124          $("#restrictdomain").attr("disabled", true);
   125          $('#deletecampaign').attr("disabled", true);
   126          $('#lastlogin').attr("disabled", true);
   127          $('#imapfreq').attr("disabled", true);
   128          $("#validateimap").attr("disabled", true);  
   129          $("#validateimap").html("<i class='fa fa-circle-o-notch fa-spin'></i> Testing...");
   130          
   131          api.IMAP.validate(server).done(function(data) {
   132              if (data.success == true) {
   133                  Swal.fire({
   134                      title: "Success",
   135                      html: "Logged into <b>" + $("#imaphost").val() + "</b>",
   136                      type: "success",
   137                  })
   138              } else {
   139                  Swal.fire({
   140                      title: "Failed!",
   141                      html: "Unable to login to <b>" + $("#imaphost").val() + "</b>.",
   142                      type: "error",
   143                      showCancelButton: true,
   144                      cancelButtonText: "Close",
   145                      confirmButtonText: "More Info",
   146                      confirmButtonColor: "#428bca",
   147                      allowOutsideClick: false,
   148                  }).then(function(result) {
   149                      if (result.value) {
   150                          Swal.fire({
   151                              title: "Error:",
   152                              text: data.message,
   153                          })
   154                      }
   155                    })
   156              }
   157              
   158            })
   159            .fail(function() {
   160              Swal.fire({
   161                  title: "Failed!",
   162                  text: "An unecpected error occured.",
   163                  type: "error",
   164              })
   165            })
   166            .always(function() {
   167              //Re-enable inputs and change button text
   168              $("#imaphost").attr("disabled", false);
   169              $("#imapport").attr("disabled", false);
   170              $("#imapusername").attr("disabled", false);
   171              $("#imappassword").attr("disabled", false);
   172              $("#use_imap").attr("disabled", false);
   173              $("#use_tls").attr("disabled", false);
   174              $("#folder").attr("disabled", false);
   175              $("#restrictdomain").attr("disabled", false);
   176              $('#deletecampaign').attr("disabled", false);
   177              $('#lastlogin').attr("disabled", false);
   178              $('#imapfreq').attr("disabled", false);
   179              $("#validateimap").attr("disabled", false);
   180              $("#validateimap").html(oldHTML);
   181  
   182            });
   183  
   184        }); //end testclick
   185  
   186      $("#reporttab").click(function() {
   187          loadIMAPSettings()
   188      })
   189  
   190      $("#advanced").click(function() {
   191          $("#advancedarea").toggle();
   192      })
   193  
   194      function loadIMAPSettings(){
   195          api.IMAP.get()
   196          .success(function (imap) {
   197              if (imap.length == 0){
   198                  $('#lastlogindiv').hide()
   199              } else {
   200                  imap = imap[0]
   201                  if (imap.enabled == false){
   202                      $('#lastlogindiv').hide()
   203                  } else {
   204                      $('#lastlogindiv').show()
   205                  }
   206                  $("#imapusername").val(imap.username)
   207                  $("#imaphost").val(imap.host)
   208                  $("#imapport").val(imap.port)
   209                  $("#imappassword").val(imap.password)
   210                  $('#use_tls').prop('checked', imap.tls)
   211                  $('#use_imap').prop('checked', imap.enabled)
   212                  $("#folder").val(imap.folder)
   213                  $("#restrictdomain").val(imap.restrict_domain)
   214                  $('#deletecampaign').prop('checked', imap.delete_reported_campaign_email)
   215                  $('#lastloginraw').val(imap.last_login)
   216                  $('#lastlogin').val(moment.utc(imap.last_login).fromNow())
   217                  $('#imapfreq').val(imap.imap_freq)
   218              }  
   219  
   220          })
   221          .error(function () {
   222              errorFlash("Error fetching IMAP settings")
   223          })
   224      }
   225  
   226      var use_map = localStorage.getItem('gophish.use_map')
   227      $("#use_map").prop('checked', JSON.parse(use_map))
   228      $("#use_map").on('change', function () {
   229          localStorage.setItem('gophish.use_map', JSON.stringify(this.checked))
   230      })
   231  
   232      loadIMAPSettings()
   233  })