-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge.php
186 lines (171 loc) · 7.19 KB
/
challenge.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?PHP
session_start();
unset($_SESSION['quizAttempted']);
?>
<html>
<head>
<title> IKwizU </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Tajawal" rel="stylesheet">
<link href="../CSS/styles.css" rel="stylesheet">
<script src="../JS/validation.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<header>
<nav class="navBar">
<nav class="menuwrapper">
<div class="logo"><a href="/IKwizU">IKwizU</a></div>
<input type="checkbox" id="menu-toggle" />
<label for="menu-toggle" class="label-toggle"></label>
<ul>
<li><a href="../about/" >About</a></li>
<li><a href="../standings/" >Check Standings </a></li>
<li><a href="../analytics/">Analyze</a></li>
<li><a href="../feedback/">Feedback</a></li>
</ul>
</nav>
</nav>
</header>
<div id="main">
<?PHP
require "db.inc";
if (!($connection = @ mysqli_connect("localhost", $username, $password)))//Connecting to localhost
die("Could not connect to database");
if (!mysqli_select_db($connection, $databaseName)) //connecting to Database using "db.inc"
showerror($connection);
$_SESSION['challenge'] = true;
$_SESSION['challToken'] = $_REQUEST['challengeToken'];
$lToken = $_REQUEST['challengeToken'];
$invalidToken = 0;
if(!is_numeric($lToken))
$invalidToken = 1;
else {
$tokenDtlsStmt = "select name, email_id, created_date from tokens where id = $lToken";
$challengerDetails = mysqli_query ($connection, $tokenDtlsStmt);
$challengerRec = mysqli_fetch_row($challengerDetails);
$challengerScoreStmt = "select score from scorebytoken where token = $lToken and email = \"$challengerRec[1]\"";
$challengerScore = mysqli_query ($connection, $challengerScoreStmt);
$challengerScoreRec = mysqli_fetch_row($challengerScore);
}
if($invalidToken == 1 || $challengerRec[0] == null){
print "<br/><br/><left class='challenge'><span style='color: #B60000;'>Error: </span>Invalid URL. There exists no challenge with this token.</left>";
print "<right class='challenge'>";
print "Here's a few challenges you can try. Sorted from Hardest to moderate.<br/><br/>";
try{
$i = 0;
$tokenLinksQueryStmt = "select distinct token from scorebytoken order by score DESC LIMIT 10";
$tokenLinksResults = @ mysqli_query ($connection, $tokenLinksQueryStmt);
while ($record = @ mysqli_fetch_array($tokenLinksResults)){
$i += 1;
print "<i class='fa fa-angle-double-right'></i><a class='others' href = 'http://localhost/IKwizU/challenge/".$record["token"]."'> Attempt challenge ". $record["token"]."</a><br/><br/>";
}
}
catch(Exception $e){
print "Error occured while fetching the quizes that couldn't be cracked - ". $e;
}
print"</right>";
exit;
}
if(isset($_POST["proceed"])){
if(!isset($_SESSION['quizAttempted'])){
$_SESSION['inpName'] = mysqli_real_escape_string($connection, $_POST["inpName"]);
$_SESSION['inpEmail'] = mysqli_real_escape_string($connection, $_POST["inpEmail"]);
$lEmail = $_POST["inpEmail"];
$uniqueChallengerQuery = "select * from scorebytoken where token = $lToken and email = \"$lEmail\"";
$uniqueChallengerCheck = mysqli_query($connection, $uniqueChallengerQuery);
$uniqueChallengerRec = mysqli_fetch_row($uniqueChallengerCheck);
if($uniqueChallengerRec[0] != null){
print "<br/>Hey it looks like this challenge was already attempted with the given email id, hence cannot proceed.<br/><br/>";
print "You will be redirected in 15 secs to the challenge page, try with different email address this time. ";
print "<a style='color:#B60000' href='http://localhost/IKwizU/challenge/$lToken'>Click here</a> to redirected manually.";
header( "refresh:15; url=http://localhost/IKwizU/challenge/$lToken" );
exit;
} else {
$i = 0;
$quesArr = [];
$corrAns = [];
$quesStmt = "select qno, question, quesType, optA, optB, optC, optD, optKey from quizbytoken where token = $lToken order by qNo ASC";
$quesAnsQuery = @ mysqli_query ($connection, $quesStmt);
print "<form action='../results/' class ='quizFormBox' method='POST' name='quizForm' onSubmit='return quizFormValidation();'>";
while ($record = @ mysqli_fetch_array($quesAnsQuery)){
$i += 1;
print $record['qno'].". ".$record['question']."<br/><br/>";
if($record['quesType'] == "boolean")
$choicesArr = array($record['optA'], $record['optB']);
else
$choicesArr = array($record['optA'], $record['optB'], $record['optC'], $record['optD']);
foreach($choicesArr as $key=>$choiceOptions){
print"<label class='container'>$choiceOptions
<input type='radio' name='Ques$i' id='$key' value='$choiceOptions'>
<span class='checkmark'></span>
</label><br/>";
}
print "<br/><br/>";
}
print "<button class='formButton' name='submit' type='submit'>Submit</button></form>";
}
} else{
$_SESSION['challenge'] = true;
$_SESSION['challToken'] = $_REQUEST['challengeToken'];
header("Location: http://localhost/IKwizU/results/");
exit;
}
} else {
?>
<br/><br/>
<left class="challenge">
<form method = "POST">
<b>Name: </b><br/>
<input name="inpName" type="text" value="" placeholder="Enter your name here.." class = "standingsFormTB" style="background-color:#FFFFF" required/> <br/><br/>
<b>Email: </b><br/>
<input name="inpEmail" type="email" value="" placeholder="Enter your email here.." class ="standingsFormTB" style="background-color:#FFFFF" required/><br/><br/><br/>
<input type="submit" name ="proceed" value="Proceed to challenge" class="challengeFormButton"/>
<input type="reset" name ="reset" value="Reset Fields" class="challengeFormButton"/><br/><br/><br/>
<label><span style="color: #B60000;">Note: </span>Please mind your email has to be unique, as you can attempt this challenge only once.</label>
</form>
</left>
<?PHP
print "<right class='challenge'><span style='color: steelblue'><b> Challenger details:</b> </span><hr/>";
print "<i class='fa fa-address-book'></i> Name: $challengerRec[0] <br/>
<i class='fa'></i> Score: $challengerScoreRec[0]<br/>
Difficulty: ";
if($challengerScoreRec[0] >= 10)
print "Easy <br/><br/>";
else if($challengerScoreRec[0] >=7)
print "Medium <br/><br/>";
else
print "Hard <br/><br/>";
print "<span style='color: steelblue'><b>$lToken token standings -</b></span><hr/>";
$i=0;
try{
$standingsQueryStmt = "select name, email, score, inserted_date from scorebytoken where token = $lToken order by score DESC, inserted_date ASC";
$standingsResults = @ mysqli_query ($connection, $standingsQueryStmt);
$noOfRowsFound = @ mysqli_num_rows($standingsResults);
print"<table class ='challengeStandings' style='min-width: 100px;'>
<thead>
<th> Standing </th>
<th> Name </th>
<th> Score </th>
</thead>";
while ($record = @ mysqli_fetch_array($standingsResults)){
$i += 1;
print "<tr>";
print "<td>$i</td>";
print "<td>".$record["name"]."</td>";
print "<td>".$record["score"]."</td>";
print "</tr>";
}
print "</table>";
}
catch(Exception $e){
print "Error occured while processing your request - ". $e;
}
print "</right>";
}
?>
</div>
<script>
</script>
</body>
</html>