forked from borisbrodski/sevenzipjbinding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvoting.php
135 lines (114 loc) · 3.68 KB
/
voting.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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="Database List">
<meta http-equiv="Pragma" content="no-cache">
<link href="styles.css" rel="stylesheet" type="text/css">
<title>7-Zip-JBinding voting</title>
</head>
<body>
<table>
<tr>
<td width="210"><?php
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function isIpRegistered($vote_id, $ip) {
$iptest = mysql_query("SELECT COUNT(*) FROM voteentry WHERE vote_id = $vote_id AND ip = '$ip'");
$iptestrow = mysql_fetch_array($iptest, MYSQL_NUM);
mysql_free_result($iptest);
/*
if ($iptestrow[0] > 0) {
echo "IP ALREADY REGISTERED!: " . $iptestrow[0];
} else {
echo "IP NOT REGISTERED!: " . $iptestrow[0];
}
*/
return $iptestrow[0] > 0;
}
function getVoteCount($vote_id, $votechoice_id) {
$voteentries = mysql_query("SELECT COUNT(*) FROM voteentry WHERE vote_id="
. $vote_id . " AND votechoice_id=" . $votechoice_id);
$voteentry = mysql_fetch_array($voteentries, MYSQL_NUM);
mysql_free_result($voteentries);
return $voteentry[0];
}
function isVoteOpened($vote_id) {
$voteorders = mysql_query("SELECT `order` FROM vote WHERE id=$vote_id");
$voteorder = mysql_fetch_array($voteorders, MYSQL_NUM);
mysql_free_result($voteorders);
return $voteorder[0] > 0;
}
//print_r($_POST);
// echo "xx" . $_SERVER['REMOTE_ADDR'] . "xx<br>";
// echo "xx" . getRealIpAddr() . "xx";
$ip = getRealIpAddr();
// echo "IP: $ip";
$link = mysql_connect('mysql-s', 's210915rw', 'HeMySQ15');
if (!$link) {
die('DB connection error: ' . mysql_error());
}
mysql_select_db("s210915_main");
$votes = mysql_query("SELECT id, question FROM vote WHERE `order` > 0 ORDER BY `order`");
while ($vote = mysql_fetch_array($votes, MYSQL_NUM)) {
echo '<div class="voting">';
$voteentry_id = $_GET['vote' . $vote[0]];
$ipRegistered = isIpRegistered($vote[0], $ip);
$show_results = $ipRegistered || $voteentry_id || $_GET["results"];
if ($show_results) {
if (!$ipRegistered && $voteentry_id && isVoteOpened($vote[0])) {
// Storing vote results
$sql = "INSERT INTO voteentry (vote_id, votechoice_id, date, ip) "
. "VALUES (". $vote[0] . ", $voteentry_id, NOW() , '$ip')";
mysql_query($sql);
}
} else {
echo '<form action="/voting.php" method="get" >';
}
echo '<table class="voting"><tr><td class="votequestion" colspan="2">';
echo $vote[1];
echo '<tr><td>';
$votechoices = mysql_query("SELECT id, description FROM votechoice WHERE vote_id = " . $vote[0] ." ORDER BY `order`");
while ($votechoice = mysql_fetch_array($votechoices, MYSQL_NUM)) {
echo '<tr>';
echo '<td class="votingdescr" valign="top">';
if ($show_results) {
$count = getVoteCount($vote[0], $votechoice[0]);
echo "<b>$count</b> - ";
} else {
echo '<INPUT type="radio" name="vote'.$vote[0].'" value="'.$votechoice[0].'"><td class="votingdescr">';
}
echo $votechoice[1];
}
mysql_free_result($votechoices);
if (!$show_results) {
echo '<tr><td colspan="2"><INPUT type="submit" value="Send">';
}
echo "</table>";
if (!$show_results) {
echo '</form>';
}
echo '</div>';
echo '<br>';
echo '<br>';
}
mysql_free_result($votes);
mysql_close($link);
?></td>
</tr>
</table>
</html>