Allow several answers in QuestyCaptcha, per request at http://permalink.gmane.org...
[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                         Xml::element( 'input', array(
44                                 'name' => 'wpCaptchaWord',
45                                 'id'   => 'wpCaptchaWord',
46                                 'tabindex' => 1 ) ) . // tab in before the edit textarea
47                         "</p>\n" .
48                         Xml::element( 'input', array(
49                                 'type'  => 'hidden',
50                                 'name'  => 'wpCaptchaId',
51                                 'id'    => 'wpCaptchaId',
52                                 'value' => $index ) );
53         }
54
55         function getMessage( $action ) {
56                 $name = 'questycaptcha-' . $action;
57                 $text = wfMsg( $name );
58                 # Obtain a more tailored message, if possible, otherwise, fall back to
59                 # the default for edits
60                 return wfEmptyMsg( $name, $text ) ? wfMsg( 'questycaptcha-edit' ) : $text;
61         }
62
63         function showHelp() {
64                 global $wgOut;
65                 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
66                 $wgOut->addWikiText( wfMsg( 'questycaptchahelp-text' ) );
67                 if ( $this->storage->cookiesNeeded() ) {
68                         $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
69                 }
70         }
71 }