$query = "delete FROM {$surveytable} where id={$iResponseID}"; $connect->execute($query) or safe_die("Could not delete response<br />{$dtquery}<br />" . $connect->ErrorMsg()); // checked } } else { if (isset($_POST['downloadfile']) && $_POST['downloadfile'] === 'marked') { // Now, zip all the files in the filelist $zipfilename = "Responses_for_survey_" . $surveyid . ".zip"; zipFiles($_POST['markedresponses'], $zipfilename); } } } else { if (isset($_POST['downloadfile']) && $_POST['downloadfile'] != '' && $_POST['downloadfile'] !== true) { // Now, zip all the files in the filelist $zipfilename = "LS_Responses_for_" . $_POST['downloadfile'] . ".zip"; zipFiles($_POST['downloadfile'], $zipfilename); } else { if (isset($_POST['downloadindividualfile']) && $_POST['downloadindividualfile'] != '') { $id = (int) $_POST['id']; $downloadindividualfile = $_POST['downloadindividualfile']; $fieldname = $_POST['fieldname']; $query = "SELECT " . db_quote_id($fieldname) . " FROM {$surveytable} WHERE id={$id}"; $result = db_execute_num($query); $row = $result->FetchRow(); $phparray = json_decode($row[0]); for ($i = 0; $i < count($phparray); $i++) { if ($phparray[$i]->name == $downloadindividualfile) { $file = $uploaddir . "/surveys/" . $surveyid . "/files/" . $phparray[$i]->filename; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream');
foreach ($filesInPreviewDirectory as $fileInPreviewDirectory) { if (!stringStartsWith(basename($fileInPreviewDirectory), "docvert") && !stringEndsWith(basename($fileInPreviewDirectory), "wmf") && !stringEndsWith(basename($fileInPreviewDirectory), "gif") && !stringEndsWith(basename($fileInPreviewDirectory), "png") && !stringEndsWith(basename($fileInPreviewDirectory), "jpeg") && !stringEndsWith(basename($fileInPreviewDirectory), "jpg") && !stringEndsWith(basename($fileInPreviewDirectory), "svg")) { //print 'Delete: '.$fileInPreviewDirectory.'<br />'; silentlyUnlink($fileInPreviewDirectory); } else { //print 'Retain: '.$fileInPreviewDirectory.'<br />'; } } $docbookPath = $previewDirectory . 'docvert--all-docbook.xml'; $docbook = file_get_contents($docbookPath); $docbook = str_replace('{{body}}', $docbookBody, $docbook); $docbook = str_replace('{{title}}', $docbookTitle, $docbook); $contentPath = $previewDirectory . 'content.xml'; file_put_contents($contentPath, $docbook); $pipelineToUse = $pipeline; $autoPipeline = $autopipeline; $skipAheadToDocbook = true; $pipelinePreviewDirectory = 'writable' . DIRECTORY_SEPARATOR . $documentPathParts[0]; applyPipeline($contentPath, $pipelineToUse, $autoPipeline, $pipelinePreviewDirectory, $skipAheadToDocbook); zipFiles($allDocumentsPreviewDirectory, $zipFilePath); $urlParamsArray = array('preview' => $documentPathParts[0]); //'pipeline' => $pipelineToUse, //'autopipeline' => $autopipeline $urlParams = ""; foreach ($urlParamsArray as $key => $value) { if ($urlParams) { $urlParams .= '&'; } $urlParams .= rawurlencode($key) . '=' . rawurlencode($value); } header("Location: web-service.php?" . $urlParams);
/** * parses the whole directory and archives it in small parts, each * containg $maxFiles files each. * * @param: * * $dir: Directory to archive recursively. */ function parse($dir) { global $filenames; global $fileNums; global $maxFiles; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && is_file($dir . '/' . $file)) { $filenames[] = $dir . '/' . $file; $fileNums++; if ($fileNums >= $maxFiles) { $fileNums = 0; if (!zipFiles()) { return; } $filenames = array(); } } else { if ($file != "." && $file != ".." && is_dir($dir . '/' . $file)) { parse($dir . '/' . $file); } } } closedir($handle); } }
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); }
$names = explode(',', $name); array_filter($names); foreach ($names as $name) { $dirs[] = __DIR__ . DIRECTORY_SEPARATOR . $name; } } if (empty($dirs)) { $dirs = scandir(__DIR__); } foreach ($dirs as $dir) { if ($dir != '.' && $dir != '..' && $dir != 'zips' && is_dir($dir)) { $name = basename($dir); $tmp = 'zips' . DIRECTORY_SEPARATOR . $name; recurse_copy($dir, $tmp); combineXML($tmp, $name); zipFiles($name); deldir(__DIR__ . DIRECTORY_SEPARATOR . $tmp); } } function recurse_copy($src, $dst) { $dir = opendir($src); @mkdir($dst); while (false !== ($file = readdir($dir))) { if ($file != '.' && $file != '..') { if (is_dir($src . '/' . $file)) { recurse_copy($src . '/' . $file, $dst . '/' . $file); } else { copy($src . '/' . $file, $dst . '/' . $file); } }
function generateDocument($pages, $generatorPipeline) { if (preg_match('/.\\//s', $generatorPipeline)) { webServiceError('&error-disallowed-characters;'); } $userAgent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:bignumber) Docvert'; $httpContextOptions = array('http' => array('header' => 'User-Agent: ' . $userAgent)); $httpContext = stream_context_create($httpContextOptions); $docvertDir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR; $docvertWritableDir = getWritableDirectory(); $disallowDocumentGeneration = getGlobalConfigItem('doNotAllowDocumentGeneration'); if ($disallowDocumentGeneration == 'true') { webServiceError('&document-generation-disabled;'); } $pageXml = '<c:document xmlns="http://www.w3.org/1999/xhtml" xmlns:c="container">' . "\n"; $pageTemplate = "\n\t" . '<c:page url="{{url}}" {{baseUrl}}>{{page}}</c:page>' . "\n"; $config = array('indent' => true, 'output-xhtml' => true, 'wrap' => 200); if (!class_exists('tidy')) { webServiceError('&tidy-is-not-installed;'); } $tidy = new tidy(); $baseTagPattern = "/<base[^>]*?href=([^>]*?)>/is"; foreach ($pages as $page) { if (trim($page) != '' && (stringStartsWith($page, 'http://') || stringStartsWith($page, 'https://'))) { $pageHtml = file_get_contents($page, null, $httpContext); $tidy->parseString($pageHtml, $config, 'utf8'); $tidy->cleanRepair(); $thisPage = str_replace('{{url}}', $page, $pageTemplate); $baseUrl = ''; //supporting that ugly old hack of <base> preg_match($baseTagPattern, $pageHtml, $matches); if (count($matches) > 0) { $baseUrl = 'baseUrl="' . substr($matches[1], 1, -2) . '"'; } $thisPage = str_replace('{{baseUrl}}', $baseUrl, $thisPage); $tidiedPageContents = characterEntityToNCR(removeDoctype(removeXmlComments($tidy))); $styleTagPattern = "/<style.*?<\\/style>/is"; $tidiedPageContents = preg_replace($styleTagPattern, '', $tidiedPageContents); $scriptTagPattern = "/<script.*?<\\/script>/is"; $tidiedPageContents = preg_replace($scriptTagPattern, '', $tidiedPageContents); $questionMarkPattern = "/<\\?.*?\\?>/is"; //as strangely used on news.yahoo.com $tidiedPageContents = preg_replace($questionMarkPattern, '', $tidiedPageContents); $thisPage = str_replace('{{page}}', $tidiedPageContents, $thisPage); $pageXml .= $thisPage; } } $pageXml .= '</c:document>'; $temporaryDirectory = getTemporaryDirectory(); $pipelineDirectory = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'generator-pipeline' . DIRECTORY_SEPARATOR . $generatorPipeline . DIRECTORY_SEPARATOR; $pipelinePath = $pipelineDirectory . 'pipeline.xml'; if (!file_exists($pipelinePath)) { webServiceError('&generation-pipeline-not-found; ' . revealXml($pipelinePath)); } $pipelineString = file_get_contents($pipelinePath); $pipelineString = substr($pipelineString, strpos($pipelineString, '<pipeline>') + 10); $pipelineString = substr($pipelineString, 0, strpos($pipelineString, '</pipeline>')); $pipelineStages = xmlStringToArray($pipelineString); $pipelineSettings = array("pipeline" => $generatorPipeline, "autopipeline" => $generatorPipeline); processAPipelineLevel($pipelineStages, $pageXml, $pipelineDirectory, $temporaryDirectory, $temporaryDirectory, $pipelineSettings); $openDocumentPath = $temporaryDirectory . 'output.odt'; zipFiles($temporaryDirectory, $openDocumentPath); header('Content-disposition: attachment; filename=' . basename($openDocumentPath)); header('Content-type: application/vnd.oasis.opendocument.text'); readfile($openDocumentPath); }