Blame view
sources/apps/documents/lib/converter.php
2.56 KB
|
6d9380f96
|
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 |
<?php
/**
* ownCloud - Documents App
*
* @author Victor Dubiniuk
* @copyright 2014 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Documents;
class Converter {
public static function convert($input, $targetFilter, $targetExtension){
if (Config::getConverter() == 'local'){
$output = self::convertLocal($input, $targetFilter, $targetExtension);
} else {
$output = self::convertExternal($input, $targetExtension);
}
if (empty($output)){
Helper::warnLog('Empty conversion output');
throw new \RuntimeException('Empty conversion output');
}
return $output;
}
public static function checkConnection(){
$expected = file_get_contents(__DIR__ . '/response.odt');
$converted = self::convertExternal('', 'application/vnd.oasis.opendocument.text');
return $converted === $expected;
}
/**
* convert via openOffice hosted on the same server
* @param string $input
* @param string $targetFilter
* @param string $targetExtension
* @return string
*/
protected static function convertLocal($input, $targetFilter, $targetExtension){
$infile = \OCP\Files::tmpFile();
$outdir = \OCP\Files::tmpFolder();
$cmd = Helper::findOpenOffice();
$params = ' --headless --convert-to ' . $targetFilter . ' --outdir '
. escapeshellarg($outdir)
. ' --writer '. escapeshellarg($infile)
. ' -env:UserInstallation=file://' . escapeshellarg(get_temp_dir())
;
file_put_contents($infile, $input);
shell_exec($cmd . $params);
$output = file_get_contents($outdir . '/' . basename($infile) . '.' . $targetExtension);
return $output;
}
/**
* convert via format-filter-server installed on the same host with openOffice
* @param string $input
* @return string
*/
protected static function convertExternal($input, $targetExtension){
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 2,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $input,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_VERBOSE => 1
);
$ch = curl_init(Config::getConverterUrl() . '?target_format=' . $targetExtension);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
if (curl_errno($ch)){
Helper::debugLog('cURL error' . curl_errno($ch) . ':' . curl_error($ch));
}
curl_close($ch);
return $content;
}
}
|