We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18913
    • 654 Posts
    I’ve taken a shot at incorporating Recaptcha into SPForm. 4 files were edited and are attached, with assumption being that recaptcha’s library and a key file exist at assets/snippets/recaptcha. Steps taken were :

    - In spfconfig.cfg.php
    add
    	
    /*
     * Recaptcha-related variables defined here //MattC
     */
    $requireRecaptcha = true; // Require Recaptcha human being verification? - default false //MattC
    $warnRecaptcha = true;   // default true //MattC
    // Tell $errorsTo recipient about failed Recaptcha verification?  //MattC
    $adviseOnRecaptcha = false;  //MattC
    // Log failed Recaptcha verifications?  //MattC
    $logOnRecaptcha = false;  //MattC
    

    at line 239ish, mirroring the logic for existing Captcha verification

    - In spfdebug.php
    add
     echo "requireRecaptcha: ",$requireRecaptcha,"<br>";  //MattC
     echo "warnRecaptcha: ",$warnRecaptcha,"<br>";  //MattC
     echo "adviseOnRecaptcha: ",$adviseOnRecaptcha,"<br>";  //MattC
     echo "logOnRecaptcha: ",$logOnRecaptcha,"<br>";   //MattC
    

    at the end

    - In spform.inc.php
    add
        $recaptchaPath = "assets/snippets/recaptcha/";  //MattC
    

    at line 22ish

    add
         $recaptchaUrl=$modx->config['site_url'].$recaptchaPath.'recaptchalib.php';  //MattC
      

    at line30ish

    add
        echo "<b>Recaptcha URL:</b><br />".$recaptchaUrl."<br />";  //MattC
      

    at line 35ish

    change line115ish to read (I think this is true for recaptcha...)
        if (($requireVerify) || ($requireRecaptcha))  //MattC
      


    add
        if($requireRecaptcha){  //MattC start
    		if (file_exists("assets/snippets/recaptcha/recaptchalib.php") &&
    			file_exists("assets/snippets/recaptcha/recaptchakeys.php")) {
    				require_once('assets/snippets/recaptcha/recaptchalib.php');  //MattC
    				require_once('assets/snippets/recaptcha/recaptchakeys.php');  //MattC
    				
    				echo recaptcha_get_html($publickey, $resp->error);
    		}
            else {
    			echo "<br>Can't find Recaptcha lib and/or keys<br>Can't use Recaptcha.";
    			exit();
            }		
    	}  //MattC end
    

    before the submit button around line 285ish

    - In spformproc.inc.php
    add
    if($requireRecaptcha && 
    	file_exists("assets/snippets/recaptcha/recaptchalib.php") &&
    	file_exists("assets/snippets/recaptcha/recaptchakeys.php")) {  //MattC start
    		require_once('assets/snippets/recaptcha/recaptchalib.php');  //MattC
    		require_once('assets/snippets/recaptcha/recaptchakeys.php');  //MattC
    		# the response from reCAPTCHA
    		$resp = null;
    		# the error code from reCAPTCHA, if any
    		$error = null;				
    	
            $resp = recaptcha_check_answer ($privatekey,
                                            $_SERVER["REMOTE_ADDR"],
                                            $_POST["recaptcha_challenge_field"],
                                            $_POST["recaptcha_response_field"]);
            if ($resp->is_valid) {
                    echo "Valid response";
            } else {
    			if($logOnRecaptcha) {
    				error_log("[$scriptName] Recaptcha verification failed for host " . $_SERVER['REMOTE_ADDR'], 0);
    			}
    			if($adviseOnRecaptcha) {
    				mail_advisory($errors, $spf_lang);
    			}
    			if ($warnRecaptcha) {
    				$errors[] = $spf_lang['bad-verification'];
    			} else {
    				exit;
    			}
            }
    } else { 
    	echo "<br>Can't find Recaptcha lib and/or keys<br>Can't use Recaptcha.";
    	exit();
    } //MattC end
    

    around line 506ish

    I’ve added a demo page to
    http://www.considine.net/recaptcha
    which shows this at work.

    A couple of things :
    - I realize now that while I’ve defined recaptcha’s URLs, I ended up using hard-coded locations in requiring the files. Bad doggey. That should be changed.
    - I didn’t change any of the logic (that I know of!) of the existing SPForm. So when the Recaptcha check fails, it brings up and error page, rather than redisplay the recaptcha dialogue with an "Incorrect" message. I don’t have time to look into that.

    Hopefully Bob can chime in if there is anything I’ve obviously goofed up. (And I hope I haven’t overstepped any bounds by making these alterations).

    The changed files are attached. Don’t forget to edit the config file for your own site. Note also that the enclose config file has recaptcha turned on.

    Constructive critiques are welcome and I hope this helps someone.
    Matt