-
Notifications
You must be signed in to change notification settings - Fork 4
/
polls_take_vote.php
144 lines (109 loc) · 4.09 KB
/
polls_take_vote.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* http://btdev.net:1337/svn/test/Installer09_Beta
* Licence Info: GPL
* Copyright (C) 2010 BTDev Installer v.1
* A bittorrent tracker source based on TBDev.net/tbsource/bytemonsoon.
* Project Leaders: Mindless,putyn.
**/
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'include'.DIRECTORY_SEPARATOR.'bittorrent.php');
require_once(INCL_DIR.'user_functions.php');
dbconn(true);
loggedinorreturn();
//print_r($_POST);
//print_r($_GET); exit;
$lang = array_merge( load_language('global') );
$poll_id = isset($_GET['pollid']) ? intval($_GET['pollid']) : false;
if ( ! is_valid_id($poll_id) )
stderr('ERROR', 'No poll with that ID');
$vote_cast = array();
$_POST['choice'] = isset($_POST['choice']) ? $_POST['choice'] : array();
//-----------------------------------------
// Permissions check
//-----------------------------------------
/*
if ( ! $CURUSER['can_vote'] )
{
stderr( 'ERROR', 'ya nay alood ta vot' );
}
*/
$query = sql_query( "SELECT * FROM polls
LEFT JOIN poll_voters ON polls.pid = poll_voters.poll_id
AND poll_voters.user_id = {$CURUSER['id']}
WHERE pid = ".sqlesc($poll_id) );
if ( ! mysql_num_rows( $query ) == 1 )
{
stderr( 'ERROR', 'No poll with that ID' );
}
$poll_data = mysql_fetch_assoc( $query );
if ( $poll_data['user_id'] )
{
stderr( 'ERROR', 'You have already voted!' );
}
$_POST['nullvote'] = isset($_POST['nullvote']) ? $_POST['nullvote'] : 0;
if ( !$_POST['nullvote'] )
{
if ( is_array( $_POST['choice'] ) and count( $_POST['choice'] ) )
{
foreach( $_POST['choice'] as $question_id => $choice_id )
{
if ( ! $question_id or ! isset($choice_id) )
{
continue;
}
$vote_cast[ $question_id ][] = $choice_id;
}
}
foreach( $_POST as $k => $v )
{
if( preg_match( "#^choice_(\d+)_(\d+)$#", $k, $matches ) )
{
if( $_POST[$k] == 1 )
{
$vote_cast[ $matches[1] ][] = $matches[2];
}
}
}
$poll_answers = unserialize( stripslashes( $poll_data['choices'] ) );
reset($poll_answers);
if ( count( $vote_cast ) < count( $poll_answers ) )
{
stderr( 'ERROR', 'No vote' );
}
@sql_query( "INSERT INTO poll_voters (user_id, ip_address, poll_id, vote_date)
VALUES ({$CURUSER['id']},
".sqlesc($CURUSER['ip']).",
{$poll_data['pid']},
".time().")" );
if( -1 == mysql_affected_rows() )
stderr('DBERROR', 'Could not update records');
foreach ( $vote_cast as $question_id => $choice_array )
{
foreach( $choice_array as $choice_id )
{
$poll_answers[ $question_id ]['votes'][ $choice_id ]++;
if ( $poll_answers[ $question_id ]['votes'][ $choice_id ] < 1 )
{
$poll_answers[ $question_id ]['votes'][ $choice_id ] = 1;
}
}
}
$poll_data['choices'] = addslashes( serialize( $poll_answers ) );
@sql_query( "UPDATE polls set votes=votes+1, choices='{$poll_data['choices']}'
WHERE pid={$poll_data['pid']}" );
if( -1 == mysql_affected_rows() )
stderr('DBERROR', 'Could not update records');
}
else
{
@sql_query("INSERT INTO poll_voters (user_id, ip_address, poll_id, vote_date)
VALUES
({$CURUSER['id']}, ".sqlesc($CURUSER['ip']).", {$poll_data['pid']},
".time().")"
);
if( -1 == mysql_affected_rows() )
stderr('DBERROR', 'Could not update records');
}
$mc1->delete_value('poll_data_'.$CURUSER['id']);
header("location: {$TBDEV['baseurl']}/index.php");
?>