-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc_parser.php
More file actions
66 lines (52 loc) · 1.33 KB
/
doc_parser.php
File metadata and controls
66 lines (52 loc) · 1.33 KB
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
<?php
/**
* DocParser script (server)
*
* Parses docs using LibreOffice command
*
* @author Jhalak <jhalak1983@gmail.com>
*
*/
$error = false;
// assert API Key
if (empty($_POST['api_key'])
|| $_POST['api_key'] != 'e4a2ed88c280c425a1e8add7c0ac5f171be9c4b2ecfe5a881d71e64d14361ac8'
) {
$error = true;
}
// assert file content
if (empty($_POST['file_content'])) {
$error = true;
}
// Something going fishy??
if ($error) {
echo 'Better luck next time buddy !!';
exit;
}
$filename = $_POST['filename'];
$outdir = dirname(__FILE__) . '/files/';
$doc_filepath = $outdir . $filename;
$pathinfo = pathinfo($doc_filepath);
$html_filepath = $outdir . $pathinfo['filename'] . '.html';
// write the doc data locally into a file
file_put_contents($doc_filepath, $_POST['file_content']);
try{
// execute shell command to convert the file
shell_exec('/usr/bin/unoconv -f html ' . $doc_filepath);
// if html is created then get the content
if (stat($html_filepath)) {
$html = file_get_contents($html_filepath);
// say bye bye to html file
unlink($html_filepath);
// We got it!! send it, hurry!!
echo $html;
} else{
throw new Exception('Problem in converting doc to HTML');
}
}catch (Exception $e) {
echo $e->getMessage();
}
// say bye bye to doc file
if (stat($doc_filepath)) {
unlink($doc_filepath);
}