Example #1
0
/**
 * Process the requests sent by the form submissions originated in the integrity
 * page, all forms must have a nonce field that will be checked against the one
 * generated in the template render function.
 *
 * @return void
 */
function sucuriscan_integrity_form_submissions()
{
    if (SucuriScanInterface::check_nonce()) {
        // Force the execution of the filesystem scanner.
        if (SucuriScanRequest::post(':force_scan') !== false) {
            SucuriScanEvent::notify_event('plugin_change', 'Filesystem scan forced at: ' . date('r'));
            SucuriScanEvent::filesystem_scan(true);
        }
        // Restore, Remove, Mark as fixed the core files.
        $allowed_actions = '(restore|delete|fixed)';
        $integrity_action = SucuriScanRequest::post(':integrity_action', $allowed_actions);
        if ($integrity_action !== false) {
            $cache = new SucuriScanCache('integrity');
            $integrity_files = SucuriScanRequest::post(':integrity_files', '_array');
            $integrity_types = SucuriScanRequest::post(':integrity_types', '_array');
            $files_selected = count($integrity_files);
            $files_affected = array();
            $files_processed = 0;
            $action_titles = array('restore' => 'Core file restored', 'delete' => 'Non-core file deleted', 'fixed' => 'Core file marked as fixed');
            if ($integrity_files) {
                foreach ((array) $integrity_files as $i => $file_path) {
                    $full_path = ABSPATH . $file_path;
                    $status_type = $integrity_types[$i];
                    switch ($integrity_action) {
                        case 'restore':
                            $file_content = SucuriScanAPI::get_original_core_file($file_path);
                            if ($file_content) {
                                $restored = @file_put_contents($full_path, $file_content, LOCK_EX);
                                $files_processed += $restored ? 1 : 0;
                                $files_affected[] = $full_path;
                            }
                            break;
                        case 'delete':
                            if (@unlink($full_path)) {
                                $files_processed += 1;
                                $files_affected[] = $full_path;
                            }
                            break;
                        case 'fixed':
                            $cache_key = md5($file_path);
                            $cache_value = array('file_path' => $file_path, 'file_status' => $status_type, 'ignored_at' => time());
                            $cached = $cache->add($cache_key, $cache_value);
                            $files_processed += $cached ? 1 : 0;
                            $files_affected[] = $full_path;
                            break;
                    }
                }
                // Report files affected as a single event.
                if (!empty($files_affected)) {
                    $message_tpl = count($files_affected) > 1 ? '%s: (multiple entries): %s' : '%s: %s';
                    $message = sprintf($message_tpl, $action_titles[$integrity_action], @implode(',', $files_affected));
                    switch ($integrity_action) {
                        case 'restore':
                            SucuriScanEvent::report_info_event($message);
                            break;
                        case 'delete':
                            SucuriScanEvent::report_notice_event($message);
                            break;
                        case 'fixed':
                            SucuriScanEvent::report_warning_event($message);
                            break;
                    }
                }
                SucuriScanInterface::info(sprintf('<code>%d</code> out of <code>%d</code> files were successfully processed.', $files_selected, $files_processed));
            }
        }
    }
}