Example #1
0
 function __construct()
 {
     $this->converters = getConverters();
 }
Example #2
0
/**
 * creates an Oasis OpenDocument from another Suite's file format, returns path
 * @return string
 */
function makeOasisOpenDocument($inputDocumentPath, $converter, $mockConversion = false)
{
    if (!file_exists($inputDocumentPath) && !$mockConversion) {
        webServiceError('&error-non-existant-non-opendocument-file;');
    }
    $inputDocumentPath = convertPathSlashesForCurrentOperatingSystem($inputDocumentPath);
    $docvertCommandPath = DOCVERT_DIR . 'core' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR;
    $commandTemplate = null;
    $extensionlessOutputDocumentPath = null;
    $outputDocumentPath = null;
    $stdInData = null;
    if (!$mockConversion) {
        $outputPathInfo = pathinfo($inputDocumentPath);
        $extensionlessOutputDocumentPath = $outputPathInfo['dirname'] . DIRECTORY_SEPARATOR . basename($outputPathInfo['basename']);
        $outputDocumentPath = $extensionlessOutputDocumentPath . '.odt';
    }
    if ($outputDocumentPath == '.odt' && !$mockConversion) {
        webServiceError('&error-unable-to-determine-output-filename;', 500);
    }
    $commandTemplateVariable = array('elevatePermissions' => '', 'inputDocumentPath' => $inputDocumentPath, 'outputDocumentPath' => $outputDocumentPath);
    $converters = getConverters();
    $docvertDir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
    $docvertWritableDir = getWritableDirectory();
    if (!$converter) {
        $defaultConverter = '';
        $numberOfConvertersThatAreDisallowed = 0;
        foreach ($converters as $converterId => $converterName) {
            $doNotUseConverter = getGlobalConfigItem('doNotUseConverter' . $converterId);
            if ($doNotUseConverter == 'true') {
                $numberOfConvertersThatAreDisallowed++;
            } else {
                $defaultConverter = $converterId;
            }
        }
        if ($numberOfConvertersThatAreDisallowed + 1 == count($converters)) {
            $converter = $defaultConverter;
            // There's only one choice, so don't bother asking the user
        }
    }
    $doNotUseConverter = getGlobalConfigItem('doNotUseConverter' . $converter);
    if ($doNotUseConverter == 'true' || !array_key_exists($converter, $converters)) {
        webServiceError('&error-disabled-converter;', 500, array('converter' => $converter));
    }
    $operatingSystemFamily = getOperatingSystemFamily();
    switch ($converter) {
        case 'openofficeorg':
            if (!$commandTemplate) {
                $commandTemplate = '{elevatePermissions} {scriptPath} {useXVFB} {macrosDocumentPath} {inputDocumentUrl} {outputDocumentUrl}';
            }
            $commandTemplateVariable['macrosDocumentPath'] = $docvertCommandPath . 'trusted-macros' . DIRECTORY_SEPARATOR . 'macros.odt';
            $commandTemplateVariable['inputDocumentUrl'] = null;
            $commandTemplateVariable['outputDocumentUrl'] = null;
            $commandTemplateVariable['useXVFB'] = 'false';
            if (!$mockConversion) {
                $commandTemplateVariable['inputDocumentUrl'] = convertLocalPathToOpenOfficeOrgUrl($commandTemplateVariable['inputDocumentPath']);
                $commandTemplateVariable['outputDocumentUrl'] = convertLocalPathToOpenOfficeOrgUrl($commandTemplateVariable['outputDocumentPath']);
            }
            if ($operatingSystemFamily == 'Windows') {
                if (!isset($commandTemplateVariable['scriptPath'])) {
                    $commandTemplateVariable['scriptPath'] = $docvertCommandPath . 'windows-specific' . DIRECTORY_SEPARATOR . 'convert-using-openoffice.org.bat';
                }
            } elseif ($operatingSystemFamily == 'Unix') {
                $disallowXVFB = getGlobalConfigItem('disallowXVFB');
                $commandTemplateVariable['elevatePermissions'] = 'sudo';
                $customUser = getGlobalConfigItem('runExternalApplicationAsUser');
                if ($customUser !== null && $customUser != '' && $customUser != 'root') {
                    $commandTemplateVariable['elevatePermissions'] .= ' -u ' . $customUser;
                }
                if ($disallowXVFB == 'true' || $mockConversion) {
                    $commandTemplateVariable['useXVFB'] = 'true';
                }
                if (!isset($commandTemplateVariable['scriptPath'])) {
                    $commandTemplateVariable['scriptPath'] = $docvertCommandPath . 'unix-specific' . DIRECTORY_SEPARATOR . 'convert-using-openoffice.org.sh';
                }
            }
            break;
        case 'abiword':
            if ($operatingSystemFamily == 'Windows') {
                $commandTemplate = '{elevatePermissions} {scriptPath} {inputDocumentPath}';
                $commandTemplateVariable['inputDocumentPath'] = $commandTemplateVariable['inputDocumentPath'];
                $commandTemplateVariable['scriptPath'] = $docvertCommandPath . 'windows-specific' . DIRECTORY_SEPARATOR . 'convert-using-abiword.bat';
            } elseif ($operatingSystemFamily == 'Unix') {
                $commandTemplate = '{elevatePermissions} {scriptPath} --stream';
                $customUser = getGlobalConfigItem('runExternalApplicationAsUser');
                $superUserPreference = getGlobalConfigItem('superUserPreference');
                if ($superUserPreference == null || $superUserPreference == 'nothing') {
                    $superUserPreference = '';
                } elseif ($superUserPreference == 'sudo') {
                    $commandTemplateVariable['elevatePermissions'] = 'sudo';
                    if ($customUser !== null && $customUser != '' && $customUser != 'root') {
                        $commandTemplateVariable['elevatePermissions'] .= ' -u ' . $customUser;
                    }
                }
                $commandTemplateVariable['scriptPath'] = $docvertCommandPath . 'unix-specific' . DIRECTORY_SEPARATOR . 'convert-using-abiword.py';
                $stdInData = file_get_contents($commandTemplateVariable['inputDocumentPath']);
            }
            break;
        case 'jodconverter':
            $commandTemplate = '{elevatePermissions} java -jar {jodConverterJar} {inputDocumentPath} {outputDocumentPath}';
            $commandTemplateVariable['conversionDirectory'] = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'jodconverter';
            $commandTemplateVariable['jodConverterJar'] = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'jodconverter' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'jodconverter-2.1.1.jar';
            if (!file_exists($commandTemplateVariable['jodConverterJar'])) {
                webServiceError('&error-jodconverter-not-found;', 500, array('jodConverterPath' => $commandTemplateVariable['jodConverterJar']));
            }
            $commandTemplateVariable['inputDocumentUrl'] = convertLocalPathToOpenOfficeOrgUrl($commandTemplateVariable['inputDocumentPath']);
            $commandTemplateVariable['outputDocumentUrl'] = convertLocalPathToOpenOfficeOrgUrl($commandTemplateVariable['outputDocumentPath']);
            break;
        case 'pyodconverter':
            $commandTemplate = '{elevatePermissions} {pyodConverterPath} --stream';
            $commandTemplateVariable['pyodConverterPath'] = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'pyodconverter' . DIRECTORY_SEPARATOR . 'pyodconverter.py';
            if (!file_exists($commandTemplateVariable['pyodConverterPath'])) {
                webServiceError('&error-jodconverter-not-found;', 500, array('pyodConverterPath' => $commandTemplateVariable['pyodConverterPath']));
            }
            $stdInData = file_get_contents($commandTemplateVariable['inputDocumentPath']);
            break;
        default:
            $additionalError = '';
            if ($converter) {
                $additionalError = 'Was "' . revealXml($converter) . '"';
            } else {
                $additionalError = ' Was empty.';
            }
            webServiceError('&error-converter-not-chosen;', 500, array('additionalError' => $additionalError));
            break;
    }
    if (isset($commandTemplateVariable['scriptPath']) && !file_exists($commandTemplateVariable['scriptPath'])) {
        webServiceError('&error-script-path-does-not-exist-at;', 500, array('scriptPath' => $commandTemplateVariable['scriptPath']));
    }
    $command = $commandTemplate;
    foreach ($commandTemplateVariable as $key => $value) {
        $replaceValue = $value;
        if ($replaceValue) {
            if ($operatingSystemFamily == 'Windows') {
                $replaceValue = '"' . $replaceValue . '"';
            } elseif ($operatingSystemFamily == 'Unix') {
                $replaceValue = escapeshellcmd($replaceValue);
            }
        }
        $command = str_replace('{' . $key . '}', $replaceValue, $command);
    }
    if (!$stdInData) {
        $output = shellCommand($command);
    } else {
        $response = shellCommand($command, null, $stdInData, false);
        $theMagicNumbersOfAZipFile = array(80, 75);
        // ASCII code of letters "PK": the magic numbers of a zip file, see: http://en.wikipedia.org/wiki/Magic_number_(programming)
        if (ord(substr($response['stdOut'], 0, 1)) == $theMagicNumbersOfAZipFile[0] && ord(substr($response['stdOut'], 1, 1)) == $theMagicNumbersOfAZipFile[1]) {
            file_put_contents($commandTemplateVariable['outputDocumentPath'], $response['stdOut']);
            $output = $response['stdErr'];
        } else {
            $output = $response['stdOut'] . $response['stdErr'];
        }
    }
    if (!file_exists($commandTemplateVariable['outputDocumentPath']) && !$mockConversion) {
        $errorMessage = '&error-unable-to-generate-opendocument;';
        $suggestedFixes = suggestFixesToCommandLineErrorMessage($output, $commandTemplateVariable, true);
        $errorMessage .= $suggestedFixes;
        $notExecutable = '';
        if (isset($commandTemplateVariable['scriptPath']) && !is_executable($commandTemplateVariable['scriptPath'])) {
            if ($operatingSystemFamily == 'Windows') {
                $notExecutable = '&error-conversion-script-windows-not-executable;';
            } elseif ($operatingSystemFamily == 'Unix') {
                $notExecutable = '&error-conversion-script-unix-not-executable;';
            }
        }
        webServiceError($errorMessage, 500, array('commandToRun' => $command, 'responseToCommand' => $output, 'notExecutable' => $notExecutable));
    } else {
        $output = trim($output);
        if ($output) {
            $output = $converter . ': ' . $output;
            silentlyAppendLineToLog($output, 'warning');
        }
    }
    if (!$mockConversion) {
        return $commandTemplateVariable['outputDocumentPath'];
    } else {
        return $output;
    }
}