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
1c6c59ef
Commit
1c6c59ef
authored
Sep 07, 2015
by
Nguyen Van Anh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PSR2
parent
ae2c86af
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
203 additions
and
205 deletions
+203
-205
src/Facades/NoCaptcha.php
src/Facades/NoCaptcha.php
+14
-10
src/NoCaptcha.php
src/NoCaptcha.php
+130
-124
src/NoCaptchaServiceProvider.php
src/NoCaptchaServiceProvider.php
+57
-66
src/config/captcha.php
src/config/captcha.php
+2
-5
No files found.
src/Facades/NoCaptcha.php
View file @
1c6c59ef
<?php
namespace
Anhskohbo\NoCaptcha\Facades
;
<?php
use
Illuminate\Support\Facades\Facade
;
class
NoCaptcha
extends
Facade
{
namespace
Anhskohbo\NoCaptcha\Facades
;
/**
* Get the registered name of the component.
*
* @return string
*/
protected
static
function
getFacadeAccessor
()
{
return
'captcha'
;
}
use
Illuminate\Support\Facades\Facade
;
class
NoCaptcha
extends
Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected
static
function
getFacadeAccessor
()
{
return
'captcha'
;
}
}
src/NoCaptcha.php
View file @
1c6c59ef
<?php
namespace
Anhskohbo\NoCaptcha
;
<?php
use
Symfony\Component\HttpFoundation\Request
;
namespace
Anhskohbo\NoCaptcha
;
class
NoCaptcha
{
const
CLIENT_API
=
'https://www.google.com/recaptcha/api.js'
;
const
VERIFY_URL
=
'https://www.google.com/recaptcha/api/siteverify'
;
/**
* //
*
* @var string
*/
protected
$secret
;
/**
* //
*
* @var string
*/
protected
$sitekey
;
/**
* //
*
* @param string $secret
* @param string $sitekey
*/
public
function
__construct
(
$secret
,
$sitekey
)
{
$this
->
secret
=
$secret
;
$this
->
sitekey
=
$sitekey
;
}
/**
* //
*
* @return string
*/
public
function
display
(
$attributes
=
[],
$lang
=
null
)
{
$attributes
[
'data-sitekey'
]
=
$this
->
sitekey
;
$html
=
'<script src="'
.
$this
->
getJsLink
(
$lang
)
.
'" async defer></script>'
.
"
\n
"
;
$html
.=
'<div class="g-recaptcha"'
.
$this
->
buildAttributes
(
$attributes
)
.
'></div>'
;
return
$html
;
}
/**
* //
*
* @param string $response
* @param string $clientIp
* @return bool
*/
public
function
verifyResponse
(
$response
,
$clientIp
=
null
)
{
if
(
empty
(
$response
))
return
false
;
$response
=
$this
->
sendRequestVerify
([
'secret'
=>
$this
->
secret
,
'response'
=>
$response
,
'remoteip'
=>
$clientIp
]);
return
isset
(
$response
[
'success'
])
&&
$response
[
'success'
]
===
true
;
}
/**
* //
*
* @param Request $request
* @return bool
*/
public
function
verifyRequest
(
Request
$request
)
{
return
$this
->
verifyResponse
(
$request
->
get
(
'g-recaptcha-response'
),
$request
->
getClientIp
()
);
}
/**
* //
*
* @return string
*/
public
function
getJsLink
(
$lang
=
null
)
{
return
$lang
?
static
::
CLIENT_API
.
'?hl='
.
$lang
:
static
::
CLIENT_API
;
}
/**
* //
*
* @param array $query
* @return array
*/
protected
function
sendRequestVerify
(
array
$query
=
[])
{
$link
=
static
::
VERIFY_URL
.
'?'
.
http_build_query
(
$query
);
$response
=
file_get_contents
(
$link
);
return
json_decode
(
$response
,
true
);
}
/**
* //
*
* @param array $attributes
* @return string
*/
protected
function
buildAttributes
(
array
$attributes
)
{
$html
=
[];
foreach
(
$attributes
as
$key
=>
$value
)
{
$html
[]
=
$key
.
'="'
.
$value
.
'"'
;
}
return
count
(
$html
)
?
' '
.
implode
(
' '
,
$html
)
:
''
;
}
use
Symfony\Component\HttpFoundation\Request
;
class
NoCaptcha
{
const
CLIENT_API
=
'https://www.google.com/recaptcha/api.js'
;
const
VERIFY_URL
=
'https://www.google.com/recaptcha/api/siteverify'
;
/**
* The recaptcha secret key.
*
* @var string
*/
protected
$secret
;
/**
* The recaptcha sitekey key.
*
* @var string
*/
protected
$sitekey
;
/**
* NoCaptcha.
*
* @param string $secret
* @param string $sitekey
*/
public
function
__construct
(
$secret
,
$sitekey
)
{
$this
->
secret
=
$secret
;
$this
->
sitekey
=
$sitekey
;
}
/**
* Render HTML captcha.
*
* @return string
*/
public
function
display
(
$attributes
=
[],
$lang
=
null
)
{
$attributes
[
'data-sitekey'
]
=
$this
->
sitekey
;
$html
=
'<script src="'
.
$this
->
getJsLink
(
$lang
)
.
'" async defer></script>'
.
"
\n
"
;
$html
.=
'<div class="g-recaptcha"'
.
$this
->
buildAttributes
(
$attributes
)
.
'></div>'
;
return
$html
;
}
/**
* Verify no-captcha response.
*
* @param string $response
* @param string $clientIp
*
* @return bool
*/
public
function
verifyResponse
(
$response
,
$clientIp
=
null
)
{
if
(
empty
(
$response
))
{
return
false
;
}
$response
=
$this
->
sendRequestVerify
([
'secret'
=>
$this
->
secret
,
'response'
=>
$response
,
'remoteip'
=>
$clientIp
,
]);
return
isset
(
$response
[
'success'
])
&&
$response
[
'success'
]
===
true
;
}
/**
* Verify no-captcha response by Symfony Request.
*
* @param Request $request
*
* @return bool
*/
public
function
verifyRequest
(
Request
$request
)
{
return
$this
->
verifyResponse
(
$request
->
get
(
'g-recaptcha-response'
),
$request
->
getClientIp
()
);
}
/**
* Get recaptcha js link.
*
* @return string
*/
public
function
getJsLink
(
$lang
=
null
)
{
return
$lang
?
static
::
CLIENT_API
.
'?hl='
.
$lang
:
static
::
CLIENT_API
;
}
/**
* Send verify request.
*
* @param array $query
*
* @return array
*/
protected
function
sendRequestVerify
(
array
$query
=
[])
{
$link
=
static
::
VERIFY_URL
.
'?'
.
http_build_query
(
$query
);
$response
=
file_get_contents
(
$link
);
return
json_decode
(
$response
,
true
);
}
/**
* Build HTML attributes.
*
* @param array $attributes
*
* @return string
*/
protected
function
buildAttributes
(
array
$attributes
)
{
$html
=
[];
foreach
(
$attributes
as
$key
=>
$value
)
{
$html
[]
=
$key
.
'="'
.
$value
.
'"'
;
}
return
count
(
$html
)
?
' '
.
implode
(
' '
,
$html
)
:
''
;
}
}
src/NoCaptchaServiceProvider.php
View file @
1c6c59ef
<?php
namespace
Anhskohbo\NoCaptcha
;
<?php
use
Illuminate\Support\ServiceProvider
;
namespace
Anhskohbo\NoCaptcha
;
class
NoCaptchaServiceProvider
extends
ServiceProvider
{
use
Illuminate\Support\ServiceProvider
;
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected
$defer
=
false
;
class
NoCaptchaServiceProvider
extends
ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected
$defer
=
false
;
/**
* Bootstrap the application events.
*
* @return void
*/
public
function
boot
()
{
$app
=
$this
->
app
;
/**
* Bootstrap the application events.
*/
public
function
boot
()
{
$app
=
$this
->
app
;
$this
->
bootConfig
();
$this
->
bootConfig
();
$app
[
'validator'
]
->
extend
(
'captcha'
,
function
(
$attribute
,
$value
)
use
(
$app
)
{
return
$app
[
'captcha'
]
->
verifyResponse
(
$value
,
$app
[
'request'
]
->
getClientIp
());
});
$app
[
'validator'
]
->
extend
(
'captcha'
,
function
(
$attribute
,
$value
)
use
(
$app
)
{
return
$app
[
'captcha'
]
->
verifyResponse
(
$value
,
$app
[
'request'
]
->
getClientIp
());
});
if
(
$app
->
bound
(
'form'
))
{
$app
[
'form'
]
->
macro
(
'captcha'
,
function
(
$attributes
=
[])
use
(
$app
)
{
return
$app
[
'captcha'
]
->
display
(
$attributes
,
$app
->
getLocale
());
});
}
}
/**
* //
*
* @return void
*/
protected
function
bootConfig
()
{
$path
=
__DIR__
.
'/config/captcha.php'
;
if
(
$app
->
bound
(
'form'
))
{
$app
[
'form'
]
->
macro
(
'captcha'
,
function
(
$attributes
=
[])
use
(
$app
)
{
return
$app
[
'captcha'
]
->
display
(
$attributes
,
$app
->
getLocale
());
});
}
}
$this
->
mergeConfigFrom
(
$path
,
'captcha'
);
/**
* Booting configure.
*/
protected
function
bootConfig
()
{
$path
=
__DIR__
.
'/config/captcha.php'
;
$this
->
publishes
([
$path
=>
config_path
(
'captcha.php'
)]);
}
$this
->
mergeConfigFrom
(
$path
,
'captcha'
);
/**
* Register the service provider.
*
* @return void
*/
public
function
register
()
{
$this
->
app
->
bind
(
'captcha'
,
function
(
$app
)
{
return
new
NoCaptcha
(
$app
[
'config'
][
'captcha.secret'
],
$app
[
'config'
][
'captcha.sitekey'
]
);
});
}
$this
->
publishes
([
$path
=>
config_path
(
'captcha.php'
)]);
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public
function
provides
()
{
return
[
'captcha'
];
}
/**
* Register the service provider.
*/
public
function
register
()
{
$this
->
app
->
bind
(
'captcha'
,
function
(
$app
)
{
return
new
NoCaptcha
(
$app
[
'config'
][
'captcha.secret'
],
$app
[
'config'
][
'captcha.sitekey'
]
);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public
function
provides
()
{
return
[
'captcha'
];
}
}
src/config/captcha.php
View file @
1c6c59ef
<?php
return
[
'secret'
=>
env
(
'NOCAPTCHA_SECRET'
),
'sitekey'
=>
env
(
'NOCAPTCHA_SITEKEY'
),
'secret'
=>
env
(
'NOCAPTCHA_SECRET'
),
'sitekey'
=>
env
(
'NOCAPTCHA_SITEKEY'
),
];
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