-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathratethread.php
More file actions
108 lines (100 loc) · 5.29 KB
/
ratethread.php
File metadata and controls
108 lines (100 loc) · 5.29 KB
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
<?php declare(strict_types=1);
/*
* You may not change or alter any portion of this comment or credits
* of supporting developers from this source code or any supporting source code
* which is considered copyrighted (c) material of the original comment or credit authors.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
// ------------------------------------------------------------------------ //
// Author: phppp (D.J., infomax@gmail.com) //
// URL: https://xoops.org //
// Project: Article Project //
// ------------------------------------------------------------------------ //
use Xmf\IPAddress;
use Xmf\Request;
use XoopsModules\Newbb\{
Helper,
TopicHandler,
Post,
PostHandler,
RateHandler
};
/** @var Helper $helper */
/** @var TopicHandler $topicHandler */
/** @var PostHandler $postHandler */
/** @var RateHandler $rateHandler */
/** @var Post $post */
require_once __DIR__ . '/header.php';
$ratinguser = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
$anonwaitdays = 1;
$ip = IPAddress::fromRequest()->asReadable();
foreach (['topic_id', 'rate', 'forum'] as $var) {
// ${$var} = isset($_POST[$var]) ? (int)($_POST[$var]) : (isset($_GET[$var])?(int)($_GET[$var]):0);
${$var} = Request::getInt($var, Request::getInt($var, 0, 'POST'), 'GET');
}
///** @var Newbb\TopicHandler $topicHandler */
//$topicHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic');
$topicObject = $topicHandler->get($topic_id);
if (!$topicHandler->getPermission($topicObject->getVar('forum_id'), $topicObject->getVar('topic_status'), 'post')
&& !$topicHandler->getPermission($topicObject->getVar('forum_id'), $topicObject->getVar('topic_status'), 'reply')) {
// irmtfan - issue with javascript:history.go(-1)
redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _NOPERM);
}
if (empty($rate)) {
redirect_header('viewtopic.php?topic_id=' . $topic_id . '&forum=' . $forum . '', 4, _MD_NEWBB_NOVOTERATE);
}
///** @var Newbb\RateHandler $rateHandler */
//$rateHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Rate');
if (0 !== $ratinguser) {
// Check if Topic POSTER is voting (UNLESS Anonymous users allowed to post)
$crit_post = new \CriteriaCompo(new \Criteria('topic_id', $topic_id));
$crit_post->add(new \Criteria('uid', $ratinguser));
// $postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post');
if ($postHandler->getCount($crit_post)) {
redirect_header('viewtopic.php?topic_id=' . $topic_id . '&forum=' . $forum . '', 4, _MD_NEWBB_CANTVOTEOWN);
}
// Check if REG user is trying to vote twice.
$crit_rate = new \CriteriaCompo(new \Criteria('topic_id', $topic_id));
$crit_rate->add(new \Criteria('ratinguser', $ratinguser));
if ($rateHandler->getCount($crit_rate)) {
redirect_header('viewtopic.php?topic_id=' . $topic_id . '&forum=' . $forum . '', 4, _MD_NEWBB_VOTEONCE);
}
} else {
// Check if ANONYMOUS user is trying to vote more than once per day.
$crit_rate = new \CriteriaCompo(new \Criteria('topic_id', $topic_id));
$crit_rate->add(new \Criteria('ratinguser', $ratinguser));
$crit_rate->add(new \Criteria('ratinghostname', $ip));
$crit_rate->add(new \Criteria('ratingtimestamp', time() - (86400 * $anonwaitdays), '>'));
if ($rateHandler->getCount($crit_rate)) {
redirect_header('viewtopic.php?topic_id=' . $topic_id . '&forum=' . $forum . '', 4, _MD_NEWBB_VOTEONCE);
}
}
$rateObject = $rateHandler->create();
$rateObject->setVar('rating', $rate * 2);
$rateObject->setVar('topic_id', $topic_id);
$rateObject->setVar('ratinguser', $ratinguser);
$rateObject->setVar('ratinghostname', $ip);
$rateObject->setVar('ratingtimestamp', time());
$ratingid = $rateHandler->insert($rateObject);
$query = 'SELECT rating FROM ' . $GLOBALS['xoopsDB']->prefix('newbb_votedata') . ' WHERE topic_id = ' . $topic_id . ' ';
$voteresult = $GLOBALS['xoopsDB']->query($query);
if (!$GLOBALS['xoopsDB']->isResultSet($voteresult)) {
\trigger_error("Query Failed! SQL: $query- Error: " . $GLOBALS['xoopsDB']->error(), E_USER_ERROR);
}
$votesDB = $GLOBALS['xoopsDB']->getRowsNum($voteresult);
$totalrating = 0;
while ([$rating] = $GLOBALS['xoopsDB']->fetchRow($voteresult)) {
$totalrating += $rating;
}
$finalrating = $totalrating / $votesDB;
$finalrating = number_format($finalrating, 4);
//$sql = sprintf('UPDATE "%s" SET rating = "%u", votes = "%u" WHERE topic_id = "%u"', $GLOBALS['xoopsDB']->prefix('newbb_topics'), $finalrating, $votesDB, $topic_id);
$sql = sprintf('UPDATE %s SET rating = %u, votes = %u WHERE topic_id = %u', $GLOBALS['xoopsDB']->prefix('newbb_topics'), $finalrating, $votesDB, $topic_id);
$GLOBALS['xoopsDB']->queryF($sql);
$ratemessage = _MD_NEWBB_VOTEAPPRE . '<br>' . sprintf(_MD_NEWBB_THANKYOU, $GLOBALS['xoopsConfig']['sitename']);
redirect_header('viewtopic.php?topic_id=' . $topic_id . '&forum=' . $forum . '', 2, $ratemessage);
// irmtfan enhance include footer.php
require_once $GLOBALS['xoops']->path('footer.php');