Passed
Push — master ( 3ff696...926303 )
by Michael
04:01 queued 51s
created

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