* @version 1.0
-------------------------------------------------------------------------------------
// Example.
// Instantiate new Captcha. This automatically generates a new 4-digit captcha number.
$captcha = new Captcha();
printAsciiNumber() ?>
printForm() ?>
-------------------------------------------------------------------------------------
*/
class Captcha {
var $secret_key = 'some random seed text for the md5';
var $random_number;
var $ascii_numbers = array(
array(
' ##### ',
' ## ## ',
'## ## ',
'## ## ',
'## ## ',
' ## ## ',
' ##### ',
), array(
' ## ',
' #### ',
' ## ',
' ## ',
' ## ',
' ## ',
' ###### ',
), array(
' ####### ',
'## ## ',
' ## ',
' ####### ',
'## ',
'## ',
'######### ',
), array(
' ####### ',
'## ## ',
' ## ',
' ####### ',
' ## ',
'## ## ',
' ####### ',
), array(
'## ',
'## ## ',
'## ## ',
'## ## ',
'######### ',
' ## ',
' ## ',
), array(
'######## ',
'## ',
'## ',
'####### ',
' ## ',
'## ## ',
' ###### ',
), array(
' ####### ',
'## ## ',
'## ',
'######## ',
'## ## ',
'## ## ',
' ####### ',
), array(
'######## ',
'## ## ',
' ## ',
' ## ',
' ## ',
' ## ',
' ## ',
), array(
' ####### ',
'## ## ',
'## ## ',
' ####### ',
'## ## ',
'## ## ',
' ####### ',
), array(
' ####### ',
'## ## ',
'## ## ',
' ######## ',
' ## ',
'## ## ',
' ####### ',
)
);
/**
* Constructor. Initialized new random number.
*
* @access public
* @author Quinn Comendant
* @since 20 Jan 2006 13:08:22
*/
function Captcha()
{
$this->secret_key = App::getParam('signing_key');
$this->random_number = $this->_getRandomNumber();
}
/**
* Print ASCII number.
*
* @access public
* @param mixed $num Number to convert.
* @return string ASCII-ized number
* @author Quinn Comendant
* @since 07 Dec 2005 21:59:25
*/
function getAsciiNumber($num=null)
{
if (!isset($num)) {
$num = $this->random_number;
}
if (preg_match('/[^\d]/', $num)) {
App::logMsg(sprintf('Bad number: %s', $num), LOG_ERR, __FILE__, __LINE__);
return false;
}
// Number must be an array of strings.
$num = preg_split('// ', strval($num), -1, PREG_SPLIT_NO_EMPTY);
// All chars must be same height.
$output = '';
$char_height = sizeof($this->ascii_numbers[0]);
for ($i=0; $i<$char_height; $i++) {
foreach ($num as $n) {
$output .= $this->ascii_numbers[$n][$i];
}
$output .= "\n";
}
return $output;
}
/**
* Prints the captcha ASCII numbers.
*
* @access public
* @author Quinn Comendant
* @since 07 Dec 2005 22:09:04
*/
function printAsciiNumber()
{
$ascii = $this->getAsciiNumber($this->random_number);
?>
* @since 07 Dec 2005 22:09:04
*/
function printForm()
{
$hash = $this->_getMD5key($this->random_number);
?>
* @since 07 Dec 2005 22:19:33
*/
function valid()
{
$number = getFormData('sc-captcha-input');
$hash = getFormData('sc-captcha-hash');
if ('' == $number . $hash) {
return false;
}
return $this->_getMD5key($number) == $hash;
}
/**
* Generate random number 3-to-5 digits long.
*
* @access private
* @return int Generated number.
* @author Quinn Comendant
* @since 07 Dec 2005 21:40:25
*/
function _getRandomNumber()
{
return substr(strval(rand(10000, 99999)), 0, rand(3, 5));
}
/**
* Generate md5 hash of number using secret key.
*
* @access private
* @param string $input Input text to whack.
* @return string md5 sum of input + seed
* @author Quinn Comendant
* @since 07 Dec 2005 21:53:35
*/
function _getMD5key($input)
{
return md5($this->secret_key . $input);
}
}
?>