3 class CookieCaptcha extends SimpleCaptcha {
5 * Check if the submitted form matches the captcha session data provided
6 * by the plugin when the form was generated.
8 * @param string $answer
13 function keyMatch( $answer, $info ) {
14 global $wgCaptchaSecret;
16 $digest = $wgCaptchaSecret . $info['salt'] . $answer . $wgCaptchaSecret . $info['salt'];
17 $answerHash = substr( md5( $digest ), 0, 16 );
19 if ( $answerHash == $info['hash'] ) {
20 wfDebug( "FancyCaptcha: answer hash matches expected {$info['hash']}\n" );
23 wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
30 function addCaptchaAPI( &$resultArr ) {
31 $info = $this->pickImage();
33 $resultArr['captcha']['error'] = 'Out of images';
36 $index = $this->storeCaptcha( $info );
37 $title = SpecialPage::getTitleFor( 'Captcha', 'image' );
38 $resultArr['captcha']['type'] = 'image';
39 $resultArr['captcha']['mime'] = 'image/png';
40 $resultArr['captcha']['id'] = $index;
41 $resultArr['captcha']['url'] = $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) );
46 * Insert the captcha prompt into the edit form.
50 $info = $this->pickImage();
52 throw new MWException( "Ran out of captcha images" );
55 // Generate a random key for use of this captcha image in this session.
56 // This is needed so multiple edits in separate tabs or windows can
57 // go through without extra pain.
58 $index = $this->storeCaptcha( $info );
60 wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
62 $title = SpecialPage::getTitleFor( 'Captcha', 'image' );
65 Html::element( 'img', array(
66 'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
67 'width' => $info['width'],
68 'height' => $info['height'],
71 Html::element( 'input', array(
73 'name' => 'wpCaptchaId',
74 'id' => 'wpCaptchaId',
75 'value' => $index ) ) .
77 Html::element( 'label', array(
78 'for' => 'wpCaptchaWord',
79 ), parent::getMessage( 'label' ) . wfMessage( 'colon-separator' )->text() ) .
80 Html::element( 'input', array(
81 'name' => 'wpCaptchaWord',
82 'id' => 'wpCaptchaWord',
84 'autocorrect' => 'off',
85 'autocapitalize' => 'off',
86 'required' => 'required',
87 'tabindex' => 1 ) ) . // tab in before the edit textarea
93 * Show a message asking the user to enter a captcha on edit
94 * The result will be treated as wiki text
96 * @param $action string Action being performed
100 function getMessage( $action ) {
101 $name = 'fancycaptcha-' . $action;
102 $text = wfMessage( $name )->text();
103 # Obtain a more tailored message, if possible, otherwise, fall back to
104 # the default for edits
105 return wfMessage( $name, $text )->isDisabled() ?
106 wfMessage( 'fancycaptcha-edit' )->text() : $text;