Passed
Push — master ( d79fd5...f185ae )
by Michael
57s queued 15s
created

ratenews.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @author         XOOPS Development Team
16
 */
17
18
/*
19
 * Enable users to note a news
20
 *
21
 * This page is called from the page "article.php" and "index.php", it
22
 * enables users to vote for a news, according to the module's option named
23
 * "ratenews". This code is *heavily* based on the file "ratefile.php" from
24
 * the mydownloads module.
25
 * Possible hack : Enable only registred users to vote
26
 * Notes :
27
 *          Anonymous users can only vote 1 time per day (except if their IP change)
28
 *          Author's can't vote for their own news
29
 *          Registred users can only vote one time
30
 *
31
 * @package News
32
 * @author Xoops Modules Dev Team
33
 * @copyright   (c) XOOPS Project (https://xoops.org)
34
 *
35
 * Parameters received by this page :
36
 * @page_param  int     storyid Id of the story we are going to vote for
37
 * @page_param  string  submit  The submit button of the rating form
38
 * @page_param  int     rating  User's rating
39
 *
40
 * @page_title          Story's title - "Rate this news" - Module's name
41
 *
42
 * @template_name       news_ratenews.html
43
 *
44
 * Template's variables :
45
 * @template_var    string  lang_voteonce   Fixed text "Please do not vote for the same resource more than once."
46
 * @template_var    string  lang_ratingscale    Fixed text "The scale is 1 - 10, with 1 being poor and 10 being excellent."
47
 * @template_var    string  lang_beobjective    Fixed text "Please be objective, if everyone receives a 1 or a 10, the ratings aren't very useful."
48
 * @template_var    string  lang_donotvote      Fixed text "Do not vote for your own resource."
49
 * @template_var    string  lang_rateit         Fixed text "Rate It!"
50
 * @template_var    string  lang_cancel         Fixed text "Cancel"
51
 * @template_var    array   news                Contains some information about the story
52
 *                                  Structure :
53
 * @template_var                    int     storyid     Story's ID
54
 * @template_var                    string  title       story's title
55
 */
56
57
use Xmf\Request;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
58
use XoopsModules\News;
59
use XoopsModules\News\NewsStory;
60
61
require_once __DIR__ . '/header.php';
62
require_once XOOPS_ROOT_PATH . '/class/module.errorhandler.php';
63
// require_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
64
require_once XOOPS_ROOT_PATH . '/modules/news/config.php';
65
$myts = \MyTextSanitizer::getInstance();
66
67
// Verify the perms
68
// 1) Is the vote activated in the module ?
69
$ratenews = News\Utility::getModuleOption('ratenews');
70
if (!$ratenews) {
71
    redirect_header(XOOPS_URL . '/modules/news/index.php', 3, _NOPERM);
72
}
73
74
// Limit rating by registred users
75
if ($cfg['config_rating_registred_only']) {
76
    if (!isset($xoopsUser) || !is_object($xoopsUser)) {
77
        redirect_header(XOOPS_URL . '/modules/news/index.php', 3, _NOPERM);
78
    }
79
}
80
81
// 2) Is the story published ?
82
$storyid = 0;
83
if (Request::hasVar('storyid', 'GET')) {
84
    $storyid = Request::getInt('storyid', 0, 'GET');
85
} elseif (Request::hasVar('storyid', 'POST')) {
86
    $storyid = Request::getInt('storyid', 0, 'POST');
87
}
88
89
if (!empty($storyid)) {
90
    $article = new NewsStory($storyid);
91
    if (0 == $article->published() || $article->published() > time()) {
92
        redirect_header(XOOPS_URL . '/modules/news/index.php', 2, _NW_NOSTORY);
93
    }
94
95
    // Expired
96
    if (0 != $article->expired() && $article->expired() < time()) {
97
        redirect_header(XOOPS_URL . '/modules/news/index.php', 2, _NW_NOSTORY);
98
    }
99
} else {
100
    redirect_header(XOOPS_URL . '/modules/news/index.php', 2, _NW_NOSTORY);
101
}
102
103
// 3) Can the user see this news? If they can't see it, they can't vote for it
104
/** @var \XoopsGroupPermHandler $grouppermHandler */
105
$grouppermHandler = xoops_getHandler('groupperm');
106
if (is_object($xoopsUser)) {
107
    $groups = $xoopsUser->getGroups();
108
} else {
109
    $groups = XOOPS_GROUP_ANONYMOUS;
110
}
111
if (!$grouppermHandler->checkRight('news_view', $article->topicid(), $groups, $xoopsModule->getVar('mid'))) {
112
    redirect_header(XOOPS_URL . '/modules/news/index.php', 3, _NOPERM);
113
}
114
115
if (!empty($_POST['submit'])) { // The form was submited
116
    $eh = new ErrorHandler(); //ErrorHandler object
117
    if (!is_object($xoopsUser)) {
118
        $ratinguser = 0;
119
    } else {
120
        $ratinguser = $xoopsUser->getVar('uid');
121
    }
122
123
    //Make sure only 1 anonymous from an IP in a single day.
124
    $anonwaitdays = 1;
125
    $ip           = getenv('REMOTE_ADDR');
126
    $storyid      = Request::getInt('storyid', 0, 'POST');
127
    $rating       = Request::getInt('rating', 0, 'POST');
128
129
    // Check if Rating is Null
130
    if ('--' == $rating) {
131
        redirect_header(XOOPS_URL . '/modules/news/ratenews.php?storyid=' . $storyid, 4, _NW_NORATING);
132
    }
133
134
    if ($rating < 1 || $rating > 10) {
135
        exit(_ERRORS);
136
    }
137
138
    // Check if News POSTER is voting (UNLESS Anonymous users allowed to post)
139
    if (0 != $ratinguser) {
140
        $result = $xoopsDB->query('SELECT uid FROM ' . $xoopsDB->prefix('news_stories') . " WHERE storyid=$storyid");
141
        while ([$ratinguserDB] = $xoopsDB->fetchRow($result)) {
142
            if ($ratinguserDB == $ratinguser) {
143
                redirect_header(XOOPS_URL . '/modules/news/article.php?storyid=' . $storyid, 4, _NW_CANTVOTEOWN);
144
            }
145
        }
146
147
        // Check if REG user is trying to vote twice.
148
        $result = $xoopsDB->query('SELECT ratinguser FROM ' . $xoopsDB->prefix('news_stories_votedata') . " WHERE storyid=$storyid");
149
        while ([$ratinguserDB] = $xoopsDB->fetchRow($result)) {
150
            if ($ratinguserDB == $ratinguser) {
151
                redirect_header(XOOPS_URL . '/modules/news/article.php?storyid=' . $storyid, 4, _NW_VOTEONCE);
152
            }
153
        }
154
    } else {
155
        // Check if ANONYMOUS user is trying to vote more than once per day.
156
        $yesterday = (time() - (86400 * $anonwaitdays));
157
        $result    = $xoopsDB->query('SELECT COUNT(*) FROM ' . $xoopsDB->prefix('news_stories_votedata') . " WHERE storyid=$storyid AND ratinguser=0 AND ratinghostname = '$ip'  AND ratingtimestamp > $yesterday");
158
        [$anonvotecount] = $xoopsDB->fetchRow($result);
159
        if ($anonvotecount >= 1) {
160
            redirect_header(XOOPS_URL . '/modules/news/article.php?storyid=' . $storyid, 4, _NW_VOTEONCE);
161
        }
162
    }
163
164
    //All is well.  Add to Line Item Rate to DB.
165
    $newid    = $xoopsDB->genId($xoopsDB->prefix('news_stories_votedata') . '_ratingid_seq');
166
    $datetime = time();
167
    $sql      = sprintf("INSERT INTO `%s` (ratingid, storyid, ratinguser, rating, ratinghostname, ratingtimestamp) VALUES (%u, %u, %u, %u, '%s', %u)", $xoopsDB->prefix('news_stories_votedata'), $newid, $storyid, $ratinguser, $rating, $ip, $datetime);
168
    // $xoopsDB->query($sql) || ErrorHandler::show('0013');
169
    $xoopsDB->query($sql) or trigger_error("Query Failed! SQL: $sql- Error: " . $xoopsDB->error(), E_USER_ERROR);
170
171
    //All is well.  Calculate Score & Add to Summary (for quick retrieval & sorting) to DB.
172
    News\Utility::updateRating($storyid);
173
    $ratemessage = _NW_VOTEAPPRE . '<br>' . sprintf(_NW_THANKYOU, $xoopsConfig['sitename']);
174
    redirect_header(XOOPS_URL . '/modules/news/article.php?storyid=' . $storyid, 4, $ratemessage);
175
} else { // Display the form to vote
176
    $GLOBALS['xoopsOption']['template_main'] = 'news_ratenews.tpl';
177
    require_once XOOPS_ROOT_PATH . '/header.php';
178
    $news = null;
179
    $news = new NewsStory($storyid);
180
    if (is_object($news)) {
181
        $title = $news->title('Show');
182
    } else {
183
        redirect_header(XOOPS_URL . '/modules/news/index.php', 3, _ERRORS);
184
    }
185
    $xoopsTpl->assign('advertisement', News\Utility::getModuleOption('advertisement'));
186
    $xoopsTpl->assign('news', ['storyid' => $storyid, 'title' => $title]);
187
    $xoopsTpl->assign('lang_voteonce', _NW_VOTEONCE);
188
    $xoopsTpl->assign('lang_ratingscale', _NW_RATINGSCALE);
189
    $xoopsTpl->assign('lang_beobjective', _NW_BEOBJECTIVE);
190
    $xoopsTpl->assign('lang_donotvote', _NW_DONOTVOTE);
191
    $xoopsTpl->assign('lang_rateit', _NW_RATEIT);
192
    $xoopsTpl->assign('lang_cancel', _CANCEL);
193
    $xoopsTpl->assign('xoops_pagetitle', $title . ' - ' . _NW_RATETHISNEWS . ' - ' . $xoopsModule->name('s'));
194
    News\Utility::createMetaDatas();
195
    require_once XOOPS_ROOT_PATH . '/footer.php';
196
}
197
require_once XOOPS_ROOT_PATH . '/footer.php';
198