PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.7
Yatra – Travel Booking & Tour Operator Software v3.0.7
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / app / Services / RecaptchaService.php

RecaptchaService.php in Yatra – Travel Booking & Tour Operator Software 3.0.7, at app/Services/RecaptchaService.php

186 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * reCAPTCHA Service
4 *
5 * Handles Google reCAPTCHA verification
6 *
7 * @package Yatra\Services
8 * @since 3.0.0
9 */
10
11 declare(strict_types=1);
12
13 namespace Yatra\Services;
14
15 class RecaptchaService
16 {
17 /**
18 * Verify reCAPTCHA response
19 *
20 * @param string $response reCAPTCHA response token
21 * @param string|null $remoteIp User's IP address
22 * @return array Verification result
23 */
24 public static function verify(string $response, ?string $remoteIp = null): array
25 {
26 // Check if reCAPTCHA is enabled
27 if (!SettingsService::isEnabled('recaptcha_enabled')) {
28 return [
29 'success' => true,
30 'message' => 'reCAPTCHA is disabled'
31 ];
32 }
33
34 $secret_key = SettingsService::getString('recaptcha_secret_key', '');
35
36 if (empty($secret_key)) {
37 return [
38 'success' => false,
39 'message' => __('reCAPTCHA secret key is not configured', 'yatra')
40 ];
41 }
42
43 if (empty($response)) {
44 return [
45 'success' => false,
46 'message' => __('Please complete the reCAPTCHA verification', 'yatra')
47 ];
48 }
49
50 // Prepare verification request
51 $verify_url = 'https://www.google.com/recaptcha/api/siteverify';
52 $data = [
53 'secret' => $secret_key,
54 'response' => $response,
55 ];
56
57 if ($remoteIp) {
58 $data['remoteip'] = $remoteIp;
59 }
60
61 // Send verification request
62 $response = wp_remote_post($verify_url, [
63 'body' => $data,
64 'timeout' => 10,
65 ]);
66
67 if (is_wp_error($response)) {
68 return [
69 'success' => false,
70 'message' => __('reCAPTCHA verification failed: ', 'yatra') . $response->get_error_message()
71 ];
72 }
73
74 $body = wp_remote_retrieve_body($response);
75 $result = json_decode($body, true);
76
77 if (!isset($result['success'])) {
78 return [
79 'success' => false,
80 'message' => __('Invalid reCAPTCHA response', 'yatra')
81 ];
82 }
83
84 if (!$result['success']) {
85 $error_codes = $result['error-codes'] ?? [];
86 $error_message = self::getErrorMessage($error_codes);
87
88 return [
89 'success' => false,
90 'message' => $error_message
91 ];
92 }
93
94 return [
95 'success' => true,
96 'message' => __('reCAPTCHA verification successful', 'yatra'),
97 'score' => $result['score'] ?? null,
98 'action' => $result['action'] ?? null,
99 ];
100 }
101
102 /**
103 * Get user-friendly error message from error codes
104 *
105 * @param array $errorCodes Error codes from reCAPTCHA
106 * @return string Error message
107 */
108 private static function getErrorMessage(array $errorCodes): string
109 {
110 if (empty($errorCodes)) {
111 return __('reCAPTCHA verification failed', 'yatra');
112 }
113
114 $messages = [
115 'missing-input-secret' => __('The secret parameter is missing', 'yatra'),
116 'invalid-input-secret' => __('The secret parameter is invalid or malformed', 'yatra'),
117 'missing-input-response' => __('The response parameter is missing', 'yatra'),
118 'invalid-input-response' => __('The response parameter is invalid or malformed', 'yatra'),
119 'bad-request' => __('The request is invalid or malformed', 'yatra'),
120 'timeout-or-duplicate' => __('The response is no longer valid: either is too old or has been used previously', 'yatra'),
121 ];
122
123 $errorCode = $errorCodes[0];
124 return $messages[$errorCode] ?? __('reCAPTCHA verification failed', 'yatra');
125 }
126
127 /**
128 * Get reCAPTCHA site key
129 *
130 * @return string Site key
131 */
132 public static function getSiteKey(): string
133 {
134 return SettingsService::getString('recaptcha_site_key', '');
135 }
136
137 /**
138 * Check if reCAPTCHA is enabled
139 *
140 * @return bool
141 */
142 public static function isEnabled(): bool
143 {
144 return SettingsService::isEnabled('recaptcha_enabled') &&
145 !empty(self::getSiteKey()) &&
146 !empty(SettingsService::getString('recaptcha_secret_key', ''));
147 }
148
149 /**
150 * Render reCAPTCHA widget HTML
151 *
152 * @return string HTML for reCAPTCHA widget
153 */
154 public static function renderWidget(): string
155 {
156 if (!self::isEnabled()) {
157 return '';
158 }
159
160 $site_key = self::getSiteKey();
161
162 return sprintf(
163 '<div class="g-recaptcha" data-sitekey="%s"></div>',
164 esc_attr($site_key)
165 );
166 }
167
168 /**
169 * Enqueue reCAPTCHA script
170 */
171 public static function enqueueScript(): void
172 {
173 if (!self::isEnabled()) {
174 return;
175 }
176
177 wp_enqueue_script(
178 'google-recaptcha',
179 'https://www.google.com/recaptcha/api.js',
180 [],
181 null,
182 true
183 );
184 }
185 }
186