funchat/inc/captcha.class.php

109 lines
4.4 KiB
PHP

<?php
/**
* (c) 2025 BeCast
* -------------------------------------
* Filename: captcha.class.php
* Purpose: Capthca Handling
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
$module["module"]["name"]="Captcha Class";
$module["module"]["ver"]="1.0.0";
class captcha{
function getCaptcha(){
global $config;
if($config['captcha']==1) {
return '<label data-mcaptcha_url="https://'.$config['mcaptcha_url'].'/widget/?sitekey='.$config['mcaptcha_sitekey'].'" for="mcaptcha__token" id="mcaptcha__token-label">mCaptcha authorization token.<a href="https://mcaptcha.org/docs/user-manual/how-to-mcaptcha-without-js/">Instructions</a>.<input type="text" name="mcaptcha__token" id="mcaptcha__token" /></label><div id="mcaptcha__widget-container"></div> <script src="https://unpkg.com/@mcaptcha/vanilla-glue@0.1.0-rc2/dist/index.js"></script>';
} else if ($config['captcha']==2) {
return '<script src="https://www.google.com/recaptcha/api.js"></script><div class="g-recaptcha" data-sitekey="'.$config['recaptcha_sitekey'].'"></div>';
} else {
return '';
}
}
function validate($response){
global $config,$functions;
if($config['captcha']==1) {
if(isset($response['mcaptcha__token']) && $response['mcaptcha__token']!=''){
$token = $response['mcaptcha__token'];
} else {
return false;
}
$data = array(
'token' => $token,
'key' => $config['mcaptcha_sitekey'],
'secret' => $config['mcaptcha_secret']
);
$json = json_encode($data);
$url = 'https://'.$config['mcaptcha_url'].'/api/v1/pow/siteverify';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
));
$fh=curl_exec($ch);
// schließe den cURL-Handle und gebe die Systemresourcen frei
curl_close($ch);
if(!$fh){
return false;
}else{
$cresp = json_decode($fh, true);
if($cresp["valid"] === true){
return true;
}else{
return false;
}
}
}else if($config['captcha']==2) {
if(isset($response['g-recaptcha-response']) && $response['g-recaptcha-response']!=''){
$data = $response['g-recaptcha-response'];
} else {
return false;
}
$ch = curl_init();
// setze die URL und andere Optionen
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify?secret=".$config['recaptcha_secret']."&response=".$data."&remoteip=".$functions->get_ip());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// führe die Aktion aus und gebe die Daten an den Browser weiter
$fh=curl_exec($ch);
// schließe den cURL-Handle und gebe die Systemresourcen frei
curl_close($ch);
if(!$fh){
return false;
}else{
$cresp = json_decode($fh, true);
if($cresp["success"] === true){
return true;
}else{
return false;
}
}
} else {
return true;
}
}
}
?>