Passed
Push — master ( 3beb71...83935f )
by Michael
16:09
created

rate-item.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
$myts          = \MyTextSanitizer::getInstance(); // MyTextSanitizer object
35
$moduleDirName = \basename(__DIR__);
36
37
if (!empty($_POST['submit'])) {
38
    //    $erh         = new ErrorHandler; //ErrorHandler object
39
    $ratinguser   = $GLOBALS['xoopsUser'] instanceof \XoopsUser ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
40
    $anonwaitdays = 1; // Make sure only 1 anonymous rating from an IP in a single day.
41
    $ip           = getenv('REMOTE_ADDR');
42
    $lid          = Request::getInt('lid', 0, 'POST');
43
    $rating       = Request::getInt('rating', 0, 'POST');
44
45
    // Check if Rating is Null
46
    if ('--' === $rating) {
0 ignored issues
show
The condition '--' === $rating is always false.
Loading history...
47
        $helper->redirect('rate-item.php?lid=' . $lid . '', 4, constant('_ADSLIGHT_NORATING'));
48
    }
49
50
    // Check if Link POSTER is voting (UNLESS Anonymous users allowed to post)
51
    if (0 !== (int)$ratinguser) {
52
        $sql    = 'SELECT submitter FROM ' . $xoopsDB->prefix('adslight_listing') . ' WHERE lid=' . $xoopsDB->escape($lid);
53
        $result = $xoopsDB->query($sql);
54
        while ([$ratinguserDB] = $xoopsDB->fetchRow($result)) {
55
            if ($ratinguserDB === $ratinguser) {
56
                $helper->redirect('viewads.php?lid=' . $lid . '', 4, constant('_ADSLIGHT_CANTVOTEOWN'));
57
            }
58
        }
59
60
        // Check if REG user is trying to vote twice.
61
        $sql    = 'SELECT ratinguser FROM ' . $xoopsDB->prefix('adslight_item_votedata') . ' WHERE lid=' . $xoopsDB->escape($lid);
62
        $result = $xoopsDB->query($sql);
63
        while ([$ratinguserDB] = $xoopsDB->fetchRow($result)) {
64
            if ($ratinguserDB === $ratinguser) {
65
                $helper->redirect('viewads.php?lid=' . $lid . '', 4, constant('_ADSLIGHT_VOTEONCE2'));
66
            }
67
        }
68
    } else {
69
        // Check if ANONYMOUS user is trying to vote more than once per day.
70
        $yesterday = time() - (86400 * $anonwaitdays);
71
        $sql       = 'SELECT count(*) FROM ' . $xoopsDB->prefix('adslight_item_votedata') . ' WHERE lid=' . $xoopsDB->escape($lid) . " AND ratinguser=0 AND ratinghostname = '{$ip}' AND date_created > {$yesterday}";
72
        $result    = $xoopsDB->query($sql);
73
        [$anonvotecount] = $xoopsDB->fetchRow($result);
74
        if ($anonvotecount > 0) {
75
            $helper->redirect('viewads.php?lid=' . $lid . '', 4, constant('_ADSLIGHT_VOTEONCE2'));
76
        }
77
    }
78
    $rating = $rating > 10 ? 10 : $rating;
79
    //All is well.  Add to Line Item Rate to DB.
80
    $newid    = $xoopsDB->genId($xoopsDB->prefix('adslight_item_votedata') . '_ratingid_seq');
81
    $datetime = time();
82
    $sql      = sprintf("INSERT INTO `%s` (ratingid, lid, ratinguser, rating, ratinghostname, date_created) VALUES (%u, %u, %u, %u, '%s', %u)", $xoopsDB->prefix('adslight_item_votedata'), $newid, $lid, $ratinguser, $rating, $ip, $datetime);
83
    // $xoopsDB->query($sql) || $eh->show('0013'); //            '0013' => 'Could not query the database.', // <br>Error: ' . $GLOBALS['xoopsDB']->error() . '',
84
    $success = $xoopsDB->query($sql);
85
    if (!$success) {
86
        /** @var \XoopsModuleHandler $moduleHandler */
87
        $moduleHandler = xoops_getHandler('module');
88
        /** @var \XoopsModule $myModule */
89
        $myModule = $moduleHandler->getByDirname('adslight');
90
        $myModule->setErrors('Could not query the database.');
91
    }
92
93
    //All is well.  Calculate Score & Add to Summary (for quick retrieval & sorting) to DB.
94
    //    updateIrating($lid);
95
    Utility::updateItemRating($lid);
96
    $ratemessage = constant('_ADSLIGHT_VOTEAPPRE') . '<br>' . sprintf(constant('_ADSLIGHT_THANKURATEITEM'), $xoopsConfig['sitename']);
97
    $helper->redirect('viewads.php?lid=' . $lid . '', 3, $ratemessage);
98
} else {
99
    $GLOBALS['xoopsOption']['template_main'] = 'adslight_rate_item.tpl';
100
    require_once XOOPS_ROOT_PATH . '/header.php';
101
    $lid    = Request::getInt('lid', 0, 'GET');
102
    $sql    = 'SELECT lid, title FROM ' . $xoopsDB->prefix('adslight_listing') . ' WHERE lid=' . $xoopsDB->escape($lid);
103
    $result = $xoopsDB->query($sql);
104
    [$lid, $title] = $xoopsDB->fetchRow($result);
105
    $GLOBALS['xoopsTpl']->assign('link', [
106
        'lid'   => $lid,
107
        'title' => htmlspecialchars($title, ENT_QUOTES | ENT_HTML5),
108
    ]);
109
    $GLOBALS['xoopsTpl']->assign('lang_voteonce', constant('_ADSLIGHT_VOTEONCE'));
110
    $GLOBALS['xoopsTpl']->assign('lang_ratingscale', constant('_ADSLIGHT_RATINGSCALE'));
111
    $GLOBALS['xoopsTpl']->assign('lang_beobjective', constant('_ADSLIGHT_BEOBJECTIVE'));
112
    $GLOBALS['xoopsTpl']->assign('lang_donotvote', constant('_ADSLIGHT_DONOTVOTE'));
113
    $GLOBALS['xoopsTpl']->assign('lang_rateit', constant('_ADSLIGHT_RATEIT'));
114
    $GLOBALS['xoopsTpl']->assign('lang_cancel', _CANCEL);
115
    $GLOBALS['xoopsTpl']->assign('mydirname', $moduleDirName);
116
    require_once XOOPS_ROOT_PATH . '/footer.php';
117
}
118