File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/lib/captcha/captcha.php
<?php
/**
* Captcha Class
*
* This class generates, manages and outputs the Captcha images in order to prevent
* automated form submittion which causes SPAM.
*
* It detects if the server has GD running, if so it uses that, otherwise it
* uses static images to generate the Captcha Code.
*
* @version $Id: class.captcha.php,v 1.16 2008/03/06 23:12:37 jordie Exp $
* @author Jordie Bodlay <jordie@interspire.com>
* @copyright Copyright (c) 2004-2006 Interspire Pty. Ltd.
* @package IWP
*
*/
class captcha {
/**
* Holds the secret captcha code.
*
* @var string
*/
public $__secret;
/**
* Holds the session variable name to use for secret storage. This only needs changing if you wish to maintain two CAPTCHA codes in one session.
*
* @var string
*/
public $sessionKey = 'captchaCode';
/**
* Determines the length of the code. Default is 6.
*
* @var integer
*/
public $length;
/**
* Contains the path to the TTF font file to be used for GD.
*
* @var string
*/
public $font;
/**
* This is the URL used to view the image
*
* @var string
*/
public $CaptchaURL;
/**
* Determines the size of the font when using GD
*
* @var integer
*/
public $fontSize;
/**
* Determines the color of the font when using GD
*
* @var string
*/
public $textColor;
/**
* Determines one of the gradient values for the image background when
* using GD
*
* @var string
*/
public $bgCol1;
/**
* Determines one of the gradient values for the image background when
* using GD
*
* @var string
*/
public $bgCol2;
/**
* List of fonts available to Content Manager
*
* @var array
*/
public $fontlist = array();
/**
* Determines the shape of the gradient fill in the image background when
* using GD
*
* @var string
*/
public $bgFillStyle;
public $width = "110";
public $height = "40";
/**
* Constructor
*
* Sets variables needed by the class
*/
public function __construct() {
// Detect if the server has GD installed or not
// Set variables for later use
// all variables
$this->length = 6;
// gd type captcha variables
$this->AddFont('tuffy.ttf');
$this->AddFont('scribble.ttf');
$this->fontSize = '20';
$this->textColor = '000000';
$this->bgCol1 = '#BFBFBF';
$this->bgCol2 = '#ffffff';
$this->bgFillStyle = 'square';
}
/**
* AddFont
*
* Adds a font to the list of fonts
*
* @return void
*/
public function AddFont($file, $path = null) {
if ($path == null) {
$path = dirname(__FILE__) . '/';
}
$this->fontlist[] = str_replace("//", "/", $path . $file);
}
/**
* AL_CAPTCHA::LoadFont()
*
* Gets a random font from the list
*
* @return string;
*/
public function LoadFont() {
$random = array_rand($this->fontlist);
return $this->fontlist[$random];
}
/**
* AL_CAPTCHA::CreateSecret()
*
* Generates a new random secret captcha code
*
* @return true
*/
public function CreateSecret() {
// get random characters, set the secret variable to it
$this->__secret = $this->GetRandom($this->length);
//set the session variable
$this->SetSecret();
return true;
}
/**
* AL_CAPTCHA::GetSecret()
*
* Detects if there is already a secret saved, if so the public function returns the secret
* otherwise it generates a new one.
*
* @return string
*/
public function GetSecret() {
if (!isset($this->__secret) or $this->__secret == '') {
// if the secret is not already set, create it
return $this->LoadSecret();
} else {
// otherwise return it
return $this->__secret;
}
}
/**
* AL_CAPTCHA::LoadSecret()
*
* If the secret is stored in the Session, retrieve and decode it
* Otherwise create a new secret.
*
* @return secret
*/
public function LoadSecret() {
// if the secret stored in the session, retreive it
// otherwise create a new secret
if (isset($_SESSION[$this->sessionKey])) {
$this->__secret = base64_decode($_SESSION[$this->sessionKey]);
} else {
$this->CreateSecret();
}
return $this->__secret;
}
/**
* AL_CAPTCHA::SetSecret()
*
* Sets the session variable to the current secret code
*
* @return unknown
*/
public function SetSecret() {
// delete current secret
unset($_SESSION[$this->sessionKey]);
// set new secret to the session
if (($_SESSION[$this->sessionKey] = base64_encode($this->GetSecret()))) {
return true;
} else {
return false;
}
}
/**
* AL_CAPTCHA::GetRandom()
*
* Generates a string of random alphanumeric characters of a length
* determined by $length
*
* @param integer $length
* @return string
*/
public function GetRandom($length = 5) {
// init
$returnRandom = '';
// make sure its an integer
$length = (int)$length;
$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9');
for ($i = 0; $i < $length; $i++) {
$key = array_rand($chars);
$returnRandom .= $chars[$key];
}
return $returnRandom;
}
/**
* AL_CAPTCHA::LoadImage()
*
* Outputs an image determined by $this->type
*
* @param char $letter
* @return binary
*/
public function LoadImage($letter = '') {
$this->font = $this->LoadFont();
// buffer everything so its all returned together
ob_start();
// are we using a preset background image
// if (!is_file($this->bg)) {
$width = $this->width;
$height = $this->height;
$img_handle = imageCreate($width, $height);
// } else {
// $img_handle = imageCreateFromPNG($this->bg);
// }
// grab text color
$col = hex2rgb($this->textColor);
// set background
gd_gradient_fill($img_handle, $this->bgFillStyle, $this->bgCol1, $this->bgCol2);
// use the image value we set, if its not valid, default to black
if ($col) {
$text_color = ImageColorAllocate($img_handle, $col['r'], $col['g'], $col['b']);
} else {
$text_color = ImageColorAllocate($img_handle, 0, 0, 0);
}
if (function_exists('imageline')) {
$line = imagecolorallocate($img_handle, 100, 100, 100);
$xmin = 0;
$xmax = $width;
$ymin = 0;
$ymax = $height;
$rand = rand(3, 10);
for ($i = 0; $i < $rand; $i++) {
imageline($img_handle, rand($xmin, $xmax), rand($ymin, $ymax), rand($xmin, $xmax), rand($ymin, $ymax), $line);
}
}
$x = 0;
// if the font-file exists then use it, otherwise, use the GD default text
if (file_exists($this->font) && function_exists("imagettftext")) {
$length = strlen($this->__secret);
for ($i = 0; $i < $length; $i++) {
$x = $x + (12 + ($i));
imagettftext($img_handle, $this->fontSize, rand(-4, 3), $x, 30 + rand(-1, 1), $text_color, $this->font, $this->__secret{$i});
}
} else {
ImageString($img_handle, 5, 20, 13, $this->__secret, $text_color);
}
// create the image
ImagePng($img_handle);
ImageDestroy($img_handle);
$content = ob_get_contents();
ob_end_clean();
return trim($content);
}
/**
* AL_CAPTCHA::OutputImage()
*
* Outputs the image header, then the content of the image
*
*/
public function OutputImage() {
$this->LoadSecret();
// send several headers to make sure the image is not cached
// a date in the past
header("Expires: Mon, 23 Jul 1993 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0, max-age=0", false);
header('Content-type: image/png');
echo $this->LoadImage();
die();
}
/**
* AL_CAPTCHA::ShowCaptcha()
*
* Returns the html img tags for the captcha image(s)
*
* @return string
*/
public function ShowCaptcha() {
// single GD generated image
$return = '';
$return .= "<img src='" . $this->CaptchaURL. "?".rand(2500,26663569);
if ($this->sessionKey != 'captchaCode') {
$return .= "&for=". iwp_htmlspecialchars(urlencode($this->sessionKey));
}
$return .= "' width='" . $this->width . "' height='" . $this->height . "' alt='Security Image' />";
return $return;
}
}