-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
70 lines (51 loc) · 1.24 KB
/
api.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
<?php
include "geraterandompath.php";
//get a valid path
do{
$token = "files_exec/".generateRandomString();
}while( is_dir($token) );
//code sended
$code = $_POST['code'];
//lang of code
$lang = isset($_POST['lang'])?$_POST['lang']:"c";
//creating folders
$uritoken= $token."/";
//has dir?
if( !mkdir($uritoken) ){
echo '{"status": "file-error"}';
return;
}
/*Language supported: c and python*/
if($lang == "c"){
$namefile =$uritoken."code.c";
$myfile = fopen($namefile, "w");
fwrite($myfile, $code);
fclose($myfile);
$cmd = "gcc ".$namefile." -o ".$uritoken."file.exe 2> ".$uritoken."output";
}else if($lang == "py"){
$namefile =$uritoken."code.py";
$myfile = fopen($namefile, "w");
fwrite($myfile, $code);
fclose($myfile);
$cmd = "python ".$namefile;
}
//executing code
exec($cmd, $result, $error);
//has a error?
if($error){
echo '{"status": "error"}';
}else{
echo '{"status": "noerror"}';
}
//delete all file gerate
if( file_exists($uritoken."output") )
unlink($uritoken."output");
if( file_exists($uritoken."code.c") )
unlink($uritoken."code.c");
if( file_exists($uritoken."code.py"))
unlink($uritoken."code.py");
if( file_exists($uritoken."file.exe"))
unlink($uritoken."file.exe");
if(is_dir($token))
rmdir($token);
?>