function padLeft(s, length) { s = s.toString(); while (s.length < length) s = '0' + s; return s; } function dateToString(date) { return padLeft(date.getFullYear(), 4) + '-' + padLeft(date.getMonth() + 1, 2) + '-' + padLeft(date.getDate(), 2) + ' ' + padLeft(date.getHours(), 2) + ':' + padLeft(date.getMinutes(), 2) + ':' + padLeft(date.getSeconds(), 2); } function escapeHtml(text) { return $('
').text(text).html(); } function generateFlagTableRows(rows) { var html = ''; rows.forEach(function (item) { var cells = [ item.sploit, item.team !== null ? item.team : '', item.flag, dateToString(new Date(item.time * 1000)), item.status, item.checksystem_response !== null ? item.checksystem_response : '' ]; html += ''; cells.forEach(function (text) { html += '' + escapeHtml(text) + ''; }); html += ''; }); return html; } function generatePaginator(totalCount, rowsPerPage, pageNumber) { var totalPages = Math.ceil(totalCount / rowsPerPage); var firstShown = Math.max(1, pageNumber - 3); var lastShown = Math.min(totalPages, pageNumber + 3); var html = ''; if (firstShown > 1) html += '
  • «
  • '; for (var i = firstShown; i <= lastShown; i++) { var extraClasses = (i === pageNumber ? "active" : ""); html += '
  • ' + '' + i + '' + '
  • '; } if (lastShown < totalPages) html += '
  • ' + '»' + '
  • '; return html; } function getPageNumber() { return parseInt($('#page-number').val()); } function setPageNumber(number) { $('#page-number').val(number); } var queryInProgress = false; function showFlags() { if (queryInProgress) return; queryInProgress = true; $('.search-results').hide(); $('.query-status').html('Loading...').show(); $.post('/ui/show_flags', $('#show-flags-form').serialize()) .done(function (response) { $('.search-results tbody').html(generateFlagTableRows(response.rows)); $('.search-results .total-count').text(response.total_count); $('.search-results .pagination').html(generatePaginator( response.total_count, response.rows_per_page, getPageNumber())); $('.search-results .page-link').click(function (event) { event.preventDefault(); setPageNumber($(this).data("content")); showFlags(); }); $('.query-status').hide(); $('.search-results').show(); }) .fail(function () { $('.query-status').html("Failed to load flags from the farm server"); }) .always(function () { queryInProgress = false; }); } function postFlagsManual() { if (queryInProgress) return; queryInProgress = true; $.post('/ui/post_flags_manual', $('#post-flags-manual-form').serialize()) .done(function () { var sploitSelect = $('#sploit-select'); if ($('#sploit-manual-option').empty()) sploitSelect.append($('')); sploitSelect.val('Manual'); $('#team-select, #flag-input, #time-since-input, #time-until-input, ' + '#status-select, #checksystem-response-input').val(''); queryInProgress = false; showFlags(); }) .fail(function () { $('.query-status').html("Failed to post flags to the farm server"); queryInProgress = false; }); } $(function () { showFlags(); $('#show-flags-form').submit(function (event) { event.preventDefault(); setPageNumber(1); showFlags(); }); $('#post-flags-manual-form').submit(function (event) { event.preventDefault(); postFlagsManual(); }); });