From daa666dec4793a2ce9675dbb8b6c365d105e8986 Mon Sep 17 00:00:00 2001 From: Cook879 Date: Sun, 24 Aug 2014 23:57:00 +0300 Subject: [PATCH] Readd CAPTCHA (ConfirmEdit extension) support Bug: 66811 Change-Id: I46041c24fedc6643fc56f9f53cd59ea5803ebd85 --- ContactPage_body.php | 44 +++++++++++++++++++++++++++++++++++++++++++- i18n/en.json | 39 ++++++++++++++++++++------------------- i18n/qqq.json | 3 ++- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/ContactPage_body.php b/ContactPage_body.php index 02eb34b..5605723 100644 --- a/ContactPage_body.php +++ b/ContactPage_body.php @@ -182,6 +182,15 @@ class SpecialContact extends UnlistedSpecialPage { ); } + if ( $this->useCaptcha() ) { + $formItems['Captcha'] = array( + 'label-message' => 'captcha-label', + 'type' => 'info', + 'default' => $this->getCaptcha(), + 'raw' => true, + ); + } + $form = new HTMLForm( $formItems, $this->getContext(), "contactpage-{$this->formType}" ); $form->setWrapperLegendMsg( 'contactpage-legend' ); $form->setSubmitTextMsg( 'emailsend' ); @@ -214,7 +223,8 @@ class SpecialContact extends UnlistedSpecialPage { * string: Error message to display */ public function processInput( $formData ) { - global $wgUserEmailUseReplyTo, $wgPasswordSender; + global $wgUserEmailUseReplyTo, $wgPasswordSender, $wgCaptcha; + $config = $this->getTypeConfig(); $request = $this->getRequest(); @@ -338,6 +348,10 @@ class SpecialContact extends UnlistedSpecialPage { $text .= "{$name}: $value\n"; } + if ( $this->useCaptcha() && !$wgCaptcha->passCaptcha() ) { + return wfMessage( 'contactpage-captcha-error' )->plain(); + } + // Stolen from Special:EmailUser $error = ''; if ( !wfRunHooks( 'EmailUser', array( &$targetAddress, &$submitterAddress, &$subject, &$text, &$error ) ) ) { @@ -391,4 +405,32 @@ class SpecialContact extends UnlistedSpecialPage { private static function getYesOrNoMsg( $value ) { return wfMessage( $value ? 'htmlform-yes' : 'htmlform-no' )->inContentLanguage()->text(); } + + /** + * @return boolean True if CAPTCHA should be used, false otherwise + */ + private function useCaptcha() { + global $wgCaptchaClass, $wgCaptchaTriggers; + + return $wgCaptchaClass && + isset( $wgCaptchaTriggers['contactpage'] ) && + $wgCaptchaTriggers['contactpage'] && + !$this->getUser()->isAllowed( 'skipcaptcha' ); + } + + /** + * @return string CAPTCHA form HTML + */ + private function getCaptcha() { + // NOTE: make sure we have a session. May be required for CAPTCHAs to work. + wfSetupSession(); + + $captcha = ConfirmEditHooks::getInstance(); + $captcha->trigger = 'contactpage'; + $captcha->action = 'contact'; + + return '
' . + $captcha->getForm() . + "
\n"; + } } diff --git a/i18n/en.json b/i18n/en.json index 7ab032c..e92938d 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1,21 +1,22 @@ { - "@metadata": { - "authors": [ - "Daniel Kinzler", - "Reedy" - ] - }, - "contactpage": "Contact page", - "contactpage-desc": "[[Special:Contact|Contact form for visitors]]", - "contactpage-title": "Contact", - "contactpage-pagetext": "Please use the form below to contact us.", - "contactpage-legend": "Send email", - "contactpage-defsubject": "Contact message", - "contactpage-subject-and-sender": "$1 (from $2)", - "contactpage-subject-and-sender-withip": "$1 (from $2 at $3)", - "contactpage-fromname": "Your name:", - "contactpage-fromaddress": "Your email address:", - "contactpage-formfootnotes": "(needed if you want an answer)", - "contactpage-includeip": "Include my IP address in this message.", - "contactpage-usermailererror": "Mail object returned error:" + "@metadata": { + "authors": [ + "Daniel Kinzler", + "Reedy" + ] + }, + "contactpage": "Contact page", + "contactpage-desc": "[[Special:Contact|Contact form for visitors]]", + "contactpage-title": "Contact", + "contactpage-pagetext": "Please use the form below to contact us.", + "contactpage-legend": "Send email", + "contactpage-defsubject": "Contact message", + "contactpage-subject-and-sender": "$1 (from $2)", + "contactpage-subject-and-sender-withip": "$1 (from $2 at $3)", + "contactpage-fromname": "Your name:", + "contactpage-fromaddress": "Your email address:", + "contactpage-formfootnotes": "(needed if you want an answer)", + "contactpage-includeip": "Include my IP address in this message.", + "contactpage-usermailererror": "Mail object returned error:", + "contactpage-captcha-error": "CAPTCHA error" } diff --git a/i18n/qqq.json b/i18n/qqq.json index 2c40cf5..1f25237 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -22,5 +22,6 @@ "contactpage-fromaddress": "{{Identical|E-mail address}}", "contactpage-formfootnotes": "Message to notify users that an email address is needed if an answer is required", "contactpage-includeip": "Used as checkbox label in the ContactPage form.", - "contactpage-usermailererror": "Error message displayed when no email could be sent by the MediWiki UserMailer script." + "contactpage-usermailererror": "Error message displayed when no email could be sent by the MediWiki UserMailer script.", + "contactpage-captcha-error": "Error message displayed when there was a CAPTCHA error (i.e. the user failed to enter the correct CAPTCHA, or didn't enter one at all, etc.)" }