Skip to content

Commit b2ef088

Browse files
Fixes Polls
1 parent bf44467 commit b2ef088

9 files changed

Lines changed: 107 additions & 53 deletions

File tree

www/actions/poll_state.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
require_once __DIR__.'/../includes/poll.inc.php';
77

8+
global $polls;
9+
810
/** Input validation and sanitization */
911
$pollId = filter_input(INPUT_GET, 'poll', FILTER_VALIDATE_INT) ?? null; // $_GET['poll']
1012
$pollState = filter_input(INPUT_GET, 'state', FILTER_DEFAULT, FILTER_REQUIRE_SCALAR) ?? null; // $_GET['state']
@@ -22,7 +24,7 @@
2224
user_error('Invalid poll-id: '.$pollId, E_USER_ERROR);
2325
}
2426

25-
$polls = new Polls();
27+
//$polls = new Polls(); --> Instantiated in poll.inc.php
2628

2729
$e = $db->query('SELECT * FROM polls WHERE user=? AND id=?', __FILE__, __LINE__, 'SELECT', [$user->id, $pollId]);
2830
$d = $db->fetch($e);

www/actions/poll_unvote.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
* Poll Unvote.
44
* @packages zorg\Polls
55
*/
6-
require_once dirname(__FILE__).'/../includes/poll.inc.php';
6+
require_once __DIR__.'/../includes/poll.inc.php';
77

8-
/** Input validation and sanitization */
9-
$pollId = filter_input(INPUT_GET, 'poll', FILTER_VALIDATE_INT) ?? null; // $_GET['poll']
8+
global $polls;
109

1110
if(!$user->is_loggedin()) {
1211
http_response_code(403); // Set response code 403 (Access denied)
1312
user_error('Du bist nicht eingeloggt', E_USER_ERROR);
1413
}
14+
$pollId = filter_input(INPUT_GET, 'poll', FILTER_VALIDATE_INT) ?? null; // $_GET['poll']
1515
if(empty($pollId) || $pollId <= 0) {
1616
http_response_code(404); // Set response code 404 (Not found)
1717
user_error('Invalid poll-id: '.$pollId, E_USER_ERROR);
1818
}
1919

20-
$polls = new Polls();
20+
//$polls = new Polls(); --> Instantiated in poll.inc.php
2121

2222
$e = $db->query('SELECT * FROM polls WHERE id=?', __FILE__, __LINE__, 'SELECT', [$pollId]);
2323
$d = $db->fetch($e);

www/actions/poll_vote.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,33 @@
55
*/
66
require_once __DIR__.'/../includes/poll.inc.php';
77

8-
/** Input validation and sanitization */
9-
$poll = (filter_input(INPUT_POST, 'poll', FILTER_VALIDATE_INT) ?? (filter_input(INPUT_GET, 'poll', FILTER_VALIDATE_INT) ?? null)); // $_POST['poll'] / $_GET['poll']
10-
$vote = (filter_input(INPUT_POST, 'vote', FILTER_VALIDATE_INT) ?? (filter_input(INPUT_GET, 'vote', FILTER_VALIDATE_INT) ?? null)); // $_POST['vote'] / $_GET['vote']
11-
$redirect = base64url_decode(filter_input(INPUT_GET, 'redirect', FILTER_DEFAULT, FILTER_REQUIRE_SCALAR)) ?? null; // $_GET['redirect']
8+
global $polls;
129

1310
if (!$user->is_loggedin()) {
1411
http_response_code(403); // Set response code 403 (Access denied)
1512
user_error('Access denied', E_USER_ERROR);
1613
}
14+
$poll = (filter_input(INPUT_POST, 'poll', FILTER_VALIDATE_INT) ?? (filter_input(INPUT_GET, 'poll', FILTER_VALIDATE_INT) ?? null)); // $_POST['poll'] / $_GET['poll']
15+
$vote = (filter_input(INPUT_POST, 'vote', FILTER_VALIDATE_INT) ?? (filter_input(INPUT_GET, 'vote', FILTER_VALIDATE_INT) ?? null)); // $_POST['vote'] / $_GET['vote']
1716
if (empty($poll) || $poll <= 0 || empty($vote) || $vote <= 0) {
1817
http_response_code(403); // Set response code 403 (Access denied)
1918
user_error('Nice try', E_USER_ERROR);
2019
}
2120

22-
if ($poll !== null && $vote !== null)
21+
if ($poll>0 && $vote>0)
2322
{
24-
$polls = new Polls();
23+
//$polls = new Polls(); --> Instantiated in poll.inc.php
2524

2625
$e = $db->query('SELECT p.* FROM polls p, poll_answers a WHERE a.poll=p.id AND p.id=? AND a.id=?', __FILE__, __LINE__, __FILE__, [$poll, $vote]);
2726
$d = $db->fetch($e);
2827

2928
if ($d && $d['state']=='open' && $polls->user_has_vote_permission($d['type'])) {
30-
$db->query('REPLACE INTO poll_votes (poll, user, answer) VALUES (?, ?, ?)',
31-
__FILE__, __LINE__, 'REPLACE INTO poll_votes', [$poll, $user->id, $vote]);
29+
$db->query('REPLACE INTO poll_votes (poll, user, answer) VALUES (?, ?, ?)', __FILE__, __LINE__, 'REPLACE INTO poll_votes', [$poll, $user->id, $vote]);
3230
}else{
3331
user_error('Invalid Poll/Vote "'.$poll.' / '.$vote.'"', E_USER_ERROR);
3432
}
3533

34+
$redirect = base64url_decode(filter_input(INPUT_GET, 'redirect', FILTER_DEFAULT, FILTER_REQUIRE_SCALAR)) ?? null; // $_GET['redirect']
3635
header('Location: '.$redirect);
3736
exit;
3837

www/includes/poll.inc.php

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,34 @@ class Polls
3838
* @param $id Poll-ID to display
3939
* @global object $db Globales Class-Object mit allen MySQL-Methoden
4040
* @global object $user Globales Class-Object mit den User-Methoden & Variablen
41-
* @return string HTML-markup to display the Poll
41+
* @return string smarty->fetch() Results
4242
*/
4343
function show($id)
4444
{
4545
global $db, $user, $smarty;
4646

47+
/** Validate Parameters */
48+
if (!is_numeric($id) || $id <= 0) {
49+
$smarty->assign('error', ['type' => 'warn', 'title' => t('invalid-poll_id', 'poll', [$id]), 'dismissable' => false]);
50+
return $smarty->fetch('file:layout/elements/block_error.tpl');
51+
}
52+
$id = intval($id);
53+
zorgDebugger::log()->debug('poll %d', [$id]);
54+
55+
$sql = '';
4756
$params = [];
48-
$sql = 'SELECT p.* ,UNIX_TIMESTAMP(p.date) date ,(SELECT count(*) FROM poll_votes WHERE poll=?) total_votes
49-
'.($user->is_loggedin() ? ',(SELECT answer FROM poll_votes WHERE poll=? AND user=?) myvote' : '').'
50-
FROM polls p WHERE id=? GROUP BY p.id';
51-
$params[] = $id;
5257
if ($user->is_loggedin()) {
58+
$sql = 'SELECT p.*, UNIX_TIMESTAMP(p.date) date, (SELECT count(*) FROM poll_votes WHERE poll=?) total_votes, (SELECT answer FROM poll_votes WHERE poll=? AND user=?) myvote FROM polls p WHERE id=? GROUP BY p.id';
59+
$params[] = $id;
5360
$params[] = $id;
5461
$params[] = $user->id;
62+
$params[] = $id;
63+
} else {
64+
$sql = 'SELECT p.*, UNIX_TIMESTAMP(p.date) date, (SELECT count(*) FROM poll_votes WHERE poll=?) total_votes FROM polls p WHERE id=? GROUP BY p.id';
65+
$params[] = $id;
66+
$params[] = $id;
5567
}
56-
$params = $id;
5768
$poll = $db->fetch($db->query($sql, __FILE__, __LINE__, __FUNCTION__, $params));
58-
//if (DEVELOPMENT) error_log(sprintf('[DEBUG] <%s:%d> $poll: %s', __FUNCTION__, __LINE__, print_r($poll,true)));
5969

6070
if (!empty($poll) && $poll !== false)
6171
{
@@ -66,29 +76,28 @@ function show($id)
6676
$smarty->assign('user_has_vote_permission', $user_has_vote_permission);
6777
//if (DEVELOPMENT) error_log(sprintf('[DEBUG] <%s:%d> $user_has_vote_permission: %s', __FUNCTION__, __LINE__, ($user_has_vote_permission?'true':'false')));
6878

69-
/** Query Poll answers and return each answer with votes count */
70-
$pollMaxvotes = ($poll['total_votes'] > 0 ? $poll['total_votes'] : 0);
71-
$pollbarMaxwidth = 200;
72-
$pollbarSize = 0;
73-
74-
//$e = $db->query('SELECT count(*) anz FROM poll_votes WHERE poll='.$id.' GROUP BY answer', __FILE__, __LINE__, __FUNCTION__);
75-
$sql = 'SELECT a.*, count(v.user) votes FROM poll_answers a
76-
LEFT JOIN poll_votes v ON v.answer=a.id
77-
WHERE a.poll=? GROUP BY a.id ORDER BY a.id';
79+
$sql = 'SELECT a.*, count(v.user) votes FROM poll_answers a LEFT JOIN poll_votes v ON v.answer=a.id WHERE a.poll=? GROUP BY a.id ORDER BY a.id';
7880
$pollAnswers = $db->query($sql, __FILE__, __LINE__, __FUNCTION__, [$id]);
7981
while ($pollAnswer = $db->fetch($pollAnswers))
8082
{
8183
$pollAnswersArray[$pollAnswer['id']] = $pollAnswer;
8284

85+
/** Query Poll answers and return each answer with votes count */
86+
$pollMaxvotes = ($poll['total_votes'] > 0 ? $poll['total_votes'] : 0);
87+
$pollbarMaxwidth = 200;
88+
$pollbarSize = 0;
89+
8390
/** Poll votes result-bar calculations */
84-
if ($pollAnswer['votes'] == 0) $pollbarSize = 1;
85-
else $pollbarSize = round($pollAnswer['votes'] / $pollMaxvotes * $pollbarMaxwidth);
91+
if (empty($pollAnswer['votes'])) {
92+
$pollbarSize = 1;
93+
} else {
94+
$pollbarSize = round($pollAnswer['votes'] / $pollMaxvotes * $pollbarMaxwidth);
95+
}
8696
$pollAnswersArray[$pollAnswer['id']]['pollbar_size'] = $pollbarSize;
8797
$pollAnswersArray[$pollAnswer['id']]['pollbar_space'] = $pollbarMaxwidth - $pollbarSize;
8898

8999
if ($poll['myvote'] == $pollAnswer['id']) {
90100
if ($poll['myvote'] && $poll['state']=='open' && $user_has_vote_permission) {
91-
//$old_url = base64url_encode("$_SERVER[PHP_SELF]?".url_params());
92101
$pollAnswersArray[$pollAnswer['id']]['unvote_url'] = '/actions/poll_unvote.php?poll='.$poll['id'].'&redirect='.getURL();
93102
}
94103
}
@@ -117,12 +126,12 @@ function show($id)
117126
$smarty->assign('voters', $pollVotersArray);
118127
}
119128

120-
$smarty->display('file:layout/partials/polls/poll.tpl');
129+
return $smarty->fetch('file:layout/partials/polls/poll.tpl');
121130

122131
/** Poll not found - $id invalid */
123132
} else {
124133
$smarty->assign('error', ['type' => 'warn', 'title' => t('invalid-poll_id', 'poll', [$id]), 'dismissable' => false]);
125-
$smarty->display('file:layout/elements/block_error.tpl');
134+
return $smarty->fetch('file:layout/elements/block_error.tpl');
126135
}
127136
}
128137

@@ -143,7 +152,7 @@ function user_has_vote_permission($poll_type)
143152
}
144153

145154
/**
146-
* Updates the title and options of a poll.
155+
* // TODO Updates the title and options of a poll.
147156
* @link https://zorg.ch/bug/765 [Bug #765] Edit-Link bei bestehenden My Polls fehlt
148157
*
149158
* @version 1.0
@@ -173,4 +182,29 @@ public function update($poll_id, $title, $type, $answers) {
173182

174183
return true;
175184
}
185+
186+
/**
187+
* Return all Poll IDs
188+
*
189+
* @version 1.0
190+
* @since 1.0 `11.01.2024` `IneX` Method added
191+
*
192+
* @global object $db Globales Class-Object mit allen MySQL-Methoden
193+
* @return array Array with all IDs of all Polls
194+
*/
195+
public function getAll()
196+
{
197+
global $db;
198+
199+
$polls = [];
200+
$e = $db->query('SELECT id FROM polls ORDER BY date DESC', __FILE__, __LINE__, 'SELECT id FROM polls');
201+
while ($d = $db->fetch($e)) {
202+
$polls[] = $d['id'];
203+
}
204+
205+
return $polls;
206+
}
176207
}
208+
209+
/** Instantiate Polls */
210+
$polls = new Polls();

www/includes/smarty.fnc.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
include_once INCLUDES_DIR.'go_game.inc.php';
2121
include_once INCLUDES_DIR.'quotes.inc.php';
2222
include_once INCLUDES_DIR.'stockbroker.inc.php';
23-
include_once INCLUDES_DIR.'util.inc.php';
2423
include_once INCLUDES_DIR.'poll.inc.php';
2524
include_once INCLUDES_DIR.'stl.inc.php';
2625
include_once INCLUDES_DIR.'error.inc.php';
@@ -587,11 +586,13 @@ function smarty_new_tpl_link ($params, $content, &$smarty, &$repeat) {
587586

588587
return '<a href="/?tpleditor=1&tplupd=new&location='.base64url_encode($_SERVER['PHP_SELF'].'?'.url_params()).'">'.$content.'</a>';
589588
}
590-
function smarty_edit_link ($params, $content, &$smarty, &$repeat) {
591-
589+
function smarty_edit_link ($params, $content, &$smarty, &$repeat)
590+
{
592591
if (!$repeat) { // closing tag
593592
if ($params['tpl']) {
594593
$tpl = $params['tpl'];
594+
$rights = 0;
595+
$owner = 0;
595596
}else{
596597
$vars = $smarty->get_template_vars();
597598
$tpl = $vars['tpl']['id'];
@@ -740,12 +741,13 @@ function smarty_getdailyquote ($params) {
740741
*/
741742
function smarty_poll ($params)
742743
{
743-
if (!isset($params['id']) || empty($params['id']) || !is_numeric($params['id']))
744+
global $polls;
745+
if (!isset($params['id']) || empty($params['id']) || !is_numeric($params['id']) || intval($params['id'])<=0)
744746
{
745747
return smarty_error(['msg' => t('invalid-poll_id', 'poll', [$params['id']])]);
746748
} else {
747-
$poll = new Polls();
748-
return $poll->show($params['id']);
749+
//$poll = new Polls(); --> Instantiated in poll.inc.php
750+
return $polls->show(intval($params['id']));
749751
}
750752
}
751753

www/packages/polls.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Polls Packages
4+
*
5+
* Holt und übergibt Polls an Smarty
6+
*
7+
* @version 1.0
8+
* @since 1.0 `11.01.2024` `IneX` Package added
9+
*
10+
* @package zorg\Polls
11+
*/
12+
13+
/**
14+
* @global object $polls Globales Class-Object mit allen Polls-Methoden
15+
*/
16+
global $polls;
17+
18+
//$polls = new Polls(); --> Instantiated in poll.inc.php
19+
$smarty->assign('polls', $polls->getAll());

www/scripts/poll_editor.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
<?php
2-
require_once dirname(__FILE__).'/../includes/config.inc.php';
2+
require_once __DIR__.'/../includes/config.inc.php';
33
require_once INCLUDES_DIR.'usersystem.inc.php';
44

55
global $smarty, $user;
66

77
if ($user->is_loggedin())
88
{
9-
$types = array();
10-
$types_n = array();
11-
$types[] = 'standard';
12-
$types_n[] = 'Standard';
13-
14-
if ($user->typ == USER_MEMBER) {
9+
$types = ['standard'];
10+
$types_n = ['Standard'];
11+
12+
if ($user->typ >= USER_MEMBER) {
1513
$types[] = 'member';
1614
$types_n[] = 'Member';
1715
}
18-
16+
1917
$smarty->assign('poll_types_v', $types);
2018
$smarty->assign('poll_types_n', $types_n);
2119
} else {

www/scripts/poll_my.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
if ($user->is_loggedin())
55
{
6-
$e = $db->query('SELECT * FROM polls WHERE user='.$user->id.' ORDER BY date DESC', __FILE__, __LINE__, 'SELECT * FROM polls');
7-
$polls = array();
6+
$polls = [];
7+
$e = $db->query('SELECT * FROM polls WHERE user=? ORDER BY date DESC', __FILE__, __LINE__, 'SELECT polls of User', [$user->id]);
88
while ($d = $db->fetch($e)) {
99
$polls[] = $d['id'];
1010
}

www/scripts/poll_overview.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
global $db, $user, $smarty;
33

4-
$e = $db->query('SELECT * FROM polls ORDER BY date DESC', __FILE__, __LINE__);
5-
$polls = array();
4+
$polls = [];
5+
$e = $db->query('SELECT * FROM polls ORDER BY date DESC', __FILE__, __LINE__, 'SELECT * FROM polls');
66
while ($d = $db->fetch($e)) {
77
$polls[] = $d['id'];
88
}

0 commit comments

Comments
 (0)