Completed
Pull Request — master (#15)
by Michael
02:51
created

index.php (3 issues)

Labels
Severity

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 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
 * @version  ::    $Id $
33
 *
34
 * @uses     xoops_load() method used to load classes
35
 * @uses     CriteriaCompo
36
 * @uses     Criteria
37
 * @uses     mixed $GLOBALS['xoops']::path gets XOOPS directory information
38
 * @uses     string $GLOBALS['xoops']::url gets XOOPS URL/URI information
39
 * @uses     mixed $GLOBALS['xoopsUser'] gets information about the currently logged in user
40
 * @uses     xoops_getenv() function to retrieve XOOPS environment variables
41
 * @uses     xoops_getUserTimestamp() function to convert time to user timestamp
42
 * @uses     formatTimestamp() function to convert timestamp to human readable form
43
 * @uses     xoops_getmodulehandler() to load handler for this module's class(es)
44
 * @uses     redirect_header() function used to send user to another location after completing task(s)
45
 */
46
47
include_once dirname(dirname(__DIR__)) . '/mainfile.php';
48
49
xoops_load('constants', 'xoopspoll');
50
xoops_load('renderer', 'xoopspoll');
51
xoops_load('XoopsRequest');
52
53
$myts        =& MyTextSanitizer::getInstance();
54
$pollHandler =& xoops_getmodulehandler('poll', 'xoopspoll');
55
$logHandler  =& xoops_getmodulehandler('log', 'xoopspoll');
56
57
$pollId = XoopsRequest::getInt('poll_id', 0);
58
$url    = XoopsRequest::getString('url', '');
59
60
if (empty($pollId)) {
61
    $GLOBALS['xoopsOption']['template_main'] = 'xoopspoll_index.tpl';
62
    include $GLOBALS['xoops']->path('header.php');
63
    $GLOBALS['xoopsTpl']->assign(array(
64
                                     'lang_pollslist'      => _MD_XOOPSPOLL_POLLSLIST,
65
                                     'lang_pollquestion'   => _MD_XOOPSPOLL_POLLQUESTION,
66
                                     'lang_pollvoters'     => _MD_XOOPSPOLL_VOTERS,
67
                                     'lang_votes'          => _MD_XOOPSPOLL_VOTES,
68
                                     'lang_expiration'     => _MD_XOOPSPOLL_EXPIRATION,
69
                                     'lang_results'        => _MD_XOOPSPOLL_RESULTS,
70
                                     'lang_mustlogin'      => _MD_XOOPSPOLL_MUSTLOGIN,
71
                                     'disp_votes'          => $GLOBALS['xoopsModuleConfig']['disp_vote_nums'],
72
                                     'results_link_icon'   => $GLOBALS['xoopsModule']->getInfo('icons16') . '/open12.gif',
73
                                     'obscured_icon'       => $GLOBALS['xoops']->url('modules/xoopspoll/assets/images/icons/obscured.png'),
74
                                     'lang_obscured_alt'   => _MD_XOOPSPOLL_OBSCURED,
75
                                     'lang_obscured_title' => _MD_XOOPSPOLL_OBSCURED));
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
        $module_handler =& xoops_gethandler('module');
88
        $newbbModule    =& $module_handler->getByDirname('newbb');
89
        if ($newbbModule instanceof XoopsModule && $newbbModule->isactive()) {
90
            $topic_handler = & xoops_getmodulehandler('topic', 'newbb');
91
            $tFields       = array('topic_id', 'poll_id');
92
            $tArray        = $topic_handler->getAll(new Criteria('topic_haspoll', 0, '>'), $tFields, false);
93
            if (!empty($tArray)) {
94
                $tcriteria = array();
95
                foreach ($tArray as $t) {
96
                    $tcriteria[] = $t['poll_id'];
97
                }
98
                if (!empty($tcriteria)) {
99
                    $tstring = '(' . implode(',', $tcriteria) . ')';
100
                    $criteria->add(new Criteria('poll_id', $tstring, 'NOT IN'));
101
                }
102
            }
103
            unset($topic_handler, $tFields, $tArray);
104
        }
105
        unset($newbbModule);
106
    }
107
    $criteria->setLimit($limit);
108
    $criteria->setStart($start);
109
    $criteria->setSort('weight ASC, end_time');  // trick criteria to allow 2 sort criteria
110
    $criteria->setOrder('DESC');
111
    $pollObjs = $pollHandler->getAll($criteria);
112
113
    foreach ($pollObjs as $pollObj) {
114
        $polls                 = array();
115
        $id                    = $pollObj->getVar('poll_id');
116
        $polls['pollId']       = $id;
117
        $polls['pollQuestion'] = $pollObj->getVar('question');
118
119
        if ($pollObj->getVar('end_time') > time()) {
120
            $polls['hasEnded'] = false;
121
            $polls['pollEnd']  = formatTimestamp($pollObj->getVar('end_time'), 'm');
122
            $uid               = (($GLOBALS['xoopsUser'] instanceof XoopsUser) && ($GLOBALS['xoopsUser']->getVar('uid') > 0)) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
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...
123
            /**
124
             * {@internal DEBUG CODE
125
             * echo "<br />ID[{$id}] IP[" . xoops_getenv('REMOTE_ADDR') . "] UID[{$uid}]<br />";
126
             * $vp = (!empty($_COOKIE['voted_polls'])) ? $_COOKIE['voted_polls'] : array();
127
             * $cook = (!array_key_exists($id, $vp)) ? "NO COOKIE KEY" : "FOUND COOKIE KEY";
128
             * $cv = (!$pollObj->isAllowedToVote()) ? "Not ALLOWED" :  "ALLOWED";
129
             * $lv = ($logHandler->hasVoted($id, xoops_getenv('REMOTE_ADDR'), $uid)) ? "HAS VOTED" : "HAS NOT VOTED";
130
             * if (!$pollObj->isAllowedToVote() || ($logHandler->hasVoted($id, xoops_getenv('REMOTE_ADDR'), $uid))) {
131
             * echo "NO: {$cv} {$lv} {$cook}<br />\n";
132
             * } else {
133
             * echo "YES: {$cv} {$lv} {$cook}<br />\n";
134
             * }
135
             * } */
136
            if (!$pollObj->isAllowedToVote() || ($logHandler->hasVoted($id, xoops_getenv('REMOTE_ADDR'), $uid))) {
137
                $polls['canVote'] = false;
138
            } else {
139
                $polls['canVote'] = true;
140
            }
141
        } else {
142
            /* poll has ended */
143
            $polls['hasEnded'] = true;
144
            $polls['pollEnd']  = _MD_XOOPSPOLL_EXPIRED;
145
            $polls['canVote']  = false; /* force so user can't vote */
146
        }
147
        $polls['pollVoters'] = (int)($pollObj->getVar('voters'));
148
        $polls['pollVotes']  = (int)($pollObj->getVar('votes'));
149
        $polls['visible']    = (true === $pollObj->isResultVisible()) ? true : false;
150
        $GLOBALS['xoopsTpl']->append('polls', $polls);
151
    }
152
    unset($pollObjs);
153
    include $GLOBALS['xoops']->path('footer.php');
154
} elseif (!empty($_POST['option_id'])) {
155
    /* user just tried to vote */
156
    //    $option_id   = XoopsRequest::getInt('option_id', 0, 'POST');
157
    $mail_author = false;
158
    $pollObj     = $pollHandler->get($pollId);
159 View Code Duplication
    if ($pollObj instanceof XoopspollPoll) {
160
        if ($pollObj->getVar('multiple')) {
161
            $optionId = XoopsRequest::getArray('option_id', array(), 'POST');
162
            $optionId = (array)$optionId; // type cast to make sure it's an array
163
            $optionId = array_map('intval', $optionId); // make sure values are integers
164
        } else {
165
            $optionId = XoopsRequest::getInt('option_id', 0, 'POST');
166
        }
167
        if (!$pollObj->hasExpired()) {
168
            $msg = _MD_XOOPSPOLL_MUSTLOGIN;
169
            //@todo:: add $url to all redirects
170
            //            $url = $GLOBALS['xoops']->buildUrl("index.php", array('poll_id' => $pollId));
171
            if ($pollObj->isAllowedToVote()) {
172
                $thisVoter     = (!empty($GLOBALS['xoopsUser']) && ($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...
173
                $votedThisPoll = $logHandler->hasVoted($pollId, xoops_getenv('REMOTE_ADDR'), $thisVoter);
174
                if (!$votedThisPoll) {
175
                    /* user that hasn't voted before in this poll or module preferences allow it */
176
                    $voteTime = time();
177
                    if ($pollObj->vote($optionId, xoops_getenv('REMOTE_ADDR'), $voteTime)) {
178
                        if (!$pollHandler->updateCount($pollObj)) { // update the count and save in db
179
                            echo $pollObj->getHtmlErrors();
180
                            exit();
181
                        }
182
                        $msg = _MD_XOOPSPOLL_THANKSFORVOTE;
183
                    } else {
184
                        /* there was a problem registering the vote */
185
                        redirect_header($GLOBALS['xoops']->buildUrl('index.php', array('poll_id' => $pollId)), XoopspollConstants::REDIRECT_DELAY_MEDIUM, _MD_XOOPSPOLL_VOTE_ERROR);
186
                    }
187
                } else {
188
                    $msg = _MD_XOOPSPOLL_ALREADYVOTED;
189
                }
190
                /* set anon user vote (and the time they voted) */
191
                if (!$GLOBALS['xoopsUser'] instanceof XoopsUser) {
192
                    xoops_load('pollUtility', 'xoopspoll');
193
                    XoopspollPollUtility::setVoteCookie($pollId, $voteTime, 0);
194
                }
195
            } else {
196
                $msg = _MD_XOOPSPOLL_CANNOTVOTE;
197
            }
198
        } else {
199
            /* poll has expired so just show the results */
200
            $msg = _MD_XOOPSPOLL_SORRYEXPIRED;
201
        }
202
    } else {
203
        $msg = _MD_XOOPSPOLL_ERROR_INVALID_POLLID;
204
    }
205 View Code Duplication
    if ('' !== $url) {
206
        redirect_header($url, XoopspollConstants::REDIRECT_DELAY_MEDIUM, $msg);
207
    } else {
208
        redirect_header($GLOBALS['xoops']->buildUrl('pollresults.php', array('poll_id' => $pollId)), XoopspollConstants::REDIRECT_DELAY_MEDIUM, $msg);
209
    }
210
} else {
211
    $pollObj = $pollHandler->get($pollId);
212
    if ($pollObj->hasExpired()) {
213
        redirect_header($GLOBALS['xoops']->buildUrl('pollresults.php', array('poll_id' => $pollId)), XoopspollConstants::REDIRECT_DELAY_SHORT, _MD_XOOPSPOLL_SORRYEXPIRED);
214
    }
215
    $GLOBALS['xoopsOption']['template_main'] = 'xoopspoll_view.tpl';
216
    include $GLOBALS['xoops']->path('header.php');
217
218
    $renderer = new XoopspollRenderer($pollObj);
219
    $renderer->assignForm($GLOBALS['xoopsTpl']);
220
221
    $voteCount = $logHandler->getTotalVotesByPollId($pollId);
222
223
    $canVote    = false;
224
    $lang_multi = '';
225
    if ($pollObj->isAllowedToVote()) {
226
        $thisVoter  = (!empty($GLOBALS['xoopsUser']) && ($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...
227
        $canVote    = ($logHandler->hasVoted($pollId, xoops_getenv('REMOTE_ADDR'), $thisVoter)) ? false : true;
228
        $multiple   = ($pollObj->getVar('multiple')) ? true : false;
229
        $multiLimit = (int)($pollObj->getVar('multilimit'));
230
        if ($multiple && ($multiLimit > 0)) {
231
            $lang_multi = sprintf(_MD_XOOPSPOLL_MULTITEXT, $multiLimit);
232
        }
233
    }
234
235
    $GLOBALS['xoopsTpl']->assign(array(
236
                                     'voteCount'    => $voteCount,
237
                                     'lang_vote'    => _MD_XOOPSPOLL_VOTE,
238
                                     'lang_results' => _MD_XOOPSPOLL_RESULTS,
239
                                     'disp_votes'   => $GLOBALS['xoopsModuleConfig']['disp_vote_nums'],
240
                                     'can_vote'     => $canVote,
241
                                     'lang_multi'   => $lang_multi));
242
    include $GLOBALS['xoops']->path('footer.php');
243
}
244