Completed
Pull Request — master (#16)
by Richard
02:46
created

index.php (10 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
                XOOPS - PHP Content Management System
4
                    Copyright (c) 2000-2016 XOOPS.org
5
                       <http://xoops.org/>
6
  This program is free software; you can redistribute it and/or modify
7
  it under the terms of the GNU General Public License as published by
8
  the Free Software Foundation; either version 2 of the License, or
9
  (at your option) any later version.
10
11
  You may not change or alter any portion of this comment or credits
12
  of supporting developers from this source code or any supporting
13
  source code which is considered copyrighted (c) material of the
14
  original comment or credit authors.
15
16
  This program is distributed in the hope that it will be useful,
17
  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
  GNU General Public License for more details.
20
21
  You should have received a copy of the GNU General Public License
22
  along with this program; if not, write to the Free Software
23
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24
*/
25
/**
26
 * XOOPS Poll main index page
27
 *
28
 * @copyright::  {@link http://xoops.org XOOPS Project}
29
 * @license  ::    {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
30
 * @package  ::    xoopspoll
31
 * @since    ::      1.0
32
 *
33
 * @uses     xoops_load() method used to load classes
34
 * @uses     CriteriaCompo
35
 * @uses     Criteria
36
 * @uses     mixed $GLOBALS['xoops']::path gets XOOPS directory information
37
 * @uses     string $GLOBALS['xoops']::url gets XOOPS URL/URI information
38
 * @uses     mixed $GLOBALS['xoopsUser'] gets information about the currently logged in user
39
 * @uses     xoops_getenv() function to retrieve XOOPS environment variables
40
 * @uses     xoops_getUserTimestamp() function to convert time to user timestamp
41
 * @uses     formatTimestamp() function to convert timestamp to human readable form
42
 * @uses     xoops_getModuleHandler() to load handler for this module's class(es)
43
 * @uses     redirect_header() function used to send user to another location after completing task(s)
44
 */
45
46
include_once dirname(dirname(__DIR__)) . '/mainfile.php';
47
48
xoops_load('constants', 'xoopspoll');
49
xoops_load('renderer', 'xoopspoll');
50
xoops_load('XoopsRequest');
51
52
$myts        = MyTextSanitizer::getInstance();
53
$pollHandler = xoops_getModuleHandler('poll', 'xoopspoll');
54
$logHandler  = xoops_getModuleHandler('log', 'xoopspoll');
55
56
$pollId = XoopsRequest::getInt('poll_id', 0);
57
$url    = XoopsRequest::getString('url', '');
58
59
if (empty($pollId)) {
60
    $GLOBALS['xoopsOption']['template_main'] = 'xoopspoll_index.tpl';
61
    include $GLOBALS['xoops']->path('header.php');
62
    $GLOBALS['xoopsTpl']->assign(array(
63
                                     'lang_pollslist'      => _MD_XOOPSPOLL_POLLSLIST,
64
                                     'lang_pollquestion'   => _MD_XOOPSPOLL_POLLQUESTION,
65
                                     'lang_pollvoters'     => _MD_XOOPSPOLL_VOTERS,
66
                                     'lang_votes'          => _MD_XOOPSPOLL_VOTES,
67
                                     'lang_expiration'     => _MD_XOOPSPOLL_EXPIRATION,
68
                                     'lang_results'        => _MD_XOOPSPOLL_RESULTS,
69
                                     'lang_mustlogin'      => _MD_XOOPSPOLL_MUSTLOGIN,
70
                                     'disp_votes'          => $GLOBALS['xoopsModuleConfig']['disp_vote_nums'],
71
                                     'results_link_icon'   => $GLOBALS['xoopsModule']->getInfo('icons16') . '/open12.gif',
72
                                     'obscured_icon'       => $GLOBALS['xoops']->url('modules/xoopspoll/assets/images/icons/obscured.png'),
73
                                     'lang_obscured_alt'   => _MD_XOOPSPOLL_OBSCURED,
74
                                     'lang_obscured_title' => _MD_XOOPSPOLL_OBSCURED
75
                                 ));
76
77
    /* get polls to display on this page */
78
    $limit    = XoopsRequest::getInt('limit', XoopspollConstants::DEFAULT_POLL_PAGE_LIMIT);
79
    $start    = XoopsRequest::getInt('start', 0);
80
    $criteria = new CriteriaCompo();
81
    $criteria->add(new Criteria('start_time', time(), '<='));  // only display polls that have started
82
83
    /* check to see if forum module is installed and
84
     * exclude polls created from a forum
85
     */
86
    if ($GLOBALS['xoopsModuleConfig']['hide_forum_polls']) {
87
        /** @var XoopsModuleHandler $moduleHandler */
88
        $moduleHandler = xoops_getHandler('module');
89
        $newbbModule   = $moduleHandler->getByDirname('newbb');
90
        if ($newbbModule instanceof XoopsModule && $newbbModule->isactive()) {
0 ignored issues
show
The class XoopsModule does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
91
            /** @var NewbbTopicHandler $topicHandler */
92
            $topicHandler = xoops_getModuleHandler('topic', 'newbb');
93
            $tFields       = array('topic_id', 'poll_id');
94
            $tArray        = $topicHandler->getAll(new Criteria('topic_haspoll', 0, '>'), $tFields, false);
95
            if (!empty($tArray)) {
96
                $tcriteria = array();
97
                foreach ($tArray as $t) {
98
                    $tcriteria[] = $t['poll_id'];
99
                }
100
                if (!empty($tcriteria)) {
101
                    $tstring = '(' . implode(',', $tcriteria) . ')';
102
                    $criteria->add(new Criteria('poll_id', $tstring, 'NOT IN'));
103
                }
104
            }
105
            unset($topicHandler, $tFields, $tArray);
106
        }
107
        unset($newbbModule);
108
    }
109
    $criteria->setLimit($limit);
110
    $criteria->setStart($start);
111
    $criteria->setSort('weight ASC, end_time');  // trick criteria to allow 2 sort criteria
112
    $criteria->setOrder('DESC');
113
    $pollObjs = $pollHandler->getAll($criteria);
114
115
    foreach ($pollObjs as $pollObj) {
116
        $polls                 = array();
117
        $id                    = $pollObj->getVar('poll_id');
118
        $polls['pollId']       = $id;
119
        $polls['pollQuestion'] = $pollObj->getVar('question');
120
121
        if ($pollObj->getVar('end_time') > time()) {
122
            $polls['hasEnded'] = false;
123
            $polls['pollEnd']  = formatTimestamp($pollObj->getVar('end_time'), 'm');
124
            $uid               = (($GLOBALS['xoopsUser'] instanceof XoopsUser)
0 ignored issues
show
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
125
                                  && ($GLOBALS['xoopsUser']->getVar('uid') > 0)) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
126
            /**
127
             * {@internal DEBUG CODE
128
             * echo "<br>ID[{$id}] IP[" . xoops_getenv('REMOTE_ADDR') . "] UID[{$uid}]<br>";
129
             * $vp = (!empty($_COOKIE['voted_polls'])) ? $_COOKIE['voted_polls'] : array();
130
             * $cook = (!array_key_exists($id, $vp)) ? "NO COOKIE KEY" : "FOUND COOKIE KEY";
131
             * $cv = (!$pollObj->isAllowedToVote()) ? "Not ALLOWED" :  "ALLOWED";
132
             * $lv = ($logHandler->hasVoted($id, xoops_getenv('REMOTE_ADDR'), $uid)) ? "HAS VOTED" : "HAS NOT VOTED";
133
             * if (!$pollObj->isAllowedToVote() || ($logHandler->hasVoted($id, xoops_getenv('REMOTE_ADDR'), $uid))) {
134
             * echo "NO: {$cv} {$lv} {$cook}<br>\n";
135
             * } else {
136
             * echo "YES: {$cv} {$lv} {$cook}<br>\n";
137
             * }
138
             * } */
139
            if (!$pollObj->isAllowedToVote() || $logHandler->hasVoted($id, xoops_getenv('REMOTE_ADDR'), $uid)) {
140
                $polls['canVote'] = false;
141
            } else {
142
                $polls['canVote'] = true;
143
            }
144
        } else {
145
            /* poll has ended */
146
            $polls['hasEnded'] = true;
147
            $polls['pollEnd']  = _MD_XOOPSPOLL_EXPIRED;
148
            $polls['canVote']  = false; /* force so user can't vote */
149
        }
150
        $polls['pollVoters'] = (int)$pollObj->getVar('voters');
151
        $polls['pollVotes']  = (int)$pollObj->getVar('votes');
152
        $polls['visible']    = (true === $pollObj->isResultVisible()) ? true : false;
153
        $GLOBALS['xoopsTpl']->append('polls', $polls);
154
    }
155
    unset($pollObjs);
156
    include $GLOBALS['xoops']->path('footer.php');
157
} elseif (!empty($_POST['option_id'])) {
158
    /* user just tried to vote */
159
    //    $option_id   = XoopsRequest::getInt('option_id', 0, 'POST');
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
160
    $mail_author = false;
161
    $pollObj     = $pollHandler->get($pollId);
162 View Code Duplication
    if ($pollObj instanceof XoopspollPoll) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
        if ($pollObj->getVar('multiple')) {
164
            $optionId = XoopsRequest::getArray('option_id', array(), 'POST');
165
            $optionId = (array)$optionId; // type cast to make sure it's an array
166
            $optionId = array_map('intval', $optionId); // make sure values are integers
167
        } else {
168
            $optionId = XoopsRequest::getInt('option_id', 0, 'POST');
169
        }
170
        if (!$pollObj->hasExpired()) {
171
            $msg = _MD_XOOPSPOLL_MUSTLOGIN;
172
            //@todo:: add $url to all redirects
173
            //            $url = $GLOBALS['xoops']->buildUrl("index.php", array('poll_id' => $pollId));
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
174
            if ($pollObj->isAllowedToVote()) {
175
                $thisVoter     = (!empty($GLOBALS['xoopsUser'])
176
                                  && ($GLOBALS['xoopsUser'] instanceof XoopsUser)) ? $GLOBALS['xoopsUser']->getVar('uid') : null;
0 ignored issues
show
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
177
                $votedThisPoll = $logHandler->hasVoted($pollId, xoops_getenv('REMOTE_ADDR'), $thisVoter);
178
                if (!$votedThisPoll) {
179
                    /* user that hasn't voted before in this poll or module preferences allow it */
180
                    $voteTime = time();
181
                    if ($pollObj->vote($optionId, xoops_getenv('REMOTE_ADDR'), $voteTime)) {
182
                        if (!$pollHandler->updateCount($pollObj)) { // update the count and save in db
183
                            echo $pollObj->getHtmlErrors();
184
                            exit();
185
                        }
186
                        $msg = _MD_XOOPSPOLL_THANKSFORVOTE;
187
                    } else {
188
                        /* there was a problem registering the vote */
189
                        redirect_header($GLOBALS['xoops']->buildUrl('index.php', array('poll_id' => $pollId)), XoopspollConstants::REDIRECT_DELAY_MEDIUM, _MD_XOOPSPOLL_VOTE_ERROR);
190
                    }
191
                } else {
192
                    $msg = _MD_XOOPSPOLL_ALREADYVOTED;
193
                }
194
                /* set anon user vote (and the time they voted) */
195
                if (!$GLOBALS['xoopsUser'] instanceof XoopsUser) {
0 ignored issues
show
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
196
                    xoops_load('pollUtility', 'xoopspoll');
197
                    XoopspollPollUtility::setVoteCookie($pollId, $voteTime, 0);
0 ignored issues
show
$voteTime is of type integer, but the function expects a object<unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
198
                }
199
            } else {
200
                $msg = _MD_XOOPSPOLL_CANNOTVOTE;
201
            }
202
        } else {
203
            /* poll has expired so just show the results */
204
            $msg = _MD_XOOPSPOLL_SORRYEXPIRED;
205
        }
206
    } else {
207
        $msg = _MD_XOOPSPOLL_ERROR_INVALID_POLLID;
208
    }
209 View Code Duplication
    if ('' !== $url) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
        redirect_header($url, XoopspollConstants::REDIRECT_DELAY_MEDIUM, $msg);
211
    } else {
212
        redirect_header($GLOBALS['xoops']->buildUrl('pollresults.php', array('poll_id' => $pollId)), XoopspollConstants::REDIRECT_DELAY_MEDIUM, $msg);
213
    }
214
} else {
215
    $pollObj = $pollHandler->get($pollId);
216
    if ($pollObj->hasExpired()) {
217
        redirect_header($GLOBALS['xoops']->buildUrl('pollresults.php', array('poll_id' => $pollId)), XoopspollConstants::REDIRECT_DELAY_SHORT, _MD_XOOPSPOLL_SORRYEXPIRED);
218
    }
219
    $GLOBALS['xoopsOption']['template_main'] = 'xoopspoll_view.tpl';
220
    include $GLOBALS['xoops']->path('header.php');
221
222
    $renderer = new XoopspollRenderer($pollObj);
223
    $renderer->assignForm($GLOBALS['xoopsTpl']);
224
225
    $voteCount = $logHandler->getTotalVotesByPollId($pollId);
226
227
    $canVote    = false;
228
    $lang_multi = '';
229
    if ($pollObj->isAllowedToVote()) {
230
        $thisVoter  = (!empty($GLOBALS['xoopsUser'])
231
                       && ($GLOBALS['xoopsUser'] instanceof XoopsUser)) ? $GLOBALS['xoopsUser']->getVar('uid') : null;
0 ignored issues
show
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
232
        $canVote    = $logHandler->hasVoted($pollId, xoops_getenv('REMOTE_ADDR'), $thisVoter) ? false : true;
233
        $multiple   = $pollObj->getVar('multiple') ? true : false;
234
        $multiLimit = (int)$pollObj->getVar('multilimit');
235
        if ($multiple && ($multiLimit > 0)) {
236
            $lang_multi = sprintf(_MD_XOOPSPOLL_MULTITEXT, $multiLimit);
237
        }
238
    }
239
240
    $GLOBALS['xoopsTpl']->assign(array(
241
                                     'voteCount'    => $voteCount,
242
                                     'lang_vote'    => _MD_XOOPSPOLL_VOTE,
243
                                     'lang_results' => _MD_XOOPSPOLL_RESULTS,
244
                                     'disp_votes'   => $GLOBALS['xoopsModuleConfig']['disp_vote_nums'],
245
                                     'can_vote'     => $canVote,
246
                                     'lang_multi'   => $lang_multi
247
                                 ));
248
    include $GLOBALS['xoops']->path('footer.php');
249
}
250