-
Notifications
You must be signed in to change notification settings - Fork 3
/
tikz.php
172 lines (135 loc) · 4.37 KB
/
tikz.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Execute a command and kill it if the timeout limit fired to prevent long php execution
*
* @see http://stackoverflow.com/questions/2603912/php-set-timeout-for-script-with-system-call-set-time-limit-not-working
*
* @param string $cmd Command to exec (you should use 2>&1 at the end to pipe all output)
* @param integer $timeout
* @return string Returns command output
*/
function exec_timeout($cmd, $timeout = 5)
{
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$pipes = array();
$timeout += time();
$process = proc_open($cmd, $descriptorspec, $pipes);
if (!is_resource($process)) {
throw new Exception("proc_open failed on: " . $cmd);
}
$output = '';
do {
$timeleft = $timeout - time();
$read = array($pipes[1]);
stream_select($read, $write = NULL, $exeptions = NULL, $timeleft, NULL);
if (!empty($read)) {
$output .= fread($pipes[1], 8192);
}
} while (!feof($pipes[1]) && $timeleft > 0);
if ($timeleft <= 0) {
proc_terminate($process);
throw new Exception("command timeout on: " . $cmd);
} else {
return $output;
}
}
/**
* @returns whether a file is old, i.e. more than 20sec!
*/
function is_old_file($file)
{
return filemtime($file) < time() - 20;
}
/**
* @returns true if $haystack finishes with $needle
*/
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if (!$length) {
return true;
}
return substr($haystack, -$length) === $needle;
}
/**
* remove too old files
*/
function clean()
{
$files = glob('*'); // get all file names
foreach ($files as $file) { // iterate files
if (is_file($file) && is_old_file($file) && (endsWith($file, ".log") || endsWith($file, ".tex") || endsWith($file, ".pdf") || endsWith($file, ".aux"))) {
unlink($file); // delete file
}
}
}
$trueiffinalversiontodownload = false;
/*
input: a LaTEX file (without directory information and without extension)
(but such a file exists in the directory tmp and with the extension ".tex")
output: compile the file and generate a pdf and an image .png of the pdf
*/
function compileAndGenerateImage($latexfilename)
{
chdir("tmp");
if (file_exists("$latexfilename.svg"))
unlink("$latexfilename.svg");
try {
exec_timeout("pdflatex -interaction=nonstopmode -shell-escape $latexfilename.tex", 5);
} catch (Exception $e) {
echo "error in compiling", $e->getMessage(), "\n";
}
if (!file_exists("$latexfilename.svg")) {
if (!file_exists("$latexfilename.log")) {
echo ("no .log file\n");
}
echo file_get_contents("$latexfilename.log");
}
clean();
chdir("..");
}
/*
input: the latexcode of an exercice typically a "\begin{exercice}.... \end{exercice}\begin{correction}...\begin{correction}" string
output: the code of a document containing the exercice and such that the result of a LaTEX compilation is a pdf or png whose size of the page crop the exercice
*/
function getLaTEXCodeFromOneTikzCrop($latexcode)
{
$str = file_get_contents("latexheader.tex") . "\\begin{document}\n";
$str .= $latexcode;
$str .= "\n\n \\end{document}";
return $str;
}
/*
input: latex code of an exercice (typically a "\begin{exercice}.... \end{exercice}\begin{correction}...\begin{correction}"
WRITE the content of a real document that crop the page to the exercice
return the name of the file (without directory information and without extension) containing this document LaTEX code
this file is in the directory tmp
*/
function producedottex($latexcode, $latexfilename)
{
$mytmpfile = "tmp/$latexfilename.tex";
$finalCode = getLaTEXCodeFromOneTikzCrop($latexcode);
file_put_contents($mytmpfile, $finalCode);
}
session_start();
$id = session_id();
$code = $_POST["code"];
$trueiffinalversiontodownload = $_POST["trueiffinalversiontodownload"];
set_time_limit(2);
if (!isset($code)) {
echo "no code was given : $code";
exit;
}
if ($trueiffinalversiontodownload != "true") {
$latexfilename = "tmp$id";
} else {
$latexfilename = "tmptodownload$id";
}
producedottex($code, $latexfilename);
compileAndGenerateImage($latexfilename);
echo ("\ntmp/$latexfilename");
?>