-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttw_includes_var_func.php
279 lines (217 loc) · 7.88 KB
/
ttw_includes_var_func.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
//Variables...
$path = basename(getcwd()) . "/";
$ttwPath = __DIR__ . "\\ttw\\";
$helpPath = $path . "ttw_show_help.php\" target=\"blank";
$indexPage = "href=\"ttw_index.php\"";
//Create temp ID for temp folder for all session files from Session ID
$currentDate = date('Ymd');
$tempID = substr(session_id(), 0, 4);
$nameTempDirectory = $currentDate . "_" . $tempID;
$sessionTempPath = $ttwPath . $nameTempDirectory . "\\";
//Get URL
$ttwURLcomplete = $_SERVER['REQUEST_URI'];
if($ttwURLcomplete != ""){
$ttwURLsplit = explode("/", $ttwURLcomplete, -1);
$ttwURL = implode("/", $ttwURLsplit);
$ttwURL = $ttwURL . "/";
}
//Functions _______________________________________________________________
function call_ttw($options){
global $ttwPath;
global $nameTempDirectory;
$arguments = $ttwPath . "tagtool_v1-3-0.exe articleFile.html" . " --tempID" . $nameTempDirectory;
foreach($options as $option){
$arguments = $arguments . " " . $option;
}
//Switch on to check ttw call:
//echo "ttw called with following arguments: " . $arguments;
$result = shell_exec($arguments);
return $result;
}
function check_file_properties(){
$ok = true;
//Sizes...
for ($i=0; $i<sizeof($_FILES["userfile"]["tmp_name"]); $i++) {
if($_FILES["userfile"]["size"][$i] >= 1000000 || $_FILES["userfile"]["size"][$i] == 0){
$ok=false;
}
}
//... now types for articleFile ...
if(mime_content_type($_FILES["userfile"]["tmp_name"][0]) != "application/vnd.openxmlformats-officedocument.wordprocessingml.document"){
$ok=false;
}
$fileName = basename($_FILES["userfile"]["name"][0]);
if(!str_contains($fileName, ".docx")){
$ok=false;
}
//... and value lists ...
for ($i=1; $i<sizeof($_FILES["userfile"]["type"]); $i++) {
$fileName = basename($_FILES["userfile"]["name"][$i]);
if(!str_contains($fileName, ".csv")){
$ok=false;
}
if(mime_content_type($_FILES["userfile"]["tmp_name"][$i]) != "text/plain" && mime_content_type($_FILES["userfile"]["tmp_name"][$i]) != "text/csv"){
$ok=false;
}
}
if(!$ok){
return false;
} else {
return true;
}
}
function deleteFolder($folderName) { //function copied from https://paulund.co.uk/
if (is_dir($folderName)){
$folderHandle = opendir($folderName);
}
if (!$folderHandle){
return false;
}
while($file = readdir($folderHandle)) {
if ($file != "." && $file != "..") {
if (!is_dir($folderName."/".$file))
unlink($folderName."/".$file);
else
deleteFolder($folderName.'/'.$file);
}
}
closedir($folderHandle);
rmdir($folderName);
return true;
}
function open_proceed_box($stepNumber, $array){
$file = "ttw_step" . $stepNumber . ".php";
?>
<br /><br />
<table class="box">
<tr>
<?php
if($stepNumber == 2){
echo "<td>Following files have been uploaded:<br />";
}
if($stepNumber == 3) {
echo "<td>Following options have been set:<br />";
}
?>
<span style="color: rgb(50, 205, 50);">
<?php
foreach($array as $option){
echo $option . "<br />";
}
?>
</span></td>
<td><b>Proceed <a href="../<?php global $path; echo $path. $file?>">here to step <?php echo $stepNumber;?></a></b> ✓</td>
</tr>
<table>
<?php
}
function prepare_session() {
global $sessionTempPath;
global $ttwPath;
global $currentDate;
if(!file_exists($sessionTempPath)){
mkdir($sessionTempPath);
}
//Now delete temp folders from previous sessions
$currentFiles = scandir($ttwPath);
//Find garbage folders ..
$garbageFolders = array();
$extractedDate = array();
foreach ($currentFiles as $currentFile){
$pattern = '{(^20[0-9]{6}_)}';
if (preg_match($pattern, $currentFile, $match)) {
$extractedDate = explode("_", $currentFile);
if((int)$extractedDate[0] < (int)$currentDate){ // = folders of the previous day and earlier
array_push($garbageFolders, $currentFile);
}
}
}
//... and delete them
foreach($garbageFolders as $garbageFolder){
$toDelete = $ttwPath . $garbageFolder;
if(!deleteFolder($toDelete)){
echo "Warning: Garbage folder(s) could not be deleted!";
}
}
$_SESSION["sessionPrepared"] = true;
}
function provide_result($fileName, $outputFormat){
global $tempID;
global $currentDate;
global $ttwURLcomplete;
global $ttwPath;
global $ttwURL;
global $sessionTempPath;
global $nameTempDirectory;
$fullPath = $sessionTempPath . $fileName;
//Copy result to .zip archive and offer download...
if(str_contains($fullPath, ".zip")){
include_once "ttw_includes_head.php";
$zipArchive = new ZipArchive();
$zipArchive->open($fullPath, ZipArchive::OVERWRITE);
if($outputFormat == "xml"){
$zipArchive->addFile($sessionTempPath . "articleFile.html" , "articleFile_pandoc.html");
$zipArchive->addFile($sessionTempPath . "articleFile_edited_1_.xml" , "articleFile_edited_1_.xml");
}
if($outputFormat == "html"){
$zipArchive->addFile($sessionTempPath . "articleFile.html" , "articleFile_pandoc.html");
$zipArchive->addFile($sessionTempPath . "articleFile_edited_1_.html" , "articleFile_edited_1_.html");
$zipArchive->addEmptyDir('articleFile_edited_1__ress');
$zipArchive->addFile($sessionTempPath . "articleFile_edited_1__ress/colorschememapping.xml" , "articleFile_edited_1__ress/colorschememapping.xml");
$zipArchive->addFile($sessionTempPath . "articleFile_edited_1__ress/filelist.xml" , "articleFile_edited_1__ress/filelist.xml");
$zipArchive->addFile($sessionTempPath . "articleFile_edited_1__ress/header.html" , "articleFile_edited_1__ress/header.html");
$zipArchive->addFile($sessionTempPath . "articleFile_edited_1__ress/item0001.xml" , "articleFile_edited_1__ress/item0001.xml");
}
$zipArchive->close();
//URL is needed for the download attribute
$targetURL = $ttwURL . "ttw/" . $nameTempDirectory . "/" . $fileName;
?>
<h3>Get your files <a href="<?php echo $targetURL?>" download="ttw_result.zip">here</a>.</h3>
<div class="infobox">
<p><b>Tip:</b> If you want to convert another article please close browser completely and start a new browser session.</p>
</div>
</body>
</html>
<?php
}
//In case of .html show directly...
if(mime_content_type($fullPath) == "text/html"){
$articleContent = file_get_contents($fullPath);
echo $articleContent;
}
//... in case of .xml show code...
if(mime_content_type($fullPath) == "text/xml"){
$articleContent = file_get_contents($fullPath);
include_once "ttw_includes_head.php";
echo "\n<textarea style=\"background-color: rgb(237,252,237); font-family: calibri;\" cols=\"90\" rows=\"30\" name=\"actionWindow\">\n";
echo $articleContent;
echo "</textarea>\n</body>\n</html>";
}
//... in case of .csv prepare table and show
if(mime_content_type($fullPath) == "text/plain"){
include_once "ttw_includes_head.php";
$valueList = fopen($fullPath, 'r');
echo "<table class=\"show_csv\">\n";
while (!feof($valueList)) {
$conentLine = fgets($valueList);
$tableRow = "<tr><td>" . $conentLine;
$tableRow = nl2br($tableRow);
$tableRow = str_replace("<br />", "</td></tr>", $tableRow);
$tableRow = str_replace("|", "</td><td>", $tableRow);
$tableRow = str_replace("<td></td>", "<td><span class=\"soften\">-/-</span></td>", $tableRow);
echo $tableRow;
$tableRow="";
}
echo "\n</table>\n</div></body>\n</html>";
fclose($valueList);
}
}
function show_intro(){
?>
<h2><a href="ttw_index.php" style="text-decoration: none";>TagTool_WiZArD - Web Extension</a></h2>
<h3>How to use</h3>
<p>You will find here <a href="../<?php global $helpPath; echo $helpPath?>">the application´s help documentation.</a> </p>
<?php
}
?>