Passed
Push — master ( cdf389...4c85d7 )
by Michael
03:23
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
        if (!$xoopsDB->isResultSet($result)) {
55
            \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
56
        }
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
        if (!$xoopsDB->isResultSet($result)) {
67
            \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
68
        }
69
        while ([$ratinguserDB] = $xoopsDB->fetchRow($result)) {
70
            if ($ratinguserDB === $ratinguser) {
71
                $helper->redirect('viewads.php?lid=' . $lid, 4, constant('_ADSLIGHT_VOTEONCE2'));
72
            }
73
        }
74
    } else {
75
        // Check if ANONYMOUS user is trying to vote more than once per day.
76
        $yesterday = time() - (86400 * $anonwaitdays);
77
        $sql       = 'SELECT count(*) FROM ' . $xoopsDB->prefix('adslight_item_votedata') . ' WHERE lid=' . $xoopsDB->escape($lid) . " AND ratinguser=0 AND ratinghostname = '{$ip}' AND date_created > {$yesterday}";
78
        $result = $xoopsDB->query($sql);
79
        if (!$xoopsDB->isResultSet($result)) {
80
            \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
81
        }
82
        [$anonvotecount] = $xoopsDB->fetchRow($result);
83
        if ($anonvotecount > 0) {
84
            $helper->redirect('viewads.php?lid=' . $lid, 4, constant('_ADSLIGHT_VOTEONCE2'));
85
        }
86
    }
87
    $rating = $rating > 10 ? 10 : $rating;
88
    //All is well.  Add to Line Item Rate to DB.
89
    $newid    = $xoopsDB->genId($xoopsDB->prefix('adslight_item_votedata') . '_ratingid_seq');
90
    $datetime = time();
91
    $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);
92
    // $xoopsDB->query($sql) || $eh->show('0013'); //            '0013' => 'Could not query the database.', // <br>Error: ' . $GLOBALS['xoopsDB']->error() . '',
93
    $success = $xoopsDB->query($sql);
94
    if (!$success) {
95
        /** @var \XoopsModuleHandler $moduleHandler */
96
        $moduleHandler = xoops_getHandler('module');
97
        /** @var \XoopsModule $myModule */
98
        $myModule = $moduleHandler->getByDirname('adslight');
99
        $myModule->setErrors('Could not query the database.');
100
    }
101
102
    //All is well.  Calculate Score & Add to Summary (for quick retrieval & sorting) to DB.
103
    //    updateIrating($lid);
104
    Utility::updateItemRating($lid);
105
    $ratemessage = constant('_ADSLIGHT_VOTEAPPRE') . '<br>' . sprintf(constant('_ADSLIGHT_THANKURATEITEM'), $xoopsConfig['sitename']);
106
    $helper->redirect('viewads.php?lid=' . $lid, 3, $ratemessage);
107
} else {
108
    $GLOBALS['xoopsOption']['template_main'] = 'adslight_rate_item.tpl';
109
    require_once XOOPS_ROOT_PATH . '/header.php';
110
    $lid    = Request::getInt('lid', 0, 'GET');
111
    $sql    = 'SELECT lid, title FROM ' . $xoopsDB->prefix('adslight_listing') . ' WHERE lid=' . $xoopsDB->escape($lid);
112
    $result = $xoopsDB->query($sql);
113
    if (!$xoopsDB->isResultSet($result)) {
114
        \trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
115
    }
116
    [$lid, $title] = $xoopsDB->fetchRow($result);
117
    $GLOBALS['xoopsTpl']->assign('link', [
118
        'lid'   => $lid,
119
        'title' => htmlspecialchars($title, ENT_QUOTES | ENT_HTML5),
120
    ]);
121
    $GLOBALS['xoopsTpl']->assign('lang_voteonce', constant('_ADSLIGHT_VOTEONCE'));
122
    $GLOBALS['xoopsTpl']->assign('lang_ratingscale', constant('_ADSLIGHT_RATINGSCALE'));
123
    $GLOBALS['xoopsTpl']->assign('lang_beobjective', constant('_ADSLIGHT_BEOBJECTIVE'));
124
    $GLOBALS['xoopsTpl']->assign('lang_donotvote', constant('_ADSLIGHT_DONOTVOTE'));
125
    $GLOBALS['xoopsTpl']->assign('lang_rateit', constant('_ADSLIGHT_RATEIT'));
126
    $GLOBALS['xoopsTpl']->assign('lang_cancel', _CANCEL);
127
    $GLOBALS['xoopsTpl']->assign('mydirname', $moduleDirName);
128
    require_once XOOPS_ROOT_PATH . '/footer.php';
129
}
130