# ivyforms/0.8/backend/src/Services/Security/SecurityService.php

The Innovative Form Builder – IvyForms, version 0.8. 136 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.8/code/backend/src/Services/Security/SecurityService.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.8/raw/backend/src/Services/Security/SecurityService.php
- Modified: 2026-01-13T11:11:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.8/code/backend/src/Services/Security/SecurityService.php#L10-L20`.

```php
<?php

/**
 * @copyright © Melograno Venture Studio. All rights reserved.
 * @licence   See COPYING.md for license details.
 */

namespace IvyForms\Services\Security;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use IvyForms\Common\Exceptions\ForbiddenException;
use IvyForms\Factory\Security\SecurityServiceFactory;
use IvyForms\Factory\Security\CaptchaProviderResolver;
use IvyForms\Services\Security\CaptchaServiceInterface;

/**
 * Class SecurityService
 *
 * Central service for all security-related functionality including CAPTCHA validation.
 * This service acts as a facade over the SecurityServiceFactory, providing a clean
 * interface for controllers to interact with security features.
 *
 * @package IvyForms\Services\Security
 */
class SecurityService
{
    /**
     * @var SecurityServiceFactory
     */
    private SecurityServiceFactory $securityServiceFactory;

    public function __construct(SecurityServiceFactory $securityServiceFactory)
    {
        $this->securityServiceFactory = $securityServiceFactory;
    }

    /**
     * Get the currently active CAPTCHA provider
     *
     * @return string ('recaptcha', 'hcaptcha', 'turnstile', 'none')
     */
    public function getActiveCaptchaProvider(): string
    {
        return $this->securityServiceFactory->getActiveCaptchaProvider();
    }

    /**
     * Determine CAPTCHA provider based on form fields and current settings
     *
     * @param array<object> $formFields Array of form field objects
     * @return string The CAPTCHA provider to use for this form
     */
    public function getCaptchaProviderForForm(array $formFields): string
    {
        return $this->securityServiceFactory->getCaptchaProviderForForm($formFields);
    }

    /**
     * Validate CAPTCHA response for form submission
     *
     * This method automatically determines the appropriate CAPTCHA provider
     * based on the form fields and validates the submission accordingly.
     *
     * @param array<string,mixed> $submissionData The form submission data
     * @param array<object> $formFields The form field objects
     * @return bool
     * @throws ForbiddenException If CAPTCHA validation fails
     */
    public function validateFormSubmission(array $submissionData, array $formFields): bool
    {
        return $this->securityServiceFactory->validateFormSubmission($submissionData, $formFields);
    }

    /**
     * Get security configuration for frontend
     *
     * @param array<object> $formFields Array of form field objects to determine required config
     * @return array<string, mixed>
     */
    public function getFrontendSecurityConfig(array $formFields): array
    {
        $provider = $this->getCaptchaProviderForForm($formFields);

        if ($provider === CaptchaProviderResolver::PROVIDER_NONE) {
            return [
                'captcha' => [
                    'enabled' => false,
                    'provider' => CaptchaProviderResolver::PROVIDER_NONE
                ]
            ];
        }

        $config = [
            'captcha' => [
                'enabled' => true,
                'provider' => $provider
            ]
        ];

        // Add provider-specific configuration
        switch ($provider) {
            case CaptchaProviderResolver::PROVIDER_RECAPTCHA:
                $service = $this->securityServiceFactory->createActiveCaptchaService($formFields);
                $config['captcha']['recaptcha'] = [
                    'configured' => false,
                    'type' => 'v2',
                    'siteKey' => '',
                    'scriptUrl' => '',
                    'size' => 'normal'
                ];
                if ($service && $service->isConfigured()) {
                    $recaptchaConfig = $service->getFrontendConfig(
                        $service->getType(),
                        $service->getSiteKey()
                    );
                    // Add the configured flag that frontend expects
                    $recaptchaConfig['configured'] = true;
                    $config['captcha']['recaptcha'] = $recaptchaConfig;
                }
                break;

            // Future providers can be added here
            case CaptchaProviderResolver::PROVIDER_HCAPTCHA:
            case CaptchaProviderResolver::PROVIDER_TURNSTILE:
                // To be implemented
                break;
        }

        return $config;
    }
}

```
