<?php

/**

TextCaptcha - Simple captcha that don't use images, session, cookies and using ascii art
              for a clue of what to type.

*/

$RecipeInfo['TextCaptcha']['Version'] = '2009-12-28';

SDV($TextCaptchaValue, rand(1000, 9999));

SDVA($TextCaptchaAscii[0], array( 0 => '   _ ',
                                  1 => '  / /',
                                  2 => ' /_/ '));
SDVA($TextCaptchaAscii[1], array( 0 => '   ',
                                  1 => '  /',
                                  2 => ' / '));
SDVA($TextCaptchaAscii[2], array( 0 => '   _',
                                  1 => '  _/',
                                  2 => ' /_ '));
SDVA($TextCaptchaAscii[3], array( 0 => '   _',
                                  1 => '  _/',
                                  2 => ' _/ '));
SDVA($TextCaptchaAscii[4], array( 0 => '    ',
                                  1 => ' /_/',
                                  2 => '  / '));
SDVA($TextCaptchaAscii[5], array( 0 => '   _ ',
                                  1 => '  /_ ',
                                  2 => ' _ / '));
SDVA($TextCaptchaAscii[6], array( 0 => '   _',
                                  1 => '  /_',
                                  2 => ' /_/'));
SDVA($TextCaptchaAscii[7], array( 0 => ' __',
                                  1 => '  /',
                                  2 => ' / '));
SDVA($TextCaptchaAscii[8], array( 0 => '   _ ',
                                  1 => '  /_/',
                                  2 => ' /_/ '));
SDVA($TextCaptchaAscii[9], array( 0 => '   _',
                                  1 => ' /_/',
                                  2 => '  / '));

//format the numbers above in one line
SDV($TextCaptchaAsciiFmt,
             "\n&nbsp;".$TextCaptchaAscii[substr($TextCaptchaValue,0,1)][0]
            .$TextCaptchaAscii[substr($TextCaptchaValue,1,1)][0]
            .$TextCaptchaAscii[substr($TextCaptchaValue,2,1)][0]
            .$TextCaptchaAscii[substr($TextCaptchaValue,3,1)][0]."\n"
            ."&nbsp;".$TextCaptchaAscii[substr($TextCaptchaValue,0,1)][1]
            .$TextCaptchaAscii[substr($TextCaptchaValue,1,1)][1]
            .$TextCaptchaAscii[substr($TextCaptchaValue,2,1)][1]
            .$TextCaptchaAscii[substr($TextCaptchaValue,3,1)][1]."\n"
            ."&nbsp;".$TextCaptchaAscii[substr($TextCaptchaValue,0,1)][2]
            .$TextCaptchaAscii[substr($TextCaptchaValue,1,1)][2]
            .$TextCaptchaAscii[substr($TextCaptchaValue,2,1)][2]
            .$TextCaptchaAscii[substr($TextCaptchaValue,3,1)][2]
);

// Put RequireTextCaptcha first
// on EditFunctions array
array_unshift($EditFunctions, 'RequireTextCaptcha');

$TextCaptchaCryptPass = TextCaptchaCrypt($TextCaptchaValue);
$TextCaptchaAsciiFmt = '<pre>'.$TextCaptchaAsciiFmt.'</pre>';

// Make some variables accessible with FmtPV
$FmtPV['$TextCaptchaPass'] = "'".$TextCaptchaCryptPass."'";
$FmtPV['$TextCaptchaClue'] = 'Keep("'.$TextCaptchaAsciiFmt.'")';

// Check the captcha sent by user
function RequireTextCaptcha(){
  global $MessagesFmt, $EnablePost,
         $TextCaptchaRequiredFmt;

  $resp = $_POST['captcharesp'];
  $pass = $_POST['captchapass'];

  if( $pass == TextCaptchaCrypt($resp)) return;
  SDV($TextCaptchaRequiredFmt, 
    "<div class='wikimessage'>$[You must enter valid code]</div>");
  $MessagesFmt[] = $TextCaptchaRequiredFmt;
  $EnablePost = 0;
}

// really simple encryption
// that you can expand
function TextCaptchaCrypt($x){
  $x = strrev($x);
  $x = strtr($x,'0123456789','7231894650');
  $x = base64_encode($x);
  return $x;
}