Ideas for improved versions.
[toast/cookiecaptcha.git] / QuestyCaptcha.class.php
1 <?php
2
3 /**
4  * QuestyCaptcha class
5  *
6  * @file
7  * @author Benjamin Lees <emufarmers@gmail.com>
8  * @ingroup Extensions
9  */
10
11 class QuestyCaptcha extends SimpleCaptcha {
12
13         /** Validate a captcha response */
14         function keyMatch( $answer, $info ) {
15                 if ( is_array( $info['answer'] ) ) {
16                         return in_array( strtolower( $answer ), $info['answer'] );
17                 } else {
18                         return strtolower( $answer ) == strtolower( $info['answer'] );
19                 }
20         }
21
22         function addCaptchaAPI( &$resultArr ) {
23                 $captcha = $this->getCaptcha();
24                 $index = $this->storeCaptcha( $captcha );
25                 $resultArr['captcha']['type'] = 'question';
26                 $resultArr['captcha']['mime'] = 'text/plain';
27                 $resultArr['captcha']['id'] = $index;
28                 $resultArr['captcha']['question'] = $captcha['question'];
29         }
30
31         function getCaptcha() {
32                 global $wgCaptchaQuestions;
33                 return $wgCaptchaQuestions[mt_rand( 0, count( $wgCaptchaQuestions ) - 1 )]; // pick a question, any question
34         }
35
36         function getForm() {
37                 $captcha = $this->getCaptcha();
38                 if ( !$captcha ) {
39                         die( "No questions found; set some in LocalSettings.php using the format from QuestyCaptcha.php." );
40                 }
41                 $index = $this->storeCaptcha( $captcha );
42                 return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
43                         Html::element( 'input', array(
44                                 'name' => 'wpCaptchaWord',
45                                 'id'   => 'wpCaptchaWord',
46                                 'required',
47                                 'tabindex' => 1 ) ) . // tab in before the edit textarea
48                         "</p>\n" .
49                         Xml::element( 'input', array(
50                                 'type'  => 'hidden',
51                                 'name'  => 'wpCaptchaId',
52                                 'id'    => 'wpCaptchaId',
53                                 'value' => $index ) );
54         }
55
56         function getMessage( $action ) {
57                 $name = 'questycaptcha-' . $action;
58                 $text = wfMessage( $name )->text();
59                 # Obtain a more tailored message, if possible, otherwise, fall back to
60                 # the default for edits
61                 return wfMessage( $name, $text )->isDisabled() ? wfMessage( 'questycaptcha-edit' )->text() : $text;
62         }
63
64         function showHelp() {
65                 global $wgOut;
66                 $wgOut->setPageTitle( wfMessage( 'captchahelp-title' )->text() );
67                 $wgOut->addWikiMsg( 'questycaptchahelp-text' );
68                 if ( CaptchaStore::get()->cookiesNeeded() ) {
69                         $wgOut->addWikiMsg( 'captchahelp-cookies-needed' );
70                 }
71         }
72 }