-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompileTexFromDir.php
179 lines (162 loc) · 6.24 KB
/
compileTexFromDir.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
173
174
175
176
177
178
179
<?php
/*
En entree, la requete est :
de type POST
parametres :
- dir : (commence par /docs/ ou /cache)
- source :
- stopOnError :
- withLogFile :
- fileName :
*/
/**
* LIMITS: Original documents must be in directory name as /docs/ or /cache/
* The regep is not in config yet
*/
include __DIR__ . "/tex.php";
ini_set("memory_limit","-1");
set_time_limit(0);
/* Pour transition entre ccsd06 et MV */
$arch = php_uname('m');
$confFile = '/etc/default/convert';
/** Bon ce serait bien d'avoir ça en config globale... */
define('ARCH', 'x86_64-linux');
$texliveversion = 2020; // by default... is it needed ? Can break if config not here ?
# on a machine, we can choose the good latex version
if (file_exists($confFile)) {
$config = file_get_contents($confFile);
$matches = [];
if (preg_match('/TEXLIVEVERSION\s+=\s+(\d+)/', $config, $matches)) {
$texliveversion = $matches[1];
}
}
define('TEXLIVEVERSION', $texliveversion);
/** Default configuration */
/* Les constantes correspondantes seront definies apres l'include de la configuration */
$CHROOT='/latexRoot';
$BASETEMPREP='/tmp/ccsdtex';
$HOME='/home/nobody';
$GLOBALS['texlive'] = "/usr/local/texlive/" . TEXLIVEVERSION;
$GLOBALS['path'] = "/usr/local/texlive/" . TEXLIVEVERSION . '/bin/' . ARCH . '/';
$GLOBALS['tex'] = "tex -interaction=nonstopmode";
$GLOBALS['latex'] = "latex -interaction=nonstopmode";
$GLOBALS['pdflatex'] = "pdflatex -interaction=nonstopmode";
$GLOBALS['xelatex'] = "xelatex -interaction=nonstopmode";
$GLOBALS['bibtex'] = "bibtex -terse";
$GLOBALS['makeindex'] = "makeindex -q";
$GLOBALS['dvips'] = "dvips -q -Ptype1";
$GLOBALS['ps2pdf'] = "/usr/bin/ps2pdf14";
$GLOBALS['chroot'] = "/usr/sbin/chroot";
$conffile = __DIR__ . "/conf.php";
if (file_exists($conffile)) {
include_once $conffile;
}
define('CHROOT', $CHROOT);
define('BASETEMPREP', $BASETEMPREP);
putenv("HOME=$HOME");
putenv('TEXMFVAR=/usr/local/texlive/' . TEXLIVEVERSION . '/texmf-var');
putenv('PATH=/usr/local/texlive/' . TEXLIVEVERSION . '/bin/' . ARCH . '/:/usr/bin/:/bin');
function internalServerError($msg) {
header('HTTP/1.1 500 Internal Server Error');
print "$msg\n";
exit;
}
if ( $_SERVER['REQUEST_METHOD'] != "POST" ) {
header('HTTP/1.1 400 Bad Request');
print 'Use POST HTTP method';
exit;
}
# Traitement des parametres post
$dirparam=isset($_POST['dir']) ? $_POST['dir'] : '';
if ( $dirparam == '' ) {
header('HTTP/1.1 400 Bad Request');
print 'Source directory undefined';
exit;
}
// Répertoire des fichiers sources
$dir = realpath(urldecode($dirparam));
if ( $dir == '' || !is_dir($dir) ) {
internalServerError("Source directory '$dir' not exist");
}
if ( !preg_match('+^(/docs/|/cache|/nas/spm/docs|/nas/docs)+', $dir) ) { # /docs/... pour Hal ou pour sc
# Attention, il faut accepter les /docs/xx/xx/xx
# Et les compilations de frontpage dans /docs/tmp/...
internalServerError("Directory '$dir' is not in the accepted path scope");
}
$dir .= DIRECTORY_SEPARATOR;
$source = isset($_POST['source']) ? $_POST['source'] : '';
// récupération de la variable stopOnError
$stopOnError = true; # default value
if ( isset($_POST['stopOnError']) ) {
$stopOnError = ( $_POST['stopOnError'] == 1 );
}
// récupération de la variable withLogFile
$withLogFile = true; # default value
if ( isset($_POST['withLogFile']) ) {
$withLogFile = ( $_POST['withLogFile'] == 1 );
}
$filename = isset($_POST['fileName']) ? $_POST['fileName'] : '';
// création du répertoire tempo privé
$tempchrootrep = BASETEMPREP.DIRECTORY_SEPARATOR.uniqid('', true).DIRECTORY_SEPARATOR;
$temprep = CHROOT.$tempchrootrep;
if ( !mkdir($temprep, 0777, true) ) {
internalServerError('Can\'t make temp directory' . $temprep);
}
// copie fichiers sources dans un répertoire tempo privé
if ( ($source != '') && is_file($dir.DIRECTORY_SEPARATOR.$source) ) { // un fichier source est fourni.
if ( false === copy($dir.DIRECTORY_SEPARATOR.$source, $temprep.$source) ) {
error_log("Can't copy file $dir/$source to temp directory $temprep");
internalServerError('Can\'t copy source file '
. " (Dirparam: $dirparam) : "
. '"'. $dir.DIRECTORY_SEPARATOR.$source.'" to temp directory "'.$temprep.$source.'"');
}
} else { // on copie tout le répertoire $_POST['dir'].
if (preg_match('|/tmp/?$|', $dir) || false === recurse_copy($dir, $temprep, false) ) {
error_log('Can\'t copy directory "'.$dir.'" to temp directory "'.$temprep." (source=$source)");
internalServerError('Can\'t copy directory "'.$dir.'" to temp directory "'.$temprep.'"');
}
}
// unzip du répertoire $temprep
recurse_unzip($temprep);
// on se place dans le répertoire de travail tempo privé
chdir($temprep);
// recherche des fichiers à compiler
$compilateur = new Ccsd_Tex_Compile($GLOBALS['texlive'], $GLOBALS, $tempchrootrep, CHROOT, $withLogFile, $stopOnError);
$tex_files = $compilateur -> mainTexFile();
if ( count($tex_files) == 0 ) {
recurse_rmdir($temprep);
internalServerError('No TeX, LaTeX or PdfLaTeX primary source file found');
}
$pdfCreated = array();
$fileCreated = array();
// latex ou pdflatex ?
$bin = $compilateur -> checkTexBin();
try {
$fileCreated = $compilateur -> compile($bin,$dir,$tex_files,$filename);
foreach ($fileCreated as $file => $destname) {
copy("$temprep/$file", "$dir/$destname");
if (preg_match('/\.pdf$/', $file)) {
$pdfCreated[] = $destname;
}
}
} catch (TexCompileException $e) {
// Recuperation des logs en cas d'erreurs !
$logfile = $e -> logfile;
error_log("$tempchrootrep ne compile pas. Logfile: $logfile . Et message: " . $e ->getMessage());
if (isset($logfile) && file_exists($logfile)) {
copy("$temprep/$logfile", "$dir/$logfile");
}
recurse_rmdir($temprep);
internalServerError($e -> getMessage());
}
if ( count($pdfCreated) ) {
header('HTTP/1.1 200 OK');
echo '<files><pdf>'.implode('</pdf><pdf>', $pdfCreated).'</pdf></files>';
} else {
recurse_rmdir($temprep);
internalServerError('No pdf created');
}
if (! file_exists(BASETEMPREP . DIRECTORY_SEPARATOR. "NO_RM")) {
recurse_rmdir($temprep);
}
exit;