Commit 2710ea9e authored by anhskohbo's avatar anhskohbo

Add source.

parents
/vendor
composer.phar
composer.lock
.DS_Store
language: php
php:
- 5.4
- 5.5
- 5.6
- hhvm
before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev
script: phpunit
{
"name": "anhskohbo/no-captcha",
"description": "No CAPTCHA reCAPTCHA For Laravel.",
"authors": [
{
"name": "anhskohbo",
"email": "anhskohbo@gmail.com"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.*|5.0.*"
},
"autoload": {
"psr-4": {
"Anhskohbo\\NoCaptcha\\": "src/"
}
},
"minimum-stability": "stable"
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
<?php namespace Anhskohbo\NoCaptcha;
class NoCaptcha {
const CLIENT_API = 'https://www.google.com/recaptcha/api.js';
const VERYFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
/**
* //
*
* @var string
*/
protected $secret;
/**
* //
*
* @var string
*/
protected $sitekey;
/**
* //
*
* @var string
*/
protected $lang;
/**
* //
*
* @param string $secret
* @param string $sitekey
*/
public function __construct($secret, $sitekey, $lang = null)
{
$this->lang = $lang;
$this->secret = $secret;
$this->sitekey = $sitekey;
}
/**
* //
*
* @return string
*/
public function display($attributes = array())
{
$attributes['data-sitekey'] = $this->sitekey;
$html = '<script src="'.$this->getJsLink().'"></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(array(
'secret' => $this->secret,
'response' => $response,
'remoteip' => $clientIp
));
return isset($response['success']) && $response['success'] === true;
}
/**
* //
*
* @return string
*/
public function getJsLink()
{
if ($this->lang)
{
return static::CLIENT_API.'?hl='.$this->lang;
}
return static::CLIENT_API;
}
/**
* //
*
* @param array $query
* @return array
*/
protected function sendRequestVerify(array $query = array())
{
$link = static::VERYFY_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 = array();
foreach ($attributes as $key => $value)
{
$html[] = $key.'="'.$value.'"';
}
return count($html) ? ' '.implode(' ', $html) : '';
}
}
<?php namespace Anhskohbo\NoCaptcha;
use Illuminate\Support\ServiceProvider;
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;
$app['config']->package('anhskohbo/no-captcha', __DIR__.'/config');
$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 = array()) use ($app)
{
return $app['captcha']->display($attributes);
});
}
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('captcha', function($app)
{
return new NoCaptcha(
$app['config']->get('no-captcha::secret'),
$app['config']->get('no-captcha::sitekey'),
$app['config']->get('no-captcha::lang')
);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['captcha'];
}
}
<?php
return array(
'secret' => '',
'sitekey' => '',
'lang' => '',
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment