| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Services\Security; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 |
use IvyForms\Factory\Security\SecurityServiceFactory; |
| 17 |
use IvyForms\Factory\Security\CaptchaProviderResolver; |
| 18 |
use IvyForms\Services\Security\CaptchaServiceInterface; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class SecurityService |
| 22 |
* |
| 23 |
* Central service for all security-related functionality including CAPTCHA validation. |
| 24 |
* This service acts as a facade over the SecurityServiceFactory, providing a clean |
| 25 |
* interface for controllers to interact with security features. |
| 26 |
* |
| 27 |
* @package IvyForms\Services\Security |
| 28 |
*/ |
| 29 |
class SecurityService |
| 30 |
{ |
| 31 |
/** |
| 32 |
* @var SecurityServiceFactory |
| 33 |
*/ |
| 34 |
private SecurityServiceFactory $securityServiceFactory; |
| 35 |
|
| 36 |
public function __construct(SecurityServiceFactory $securityServiceFactory) |
| 37 |
{ |
| 38 |
$this->securityServiceFactory = $securityServiceFactory; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get the currently active CAPTCHA provider |
| 43 |
* |
| 44 |
* @return string ('recaptcha', 'hcaptcha', 'turnstile', 'none') |
| 45 |
*/ |
| 46 |
public function getActiveCaptchaProvider(): string |
| 47 |
{ |
| 48 |
return $this->securityServiceFactory->getActiveCaptchaProvider(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Determine CAPTCHA provider based on form fields and current settings |
| 53 |
* |
| 54 |
* @param array<object> $formFields Array of form field objects |
| 55 |
* @return string The CAPTCHA provider to use for this form |
| 56 |
*/ |
| 57 |
public function getCaptchaProviderForForm(array $formFields): string |
| 58 |
{ |
| 59 |
return $this->securityServiceFactory->getCaptchaProviderForForm($formFields); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Validate CAPTCHA response for form submission |
| 64 |
* |
| 65 |
* This method automatically determines the appropriate CAPTCHA provider |
| 66 |
* based on the form fields and validates the submission accordingly. |
| 67 |
* |
| 68 |
* @param array<string,mixed> $submissionData The form submission data |
| 69 |
* @param array<object> $formFields The form field objects |
| 70 |
* @return bool |
| 71 |
* @throws ForbiddenException If CAPTCHA validation fails |
| 72 |
*/ |
| 73 |
public function validateFormSubmission(array $submissionData, array $formFields): bool |
| 74 |
{ |
| 75 |
return $this->securityServiceFactory->validateFormSubmission($submissionData, $formFields); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get security configuration for frontend |
| 80 |
* |
| 81 |
* @param array<object> $formFields Array of form field objects to determine required config |
| 82 |
* @return array<string, mixed> |
| 83 |
*/ |
| 84 |
public function getFrontendSecurityConfig(array $formFields): array |
| 85 |
{ |
| 86 |
$provider = $this->getCaptchaProviderForForm($formFields); |
| 87 |
|
| 88 |
if ($provider === CaptchaProviderResolver::PROVIDER_NONE) { |
| 89 |
return [ |
| 90 |
'captcha' => [ |
| 91 |
'enabled' => false, |
| 92 |
'provider' => CaptchaProviderResolver::PROVIDER_NONE |
| 93 |
] |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
$config = [ |
| 98 |
'captcha' => [ |
| 99 |
'enabled' => true, |
| 100 |
'provider' => $provider |
| 101 |
] |
| 102 |
]; |
| 103 |
|
| 104 |
// Add provider-specific configuration |
| 105 |
switch ($provider) { |
| 106 |
case CaptchaProviderResolver::PROVIDER_RECAPTCHA: |
| 107 |
$service = $this->securityServiceFactory->createActiveCaptchaService($formFields); |
| 108 |
$config['captcha']['recaptcha'] = [ |
| 109 |
'configured' => false, |
| 110 |
'type' => 'v2', |
| 111 |
'siteKey' => '', |
| 112 |
'scriptUrl' => '', |
| 113 |
'size' => 'normal' |
| 114 |
]; |
| 115 |
if ($service && $service->isConfigured()) { |
| 116 |
$recaptchaConfig = $service->getFrontendConfig( |
| 117 |
$service->getType(), |
| 118 |
$service->getSiteKey() |
| 119 |
); |
| 120 |
// Add the configured flag that frontend expects |
| 121 |
$recaptchaConfig['configured'] = true; |
| 122 |
$config['captcha']['recaptcha'] = $recaptchaConfig; |
| 123 |
} |
| 124 |
break; |
| 125 |
|
| 126 |
// Future providers can be added here |
| 127 |
case CaptchaProviderResolver::PROVIDER_HCAPTCHA: |
| 128 |
case CaptchaProviderResolver::PROVIDER_TURNSTILE: |
| 129 |
// To be implemented |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
return $config; |
| 134 |
} |
| 135 |
} |
| 136 |
|