Passed
Push — master ( 486a1b...680d04 )
by Michael
11:45 queued 18s
created

rate-user.php (1 issue)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    XOOPS Project (https://xoops.org)
17
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @author       XOOPS Development Team
19
 * @author       Pascal Le Boustouller: original author ([email protected])
20
 * @author       Luc Bizet (www.frxoops.org)
21
 * @author       jlm69 (www.jlmzone.com)
22
 * @author       mamba (www.xoops.org)
23
 */
24
25
use Xmf\Request;
26
use XoopsModules\Adslight\{
27
    Utility
28
};
29
30
/** @var Helper $helper */
31
32
require_once __DIR__ . '/header.php';
33
34
global $xoopsModule, $xoopsDB, $xoopsConfig, $xoTheme;
35
36
//require_once XOOPS_ROOT_PATH . '/class/module.errorhandler.php';
37
$moduleDirName = \basename(__DIR__);
38
$myts          = \MyTextSanitizer::getInstance(); // MyTextSanitizer object
39
40
if (!empty($_POST['submit'])) {
41
    //    $erh         = new ErrorHandler; //ErrorHandler object
42
    $ratinguser = $GLOBALS['xoopsUser'] instanceof \XoopsUser ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
43
    //Make sure only 1 anonymous from an IP in a single day.
44
    $anonwaitdays = 1;
45
    $ip           = getenv('REMOTE_ADDR');
46
    $usid         = Request::getInt('usid', 0, 'POST');
47
    $rating       = Request::getInt('rating', 0, 'POST');
48
49
    // Check if Rating is Null
50
    if ('--' === $rating) {
0 ignored issues
show
The condition '--' === $rating is always false.
Loading history...
51
        $helper->redirect('rate-user.php?usid=' . addslashes($usid) . '', 4, constant('_ADSLIGHT_NORATING'));
52
    }
53
54
    // Check if Link POSTER is voting (UNLESS Anonymous users allowed to post)
55
    if (0 !== (int)$ratinguser) {
56
        $sql    = 'SELECT submitter FROM ' . $xoopsDB->prefix('adslight_listing') . ' WHERE usid=' . $xoopsDB->escape($usid);
57
        $result = $xoopsDB->query($sql);
58
        while ([$ratinguserDB] = $xoopsDB->fetchRow($result)) {
59
            if ($ratinguserDB === $ratinguser) {
60
                $helper->redirect('members.php?usid=' . addslashes($usid) . '', 4, constant('_ADSLIGHT_CANTVOTEOWN'));
61
            }
62
        }
63
64
        // Check if REG user is trying to vote twice.
65
        $sql    = 'SELECT ratinguser FROM ' . $xoopsDB->prefix('adslight_user_votedata') . ' WHERE usid=' . $xoopsDB->escape($usid);
66
        $result = $xoopsDB->query($sql);
67
        while ([$ratinguserDB] = $xoopsDB->fetchRow($result)) {
68
            if ($ratinguserDB === $ratinguser) {
69
                $helper->redirect('members.php?usid=' . addslashes($usid) . '', 4, constant('_ADSLIGHT_VOTEONCE2'));
70
            }
71
        }
72
    } else {
73
        // Check if ANONYMOUS user is trying to vote more than once per day.
74
        $yesterday = time() - (86400 * $anonwaitdays);
75
        $sql       = 'SELECT count(*) FROM ' . $xoopsDB->prefix('adslight_user_votedata') . ' WHERE usid=' . $xoopsDB->escape($usid) . " AND ratinguser=0 AND ratinghostname = '${ip}' AND date_created > ${yesterday}";
76
        $result    = $xoopsDB->query($sql);
77
        [$anonvotecount] = $xoopsDB->fetchRow($result);
78
        if ($anonvotecount > 0) {
79
            $helper->redirect('members.php?usid=' . addslashes($usid) . '', 4, constant('_ADSLIGHT_VOTEONCE2'));
80
        }
81
    }
82
    $rating = $rating > 10 ? 10 : $rating;
83
    //All is well.  Add to Line Item Rate to DB.
84
    $newid    = $xoopsDB->genId($xoopsDB->prefix('adslight_user_votedata') . '_ratingid_seq');
85
    $datetime = time();
86
    $sql      = sprintf("INSERT INTO `%s` (ratingid, usid, ratinguser, rating, ratinghostname, date_created) VALUES (%u, %u, %u, %u, '%s', %u)", $xoopsDB->prefix('adslight_user_votedata'), $newid, $usid, $ratinguser, $rating, $ip, $datetime);
87
    // $xoopsDB->query($sql) || $eh->show('0013'); //            '0013' => 'Could not query the database.', // <br>Error: ' . $GLOBALS['xoopsDB']->error() . '',
88
    $success = $xoopsDB->query($sql);
89
    if (!$success) {
90
        /** @var \XoopsModuleHandler $moduleHandler */
91
        $moduleHandler = xoops_getHandler('module');
92
        /** @var \XoopsModule $myModule */
93
        $myModule = $moduleHandler->getByDirname('adslight');
94
        $myModule->setErrors('Could not query the database.');
95
    }
96
97
    //All is well.  Calculate Score & Add to Summary (for quick retrieval & sorting) to DB.
98
    //    updateUrating($usid);
99
    Utility::updateUserRating($usid);
100
    $ratemessage = constant('_ADSLIGHT_VOTEAPPRE') . '<br>' . sprintf(constant('_ADSLIGHT_THANKURATEUSER'), $xoopsConfig['sitename']);
101
    $helper->redirect('members.php?usid=' . addslashes($usid) . '', 3, $ratemessage);
102
} else {
103
    $GLOBALS['xoopsOption']['template_main'] = 'adslight_rate_user.tpl';
104
    require_once XOOPS_ROOT_PATH . '/header.php';
105
    $usid   = Request::getInt('usid', 0, 'GET');
106
    $sql    = 'SELECT title, usid, submitter FROM ' . $xoopsDB->prefix('adslight_listing') . ' WHERE usid=' . $xoopsDB->escape($usid);
107
    $result = $xoopsDB->query($sql);
108
    [$title, $usid, $submitter] = $xoopsDB->fetchRow($result);
109
    $GLOBALS['xoopsTpl']->assign('link', [
110
        'usid'      => $usid,
111
        'title'     => \htmlspecialchars($title, ENT_QUOTES | ENT_HTML5),
112
        'submitter' => $submitter,
113
    ]);
114
    $GLOBALS['xoopsTpl']->assign('lang_voteonce', constant('_ADSLIGHT_VOTEONCE'));
115
    $GLOBALS['xoopsTpl']->assign('lang_ratingscale', constant('_ADSLIGHT_RATINGSCALE'));
116
    $GLOBALS['xoopsTpl']->assign('lang_beobjective', constant('_ADSLIGHT_BEOBJECTIVE'));
117
    $GLOBALS['xoopsTpl']->assign('lang_donotvote', constant('_ADSLIGHT_DONOTVOTE'));
118
    $GLOBALS['xoopsTpl']->assign('lang_rateit', constant('_ADSLIGHT_RATEIT'));
119
    $GLOBALS['xoopsTpl']->assign('lang_cancel', _CANCEL);
120
    $GLOBALS['xoopsTpl']->assign('mydirname', $moduleDirName);
121
    require_once XOOPS_ROOT_PATH . '/footer.php';
122
}
123