Completed
Push — master ( 249590...ebae0d )
by Michael
05:58
created

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
 * Newbb module
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
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
 * @copyright       XOOPS Project (https://xoops.org)
13
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package         newbb
15
 * @since           4.0
16
 * @author          Taiwen Jiang <[email protected]>
17
 */
18
19
use Xmf\Request;
20
use XoopsModules\Newbb;
21
22
// rewrite by irmtfan and zyspec to accept xoopspoll 1.4 and all old xoopspoll and umfrage versions and all clones
23
24
include_once __DIR__ . '/header.php';
25
include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
26
include_once $GLOBALS['xoops']->path('class/xoopslists.php');
27
include_once $GLOBALS['xoops']->path('class/xoopsblock.php');
28
xoops_load('XoopsLocal');
29
$op      = 'add';
30
$goodOps = [
31
    'add',
32
    'save',
33
    'edit',
34
    'update',
35
    'addmore',
36
    'savemore',
37
    'delete',
38
    'delete_ok',
39
    'restart',
40
    'restart_ok',
41
    'log'
42
];
43
$op      = isset($_REQUEST['op']) ? $_REQUEST['op'] : 'add';
44
$op      = (!in_array($op, $goodOps)) ? 'add' : $op;
45
46
$poll_id  = Request::getInt('poll_id', Request::getInt('poll_id', 0, 'GET'), 'POST');
47
$topic_id = Request::getInt('topic_id', Request::getInt('topic_id', 0, 'GET'), 'POST');
48
49
// deal with permissions
50
/** @var Newbb\TopicHandler $topicHandler */
51
$topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
52
$topicObject  = $topicHandler->get($topic_id);
53
// topic exist
54
if (is_object($topicObject)) {
55
    $forum_id = $topicObject->getVar('forum_id');
56
} else {
57
    redirect_header(XOOPS_URL . '/index.php', 2, _MD_NEWBB_POLLMODULE_ERROR . ': ' . _MD_NEWBB_FORUMNOEXIST);
58
}
59
// forum access permission
60
/** @var Newbb\ForumHandler $forumHandler */
61
$forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
62
$forumObject  = $forumHandler->get($forum_id);
63
if (!$forumHandler->getPermission($forumObject)) {
64
    redirect_header(XOOPS_URL . '/index.php', 2, _MD_NEWBB_NORIGHTTOACCESS);
65
}
66
// topic view permission
67
if (!$topicHandler->getPermission($forumObject, $topicObject->getVar('topic_status'), 'view')) {
68
    redirect_header('viewforum.php?forum=' . $forum_id, 2, _MD_NEWBB_NORIGHTTOVIEW);
69
}
70
// poll module
71
$pollModuleHandler = $moduleHandler->getByDirname($GLOBALS['xoopsModuleConfig']['poll_module']);
72
if (is_object($pollModuleHandler) && $pollModuleHandler->getVar('isactive')) {
73
    // new xoopspoll module
74
    if ($pollModuleHandler->getVar('version') >= 140) {
75
        xoops_load('constants', $GLOBALS['xoopsModuleConfig']['poll_module']);
76
        xoops_load('pollUtility', $GLOBALS['xoopsModuleConfig']['poll_module']);
77
        xoops_load('request', $GLOBALS['xoopsModuleConfig']['poll_module']);
78
        xoops_loadLanguage('admin', $GLOBALS['xoopsModuleConfig']['poll_module']);
79
        /** @var \XoopspollPollHandler $xpPollHandler */
80
        $xpPollHandler = xoops_getModuleHandler('poll', $GLOBALS['xoopsModuleConfig']['poll_module']);
81
        /** @var \XoopsPoll $pollObject */
82
        $pollObject = $xpPollHandler->get($poll_id); // will create poll if poll_id = 0 exist
83
        // old xoopspoll or umfrage or any clone from them
84 View Code Duplication
    } else {
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...
85
        include $GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/include/constants.php');
86
        $classPoll  = $topicObject->loadOldPoll();
87
        $pollObject = new $classPoll($poll_id); // will create poll if poll_id = 0 exist
88
    }
89
} else {
90
    // irmtfan - issue with javascript:history.go(-1)
91
    redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_POLLMODULE_ERROR);
92
}
93
// include header
94
include $GLOBALS['xoops']->path('header.php');
95
96
// no admin user permission
97
if (is_object($GLOBALS['xoopsUser']) && !newbbIsAdmin($forumObject)) {
98
    $perm = false;
99
    if ($topicHandler->getPermission($forumObject, $topicObject->getVar('topic_status'), 'addpoll')) {
100
        if (('add' === $op || 'save' === $op || 'update' === $op) && !$topicObject->getVar('topic_haspoll')
101
            && ($GLOBALS['xoopsUser']->getVar('uid') == $topicObject->getVar('topic_poster'))) {
102
            $perm = true;
103
        } elseif (!empty($poll_id) && ($GLOBALS['xoopsUser']->getVar('uid') == $pollObject->getVar('user_id'))) {
104
            $perm = true;
105
        }
106
    }
107
    if (!$perm) {
108
        redirect_header("viewtopic.php?topic_id={$topic_id}", 2, _NOPERM);
109
    }
110
}
111
switch ($op) {
112
    case 'add':
113
        // new xoopspoll module
114
        if ($pollModuleHandler->getVar('version') >= 140) {
115
            echo '<h4>' . _MD_NEWBB_POLL_CREATNEWPOLL . "</h4>\n";
116
            $pollObject->renderForm(Request::getString('PHP_SELF', '', 'SERVER'), 'post', ['topic_id' => $topic_id]);
117
            // old xoopspoll or umfrage or any clone from them
118
        } else {
119
            $classOption  = $classPoll . 'Option';
120
            $poll_form    = new \XoopsThemeForm(_MD_NEWBB_POLL_CREATNEWPOLL, 'poll_form', 'polls.php', 'post', true);
121
            $author_label = new \XoopsFormLabel(_MD_NEWBB_POLL_AUTHOR, is_object($GLOBALS['xoopsUser']) ? ("<a href='"
122
                                                                                                          . XOOPS_URL
123
                                                                                                          . '/userinfo.php?uid='
124
                                                                                                          . $GLOBALS['xoopsUser']->getVar('uid')
125
                                                                                                          . "'>"
126
                                                                                                          . newbbGetUnameFromId($GLOBALS['xoopsUser']->getVar('uid'), $GLOBALS['xoopsModuleConfig']['show_realname'])
127
                                                                                                          . '</a>') : $GLOBALS['xoopsConfig']['anonymous']);
128
            $poll_form->addElement($author_label);
129
            $question_text = new \XoopsFormText(_MD_NEWBB_POLL_POLLQUESTION, 'question', 50, 255);
130
            $poll_form->addElement($question_text);
131
            $desc_tarea = new \XoopsFormTextarea(_MD_NEWBB_POLL_POLLDESC, 'description');
132
            $poll_form->addElement($desc_tarea);
133
            $currenttime = formatTimestamp(time(), 'Y-m-d H:i:s');
134
            $endtime     = formatTimestamp(time() + 604800, 'Y-m-d H:i:s');
135
            $expire_text = new \XoopsFormText(_MD_NEWBB_POLL_EXPIRATION . '<br><small>' . _MD_NEWBB_POLL_FORMAT . '<br>' . sprintf(_MD_NEWBB_POLL_CURRENTTIME, $currenttime) . '</small>', 'end_time', 30, 19, $endtime);
136
            $poll_form->addElement($expire_text);
137
138
            $weight_text = new \XoopsFormText(_MD_NEWBB_POLL_DISPLAYORDER, 'weight', 6, 5, 0);
139
            $poll_form->addElement($weight_text);
140
141
            $multi_yn = new \XoopsFormRadioYN(_MD_NEWBB_POLL_ALLOWMULTI, 'multiple', 0);
142
            $poll_form->addElement($multi_yn);
143
144
            $notify_yn = new \XoopsFormRadioYN(_MD_NEWBB_POLL_NOTIFY, 'notify', 1);
145
            $poll_form->addElement($notify_yn);
146
147
            $option_tray    = new \XoopsFormElementTray(_MD_NEWBB_POLL_POLLOPTIONS, '');
148
            $barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/assets/images/colorbars/'));
149
            for ($i = 0; $i < 10; ++$i) {
150
                $current_bar = ('blank.gif' !== current($barcolor_array)) ? current($barcolor_array) : next($barcolor_array);
151
                $option_text = new \XoopsFormText('', 'option_text[]', 50, 255);
152
                $option_tray->addElement($option_text);
153
                $color_select = new \XoopsFormSelect('', "option_color[{$i}]", $current_bar);
154
                $color_select->addOptionArray($barcolor_array);
155
                $color_select->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/" . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/assets/images/colorbars", "", "' . XOOPS_URL . "\")'");
156
                $color_label = new \XoopsFormLabel('', "<img src='"
157
                                                      . XOOPS_URL
158
                                                      . '/modules/'
159
                                                      . $GLOBALS['xoopsModuleConfig']['poll_module']
160
                                                      . '/assets/images/colorbars/'
161
                                                      . $current_bar
162
                                                      . "' name='option_color_image[{$i}]' id='option_color_image[{$i}]' width='30' align='bottom' height='15' alt='' /><br>");
163
                $option_tray->addElement($color_select);
164
                $option_tray->addElement($color_label);
165
                if (!next($barcolor_array)) {
166
                    reset($barcolor_array);
167
                }
168
                unset($color_select, $color_label);
169
            }
170
            $poll_form->addElement($option_tray);
171
172
            $poll_form->addElement(new \XoopsFormHidden('op', 'save'));
173
            $poll_form->addElement(new \XoopsFormHidden('topic_id', $topic_id));
174
            $poll_form->addElement(new \XoopsFormHidden('poll_id', $poll_id));
175
            $poll_form->addElement(new \XoopsFormHidden('user_id', is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0));
176
            $poll_form->addElement(new \XoopsFormButtonTray('poll_submit', _SUBMIT, 'submit'));
177
            echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . '</h4>';
178
            $poll_form->display();
179
        }
180
        break; // op: add
181
182
    case 'edit':
183
        // new xoopspoll module
184
        if ($pollModuleHandler->getVar('version') >= 140) {
185
            echo '<h4>' . _MD_NEWBB_POLL_EDITPOLL . "</h4>\n";
186
            $pollObject->renderForm(Request::getString('PHP_SELF', '', 'SERVER'), 'post', ['topic_id' => $topic_id]);
187
            // old xoopspoll or umfrage or any clone from them
188
        } else {
189
            $classOption  = $classPoll . 'Option';
190
            $poll_form    = new \XoopsThemeForm(_MD_NEWBB_POLL_EDITPOLL, 'poll_form', 'polls.php', 'post', true);
191
            $author_label = new \XoopsFormLabel(_MD_NEWBB_POLL_AUTHOR, "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $pollObject->getVar('user_id') . "'>" . newbbGetUnameFromId($pollObject->getVar('user_id'), $GLOBALS['xoopsModuleConfig']['show_realname']) . '</a>');
192
            $poll_form->addElement($author_label);
193
            $question_text = new \XoopsFormText(_MD_NEWBB_POLL_POLLQUESTION, 'question', 50, 255, $pollObject->getVar('question', 'E'));
194
            $poll_form->addElement($question_text);
195
            $desc_tarea = new \XoopsFormTextarea(_MD_NEWBB_POLL_POLLDESC, 'description', $pollObject->getVar('description', 'E'));
196
            $poll_form->addElement($desc_tarea);
197
            $date = formatTimestamp($pollObject->getVar('end_time'), 'Y-m-d H:i:s'); // important "Y-m-d H:i:s" use in jdf function
198
            if (!$pollObject->hasExpired()) {
199
                $expire_text = new \XoopsFormText(_MD_NEWBB_POLL_EXPIRATION . '<br><small>' . _MD_NEWBB_POLL_FORMAT . '<br>' . sprintf(_MD_NEWBB_POLL_CURRENTTIME, formatTimestamp(time(), 'Y-m-d H:i:s')) . '</small>', 'end_time', 20, 19, $date);
200
                $poll_form->addElement($expire_text);
201
            } else {
202
                // irmtfan full URL - add topic_id
203
                $restart_label = new \XoopsFormLabel(_MD_NEWBB_POLL_EXPIRATION,
204
                                                    sprintf(_MD_NEWBB_POLL_EXPIREDAT, $date) . "<br><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/polls.php?op=restart&amp;poll_id={$poll_id}&amp;topic_id={$topic_id}'>" . _MD_NEWBB_POLL_RESTART . '</a>');
205
                $poll_form->addElement($restart_label);
206
            }
207
            $weight_text = new \XoopsFormText(_MD_NEWBB_POLL_DISPLAYORDER, 'weight', 6, 5, $pollObject->getVar('weight'));
208
            $poll_form->addElement($weight_text);
209
            $multi_yn = new \XoopsFormRadioYN(_MD_NEWBB_POLL_ALLOWMULTI, 'multiple', $pollObject->getVar('multiple'));
210
            $poll_form->addElement($multi_yn);
211
            $options_arr  =& $classOption::getAllByPollId($poll_id);
212
            $notify_value = 1;
213
            if (0 !== $pollObject->getVar('mail_status')) {
214
                $notify_value = 0;
215
            }
216
            $notify_yn = new \XoopsFormRadioYN(_MD_NEWBB_POLL_NOTIFY, 'notify', $notify_value);
217
            $poll_form->addElement($notify_yn);
218
            $option_tray    = new \XoopsFormElementTray(_MD_NEWBB_POLL_POLLOPTIONS, '');
219
            $barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars/"));
220
            $i              = 0;
221
            foreach ($options_arr as $option) {
222
                /** @var \XoopsPoll $option */
223
                $option_tray->addElement(new \XoopsFormText('', 'option_text[]', 50, 255, $option->getVar('option_text')));
224
                $option_tray->addElement(new \XoopsFormHidden('option_id[]', $option->getVar('option_id')));
225
                $color_select = new \XoopsFormSelect('', 'option_color[{$i}]', $option->getVar('option_color'));
226
                $color_select->addOptionArray($barcolor_array);
227
                $color_select->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[" . $i . "]\", \"modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars\", \"\", \"" . XOOPS_URL . "\")'");
228
                $color_label = new \XoopsFormLabel('', "<img src='"
229
                                                      . $GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars/" . $option->getVar('option_color', 'E'))
230
                                                      . "' name='option_color_image[{$i}]' id='option_color_image[{$i}]' class='alignbottom' width='30' height='15' alt='' /><br>");
231
                $option_tray->addElement($color_select);
232
                $option_tray->addElement($color_label);
233
                unset($color_select, $color_label);
234
                ++$i;
235
            }
236
            // irmtfan full URL
237
            $more_label = new \XoopsFormLabel('', "<br><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/polls.php?op=addmore&amp;poll_id={$poll_id}&amp;topic_id={$topic_id}'>" . _MD_NEWBB_POLL_ADDMORE . '</a>');
238
            $option_tray->addElement($more_label);
239
            $poll_form->addElement($option_tray);
240
            $poll_form->addElement(new \XoopsFormHidden('op', 'update'));
241
            $poll_form->addElement(new \XoopsFormHidden('topic_id', $topic_id));
242
            $poll_form->addElement(new \XoopsFormHidden('poll_id', $poll_id));
243
            $poll_form->addElement(new \XoopsFormButtonTray('poll_submit', _SUBMIT, 'submit'));
244
245
            echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
246
            $poll_form->display();
247
        }
248
        break; // op: edit
249
250
    case 'save':
251
        // old xoopspoll or umfrage or any clone from them
252
        if ($pollModuleHandler->getVar('version') < 140) {
253
            // check security token
254 View Code Duplication
            if (!$GLOBALS['xoopsSecurity']->check()) {
255
                redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
256
            }
257
            /*
258
             * The option check should be done before submitting
259
             */
260
            $option_empty = true;
261
            if (!Request::getString('option_text', '', 'POST')) {
262
                // irmtfan - issue with javascript:history.go(-1)
263
                redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
264
            }
265
            $option_text = Request::getArray('option_text', '', 'POST');
266
            foreach ($option_text as $optxt) {
267
                if ('' !== trim($optxt)) {
268
                    $option_empty = false;
269
                    break;
270
                }
271
            }
272 View Code Duplication
            if ($option_empty) {
273
                // irmtfan - issue with javascript:history.go(-1)
274
                redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
275
            }
276
            $pollObject->setVar('question', Request::getString('question', '', 'POST'));
277
            $pollObject->setVar('description', Request::getString('description', '', 'POST'));
278
            $end_time = Request::getString('end_time', '', 'POST'); // (empty($_POST['end_time'])) ? "" : $_POST['end_time'];
279 View Code Duplication
            if ('' !== $end_time) {
280
                $timezone = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('timezone') : null;
281
                $pollObject->setVar('end_time', userTimeToServerTime(method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime($end_time) : strtotime($end_time), $timezone));
282
            } else {
283
                // if expiration date is not set, set it to 10 days from now
284
                $pollObject->setVar('end_time', time() + (86400 * 10));
285
            }
286
287
            $pollObject->setVar('display', 0);
288
            $pollObject->setVar('weight', Request::getInt('weight', 0, 'POST'));
289
            $pollObject->setVar('multiple', Request::getInt('multiple', 0, 'POST'));
290
            $pollObject->setVar('user_id', Request::getInt('user_id', 0, 'POST'));
291 View Code Duplication
            if (Request::getInt('notify', 0, 'POST') && $end_time > time()) {
292
                // if notify, set mail status to "not mailed"
293
                $pollObject->setVar('mail_status', POLL_NOTMAILED);
294
            } else {
295
                // if not notify, set mail status to already "mailed"
296
                $pollObject->setVar('mail_status', POLL_MAILED);
297
            }
298
            $new_poll_id = $pollObject->store();
299
            if (empty($new_poll_id)) {
300
                xoops_error($pollObject->getHtmlErrors);
301
                break;
302
            }
303
            $i            = 0;
304
            $option_color = Request::getArray('option_color', null, 'POST');
305
            $classOption  = $classPoll . 'Option';
306
            foreach ($option_text as $optxt) {
307
                $optxt = trim($optxt);
308
                /** @var XoopspollOption $optionObject */
309
                $optionObject = new $classOption();
310
                if ('' !== $optxt) {
311
                    $optionObject->setVar('option_text', $optxt);
312
                    $optionObject->setVar('option_color', $option_color[$i]);
313
                    $optionObject->setVar('poll_id', $new_poll_id);
314
                    $optionObject->store();
315
                }
316
                ++$i;
317
            }
318
            // clear the template cache so changes take effect immediately
319
            include_once $GLOBALS['xoops']->path('class/template.php');
320
            xoops_template_clear_module_cache($GLOBALS['xoopsModule']->getVar('mid'));
321
            xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
322
323
            // update topic to indicate it has a poll
324
            $topicObject->setVar('topic_haspoll', 1);
325
            $topicObject->setVar('poll_id', $new_poll_id);
326
            $success = $topicHandler->insert($topicObject);
327
            if (!$success) {
328
                xoops_error($topicHandler->getHtmlErrors());
329
            } else {
330
                redirect_header("viewtopic.php?topic_id={$topic_id}", 2, _MD_NEWBB_POLL_DBUPDATED);
331
            }
332
            break;// op: save
333
        }
334
    // no break
335
    case 'update':
336
        // check security token
337 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
338
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
339
        }
340
        /* make sure there's at least one option */
341
        $option_text   = Request::getString('option_text', '', 'POST');
342
        $option_string = is_array($option_text) ? implode('', $option_text) : $option_text;
343
        $option_string = trim($option_string);
344 View Code Duplication
        if ('' === $option_string) {
345
            redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
346
        }
347
348
        // new xoopspoll module
349
        if ($pollModuleHandler->getVar('version') >= 140) {
350
            /** @var \XoopspollOptionHandler $xpOptHandler */
351
            $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
352
            /** @var \XoopspollLogHandler $xpLogHandler */
353
            $xpLogHandler = xoops_getModuleHandler('log', $GLOBALS['xoopsModuleConfig']['poll_module']);
354
            //            $classRequest = ucfirst($GLOBALS['xoopsModuleConfig']["poll_module"]) . "Request";
355
            $classConstants   = ucfirst($GLOBALS['xoopsModuleConfig']['poll_module']) . 'Constants';
356
            $notify           = Request::getInt('notify', $classConstants::NOTIFICATION_ENABLED, 'POST');
357
            $currentTimestamp = time();
358
            //$xuEndTimestamp   = method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime(Request::getString('xu_end_time', null, 'POST'))
359
            //                                                             : strtotime(Request::getString('xu_end_time', null, 'POST'));
360
            $xuEndTimestamp = strtotime(Request::getString('xu_end_time', null, 'POST'));
361
            $endTimestamp   = (!Request::getString('xu_end_time', null, 'POST')) ? ($currentTimestamp + $classConstants::DEFAULT_POLL_DURATION) : userTimeToServerTime($xuEndTimestamp);
362
            //$xuStartTimestamp = method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime(Request::getString('xu_start_time', null, 'POST'))
363
            //                                                             : strtotime(Request::getString('xu_start_time', null, 'POST'));
364
            $xuStartTimestamp = strtotime(Request::getString('xu_start_time', null, 'POST'));
365
            $startTimestamp   = (!Request::getString('xu_start_time', null, 'POST')) ? ($endTimestamp - $classConstants::DEFAULT_POLL_DURATION) : userTimeToServerTime($xuStartTimestamp);
366
367
            //  don't allow changing start time if there are votes in the log
368
            if (($startTimestamp < $pollObject->getVar('start_time'))
369
                && ($xpLogHandler->getTotalVotesByPollId($poll_id) > 0)) {
370
                $startTimestamp = $pollObject->getVar('start_time'); //don't change start time
371
            }
372
373
            $poll_vars = [
374
                'user_id'     => Request::getInt('user_id', $GLOBALS['xoopsUser']->uid(), 'POST'),
375
                'question'    => Request::getString('question', null, 'POST'),
376
                'description' => Request::getText('description', null, 'POST'),
377
                'mail_status' => ($classConstants::NOTIFICATION_ENABLED == $notify) ? $classConstants::POLL_NOT_MAILED : $classConstants::POLL_MAILED,
378
                'mail_voter'  => Request::getInt('mail_voter', $classConstants::NOT_MAIL_POLL_TO_VOTER, 'POST'),
379
                'start_time'  => $startTimestamp,
380
                'end_time'    => $endTimestamp,
381
                'display'     => Request::getInt('display', $classConstants::DO_NOT_DISPLAY_POLL_IN_BLOCK, 'POST'),
382
                'visibility'  => Request::getInt('visibility', $classConstants::HIDE_NEVER, 'POST'),
383
                'weight'      => Request::getInt('weight', $classConstants::DEFAULT_WEIGHT, 'POST'),
384
                'multiple'    => Request::getInt('multiple', $classConstants::NOT_MULTIPLE_SELECT_POLL, 'POST'),
385
                'multilimit'  => Request::getInt('multilimit', $classConstants::MULTIPLE_SELECT_LIMITLESS, 'POST'),
386
                'anonymous'   => Request::getInt('anonymous', $classConstants::ANONYMOUS_VOTING_DISALLOWED, 'POST')
387
            ];
388
            $pollObject->setVars($poll_vars);
389
            $poll_id = $xpPollHandler->insert($pollObject);
390
            if (!$poll_id) {
391
                $err = $pollObject->getHtmlErrors();
392
                exit($err);
393
            }
394
395
            // now get the options
396
            $optionIdArray    = Request::getArray('option_id', [], 'POST');
397
            $optionIdArray    = array_map('intval', $optionIdArray);
398
            $optionTextArray  = Request::getArray('option_text', [], 'POST');
399
            $optionColorArray = Request::getArray('option_color', [], 'POST');
400
401
            foreach ($optionIdArray as $key => $oId) {
402
                if (!empty($oId) && ($optionObject = $xpOptHandler->get($oId))) {
403
                    // existing option object so need to update it
404
                    $optionTextArray[$key] = trim($optionTextArray[$key]);
405
                    if ('' === $optionTextArray[$key]) {
406
                        // want to delete this option
407
                        if (false !== $xpOptHandler->delete($optionObject)) {
408
                            // now remove it from the log
409
                            $xpLogHandler->deleteByOptionId($optionObject->getVar('option_id'));
410
                            //update vote count in poll
411
                            $xpPollHandler->updateCount($pollObject);
412
                        } else {
413
                            xoops_error($xpLogHandler->getHtmlErrors());
414
                            break;
415
                        }
416 View Code Duplication
                    } else {
417
                        $optionObject->setVar('option_text', $optionTextArray[$key]);
418
                        $optionObject->setVar('option_color', $optionColorArray[$key]);
419
                        $optionObject->setVar('poll_id', $poll_id);
420
                        $xpOptHandler->insert($optionObject);
421
                    }
422
                } else {
423
                    // new option object
424
                    $optionObject          = $xpOptHandler->create();
425
                    $optionTextArray[$key] = trim($optionTextArray[$key]);
426 View Code Duplication
                    if ('' !== $optionTextArray[$key]) { // ignore if text is empty
427
                        $optionObject->setVar('option_text', $optionTextArray[$key]);
428
                        $optionObject->setVar('option_color', $optionColorArray[$key]);
429
                        $optionObject->setVar('poll_id', $poll_id);
430
                        $xpOptHandler->insert($optionObject);
431
                    }
432
                    unset($optionObject);
433
                }
434
            }
435
            // old xoopspoll or umfrage or any clone from them
436
        } else {
437
            $pollObject->setVar('question', Request::getString('question', '', 'POST'));
438
            $pollObject->setVar('description', Request::getString('description', '', 'POST'));
439
440
            $end_time = Request::getString('end_time', '', 'POST');
441
            if ('' !== $end_time) {
442
                $timezone = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('timezone') : null;
443
                $pollObject->setVar('end_time', userTimeToServerTime(method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime($end_time) : strtotime($end_time), $timezone));
444
            }
445
            $pollObject->setVar('display', 0);
446
            $pollObject->setVar('weight', Request::getInt('weight', 0, 'POST'));
447
            $pollObject->setVar('multiple', Request::getInt('multiple', 0, 'POST'));
448
            $pollObject->setVar('user_id', Request::getInt('user_id', 0, 'POST'));
449 View Code Duplication
            if (Request::getInt('notify', 0, 'POST') && $end_time > time()) {
450
                // if notify, set mail status to "not mailed"
451
                $pollObject->setVar('mail_status', POLL_NOTMAILED);
452
            } else {
453
                // if not notify, set mail status to already "mailed"
454
                $pollObject->setVar('mail_status', POLL_MAILED);
455
            }
456
457
            if (!$pollObject->store()) {
458
                xoops_error($pollObject->getHtmlErrors);
459
                break;
460
            }
461
            $i            = 0;
462
            $option_id    = Request::getArray('option_id', null, 'POST');
463
            $option_color = Request::getArray('option_color', null, 'POST');
464
            $classOption  = $classPoll . 'Option';
465
            $classLog     = $classPoll . 'Log';
466
            foreach ($option_id as $opid) {
467
                $optionObject    = new $classOption($opid);
468
                $option_text[$i] = trim($option_text[$i]);
469
                if ('' !== $option_text[$i]) {
470
                    $optionObject->setVar('option_text', $option_text[$i]);
471
                    $optionObject->setVar('option_color', $option_color[$i]);
472
                    $optionObject->store();
473
                } else {
474
                    if (false !== $optionObject->delete()) {
475
                        $classLog::deleteByOptionId($option->getVar('option_id'));
476
                    }
477
                }
478
                ++$i;
479
            }
480
            $pollObject->updateCount();
481
        }
482
        // clear the template cache so changes take effect immediately
483
        include_once $GLOBALS['xoops']->path('class/template.php');
484
        xoops_template_clear_module_cache($GLOBALS['xoopsModule']->getVar('mid'));
485
        xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
486
487
        // update topic to indicate it has a poll
488
        $topicObject->setVar('topic_haspoll', 1);
489
        $topicObject->setVar('poll_id', $pollObject->getVar('poll_id'));
490
        $success = $topicHandler->insert($topicObject);
491
        if (!$success) {
492
            xoops_error($topicHandler->getHtmlErrors());
493
        } else {
494
            redirect_header("viewtopic.php?topic_id={$topic_id}", 2, _MD_NEWBB_POLL_DBUPDATED);
495
        }
496
        break;// op: save | update
497
498
    case 'addmore':
499
        $question = $pollObject->getVar('question');
500
        unset($pollObject);
501
        $poll_form = new \XoopsThemeForm(_MD_NEWBB_POLL_ADDMORE, 'poll_form', 'polls.php', 'post', true);
502
        $poll_form->addElement(new \XoopsFormLabel(_MD_NEWBB_POLL_POLLQUESTION, $question));
503
        // new xoopspoll module
504
        if ($pollModuleHandler->getVar('version') >= 140) {
505
            $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
506
            $option_tray  = $xpOptHandler->renderOptionFormTray($poll_id);
507
            // old xoopspoll or umfrage or any clone from them
508
        } else {
509
            $option_tray    = new \XoopsFormElementTray(_MD_NEWBB_POLL_POLLOPTIONS, '');
510
            $barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/assets/images/colorbars/'));
511
            for ($i = 0; $i < 10; ++$i) {
512
                $current_bar = ('blank.gif' !== current($barcolor_array)) ? current($barcolor_array) : next($barcolor_array);
513
                $option_text = new \XoopsFormText('', 'option_text[]', 50, 255);
514
                $option_tray->addElement($option_text);
515
                $color_select = new \XoopsFormSelect('', "option_color[{$i}]", $current_bar);
516
                $color_select->addOptionArray($barcolor_array);
517
                $color_select->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars\", \"\", \"" . XOOPS_URL . "\")'");
518
                $color_label = new \XoopsFormLabel('', "<img src='"
519
                                                      . $GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars/{$current_bar}")
520
                                                      . "' name='option_color_image[{$i}]' id='option_color_image[{$i}]' class='alignbottom' width='30' height='15' alt='' /><br>");
521
                $option_tray->addElement($color_select);
522
                $option_tray->addElement($color_label);
523
                unset($color_select, $color_label, $option_text);
524
                if (!next($barcolor_array)) {
525
                    reset($barcolor_array);
526
                }
527
            }
528
        }
529
        $poll_form->addElement($option_tray);
530
        $poll_form->addElement(new \XoopsFormButtonTray('poll_submit', _SUBMIT, 'submit'));
531
        $poll_form->addElement(new \XoopsFormHidden('op', 'savemore'));
532
        $poll_form->addElement(new \XoopsFormHidden('topic_id', $topic_id));
533
        $poll_form->addElement(new \XoopsFormHidden('poll_id', $poll_id));
534
535
        echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
536
        $poll_form->display();
537
        break;
538
539
    case 'savemore':
540
        // check security token
541 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
542
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
543
        }
544
545
        $option_text   = Request::getString('option_text', '', 'POST');
546
        $option_string = is_array($option_text) ? implode('', $option_text) : $option_text;
547
        $option_string = trim($option_string);
548 View Code Duplication
        if ('' === $option_string) {
549
            // irmtfan - issue with javascript:history.go(-1)
550
            redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
551
        }
552
        $i            = 0;
553
        $option_color = Request::getArray('option_color', null, 'POST');
554
        foreach ($option_text as $optxt) {
555
            $optxt = trim($optxt);
556
            if ('' !== $optxt) {
557
                // new xoopspoll module
558
                if ($pollModuleHandler->getVar('version') >= 140) {
559
                    $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
560
                    $optionObject = $xpOptHandler->create();
561
                    $optionObject->setVar('option_text', $optxt);
562
                    $optionObject->setVar('poll_id', $poll_id);
563
                    $optionObject->setVar('option_color', $option_color[$i]);
564
                    $xpOptHandler->insert($optionObject);
565
                    // old xoopspoll or umfrage or any clone from them
566
                } else {
567
                    $classOption  = $classPoll . 'Option';
568
                    $optionObject = new $classOption();
569
                    $optionObject->setVar('option_text', $optxt);
570
                    $optionObject->setVar('poll_id', $poll_id);
571
                    $optionObject->setVar('option_color', $option_color[$i]);
572
                    $optionObject->store();
573
                }
574
                unset($optionObject);
575
            }
576
            ++$i;
577
        }
578
        include_once $GLOBALS['xoops']->path('class/template.php');
579
        xoops_template_clear_module_cache($GLOBALS['xoopsModule']->getVar('mid'));
580
        xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
581
        redirect_header("polls.php?op=edit&amp;poll_id={$poll_id}&amp;topic_id={$topic_id}", 2, _MD_NEWBB_POLL_DBUPDATED);
582
        break;
583
584
    case 'delete':
585
        echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
586
        xoops_confirm(['op' => 'delete_ok', 'topic_id' => $topic_id, 'poll_id' => $poll_id], 'polls.php', sprintf(_MD_NEWBB_POLL_RUSUREDEL, $pollObject->getVar('question')));
587
        break;
588
589
    case 'delete_ok':
590
        // check security token
591 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
592
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
593
        }
594
        //try and delete the poll
595
        // new xoopspoll module
596
        if ($pollModuleHandler->getVar('version') >= 140) {
597
            $status = $xpPollHandler->delete($pollObject);
598
            if (false !== $status) {
599
                $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
600
                $xpLogHandler = xoops_getModuleHandler('log', $GLOBALS['xoopsModuleConfig']['poll_module']);
601
                $xpOptHandler->deleteByPollId($poll_id);
602
                $xpLogHandler->deleteByPollId($poll_id);
603
            } else {
604
                $msg = $xpPollHandler->getHtmlErrors();
605
            }
606
            // old xoopspoll or umfrage or any clone from them
607
        } else {
608
            $status      = $pollObject->delete();
609
            $classOption = $classPoll . 'Option';
610
            $classLog    = $classPoll . 'Log';
611
            if (false !== $status) {
612
                $classOption::deleteByPollId($poll_id);
613
                $classLog::deleteByPollId($poll_id);
614
            } else {
615
                $msg = $pollObject->getHtmlErrors();
616
            }
617
        }
618
        if (false !== $status) {
619
            include_once $GLOBALS['xoops']->path('class/template.php');
620
            xoops_template_clear_module_cache($xoopsModule->getVar('mid'));
621
            xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
622
            // delete comments for this poll
623
            xoops_comment_delete($xoopsModule->getVar('mid'), $poll_id);
624
625
            $topicObject->setVar('votes', 0); // not sure why we want to clear votes too... but I left it alone
626
            $topicObject->setVar('topic_haspoll', 0);
627
            $topicObject->setVar('poll_id', 0);
628
            $success = $topicHandler->insert($topicObject);
629
            if (!$success) {
630
                xoops_error($topicHandler->getHtmlErrors());
631
                break;
632
            }
633
        } else {
634
            xoops_error($msg);
635
            break;
636
        }
637
        redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?topic_id={$topic_id}", 1, _MD_NEWBB_POLL_DBUPDATED);
638
        break;
639
640
    case 'restart':
641
        // new xoopspoll module
642
        if ($pollModuleHandler->getVar('version') >= 140) {
643
            $classConstants        = ucfirst($GLOBALS['xoopsModuleConfig']['poll_module']) . 'Constants';
644
            $default_poll_duration = $classConstants::DEFAULT_POLL_DURATION;
645
            // old xoopspoll or umfrage or any clone from them
646
        } else {
647
            $default_poll_duration = (86400 * 10);
648
        }
649
        $poll_form   = new \XoopsThemeForm(_MD_NEWBB_POLL_RESTARTPOLL, 'poll_form', 'polls.php', 'post', true);
650
        $expire_text = new \XoopsFormText(_MD_NEWBB_POLL_EXPIRATION . '<br><small>' . _MD_NEWBB_POLL_FORMAT . '<br>' . sprintf(_MD_NEWBB_POLL_CURRENTTIME, formatTimestamp(time(), 'Y-m-d H:i:s')) . '<br>' . sprintf(_MD_NEWBB_POLL_EXPIREDAT,
651
                                                                                                                                                                                                                     formatTimestamp($pollObject->getVar('end_time'), 'Y-m-d H:i:s')) . '</small>',
652
                                         'end_time', 20, 19, formatTimestamp(time() + $default_poll_duration, 'Y-m-d H:i:s'));
653
        $poll_form->addElement($expire_text);
654
        $poll_form->addElement(new \XoopsFormRadioYN(_MD_NEWBB_POLL_NOTIFY, 'notify', 1));
655
        $poll_form->addElement(new \XoopsFormRadioYN(_MD_NEWBB_POLL_RESET, 'reset', 0));
656
        $poll_form->addElement(new \XoopsFormHidden('op', 'restart_ok'));
657
        $poll_form->addElement(new \XoopsFormHidden('topic_id', $topic_id));
658
        $poll_form->addElement(new \XoopsFormHidden('poll_id', $poll_id));
659
        $poll_form->addElement(new \XoopsFormButton('', 'poll_submit', _MD_NEWBB_POLL_RESTART, 'submit'));
660
661
        echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
662
        $poll_form->display();
663
        break;
664
665
    case 'restart_ok':
666
        // check security token
667 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
668
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
669
        }
670
671
        // new xoopspoll module
672
        if ($pollModuleHandler->getVar('version') >= 140) {
673
            $classConstants        = ucfirst($GLOBALS['xoopsModuleConfig']['poll_module']) . 'Constants';
674
            $default_poll_duration = $classConstants::DEFAULT_POLL_DURATION;
675
            $poll_not_mailed       = $classConstants::POLL_NOT_MAILED;
676
            $poll_mailed           = $classConstants::POLL_MAILED;
677
            // old xoopspoll or umfrage or any clone from them
678
        } else {
679
            $default_poll_duration = (86400 * 10);
680
            $poll_not_mailed       = POLL_NOTMAILED;
681
            $poll_mailed           = POLL_MAILED;
682
        }
683
684
        $end_time = !Request::getInt('end_time', 0, 'POST');
685 View Code Duplication
        if (0 !== $end_time) {
686
            $timezone = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('timezone') : null;
687
            $pollObject->setVar('end_time', userTimeToServerTime(method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime($end_time) : strtotime($end_time), $timezone));
688
        } else {
689
            $pollObject->setVar('end_time', time() + $default_poll_duration);
690
        }
691
692
        $isNotify = Request::getInt('notify', 0, 'POST');
693
        if (!empty($isNotify) && ($end_time > time())) {
694
            // if notify, set mail status to "not mailed"
695
            $pollObject->setVar('mail_status', $poll_not_mailed);
696
        } else {
697
            // if not notify, set mail status to already "mailed"
698
            $pollObject->setVar('mail_status', $poll_mailed);
699
        }
700
701
        // new xoopspoll module
702
        if ($pollModuleHandler->getVar('version') >= 140) {
703
            if (!$xpPollHandler->insert($pollObject)) {  // update the poll
704
                xoops_error($pollObject->getHtmlErrors());
705
                exit();
706
            }
707
            if (Request::getInt('reset', 0, 'POST')) { // reset all vote/voter counters
708
                /** @var \XoopspollOptionHandler $xpOptHandler */
709
                $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
710
                /** @var \XoopspollLogHandler $xpLogHandler */
711
                $xpLogHandler = xoops_getModuleHandler('log', $GLOBALS['xoopsModuleConfig']['poll_module']);
712
                $xpLogHandler->deleteByPollId($poll_id);
713
                $xpOptHandler->resetCountByPollId($poll_id);
714
                $xpPollHandler->updateCount($pollObject);
715
            }
716
            // old xoopspoll or umfrage or any clone from them
717
        } else {
718
            if (!$pollObject->store()) { // update the poll
719
                xoops_error($pollObject->getHtmlErrors());
720
                exit();
721
            }
722
            if (Request::getInt('reset', 0, 'POST')) { // reset all logs
723
                $classOption = $classPoll . 'Option';
724
                $classLog    = $classPoll . 'Log';
725
                $classLog::deleteByPollId($poll_id);
726
                $classOption::resetCountByPollId($poll_id);
727
                $pollObject->updateCount();
728
            }
729
        }
730
        include_once $GLOBALS['xoops']->path('class/template.php');
731
        xoops_template_clear_module_cache($xoopsModule->getVar('mid'));
732
        xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
733
        redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?topic_id={$topic_id}", 1, _MD_NEWBB_POLL_DBUPDATED);
734
        break;
735
736
    case 'log':
737
        // new xoopspoll module
738
        if ($pollModuleHandler->getVar('version') >= 140) {
739
            redirect_header($GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/admin/main.php?op=log&amp;poll_id={$poll_id}"), 2, _MD_NEWBB_POLL_VIEWLOG);
740
            // old xoopspoll or umfrage or any clone from them
741
        } else {
742
            redirect_header($GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/admin/index.php?op=log&amp;poll_id={$poll_id}"), 2, _MD_NEWBB_POLL_VIEWLOG);
743
        }
744
        break;
745
} // switch
746
747
// irmtfan move to footer.php
748
include_once __DIR__ . '/footer.php';
749
include $GLOBALS['xoops']->path('footer.php');
750