Skip to content

Instantly share code, notes, and snippets.

@mavitm
Last active September 16, 2019 12:19
Show Gist options
  • Save mavitm/a5938f8e7370ae3e2e9e5ab6299b378b to your computer and use it in GitHub Desktop.
Save mavitm/a5938f8e7370ae3e2e9e5ab6299b378b to your computer and use it in GitHub Desktop.
recapcha
<?php
use My\Project\Classes\Recaptcha;
function onStart()
{
$prefix = Input::get("px");
$charSize = Input::get("cs",6);
$width = Input::get("w",80);
$height = Input::get("h",38);
$charList = "ABCDEFGHKLMNPRSTUVYZWXQ123456789";
return Response::make(Recaptcha::instance()->generateImg($prefix, $charList, $charSize, $width, $height))
->header('Content-Type', "image/png");
}
$(".guvrefresh").off("click").on("click",function () {
var targetimg = $($(this).data("guvimg"));
var imgSrc = $(targetimg).data("src");
var newSrc = imgSrc+"&cr="+Math.floor(Math.random() * 100000) + 1;
$(targetimg).attr("src", newSrc);
});
<?php
/**
* Created by PhpStorm.
* User: xubuntu2045
* Date: 20.04.2018
* Time: 11:45
*/
namespace Sarki\Project\Classes;
use Log;
use Session;
use Response;
use My\Project\Classes\Translate;
use My\Project\Traits\Helper;
use October\Rain\Exception\ApplicationException;
use October\Rain\Support\Traits\Singleton;
use My\Project\Classes\Curl;
use Cms\Classes\Controller;
use My\Project\Models\Projectsettings as Framesettings;
class Recaptcha
{
use Singleton, Helper;
public
$verifyUrl = "https://www.google.com/recaptcha/api/siteverify?",
$jsUrl = "https://www.google.com/recaptcha/api.js";
public $captchaPage, $captcha_type = 'localimg';
private
$secret = "****************************",
$sitekey = "****************************";
public function init()
{
//$this->secret = Framesettings::get("captcha_secretkey");
//$this->sitekey = Framesettings::get("captcha_sitekey");
$this->captchaPage = "localimg"; //Framesettings::get("captcha_img_page");
}
public function getCaptchaType()
{
//return Framesettings::get("captcha_type");
}
public function getSiteKey()
{
return $this->sitekey;
}
public function getHtml($elementID)
{
//throw new ApplicationException(Framesettings::get("captcha_type"));
if($this->captcha_type == "google")
{
return '<div id="'.$elementID.'" data-size="normal" data-theme="dark" class="g-recaptcha" data-sitekey="'.$this->getSiteKey().'"></div>';
}
elseif($this->captcha_type == "localimg")
{
//$imgUrl = Controller::getController()->pageUrl($this->captchaPage) . "?px=" . $elementID;
Session::put("captchaPrefix", $elementID);
$htmlCode = '<div id="' . $elementID . '" class="row">';
$htmlCode .= '<div class="col-sm-4">';
$htmlCode .= '<div class="imgparent bgDark">';
$htmlCode .= '<span id="'.$elementID.'imgparent">'.$this->localeImg($elementID).'</span>';
$htmlCode .= '<button type="button" class="btn info guvrefresh float-right" data-guvimg="#'.$elementID.'cardguvimg"><i class="fa fa-refresh"></i></button></div></div>';
$htmlCode .= '<div class="formparent col-sm-8"><input type="text" id="'.$elementID.'cardguvtext" name="g-recaptcha-response" class="form-control" placeholder="' . Translate::getTranslate("Resimdeki Kodu giriniz.") . '"></div>';
$htmlCode .= '</div>';
return $htmlCode;
}
}
public function localeImg($elementID)
{
//burda gorsel için ellenen http url adresi lazım altta capchaimg.php olarak ekledim ona giden http url adresi
$imgUrl = Controller::getController()->pageUrl($this->captchaPage)."?px=".$elementID;
$imgUrl = Http::instance()->redirectUrl($imgUrl);
return '<img id="'.$elementID.'cardguvimg" data-src="'.$imgUrl.'" data-prefix="'.$elementID.'" src="'.$imgUrl.'&cr='.rand(1000, 9999999).'" alt="security code">';
}
public function getJsUrl()
{
return $this->jsUrl;
}
public function addJs($control)
{
if($this->captcha_type == "google")
{
$control->addJs($this->jsUrl);
}
}
public function check($g_recaptcha_response)
{
/*
$response = json_decode(
file_get_contents(
"https://www.google.com/recaptcha/api/siteverify?secret=".Settings::get('secret_key')."&response=".post('g-recaptcha-response')."&remoteip=".$_SERVER['REMOTE_ADDR']
),
true
);
*/
if($this->captcha_type == "google")
{
$query = $this->verifyUrl . "secret=" . $this->secret . "&response=" . $g_recaptcha_response . "&remoteip=" . $_SERVER['REMOTE_ADDR'];
$response = file_get_contents($query);
//$response = Curl::instance()->getUrl($query);
$responseArray = json_decode($response, true);
if(empty($responseArray['success']))
{
return false;
}
//return $responseArray['success'] != false;
return $responseArray['success'] == false ? false : true;
}
elseif($this->captcha_type == "localimg")
{
$guvPrefixCode = Session::get("captchaPrefix");
if(empty($guvPrefixCode))
{
return false;
}
// $all = Session::all();
// Log::debug(json_encode($all));
$guvCode = Session::get($guvPrefixCode."Captcha");
if(empty($guvCode))
{
return false;
}
if($guvCode == $g_recaptcha_response)
{
return true;
}
}
return false;
}
public function randomkey($charList = "AaBbCcDdEeFfGgHhKkLlMmNnOoPpRrSsTtUuVvYyZzWwXxQq0123456789", $limit = 8)
{
if(!is_array($charList))
{
$charList = str_split($charList);
}
$k = [];
for ($i = 0; $i < $limit; $i++)
{
$k[] = $charList[array_rand($charList)];
}
return implode('',$k);
}
//generator random img
public function generateImg($sessionIndexPrefix = 'guv', $charList = "AaBbCcDdEeFfGgHhKkLlMmNnOoPpRrSsTtUuVvYyZzWwXxQq0123456789", $charSize = 6, $width = 80, $height = 30)
{
if($charSize < 6)
{
$charSize = 6;
}
$str = strtoupper($this->randomkey($charList, $charSize));
Session::put($sessionIndexPrefix."Captcha", $str);
$img = ImageCreate($width, $height);
$white = ImageColorAllocate($img, 255, 255, 255);
$black = ImageColorAllocate($img, 0, 0, 0);
ImageFill($img, 0, 0, $black);
ImageString($img, 12, 10, 7, $str, $white);
ImageLine($img, $width, ($height - 5), 0, ($height - 8), $white);
header("Content-type: image/png");
ImagePng($img);
ImageDestroy($img);
}
}

view

echo Recaptcha::instance()->getHtml('contactForm');
echo Recaptcha::instance()->getHtml('commentForm');

check

if(!Recaptcha::instance()->check(input("g-recaptcha-response"))){
  //err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment