Captcha for Coppermine

Solution based on this code. Edit register.php and add the include which displays the input code just before the "if ($errors)" statement (somewhere around line 178). At around line 247 add the include for the captcha check code just before the "if ($error != '') return false;" statement. Diff output for the register.php of Coppermine 1.4.20:

178,180d177
<     // Display captcha code and input field
<     include("./plugins/captcha/input.php");
<         
250,253d246
<     // Check captcha code
<     include("./plugins/captcha/check.php");
<         
< 

plugins/captcha/input.php:

<tr>
  <td colspan="2" class="tableb">
    <table>
      <tr>
        <td><img src="plugins/captcha/image.php" width="80" height="40" alt="Captcha Code"></td>
        <td>
          Enter the captcha code (6 letters or digits, case insensitive) 
          shown in the image to prove that you're a human being and not 
          a SPAM robot (reload page if unreadable):
          <input type="text" name="captcha_code" size="6" maxlen="6">
        </td>
      </tr>
    </tabe>
  </td>
</tr>

plugins/captcha/check.php:

<?php

session_start();

if (!$_REQUEST['captcha_code'])
{ $error .= "<li>Captcha code is missing";
}
elseif (md5(strtolower($_REQUEST['captcha_code'])) != $_SESSION['captcha_md5sum'])
{ $error .= "<li>You entered the wrong Captcha code!";
}
?>

plugins/captcha/image.php:

<?php
session_start();
$passlen = 6;
$height = 40;
$width = 80;
$possiblechars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789";
$captcha_code = "";

for($i = 0; $i<$passlen; $i++)
{ $captcha_code .= substr($possiblechars, rand(0,strlen($possiblechars)-1), 1);
}

$_SESSION['captcha_md5sum'] = md5(strtolower($captcha_code));

$image = imageCreateTrueColor($width, $height);

$ar = rand(80, 255); $er = rand(80,255);
$ag = rand(80, 255); $eg = rand(80,255);
$ab = rand(80, 255); $eb = rand(80,255);
$sr = ($er-$ar)/$height;
$sg = ($eg-$ag)/$height;
$sb = ($eb-$ab)/$height;

for($i=0; $i<$height; $i++)
{ $r = $ar + $i*$sr;
  $g = $ag + $i*$sg;
  $b = $ab + $i*$sb;
  $color = imagecolorallocate($image, $r, $g, $b);
  imageline($image, 0, $height-$i, $width, $height-$i, $color);
}

for($i=1; $i<=100; $i++)
{ $color = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));
  imagesetpixel($image, rand(0,$width-1), rand(0,$height-1), $color);
}

for($i=0; $i<$passlen; $i++)
{ $r = rand(0, 200);
  $g = rand(0, 200);
  $b = rand(0, 200);
  $textcolor = imageColorAllocate($image, $r, $g, $b);
  $shadcolor = imageColorAllocate($image, 255-$r, 255-$g, 255-$b);
  $font = 5;
  $x = 2+ ($width-4)/$passlen*$i+rand(0,6);
  $y = $height/2-rand(0, imagefontheight($font));
  $char = substr($captcha_code,$i,1);
  imagestring($image, $font, $x+1, $y+1, $char, $shadcolor);
  imagestring($image, $font, $x, $y, $char, $textcolor);
}

$bordercolor = imagecolorallocate($image, 80, 80, 80);
imagerectangle($image,0,0,$width-1,$height-1,$bordercolor);

header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-type: image/png');
imagePNG($image);
imageDestroy($image);
?>