forked from DreyWesson/team-superman-hngi7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
123 lines (105 loc) · 4.09 KB
/
index.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
<?php
$files = array_filter(scandir('scripts'), function ($script) {
return !is_dir('scripts/' . $script);
}); // To remove "." and ".." from the array output os scabdir
$final = [];
if ($files) {
foreach ($files as $file) {
$script = [];
$script['file'] = $file;
if (preg_match('/.php$/i', $file)) {
$output = exec('php scripts/' . $file);
} elseif (preg_match('/.py$/i', $file)) {
$output = exec('python scripts/' . $file);
} elseif (preg_match('/.js$/i', $file)) {
$output = exec('node scripts/' . $file);
}
if (isset($output)) {
$script['output'] = $output;
$result = [];
preg_match('/^Hello World, this is ([a-zA-Z -]*) with HNGi7 ID ((HNG-|)[0-9]{1,5}) using (Python|PHP|JavaScript|Node.js) for stage 2 task(.|)$/i', $output, $result);
if (count($result) > 0) {
$script['name'] = $result[1];
$script['id'] = $result[2];
$script['language'] = $result[4];
$script['status'] = 'Pass';
} else {
$script['name'] = "";
$script['id'] = "";
$script['language'] = "";
$script['status'] = 'Fail';
}
array_push($final, $script);
}
}
}
if (!isset($_GET['json'])) {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Team Superman- Task 2</title>
<!-- Bootstrap core CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet">
<link href="//cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top">
<div class="container">
<a class="navbar-brand" href="#">Team Superman- Task 2</a>
</div>
</nav>
<!-- Page Content -->
<main role="main" class="container">
<div class="my-3 p-3 bg-white rounded shadow-sm">
<h6 class="border-bottom border-gray pb-2 mb-0">Scripts Results</h6>
<table id="table" class="table table-striped table-bordered">
<thead>
<tr>
<th>Status</th>
<th>Output</th>
<th>File Name</th>
<th>Name</th>
<th>Language</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<?php foreach ($final as $script) { ?>
<tr>
<td><?=$script['status']=='Pass'? '<span class="badge badge-success">Pass</span>': '<span class="badge badge-danger">Fail</span>' ?></td>
<td><?=$script['output']?></td>
<td><?=$script['file']?></td>
<td><?=$script['name']?></td>
<td><?=$script['language']?></td>
<td><?=$script['id']?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</main>
<!-- Bootstrap core JavaScript -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script src="//cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#table').DataTable();
});
</script>
</body>
</html>
<?php
} else {
//return the json response :
header('Content-Type: application/json'); // <-- header declaration
echo json_encode($final, true); // <--- encode
exit();
}