/** * Copyright (C) 2015-20 CERBER TECH INC., https://wpcerber.com */ jQuery(document).ready(function ($) { window.crb_scan_id = 0; var crb_req_min_delay = 1000; // ms, throttling - making requests to the server not often than var crb_scan_mode = ''; var crb_user_stop = false; var crb_scan_in_progress = false; var crb_issues_counter = [0, 0, 0, 0]; var crb_issues_total = 0; var crb_response; var scanner_data; var all_issues = {}; var crb_scan_requests = 0; var crb_server_errors = 0; var crb_scan_display = $("#crb-scan-display"); var crb_scan_controls = $('#crb-scan-controls'); var crb_file_controls = $('#crb-file-controls'); var crb_scan_filter = $('#crb-scan-filter'); var crb_scan_details = $('#crb-scan-details'); var crb_scan_progress = $('#crb-scan-progress'); var crb_scan_bar = crb_scan_progress.find('#the-scan-bar'); var crb_scan_message = $("#crb-scan-message"); var crb_scan_browser = $("#crb-browse-files"); var crb_txt_strings = []; var crb_the_file; var crb_row_id = 0; // For local parent -> child relationship if (crb_admin_page === 'cerber-integrity' && (crb_admin_tab === '' || crb_admin_tab === 'scan_main')) { cerber_scan_load_data(); } crb_scan_controls.find(':button,a').click(function (event) { var operation = $(event.target).data('control'); switch (operation) { case 'start_scan': cerber_scan_start($(event.target)); break; case 'continue_scan': cerber_scan_continue(); break; case 'stop_scan': crb_user_stop = true; crb_scan_in_progress = false; //cerber_scan_controls('stopped'); //cerber_scan_controls('disabled'); break; case 'delete_file': case 'ignore_add_file': cerber_scan_bulk_files(operation); break; case 'full-paths': cerber_toggle_file_name(event.target); break; } if (crb_scan_in_progress) { window.onbeforeunload = function () { return 'Scanning in progress'; } } else { window.onbeforeunload = null; } event.preventDefault(); }); function cerber_scan_start(object) { console.log('Start Scan'); all_issues = {}; crb_scan_mode = object.data('mode'); crb_scan_requests = 0; crb_user_stop = false; crb_issues_counter = [0, 0, 0, 0]; crb_issues_total = 0; crb_scan_display.find('[data-init]').each(function () { $(this).html($(this).data('init')); }); crb_scan_filter.find('.crb-scan-flon').removeClass('crb-scan-flon'); /*$('#crb-scan-details').find('[data-init]').each(function () { $(this).html($(this).data('init')); });*/ crb_scan_message.slideDown().html(crb_scan_msg_steps[0]); cerber_update_bar(true); cerber_scan_controls('scanning'); crb_scan_browser.find('tr').not('.crb-scan-container').remove(); cerber_scan_step('start_scan'); } function cerber_scan_continue() { cerber_scan_controls('scanning'); cerber_scan_step(); } function cerber_scan_step(operation) { console.log('Request ' + crb_scan_requests); if (!operation) { operation = 'continue_scan'; } crb_scan_in_progress = true; crb_scan_requests++; cerber_rate_control.setState(0); setTimeout(function (state) { cerber_rate_control.setState(state); }, crb_req_min_delay, 1); $.post(ajaxurl, { action: 'cerber_scan_control', cerber_scan_do: operation, cerber_scan_mode: crb_scan_mode, ajax_nonce: crb_ajax_nonce }, function (server_response) { cerber_scan_parse(server_response); cerber_scan_display(false); if (!crb_user_stop && crb_response.cerber_scan_do !== 'stop') { cerber_scan_next_step(); } else { cerber_scan_ended(); } } ).fail(function (jqXHR, textStatus, errorThrown) { console.error('Server error: ' + jqXHR.status); crb_server_errors++; if (crb_server_errors < 3) { cerber_scan_next_step(); } else { cerber_scan_ended(); alert('Process has been aborted due to server error. Check the browser console for errors.'); } }); } // Continue to scan with rate control function cerber_scan_next_step() { if (cerber_rate_control.getState()) { cerber_scan_step(); } else { setTimeout(cerber_scan_step, crb_req_min_delay); } } function cerber_scan_ended(){ window.onbeforeunload = null; crb_scan_in_progress = false; cerber_scan_controls('stopped'); crb_scan_message.slideUp('slow'); cerber_update_bar(); if (scanner_data.aborted) { var msg = 'Scanning is aborted due to server error. '; if (scanner_data.errors && scanner_data.errors.length) { msg = msg + scanner_data.errors[0]; } alert(msg); } else if (!crb_user_stop) { cerber_popup_show('The scan is finished', '
The scan is finished. Please review the results.
'); } } function cerber_scan_display(no_scroll) { if (!scanner_data.started) { return; } var smode = scanner_data.mode; if (scanner_data.cloud) { smode += ', Scheduled'; } else { smode += ', Manual'; } smode = '' + smode + ''; $("#crb-started").html(scanner_data.started); $("#crb-finished").html(scanner_data.finished); $("#crb-duration").html(scanner_data.duration); $("#crb-performance").html(scanner_data.performance); $("#crb-smode").html(smode); $.each(scanner_data.numbers, function (type, value) { var e = $('#crb-numbers-' + type); if (e.length) { e.find('.crb-scan-number').html(value); e.find('span').addClass('crb-scan-flon'); } }); $("#crb-total-files").html(scanner_data.total.files); $("#crb-scanned-files").html(scanner_data.scanned.files); if (!scanner_data.aborted && crb_scan_in_progress) { crb_scan_message.html(crb_scan_msg_steps[scanner_data.step]); } cerber_update_bar(); // Displaying issues var issues; if (!scanner_data.issues && scanner_data.step_issues) { issues = scanner_data.step_issues; } else { issues = scanner_data.issues; } $.each(issues, function (section_id, section_data) { var the_items = []; if (!this.issues.length) { //return; } // Avoid JS undefined error with an old data set var vul_list; if (typeof section_data.sec_details !== 'undefined') { vul_list = section_data.sec_details.vul_list; } var section_name = section_data.name; var setype = section_data.setype; var section_header_class = 'crb-scan-section'; if (section_data.container) { section_header_class = section_header_class + ' section-' + section_data.container; } var section_items = []; var issue_type_id, f_name, isize, itime, full_name; var risk; var rbox; var name_classes; var version; var target_section = crb_scan_browser.find('#' + section_id); var parent_section_id = (target_section.length ? target_section.data('row-id') : crb_row_id); var section_header = '' + under + '
'; } section_header = '' + crb_scan_msg_issues[issue.data.prced] + '
'; } else if (issue[3]) { ret += '' + crb_scan_msg_issues[issue[3]] + '
'; } return ret; } // Enable/disable scan controls function cerber_scan_controls(state) { var stop = $('#crb-stop-scan'); cerber_file_controls(); switch (state) { case 'scanning': crb_scan_controls.find(':button').hide(); stop.show(); break; case 'stopped': crb_scan_controls.find(':button').show(); stop.hide(); if (scanner_data.finished) { $('#crb-continue-scan').hide(); } break; case 'disabled': crb_scan_controls.find(':button').prop( "disabled", true ); break; } } // Enable/disable file controls function cerber_file_controls() { var b = crb_file_controls.find(':button'); if (crb_scan_browser.find('input[type=checkbox]').length) { b.show(); } else { b.hide(); } var a = crb_scan_controls.find('a'); if (crb_scan_browser.find('.crb-item-file').length) { a.show(); } else { a.hide(); } } function cerber_update_bar(show) { if (!crb_scan_in_progress) { if (!show) { crb_scan_progress.hide(); } else { crb_scan_progress.show(); } crb_scan_bar.width(0); return; } crb_scan_progress.show(); crb_scan_progress.width('100%'); if (scanner_data.scanned.files > 0) { percentage = 30 + (scanner_data.scanned.files / scanner_data.total.files) * 70; } else { if (scanner_data.step < 3) { percentage = 10; } else { percentage = 10 + crb_scan_requests * 5; } } crb_scan_bar.animate({width: percentage + '%'}, 1000); //var bar_width = (percentage*crb_scan_bar.parent().width()/100)+'px'; //crb_scan_bar.width('100px'); //crb_scan_bar.find('div').animate({width: bar_width}, 1000); ///crb_scan_bar.animate({width: bar_width}, 1000); //crb_scan_bar.find('div').animate({width: percentage + '%'}, 1000); //crb_scan_bar.animate({width: percentage + '%'}, 1000); } // Rate limiting helper var cerber_rate_control = (function () { var state = 0; var obj = {}; obj.setState = function (setnew) { state = setnew; }; obj.getState = function() { return state; }; return obj; }()); function cerber_scan_bulk_files(operation) { var selected = crb_scan_browser.find('input[type=checkbox]:checked'); if (!selected.length) { return; } if (!cerber_user_confirm(crb_scan_msg_misc[operation][0])) { return; } var files = []; $.each(selected, function () { files.push($(this).closest('tr').data('file_name')); }); cerber_scan_ajax_operation(files, operation); } function cerber_scan_ajax_operation(files, operation) { if (!files.length) { return; } var formData = new FormData(); formData.append('action', 'cerber_scan_bulk_files'); formData.append('ajax_nonce', crb_ajax_nonce); formData.append('scan_id', window.crb_scan_id); formData.append('scan_file_operation', operation); if (files instanceof Array) { $.each(files, function (index, value) { formData.append('files[]', value); }); } else { formData.append('files[]', files); } $.ajax({ url: ajaxurl, type: 'POST', data: formData, contentType: false, processData: false, dataType: 'json' }).done(function (server_response) { var msg = '', title = ''; if (server_response.errors && server_response.errors.length) { title = crb_scan_msg_misc['file_error']; msg = '' + crb_scan_msg_misc['file_error'] + '
' + server_response.errors.join('
') + '
' + crb_scan_msg_misc[operation][1] + '
' + server_response.processed.join('
') + '
' + crb_txt_strings['explain'][0] + '
'); } // Some data after file inspection? var d = cerber_xdata_info(section.prop('id'), $(element).data('idx')); if (d.length) { info.push(d); } if (section_type > 20) { info.push(cerber_get_issue_explain(itype)); } cerber_popup_show($(element).text(), info, true); } function cerber_xdata_info(section_id, idx) { if (!section_id || typeof idx === 'undefined') { return ''; } if (typeof all_issues[section_id].issues[idx].details === 'undefined') { return ''; } var xdata = all_issues[section_id].issues[idx].details.xdata; if (!xdata.length) { return ''; } var itype = all_issues[section_id].issues[idx][0]; var tokens = [], regs = [], info = '', ls = []; $.each(xdata, function (index, e) { if (e[0] === 1) { tokens.push('Line ' + e[2] + ': ' + e[1] + '
' + crb_txt_strings[e[0]][e[1]][1] + '
'); } else { ls = []; $.each(e[2], function (index, s) { ls.push('Line ' + s[2] + ': ' + s[0] + '
');
});
regs.push(ls.join('' + crb_txt_strings[e[0]][e[1]] + ' (' + e[1] + ')' + '
'); } }); if (tokens.length) { info += '' + crb_txt_strings['explain'][3] + '
' + title + '
'+ ret.join('
') + '
' } function cerber_get_itype(e) { return $(e).closest('tr').data('itype'); } function cerber_get_section(e) { return $(e).closest('tr').prevAll('.crb-scan-section:first'); } function cerber_get_ifile(e) { return $(e).closest('tr').data('file_name'); } /* function cerber_load_strings() { $.get(ajaxurl, { action: 'cerber_get_strings', ajax_nonce: crb_ajax_nonce, }, function (server_response) { crb_scan_strings = $.parseJSON(server_response); if (!crb_scan_strings.complete) { alert('Unable to load strings due to a server error.'); } }).fail(function () { alert('Unable to load strings due to a server error.'); }); }*/ // Uploader var crb_upload_form = $('#crb-ref-upload-dialog').find('form'); var crb_upload_form_ul = $(crb_upload_form).find('ul'); crb_upload_form.submit(function (event) { var formData = new FormData($(this)[0]); formData.append('action', 'cerber_ref_upload'); formData.append('ajax_nonce', crb_ajax_nonce); crb_upload_form.find('input').prop('disabled', true); crb_upload_form.find('input').hide(); //crb_upload_form_ul.find('li').not(':nth-child(-n+2)').hide(); crb_upload_form_ul.children().hide(); crb_upload_form_ul.find('li:nth-child(1)').show(); //ref_file_name = $(this).find('input[name="refile"]').val(); $.ajax({ url: ajaxurl, type: 'POST', enctype: 'multipart/form-data', data: formData, contentType: false, processData: false, dataType: 'json' }).done(crb_ref_step2); crb_upload_form.trigger('reset'); event.preventDefault(); }); function crb_ref_step2(server_response) { if (!server_response.error) { crb_upload_form_ul.find('li:nth-child(2)').show(); $.post(ajaxurl, { action: 'cerber_ref_upload', ajax_nonce: crb_ajax_nonce, }, crb_ref_done, 'json'); } else { crb_ref_done(server_response); } } function crb_ref_done(server_response) { crb_ref_errors(server_response); if (!server_response.error) { tb_remove(); } crb_enable_ref_form(); } function crb_ref_errors(response) { if (response.error) { crb_upload_form_ul.append('' + button + '
'); $('#TB_window .crb-popup-inner').html(''); //popup.find('input[type=button]').off('click'); popup.find('input[type=button]').on('click', function (event) { //$(this).off('click'); /*event.preventDefault(); event.stopPropagation(); event.stopImmediatePropagation();*/ cerber_popup_close(this); }); tb_show(title, '#TB_inline?width=' + w + '&height=' + h + '&inlineId=crb-popup-box'); $('#TB_closeWindowButton').blur(); } /* function cerber_dialog_show(title, message, control_id) { var button = ''; var popup = cerber_init_popup('crb-popup-dialog'); popup.html('