-
Notifications
You must be signed in to change notification settings - Fork 13
/
buildStm8.php
81 lines (80 loc) · 2.37 KB
/
buildStm8.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
<?php
function ip()
{
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
{
$ip = getenv('HTTP_CLIENT_IP');
}
elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
{
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
{
$ip = getenv('REMOTE_ADDR');
}
elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : 'unknown';
}
$ip=ip();
$times=date("Y-m-d");
$time=date("Y-m-d H:i:s");
$str=$ip." ".$time;
$l=fopen("log/$times.txt","a+");
fwrite($l,$str. "\n");
fclose($l);
$res = array('result'=>0, 'info'=>'', 'bin'=>'');
//创建临时目录
$dirname = "stm8_" . strval(getmypid());
if(mkdir($dirname))
{
//创建app.c,拷入代码
chdir($dirname);
$file = fopen("app.c", "w+");
fwrite($file, file_get_contents("php://input"));
fclose($file);
//调用wine
exec("wine ../make -f ../Makefile", $output, $return_var);
if($return_var == 0) //执行成功
{
$res['result'] = 1;
$filename = "app.bin";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
$res['bin'] = base64_encode($contents);
}
else //失败
{
//nothing
}
//生成编译信息
$res['info'] = implode
( " ",
array_merge(
array("编译结果:\n"),file_exists("c.txt")?array_slice(file("c.txt"),4):array("无\n"),
array("链接结果:\n"),file_exists("l.txt")?array_slice(file("l.txt"),3):array("无\n"),
array("转换结果:\n"),file_exists("b.txt")?array_slice(file("b.txt"),4):array("无\n")
)
); //融合转换结果
$res['info'] = str_replace
( "\"Z:\\www\\uclink.org\\_\\STM8_app\\", "\"",
$res['info']
); //替换无关字符
//删除临时目录
chdir("../");
$file = fopen("app.log", "w+");
fwrite($file, $res['info']);
fclose($file);
exec("rm -rf $dirname");
}
else
{
$res['info'] = '临时文件夹创建失败!';
}
//返回结果
echo json_encode($res);
?>