Big changes to extensions' documentation:
[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                 return strtolower( $answer ) == strtolower( $info['answer'] );
16         }
17
18         function addCaptchaAPI( &$resultArr ) {
19                 $captcha = $this->getCaptcha();
20                 $index = $this->storeCaptcha( $captcha );
21                 $resultArr['captcha']['type'] = 'question';
22                 $resultArr['captcha']['mime'] = 'text/plain';
23                 $resultArr['captcha']['id'] = $index;
24                 $resultArr['captcha']['question'] = $captcha['question'];
25         }
26
27         function getCaptcha() {
28                 global $wgCaptchaQuestions;
29                 return $wgCaptchaQuestions[mt_rand( 0, count( $wgCaptchaQuestions ) - 1 )]; // pick a question, any question
30         }
31
32         function getForm() {
33                 $captcha = $this->getCaptcha();
34                 if ( !$captcha ) {
35                         die( "No questions found; set some in LocalSettings.php using the format from QuestyCaptcha.php." );
36                 }
37                 $index = $this->storeCaptcha( $captcha );
38                 return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
39                         Xml::element( 'input', array(
40                                 'name' => 'wpCaptchaWord',
41                                 'id'   => 'wpCaptchaWord',
42                                 'tabindex' => 1 ) ) . // tab in before the edit textarea
43                         "</p>\n" .
44                         Xml::element( 'input', array(
45                                 'type'  => 'hidden',
46                                 'name'  => 'wpCaptchaId',
47                                 'id'    => 'wpCaptchaId',
48                                 'value' => $index ) );
49         }
50
51         function getMessage( $action ) {
52                 $name = 'questycaptcha-' . $action;
53                 $text = wfMsg( $name );
54                 # Obtain a more tailored message, if possible, otherwise, fall back to
55                 # the default for edits
56                 return wfEmptyMsg( $name, $text ) ? wfMsg( 'questycaptcha-edit' ) : $text;
57         }
58
59         function showHelp() {
60                 global $wgOut, $ceAllowConfirmedEmail;
61                 $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
62                 $wgOut->addWikiText( wfMsg( 'questycaptchahelp-text' ) );
63                 if ( $this->storage->cookiesNeeded() ) {
64                         $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
65                 }
66         }
67 }