Etienne Kneuss

home » repository » php » php.net » spam-protection.php
Tree:
school
mp3
php
sample
publications
Math_Derivative
Math_Expression
ircbot
php.net
docweb
lsb
check-acronyms.php
spam_tickets.sql.txt
spam-protection.php
genphpt.php
fix-whitespace.php
generrorlinks.php
remove-ordie.php
docbkphp.vim
script-skel.php
cvslogger.php
generateCvslogs.php
images
dump

Source code raw phps

    <?php  /**   * Requested to output a spam-protection challenge   **   * Output format:   *    * TicketID<NL>   * Question   * [<NL>serialized possible answers](optionnal)   */ @mysql_connect("localhost","nobody", "")   or die("[ERROR] failed to connect to database"); @mysql_select_db("phpmasterdb")   or die("[ERROR] failed to select database"); $spamChallengeCallbacks = array('spamChallenge_checkbox'); // randomly choose one of the challenge available $chosenChallenge = $spamChallengeCallbacks[array_rand($spamChallengeCallbacks)]; if(!function_exists($chosenChallenge)) {     //erroneous callback     echo '[ERROR] Invalid callback';     exit(1); } $return = call_user_func($chosenChallenge); // create a ticket $sql_query = 'INSERT INTO spam_tickets                      (`answer`, `date`)               VALUES (\''.mysql_real_escape_string($return[1]).'\', NOW())'; @mysql_query($sql_query) or die('[ERROR] Failed to generate ticket'); $uniqID = @mysql_insert_id(); //output echo $uniqID."\n".$return[0];  /**   * Callbacks:   ***   * return value of the call back:   * array( <html question>, <correct answer>)   */ function spamChallenge_checkbox() {     return array('You need to uncheck this box: <input type="checkbox" name="spamAnswer" checked />', '[unset]'); } function spamChallenge_equation() {     //initial settings     $nMaxCoefs = 3;     $nMaxNums  = 3;     $scrambleSignsCoefs = true;     $scrambleSignsNums  = true;          $answerRange = array(1, 100);          $coefRange   = array(1,3);          //pick a variable letter     $variables = range('a', 'h');     $var = $variables[array_rand($variables)];     //choose an answer     $init_answer = rand($answerRange[0], $answerRange[1]);     $init_coef   = rand($coefRange[0], $coefRange[1]);          $answer = $init_answer*$init_coef;     // explode the coefficient     $remaining = $init_coef;     $coefs = array();          for($i = 0; $i < $nMaxCoefs && $remaining >= 1; $i++) {         $coef       = rand(1, $remaining);         $remaining -=  $coef;         $coefs[]    = $coef;     }          if ($remaining > 0) {         $coefs[] = $remaining;     }     if ($scrambleSignsCoefs) {         $n = rand(0, count($coefs));         for($i=0; $i<$n; $i++) {             //pick one and revert its sign, compensate the result             $key = array_rand($coefs);             $tmp = $coefs[$key];             $coefs[$key] *= -1;             $coefs[array_rand($coefs)] += 2*$tmp;                      }     }     // explode the result     $remaining = $answer;     $nums = array();          for($i = 0; $i < $nMaxNums && $remaining >= 1; $i++) {         $num       = rand(1, $remaining);         $remaining -=  $num;         $nums[]    = $num;     }          if ($remaining > 0) {         $nums[] = $remaining;     }     if ($scrambleSignsNums) {         $n = rand(0, count($nums));         for($i=0; $i<$n; $i++) {             //pick one and revert its sign, compensate the result             $key = array_rand($nums);             $tmp = $nums[$key];             $nums[$key] *= -1;             $nums[array_rand($nums)] += 2*$tmp;                      }     }     // lets build the left side:     $left = '';     for($i = rand(1, count($coefs)+count($nums)-1); $i > 0; $i--) {         if (rand(0,1) && count($coefs)) {             $left .= ($number = array_pop($coefs)) < 0 ? ' - '.abs($number).'*'.$var : ' + '.$number.'*'.$var;         } else if (count($nums)) {             $left .= ($number = array_pop($nums)) < 0 ? ' + '.abs($number) : ' - '.$number;         } else {             $left .= ($number = array_pop($coefs)) < 0 ? ' - '.abs($number).'*'.$var : ' + '.$number.'*'.$var;         }     }     // lets build the right side:     $right = '';     while(count($coefs) || count($nums)) {         if (rand(0,1) && count($coefs)) {             $right .= ($number = array_pop($coefs)) < 0 ? ' + '.abs($number).'*'.$var : ' - '.$number.'*'.$var;         } else if (count($nums)) {             $right .= ($number = array_pop($nums)) < 0 ? ' - '.abs($number) : ' + '.$number;         } else {             $right .= ($number = array_pop($coefs)) < 0 ? ' + '.abs($number).'*'.$var : ' - '.$number.'*'.$var;         }     }     $eq = ltrim($left, ' +') .' = '. ltrim($right, ' +');     $question = 'What is the value of <em>'.$var.'</em> that satisfies this equation: '.$eq.'<br /> '.$var.' = <input type="text" name="spamAnswer" size="20" maxlength="40" value="" />';     return array($question, $init_answer); } ?>

Comments

There is currently no comment here.

Add a comment

Username:

Spam Challenge: 6+20=?

Comment: