Commit 0179dd7e authored by PidgeyL's avatar PidgeyL
Browse files

merging more responses to statusses.js

parent 3e8fe3b4
Loading
Loading
Loading
Loading
+16 −17
Original line number Diff line number Diff line
@@ -408,19 +408,19 @@ def bookmark():
    bms = request.args.get('cve', type=str).split(",")
    try:
      db.addBookmarks(current_user.get_id(), bms)
      return jsonify({"status":"success"})
      return jsonify({"status":"bookmarked"})
    except:
      return jsonify({"status": "failure"})
      return jsonify({"status": "bookmarking_failed"})

@app.route('/_un-bookmark', methods=['GET'])
def unbookmark():
    bms = request.args.get('cve', type=str).split(",")
    try:
      db.removeBookmarks(current_user.get_id(), bms)
      return jsonify({"status":"success"})
      return jsonify({"status":"bookmarked"})
    except Exception as e:
      print(e)
      return jsonify({"status": "failure"})
      return jsonify({"status": "bookmarking_failed"})

@app.route('/browse/<vendor>')
@app.route('/browse/')
@@ -556,9 +556,11 @@ def listAdd():
    cpe = request.args.get('cpe')
    cpeType = request.args.get('type')
    lst = request.args.get('list')
    status = ["added", "success"] if addCPEToList(cpe, lst, cpeType) else ["already_exists", "info"]
    if cpe and cpeType and lst:
        status = "added_to_list" if addCPEToList(cpe, lst, cpeType) else "already_exists_in_list"
        returnList = db.getWhitelist() if lst=="whitelist" else db.getBlacklist()
        return jsonify({"status":status, "rules":returnList, "listType":lst.title()})
    else: return jsonify({"status": "could_not_add_to_list"})


@app.route('/admin/removeFromList')
@@ -569,11 +571,11 @@ def listRemove():
    cpe = cpe.replace("%3a", ":")
    cpe = cpe.replace("%2f", "/")
    lst = request.args.get('list', type=str)
    if cpe:
        result=removeWhitelist(cpe) if lst=="whitelist" else removeBlacklist(cpe)
        status = ["removed", "success"] if (result > 0) else ["already_removed", "info"]
    if cpe and lst:
        result=removeWhitelist(cpe) if lst.lower()=="whitelist" else removeBlacklist(cpe)
        status = "removed_from_list" if (result > 0) else "already_removed_from_list"
    else:
        status = ["invalid_url", "error"]
        status = "invalid_cpe"
    returnList = db.getWhitelist() if lst=="whitelist" else db.getBlacklist()
    return jsonify({"status":status, "rules":returnList, "listType":lst.title()})

@@ -587,12 +589,9 @@ def listEdit():
    CPEType = request.args.get('type')
    if oldCPE and newCPE:
        result = updateWhitelist(oldCPE, newCPE, CPEType) if lst=="whitelist" else updateBlacklist(oldCPE, newCPE, CPEType)
        if (result):
            status = ["updated", "success"]
        status = "cpelist_updated" if (result) else "cpelist_update_failed"
    else:
            status = ["update_failed", "error"]
    else:
        status = ["invalid_url", "error"]
        status = "invalid_cpe"
    returnList = list(db.getWhitelist()) if lst=="whitelist" else list(db.getBlacklist())
    return jsonify({"rules":returnList, "status":status, "listType":lst})

+117 −0
Original line number Diff line number Diff line
function clear(table){
  $("#"+table+"id").val("");
  $("#"+table+"comments").val("");
  $("#"+table+"add").val("Add");
  if(table != "cpe"){$("#"+table+"select").val("targetsoftware");}
  $('#cancel'+table).remove();
  editedcpe="";
  editedkeyword="";
}
var editedcpe;
var editedkeyword;
$(document).ready(function() {
  init();
});
function init(){
  $('#cpes tbody tr').add('#keywords tbody tr').on( 'click', '#edit', function () {
    var table = $(this).closest('table').attr('id').slice(0, -1);
    $("#cancel"+table).remove();
    window["edited"+table] = $(this).closest('tr').find("td").eq(2).text();
    if(table == "cpe"){ var td=3; }else{ var td=4; }
    var comments = $(this).closest('tr').find("td").eq(td).text().split("\n");
    var commentString="";
    for(comment in comments){
      if(comments[comment].trim().length!=0){commentString = commentString + comments[comment].trim() + "\n"};
    }
    commentString = commentString.trim();
    $("#"+table+"id").val(window["edited"+table]);
    $("#"+table+"comments").val(commentString);
    if(table != "cpe"){
      $("#"+table+"select").val($(this).closest('tr').find("td").eq(3).text().trim().replace(" ","").toLowerCase());
    }
    $("#"+table+"add").val("Edit");
    $("#add"+table).append(" <button id='cancel"+table+"' type='button'>Cancel</button> ");
    $('#cancel'+table).click(function() {
      clear(table);
    });
    jQuery('html, body').animate({scrollTop: 0}, 500);
  });
}
function remove(item){
  if(confirm("Are you sure you want to remove this rule?")){
    $.getJSON('/admin/removeFromList', {
      list: $("#values").val(), cpe:item
    }, function(data) {
      if(parseStatus(data)){ fillTable(data);}
    });
  }
}
function addItem(cpetype) {
  var CPE, commentArray, keyword;
  var comments = "";
  // get field info and build cpe
  if(cpetype == "cpe"){var listType="cpe"}else{var listType="keyword"}
  CPE = $("#"+listType+"id").val().trim();
  commentArray = $("#"+listType+"comments").val().trim();
  if(cpetype != "cpe"){
    cpetype = $("#keywordselect").val().trim();
  }
  if (commentArray){
    commentArray = commentArray.split("\n");
    for (comment in commentArray){
      comments = comments + "# " + commentArray[comment];
    }
  }
  CPE = CPE+comments;
  // check if cpe was edited
  if(window["edited"+listType]){
    $.getJSON('/admin/editInList', {
      list: $("#values").val(), cpe:CPE, oldCPE:window["edited"+listType], type:cpetype
    }, function(data) {
      if(parseStatus(data)){ fillTable(data);}
    });
  }else{
    alert($("#values").val());
    $.getJSON('/admin/addToList', {
      list: $("#values").val(), cpe:CPE, type:cpetype
    }, function(data) {
      if(parseStatus(data)){ fillTable(data);}
    });
  }
  clear(listType);
}
function fillTable(data){
  alert('in here');
  var rules=data['rules'];
  $("#cpes > tbody > tr").remove();
  $("#keywords > tbody > tr").remove();
  var line = "";
  for (i=0;i<rules.length;i++){
    //First td
    line += "<tr><td><a href='javascript:remove(\""+rules[i]['id']+"\")'><span class='glyphicon glyphicon-remove'></span></a></td>";
    //Second td
    if('comments' in rules[i]){line += "<td><a id='edit'><span class='glyphicon glyphicon-edit'></span></a></td>";
    }else{                     line += "<td><a id='edit'><span class='glyphicon glyphicon-edit'></span></a></td>";}
    //Third td
    line += "<td>"+rules[i]['id']+"</td>";
    //Possible fourth td
    if(rules[i]['type']!='cpe'){
      if(rules[i]['type'] == 'targethardware'){        line += "<td>Target Hardware</td>";
      }else if (rules[i]['type'] == 'targetsoftware'){ line += "<td>Target Software</td>";}
    }
    //last td
    line += "<td><ul>";
    if('comments' in rules[i]){
      for (j=0;j<rules[i]['comments'].length;j++){
        line += "<li>"+rules[i]['comments'][j]+"</li>";
      }
    }
    line += "</ul></td></tr>";
    if(rules[i]['type']=='cpe'){ $("#cpes > tbody").append(line);
    }else{                       $("#keywords > tbody").append(line);
    }
    line="";
  }
  init();
}
    
+26 −12
Original line number Diff line number Diff line
@@ -11,20 +11,24 @@ function parseStatus(data){
          setStatus("Last update info <div class='well'><pre>"+data['updateOutput']+"</pre></div>", "success"); break;
        }
        _ok=true;break;
      case "logged_in":
        setStatus("Logged in successfully", "success"); _ok=true; break;
      case "db_updated":
        setStatus("Database update finished <div class='well'><pre>"+data['updateOutput']+"</pre></div>", "success"); break;
        _ok=true;break;
      case "logged_in":                 setStatus("Logged in successfully", "success");                                                               _ok=true;break;
      case "db_updated":                setStatus("Database update finished <div class='well'><pre>"+data['updateOutput']+"</pre></div>", "success"); _ok=true;break;
      case "wl_imported":
      case "bl_imported":
        setStatus(list+" import finished"); _ok=true; break;
      case "bl_imported":               setStatus(list+" import finished");                                                                           _ok=true;break;
      case "wl_already_filled":
      case "bl_already_filled":
        setStatus(list+" is already filled. You can force to drop the database", "info"); break;
      case "bl_already_filled":         setStatus(list+" is already filled. You can force to drop the database", "info");                                      break;
      case "wl_dropped":
      case "bl_dropped":
        setStatus(list+" dropped", "success"); _ok=true; _ok=true; break;
      case "bl_dropped":                setStatus(list+" dropped", "success");                                                                        _ok=true;break;
      case "bookmarked":                                                                                                                              _ok=true;break;
      case "bookmarking_failed":        setStatus("Bookmarking failed", "danger ");                                                                            break;
      case "added_to_list":             briefShow("Rule added to the "+data["listType"], "success", "ok");                                            _ok=true;break;
      case "could_not_add_to_list":     briefShow("Could not add the CPE to the " +data["listType"], "danger", "remove");                                      break;
      case "removed_from_list":         briefShow("Rule removed from the "+data["listType"], "success", "ok");                                        _ok=true;break;
      case "already_exists_in_list":    briefShow("This rule or a more global rule already exists in the "+data["listType"], "info", "info");                  break;
      case "already_removed_from_list": briefShow("Rule was already removed from the "+data["listType"], "info", "info");                                      break;
      case "invalid_cpe":               briefShow("This cpe is not valid", "danger", "remove");                                                                break;
      case "cpelist_updated":           briefShow("The rule was updated", "success", "ok");                                                           _ok=true;break;
      case "cpelist_update_failed":     briefShow("Failed to update the rule in the "+data["listType"], "danger", "remove");                                   break;
      default:
        setStatus("A problem occurred with the server!", "danger");
    }
@@ -38,3 +42,13 @@ function setStatus(text, status){
  $("#status-box").addClass("alert alert-"+status);
  $("#status-box").append(text);
}

function briefShow(text, status, icon){
  $("#status").removeClass();
  $("#status").addClass("alert alert-"+status);
  $("#status_icon").removeClass();
  $("#status_icon").addClass("glyphicon glyphicon-"+icon+"-sign");
  $("#status_message").empty();
  $("#status_message").append(text);
  $("#status").removeTemporaryClass("hidden", 3000);
}
+3 −3
Original line number Diff line number Diff line
@@ -9,11 +9,11 @@
      $("#star").on('click', function(event){
        if($("#star").hasClass("unstarred")){
          $.getJSON('/_bookmark',{cve:'{{cve["id"]}}'},function(data){ 
            if(data['status']=='success'){$("#star").attr("class", "glyphicon glyphicon-star starred")}
            if(parseStatus(data)){$("#star").attr("class", "glyphicon glyphicon-star starred")}
          })
        }else{
          $.getJSON('/_un-bookmark',{cve:'{{cve["id"]}}'},function(data){
            if(data['status']=='success'){$("#star").attr("class", "glyphicon glyphicon-star-empty unstarred")}
            if(parseStatus(data)){$("#star").attr("class", "glyphicon glyphicon-star-empty unstarred")}
          })
        }
      })
+3 −156
Original line number Diff line number Diff line
@@ -2,164 +2,11 @@
{% block title %}{{ listType }} management{% endblock %}
{% block head %}
  <!-- javascript -->
  <script type="text/javascript">
    var editedcpe;
    var editedkeyword;
    function addItem(cpetype) {
      var CPE, commentArray, keyword;
      var comments = "";
      // get field info and build cpe
      if(cpetype == "cpe"){var listType="cpe"}else{var listType="keyword"}
      CPE = $("#"+listType+"id").val().trim();
      commentArray = $("#"+listType+"comments").val().trim();
      if(cpetype != "cpe"){
        cpetype = $("#keywordselect").val().trim();
      }
      if (commentArray){
        commentArray = commentArray.split("\n");
        for (comment in commentArray){
          comments = comments + "# " + commentArray[comment];
        }
      }
      CPE = CPE+comments;
      // check if cpe was edited
      if(window["edited"+listType]){
        $.getJSON('/admin/editInList', {
          list:'{{ listType|lower }}', cpe:CPE, oldCPE:window["edited"+listType], type:cpetype
        }, function(data) {
          showStatus(data);
          fillTable(data);
        });
      }else{
        $.getJSON('/admin/addToList', {
          list:'{{ listType|lower }}', cpe:CPE, type:cpetype
        }, function(data) {
          showStatus(data);
          fillTable(data);
        });
      }
      clear(listType);
    }
    function showStatus(data){
      $("#status").removeClass();
      $("#status_icon").removeClass();
      if(data['status'][1] =='success'){
        $("#status").addClass("alert alert-success");
        $("#status_icon").addClass("glyphicon glyphicon-ok-sign");
      }else if (data['status'][1] =='info'){
        $("#status").addClass("alert alert-info");
        $("#status_icon").addClass("glyphicon glyphicon-info-sign");
      }else if (data['status'][1] =='warning'){
        $("#status").addClass("alert alert-warning");
        $("#status_icon").addClass("glyphicon glyphicon-warning-sign");
      }else if (data['status'][1] =='error'){
        $("#status").addClass("alert alert-danger");
        $("#status_icon").addClass("glyphicon glyphicon-remove-sign");
      }
      $("#status_message").empty();
      if(     data['status'][0] == 'added'          ){ $("#status_message").append("Rule added to the "+data["listType"]);}
      else if(data['status'][0] == 'removed'        ){ $("#status_message").append("Rule removed from the "+data["listType"]);}
      else if(data['status'][0] == 'updated'        ){ $("#status_message").append("The rule was updated");}
      else if(data['status'][0] == 'update_failed'  ){ $("#status_message").append("Failed to update the rule in the "+data["listType"]);}
      else if(data['status'][0] == 'already_exists' ){ $("#status_message").append("This rule or a more global rule already exists in the "+data["listType"]);}
      else if(data['status'][0] == 'already_removed'){ $("#status_message").append("Rule was already removed from the "+data["listType"]);}
      else if(data['status'][0] == 'invalid_url'    ){ $("#status_message").append("Invalid URL!");}
      $("#status").removeTemporaryClass("hidden", 3000);
    }
    function fillTable(data){
      var rules=data['rules'];
      $("#cpes > tbody > tr").remove();
      $("#keywords > tbody > tr").remove();
      var line = "";
      for (i=0;i<rules.length;i++){
        //First td
        line += "<tr><td><a href='javascript:remove(\""+rules[i]['id']+"\")'><span class='glyphicon glyphicon-remove'></span></a></td>";
        //Second td
        if('comments' in rules[i]){
          line += "<td><a id='edit'><span class='glyphicon glyphicon-edit'></span></a></td>";
        }else{
          line += "<td><a id='edit'><span class='glyphicon glyphicon-edit'></span></a></td>";
        }
        //Third td
        line += "<td>"+rules[i]['id']+"</td>";
        //Possible fourth td
        if(rules[i]['type']!='cpe'){
          if(rules[i]['type'] == 'targethardware'){
            line += "<td>Target Hardware</td>";
          }else if (rules[i]['type'] == 'targetsoftware'){
            line += "<td>Target Software</td>";
          }
        }
        //last td
        line += "<td><ul>";
        if('comments' in rules[i]){
          for (j=0;j<rules[i]['comments'].length;j++){
            line += "<li>"+rules[i]['comments'][j]+"</li>";
          }
        }
        line += "</ul></td></tr>";
        if(rules[i]['type']=='cpe'){
          $("#cpes > tbody").append(line);
        }else{
          $("#keywords > tbody").append(line);
        }
        line="";
      }
      init();
    }

    function remove(item){
      if(confirm("Are you sure you want to remove this rule?")){
        $.getJSON('/admin/removeFromList', {
          list:'{{ listType|lower }}',
          cpe:item
        }, function(data) {
          showStatus(data);
          fillTable(data);
        });
      }
    }

    $(document).ready(function() {
      init();
    });
    function clear(table){
      $("#"+table+"id").val("");
      $("#"+table+"comments").val("");
      $("#"+table+"add").val("Add");
      if(table != "cpe"){$("#"+table+"select").val("targetsoftware");}
      $('#cancel'+table).remove();
      editedcpe="";
      editedkeyword="";
    }
    function init(){
      $('#cpes tbody tr').add('#keywords tbody tr').on( 'click', '#edit', function () {
        var table = $(this).closest('table').attr('id').slice(0, -1);
        $("#cancel"+table).remove();
        window["edited"+table] = $(this).closest('tr').find("td").eq(2).text();
        if(table == "cpe"){ var td=3; }else{ var td=4; }
        var comments = $(this).closest('tr').find("td").eq(td).text().split("\n");
        var commentString="";
        for(comment in comments){
          if(comments[comment].trim().length!=0){commentString = commentString + comments[comment].trim() + "\n"};
        }
        commentString = commentString.trim();
        $("#"+table+"id").val(window["edited"+table]);
        $("#"+table+"comments").val(commentString);
        if(table != "cpe"){
          $("#"+table+"select").val($(this).closest('tr').find("td").eq(3).text().trim().replace(" ","").toLowerCase());
        }
        $("#"+table+"add").val("Edit");
        $("#add"+table).append(" <button id='cancel"+table+"' type='button'>Cancel</button> ");
        $('#cancel'+table).click(function() {
          clear(table);
        });
        jQuery('html, body').animate({scrollTop: 0}, 500);
      });
    }
  </script>
  <script type="text/javascript" src="/static/js/custom/list.js"></script>
{% endblock %}
{% block content %}
  <!-- Values -->
  <input id="values" type="hidden" value="{{listType}}">
  <!-- breadcrumb -->
  <ol class="breadcrumb">
    <li><a href="/admin">Admin</a></li>