
{% extends "base/base_layout.html" %}
{% load static %}
 {% block sidebar_option %}
      sidebar-collapse
{% endblock %}
 {% block extra_css %}
<link href="{% static "adminlte/plugins/sweetalert2/sweetalert2.min.css" %}" rel="stylesheet">

<style>
#app_icon{
        width: 64px;
        height: 64px;
    }
.selected {
    background-color: lightgreen !important;
}

.selectable_table tr:hover {
    background-color: lightgreen !important;
}
</style>
{% endblock %}
{% block content %}
<div class="content-wrapper">
  <div class="content-header">
  </div>
   <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
            <div class="card">
              <div class="card-body">
                

                 <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title"><i class="fa fa-rocket"></i> Recent Scans</h3>
            {% if async_scans %}
            <p class="lead">View scan progress in the <strong><a href="{% url 'list_tasks' %}">Scan Queue</a></strong></p>
            {% endif %}
        </div>

        <div class="box-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover table-striped">
                    <thead>
                    <tr>
                        <th>APP</th>
                        <th>FILE</th>
                        <th>TYPE</th>
                        <th>HASH</th>
                        <th>SCAN DATE</th>
                        <th style="width: 9%">ACTIONS</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for e in entries %}
                        <tr>
                            <td style="text-align: center;">
                                <img id="app_icon" src="{% if e.ICON_PATH %}/download/{{ e.ICON_PATH }}{% else %}{% static 'img/no_icon.png' %}{% endif %}">
                                {% if e.PACKAGE_NAME or e.APP_NAME %}
                                  <br/><strong>{{ e.APP_NAME }} {% if e.VERSION_NAME %} - {{ e.VERSION_NAME }} {% endif %}</strong>
                                  </br>{{ e.PACKAGE_NAME }}
                                  {% if e.FILE_NAME|slice:"-6:" != '.dylib' %}
                                  {% if e.FILE_NAME|slice:"-5:" != '.appx' %}
                                    {% if e.FILE_NAME|slice:"-3:" != '.so' %}
                                      {% if e.FILE_NAME|slice:"-2:" != '.a' %}
                                    <p> <a href="{% url "appsec_dashboard" checksum=e.MD5 %}" class="btn btn-sm btn-outline-primary" role="button"><i class="fas fa-user-shield"></i> MobSF Scorecard</a></p>
                                      {% endif %}
                                    {% endif %}
                                    {% endif %}
                                  {% endif %}
                                  <p><a class="btn btn-primary btn-sm" href="../../../{{ e.ANALYZER }}/{{e.MD5}}/"><i class="fas fa-eye"></i> Static Report</a>
                                  {% if '.apk' == e.FILE_NAME|slice:"-4:" or '.xapk' == e.FILE_NAME|slice:"-5:" or '.apks' == e.FILE_NAME|slice:"-5:" or '.aab' == e.FILE_NAME|slice:"-4:" %}
                                    <a  class="btn btn-success btn-sm {% if not e.DYNAMIC_REPORT_EXISTS %}disabled{% endif %}" href="{% url "dynamic_report" checksum=e.MD5 %}"><i class="fa fa-mobile"></i> Dynamic Report</a>
                                  {% elif '.ipa' == e.FILE_NAME|slice:"-4:" %}
                                    {% if e.PACKAGE_NAME %}
                                        <a  class="btn btn-success btn-sm {% if not e.DYNAMIC_REPORT_EXISTS %}disabled{% endif %}" href="{% url "ios_view_report" bundle_id=e.PACKAGE_NAME %}"><i class="fa fa-mobile"></i> Dynamic Report</a>
                                    {% endif %}
                                  {% endif %}
                                  </p>
                                {% else %}
                                </br><span class="badge bg-warning">scan not completed</span>
                                {% endif %}
                            </td>
                            <td>{{ e.FILE_NAME }}
                            </td>
                            <td style="text-align: center;">
                                {% if '.apk' == e.FILE_NAME|slice:"-4:"%}<i class="fab fa-android fa-3x"></i>
                                {% elif '.xapk' == e.FILE_NAME|slice:"-5:"%}<i class="fab fa-android fa-3x"></i>
                                {% elif '.apks' == e.FILE_NAME|slice:"-5:"%}<i class="fab fa-android fa-3x"></i>
                                {% elif '.aab' == e.FILE_NAME|slice:"-4:"%}<i class="fab fa-android fa-3x"></i>
                                {% elif '.jar' == e.FILE_NAME|slice:"-4:"%}<i class="fab fa-java fa-3x"></i>
                                {% elif '.aar' == e.FILE_NAME|slice:"-4:"%}<i class="fas fa-table fa-3x"></i>
                                {% elif '.so' == e.FILE_NAME|slice:"-3:"%}<i class="fa fa-th-large fa-3x"></i>
                                {% elif '.ipa' == e.FILE_NAME|slice:"-4:"%}<i class="fab fa-apple fa-3x"></i>
                                {% elif '.dylib' == e.FILE_NAME|slice:"-6:"%}<i class="fa fa-th-large fa-3x"></i>
                                {% elif '.a' == e.FILE_NAME|slice:"-2:"%}<i class="fa fa-th-large fa-3x"></i>
                                {% elif '.zip' == e.FILE_NAME|slice:"-4:"%}<i class="fas fa-file-archive fa-3x"></i>
                                {% elif '.appx' == e.FILE_NAME|slice:"-5:"%}<i class="fab fa-windows fa-3x"></i>
                                {% endif %}
                            </td>
                            <td>{{ e.MD5 }}</td>
                            <td>{{ e.TIMESTAMP }}</td>
                            <td><p>
                                   <a class="btn btn-outline-primary btn-sm" href="{% url "pdf" checksum=e.MD5%}"><i class="fas fa-file-pdf"></i></a>
                                   <a class="btn btn-outline-info btn-sm" href="../../../{{ e.ANALYZER }}/{{e.MD5}}/?rescan=1"><i class="fas fa-sync-alt"></i></a>
                                   <a class="btn btn-outline-secondary btn-sm" href="{% url "download_binary" checksum=e.MD5%}"><i class="fas fa-download"></i></a>
                                </p>
                            {% if '.apk' == e.FILE_NAME|slice:"-4:" or '.xapk' == e.FILE_NAME|slice:"-5:" or '.apks' == e.FILE_NAME|slice:"-5:" or '.aab' == e.FILE_NAME|slice:"-4:"%}
                                <p><a class="diffButton btn btn-warning btn-sm" id="{{ e.MD5 }}_{{ e.FILE_NAME }}"><i class="fas fa-not-equal"></i> Diff or Compare</a>
                                </p>
                            {% endif %}
                            <p> <a class="btn btn-danger btn-sm" id="{{ e.MD5 }}" onclick="delete_scan(this)" href="#"><i class="fa fa-trash"></i> Delete Scan</a> </p>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <!--pagination-->
            <nav aria-label="Pagination">
                <ul class="pagination justify-content-center">
                  {% if page_obj.has_previous %}
                  <li class="page-item">
                    <a class="page-link" href="{% url 'scans_paginated' page_size=page_obj.page_size page_number=page_obj.previous_page_number %}">Previous</a>
                  </li>   
                  {% else %}
                  <li class="page-item">
                  <a class="page-link">Previous</a>
                  </li>
                  {% endif %}
      
                  {% for i in page_obj.paginator.page_range %}
                  {% if page_obj.number == i %}
                  <li class="page-item active">
                      <a class="page-link" href="#">{{ i }} </a>
                  </li>
                  {% else %}
                  <li class="page-item">
                      <a class="page-link" href="{% url 'scans_paginated' page_size=page_obj.page_size page_number=i %}">{{ i }}</a>
                  </li>
                  {% endif %}
                  {% endfor %}
      
                  {% if page_obj.has_next %}
                  <li class="page-item">
                    <a class="page-link" href="{% url 'scans_paginated' page_size=page_obj.page_size page_number=page_obj.next_page_number %}">Next</a>
                   </li>
                  {% else %}
                  <li class="page-item">
                    <a class="page-link">Next</a>
                  </li>
                  {% endif %}
                  <li class="page-item">
                    <div class="dropdown">
                        <button class="page-link dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                          Items per page
                        </button>
                        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                          <a class="dropdown-item" href="{% url 'scans_paginated' page_size=5 page_number=page_obj.number %}">5</a>
                          <a class="dropdown-item" href="{% url 'scans_paginated' page_size=10 page_number=page_obj.number %}">10</a>
                          <a class="dropdown-item" href="{% url 'scans_paginated' page_size=20 page_number=page_obj.number %}">20</a>
                          <a class="dropdown-item" href="{% url 'scans_paginated' page_size=50 page_number=page_obj.number %}">50</a>
                          <a class="dropdown-item" href="{% url 'scans_paginated' page_size=100 page_number=page_obj.number %}">100</a>
                          <a class="dropdown-item" href="{% url 'scans_paginated' page_size=250 page_number=page_obj.number %}">250</a>
                        </div>
                      </div>
                </ul>
              </nav>
            <!--end pagination-->
        </div>
        <!-- /.box-body -->
    </div>


          </div>
        </div>
       </div>
     </div>
    </div>
</div>
{% endblock %}
{% block extra_scripts %}
<script src="{% static "adminlte/plugins/sweetalert2/sweetalert2.min.js" %}"></script>
<script>

    // Escape HTML
    function escapeHtml(unsafe)
    {
        return unsafe
            .replace(/&/g, "&amp;")
            .replace(/</g, "&lt;")
            .replace(/>/g, "&gt;")
            .replace(/"/g, "&quot;")
            .replace(/'/g, "&#039;");
    }

    // Diff functions
    var diff_first_md5 = '';
    var diff_first_name = '';

    // When a row is clicked, we check if we already have first scan, if so, ask for confirmation
    function enable_partner_select() {
        $('table tr').on('click', function (e) {
            e.preventDefault();
            if (diff_first_md5 == '') {
                return;
            }
            diff_confirmation($(this));
        })
    }


    function enable_diff_button(){
        $(".diffButton").on('click', function(e){
            e.stopPropagation();
            diff_select($(this));
        });
    }

    function disable_diff_button() {
        $('.diffButton').off('click');
    }

    // First pop up only saves the first scan to diff and tells the user to select a partner
    function diff_select(item) {

        Swal.fire({
            title: '<strong>Select App to Compare</strong>',
            type: 'info',
            text: 'Please select the second scan result for comparison',
            timer: 10000
        });
        diff_first_md5 = item.attr('id').slice(0, 32);
        diff_first_name = item.attr('id').slice(33);
        item.closest("tr").addClass("selected");
        item.closest("tbody").addClass("selectable_table");

        // Enable the second partner selection
        enable_partner_select();
        disable_diff_button();
    }

    function diff_cleanup() {
        first_td_id = diff_first_md5 + '_' + escapeHtml(diff_first_name);
        $('[id="' + first_td_id + '"]').closest("tr").removeClass("selected");
        $('[id="' + first_td_id + '"]').closest("tbody").removeClass("selectable_table");
        diff_first_md5 = "";
        diff_first_name = "";
        enable_diff_button();
    }

    // Here we get jquery row
    function diff_confirmation(item) {
        // First we need the id which has the md5 and name
        rows_tds = item.find('td');
        selected_md5 = rows_tds[3].innerText;
        if (diff_first_md5 == selected_md5) {
            return;
        }
        diff_second_md5 = selected_md5;
        diff_second_name = rows_tds[1].innerText;

        Swal.fire({
            title: '<strong>Diff confirmation</strong>',
            type: 'info',
            html:
                '<strong>Do you want to diff - </strong><br />' + escapeHtml(diff_first_name) +
                '<br /> <strong>with - <br /> </strong>' + escapeHtml(diff_second_name) + ' <br /> <strong>?</strong>',

            showCancelButton: true,
            cancelButtonText: 'Cancel',
            confirmButtonText: 'Start Diffing!',
        }).then((result) => {
            if (result.value) {
                window.location = '/compare/' + diff_first_md5 + '/' + diff_second_md5 + '/';
            } else {
                 diff_cleanup();
            }
        })
    }

    function delete_scan(item){
      Swal.fire({
      title: 'Are you sure?',
      text: "This will permanently remove the scan results from MobSF",
      type: 'warning',
      showCancelButton: true,
      confirmButtonText: 'Yes',
      cancelButtonText: 'No',
      confirmButtonColor: '#d33',
      cancelButtonColor: '#2da532',
    }).then((result) => {
        if (result.value) {
            var md5_hash = item.id;
            $.ajax({
                    url: '{% url "delete_scan" %}',
                        type : 'POST',
                    dataType: 'json',
                        data : {
                                csrfmiddlewaretoken: '{{ csrf_token }}',
                                md5: md5_hash,
                                },
                            success : function(json) {
                                if (json.deleted ==='yes'){
                                    Swal.fire(
                                        'Deleted!',
                                        'The scan result is deleted!',
                                        'success'
                                    ).then(function () {
                                        location.reload();
                                    })
                                }
                                else {
                                    Swal.fire(
                                    'Delete Failed',
                                    json.deleted,
                                    'error'
                                    )
                                }
                            },
                            error : function(xhr,errmsg,err) {
                                Swal.fire(
                                    'Delete Failed',
                                    errmsg,
                                    'error'
                                    )
                            }
                });
               
        } else {
                diff_cleanup();
        }
        });
}
    

enable_diff_button();

</script>

{% endblock %}