1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ownCloud - Documents App |
5
|
|
|
* |
6
|
|
|
* @author Victor Dubiniuk |
7
|
|
|
* @copyright 2014 Victor Dubiniuk [email protected] |
8
|
|
|
* |
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
10
|
|
|
* later. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace OCA\Documents; |
14
|
|
|
|
15
|
|
|
use OCA\Documents\AppInfo\Application; |
16
|
|
|
|
17
|
|
|
class Converter { |
18
|
|
|
|
19
|
|
|
const TEST_DOC_PATH = '/assets/test.doc'; |
20
|
|
|
|
21
|
|
|
public static function testConversion(){ |
22
|
|
|
$targetFilter = 'odt:writer8'; |
23
|
|
|
$targetExtension = 'odt'; |
24
|
|
|
$input = file_get_contents(dirname(__DIR__) . self::TEST_DOC_PATH); |
25
|
|
|
$infile = \OC::$server->getTempManager()->getTemporaryFile(); |
26
|
|
|
$outdir = \OC::$server->getTempManager()->getTemporaryFolder(); |
27
|
|
|
$outfile = $outdir . '/' . basename($infile) . '.' . $targetExtension; |
28
|
|
|
$cmd = Helper::findOpenOffice(); |
29
|
|
|
|
30
|
|
|
$params = ' --headless --convert-to ' . escapeshellarg($targetFilter) . ' --outdir ' |
31
|
|
|
. escapeshellarg($outdir) |
32
|
|
|
. ' --writer '. escapeshellarg($infile) |
33
|
|
|
. ' -env:UserInstallation=file://' |
34
|
|
|
. escapeshellarg(\OC::$server->getTempManager()->getTempBaseDir() . '/owncloud-' . \OC_Util::getInstanceId().'/') . ' 2>&1' |
35
|
|
|
; |
36
|
|
|
file_put_contents($infile, $input); |
37
|
|
|
|
38
|
|
|
$result = shell_exec($cmd . $params); |
39
|
|
|
$exists = file_exists($outfile); |
40
|
|
|
|
41
|
|
|
if (!$exists){ |
42
|
|
|
\OC::$server->getLogger()->warning( |
43
|
|
|
'Conversion test failed. Raw output:' . $result, |
44
|
|
|
['app' => 'documents'] |
45
|
|
|
|
46
|
|
|
); |
47
|
|
|
return false; |
48
|
|
|
} else { |
49
|
|
|
unlink($outfile); |
50
|
|
|
} |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function convert($input, $targetFilter, $targetExtension){ |
55
|
|
|
if (self::getAppConfig()->getAppValue('converter') === 'local'){ |
56
|
|
|
$output = self::convertLocal($input, $targetFilter, $targetExtension); |
57
|
|
|
} else { |
58
|
|
|
$output = self::convertExternal($input, $targetExtension); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (empty($output)){ |
62
|
|
|
\OC::$server->getLogger()->warning( |
63
|
|
|
'Empty conversion output', |
64
|
|
|
['app' => 'documents'] |
65
|
|
|
|
66
|
|
|
); |
67
|
|
|
throw new \RuntimeException('Empty conversion output'); |
68
|
|
|
} |
69
|
|
|
return $output; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function checkConnection(){ |
73
|
|
|
$expected = file_get_contents(dirname(__DIR__) . '/assets/response.odt'); |
74
|
|
|
$converted = self::convertExternal('', 'odt'); |
75
|
|
|
|
76
|
|
|
return $converted === $expected; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* convert via openOffice hosted on the same server |
81
|
|
|
* @param string $input |
82
|
|
|
* @param string $targetFilter |
83
|
|
|
* @param string $targetExtension |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected static function convertLocal($input, $targetFilter, $targetExtension){ |
87
|
|
|
$infile = \OC::$server->getTempManager()->getTemporaryFile(); |
88
|
|
|
$outdir = \OC::$server->getTempManager()->getTemporaryFolder(); |
89
|
|
|
$cmd = Helper::findOpenOffice(); |
90
|
|
|
$params = ' --headless --convert-to ' . $targetFilter . ' --outdir ' |
91
|
|
|
. escapeshellarg($outdir) |
92
|
|
|
. ' --writer '. escapeshellarg($infile) |
93
|
|
|
. ' -env:UserInstallation=file://' |
94
|
|
|
. escapeshellarg(\OC::$server->getTempManager()->getTempBaseDir() . '/owncloud-' . \OC_Util::getInstanceId().'/') |
95
|
|
|
; |
96
|
|
|
|
97
|
|
|
file_put_contents($infile, $input); |
98
|
|
|
shell_exec($cmd . $params); |
99
|
|
|
$output = file_get_contents($outdir . '/' . basename($infile) . '.' . $targetExtension); |
100
|
|
|
|
101
|
|
|
return $output; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* convert via format-filter-server installed on the same host with openOffice |
106
|
|
|
* @param string $input |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected static function convertExternal($input, $targetExtension){ |
110
|
|
|
$options = array( |
111
|
|
|
CURLOPT_RETURNTRANSFER => true, |
112
|
|
|
CURLOPT_HEADER => false, |
113
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
114
|
|
|
CURLOPT_ENCODING => "", |
115
|
|
|
CURLOPT_AUTOREFERER => true, |
116
|
|
|
CURLOPT_CONNECTTIMEOUT => 120, |
117
|
|
|
CURLOPT_TIMEOUT => 120, |
118
|
|
|
CURLOPT_MAXREDIRS => 2, |
119
|
|
|
CURLOPT_POST => 1, |
120
|
|
|
CURLOPT_POSTFIELDS => $input, |
121
|
|
|
CURLOPT_SSL_VERIFYHOST => 0, |
122
|
|
|
CURLOPT_SSL_VERIFYPEER => 0, |
123
|
|
|
CURLOPT_VERBOSE => 1 |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$ch = curl_init(self::getAppConfig()->getAppValue('converter_url') . '?target_format=' . $targetExtension); |
127
|
|
|
curl_setopt_array($ch, $options); |
128
|
|
|
$content = curl_exec($ch); |
129
|
|
|
if (curl_errno($ch)){ |
130
|
|
|
\OC::$server->getLogger()->debug( |
131
|
|
|
'cURL error' . curl_errno($ch) . ':' . curl_error($ch), |
132
|
|
|
['app' => 'documents'] |
133
|
|
|
|
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
curl_close($ch); |
137
|
|
|
|
138
|
|
|
return $content; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected static function getAppConfig(){ |
142
|
|
|
$app = new Application(); |
143
|
|
|
$c = $app->getContainer(); |
144
|
|
|
return $c->query('AppConfig'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|