getMessage()); } } /** * Test encryption/decryption with debug output * * @param string $plaintext * @param string $password * @return array Debug information */ public static function debugEncryption(string $plaintext, string $password): array { try { $encrypted = self::encrypt($plaintext, $password); $decrypted = self::decrypt($encrypted, $password); return [ 'success' => true, 'plaintext' => $plaintext, 'encrypted' => $encrypted, 'decrypted' => $decrypted, 'match' => ($plaintext === $decrypted), 'encrypted_length' => strlen($encrypted), 'binary_length' => strlen(base64_decode($encrypted)) ]; } catch (\Exception $e) { return [ 'success' => false, 'error' => $e->getMessage(), 'plaintext' => $plaintext ]; } } /** * Validates URL for security * * @param string $url * @return bool */ public static function validateUrl(string $url): bool { $allowedProtocols = ['http', 'https', 'mailto']; $maxLength = 2048; if (strlen($url) > $maxLength) { return false; } $parsedUrl = parse_url($url); if (!$parsedUrl || !isset($parsedUrl['scheme'])) { return false; } if (!in_array($parsedUrl['scheme'], $allowedProtocols)) { return false; } // Additional validation for mailto URLs if ($parsedUrl['scheme'] === 'mailto') { $email = $parsedUrl['path'] ?? ''; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { return false; } } return true; } }