-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchallenge.php
156 lines (138 loc) · 6.41 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
<?php
include_once('./config.php');
define('SELF_FILE', __FILE__);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>challenge</title>
<!-- source_header -->
<?php include_once(ROOT_DIR.'template/source_header.php'); ?>
</head>
<body>
<div class="body-wrap boxed-container">
<!-- header -->
<?php include_once(ROOT_DIR.'template/header.php'); ?>
<div class="container">
<h1 class="text-center">Challenge</h1>
<div>
<?php
$conn = get_sql_conn();
$user_solved = array();
if(isset($_SESSION['uid']) && isset($_SESSION['user'])){
$sql = "select cid from solved where uid=" . addslashes((string)$_SESSION['uid']);
$user_result = $conn->query($sql);
if ($user_result->num_rows > 0) {
while($row = $user_result->fetch_assoc()) {
array_push($user_solved, $row['cid']);
}
}
}
$sql = "select name,times,cid from (select distinct a.name as name, count(b.sid) as times, a.cid as cid from challenge as a left join solved as b on a.cid=b.cid group by cid,a.name) as c order by times desc, cid";
$result = $conn->query($sql);
$length = $result->num_rows;
$conn->close();
for($i = 0; $i < ceil($length/4); $i ++){
?>
<div class="row">
<?php
for($ii = 0; $ii < 4; $ii++){
if($row = $result->fetch_assoc()){
if($user_solved && in_array($row['cid'], $user_solved)){
echo '<div class="col-md-3 col-lg-3 challenge" cid="'.$row['cid'].'"><div class="bg-success challenge-inner">';
}else{
echo '<div class="col-md-3 col-lg-3 challenge" cid="'.$row['cid'].'"><div class="bg-primary challenge-inner">';
}
echo '<h3 class="text-center">'.htmlspecialchars($row['name']).'</h3>';
echo '<h5 class="text-center">solved: '.htmlspecialchars($row['times']).' times</h5>';
echo '</div></div>';
}else{
break;
}
}
?>
</div>
<?php } ?>
</div>
</div>
<!-- source_footer -->
<?php include_once(ROOT_DIR.'template/source_footer.php'); ?>
</div>
<script>
$(document).ready(function () {
$(".challenge").click(function () {
$.getJSON("<?php echo (relative(SELF_FILE)); ?>challenge_detail.php", { cid: $(this).attr("cid") }, function (data) {
$("#challenge-info").attr("cid", data.cid);
$("#challenge-name").text(data.name);
$("#challenge-content").html(data.content);
$("#challenge-file").html(data.file.replace(/ /g, "<br/>"));
if (data.isSolved == "true") {
$("#challenge-flag").hide();
$("#challenge-solved").show();
} else {
$("#challenge-flag").show();
$("#challenge-solved").hide();
}
$('#challenge-info').modal('show');
});
});
$("#flag-submit").click(function () {
$.post("<?php echo (relative(SELF_FILE)); ?>flag.php", {
cid: $("#challenge-info").attr("cid"),
flag: $("#flag-content").val()
},
function (data, status) {
if (data == "success") {
$("#challenge-flag").hide();
$("#challenge-solved").show();
} else if (data == "failed") {
alert("flag error");
} else {
alert(data);
}
});
});
});
</script>
<div class="modal fade" id="challenge-info" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title error text-center" id="challenge-name"></h4>
</div>
<div class="modal-body">
<div id="challenge-content"></div>
<div id="challenge-file"></div>
<div id="challenge-flag">
<div style="padding: 100px 100px 10px;">
<div class="bs-example bs-example-form" role="form">
<div class="row">
<div class="">
<div class="input-group">
<input type="text" placeholder="flag" id="flag-content"
class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" id="flag-submit"
type="button">Submit</button>
</span>
</div><!-- /input-group -->
</div>
</div><!-- /.row -->
</div>
</div>
</div>
<div id="challenge-solved" style="margin-top:2em;"><p class="text-center" style="color:green;">You had solved this challenge.</p></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
</body>
</html>