function setupOpenOfficeOrg() { if (!$this->allowedAdminAccess) { return; } $hideOpenOfficeOrgOption = getGlobalConfigItem('hideAdminOptionOpenOfficeOrg'); if ($hideOpenOfficeOrgOption == "true") { return; } $numberOfOpenOfficeBasedConvertersFound = 0; $openOfficeBasedConverters = array('openofficeorg', 'jodconverter', 'pyodconverter'); foreach ($this->converters as $converterId => $converterName) { if (!in_array($converterId, $openOfficeBasedConverters)) { continue; } $hideConverter = getGlobalConfigItem('hideAdminOption' . $converterId); if ($hideConverter == 'true') { $numberOfOpenOfficeBasedConvertersFound++; } } if ($numberOfOpenOfficeBasedConvertersFound == count($openOfficeBasedConverters)) { return; } $docvertDir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR; $docvertWritableDir = getWritableDirectory(); $template = $this->getThemeFragment('admin-setupopenofficeorg.htmlf'); $toggleStatus = ''; include_once dirname(__FILE__) . '/config.php'; if (DIRECTORY_SEPARATOR == '/') { $disallowXVFB = getGlobalConfigItem('disallowXVFB'); if (isset($_POST['startOpenOfficeOrgServerLinux'])) { $shellCommandTemplate = '{{elevate-privledges}} {{bash-script}} {{xvfb}}'; $xvfbCommand = ''; $elevatePrivledges = ''; if ($disallowXVFB) { $xvfbCommand = 'true'; } $shellCommandTemplate = str_replace('{{xvfb}}', $xvfbCommand, $shellCommandTemplate); $shellCommandTemplate = str_replace('{{elevate-privledges}}', $elevatePrivledges, $shellCommandTemplate); $output = shellCommand($shellCommandTemplate, 3); include_once dirname(__FILE__) . '/lib.php'; $diagnostics = suggestFixesToCommandLineErrorMessage($output, array(), false); if ($diagnostics) { $diagnostics .= '<div style="background:#ffff99;border: solid 1px #ffff99;"><h1 style="font-size:small;padding-left:1%;color:red">Diagnostics</h1> <p>There were problems opening up JODConverter OpenOffice.org Server</p><p>I ran this command,</p><blockquote><tt>' . $shellCommandTemplate . '</tt></blockquote><p>But I don\'t think I was able to start OpenOffice.org because the script returned.</p><blockquote><tt>' . $output . '</tt></blockquote>' . $diagnostics . '</div>'; $toggleStatus = $diagnostics . $toggleStatus; } } } $template = str_replace('{{toggle}}', $toggleStatus, $template); return $template; }
function wmfOrEmfToPdf($imagePath, &$currentXml) { //Step 1. Detect width/height of image. $imageName = basename($imagePath); $imageOffset = strpos($currentXml, $imageName); if ($imageOffset === False) { return False; } //image not in document, don't worry about it. //header('Content-type: text/xml'); die($currentXml); $dom = simplexml_load_string($currentXml); $xpath = "//*[@xlink:href='" . $imageName . "']//parent::draw:frame"; $imageMatch = $dom->xpath($xpath); if (count($imageMatch) == 0) { webServiceError('&error-process-convertimages-no-dom;', 500, array('xpath' => $xpath)); } $imageMatch = $imageMatch[0]; $attributes = $imageMatch->attributes('svg', true); $width = (string) $attributes['width']; $height = (string) $attributes['height']; //Step 2. Make an ODT file containing only the WMF/EMF // (ugh.. I know, but it works and it's reliable because we benefit from OpenOffice's years of // reverse-engineering the EMF/WMF formats so really we should get over it) //step 2a -- make a working directory for our OpenDocument file and copy the files in $workingDirectory = getTemporaryDirectoryInsideDirectory($this->contentDirectory); mkdir($workingDirectory . DIRECTORY_SEPARATOR . 'Pictures'); $destinationImagePath = $workingDirectory . DIRECTORY_SEPARATOR . 'Pictures' . DIRECTORY_SEPARATOR . basename($imagePath); copy($imagePath, $destinationImagePath); $odtTemplateDirectory = DOCVERT_DIR . 'core' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; $stylesXml = file_get_contents($odtTemplateDirectory . 'styles.xml'); $stylesXml = str_replace('{{page-width}}', $width, $stylesXml); $stylesXml = str_replace('{{page-height}}', $height, $stylesXml); file_put_contents($workingDirectory . DIRECTORY_SEPARATOR . 'styles.xml', $stylesXml); copy($odtTemplateDirectory . 'settings.xml', $workingDirectory . DIRECTORY_SEPARATOR . 'settings.xml'); copy($odtTemplateDirectory . 'meta.xml', $workingDirectory . DIRECTORY_SEPARATOR . 'meta.xml'); copy($odtTemplateDirectory . 'manifest.rdf', $workingDirectory . DIRECTORY_SEPARATOR . 'manifest.rdf'); copy($odtTemplateDirectory . 'mimetype', $workingDirectory . DIRECTORY_SEPARATOR . 'mimetype'); $contentXml = file_get_contents($odtTemplateDirectory . 'content.xml'); $imageTemplate = '<text:p><draw:frame text:anchor-type="as-char" svg:width="{{width}}" svg:height="{{height}}" draw:z-index="1"><draw:image xlink:href="{{path}}" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/></draw:frame></text:p>'; $imageString = str_replace('{{width}}', $width, $imageTemplate); $imageString = str_replace('{{height}}', $height, $imageString); $imageString = str_replace('{{path}}', 'Pictures/' . basename($destinationImagePath), $imageString); $contentXml = str_replace('<!--{{content}}-->', $imageString, $contentXml); file_put_contents($workingDirectory . DIRECTORY_SEPARATOR . 'content.xml', $contentXml); mkdir($workingDirectory . DIRECTORY_SEPARATOR . 'META-INF'); $manifestXml = file_get_contents($odtTemplateDirectory . 'manifest.xml'); $manifestItemTemplate = ' <manifest:file-entry manifest:media-type="" manifest:full-path="{{path}}"/>'; $manifestItem = str_replace('{{path}}', 'Pictures/' . basename($imagePath), $manifestItemTemplate); $manifestXml = str_replace('<!--{{content}}-->', $manifestItem, $manifestXml); file_put_contents($workingDirectory . DIRECTORY_SEPARATOR . 'META-INF' . DIRECTORY_SEPARATOR . 'manifest.xml', $manifestXml); //step 2b zip it into an ODT $zipPath = $this->contentDirectory . DIRECTORY_SEPARATOR . basename($imagePath) . '.odt'; $zipPath = zipFiles($workingDirectory, $zipPath); $zipData = file_get_contents($zipPath); silentlyUnlink($zipPath); silentlyUnlink($workingDirectory); //Step 3 . Stream it to PyODConverter. Make a PDF and save it. $pyodConverterPath = DOCVERT_DIR . 'core' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'pyodconverter' . DIRECTORY_SEPARATOR . 'pyodconverter.py'; if (!file_exists($pyodConverterPath)) { die("Can't find PyODconverter at " . htmlentities($pyodConverterPath)); } $command = $pyodConverterPath . ' --stream --pdf'; $response = shellCommand($command, 20, $zipData, false); $pdfMagicBytes = '%PDF'; if (substr($response['stdOut'], 0, strlen($pdfMagicBytes)) != $pdfMagicBytes) { die("Expected a PDF response was didn't receive one. Received back " . htmlentities(print_r($response, true))); } $imagePathInfo = pathinfo($imagePath); $pdfPath = dirname($imagePath) . DIRECTORY_SEPARATOR . basename($imagePath, '.' . $imagePathInfo['extension']) . '.pdf'; file_put_contents($pdfPath, $response['stdOut']); return array('width' => $width, 'height' => $height, 'path' => $pdfPath); }
function deleteDirectoryRecursively($directoryPath) { $directoryPath = convertPathSlashesForCurrentOperatingSystem($directoryPath); if ($files = glob($directoryPath . DIRECTORY_SEPARATOR . '*')) { foreach ($files as $file) { if (is_dir($file)) { deleteDirectoryRecursively($file); } else { silentlyUnlink($file); } } } $operatingSystemFamily = getOperatingSystemFamily(); $detailedError = ''; if ($operatingSystemFamily == 'Windows') { $rmdirCommand = 'rmdir /s /q ' . $directoryPath; $response = shellCommand($rmdirCommand); $response = trim($response); //file_put_contents('c:\\results.txt', "\r\n".$response."\r\n", FILE_APPEND); if (stripos($response, 'process cannot access the file') !== false) { $detailedError .= '. Reason: ' . $response; } else { $detailedError .= '. Running command "' . $rmdirCommand . '" ' . $response; } } if (@(!rmdir($directoryPath))) { $detailedError = null; $contentsOfDirectory = implode(glob($directoryPath . DIRECTORY_SEPARATOR . '*'), ', '); $contentsOfDirectory = substr($contentsOfDirectory, 0, strlen($contentsOfDirectory) - 1); if (trim($contentsOfDirectory)) { $detailedError .= 'Directory contains ' . $contentsOfDirectory . '. '; } if (file_exists($directoryPath)) { silentlyAppendLineToLog('Unable to delete directory ' . $directoryPath . $detailedError, 'error'); } } }