Skip to content

Commit

Permalink
Final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbaczek committed Nov 19, 2018
1 parent f00f645 commit f4dc29b
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 4 deletions.
10 changes: 6 additions & 4 deletions classes/Kohana/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* Class Kohana_Recaptcha
* @author Piotr Bączek
*/
class Kohana_Recaptcha
{
Expand Down Expand Up @@ -31,10 +32,11 @@ public static function getSiteKey()
/**
* Validation method
* Can be used in Valid class
* @param String $response
* @param String $response Response token from frontend google recaptcha
* @param String $action Name of action from JS (homepage, login, etc.)
* @return bool
*/
public static function recaptchaValid(String $response)
public static function recaptchaValid(String $response, String $action)
{
$config = Kohana::$config->load('koseven-recaptcha');
$secretKey = $config->get('secret_key');
Expand All @@ -51,13 +53,13 @@ public static function recaptchaValid(String $response)
{
return FALSE;
}

Log::instance()->add(Log::DEBUG, $result->body());
$body = json_decode($result->body());
if ($body === NULL)
{
return FALSE;
}

return $body->success;
return $body->action == $action && $body->success;
}
}
92 changes: 92 additions & 0 deletions classes/Kohana/Recaptcha/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* Class Kohana_Recaptcha_Helper
*/
class Kohana_Recaptcha_Helper
{
const ARABIC = 'ar';
const AFRIKAANS = 'af';
const AMHARIC = 'am';
const ARMENIAN = 'hy';
const AZERBAIJANI = 'az';
const BASQUE = 'eu';
const BENGALI = 'bn';
const BULGARIAN = 'bg';
const CATALAN = 'ca';
const CHINESE_HK = 'zh-HK';
const CHINESE_SIMPLIFIED = 'zh-CN';
const CHINESE_TRADITIONAL = 'zh-TW';
const CROATIAN = 'hr';
const CZECH = 'cs';
const DANISH = 'da';
const DUTCH = 'nl';
const ENGLISH_UK = 'en-GB';
const ENGLISH_US = 'en';
const ESTONIAN = 'et';
const FILIPINO = 'fil';
const FINNISH = 'fi';
const FRENCH = 'fr';
const FRENCH_CANADIAN = 'fr-CA';
const GALICIAN = 'gl';
const GEORGIAN = 'ka';
const GERMAN = 'de';
const GERMAN_AUSTRIA = 'de-AT';
const GERMAN_SWITZERLAND = 'de-CH';
const GREEK = 'el';
const GUJURATI = 'gu';
const HEBREW = 'iw';
const HINDI = 'hi';
const HUNGARIAN = 'hu';
const ICELANDIC = 'is';
const INDONESIAN = 'id';
const ITALIAN = 'it';
const JAPANESE = 'ja';
const KANNADA = 'kn';
const KOREAN = 'ko';
const LAOTHIAN = 'lo';
const LATVIAN = 'lv';
const LITHUANIAN = 'lt';
const MALAY = 'ms';
const MALAYALAM = 'ml';
const MARATHI = 'mr';
const MONGOLIAN = 'mn';
const NORWEGIAN = 'no';
const PERSIAN = 'fa';
const POLISH = 'pl';
const PORTUGESE = 'pt';
const PORTUGESE_BRAZIL = 'pt-BR';
const PORTUGESE_PORTUGAL = 'pt-PT';
const ROMANIAN = 'ro';
const RUSSIAN = 'ru';
const SERBIAN = 'sr';
const SINHALESE = 'si';
const SLOVAK = 'sk';
const SLOVENIAN = 'sl';
const SPANISH = 'es';
const SPANISH_LATIN_AMERICA = 'es-419';
const SWAHILI = 'sw';
const SWEDISH = 'sv';
const TAMIL = 'ta';
const TELUGU = 'te';
const THAI = 'th';
const TURKISH = 'tr';
const UKRAINIAN = 'uk';
const URDU = 'ur';
const VIETNAMESE = 'vi';
const ZULU = 'zu';

/**
* Get Api URL
* @param String $lang
* @param Recaptcha $recaptcha
* @return String
*/
public static function getApiUrl(String $lang = self::ENGLISH_US, Recaptcha $recaptcha)
{
return 'https://www.google.com/recaptcha/api.js?' . http_build_query([
'hl' => $lang,
'render' => $recaptcha::getSiteKey()
]);
}
}
9 changes: 9 additions & 0 deletions classes/Recaptcha/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Class Recaptcha_Helper
*/
class Recaptcha_Helper extends Kohana_Recaptcha_Helper
{

}
51 changes: 51 additions & 0 deletions example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Create PHP file:

<!DOCTYPE html>
<html>
<head>
<script src="<?= $recaptcha_helper::getApiUrl($recaptcha_helper::ENGLISH_US, $recaptcha) ?>"></script>
<script>
grecaptcha.ready(function () {
grecaptcha.execute('<?=$recaptcha->getSiteKey()?>', {action: 'homepage'}).then(function (token) {
let form = document.getElementById('firstForm');
var input = document.createElement('input');
input.type = 'hidden';
input.name = '<?=$recaptcha->getPublicKeyName()?>';
input.value = token;
form.appendChild(input);
});
});
</script>
</head>
<body>
<form method="POST" id="firstForm">
<input type="text" name="name" value="test"/>
<input type="submit" value="Send"/>
</form>

</body>
</html>

Kohana Controller:

public function action_index()
{
$this->template->recaptcha = new Recaptcha();
$this->template->recaptcha_helper = new Recaptcha_Helper();

if ($this->request->method() == Request::POST)
{
$validation = Validation::factory($this->request->post())
//Action provided must be the same as in JS init
->rule(Recaptcha::getPublicKeyName(), 'Recaptcha::recaptchaValid', [':value', 'homepage']);

if ($validation->check())
{
echo 'no_errors';
}
else
{
var_dump($validation->errors());
}
}
}

0 comments on commit f4dc29b

Please sign in to comment.