Passed
Push — master ( 6af1fb...f5fe55 )
by Michael
02:53 queued 10s
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
//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
        $result = $xoopsDB->query('SELECT submitter FROM ' . $xoopsDB->prefix('adslight_listing') . ' WHERE usid=' . $xoopsDB->escape($usid));
54
        while ([$ratinguserDB] = $xoopsDB->fetchRow($result)) {
55
            if ($ratinguserDB === $ratinguser) {
56
                $helper->redirect('members.php?usid=' . addslashes($usid) . '', 4, constant('_ADSLIGHT_CANTVOTEOWN'));
57
            }
58
        }
59
60
        // Check if REG user is trying to vote twice.
61
        $result = $xoopsDB->query('SELECT ratinguser FROM ' . $xoopsDB->prefix('adslight_user_votedata') . ' WHERE usid=' . $xoopsDB->escape($usid));
62
        while ([$ratinguserDB] = $xoopsDB->fetchRow($result)) {
63
            if ($ratinguserDB === $ratinguser) {
64
                $helper->redirect('members.php?usid=' . addslashes($usid) . '', 4, constant('_ADSLIGHT_VOTEONCE2'));
65
            }
66
        }
67
    } else {
68
        // Check if ANONYMOUS user is trying to vote more than once per day.
69
        $yesterday = time() - (86400 * $anonwaitdays);
70
        $result    = $xoopsDB->query('SELECT count(*) FROM ' . $xoopsDB->prefix('adslight_user_votedata') . ' WHERE usid=' . $xoopsDB->escape($usid) . " AND ratinguser=0 AND ratinghostname = '${ip}' AND date_created > ${yesterday}");
71
        [$anonvotecount] = $xoopsDB->fetchRow($result);
72
        if ($anonvotecount > 0) {
73
            $helper->redirect('members.php?usid=' . addslashes($usid) . '', 4, constant('_ADSLIGHT_VOTEONCE2'));
74
        }
75
    }
76
    $rating = $rating > 10 ? 10 : $rating;
77
    //All is well.  Add to Line Item Rate to DB.
78
    $newid    = $xoopsDB->genId($xoopsDB->prefix('adslight_user_votedata') . '_ratingid_seq');
79
    $datetime = time();
80
    $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);
81
    // $xoopsDB->query($sql) || $eh->show('0013'); //            '0013' => 'Could not query the database.', // <br>Error: ' . $GLOBALS['xoopsDB']->error() . '',
82
    $success = $xoopsDB->query($sql);
83
    if (!$success) {
84
        /** @var \XoopsModuleHandler $moduleHandler */
85
        $moduleHandler = xoops_getHandler('module');
86
        /** @var \XoopsModule $myModule */
87
        $myModule = $moduleHandler->getByDirname('adslight');
88
        $myModule->setErrors('Could not query the database.');
89
    }
90
91
    //All is well.  Calculate Score & Add to Summary (for quick retrieval & sorting) to DB.
92
    //    updateUrating($usid);
93
    Utility::updateUserRating($usid);
94
    $ratemessage = constant('_ADSLIGHT_VOTEAPPRE') . '<br>' . sprintf(constant('_ADSLIGHT_THANKURATEUSER'), $xoopsConfig['sitename']);
95
    $helper->redirect('members.php?usid=' . addslashes($usid) . '', 3, $ratemessage);
96
} else {
97
    $GLOBALS['xoopsOption']['template_main'] = 'adslight_rate_user.tpl';
98
    require_once XOOPS_ROOT_PATH . '/header.php';
99
    $usid   = Request::getInt('usid', 0, 'GET');
100
    $result = $xoopsDB->query('SELECT title, usid, submitter FROM ' . $xoopsDB->prefix('adslight_listing') . ' WHERE usid=' . $xoopsDB->escape($usid));
101
    [$title, $usid, $submitter] = $xoopsDB->fetchRow($result);
102
    $GLOBALS['xoopsTpl']->assign('link', [
103
        'usid'      => $usid,
104
        'title'     => \htmlspecialchars($title, ENT_QUOTES | ENT_HTML5),
105
        'submitter' => $submitter,
106
    ]);
107
    $GLOBALS['xoopsTpl']->assign('lang_voteonce', constant('_ADSLIGHT_VOTEONCE'));
108
    $GLOBALS['xoopsTpl']->assign('lang_ratingscale', constant('_ADSLIGHT_RATINGSCALE'));
109
    $GLOBALS['xoopsTpl']->assign('lang_beobjective', constant('_ADSLIGHT_BEOBJECTIVE'));
110
    $GLOBALS['xoopsTpl']->assign('lang_donotvote', constant('_ADSLIGHT_DONOTVOTE'));
111
    $GLOBALS['xoopsTpl']->assign('lang_rateit', constant('_ADSLIGHT_RATEIT'));
112
    $GLOBALS['xoopsTpl']->assign('lang_cancel', _CANCEL);
113
    $GLOBALS['xoopsTpl']->assign('mydirname', $moduleDirName);
114
    require_once XOOPS_ROOT_PATH . '/footer.php';
115
}
116