Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
no-captcha
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
CENTER-TBI
no-captcha
Commits
821a256c
Unverified
Commit
821a256c
authored
Aug 10, 2018
by
Nguyen Van Anh
Committed by
GitHub
Aug 10, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #114 from nstapelbroek/feature/invisible-recaptcha-challenge-button
Feature/invisible recaptcha challenge button
parents
f6c3a006
f138a6fd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
5 deletions
+80
-5
README.md
README.md
+8
-0
src/NoCaptcha.php
src/NoCaptcha.php
+54
-3
tests/NoCaptchaTest.php
tests/NoCaptchaTest.php
+18
-2
No files found.
README.md
View file @
821a256c
...
@@ -84,6 +84,14 @@ With [custom attributes](https://developers.google.com/recaptcha/docs/display#re
...
@@ -84,6 +84,14 @@ With [custom attributes](https://developers.google.com/recaptcha/docs/display#re
{
!!
NoCaptcha
::
display
([
'data-theme'
=>
'dark'
])
!!
}
{
!!
NoCaptcha
::
display
([
'data-theme'
=>
'dark'
])
!!
}
```
```
Invisible reCAPTCHA using a
[
submit button
](
https://developers.google.com/recaptcha/docs/invisible
)
:
```
php
{
!!
NoCaptcha
::
displaySubmit
(
'my-form-id'
,
'submit now!'
,
[
'data-theme'
=>
'dark'
])
!!
}
```
Notice that the id of the form is required in this method to let the autogenerated
callback submit the form on a successful captcha verification.
#### Validation
#### Validation
Add
`'g-recaptcha-response' => 'required|captcha'`
to rules array :
Add
`'g-recaptcha-response' => 'required|captcha'`
to rules array :
...
...
src/NoCaptcha.php
View file @
821a256c
...
@@ -53,14 +53,47 @@ class NoCaptcha
...
@@ -53,14 +53,47 @@ class NoCaptcha
/**
/**
* Render HTML captcha.
* Render HTML captcha.
*
*
* @param array
$attributes
* @param array $attributes
*
*
* @return string
* @return string
*/
*/
public
function
display
(
$attributes
=
[])
public
function
display
(
$attributes
=
[])
{
{
$attributes
[
'data-sitekey'
]
=
$this
->
sitekey
;
$attributes
=
$this
->
prepareAttributes
(
$attributes
);
return
'<div class="g-recaptcha"'
.
$this
->
buildAttributes
(
$attributes
)
.
'></div>'
;
return
'<div'
.
$this
->
buildAttributes
(
$attributes
)
.
'></div>'
;
}
/**
* @see display()
*/
public
function
displayWidget
(
$attributes
=
[])
{
return
$this
->
display
(
$attributes
);
}
/**
* Display a Invisible reCAPTCHA by embedding a callback into a form submit button.
*
* @param string $formIdentifier the html ID of the form that should be submitted.
* @param string $text the text inside the form button
* @param array $attributes array of additional html elements
*
* @return string
*/
public
function
displaySubmit
(
$formIdentifier
,
$text
=
'submit'
,
$attributes
=
[])
{
$functionName
=
'onSubmit'
.
str_replace
([
'-'
,
'='
,
'\''
,
'"'
,
'<'
,
'>'
,
'`'
],
''
,
$formIdentifier
);
$attributes
[
'data-callback'
]
=
$functionName
;
$attributes
=
$this
->
prepareAttributes
(
$attributes
);
$button
=
sprintf
(
'<button%s><span>%s</span></button>'
,
$this
->
buildAttributes
(
$attributes
),
$text
);
$javascript
=
sprintf
(
'<script>function %s(){document.getElementById("%s").submit();}</script>'
,
$functionName
,
$formIdentifier
);
return
$button
.
$javascript
;
}
}
/**
/**
...
@@ -171,6 +204,24 @@ class NoCaptcha
...
@@ -171,6 +204,24 @@ class NoCaptcha
return
json_decode
(
$response
->
getBody
(),
true
);
return
json_decode
(
$response
->
getBody
(),
true
);
}
}
/**
* Prepare HTML attributes and assure that the correct classes and attributes for captcha are inserted.
*
* @param array $attributes
*
* @return array
*/
protected
function
prepareAttributes
(
array
$attributes
)
{
$attributes
[
'data-sitekey'
]
=
$this
->
sitekey
;
if
(
!
isset
(
$attributes
[
'class'
]))
{
$attributes
[
'class'
]
=
''
;
}
$attributes
[
'class'
]
=
trim
(
'g-recaptcha '
.
$attributes
[
'class'
]);
return
$attributes
;
}
/**
/**
* Build HTML attributes.
* Build HTML attributes.
*
*
...
...
tests/NoCaptchaTest.php
View file @
821a256c
...
@@ -4,6 +4,9 @@ use Anhskohbo\NoCaptcha\NoCaptcha;
...
@@ -4,6 +4,9 @@ use Anhskohbo\NoCaptcha\NoCaptcha;
class
NoCaptchaTest
extends
PHPUnit_Framework_TestCase
class
NoCaptchaTest
extends
PHPUnit_Framework_TestCase
{
{
/**
* @var NoCaptcha
*/
private
$captcha
;
private
$captcha
;
public
function
setUp
()
public
function
setUp
()
...
@@ -29,10 +32,23 @@ class NoCaptchaTest extends PHPUnit_Framework_TestCase
...
@@ -29,10 +32,23 @@ class NoCaptchaTest extends PHPUnit_Framework_TestCase
{
{
$this
->
assertTrue
(
$this
->
captcha
instanceof
NoCaptcha
);
$this
->
assertTrue
(
$this
->
captcha
instanceof
NoCaptcha
);
$simple
=
'<div
class="g-recaptcha" data-sitekey="{site-key}
"></div>'
;
$simple
=
'<div
data-sitekey="{site-key}" class="g-recaptcha
"></div>'
;
$withAttrs
=
'<div
class="g-recaptcha" data-theme="light" data-sitekey="{site-key}
"></div>'
;
$withAttrs
=
'<div
data-theme="light" data-sitekey="{site-key}" class="g-recaptcha
"></div>'
;
$this
->
assertEquals
(
$this
->
captcha
->
display
(),
$simple
);
$this
->
assertEquals
(
$this
->
captcha
->
display
(),
$simple
);
$this
->
assertEquals
(
$this
->
captcha
->
display
([
'data-theme'
=>
'light'
]),
$withAttrs
);
$this
->
assertEquals
(
$this
->
captcha
->
display
([
'data-theme'
=>
'light'
]),
$withAttrs
);
}
}
public
function
testdisplaySubmit
()
{
$this
->
assertTrue
(
$this
->
captcha
instanceof
NoCaptcha
);
$javascript
=
'<script>function onSubmittest(){document.getElementById("test").submit();}</script>'
;
$simple
=
'<button data-callback="onSubmittest" data-sitekey="{site-key}" class="g-recaptcha"><span>submit</span></button>'
;
$withAttrs
=
'<button data-theme="light" class="g-recaptcha 123" data-callback="onSubmittest" data-sitekey="{site-key}"><span>submit123</span></button>'
;
$this
->
assertEquals
(
$this
->
captcha
->
displaySubmit
(
'test'
),
$simple
.
$javascript
);
$withAttrsResult
=
$this
->
captcha
->
displaySubmit
(
'test'
,
'submit123'
,[
'data-theme'
=>
'light'
,
'class'
=>
'123'
]);
$this
->
assertEquals
(
$withAttrsResult
,
$withAttrs
.
$javascript
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment