Hack: reCaptcha

Current version:
Compatible with: R4.2
Credits: Baturin Alexey, Voronkov Alexandr
URL: http://parallels.nsu.ru/WackoWiki/WackoWikiRecaptchaen

Install reCaptcha on Wacko Wiki

1. Go to http://recaptcha.net, register yourself, receive private and public keys for your domain and download library with php-source.
Copy this library to your server, extract. Keep in mind path to file 'recaptchalib.php'.


2. Go to the Wacko Wiki home directory and edit file «actions/registration.php».
Edit file «actions/registration.php»
In the very begining, before the lines:

<!--notypo-->
<?php

You should add the following:

<?php 
    
/*Captcha BEGIN*/
    
require_once('path_to_file_recaptchalib.php'); //here you should write path to 'recaptchalib.php' 

    
$publickey "public_key";
    
$privatekey "private_key";

    
# the response from reCAPTCHA
    
$resp null;
    
    
# was there a reCAPTCHA response?
    
if ($_POST["recaptcha_response_field"]) {
        
$resp recaptcha_check_answer ($privatekey,
                                        
$_SERVER["REMOTE_ADDR"],
                                        
$_POST["recaptcha_challenge_field"],
                                        
$_POST["recaptcha_response_field"]);
    }
     
/*Captcha END*/
?>

Next, search the lines:
// check if name is WikiName style
      if (!$this->IsWikiName($name)) $error = $this->GetTranslation("MustBeWikiName");
      else if (!$email) $error = $this->GetTranslation("SpecifyEmail");
      else if (!preg_match("/^.+?\@.+$/", $email)) $error = $this->GetTranslation("NotAEmail");
      else if ($confpassword != $password) $error = $this->GetTranslation("PasswordsDidntMatch");
      else if (preg_match("/ /", $password)) $error = $this->GetTranslation("SpacesArentAllowed");
      else if (strlen($password) < 5) $error = $this->GetTranslation("PasswordTooShort");

and add right after them:
/*Captcha BEGIN*/
	  else if ($resp==null || !($resp->is_valid)) { $error = $resp->error;}
	  /*Captcha END*/

3. And now you may add reCaptcha form itself right after lines:
<tr>
      <td align="right"><?php echo $this->GetTranslation("Email");?>:</td>
      <td><input name="email" size="30" value="<?php echo htmlspecialchars($email); ?>" /></td>
    </tr>

(here the source of reCaptcha form itself)
<!--Captcha BEGIN-->
	<tr><td align="right">Captcha</td><td>
	<?php 
		echo recaptcha_get_html($publickey, $error);
	?>
	</td></tr>
<!--Captcha END-->

4. Now it should work. Enjoy!

Possible problems and solutions

Problems with global variables in reCaptcha library

After the integration reCaptcha in Wacko Wiki you may find that it doesn't work. Possible reason may be the next: global variables in are not visible 'recaptchalib.php'. So, you may do the following:


1. Comment out global variables in the source of library and their use with operator Global.


2. Copy these variables in every function where they were used.


Let's try to understand the reasons of such behavior of library ReCaptcha after it was built into Wacko Wiki.
First, the file wacco.php is launched with its parameters. Inside it the object of class “Wacko” and then function of this object Run() is launched.
In its tern function Run() launches lots of other functions and one of them – registration.php – includes ReCaptcha library.
But function include() here just inserts the source of reCaptcha library inside the function, so all variables which were declared as Global in fact are Local inside this function.
So there are no Global variables with this names.

Problems with reCaptcha and Opera-browser

In some cases valid html-source is very necessary for Opera (You may check validity of web page on http://w3c.org ). After installation of reCaptcha source started not to be valid and its become impossible to register ith the help of Opera. Solution: make the source valid:


1. Open file 'recaptchalib.php' and rewrite all html-tags in accordance with xhtml 1.0. In particular you should close tags which come in pairs. (for example: use tag <br/> instead of <br>).


2. Error in some formatters in Wacko Wiki. “Wacko wiki”-engine adds <p> and </p> tags around the form of registration. Solution: open file «handlers\page\show.php”, search for the line:

$data = $this->Format($this->page["body_r"], "post_wacko", array("bad"=>"good"));

and substitute it for
$data = $this->Format($this->page["body"], "wakka");

Extra: diff-files

You may install reCaptcha with patch-applet using the following diff-files:
“recaptchalib.php”
-problems with global variables and fitness for xhtml 1.0.

*** WackoWiki/lib/recaptchalib.php    2007-06-05 11:34:40.000000000 +0700
--- MWackoWiki/lib/recaptchalib.php    2007-07-05 09:47:07.307574744 +0700
***************
*** 37,41 ****
   */
- $recaptcha_api_server = 'http://api.recaptcha.net';
- $recaptcha_api_secure_server = 'https://api-secure.recaptcha.net';
- $recaptcha_verify_server = 'api-verify.recaptcha.net';
  
--- 37,38 ----
***************
*** 108,110 ****
  {
!         global $recaptcha_api_server, $recaptcha_api_secure_server;
  
--- 105,108 ----
  {
!     $recaptcha_api_server = 'http://api.recaptcha.net';
!     $recaptcha_api_secure_server = 'https://api-secure.recaptcha.net';
  
***************
*** 126,130 ****
      <noscript>
!           <iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br>
            <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
!           <input type="hidden" name="recaptcha_response_field" value="manual_challenge">
      </noscript>';
--- 124,128 ----
      <noscript>
!           <iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br />
            <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
!           <input type="hidden" name="recaptcha_response_field" value="manual_challenge" />
      </noscript>';
***************
*** 172,174 ****
  
!         global $recaptcha_verify_server;
          $response = _recaptcha_http_post ($recaptcha_verify_server, "/verify",
--- 170,172 ----
  
!         $recaptcha_verify_server = 'api-verify.recaptcha.net';
          $response = _recaptcha_http_post ($recaptcha_verify_server, "/verify",

«handlers/page/show.php»
*** WackoWiki/handlers/page/show.php    2005-02-26 19:16:34.000000000 +0600
--- MWackoWiki/handlers/page/show.php    2007-07-05 09:33:21.021189368 +0700
***************
*** 47,49 ****
      // display page
!     $data = $this->Format($this->page["body_r"], "post_wacko", array("bad"=>"good"));
      $data = $this->NumerateToc( $data ); //  numerate toc if needed
--- 47,51 ----
      // display page
!     /*Captcha BEGIN*/
!     $data = $this->Format($this->page["body"], "wakka");
!     /*Captcha END*/
      $data = $this->NumerateToc( $data ); //  numerate toc if needed

«actions/registration.php»
Don't forget to fill up the keys.
*** WackoWiki/actions/registration.php    2005-02-26 20:23:20.000000000 +0600
--- MWackoWiki/actions/registration.php    2007-07-05 09:58:44.835534376 +0700
***************
*** 1 ****
--- 1,20 ----
+ <?php 
+     /*Captcha BEGIN*/
+     require_once('lib/recaptchalib.php');
+       $publickey = "recaptcha_public_key"; 
+     $privatekey = "recaptcha_private_key";
+ 
+     # the response from reCAPTCHA
+     $resp = null;
+     
+     # was there a reCAPTCHA response?
+     if ($_POST["recaptcha_response_field"]) {
+         $resp = recaptcha_check_answer ($privatekey,
+                                         $_SERVER["REMOTE_ADDR"],
+                                         $_POST["recaptcha_challenge_field"],
+                                         $_POST["recaptcha_response_field"]);
+     }
+      /*Captcha END*/
+ ?>
+ 
  <!--notypo-->
***************
*** 40,41 ****
--- 59,63 ----
        else if (strlen($password) < 5) $error = $this->GetTranslation("PasswordTooShort");
+       /*Captcha BEGIN*/
+       else if ($resp==null || !($resp->is_valid)) { $error = $resp->error;}
+       /*Captcha END*/
        else
***************
*** 126,127 ****
--- 148,156 ----
      </tr>
+     <!--Captcha BEGIN-->
+     <tr><td align="right">Captcha</td><td>
+     <?php 
+         echo recaptcha_get_html($publickey, $error);
+     ?>
+     </td></tr>
+     <!--Captcha END-->
      <tr>

Script for automatic install

Can be found here: http://swsoft.nsu.ru/~abaturin/recaptcha_install.tar.gz
Install: extract to somewhere and read readme.txt.

Links

WackoWiki site: http://wackowiki.org/
ReCaptcha site: http://recaptcha.net/
Russian version of this article: http://swsoft.nsu.ru/WackoWiki/WackoWikiRecaptcha

Show Files (1 file)