<?php

/**
 * @file
 * Site security review and reporting Drupal module.
 */

use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Drupal\security_review\CheckResult;
use Drupal\security_review\SecurityCheckInterface;

/**
 * Implements hook_modules_uninstalled().
 */
function security_review_modules_uninstalled($modules): void {
  /** @var \Drupal\security_review\SecurityReview $security_review */
  $security_review = Drupal::service('security_review');

  // Clean orphaned checks.
  $security_review->cleanStorage();
}

/**
 * Implements hook_modules_installed().
 */
function security_review_modules_installed($modules): void {
  // Post-install hook.
  if (in_array('security_review', $modules)) {
    /** @var \Drupal\security_review\SecurityReview $security_review */
    $security_review = Drupal::service('security_review');

    // Clean orphaned checks.
    $security_review->cleanStorage();

    // Store the web server's user.
    $security_review->setServerData();
  }
}

/**
 * Implements hook_theme().
 */
function security_review_theme($existing, $type, $theme, $path): array {
  return [
    'check_evaluation' => [
      'template' => 'check_evaluation',
      'variables' => [
        'paragraphs' => [],
        'items' => [],
        'hushed_items' => [],
      ],
    ],
    'check_help' => [
      'template' => 'check_help',
      'variables' => [
        'title' => [],
        'paragraphs' => [],
      ],
    ],
    'general_help' => [
      'template' => 'general_help',
      'variables' => [
        'paragraphs' => [],
        'checks' => [],
      ],
    ],
    'run_and_review' => [
      'template' => 'run_and_review',
      'variables' => [
        'date' => [],
        'checks' => [],
      ],
    ],
  ];
}

/**
 * Preprocesses variables for template 'run_and_review'.
 */
function template_preprocess_run_and_review(&$variables): void {
  // Get icon list.
  $icons_root = '/core/misc/icons/';
  $variables['icons'] = [
    'success' => $icons_root . '73b355/check.svg',
    'warning' => $icons_root . 'e29700/warning.svg',
    'fail' => $icons_root . 'e32700/error.svg',
  ];

  // Generate full URLs.
  foreach ($variables['icons'] as $icon => $path) {
    $variables['icons'][$icon] = Url::fromUserInput($path)->setAbsolute()
      ->toString();
  }

  // Format date.
  $variables['date'] = \Drupal::service('date.formatter')
    ->format($variables['date']);

  // Convert check result integers to strings.
  foreach ($variables['checks'] as &$check) {
    if (isset($check['result'])) {
      switch ($check['result']) {
        case CheckResult::SUCCESS:
          $check['result'] = 'success';
          break;

        case CheckResult::FAIL:
          $check['result'] = 'fail';
          break;

        case CheckResult::WARN:
          $check['result'] = 'warning';
          break;

        case CheckResult::INFO:
          $check['result'] = 'info';
          break;
      }
    }
  }
}

/**
 * Implements hook_cron().
 */
function security_review_cron(): void {
  // Store the web server's user.
  Drupal::service('security_review')->setServerData();
}

/**
 * Batch operation: runs a single check.
 *
 * @param \Drupal\security_review\SecurityCheckInterface $check
 *   The check to run.
 * @param array $context
 *   The Batch context.
 */
function _security_review_batch_run_op(SecurityCheckInterface $check, array &$context): void {

  $context['message'] = $check->getTitle();

  if (\Drupal::config('security_review.settings')->get('log')) {
    // Inform the user about the progress.
    \Drupal::logger('security_review')
      ->log(RfcLogLevel::NOTICE, t('@name check has started', ['@name' => $context['message']]));
  }

  // Run the check.
  $finished = $check->run(FALSE, $context['sandbox']);

  // Report percentage for checks that do their own batching.
  if ($finished < 1) {
    $context['message'] .= ' (' . round($finished * 100, 1) . '%)';
  }
  $context['finished'] = $finished;

  // Store the results.
  $context['results'] = array_merge($context['results'], [$check]);
}

/**
 * Callback for finishing the batch job of running the checklist.
 *
 * @param bool $success
 *   Whether the batch job was successful.
 * @param \Drupal\security_review\CheckResult[] $results
 *   The results of the batch job.
 * @param array $operations
 *   The array of batch operations.
 */
function _security_review_batch_run_finished(bool $success, array $results, array $operations): void {
  /** @var \Drupal\security_review\SecurityReview $security_review */
  $security_review = Drupal::service('security_review');

  $security_review->setLastRun(time());
  if ($success) {
    \Drupal::messenger()->addMessage(t('Review complete'));
  }
  else {
    // Show error information.
    $error_operation = reset($operations);
    $message = t(
      'An error occurred while processing %error_operation with arguments: @arguments',
      [
        '%error_operation' => $error_operation[0],
        '@arguments' => print_r($error_operation[1], TRUE),
      ]
    );

    \Drupal::logger('security_review')->log(RfcLogLevel::ERROR, $message);
    \Drupal::messenger()->addMessage(
      t('The review did not store all results, please run again or check the logs for details.')
    );
  }
}
