Extension · Mecha CMS

Add extra functionality to the core engine.

Captcha

Author: Taufik Nurrohman · 725 Views

Tweet

Form protection using captcha. Captcha is a type of challenge-response test used in computing to determine whether or not the user is human.

Download Version 1.4.0 Download Development Version

Usage 

Input 

<form method="post">
  <p><?php echo Captcha::math('foo'); ?></p>
  <p><?php echo Form::text('captcha'); ?></p>
  <p><?php echo Form::submit(null, null, 'Submit'); ?></p>
</form>

Output 

Captcha::check($input, $id, $fail = false);
if (Captcha::check(Request::post('captcha'), 'foo') !== false) {
    // Passed the test!
}

Features 

Math Captcha 

Form protection using math question.

Captcha::math(
    $id = null,
    $min = 1,
    $max = 10,
    $text = [],
    $format = '%{a}% %{?}% %{b}%'
);

Result:

<span class="captcha captcha-math" id="captcha:foo">2 &#x002B; 5</span>
ParameterDescription
$idThe captcha ID.
$minRange minimum of the randomly generated number.
$maxRange maximum of the randomly generated number.
$textList of text used to alter the output number.
$format%{a}% for the first random number; %{b}% for the second random number; %{?}% for the math operator.

Example of using the $text parameter:

echo Captcha::math('foo', 1, 5, [
    1 => 'one',
    2 => 'two',
    3 => 'three',
    4 => 'four',
    5 => 'five',
    '+' => 'plus',
    '-' => 'minus'
]);

Result will be like:

<span class="captcha captcha-math" id="captcha:foo">one plus five</span>

Text Captcha 

Form protection using text image question.

Captcha::text(
    $id = null,
    $background = false,
    $color = '000',
    $size = 16,
    $width = 6,
    $height = 2,
    $text = 7,
    $font = '0'
);

Result:

<img src="http://mecha-cms.com/captcha.png?id=foo" alt="foo" class="captcha captcha-text" id="captcha:foo" width="96" height="32">
ParameterDescription
$idThe captcha ID.
$backgroundText background color using HEX color code without the hash prefix or false for transparent background.
$colorText color using HEX color code without the hash prefix.
$sizeFont size in px.
$widthImage width in em relative to $size.
$heightImage height in em relative to $size.
$textText length.
$fontTrue type font file name without the file extension, stored in lot\extend\captcha\lot\asset\ttf.

Note: I have removed some of the glyphs in the font to make it only contains [a-zA-Z0-9] to reduce the file size.

Toggle Captcha 

Form protection using a hash value that is not included by default.

Captcha::toggle(
    $id = null,
    $text = null
);

Result:

<input name="captcha" value="58e87c6947e7f" type="checkbox" class="captcha captcha-toggle" id="captcha:foo">
<label for="captcha:foo">I am not a robot.</label>
ParameterDescription
$idThe captcha ID.
$textThe toggle label. If not defined, it will depends on the $language->captcha_toggle value.

Note: This is the only captcha type that has a form input included in the output. If you ever need the token hash only, try Token Captcha.

Token Captcha 

A type of captcha that will only return a token hash.

Captcha::token(
    $id = null,
    $hash = null,
    $html = true
);

Result:

<span class="captcha captcha-token" id="captcha:foo" contenteditable>1c6948fae7c880eb2dfa4f1b8ecb0e5ebc748d93</span>
ParameterDescription
$idThe captcha ID.
$hashSet random value here, as the token hash, or use the default hash generated by Guardian::hash().
$htmlSet to false to output only the token hash.

Example of using the $hash parameter:

echo Captcha::token('foo', uniqid());
echo Captcha::token('foo', array_rand([0, 1, 2, 3, 4, 5]));
echo Captcha::token('foo', function($hash) {
    return $hash . time();
});

The output token will becomes the answer. Do whatever you want with this. Here are some examples:

As copy and paste captcha:

<form method="post">
  <p>Copy the token below and paste it to the text area provided.</p>
  <p><?php echo Captcha::token('foo'); ?></p>
  <p><?php echo Form::textarea('captcha'); ?></p>
  <p><?php echo Form::submit(null, null, 'Submit'); ?></p>
</form>

As toggle captcha:

<?php echo Form::checkbox('captcha', Captcha::token('foo', null, false), false, 'I am not a robot.'); ?>

Dependency 

Extension