Passed
Push — master ( 1a0aae...3beb71 )
by Michael
03:11
created

rate-user.php (1 issue)

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