{% extends "layout.html" %}
{% block content %}
<div class="jumbotron">
    <h1>Phone: {{phone.name}}</h1>
            <table class="table" class="display hover compact table table-striped table-bordered" id="table1" style="width: 100%">
                <thead>
                <tr>
                    <th>Package Name</th>
                    <th>App Name</th>
                    <th>Cert</th>
                    <th>Perms</th>
                    <td></td>
                    <th>VT</th>
                    <th>Yara</th>
                    <th>Risk</th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th>Cert</th>
                    <th>Split</th>
                </tr>
                </thead>
                <tbody>
                {% for p in apks %}
                <tr>
                    <td><a href="/apk/{{p.id}}">{{p.package_name}}</a>
<span class="dropdown">
  <button class="btn btn-xs btn-light dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
      {% if p.vt_link %}
      <a class="dropdown-item" href="{{p.vt_link}}" target="blank_">VT</a>
      {% endif %}
      <a class="dropdown-item" href="https://koodous.com/apks/{{p.sha256}}" target="_blank">Koodous</a>
        <a class="dropdown-item" href="https://www.apklab.io/samples.html?filter={{p.sha256}}" target="blank_">APKLab</a>
  </span>

                    </td>
                    <td>{{p.app_name}}</td>
                    <td><small>{{p.certificate_sha1}}</small>{% if p.certificate_trusted%} <i class="fa fa-check-circle text-success" data-toggle="tooltip" data-placement="top" title="{{p.certificate_trusted_name}}"></i> {%endif%}</td>
                    <td style="text-align: center;">{{p.permissions_suspicious}}</td>
                    <td style="text-align: center;">{% if p.frosting %}<i class="fa fa-play" data-toggle="tooltip" data-placement="top" title="Google Play Store Metadata"></i>{%endif%}</td>
                    <td style="text-align: center;">
                        {% if p.vt_check %}
                        {% if p.vt_link %}
                            {% if p.vt_positives == 0 %}
                                <a href="{{p.vt_link}}" target="_blank"><span class="badge badge-success">{{p.vt_positives}} / {{p.vt_total}}</span></a>
                            {% elif p.vt_positives < 5 %}
                                <a href="{{p.vt_link}}" target="_blank"><span class="badge badge-warning">{{p.vt_positives}} / {{p.vt_total}}</span></a>
                            {% else %}
                                <a href="{{p.vt_link}}" target="_blank"><span class="badge badge-danger">{{p.vt_positives}} / {{p.vt_total}}</span></a>
                            {% endif %}
                        {% else %}
                            <a href="https://www.virustotal.com/gui/search/{{p.sha256}}" target="_blank"><span class="badge badge-warning">Unknown</span></a>
                        {% endif %}
                        {% else %}
                            <span class="badge badge-danger">Failed</span>
                        {% endif %}
                    </td>
                    <td style="text-align: center;">
                        {% for y in p.yara %}
                            <span class="badge badge-danger">{{y}}</span>
                        {% endfor %}
                    </td>
                    <td style="text-align: center;">{% if p.suspicious_level == 1%}<span class="badge badge-success">Low</span>{%elif p.suspicious_level == 2%}<span class="badge badge-warning">Medium</span>{%else%}<span class="badge badge-danger">High</span>{% endif %}</td>
                    <td style="text-align: center;"><span id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{% if p.suspicious == False %}<i class="fa fa-thumbs-up"></i>{%elif p.suspicious == True %}<i class="fa fa-thumbs-down"></i>{% else %}<i class="fa fa-question"></i>{% endif %}</span><div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                            <a class="dropdown-item" href="/apk/{{p.id}}/status?status=good&next=phone"><i class="fa fa-thumbs-up"></i></a>
                            <a class="dropdown-item" href="/apk/{{p.id}}/status?status=none&next=phone"><i class="fa fa-question"></i></a>
    <a class="dropdown-item" href="/apk/{{p.id}}/status?status=bad&next=phone"><i class="fa fa-thumbs-down"></i></a>
  </div></td>
                    <td>{{p.id}}</td>
                    <td>{{p.suspicious}}</td>
                    <td>{{p.suspicious_level}}</td>
                    <td>{{p.certificate_trusted}}</td>
                    <td>{{p.split}}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
</div>
{% endblock %}
{% block scripts %}
    <script>
$(function(){
    var table = $('#table1').DataTable({
        "paging":   false,
        "order": [[ 9, "desc" ]],
        "columnDefs": [
            {
                "targets": [ 0],
                "width": "30%"
            },
            {
                "targets": [ 1],
                "width": "15%"
            },
            {
                "targets": [ 2],
                "width": "320px"
            },
            {
                "targets": [ 3],
                "width": "50px"
            },
            {
                "targets": [ 4],
                "width": "50px"
            },
            {
                "targets": [ 5],
                "width": "80px"
            },
            {
                "targets": [ 6],
                "width": "50px"
            },
            {
                "targets": [ 7],
                "width": "60px"
            },
            {
                "targets": [ 8],
                "width": "15px"
            },
            {
                "targets": [ 9 ],
                "visible": false,
                "searchable": false
            },
            {
                "targets": [ 10 ],
                "visible": false,
            },
            {
                "targets": [ 11 ],
                "visible": false,
            },
            {
                "targets": [ 12 ],
                "visible": false,
            },
            {
                "targets": [ 13 ],
                "visible": false,
            }
        ],
        "dom": 'Bfrtip',
        "buttons": [
            {
                text: 'Mark Displayed Apps As Good',
                action: function ( e, dt, node, config ) {
                    data = table.rows({ search: 'applied' }).data();
                    apks = Array.from(data, x => x[9]);
                    if (apks.length > 0) {
                        d = JSON.stringify({'status': 'good', 'apks': apks});
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "/apk/bulk_status",
                            data: d,
                            dataType: "json",
                            success: function (data, st, xhr) {
                                document.location.reload(true);
                            }
                        });
                    }
                }
            },
            {
                text: 'Reset Analysis of Displayed Apps',
                action: function ( e, dt, node, config ) {
                    data = table.rows({ search: 'applied' }).data();
                    apks = Array.from(data, x => x[9]);
                    if (apks.length > 0) {
                        d = JSON.stringify({'status': 'none', 'apks': apks});
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "/apk/bulk_status",
                            data: d,
                            dataType: "json",
                            success: function (data, st, xhr) {
                                document.location.reload(true);
                            }
                        });
                    }
                }
            },
            {
                text: 'Hide Analyzed Apps',
                action: function ( e, dt, node, config ) {
                    table
                    .column(10)
                    .search('None')
                    .draw();
                }
            },
            {
                text: 'Hide Apps with Trusted Certs',
                action: function ( e, dt, node, config ) {
                    table
                    .column(12)
                    .search('False')
                    .draw();
                }
            },
            {
                text: 'Hide Secondary Split Apks',
                action: function ( e, dt, node, config ) {
                    table
                    .column(13)
                    .search('False')
                    .draw();
                }
            },
            {
                text: 'Show All',
                action: function ( e, dt, node, config ) {
                    table
                    .search( '' )
                    .columns().search( '' )
                    .draw();
                }
            }
        ]
    });
} );
    </script>
{% endblock %}
