diff options
Diffstat (limited to 'extensions/FunnyQuestion/FunnyQuestion.body.php')
-rw-r--r-- | extensions/FunnyQuestion/FunnyQuestion.body.php | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/extensions/FunnyQuestion/FunnyQuestion.body.php b/extensions/FunnyQuestion/FunnyQuestion.body.php index 04d25f8d..2bfb9343 100644 --- a/extensions/FunnyQuestion/FunnyQuestion.body.php +++ b/extensions/FunnyQuestion/FunnyQuestion.body.php @@ -24,9 +24,29 @@ class FunnyQuestion { 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'])) { @@ -51,6 +71,7 @@ class FunnyQuestion { foreach ($answers as $answer) { if (self::normalizeAnswer($answer) == $userAnswer && $hash == sha1($time.$question.$wgFunnyQuestionHash)) { + self::setFunnyCookie(); return true; } } @@ -62,7 +83,7 @@ class FunnyQuestion { public static function addFunnyQuestionToEditPage($editpage, $output) { global $wgUser; - if (!$wgUser->isLoggedIn()) { + if (!$wgUser->isLoggedIn() && !self::hasFunnyCookie()) { $funnyQuestion = self::getFunnyQuestion(); $editpage->editFormTextAfterWarn .= '<div class="editOptions"> @@ -86,10 +107,12 @@ class FunnyQuestion { } 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', ''); + 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; } |