Mecha CMS

Add extra functionality to the core engine.


Form protection using captcha.

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



<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>


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


Math Captcha 

Form protection using math question.

    $id = null,
    $min = 1,
    $max = 10,
    $text = [],
    $format = '%{a}% %{?}% %{b}%'


<span class="captcha captcha-math" id="captcha:foo">2 &#x002B; 5</span>
$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.

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


<img src="" alt="foo" class="captcha captcha-text" id="captcha:foo" width="96" height="32">
$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\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.

Token Captcha 

A type of captcha that simply return a token hash.

    $id = null,
    $hash = null,
    $html = true


<span class="captcha captcha-token" id="captcha:foo" contenteditable>1c6948fae7c880eb2dfa4f1b8ecb0e5ebc748d93</span>
$idThe captcha ID.
$hashSet random value here as the token hash, or use the default hash generated by Guard::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>

As toggle captcha:

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