Skip to content

replace deprecated spaceless tag in template #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
bf2e71e
replace deprecated spaceless tag in template
cyrilverloop Nov 8, 2019
5732d0e
Make TreeBuilder use compatible with Symfony 5.x
carlos-mg89 Dec 9, 2020
70c9add
Merge pull request #1 from carlos-mg89/feature-symfony-5-support
carlos-mg89 Dec 9, 2020
8884d18
Updated composer.json so this repository becomes a different package …
carlos-mg89 Dec 9, 2020
7fb887a
Merge pull request #2 from carlos-mg89/feature-make-repository-as-new…
carlos-mg89 Dec 9, 2020
57f1054
Changed author's email
carlos-mg89 Dec 9, 2020
baba7b2
Merge pull request #3 from carlos-mg89/feature-make-repository-as-new…
carlos-mg89 Dec 9, 2020
e5e1b56
Update README.md
carlos-mg89 Dec 9, 2020
05d6af1
Merge pull request #4 from carlos-mg89/feature-clarify-composer-insta…
carlos-mg89 Dec 9, 2020
97ed51d
Removed deprecated Twig spaceless tag
carlos-mg89 Dec 9, 2020
11f9e47
Merge pull request #5 from carlos-mg89/feature-removed-deprecated-twi…
carlos-mg89 Dec 9, 2020
9b863d3
Update CaptchaHandlerController.php
carlos-mg89 Dec 9, 2020
bedc1f3
Changed base controller
carlos-mg89 Dec 9, 2020
c39e1e8
Merge pull request #6 from carlos-mg89/feature-changed-base-controlle…
carlos-mg89 Dec 9, 2020
da77378
Merge pull request #7 from carlos-mg89/feature-update-base-controller
carlos-mg89 Dec 9, 2020
d74245b
Fix routing with new controller definition syntax
carlos-mg89 Dec 9, 2020
9eb279c
Merge pull request #8 from carlos-mg89/feature-controller-definition-…
carlos-mg89 Dec 9, 2020
73a216d
Update relative library path
carlos-mg89 Dec 9, 2020
9372d5d
Merge pull request #9 from carlos-mg89/feature-update-library-path
carlos-mg89 Dec 9, 2020
03cef19
Changed the method to obtain the library Path
carlos-mg89 Dec 9, 2020
28aeb20
Merge pull request #10 from carlos-mg89/feature-fix-library-path-getter
carlos-mg89 Dec 9, 2020
e612232
Update the packagist data with the new package
carlos-mg89 Dec 9, 2020
3d58e3e
Merge pull request #11 from carlos-mg89/feature-update-packagist-data…
carlos-mg89 Dec 9, 2020
5fa4814
Added some imports and inlined some code for simplicity
carlos-mg89 Dec 9, 2020
401a229
Updated controllers and classes so the used ContainerInterface is the…
carlos-mg89 Dec 9, 2020
126981c
Update README.md
carlos-mg89 Dec 9, 2020
fd8fed0
Added parameter in services.yaml so the package user doesn't have to
carlos-mg89 Dec 9, 2020
eb3531c
Merge branch 'master' of https://github.com/carlos-mg89/symfony-captc…
carlos-mg89 Dec 9, 2020
19cd66f
Updated README so we remove the unnecessary parameter
carlos-mg89 Dec 9, 2020
e3950e2
Include all necessary wiring information on the services.yaml of the …
carlos-mg89 Dec 9, 2020
dbe25f4
Reverted change that makes mandatory to edit the project's services.y…
carlos-mg89 Dec 9, 2020
fdee073
Merge branch 'master' into twig
cyrilverloop Dec 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 53 additions & 48 deletions Controller/CaptchaHandlerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

namespace Captcha\Bundle\CaptchaBundle\Controller;

use BDC_CaptchaBase;
use BDC_CaptchaHttpCommand;
use BDC_CaptchaScriptsHelper;
use BDC_CryptoHelper;
use BDC_HttpHelper;
use BDC_StringHelper;
use Captcha\Bundle\CaptchaBundle\Support\Path;
use Captcha\Bundle\CaptchaBundle\Support\LibraryLoader;
use Captcha\Bundle\CaptchaBundle\Helpers\BotDetectCaptchaHelper;
use SoundRegenerationMode;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class CaptchaHandlerController extends Controller
class CaptchaHandlerController extends AbstractController
{
/**
* @var object
Expand All @@ -33,31 +40,31 @@ public function indexAction()
}

$commandString = $this->getUrlParameter('get');
if (!\BDC_StringHelper::HasValue($commandString)) {
\BDC_HttpHelper::BadRequest('command');
if (!BDC_StringHelper::HasValue($commandString)) {
BDC_HttpHelper::BadRequest('command');
}

$commandString = \BDC_StringHelper::Normalize($commandString);
$command = \BDC_CaptchaHttpCommand::FromQuerystring($commandString);
$commandString = BDC_StringHelper::Normalize($commandString);
$command = BDC_CaptchaHttpCommand::FromQuerystring($commandString);
$responseBody = '';
switch ($command) {
case \BDC_CaptchaHttpCommand::GetImage:
case BDC_CaptchaHttpCommand::GetImage:
$responseBody = $this->getImage();
break;
case \BDC_CaptchaHttpCommand::GetSound:
case BDC_CaptchaHttpCommand::GetSound:
$responseBody = $this->getSound();
break;
case \BDC_CaptchaHttpCommand::GetValidationResult:
case BDC_CaptchaHttpCommand::GetValidationResult:
$responseBody = $this->getValidationResult();
break;
case \BDC_CaptchaHttpCommand::GetScriptInclude:
case BDC_CaptchaHttpCommand::GetScriptInclude:
$responseBody = $this->getScriptInclude();
break;
case \BDC_CaptchaHttpCommand::GetP:
case BDC_CaptchaHttpCommand::GetP:
$responseBody = $this->getP();
break;
default:
\BDC_HttpHelper::BadRequest('command');
BDC_HttpHelper::BadRequest('command');
break;
}

Expand Down Expand Up @@ -105,7 +112,7 @@ public function getResourceContents()
throw new BadRequestHttpException('Invalid file name.');
}

$resourcePath = realpath(Path::getPublicDirPathInLibrary($this->container) . $filename);
$resourcePath = realpath(Path::getPublicDirPathInLibrary() . $filename);

if (!is_file($resourcePath)) {
throw new BadRequestHttpException(sprintf('File "%s" could not be found.', $filename));
Expand All @@ -129,20 +136,20 @@ public function getResourceContents()
public function getImage()
{
if (is_null($this->captcha)) {
\BDC_HttpHelper::BadRequest('captcha');
BDC_HttpHelper::BadRequest('captcha');
}

// identifier of the particular Captcha object instance
$instanceId = $this->getInstanceId();
if (is_null($instanceId)) {
\BDC_HttpHelper::BadRequest('instance');
BDC_HttpHelper::BadRequest('instance');
}

// image generation invalidates sound cache, if any
$this->clearSoundData($instanceId);

// response headers
\BDC_HttpHelper::DisallowCache();
BDC_HttpHelper::DisallowCache();

// response MIME type & headers
$mimeType = $this->captcha->CaptchaBase->ImageMimeType;
Expand All @@ -167,34 +174,34 @@ public function getImage()
public function getSound()
{
if (is_null($this->captcha)) {
\BDC_HttpHelper::BadRequest('captcha');
BDC_HttpHelper::BadRequest('captcha');
}

// identifier of the particular Captcha object instance
$instanceId = $this->getInstanceId();
if (is_null($instanceId)) {
\BDC_HttpHelper::BadRequest('instance');
BDC_HttpHelper::BadRequest('instance');
}

$soundBytes = $this->getSoundData($this->captcha, $instanceId);

if (is_null($soundBytes)) {
\BDC_HttpHelper::BadRequest('Please reload the form page before requesting another Captcha sound');
BDC_HttpHelper::BadRequest('Please reload the form page before requesting another Captcha sound');
exit;
}

$totalSize = strlen($soundBytes);

// response headers
\BDC_HttpHelper::SmartDisallowCache();
BDC_HttpHelper::SmartDisallowCache();

// response MIME type & headers
$mimeType = $this->captcha->CaptchaBase->SoundMimeType;
header("Content-Type: {$mimeType}");
header('Content-Transfer-Encoding: binary');

if (!array_key_exists('d', $_GET)) { // javascript player not used, we send the file directly as a download
$downloadId = \BDC_CryptoHelper::GenerateGuid();
$downloadId = BDC_CryptoHelper::GenerateGuid();
header("Content-Disposition: attachment; filename=captcha_{$downloadId}.wav");
}

Expand All @@ -210,7 +217,7 @@ public function getSound()
// end of sound playback, cleanup and tell AppleCoreMedia to stop requesting
// invalid "bytes=rangeEnd-rangeEnd" ranges in an infinite(?) loop
if ($rangeStart == $rangeEnd || $rangeEnd > $totalSize) {
\BDC_HttpHelper::BadRequest('invalid byte range');
BDC_HttpHelper::BadRequest('invalid byte range');
}

$rangeBytes = substr($soundBytes, $rangeStart, $rangeSize);
Expand Down Expand Up @@ -238,7 +245,7 @@ public function getSound()
public function getSoundData($p_Captcha, $p_InstanceId)
{
$shouldCache = (
($p_Captcha->SoundRegenerationMode == \SoundRegenerationMode::None) || // no sound regeneration allowed, so we must cache the first and only generated sound
($p_Captcha->SoundRegenerationMode == SoundRegenerationMode::None) || // no sound regeneration allowed, so we must cache the first and only generated sound
$this->detectIosRangeRequest() // keep the same Captcha sound across all chunked iOS requests
);

Expand Down Expand Up @@ -286,20 +293,20 @@ private function clearSoundData($p_InstanceId)
private function detectIosRangeRequest()
{
if (array_key_exists('HTTP_RANGE', $_SERVER) &&
\BDC_StringHelper::HasValue($_SERVER['HTTP_RANGE'])) {
BDC_StringHelper::HasValue($_SERVER['HTTP_RANGE'])) {

// Safari on MacOS and all browsers on <= iOS 10.x
if (array_key_exists('HTTP_X_PLAYBACK_SESSION_ID', $_SERVER) &&
\BDC_StringHelper::HasValue($_SERVER['HTTP_X_PLAYBACK_SESSION_ID'])) {
BDC_StringHelper::HasValue($_SERVER['HTTP_X_PLAYBACK_SESSION_ID'])) {
return true;
}

$userAgent = array_key_exists('HTTP_USER_AGENT', $_SERVER) ? $_SERVER['HTTP_USER_AGENT'] : null;

// all browsers on iOS 11.x and later
if(\BDC_StringHelper::HasValue($userAgent)) {
$userAgentLC = \BDC_StringHelper::Lowercase($userAgent);
if (\BDC_StringHelper::Contains($userAgentLC, "like mac os") || \BDC_StringHelper::Contains($userAgentLC, "like macos")) {
if(BDC_StringHelper::HasValue($userAgent)) {
$userAgentLC = BDC_StringHelper::Lowercase($userAgent);
if (BDC_StringHelper::Contains($userAgentLC, "like mac os") || BDC_StringHelper::Contains($userAgentLC, "like macos")) {
return true;
}
}
Expand All @@ -311,7 +318,7 @@ private function getSoundByteRange()
{
// chunked requests must include the desired byte range
$rangeStr = $_SERVER['HTTP_RANGE'];
if (!\BDC_StringHelper::HasValue($rangeStr)) {
if (!BDC_StringHelper::HasValue($rangeStr)) {
return;
}

Expand All @@ -328,7 +335,7 @@ private function detectFakeRangeRequest()
$detected = false;
if (array_key_exists('HTTP_RANGE', $_SERVER)) {
$rangeStr = $_SERVER['HTTP_RANGE'];
if (\BDC_StringHelper::HasValue($rangeStr) &&
if (BDC_StringHelper::HasValue($rangeStr) &&
preg_match('/bytes=0-$/', $rangeStr)) {
$detected = true;
}
Expand All @@ -339,18 +346,18 @@ private function detectFakeRangeRequest()
/**
* The client requests the Captcha validation result (used for Ajax Captcha validation).
*
* @return json
* @return string
*/
public function getValidationResult()
{
if (is_null($this->captcha)) {
\BDC_HttpHelper::BadRequest('captcha');
BDC_HttpHelper::BadRequest('captcha');
}

// identifier of the particular Captcha object instance
$instanceId = $this->getInstanceId();
if (is_null($instanceId)) {
\BDC_HttpHelper::BadRequest('instance');
BDC_HttpHelper::BadRequest('instance');
}

$mimeType = 'application/json';
Expand All @@ -365,30 +372,28 @@ public function getValidationResult()
$result = $this->captcha->AjaxValidate($userInput, $instanceId);
$this->captcha->CaptchaBase->Save();
}
$resultJson = $this->getJsonValidationResult($result);

return $resultJson;
return $this->getJsonValidationResult($result);
}

public function getScriptInclude()
{
// saved data for the specified Captcha object in the application
if (is_null($this->captcha)) {
\BDC_HttpHelper::BadRequest('captcha');
BDC_HttpHelper::BadRequest('captcha');
}

// identifier of the particular Captcha object instance
$instanceId = $this->getInstanceId();
if (is_null($instanceId)) {
\BDC_HttpHelper::BadRequest('instance');
BDC_HttpHelper::BadRequest('instance');
}

// response MIME type & headers
header('Content-Type: text/javascript');
header('X-Robots-Tag: noindex, nofollow, noarchive, nosnippet');

// 1. load BotDetect script
$resourcePath = realpath(Path::getPublicDirPathInLibrary($this->container) . 'bdc-traditional-api-script-include.js');
$resourcePath = realpath(Path::getPublicDirPathInLibrary() . 'bdc-traditional-api-script-include.js');

if (!is_file($resourcePath)) {
throw new BadRequestHttpException(sprintf('File "%s" could not be found.', $resourcePath));
Expand All @@ -397,12 +402,12 @@ public function getScriptInclude()
$script = file_get_contents($resourcePath);

// 2. load BotDetect Init script
$script .= \BDC_CaptchaScriptsHelper::GetInitScriptMarkup($this->captcha, $instanceId);
$script .= BDC_CaptchaScriptsHelper::GetInitScriptMarkup($this->captcha, $instanceId);

// add remote scripts if enabled
if ($this->captcha->RemoteScriptEnabled) {
$script .= "\r\n";
$script .= \BDC_CaptchaScriptsHelper::GetRemoteScript($this->captcha);
$script .= BDC_CaptchaScriptsHelper::GetRemoteScript($this->captcha);
}

return $script;
Expand All @@ -414,8 +419,8 @@ public function getScriptInclude()
private function getInstanceId()
{
$instanceId = $this->getUrlParameter('t');
if (!\BDC_StringHelper::HasValue($instanceId) ||
!\BDC_CaptchaBase::IsValidInstanceId($instanceId)
if (!BDC_StringHelper::HasValue($instanceId) ||
!BDC_CaptchaBase::IsValidInstanceId($instanceId)
) {
return;
}
Expand Down Expand Up @@ -450,12 +455,12 @@ private function getUserInput()
/**
* Encodes the Captcha validation result in a simple JSON wrapper.
*
* @param $result
* @return string
*/
private function getJsonValidationResult($result)
{
$resultStr = ($result ? 'true': 'false');
return $resultStr;
return ($result ? 'true': 'false');
}

/**
Expand All @@ -478,13 +483,13 @@ private function getUrlParameter($param)
public function getP()
{
if (is_null($this->captcha)) {
\BDC_HttpHelper::BadRequest('captcha');
BDC_HttpHelper::BadRequest('captcha');
}

// identifier of the particular Captcha object instance
$instanceId = $this->getInstanceId();
if (is_null($instanceId)) {
\BDC_HttpHelper::BadRequest('instance');
BDC_HttpHelper::BadRequest('instance');
}

// create new one
Expand All @@ -497,7 +502,7 @@ public function getP()
// response MIME type & headers
header('Content-Type: application/json');
header('X-Robots-Tag: noindex, nofollow, noarchive, nosnippet');
\BDC_HttpHelper::SmartDisallowCache();
BDC_HttpHelper::SmartDisallowCache();

return $response;
}
Expand Down
Loading