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