summaryrefslogtreecommitdiff
path: root/extensions/FunnyQuestion/FunnyQuestion.body.php
blob: 449914fdae227914fe9b718bbaab0b5e1039ce2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php


class FunnyQuestion {

	private static function normalizeAnswer($answer) {
		return preg_replace('/[^a-z0-9]/', '', strtolower($answer));
	}

	private static function getLang() {
		global $wgLang, $wgFunnyQuestions;

		return (!empty($wgFunnyQuestions[$wgLang->getCode()]) ? $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 setFunnyCookie() {
		global $wgFunnyQuestionHash, $wgFunnyQuestionRemember, $wgRequest;

		$time = time();
		$wgRequest->response()->setcookie('FunnyQuestionHash', sha1($time.wfGetIP().$wgFunnyQuestionHash), $time+$wgFunnyQuestionRemember);
		$wgRequest->response()->setcookie('FunnyQuestionTime', $time, $time+$wgFunnyQuestionRemember);
	}

	private static function hasFunnyCookie() {
		global $wgFunnyQuestionHash, $wgFunnyQuestionRemember, $wgCookiePrefix;

		return (!empty($_COOKIE[$wgCookiePrefix.'FunnyQuestionHash']) && !empty($_COOKIE[$wgCookiePrefix.'FunnyQuestionTime'])
			&& time() - $wgFunnyQuestionRemember <= $_COOKIE[$wgCookiePrefix.'FunnyQuestionTime']
			&& sha1($_COOKIE[$wgCookiePrefix.'FunnyQuestionTime']. wfGetIP().$wgFunnyQuestionHash) == $_COOKIE[$wgCookiePrefix.'FunnyQuestionHash']);
	}

	private static function checkFunnyQuestion() {
		global $wgFunnyQuestionHash, $wgFunnyQuestions, $wgFunnyQuestionTimeout, $wgFunnyQuestionWait;

		if (self::hasFunnyCookie()) {
			return true;
		}

		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)) {
					self::setFunnyCookie();
					return true;
				}
			}
		}

		return false;
	}

	public static function addFunnyQuestionToEditPage($editpage, $output) {
		global $wgUser;

		if (!$wgUser->isLoggedIn() && !self::hasFunnyCookie()) {
			$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($editor, $text, $section, &$error, $summary) {
		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) {
		if (!self::hasFunnyCookie()) {
			$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;
		}
	}

}

?>