/** * get Content of PDF file * @param string $file * @return string The extracted content of the file */ public function getContent($file) { $this->fileInfo = GeneralUtility::makeInstance('tx_kesearch_lib_fileinfo'); $this->fileInfo->setFile($file); // get PDF informations if (!($pdfInfo = $this->getPdfInfo($file))) { return false; } // proceed only of there are any pages found if ((int) $pdfInfo['pages'] && $this->isAppArraySet) { // create the tempfile which will contain the content $tempFileName = GeneralUtility::tempnam('pdf_files-Indexer'); // Delete if exists, just to be safe. @unlink($tempFileName); // generate and execute the pdftotext commandline tool $fileEscaped = CommandUtility::escapeShellArgument($file); $cmd = "{$this->app['pdftotext']} -enc UTF-8 -q {$fileEscaped} {$tempFileName}"; CommandUtility::exec($cmd); // check if the tempFile was successfully created if (@is_file($tempFileName)) { $content = GeneralUtility::getUrl($tempFileName); unlink($tempFileName); } else { $this->addError('Content for file ' . $file . ' could not be extracted. Maybe it is encrypted?'); // return empty string if no content was found $content = ''; } return $this->removeEndJunk($content); } else { return false; } }
/** * This method actually does the processing of files locally * * takes the original file (on remote storages this will be fetched from the remote server) * does the IM magic on the local server by creating a temporary typo3temp/ file * copies the typo3temp/ file to the processing folder of the target storage * removes the typo3temp/ file * * @param TaskInterface $task * @return array */ public function process(TaskInterface $task) { $targetFile = $task->getTargetFile(); // Merge custom configuration with default configuration $configuration = array_merge(array('width' => 64, 'height' => 64), $task->getConfiguration()); $configuration['width'] = Utility\MathUtility::forceIntegerInRange($configuration['width'], 1, 1000); $configuration['height'] = Utility\MathUtility::forceIntegerInRange($configuration['height'], 1, 1000); $originalFileName = $targetFile->getOriginalFile()->getForLocalProcessing(FALSE); // Create a temporary file in typo3temp/ if ($targetFile->getOriginalFile()->getExtension() === 'jpg') { $targetFileExtension = '.jpg'; } else { $targetFileExtension = '.png'; } // Create the thumb filename in typo3temp/preview_....jpg $temporaryFileName = Utility\GeneralUtility::tempnam('preview_') . $targetFileExtension; // Check file extension if ($targetFile->getOriginalFile()->getType() != Resource\File::FILETYPE_IMAGE && !Utility\GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $targetFile->getOriginalFile()->getExtension())) { // Create a default image $this->processor->getTemporaryImageWithText($temporaryFileName, 'Not imagefile!', 'No ext!', $targetFile->getOriginalFile()->getName()); } else { // Create the temporary file if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) { $parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' ' . $this->processor->wrapFileName($originalFileName) . '[0] ' . $this->processor->wrapFileName($temporaryFileName); $cmd = Utility\GeneralUtility::imageMagickCommand('convert', $parameters) . ' 2>&1'; Utility\CommandUtility::exec($cmd); if (!file_exists($temporaryFileName)) { // Create a error gif $this->processor->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', $targetFile->getOriginalFile()->getName()); } } } return array('filePath' => $temporaryFileName); }
public static function createPublicTempFile($prefix, $suffix) { $fullname = GeneralUtility::tempnam($prefix, $suffix); $urlname = preg_replace('/.*(?=\\/typo3temp)/', '', $fullname); $url = GeneralUtility::locationHeaderUrl($urlname); return ['name' => $fullname, 'url' => $url]; }
/** * @param string $fileref */ public function download($fileref) { $extensionConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['sharepoint_connector']); $extensionConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::removeDotsFromTS($extensionConfiguration); $filePath = explode(';#', $fileref); $urlInfo = parse_url($extensionConfiguration['sharepointServer']['url']); $downloadLink = $urlInfo['scheme'] . '://' . $urlInfo['host'] . '/' . $filePath[1]; $destinationFile = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('spdownload_'); $fp = fopen($destinationFile, 'w+'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $downloadLink); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); curl_setopt($ch, CURLOPT_USERPWD, $extensionConfiguration['sharepointServer']['username'] . ':' . $extensionConfiguration['sharepointServer']['password']); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); # increase timeout to download big file curl_setopt($ch, CURLOPT_FILE, $fp); curl_exec($ch); curl_close($ch); fclose($fp); if (file_exists($destinationFile)) { header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=" . basename($downloadLink)); header("Content-Transfer-Encoding: binary"); header('Content-Length: ' . filesize($destinationFile)); ob_clean(); flush(); readfile($destinationFile); } \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($destinationFile); }
/** * Set up * * @return void */ protected function setUp() { $this->subject = new \TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider(); $this->icon = GeneralUtility::makeInstance(Icon::class); $this->icon->setIdentifier('foo'); $this->icon->setSize(Icon::SIZE_SMALL); $svgTestFileContent = '<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill="#CD201F" d="M11 12l3-2v6H2v-6l3 2 3-2 3 2z"></path><script><![CDATA[ function alertMe() {} ]]></script></svg>'; $this->testFileName = GeneralUtility::tempnam(uniqid('svg_') . '.svg'); file_put_contents($this->testFileName, $svgTestFileContent); }
/** * Creates a fake extension with a given table definition. * * @param string $tableDefinition SQL script to create the extension's tables * @throws \RuntimeException * @return void */ protected function createFakeExtension($tableDefinition) { // Prepare a fake extension configuration $ext_tables = GeneralUtility::tempnam('ext_tables'); if (!GeneralUtility::writeFile($ext_tables, $tableDefinition)) { throw new \RuntimeException('Can\'t write temporary ext_tables file.'); } $this->temporaryFiles[] = $ext_tables; $GLOBALS['TYPO3_LOADED_EXT'] = array('test_dbal' => array('ext_tables.sql' => $ext_tables)); // Append our test table to the list of existing tables $this->subject->initialize(); }
/** * Test if the local filesystem is case sensitive * * @return boolean */ protected function isCaseSensitiveFilesystem() { $caseSensitive = TRUE; $path = GeneralUtility::tempnam('aAbB'); // do the actual sensitivity check if (@file_exists(strtoupper($path)) && @file_exists(strtolower($path))) { $caseSensitive = FALSE; } // clean filesystem unlink($path); return $caseSensitive; }
/** * @param TaskInterface $task * @return array|null */ protected function process(TaskInterface $task) { $result = NULL; $sourceFile = $task->getOriginalProcessedFile() ?: $task->getSourceFile(); $sourceFilePath = realpath($sourceFile->getForLocalProcessing(FALSE)); $targetFilePath = realpath(GeneralUtility::tempnam('_processed_/nlx-tempfile-', '.' . $sourceFile->getExtension())); switch ($sourceFile->getExtension()) { case 'jpg': case 'jpeg': if ($this->settings['jpg.']['enabled'] === FALSE) { return $result; } $library = 'jpegtran'; $arguments = sprintf('-copy none -optimize %s -outfile %s %s', $this->settings['jpg.']['progressive'] === TRUE ? '-progressive' : '', escapeshellarg($targetFilePath), escapeshellarg($sourceFilePath)); break; case 'png': if ($this->settings['png.']['enabled'] === FALSE) { return $result; } $library = 'optipng'; $arguments = sprintf('-o%u -strip all -fix -clobber -force -out %s %s', $this->settings['png.']['optimizationLevel'], escapeshellarg($targetFilePath), escapeshellarg($sourceFilePath)); break; case 'gif': if ($this->settings['gif.']['enabled'] === FALSE) { return $result; } $library = 'gifsicle'; $arguments = sprintf('--batch -O%u -o %s %s', $this->settings['gif.']['optimizationLevel'], escapeshellarg($targetFilePath), escapeshellarg($sourceFilePath)); break; case 'svg': if ($this->settings['svg.']['enabled'] === FALSE) { return $result; } $library = 'svgo'; $arguments = sprintf('%s %s', escapeshellarg($sourceFilePath), escapeshellarg($targetFilePath)); break; default: return $result; } $cmd = escapeshellcmd($library) . ' ' . $arguments; $output = []; exec($cmd, $output, $return); if ($return === 0) { $result = array('filePath' => $targetFilePath); } return $result; }
/** * get Content of DOC file * @param string $file * @return string The extracted content of the file */ public function getContent($file) { // create the tempfile which will contain the content $tempFileName = GeneralUtility::tempnam('xls_files-Indexer'); // Delete if exists, just to be safe. @unlink($tempFileName); // generate and execute the pdftotext commandline tool $fileEscaped = CommandUtility::escapeShellArgument($file); $cmd = "{$this->app['xls2csv']} -c ' ' -q 0 -s8859-1 -dutf-8 {$fileEscaped} > {$tempFileName}"; CommandUtility::exec($cmd); // check if the tempFile was successfully created if (@is_file($tempFileName)) { $content = GeneralUtility::getUrl($tempFileName); unlink($tempFileName); } else { return false; } // check if content was found if (strlen($content)) { return $content; } else { return false; } }
/** * Create a temporary file. * * @param string $filePrefix File prefix. * @return string|boolean File name or FALSE * @todo Define visibility */ public function tempFile($filePrefix) { $absFile = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam($filePrefix); if ($absFile) { $ret = $absFile; $this->registerTempFile($absFile); } else { $ret = FALSE; $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.'); } return $ret; }
/** * Handler for the content of a tag */ public function spellCheckHandler($xml_parser, $string) { $incurrent = array(); $stringText = $string; $words = preg_split($this->parserCharset == 'utf-8' ? '/\\P{L}+/u' : '/\\W+/', $stringText); foreach ($words as $word) { $word = preg_replace('/ /' . ($this->parserCharset == 'utf-8' ? 'u' : ''), '', $word); if ($word && !is_numeric($word)) { if ($this->pspell_is_available && !$this->forceCommandMode) { if (!pspell_check($this->pspell_link, $word)) { if (!in_array($word, $this->misspelled)) { if (sizeof($this->misspelled) != 0) { $this->suggestedWords .= ','; } $suggest = array(); $suggest = pspell_suggest($this->pspell_link, $word); if (sizeof($suggest) != 0) { $this->suggestionCount++; $this->suggestedWordCount += sizeof($suggest); } $this->suggestedWords .= '"' . $word . '":"' . implode(',', $suggest) . '"'; $this->misspelled[] = $word; unset($suggest); } if (!in_array($word, $incurrent)) { $stringText = preg_replace('/\\b' . $word . '\\b/' . ($this->parserCharset == 'utf-8' ? 'u' : ''), '<span class="htmlarea-spellcheck-error">' . $word . '</span>', $stringText); $incurrent[] = $word; } } } else { $tmpFileName = GeneralUtility::tempnam($this->filePrefix); if (!($filehandle = fopen($tmpFileName, 'wb'))) { echo 'SpellChecker tempfile open error'; } if (!fwrite($filehandle, $word)) { echo 'SpellChecker tempfile write error'; } if (!fclose($filehandle)) { echo 'SpellChecker tempfile close error'; } $catCommand = TYPO3_OS === 'WIN' ? 'type' : 'cat'; $AspellCommand = $catCommand . ' ' . escapeshellarg($tmpFileName) . ' | ' . $this->AspellDirectory . ' -a check' . ' --mode=none' . ' --sug-mode=' . escapeshellarg($this->pspellMode) . ($this->personalDictionaryPath ? ' --home-dir=' . escapeshellarg($this->personalDictionaryPath) : '') . ' --lang=' . escapeshellarg($this->dictionary) . ' --encoding=' . escapeshellarg($this->aspellEncoding) . ' 2>&1'; $AspellAnswer = shell_exec($AspellCommand); $AspellResultLines = array(); $AspellResultLines = GeneralUtility::trimExplode(LF, $AspellAnswer, TRUE); if (substr($AspellResultLines[0], 0, 6) == 'Error:') { echo '{' . $AspellAnswer . '}'; } GeneralUtility::unlink_tempfile($tmpFileName); if ($AspellResultLines['1'][0] !== '*') { if (!in_array($word, $this->misspelled)) { if (sizeof($this->misspelled) != 0) { $this->suggestedWords .= ','; } $suggest = array(); $suggestions = array(); if ($AspellResultLines['1'][0] === '&') { $suggestions = GeneralUtility::trimExplode(':', $AspellResultLines['1'], TRUE); $suggest = GeneralUtility::trimExplode(',', $suggestions['1'], TRUE); } if (sizeof($suggest) != 0) { $this->suggestionCount++; $this->suggestedWordCount += sizeof($suggest); } $this->suggestedWords .= '"' . $word . '":"' . implode(',', $suggest) . '"'; $this->misspelled[] = $word; unset($suggest); unset($suggestions); } if (!in_array($word, $incurrent)) { $stringText = preg_replace('/\\b' . $word . '\\b/' . ($this->parserCharset == 'utf-8' ? 'u' : ''), '<span class="htmlarea-spellcheck-error">' . $word . '</span>', $stringText); $incurrent[] = $word; } } unset($AspellResultLines); } $this->wordCount++; } } $this->text .= $stringText; unset($incurrent); }
/** * @test */ public function templateWithErrorReturnsFormWithErrorReporter() { $badSource = '<f:layout invalid="TRUE" />'; $temp = GeneralUtility::tempnam('badtemplate') . '.html'; GeneralUtility::writeFileToTypo3tempDir($temp, $badSource); $form = $this->createFluxServiceInstance()->getFormFromTemplateFile($temp); $this->assertInstanceOf('FluidTYPO3\\Flux\\Form', $form); $this->assertInstanceOf('FluidTYPO3\\Flux\\Form\\Field\\UserFunction', reset($form->getFields())); $this->assertEquals('FluidTYPO3\\Flux\\UserFunction\\ErrorReporter->renderField', reset($form->getFields())->getFunction()); }
/** * @test */ public function tempnamReturnsAbsolutePathInsideDocumentRoot() { $filePath = GeneralUtility::tempnam('foo'); $this->assertStringStartsWith(PATH_site, $filePath); }
/** * Returns a temporary path for a given file, including the file extension. * * @param \TYPO3\CMS\Core\Resource\FileInterface $file * @return string */ protected function getTemporaryPathForFile(\TYPO3\CMS\Core\Resource\FileInterface $file) { return \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('fal-tempfile-') . '.' . $file->getExtension(); }
/** * @param array $data * @param ImportSource $importSource * @return NULL|array */ protected function processMedia(array $data, ImportSource $importSource) { $media = NULL; if (empty($data['image']) && $importSource->getDefaultImage()) { return array(array('type' => 0, 'image' => $importSource->getDefaultImage()->getOriginalResource()->getCombinedIdentifier(), 'showinpreview' => 1)); } $folder = NULL; if ($importSource->getImageFolder()) { try { $folder = ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier(ltrim($importSource->getImageFolder(), 'file:')); } catch (\Exception $e) { } } if (!empty($data['image']) && $folder) { $media = array(); if (!is_array($data['image'])) { $data['image'] = array($data['image']); } foreach ($data['image'] as $image) { $tmp = GeneralUtility::getUrl($image); if ($tmp) { $tempFile = GeneralUtility::tempnam('news_importer'); file_put_contents($tempFile, $tmp); list(, , $imageType) = getimagesize($tempFile); try { $falImage = $folder->addFile($tempFile, ($data['title'] ?: 'news_import') . image_type_to_extension($imageType, TRUE), 'changeName'); $media[] = array('type' => 0, 'image' => $falImage->getCombinedIdentifier(), 'showinpreview' => 1); } catch (\Exception $e) { } } } } return $media; }
/** * Sets the contents of a file to the specified value. * * @param string $fileIdentifier * @param string $contents * @return int The number of bytes written to the file */ public function setFileContents($fileIdentifier, $contents) { $localFilePath = GeneralUtility::tempnam('fal_dropbox'); $bytes = file_put_contents($localFilePath, $contents); $handle = fopen($localFilePath, 'rb'); $this->dropboxClient->uploadFile($fileIdentifier, WriteMode::force(), $handle); // $handle was fclosed by uploadFile() unlink($localFilePath); $this->cache->flush(); return $bytes; }
/** * Returns the path to a temporary file for processing * * @param TaskInterface $task * @return string */ protected function getTemporaryFilePath(TaskInterface $task) { return GeneralUtility::tempnam('preview_', '.' . $task->getTargetFileExtension()); }
/** * Export part of module * Setting content in $this->content * * @param array $inData Content of POST VAR tx_impexp[].. * @throws \InvalidArgumentException * @throws \RuntimeException * @throws \TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException * @return void */ public function exportData($inData) { // BUILDING EXPORT DATA: // Processing of InData array values: $inData['pagetree']['maxNumber'] = MathUtility::forceIntegerInRange($inData['pagetree']['maxNumber'], 1, 1000000, 100); $inData['listCfg']['maxNumber'] = MathUtility::forceIntegerInRange($inData['listCfg']['maxNumber'], 1, 1000000, 100); $inData['maxFileSize'] = MathUtility::forceIntegerInRange($inData['maxFileSize'], 1, 1000000, 1000); $inData['filename'] = trim(preg_replace('/[^[:alnum:]._-]*/', '', preg_replace('/\\.(t3d|xml)$/', '', $inData['filename']))); if (strlen($inData['filename'])) { $inData['filename'] .= $inData['filetype'] == 'xml' ? '.xml' : '.t3d'; } // Set exclude fields in export object: if (!is_array($inData['exclude'])) { $inData['exclude'] = array(); } // Saving/Loading/Deleting presets: $this->processPresets($inData); // Create export object and configure it: $this->export = GeneralUtility::makeInstance(\TYPO3\CMS\Impexp\ImportExport::class); $this->export->init(0, 'export'); $this->export->setCharset($this->lang->charSet); $this->export->maxFileSize = $inData['maxFileSize'] * 1024; $this->export->excludeMap = (array) $inData['exclude']; $this->export->softrefCfg = (array) $inData['softrefCfg']; $this->export->extensionDependencies = (array) $inData['extension_dep']; $this->export->showStaticRelations = $inData['showStaticRelations']; $this->export->includeExtFileResources = !$inData['excludeHTMLfileResources']; // Static tables: if (is_array($inData['external_static']['tables'])) { $this->export->relStaticTables = $inData['external_static']['tables']; } // Configure which tables external relations are included for: if (is_array($inData['external_ref']['tables'])) { $this->export->relOnlyTables = $inData['external_ref']['tables']; } $saveFilesOutsideExportFile = false; if (isset($inData['save_export']) && isset($inData['saveFilesOutsideExportFile']) && $inData['saveFilesOutsideExportFile'] === '1') { $this->export->setSaveFilesOutsideExportFile(true); $saveFilesOutsideExportFile = true; } $this->export->setHeaderBasics(); // Meta data setting: $beUser = $this->getBackendUser(); $this->export->setMetaData($inData['meta']['title'], $inData['meta']['description'], $inData['meta']['notes'], $beUser->user['username'], $beUser->user['realName'], $beUser->user['email']); if ($inData['meta']['thumbnail']) { $theThumb = $this->getFile($inData['meta']['thumbnail']); if ($theThumb !== null && $theThumb->exists()) { $this->export->addThumbnail($theThumb->getForLocalProcessing(false)); } } // Configure which records to export if (is_array($inData['record'])) { foreach ($inData['record'] as $ref) { $rParts = explode(':', $ref); $this->export->export_addRecord($rParts[0], BackendUtility::getRecord($rParts[0], $rParts[1])); } } // Configure which tables to export if (is_array($inData['list'])) { $db = $this->getDatabaseConnection(); foreach ($inData['list'] as $ref) { $rParts = explode(':', $ref); if ($beUser->check('tables_select', $rParts[0])) { $res = $this->exec_listQueryPid($rParts[0], $rParts[1], MathUtility::forceIntegerInRange($inData['listCfg']['maxNumber'], 1)); while ($subTrow = $db->sql_fetch_assoc($res)) { $this->export->export_addRecord($rParts[0], $subTrow); } $db->sql_free_result($res); } } } // Pagetree if (isset($inData['pagetree']['id'])) { // Based on click-expandable tree $idH = null; if ($inData['pagetree']['levels'] == -1) { $pagetree = GeneralUtility::makeInstance(\TYPO3\CMS\Impexp\View\ExportPageTreeView::class); $tree = $pagetree->ext_tree($inData['pagetree']['id'], $this->filterPageIds($this->export->excludeMap)); $this->treeHTML = $pagetree->printTree($tree); $idH = $pagetree->buffer_idH; } elseif ($inData['pagetree']['levels'] == -2) { $this->addRecordsForPid($inData['pagetree']['id'], $inData['pagetree']['tables'], $inData['pagetree']['maxNumber']); } else { // Based on depth // Drawing tree: // If the ID is zero, export root if (!$inData['pagetree']['id'] && $beUser->isAdmin()) { $sPage = array('uid' => 0, 'title' => 'ROOT'); } else { $sPage = BackendUtility::getRecordWSOL('pages', $inData['pagetree']['id'], '*', ' AND ' . $this->perms_clause); } if (is_array($sPage)) { $pid = $inData['pagetree']['id']; $tree = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\View\PageTreeView::class); $tree->init('AND ' . $this->perms_clause . $this->filterPageIds($this->export->excludeMap)); $HTML = $this->iconFactory->getIconForRecord('pages', $sPage, Icon::SIZE_SMALL)->render(); $tree->tree[] = array('row' => $sPage, 'HTML' => $HTML); $tree->buffer_idH = array(); if ($inData['pagetree']['levels'] > 0) { $tree->getTree($pid, $inData['pagetree']['levels'], ''); } $idH = array(); $idH[$pid]['uid'] = $pid; if (!empty($tree->buffer_idH)) { $idH[$pid]['subrow'] = $tree->buffer_idH; } $pagetree = GeneralUtility::makeInstance(\TYPO3\CMS\Impexp\View\ExportPageTreeView::class); $this->treeHTML = $pagetree->printTree($tree->tree); $this->shortcutName .= ' (' . $sPage['title'] . ')'; } } // In any case we should have a multi-level array, $idH, with the page structure // here (and the HTML-code loaded into memory for nice display...) if (is_array($idH)) { // Sets the pagetree and gets a 1-dim array in return with the pages (in correct submission order BTW...) $flatList = $this->export->setPageTree($idH); foreach ($flatList as $k => $value) { $this->export->export_addRecord('pages', BackendUtility::getRecord('pages', $k)); $this->addRecordsForPid($k, $inData['pagetree']['tables'], $inData['pagetree']['maxNumber']); } } } // After adding ALL records we set relations: for ($a = 0; $a < 10; $a++) { $addR = $this->export->export_addDBRelations($a); if (empty($addR)) { break; } } // Finally files are added: // MUST be after the DBrelations are set so that files from ALL added records are included! $this->export->export_addFilesFromRelations(); $this->export->export_addFilesFromSysFilesRecords(); // If the download button is clicked, return file if ($inData['download_export'] || $inData['save_export']) { switch ((string) $inData['filetype']) { case 'xml': $out = $this->export->compileMemoryToFileContent('xml'); $fExt = '.xml'; break; case 't3d': $this->export->dontCompress = 1; // intentional fall-through // intentional fall-through default: $out = $this->export->compileMemoryToFileContent(); $fExt = ($this->export->doOutputCompress() ? '-z' : '') . '.t3d'; } // Filename: $dlFile = $inData['filename']; if (!$dlFile) { $exportName = substr(preg_replace('/[^[:alnum:]_]/', '-', $inData['download_export_name']), 0, 20); $dlFile = 'T3D_' . $exportName . '_' . date('Y-m-d_H-i') . $fExt; } // Export for download: if ($inData['download_export']) { $mimeType = 'application/octet-stream'; Header('Content-Type: ' . $mimeType); Header('Content-Length: ' . strlen($out)); Header('Content-Disposition: attachment; filename=' . basename($dlFile)); echo $out; die; } // Export by saving: if ($inData['save_export']) { $saveFolder = $this->getDefaultImportExportFolder(); if ($saveFolder !== false && $saveFolder->checkActionPermission('write')) { $temporaryFileName = GeneralUtility::tempnam('export'); file_put_contents($temporaryFileName, $out); $file = $saveFolder->addFile($temporaryFileName, $dlFile, 'replace'); if ($saveFilesOutsideExportFile) { $filesFolderName = $dlFile . '.files'; $filesFolder = $saveFolder->createFolder($filesFolderName); $temporaryFolderForExport = ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->export->getTemporaryFilesPathForExport()); $temporaryFilesForExport = $temporaryFolderForExport->getFiles(); foreach ($temporaryFilesForExport as $temporaryFileForExport) { $filesFolder->getStorage()->moveFile($temporaryFileForExport, $filesFolder); } $temporaryFolderForExport->delete(); } /** @var FlashMessage $flashMessage */ $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, sprintf($GLOBALS['LANG']->getLL('exportdata_savedInSBytes', true), $file->getPublicUrl(), GeneralUtility::formatSize(strlen($out))), $GLOBALS['LANG']->getLL('exportdata_savedFile'), FlashMessage::OK); } else { /** @var FlashMessage $flashMessage */ $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, sprintf($GLOBALS['LANG']->getLL('exportdata_badPathS', true), $saveFolder->getPublicUrl()), $GLOBALS['LANG']->getLL('exportdata_problemsSavingFile'), FlashMessage::ERROR); } $this->content .= $flashMessage->render(); } } // OUTPUT to BROWSER: // Now, if we didn't make download file, show configuration form based on export: $menuItems = array(); // Export configuration $row = array(); $this->makeConfigurationForm($inData, $row); $menuItems[] = array('label' => $this->lang->getLL('tableselec_configuration'), 'content' => ' <table border="0" cellpadding="1" cellspacing="1"> ' . implode(' ', $row) . ' </table> '); // File options $row = array(); $this->makeSaveForm($inData, $row); $menuItems[] = array('label' => $this->lang->getLL('exportdata_filePreset'), 'content' => ' <table border="0" cellpadding="1" cellspacing="1"> ' . implode(' ', $row) . ' </table> '); // File options $row = array(); $this->makeAdvancedOptionsForm($inData, $row); $menuItems[] = array('label' => $this->lang->getLL('exportdata_advancedOptions'), 'content' => ' <table border="0" cellpadding="1" cellspacing="1"> ' . implode(' ', $row) . ' </table> '); // Generate overview: $overViewContent = $this->export->displayContentOverview(); // Print errors that might be: $errors = $this->export->printErrorLog(); $menuItems[] = array('label' => $this->lang->getLL('exportdata_messages'), 'content' => $errors, 'stateIcon' => $errors ? 2 : 0); // Add hidden fields and create tabs: $content = $this->moduleTemplate->getDynamicTabMenu($menuItems, 'tx_impexp_export', 1, false, true, false); $content .= '<input type="hidden" name="tx_impexp[action]" value="export" />'; $this->content .= $this->moduleTemplate->section('', $content, 0, 1); // Output Overview: $this->content .= $this->moduleTemplate->section($this->lang->getLL('execlistqu_structureToBeExported'), $overViewContent, 0, 1); }
/** * Index External URLs HTML content * * @param string $externalUrl URL, eg. "http://typo3.org/ * @return void * @see indexRegularDocument() */ public function indexExternalUrl($externalUrl) { // Parse External URL: $qParts = parse_url($externalUrl); $fI = pathinfo($qParts['path']); $ext = strtolower($fI['extension']); // Get headers: $urlHeaders = $this->getUrlHeaders($externalUrl); if (stristr($urlHeaders['Content-Type'], 'text/html')) { $content = $this->indexExternalUrl_content = GeneralUtility::getUrl($externalUrl); if ((string) $content !== '') { // Create temporary file: $tmpFile = GeneralUtility::tempnam('EXTERNAL_URL'); if ($tmpFile) { GeneralUtility::writeFile($tmpFile, $content); // Index that file: $this->indexRegularDocument($externalUrl, true, $tmpFile, 'html'); // Using "TRUE" for second parameter to force indexing of external URLs (mtime doesn't make sense, does it?) unlink($tmpFile); } } } }
/** * Returns a temporary path for a given file, including the file extension. * * @param string $fileIdentifier * @return string */ protected function getTemporaryPathForFile($fileIdentifier) { return \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('fal-tempfile-', '.' . PathUtility::pathinfo($fileIdentifier, PATHINFO_EXTENSION)); }
/** * The actual sprite generator, renders the command for Im/GM and executes * * @return void */ protected function generateGraphic() { $tempSprite = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam($this->spriteName); $filePath = array('mainFile' => PATH_site . $this->spriteFolder . $this->spriteName . '.png'); // Create black true color image with given size $newSprite = imagecreatetruecolor($this->spriteWidth, $this->spriteHeight); imagesavealpha($newSprite, TRUE); // Make it transparent imagefill($newSprite, 0, 0, imagecolorallocatealpha($newSprite, 0, 255, 255, 127)); foreach ($this->iconsData as $icon) { $function = 'imagecreatefrom' . strtolower($icon['fileExtension']); if (function_exists($function)) { $currentIcon = $function($icon['fileName']); imagecopy($newSprite, $currentIcon, $icon['left'], $icon['top'], 0, 0, $icon['width'], $icon['height']); } } imagepng($newSprite, $tempSprite . '.png'); \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move($tempSprite . '.png', $filePath['mainFile']); \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($tempSprite . '.png'); }
/** * Convert PNG image using pngquant command. * * @param AbstractFile $file */ public function convertPngImage(AbstractFile $file) { try { if (self::EXTENSION_PNG !== $file->getExtension()) { return; } // Ignore processed file which uses original file if ($file instanceof ProcessedFile && $file->usesOriginalFile()) { $this->logger->debug('Do not convert processed file identical with its original file', array('file' => $inputFilePath)); return; } // Set input/output files for pngquant command // Input file is the the specified file we want to quantize // Output file is a temporary file in typo3temp directory $inputFilePath = PATH_site . $file->getPublicUrl(); $outputFilePath = GeneralUtility::tempnam('sg_pngquant_', '.' . self::EXTENSION_PNG); // Build command line $cmd = $this->buildCommand($inputFilePath, $outputFilePath); $result = CommandUtility::exec($cmd, $output, $returnValue); if (0 === $returnValue) { // Replace content if ($file instanceof ProcessedFile) { // For processed file, only convert real processed file (i.e. different than their original file) // Temporary file is automatically removed when updating a processed file $this->logger->debug('Update processed file', array('cmd' => $cmd)); $file->updateWithLocalFile($outputFilePath); } elseif (!$this->confArray['keepOriginal']) { // Convert original files according to extension configuration // After conversion the temporary file is removed $this->logger->debug('Update original file', array('cmd' => $cmd)); $contents = @file_get_contents($outputFilePath); $file->setContents($contents); } } else { $this->logger->error('Convert image', array('cmd' => $cmd, 'result' => $result, 'output' => $output, 'returnValue' => $returnValue)); } } catch (\RuntimeException $e) { $this->logger->error($e->getMessage()); } // Remove temporary file, if exists if (file_exists($outputFilePath)) { $this->removeTemporaryFile($outputFilePath); } }
/** * Pass the content through tidy - a little program that cleans up HTML-code. * Requires $this->TYPO3_CONF_VARS['FE']['tidy'] to be TRUE and $this->TYPO3_CONF_VARS['FE']['tidy_path'] to * contain the filename/path of tidy including clean-up arguments for tidy. See default value in * TYPO3_CONF_VARS in t3lib/stddb/DefaultSettings.php * * @param string $content The page content to clean up. Will be written to a temporary file which "tidy" is then asked to clean up. File content is read back and returned. * @return string Returns the * @todo Define visibility */ public function tidyHTML($content) { if ($this->TYPO3_CONF_VARS['FE']['tidy'] && $this->TYPO3_CONF_VARS['FE']['tidy_path']) { $oldContent = $content; // Create temporary name $fname = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('typo3_tidydoc_'); // Delete if exists, just to be safe. @unlink($fname); // Open for writing $fp = fopen($fname, 'wb'); // Put $content fputs($fp, $content); // Close @fclose($fp); // run the $content through 'tidy', which formats the HTML to nice code. exec($this->TYPO3_CONF_VARS['FE']['tidy_path'] . ' ' . $fname, $output); // Delete the tempfile again @unlink($fname); $content = implode(LF, $output); if (!trim($content)) { // Restore old content due empty return value. $content = $oldContent; $GLOBALS['TT']->setTSlogMessage('"tidy" returned an empty value!', 2); } $GLOBALS['TT']->setTSlogMessage('"tidy" content lenght: ' . strlen($content), 0); } return $content; }
/** * @test */ public function canPrepareDomDocument() { $fileName = GeneralUtility::tempnam('test'); $instance = $this->getMock('FluidTYPO3\\Flll\\Service\\LanguageFileService', array('readFile')); $source = '<test></test>'; $domDocument = new \DOMDocument(); $domDocument->appendChild($domDocument->createElement('test')); $instance->expects($this->once())->method('readFile')->with($fileName)->will($this->returnValue($source)); $this->callInaccessibleMethod($instance, 'prepareDomDocument', $fileName); $result = $this->callInaccessibleMethod($instance, 'prepareDomDocument', $fileName); $this->assertEquals($domDocument, $result); }
/** * Writes the file from import array to temp dir and returns the filename of it. * * @param array $fI File information with three keys: "filename" = filename without path, "ID_absFile" = absolute filepath to the file (including the filename), "ID" = md5 hash of "ID_absFile * @return string Absolute filename of the temporary filename of the file. In ->alternativeFileName the original name is set. * @todo Define visibility */ public function import_addFileNameToBeCopied($fI) { if (is_array($this->dat['files'][$fI['ID']])) { $tmpFile = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('import_temp_'); \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($tmpFile, $this->dat['files'][$fI['ID']]['content']); clearstatcache(); if (@is_file($tmpFile)) { $this->unlinkFiles[] = $tmpFile; if (filesize($tmpFile) == $this->dat['files'][$fI['ID']]['filesize']) { $this->alternativeFileName[$tmpFile] = $fI['filename']; $this->alternativeFilePath[$tmpFile] = $this->dat['files'][$fI['ID']]['relFileRef']; return $tmpFile; } else { $this->error('Error: temporary file ' . $tmpFile . ' had a size (' . filesize($tmpFile) . ') different from the original (' . $this->dat['files'][$fI['ID']]['filesize'] . ')', 1); } } else { $this->error('Error: temporary file ' . $tmpFile . ' was not written as it should have been!', 1); } } else { $this->error('Error: No file found for ID ' . $fI['ID'], 1); } }
/** * This method actually does the processing of files locally * * Takes the original file (for remote storages this will be fetched from the remote server), * does the IM magic on the local server by creating a temporary typo3temp/ file, * copies the typo3temp/ file to the processing folder of the target storage and * removes the typo3temp/ file. * * The returned array has the following structure: * width => 100 * height => 200 * filePath => /some/path * * If filePath isn't set but width and height are the original file is used as ProcessedFile * with the returned width and height. This is for example useful for SVG images. * * @param TaskInterface $task * @return array|NULL */ public function process(TaskInterface $task) { $result = NULL; $targetFile = $task->getTargetFile(); $sourceFile = $task->getSourceFile(); $originalFileName = $sourceFile->getForLocalProcessing(FALSE); /** @var $gifBuilder GifBuilder */ $gifBuilder = GeneralUtility::makeInstance(GifBuilder::class); $gifBuilder->init(); $gifBuilder->absPrefix = PATH_site; $configuration = $targetFile->getProcessingConfiguration(); $configuration['additionalParameters'] = $this->modifyImageMagickStripProfileParameters($configuration['additionalParameters'], $configuration); if (empty($configuration['fileExtension'])) { $configuration['fileExtension'] = $task->getTargetFileExtension(); } $options = $this->getConfigurationForImageCropScaleMask($targetFile, $gifBuilder); $croppedImage = NULL; if (!empty($configuration['crop'])) { // check if it is a json object $cropData = json_decode($configuration['crop']); if ($cropData) { $crop = implode(',', array((int) $cropData->x, (int) $cropData->y, (int) $cropData->width, (int) $cropData->height)); } else { $crop = $configuration['crop']; } $im = $gifBuilder->imageCreateFromFile($originalFileName); $croppedImage = GeneralUtility::tempnam('crop_', '.' . $sourceFile->getExtension()); $gifBuilder->crop($im, ['crop' => $crop]); if ($gifBuilder->ImageWrite($im, $croppedImage)) { $originalFileName = $croppedImage; } } // Normal situation (no masking) if (!(is_array($configuration['maskImages']) && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im'])) { // SVG if ($croppedImage === NULL && $sourceFile->getExtension() === 'svg') { $newDimensions = $this->getNewSvgDimensions($sourceFile, $configuration, $options, $gifBuilder); $result = array(0 => $newDimensions['width'], 1 => $newDimensions['height'], 3 => ''); // all other images } else { // the result info is an array with 0=width,1=height,2=extension,3=filename $result = $gifBuilder->imageMagickConvert($originalFileName, $configuration['fileExtension'], $configuration['width'], $configuration['height'], $configuration['additionalParameters'], $configuration['frame'], $options); } } else { $targetFileName = $this->getFilenameForImageCropScaleMask($task); $temporaryFileName = $gifBuilder->tempPath . $targetFileName; $maskImage = $configuration['maskImages']['maskImage']; $maskBackgroundImage = $configuration['maskImages']['backgroundImage']; if ($maskImage instanceof Resource\FileInterface && $maskBackgroundImage instanceof Resource\FileInterface) { $temporaryExtension = 'png'; if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_mask_temp_ext_gif']) { // If ImageMagick version 5+ $temporaryExtension = $gifBuilder->gifExtension; } $tempFileInfo = $gifBuilder->imageMagickConvert($originalFileName, $temporaryExtension, $configuration['width'], $configuration['height'], $configuration['additionalParameters'], $configuration['frame'], $options); if (is_array($tempFileInfo)) { $maskBottomImage = $configuration['maskImages']['maskBottomImage']; if ($maskBottomImage instanceof Resource\FileInterface) { $maskBottomImageMask = $configuration['maskImages']['maskBottomImageMask']; } else { $maskBottomImageMask = NULL; } // Scaling: **** $tempScale = array(); $command = '-geometry ' . $tempFileInfo[0] . 'x' . $tempFileInfo[1] . '!'; $command = $this->modifyImageMagickStripProfileParameters($command, $configuration); $tmpStr = $gifBuilder->randomName(); // m_mask $tempScale['m_mask'] = $tmpStr . '_mask.' . $temporaryExtension; $gifBuilder->imageMagickExec($maskImage->getForLocalProcessing(TRUE), $tempScale['m_mask'], $command); // m_bgImg $tempScale['m_bgImg'] = $tmpStr . '_bgImg.miff'; $gifBuilder->imageMagickExec($maskBackgroundImage->getForLocalProcessing(), $tempScale['m_bgImg'], $command); // m_bottomImg / m_bottomImg_mask if ($maskBottomImage instanceof Resource\FileInterface && $maskBottomImageMask instanceof Resource\FileInterface) { $tempScale['m_bottomImg'] = $tmpStr . '_bottomImg.' . $temporaryExtension; $gifBuilder->imageMagickExec($maskBottomImage->getForLocalProcessing(), $tempScale['m_bottomImg'], $command); $tempScale['m_bottomImg_mask'] = $tmpStr . '_bottomImg_mask.' . $temporaryExtension; $gifBuilder->imageMagickExec($maskBottomImageMask->getForLocalProcessing(), $tempScale['m_bottomImg_mask'], $command); // BEGIN combining: // The image onto the background $gifBuilder->combineExec($tempScale['m_bgImg'], $tempScale['m_bottomImg'], $tempScale['m_bottomImg_mask'], $tempScale['m_bgImg']); } // The image onto the background $gifBuilder->combineExec($tempScale['m_bgImg'], $tempFileInfo[3], $tempScale['m_mask'], $temporaryFileName); $tempFileInfo[3] = $temporaryFileName; // Unlink the temp-images... foreach ($tempScale as $tempFile) { if (@is_file($tempFile)) { unlink($tempFile); } } } $result = $tempFileInfo; } } // check if the processing really generated a new file (scaled and/or cropped) if ($result !== NULL) { if ($result[3] !== $originalFileName || $originalFileName === $croppedImage) { $result = array('width' => $result[0], 'height' => $result[1], 'filePath' => $result[3]); } else { // No file was generated $result = NULL; } } // Cleanup temp file if it isn't used as result if ($croppedImage && ($result === NULL || $croppedImage !== $result['filePath'])) { GeneralUtility::unlink_tempfile($croppedImage); } return $result; }
/** * Reads the content of an external file being indexed. * * @param string $ext File extension, eg. "pdf", "doc" etc. * @param string $absFile Absolute filename of file (must exist and be validated OK before calling function) * @param string $cPKey Pointer to section (zero for all other than PDF which will have an indication of pages into which the document should be split.) * @return array Standard content array (title, description, keywords, body keys) */ public function readFileContent($ext, $absFile, $cPKey) { unset($contentArr); // Return immediately if initialization didn't set support up: if (!$this->supportedExtensions[$ext]) { return FALSE; } // Switch by file extension switch ($ext) { case 'pdf': if ($this->app['pdfinfo']) { $this->setLocaleForServerFileSystem(); // Getting pdf-info: $cmd = $this->app['pdfinfo'] . ' ' . escapeshellarg($absFile); \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $res); $pdfInfo = $this->splitPdfInfo($res); unset($res); if ((int) $pdfInfo['pages']) { list($low, $high) = explode('-', $cPKey); // Get pdf content: $tempFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('Typo3_indexer'); // Create temporary name @unlink($tempFileName); // Delete if exists, just to be safe. $cmd = $this->app['pdftotext'] . ' -f ' . $low . ' -l ' . $high . ' -enc UTF-8 -q ' . escapeshellarg($absFile) . ' ' . $tempFileName; \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd); if (@is_file($tempFileName)) { $content = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($tempFileName); unlink($tempFileName); } else { $content = ''; $this->pObj->log_setTSlogMessage(sprintf($this->sL('LLL:EXT:indexed_search/locallang.xlf:pdfToolsFailed'), $absFile), 2); } if ((string) $content !== '') { $contentArr = $this->pObj->splitRegularContent($this->removeEndJunk($content)); } } $this->setLocaleForServerFileSystem(TRUE); } break; case 'doc': if ($this->app['catdoc']) { $this->setLocaleForServerFileSystem(); $cmd = $this->app['catdoc'] . ' -d utf-8 ' . escapeshellarg($absFile); \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $res); $content = implode(LF, $res); unset($res); $contentArr = $this->pObj->splitRegularContent($this->removeEndJunk($content)); $this->setLocaleForServerFileSystem(TRUE); } break; case 'pps': case 'ppt': if ($this->app['ppthtml']) { $this->setLocaleForServerFileSystem(); $cmd = $this->app['ppthtml'] . ' ' . escapeshellarg($absFile); \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $res); $content = implode(LF, $res); unset($res); $content = $this->pObj->convertHTMLToUtf8($content); $contentArr = $this->pObj->splitHTMLContent($this->removeEndJunk($content)); $contentArr['title'] = basename($absFile); $this->setLocaleForServerFileSystem(TRUE); } break; case 'xls': if ($this->app['xlhtml']) { $this->setLocaleForServerFileSystem(); $cmd = $this->app['xlhtml'] . ' -nc -te ' . escapeshellarg($absFile); \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $res); $content = implode(LF, $res); unset($res); $content = $this->pObj->convertHTMLToUtf8($content); $contentArr = $this->pObj->splitHTMLContent($this->removeEndJunk($content)); $contentArr['title'] = basename($absFile); $this->setLocaleForServerFileSystem(TRUE); } break; case 'sxi': case 'sxc': case 'sxw': case 'ods': case 'odp': case 'odt': if ($this->app['unzip']) { $this->setLocaleForServerFileSystem(); // Read content.xml: $cmd = $this->app['unzip'] . ' -p ' . escapeshellarg($absFile) . ' content.xml'; \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $res); $content_xml = implode(LF, $res); unset($res); // Read meta.xml: $cmd = $this->app['unzip'] . ' -p ' . escapeshellarg($absFile) . ' meta.xml'; \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $res); $meta_xml = implode(LF, $res); unset($res); $utf8_content = trim(strip_tags(str_replace('<', ' <', $content_xml))); $contentArr = $this->pObj->splitRegularContent($utf8_content); $contentArr['title'] = basename($absFile); // Make sure the title doesn't expose the absolute path! // Meta information $metaContent = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2tree($meta_xml); $metaContent = $metaContent['office:document-meta'][0]['ch']['office:meta'][0]['ch']; if (is_array($metaContent)) { $contentArr['title'] = $metaContent['dc:title'][0]['values'][0] ? $metaContent['dc:title'][0]['values'][0] : $contentArr['title']; $contentArr['description'] = $metaContent['dc:subject'][0]['values'][0] . ' ' . $metaContent['dc:description'][0]['values'][0]; // Keywords collected: if (is_array($metaContent['meta:keywords'][0]['ch']['meta:keyword'])) { foreach ($metaContent['meta:keywords'][0]['ch']['meta:keyword'] as $kwDat) { $contentArr['keywords'] .= $kwDat['values'][0] . ' '; } } } $this->setLocaleForServerFileSystem(TRUE); } break; case 'rtf': if ($this->app['unrtf']) { $this->setLocaleForServerFileSystem(); $cmd = $this->app['unrtf'] . ' ' . escapeshellarg($absFile); \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $res); $fileContent = implode(LF, $res); unset($res); $fileContent = $this->pObj->convertHTMLToUtf8($fileContent); $contentArr = $this->pObj->splitHTMLContent($fileContent); $this->setLocaleForServerFileSystem(TRUE); } break; case 'txt': case 'csv': $this->setLocaleForServerFileSystem(); // Raw text $content = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($absFile); // @todo Implement auto detection of charset (currently assuming utf-8) $contentCharset = 'utf-8'; $content = $this->pObj->convertHTMLToUtf8($content, $contentCharset); $contentArr = $this->pObj->splitRegularContent($content); $contentArr['title'] = basename($absFile); // Make sure the title doesn't expose the absolute path! $this->setLocaleForServerFileSystem(TRUE); break; case 'html': case 'htm': $fileContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($absFile); $fileContent = $this->pObj->convertHTMLToUtf8($fileContent); $contentArr = $this->pObj->splitHTMLContent($fileContent); break; case 'xml': $this->setLocaleForServerFileSystem(); // PHP strip-tags() $fileContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($absFile); // Finding charset: preg_match('/^[[:space:]]*<\\?xml[^>]+encoding[[:space:]]*=[[:space:]]*["\'][[:space:]]*([[:alnum:]_-]+)[[:space:]]*["\']/i', substr($fileContent, 0, 200), $reg); $charset = $reg[1] ? $this->pObj->csObj->parse_charset($reg[1]) : 'utf-8'; // Converting content: $fileContent = $this->pObj->convertHTMLToUtf8(strip_tags(str_replace('<', ' <', $fileContent)), $charset); $contentArr = $this->pObj->splitRegularContent($fileContent); $contentArr['title'] = basename($absFile); // Make sure the title doesn't expose the absolute path! $this->setLocaleForServerFileSystem(TRUE); break; case 'jpg': case 'jpeg': case 'tif': $this->setLocaleForServerFileSystem(); // PHP EXIF if (function_exists('exif_read_data')) { $exif = @exif_read_data($absFile, 'IFD0'); } else { $exif = FALSE; } if ($exif) { $comment = trim($exif['COMMENT'][0] . ' ' . $exif['ImageDescription']); } else { $comment = ''; } $contentArr = $this->pObj->splitRegularContent($comment); $contentArr['title'] = basename($absFile); // Make sure the title doesn't expose the absolute path! $this->setLocaleForServerFileSystem(TRUE); break; default: return FALSE; } // If no title (and why should there be...) then the file-name is set as title. This will raise the hits considerably if the search matches the document name. if (is_array($contentArr) && !$contentArr['title']) { // Substituting "_" for " " because many filenames may have this instead of a space char. $contentArr['title'] = str_replace('_', ' ', basename($absFile)); } return $contentArr; }
/** * Takes a string as input and detects its language. * * @param string $input * @return string Language ISO code */ public function detectLanguageFromString($input) { $tempFilePath = GeneralUtility::tempnam('Tx_Tika_AppService_DetectLanguage'); file_put_contents($tempFilePath, $input); // detect language $language = $this->detectLanguageFromLocalFile($tempFilePath); // cleanup unlink($tempFilePath); return $language; }
/** * Writes the file from import array to temp dir and returns the filename of it. * * @param array $fI File information with three keys: "filename" = filename without path, "ID_absFile" = absolute filepath to the file (including the filename), "ID" = md5 hash of "ID_absFile * @return string|NULL Absolute filename of the temporary filename of the file. In ->alternativeFileName the original name is set. */ public function import_addFileNameToBeCopied($fI) { if (is_array($this->dat['files'][$fI['ID']])) { $tmpFile = null; // check if there is the right file already in the local folder if ($this->filesPathForImport !== null) { if (is_file($this->filesPathForImport . '/' . $this->dat['files'][$fI['ID']]['content_md5']) && md5_file($this->filesPathForImport . '/' . $this->dat['files'][$fI['ID']]['content_md5']) === $this->dat['files'][$fI['ID']]['content_md5']) { $tmpFile = $this->filesPathForImport . '/' . $this->dat['files'][$fI['ID']]['content_md5']; } } if ($tmpFile === null) { $tmpFile = GeneralUtility::tempnam('import_temp_'); GeneralUtility::writeFile($tmpFile, $this->dat['files'][$fI['ID']]['content']); } clearstatcache(); if (@is_file($tmpFile)) { $this->unlinkFiles[] = $tmpFile; if (filesize($tmpFile) == $this->dat['files'][$fI['ID']]['filesize']) { $this->alternativeFileName[$tmpFile] = $fI['filename']; $this->alternativeFilePath[$tmpFile] = $this->dat['files'][$fI['ID']]['relFileRef']; return $tmpFile; } else { $this->error('Error: temporary file ' . $tmpFile . ' had a size (' . filesize($tmpFile) . ') different from the original (' . $this->dat['files'][$fI['ID']]['filesize'] . ')'); } } else { $this->error('Error: temporary file ' . $tmpFile . ' was not written as it should have been!'); } } else { $this->error('Error: No file found for ID ' . $fI['ID']); } return null; }
/** * The actual sprite generator, renders the command for IM/GM and executes * * @return void */ protected function generateHighDensityGraphic() { $tempSprite = GeneralUtility::tempnam($this->spriteName . '@x2', '.png'); $filePath = PATH_site . $this->spriteFolder . $this->spriteName . '@x2.png'; // Create black true color image with given size $newSprite = imagecreatetruecolor($this->spriteWidth * 2, $this->spriteHeight * 2); imagesavealpha($newSprite, TRUE); // Make it transparent imagefill($newSprite, 0, 0, imagecolorallocatealpha($newSprite, 0, 255, 255, 127)); foreach ($this->iconsData as $icon) { $function = 'imagecreatefrom' . strtolower($icon['fileExtension']); if (function_exists($function)) { if ($icon['fileNameHighDensity'] !== FALSE) { // copy HighDensity file $currentIcon = $function($icon['fileNameHighDensity']); imagecopy($newSprite, $currentIcon, $icon['left'] * 2, $icon['top'] * 2, 0, 0, $icon['width'] * 2, $icon['height'] * 2); } else { // scale up normal file $currentIcon = $function($icon['fileName']); imagecopyresized($newSprite, $currentIcon, $icon['left'] * 2, $icon['top'] * 2, 0, 0, $icon['width'] * 2, $icon['height'] * 2, $icon['width'], $icon['height']); } } } imagepng($newSprite, $tempSprite); GeneralUtility::upload_copy_move($tempSprite, $filePath); GeneralUtility::unlink_tempfile($tempSprite); }