Skip to content

Commit 98d409f

Browse files
committed
init
0 parents  commit 98d409f

File tree

8 files changed

+476
-0
lines changed

8 files changed

+476
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

app.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var express = require('express');
2+
var app = express();
3+
var fs = require('fs');
4+
const {exec} = require('child_process');
5+
const uuidv1 = require('uuid/v1');
6+
7+
var bodyParser = require('body-parser');
8+
app.use(bodyParser.json()); // support json encoded bodies
9+
app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
10+
11+
app.get('/', function (req, res) {
12+
res.sendFile("index.html", {root: __dirname});
13+
});
14+
15+
app.post('/compile', function (req, res) {
16+
let code = req.body.code;
17+
18+
//create dir
19+
let dir = tmpDir();
20+
console.log('folder created')
21+
//create file
22+
fs.writeFile(dir + '/file.php', code, function (err) {
23+
if (err) throw err;
24+
//run docker
25+
let command = `docker run --rm -v $(pwd):/app -w /app php:cli php ${dir}/file.php`;
26+
console.log(command)
27+
exec(command, (err, stdout, stderr) => {
28+
res.json({
29+
"output": stdout
30+
});
31+
//delete file and folder
32+
fs.unlinkSync(dir + "/file.php");
33+
fs.rmdirSync(dir)
34+
console.log('folder removed')
35+
});
36+
});
37+
});
38+
39+
app.listen(3000, function () {
40+
console.log('app listening on port 3000!');
41+
});
42+
43+
function tmpDir() {
44+
var dir = 'temp/' + uuidv1();
45+
46+
fs.mkdir(dir, {recursive: true}, (err) => {
47+
if (err) throw err;
48+
});
49+
50+
return dir;
51+
}

index.html

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>PHP sandbox</title>
6+
<link rel="stylesheet" href="https://unpkg.com/mustard-ui@latest/dist/css/mustard-ui.min.css">
7+
</head>
8+
<body>
9+
<div class="">
10+
<h1>PHP sandbox</h1>
11+
<div class="row">
12+
<div class="col col-lg-6">
13+
<div class="form-control">
14+
<textarea name="code" cols="30" rows="10" placeholder="Write you PHP code here"></textarea>
15+
<div class="form-control">
16+
<button name="submit" class="button-primary button-large">RUN!</button>
17+
</div>
18+
</div>
19+
</div>
20+
<div class="col col-lg-6">
21+
<div class="form-control">
22+
<textarea disabled name="output" cols="30" rows="10"
23+
placeholder="Output of you code here"></textarea>
24+
</div>
25+
</div>
26+
</div>
27+
</div>
28+
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"
29+
integrity="sha256-vrn14y7WH7zgEElyQqm2uCGSQrX/xjYDjniRUQx3NyU=" crossorigin="anonymous"></script>
30+
<script>
31+
$('button[name=submit]').on('click', function () {
32+
$(this).attr('disabled', true).text('Loading...');
33+
$.ajax({
34+
type: 'POST',
35+
url: '/compile',
36+
data: {
37+
code: $('textarea[name=code]').val()
38+
},
39+
dataType: 'json',
40+
success: function (data) {
41+
console.log(data);
42+
43+
var type = data.type == 'error' ? 'invalid' : '';
44+
$('textarea[name=output]').val(data.output).addClass(type);
45+
46+
$('button[name=submit]').removeAttr('disabled').text('RUN!');
47+
},
48+
error: function (xhr, type) {
49+
alert('error from api')
50+
}
51+
});
52+
})
53+
</script>
54+
</body>
55+
</html>

package.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"dependencies": {
3+
"body-parser": "^1.18.3",
4+
"express": "^4.16.4",
5+
"fs": "^0.0.1-security",
6+
"uuid": "^3.3.2"
7+
}
8+
}

readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# PHP sandbox
2+
Written in node.js and uses docker
3+
![Screenshot](screenshot.png]
4+
# TODO !!!
5+
- add timeout docker

screenshot.png

3.79 KB
Loading

temp/readme.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This folder have folders with php code, uses in docker containers

0 commit comments

Comments
 (0)