From 6eab2599001e3455f951ca495cba451258a041f0 Mon Sep 17 00:00:00 2001 From: Muhammad Hasan Khan Date: Thu, 2 May 2019 01:03:55 -0500 Subject: [PATCH] Replace captcha with reCaptcha2 --- public/js/sunnah.js | 554 +++++++++++++++++++++------------------- public/processer.php | 90 +++---- public/recaptchalib.php | 294 ++------------------- 3 files changed, 358 insertions(+), 580 deletions(-) diff --git a/public/js/sunnah.js b/public/js/sunnah.js index c1dc6ee..3281f37 100644 --- a/public/js/sunnah.js +++ b/public/js/sunnah.js @@ -1,168 +1,188 @@ - function openquran(surah, beginayah, endayah) { - window.open("http://quran.com/"+(surah+1)+"/"+beginayah+"-"+endayah, "quranWindow", "resizable = 1, fullscreen = 1"); - } - - function reportHadith_old(urn) { - window.open("/report.php?urn="+urn, "reportWindow", "scrollbars = yes, resizable = 1, fullscreen = 1, location = 0, toolbar = 0, width = 500, height = 700"); - } - - var openre = ""; - - function reportHadith(eurn, divname) { - // first check if some other RE panel is open. - // if it is and it's not this one, close it and destroy the captcha. - // otherwise if it's this one, toggle it off (animated), return - // - // set up this panel and display it - - var reel = $("#re"+divname); - var openreel = $("#re"+openre); - - if (openre.length > 0) { - if (openre == divname) { - reel.toggle(400, function() {openreel.remove(); }); - openre = ""; - return; - } - else { - $("#re"+openre).toggle(); - openreel.remove() - openre = ""; +function openquran(surah, beginayah, endayah) { + window.open("http://quran.com/" + (surah + 1) + "/" + beginayah + "-" + endayah, "quranWindow", "resizable = 1, fullscreen = 1"); +} + +function reportHadith_old(urn) { + window.open("/report.php?urn=" + urn, "reportWindow", "scrollbars = yes, resizable = 1, fullscreen = 1, location = 0, toolbar = 0, width = 500, height = 700"); +} + +var recaptchaLoaded = false; +var pendingCaptchas = []; +function onRecaptchaLoad() { + recaptchaLoaded = true + addPendingCaptchas(); +} + +function addCaptcha(container) { + pendingCaptchas.push(container) + if (recaptchaLoaded) { + addPendingCaptchas(); + } +} + +function addPendingCaptchas() { + while (container = pendingCaptchas.shift()) { + grecaptcha.render( + container, + { + "sitekey": "6LeWQsYSAAAAAE5kS_xV6nvhhAUzslHtmYUukteH", + "theme": "light" } + ) + } +} +var openre = ""; + +function reportHadith(eurn, divname) { + // first check if some other RE panel is open. + // if it is and it's not this one, close it and destroy the captcha. + // otherwise if it's this one, toggle it off (animated), return + // + // set up this panel and display it + + var reel = $("#re" + divname); + var openreel = $("#re" + openre); + + if (openre.length > 0) { + if (openre == divname) { + reel.toggle(400, function () { openreel.remove(); }); + openre = ""; + return; + } + else { + $("#re" + openre).toggle(); + openreel.remove() + openre = ""; } + } - insertScript('https://www.google.com/recaptcha/api/js/recaptcha_ajax.js', 'recaptcha-script'); + insertScript('https://www.google.com/recaptcha/api.js?onload=onRecaptchaLoad&render=explicit', 'recaptcha-script'); - $.get("/report.php", {eurn: eurn, hid: divname}, function (data) { - $("#"+divname+" .bottomItems").append(data); - - Recaptcha.create("6Ld7_PwSAAAAAH0CMHBshuY5t3z4dTHeUTsu4iey", "rerec"+divname, - { - theme: "red" - //callback: Recaptcha.focus_response_field - } - ); - - openre = divname; - $("#reform"+divname).submit(function(event) { - event.preventDefault(); - - if (!$("#reform"+divname+" input[name=type]:checked").length) { - $("#reresp"+divname).html("Please choose the type of error."); - } - else if ($("#reform"+divname+" input[name=type]:checked").val() == "other" - && $("#reform"+divname+" input[name=othererror]").val().length < 1) { - $("#reresp"+divname).html("Please specify the type of error."); - } - else if ($("#reform"+divname+" input[name=emailme]").is(':checked') - && $("#reform"+divname+" input[name=email]").val().length < 1) { - $("#reresp"+divname).html("Please enter an email address."); - } - else { - $.ajax({ - type: "POST", - url: "/processer.php", - data: $("#reform"+divname).serialize(), - success: function(data) { - var dataObj = $.parseJSON(data); - if (dataObj.status == 0) { - $("#reresp"+divname).css('color', 'rgb(117, 161, 161'); - $("#reresp"+divname).css('font-weight', 'bold'); - $("#reresp"+divname).css('font-size', '15px'); - //$("#reresp"+divname).css('height', ($("#reresp"+divname).height()+15)+'px'); - $(".resubmit").toggle(); - } - $("#reresp"+divname).html(dataObj.message); - } - }); - } - }); - - $("#re"+divname).toggle(400); + $.get("/report.php", { eurn: eurn, hid: divname }, function (data) { + $("#" + divname + " .bottomItems").append(data); - }); - } - - - var _gaq = _gaq || []; - _gaq.push(['_setAccount', 'UA-22385858-2']); - _gaq.push(['_trackPageview']); - - (function() { - var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; - ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; - var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); - })(); - - // (a) put default input focus on the state field - // (b) jquery ajax autocomplete implementation - - function insertScript(uri, id) { - var js, fjs = document.getElementsByTagName('script')[0]; - if (document.getElementById(id)) return; - js = document.createElement('script'); js.id = id; - js.src = uri; - fjs.parentNode.insertBefore(js, fjs); - } - - function close_box() { - $('#sharefuzz, .share_mb').animate( - {'opacity':'0'}, - 200, - 'linear', - function() { - $('#sharefuzz, .share_mb').css('display', 'none'); - }); - } - - var sharescriptsInserted = false; - var justloaded = false; - - function share(permalink) { - $.get("/share.php", {"link": permalink}, function(data) { - if (!$(".share_mb").length) $("body").append('
'); - $(".share_mb").html(data); //
- - if (!sharescriptsInserted) { - insertScript("http://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.0&appId=714222162002098", 'facebook-jssdk'); - insertScript("http://platform.twitter.com/widgets.js", 'twitter-script'); - insertScript("https://apis.google.com/js/platform.js", 'gplus-script'); - sharescriptsInserted = true; - justloaded = true; - - //(function checkready() { - // if (window.FB && window.twttr && gp) return; - // else {console.log("timing out"); setTimeout(checkready, 100);} - //})(); - } + addCaptcha("rerec" + divname); + + openre = divname; + $("#reform" + divname).submit(function (event) { + event.preventDefault(); - $(".share_mb").css("left", ($(window).width() - $(".share_mb").width())/2+"px"); - $(".share_mb").css("top", ($(window).height() - $(".share_mb").height())/2.8+"px"); - - $('#sharefuzz, .share_mb').animate({'opacity':'.25'}, 200, 'linear'); - $('.share_mb').animate({'opacity':'1.00'}, 200, 'linear'); - $('#sharefuzz, .share_mb').css('display', 'block'); - - if (!justloaded) { // these only need to be called if buttons are rendered - // after the script loads and inits, not before. - gapi.plusone.render("plusone-div", {"annotation": "none", "url": "http://sunnah.com"+permalink}); - twttr.widgets.load(); - FB.XFBML.parse() + if (!$("#reform" + divname + " input[name=type]:checked").length) { + $("#reresp" + divname).html("Please choose the type of error."); + } + else if ($("#reform" + divname + " input[name=type]:checked").val() == "other" + && $("#reform" + divname + " input[name=othererror]").val().length < 1) { + $("#reresp" + divname).html("Please specify the type of error."); + } + else if ($("#reform" + divname + " input[name=emailme]").is(':checked') + && $("#reform" + divname + " input[name=email]").val().length < 1) { + $("#reresp" + divname).html("Please enter an email address."); + } + else { + $.ajax({ + type: "POST", + url: "/processer.php", + data: $("#reform" + divname).serialize(), + success: function (data) { + var dataObj = $.parseJSON(data); + if (dataObj.status == 0) { + $("#reresp" + divname).css('color', 'rgb(117, 161, 161'); + $("#reresp" + divname).css('font-weight', 'bold'); + $("#reresp" + divname).css('font-size', '15px'); + //$("#reresp"+divname).css('height', ($("#reresp"+divname).height()+15)+'px'); + $(".resubmit").toggle(); + } + $("#reresp" + divname).html(dataObj.message); + } + }); } - else justloaded = false; - - $(".permalink_box").select(); - - //$('.share_close').click(function(){ - // console.log("close ..."); - // close_box(); - //}); }); - } - - $(document).ready(function () { - $(window).scroll(function() { + $("#re" + divname).toggle(400); + + }); +} + + +var _gaq = _gaq || []; +_gaq.push(['_setAccount', 'UA-22385858-2']); +_gaq.push(['_trackPageview']); + +(function () { + var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; + ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; + var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); +})(); + +// (a) put default input focus on the state field +// (b) jquery ajax autocomplete implementation + +function insertScript(uri, id) { + var js, fjs = document.getElementsByTagName('script')[0]; + if (document.getElementById(id)) return; + js = document.createElement('script'); js.id = id; + js.src = uri; + fjs.parentNode.insertBefore(js, fjs); +} + +function close_box() { + $('#sharefuzz, .share_mb').animate( + { 'opacity': '0' }, + 200, + 'linear', + function () { + $('#sharefuzz, .share_mb').css('display', 'none'); + }); +} + +var sharescriptsInserted = false; +var justloaded = false; + +function share(permalink) { + $.get("/share.php", { "link": permalink }, function (data) { + if (!$(".share_mb").length) $("body").append('
'); + $(".share_mb").html(data); //
+ + if (!sharescriptsInserted) { + insertScript("http://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.0&appId=714222162002098", 'facebook-jssdk'); + insertScript("http://platform.twitter.com/widgets.js", 'twitter-script'); + insertScript("https://apis.google.com/js/platform.js", 'gplus-script'); + sharescriptsInserted = true; + justloaded = true; + + //(function checkready() { + // if (window.FB && window.twttr && gp) return; + // else {console.log("timing out"); setTimeout(checkready, 100);} + //})(); + } + + $(".share_mb").css("left", ($(window).width() - $(".share_mb").width()) / 2 + "px"); + $(".share_mb").css("top", ($(window).height() - $(".share_mb").height()) / 2.8 + "px"); + + $('#sharefuzz, .share_mb').animate({ 'opacity': '.25' }, 200, 'linear'); + $('.share_mb').animate({ 'opacity': '1.00' }, 200, 'linear'); + $('#sharefuzz, .share_mb').css('display', 'block'); + + if (!justloaded) { // these only need to be called if buttons are rendered + // after the script loads and inits, not before. + gapi.plusone.render("plusone-div", { "annotation": "none", "url": "http://sunnah.com" + permalink }); + twttr.widgets.load(); + FB.XFBML.parse() + } + else justloaded = false; + + $(".permalink_box").select(); + + //$('.share_close').click(function(){ + // console.log("close ..."); + // close_box(); + //}); + }); +} + +$(document).ready(function () { + + $(window).scroll(function () { if ($(window).scrollTop() > 750) $("#back-to-top").addClass('bttenabled'); else $("#back-to-top").removeClass('bttenabled'); @@ -174,7 +194,7 @@ $("#topspace").css('display', 'block'); $("#toolbar").css('display', 'none'); $("#search").css('bottom', '31px'); // crumbs height + 12 bottom padding - $("#sidePanel").css({'position': 'fixed', 'top': '65px', 'left': $(".mainContainer").position().left - $("#sidePanel").width() - 55}); // last number is sidePanelContainer padding + $("#sidePanel").css({ 'position': 'fixed', 'top': '65px', 'left': $(".mainContainer").position().left - $("#sidePanel").width() - 55 }); // last number is sidePanelContainer padding } else { $("#banner").removeClass('bannerMiddle'); @@ -189,15 +209,15 @@ $("body").append('
'); //$("#sharefuzz").css({"height": $(document).height()}); - - $('#sharefuzz').click(function(){ close_box(); }); - + + $('#sharefuzz').click(function () { close_box(); }); + if ("searchQuery" in window) { $(".searchquery").val($(' - - '; -} - - - - -/** - * A ReCaptchaResponse is returned from recaptcha_check_answer() - */ -class ReCaptchaResponse { - var $is_valid; - var $error; -} - - /** * Calls an HTTP POST function to verify if the user's guess was correct * @param string $privkey - * @param string $remoteip - * @param string $challenge * @param string $response - * @param array $extra_params an array of extra variables to post to the server - * @return ReCaptchaResponse + * @return boolean */ -function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra_params = array()) +function recaptcha_check_answer($privkey, $response) { - if ($privkey == null || $privkey == '') { - die ("To use reCAPTCHA you must get an API key from https://www.google.com/recaptcha/admin/create"); - } - - if ($remoteip == null || $remoteip == '') { - die ("For security reasons, you must pass the remote ip to reCAPTCHA"); - } - - - - //discard spam submissions - if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) { - $recaptcha_response = new ReCaptchaResponse(); - $recaptcha_response->is_valid = false; - $recaptcha_response->error = 'incorrect-captcha-sol'; - return $recaptcha_response; - } - - $response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/recaptcha/api/verify", - array ( - 'privatekey' => $privkey, - 'remoteip' => $remoteip, - 'challenge' => $challenge, - 'response' => $response - ) + $extra_params - ); - - $answers = explode ("\n", $response [1]); - $recaptcha_response = new ReCaptchaResponse(); - - if (trim ($answers [0]) == 'true') { - $recaptcha_response->is_valid = true; - } - else { - $recaptcha_response->is_valid = false; - $recaptcha_response->error = $answers [1]; - } - return $recaptcha_response; - -} - -/** - * gets a URL where the user can sign up for reCAPTCHA. If your application - * has a configuration page where you enter a key, you should provide a link - * using this function. - * @param string $domain The domain where the page is hosted - * @param string $appname The name of your application - */ -function recaptcha_get_signup_url ($domain = null, $appname = null) { - return "https://www.google.com/recaptcha/admin/create?" . _recaptcha_qsencode (array ('domains' => $domain, 'app' => $appname)); -} - -function _recaptcha_aes_pad($val) { - $block_size = 16; - $numpad = $block_size - (strlen ($val) % $block_size); - return str_pad($val, strlen ($val) + $numpad, chr($numpad)); -} - -/* Mailhide related code */ - -function _recaptcha_aes_encrypt($val,$ky) { - if (! function_exists ("mcrypt_encrypt")) { - die ("To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed."); - } - $mode=MCRYPT_MODE_CBC; - $enc=MCRYPT_RIJNDAEL_128; - $val=_recaptcha_aes_pad($val); - return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); + if ($privkey == null || $privkey == '') { + die("To use reCAPTCHA you must get an API key from https://www.google.com/recaptcha/admin/create"); + } + + if ($response == null || $response == '') { + die("Response from reCAPTCHA is required"); + } + + $url = 'https://www.google.com/recaptcha/api/siteverify'; + $data = array( + 'secret' => $privkey, + 'response' => $response + ); + $options = array( + 'http' => array( + 'method' => 'POST', + 'content' => http_build_query($data) + ) + ); + $context = stream_context_create($options); + $verify = file_get_contents($url, false, $context); + $captcha_success=json_decode($verify); + return $captcha_success->success; } - - -function _recaptcha_mailhide_urlbase64 ($x) { - return strtr(base64_encode ($x), '+/', '-_'); -} - -/* gets the reCAPTCHA Mailhide url for a given email, public key and private key */ -function recaptcha_mailhide_url($pubkey, $privkey, $email) { - if ($pubkey == '' || $pubkey == null || $privkey == "" || $privkey == null) { - die ("To use reCAPTCHA Mailhide, you have to sign up for a public and private key, " . - "you can do so at http://www.google.com/recaptcha/mailhide/apikey"); - } - - - $ky = pack('H*', $privkey); - $cryptmail = _recaptcha_aes_encrypt ($email, $ky); - - return "http://www.google.com/recaptcha/mailhide/d?k=" . $pubkey . "&c=" . _recaptcha_mailhide_urlbase64 ($cryptmail); -} - -/** - * gets the parts of the email to expose to the user. - * eg, given johndoe@example,com return ["john", "example.com"]. - * the email is then displayed as john...@example.com - */ -function _recaptcha_mailhide_email_parts ($email) { - $arr = preg_split("/@/", $email ); - - if (strlen ($arr[0]) <= 4) { - $arr[0] = substr ($arr[0], 0, 1); - } else if (strlen ($arr[0]) <= 6) { - $arr[0] = substr ($arr[0], 0, 3); - } else { - $arr[0] = substr ($arr[0], 0, 4); - } - return $arr; -} - -/** - * Gets html to display an email address given a public an private key. - * to get a key, go to: - * - * http://www.google.com/recaptcha/mailhide/apikey - */ -function recaptcha_mailhide_html($pubkey, $privkey, $email) { - $emailparts = _recaptcha_mailhide_email_parts ($email); - $url = recaptcha_mailhide_url ($pubkey, $privkey, $email); - - return htmlentities($emailparts[0]) . "...@" . htmlentities ($emailparts [1]); - -} - - -?>