From 1af011759fbfe2003273e859440e0d3bc211e528 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sat, 18 Sep 2010 13:57:31 +0200 Subject: Update FunnyQuestion extension * use MediaWiki hooks to add custom form entries on user creation * make extension translatable --- extensions/FunnyQuestion/FunnyQuestion.body.php | 108 ++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 extensions/FunnyQuestion/FunnyQuestion.body.php (limited to 'extensions/FunnyQuestion/FunnyQuestion.body.php') diff --git a/extensions/FunnyQuestion/FunnyQuestion.body.php b/extensions/FunnyQuestion/FunnyQuestion.body.php new file mode 100644 index 00000000..04d25f8d --- /dev/null +++ b/extensions/FunnyQuestion/FunnyQuestion.body.php @@ -0,0 +1,108 @@ +getCode()]) ? $wgFunnyQuestions[$wgLang->getCode()] : 'en'); + } + + private static function getFunnyQuestion() { + global $wgFunnyQuestionHash, $wgFunnyQuestions; + + $question = array_rand($wgFunnyQuestions[self::getLang()]); + $time = time(); + # make sure the user is not able to tell us the question to answer + $hash = sha1($time.$question.$wgFunnyQuestionHash); + + return array('question' => $question, 'time' => $time, 'hash' => $hash); + } + + private static function checkFunnyQuestion() { + global $wgFunnyQuestionHash, $wgFunnyQuestions, $wgFunnyQuestionTimeout, $wgFunnyQuestionWait; + + if (!empty($_POST['FunnyQuestionTime']) + && !empty($_POST['FunnyQuestionHash']) + && !empty($_POST['FunnyAnswer'])) { + $now = time(); + $time = $_POST['FunnyQuestionTime']; + $hash = $_POST['FunnyQuestionHash']; + $userAnswer = self::normalizeAnswer($_POST['FunnyAnswer']); + } else { + return false; + } + + if ($now - $time > $wgFunnyQuestionTimeout) { + return false; + } elseif ($now - $time < $wgFunnyQuestionWait) { + return false; + } + + foreach ($wgFunnyQuestions[self::getLang()] as $question => $answers) { + if (!is_array($answers)) { + $answers = array($answers); + } + foreach ($answers as $answer) { + if (self::normalizeAnswer($answer) == $userAnswer + && $hash == sha1($time.$question.$wgFunnyQuestionHash)) { + return true; + } + } + } + + return false; + } + + public static function addFunnyQuestionToEditPage($editpage, $output) { + global $wgUser; + + if (!$wgUser->isLoggedIn()) { + $funnyQuestion = self::getFunnyQuestion(); + $editpage->editFormTextAfterWarn .= + '
+ + + + +
'; + } + return true; + } + + public static function checkFunnyQuestionOnEditPage($editpage, $text, $section, $error) { + global $wgUser; + + if (!$wgUser->isLoggedIn() && !self::checkFunnyQuestion()) { + $error = '
'.wfMsg('wrong-answer').'

'; + } + return true; + } + + public static function addFunnyQuestionToUserCreateForm($template) { + $funnyQuestion = self::getFunnyQuestion(); + $template->addInputItem('FunnyAnswer', '', 'text', 'question-label', 'question-'.sha1($funnyQuestion['question'])); + $template->addInputItem('FunnyQuestionTime', $funnyQuestion['time'], 'hidden', ''); + $template->addInputItem('FunnyQuestionHash', $funnyQuestion['hash'], 'hidden', ''); + return true; + } + + public static function checkFunnyQuestionOnAbortNewAccount($user, $message) { + if (!self::checkFunnyQuestion()) { + $message = wfMsg('wrong-answer'); + return false; + } else { + return true; + } + } + +} + +?> + -- cgit v1.2.3-54-g00ecf