summaryrefslogtreecommitdiff
path: root/extensions
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2010-09-18 13:57:31 +0200
committerPierre Schmitz <pierre@archlinux.de>2010-09-18 13:57:31 +0200
commit1af011759fbfe2003273e859440e0d3bc211e528 (patch)
tree6b3afea29f336e33bc236257f8a902e0c55d87e9 /extensions
parent91da036ca57d692fba4faad573aa4bca1435eda7 (diff)
Update FunnyQuestion extension
* use MediaWiki hooks to add custom form entries on user creation * make extension translatable
Diffstat (limited to 'extensions')
-rw-r--r--extensions/FunnyQuestion.php121
-rw-r--r--extensions/FunnyQuestion/FunnyQuestion.body.php108
-rw-r--r--extensions/FunnyQuestion/FunnyQuestion.i18n.php26
-rw-r--r--extensions/FunnyQuestion/FunnyQuestion.php33
4 files changed, 167 insertions, 121 deletions
diff --git a/extensions/FunnyQuestion.php b/extensions/FunnyQuestion.php
deleted file mode 100644
index 62f196d6..00000000
--- a/extensions/FunnyQuestion.php
+++ /dev/null
@@ -1,121 +0,0 @@
-<?php
-
-$wgExtensionCredits['other'][] = array(
- 'name' => 'FunnyQuestion',
- 'version' => '1.0',
- 'description' => 'Challenge-response authentication',
- 'author' => 'Pierre Schmitz',
- 'url' => 'https://www.archlinux.de'
-);
-
-if ($wgGroupPermissions['*']['edit']) {
- $wgHooks['EditPage::showEditForm:fields'][] = 'FunnyQuestion::addFunnyQuestionToEditPage';
- $wgHooks['EditFilter'][] = 'FunnyQuestion::checkFunnyQuestionOnEditPage';
-}
-
-if ($wgGroupPermissions['*']['createaccount'] && (empty($wgAuth) || $wgAuth->canCreateAccounts())) {
- $wgHooks['UserCreateForm'][] = 'FunnyQuestion::addFunnyQuestionToUserCreateForm';
- $wgHooks['AbortNewAccount'][] = 'FunnyQuestion::checkFunnyQuestionOnAbortNewAccount';
-}
-
-
-class FunnyQuestion {
-
-private static $defaultQuestion = array(
- "What is the Ultimate Answer to the Ultimate Question of Life, The Universe, and Everything?" => "42");
-
-private static function normalizeAnswer($answer) {
- return preg_replace('/[^a-z0-9]/', '', strtolower($answer));
-}
-
-private static function getFunnyQuestion() {
- global $IP, $wgFunnyQuestionHash, $wgFunnyQuestions;
-
- !isset($wgFunnyQuestions) && $wgFunnyQuestions = self::$defaultQuestion;
- !isset($wgFunnyQuestionHash) && $wgFunnyQuestionHash = $IP;
- $question = array_rand($wgFunnyQuestions);
- $answer = self::normalizeAnswer($wgFunnyQuestions[$question]);
- $time = time();
- # make sure the user is not able to tell us the question to answer
- $hash = sha1($time.$question.$answer.$wgFunnyQuestionHash);
-
- return '<div>
- <label for="FunnyAnswerField"><strong>'.$question.'</strong></label>
- <input id="FunnyAnswerField" type="text" name="FunnyAnswer" size="'.strlen($answer).'" value="" />
- <input type="hidden" name="FunnyQuestionTime" value="'.$time.'" />
- <input type="hidden" name="FunnyQuestionHash" value="'.$hash.'" />
- </div>';
-}
-
-private static function checkFunnyQuestion() {
- global $IP, $wgFunnyQuestionHash, $wgFunnyQuestions, $wgFunnyQuestionTimeout, $wgFunnyQuestionWait;
-
- # set some sane defaults
- # can be overridden in LocalSettings.php
- !isset($wgFunnyQuestions) && $wgFunnyQuestions = self::$defaultQuestion;
- !isset($wgFunnyQuestionHash) && $wgFunnyQuestionHash = $IP;
- !isset($wgFunnyQuestionTimeout) && $wgFunnyQuestionTimeout = 3600;
- !isset($wgFunnyQuestionWait) && $wgFunnyQuestionWait = 2;
-
- if (!empty($_POST['FunnyQuestionTime'])
- && !empty($_POST['FunnyQuestionHash'])
- && !empty($_POST['FunnyAnswer'])) {
- $now = time();
- $time = $_POST['FunnyQuestionTime'];
- $hash = $_POST['FunnyQuestionHash'];
- $answer = self::normalizeAnswer($_POST['FunnyAnswer']);
- } else {
- return false;
- }
-
- if ($now - $time > $wgFunnyQuestionTimeout) {
- return false;
- } elseif ($now - $time < $wgFunnyQuestionWait) {
- return false;
- }
-
- foreach (array_keys($wgFunnyQuestions) as $question) {
- if ($hash == sha1($time.$question.$answer.$wgFunnyQuestionHash)) {
- return true;
- }
- }
-
- return false;
-}
-
-
-public static function addFunnyQuestionToEditPage($editpage, $output) {
- global $wgUser;
-
- if (!$wgUser->isLoggedIn()) {
- $editpage->editFormTextAfterWarn .= self::getFunnyQuestion();
- }
- return true;
-}
-
-public static function checkFunnyQuestionOnEditPage($editpage, $text, $section, $error) {
- global $wgUser;
-
- if (!$wgUser->isLoggedIn() && !self::checkFunnyQuestion()) {
- $error = '<div class="errorbox">Your answer was wrong!</div><br clear="all" />';
- }
- return true;
-}
-
-public static function addFunnyQuestionToUserCreateForm($template) {
- $template->set('header', self::getFunnyQuestion());
- return true;
-}
-
-public static function checkFunnyQuestionOnAbortNewAccount($user, $message) {
- if (!self::checkFunnyQuestion()) {
- $message = 'Your answer was wrong!';
- return false;
- } else {
- return true;
- }
-}
-
-}
-
-?>
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 @@
+<?php
+
+
+class FunnyQuestion {
+
+ private static function normalizeAnswer($answer) {
+ return preg_replace('/[^a-z0-9]/', '', strtolower($answer));
+ }
+
+ private static function getLang() {
+ global $wgLang;
+
+ return (!empty($wgFunnyQuestions[$wgLang->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 .=
+ '<div class="editOptions">
+ <label for="FunnyAnswerField"><strong>'
+ .wfMsg('question-'.sha1($funnyQuestion['question'])).'</strong></label>
+ <input id="FunnyAnswerField" type="text" name="FunnyAnswer" value="" />
+ <input type="hidden" name="FunnyQuestionTime" value="'.$funnyQuestion['time'].'" />
+ <input type="hidden" name="FunnyQuestionHash" value="'.$funnyQuestion['hash'].'" />
+ </div>';
+ }
+ return true;
+ }
+
+ public static function checkFunnyQuestionOnEditPage($editpage, $text, $section, $error) {
+ global $wgUser;
+
+ if (!$wgUser->isLoggedIn() && !self::checkFunnyQuestion()) {
+ $error = '<div class="errorbox">'.wfMsg('wrong-answer').'</div><br clear="all" />';
+ }
+ 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;
+ }
+ }
+
+}
+
+?>
+
diff --git a/extensions/FunnyQuestion/FunnyQuestion.i18n.php b/extensions/FunnyQuestion/FunnyQuestion.i18n.php
new file mode 100644
index 00000000..a8b31e3d
--- /dev/null
+++ b/extensions/FunnyQuestion/FunnyQuestion.i18n.php
@@ -0,0 +1,26 @@
+<?php
+
+global $wgFunnyQuestions;
+
+$messages = array();
+
+$messages['en'] = array(
+ 'question-label' => 'Your answer:',
+ 'wrong-answer' => 'Sorry, your answer was wrong. Try again!'
+);
+
+$messages['de'] = array(
+ 'question-label' => 'Deine Antwort:',
+ 'wrong-answer' => 'Deine Antwort war leider falsch. Versuche es nocheinmal!'
+);
+
+foreach ($messages as $lang => $translations) {
+ if (!empty($wgFunnyQuestions[$lang])) {
+ foreach (array_keys($wgFunnyQuestions[$lang]) as $question) {
+ $messages[$lang]['question-'.sha1($question)] = $question;
+ }
+ }
+}
+
+?>
+
diff --git a/extensions/FunnyQuestion/FunnyQuestion.php b/extensions/FunnyQuestion/FunnyQuestion.php
new file mode 100644
index 00000000..5dcc367c
--- /dev/null
+++ b/extensions/FunnyQuestion/FunnyQuestion.php
@@ -0,0 +1,33 @@
+<?php
+
+$wgExtensionCredits['other'][] = array(
+ 'name' => 'FunnyQuestion',
+ 'version' => '2.0',
+ 'description' => 'Challenge-response authentication',
+ 'author' => 'Pierre Schmitz',
+ 'url' => 'https://www.archlinux.de'
+);
+
+$wgFunnyQuestions = array(
+ 'en' => array("What is the Ultimate Answer to the Ultimate Question of Life, The Universe, and Everything?" => "42"),
+ 'de' => array("Was ist die ultimaative Antwort nach dem Leben, dem Universum und dem ganzen Rest?" => "42")
+);
+$wgFunnyQuestionHash = '';
+$wgFunnyQuestionTimeout = 3600;
+$wgFunnyQuestionWait = 2;
+
+$wgAutoloadClasses['FunnyQuestion'] = dirname(__FILE__) . '/FunnyQuestion.body.php';
+$wgExtensionMessagesFiles['FunnyQuestion'] = dirname( __FILE__ ) . '/FunnyQuestion.i18n.php';
+
+if ($wgGroupPermissions['*']['edit']) {
+ $wgHooks['EditPage::showEditForm:fields'][] = 'FunnyQuestion::addFunnyQuestionToEditPage';
+ $wgHooks['EditFilter'][] = 'FunnyQuestion::checkFunnyQuestionOnEditPage';
+}
+
+if ($wgGroupPermissions['*']['createaccount'] && (empty($wgAuth) || $wgAuth->canCreateAccounts())) {
+ $wgHooks['UserCreateForm'][] = 'FunnyQuestion::addFunnyQuestionToUserCreateForm';
+ $wgHooks['AbortNewAccount'][] = 'FunnyQuestion::checkFunnyQuestionOnAbortNewAccount';
+}
+
+?>
+