Skip to content

Commit

Permalink
Merge pull request teaminmedias-pluswerk#61 from mrvoss/properly-shel…
Browse files Browse the repository at this point in the history
…l-escape

Make external tools work w/ UTF-8 file names by using CommandUtility::escapeShellArgument()
  • Loading branch information
Christian Bülter authored Sep 8, 2016
2 parents 3378d11 + e9b2a2a commit fb6892c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Plugin 'Faceted search' for the 'ke_search' extension.
*
Expand Down Expand Up @@ -74,18 +77,19 @@ public function __construct() {
*/
public function getContent($file) {
// create the tempfile which will contain the content
$tempFileName = TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('doc_files-Indexer');
$tempFileName = GeneralUtility::tempnam('doc_files-Indexer');

// Delete if exists, just to be safe.
@unlink($tempFileName);

// generate and execute the pdftotext commandline tool
$cmd = $this->app['catdoc'] . ' -s8859-1 -dutf-8 ' . escapeshellarg($file) . ' > ' . escapeshellarg($tempFileName);
TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd);
$fileEscaped = CommandUtility::escapeShellArgument($file);
$cmd = "{$this->app['catdoc']} -s8859-1 -dutf-8 $fileEscaped > $tempFileName";
CommandUtility::exec($cmd);

// check if the tempFile was successfully created
if (@is_file($tempFileName)) {
$content = TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($tempFileName);
$content = GeneralUtility::getUrl($tempFileName);
unlink($tempFileName);
}
else
Expand All @@ -99,4 +103,4 @@ public function getContent($file) {
return false;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Plugin 'Faceted search' for the 'ke_search' extension.
*
Expand Down Expand Up @@ -72,30 +75,31 @@ public function __construct() {
* @return string The extracted content of the file
*/
public function getContent($file) {
$this->fileInfo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_kesearch_lib_fileinfo');
$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 (intval($pdfInfo['pages']) && $this->isAppArraySet) {
if ((int) $pdfInfo['pages'] && $this->isAppArraySet) {

// create the tempfile which will contain the content
$tempFileName = TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('pdf_files-Indexer');
$tempFileName = GeneralUtility::tempnam('pdf_files-Indexer');

// Delete if exists, just to be safe.
@unlink($tempFileName);

// generate and execute the pdftotext commandline tool
$cmd = $this->app['pdftotext'] . ' -enc UTF-8 -q ' . escapeshellarg($file) . ' ' . escapeshellarg($tempFileName);
$fileEscaped = CommandUtility::escapeShellArgument($file);
$cmd = "{$this->app['pdftotext']} -enc UTF-8 -q $fileEscaped $tempFileName";

TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd);
CommandUtility::exec($cmd);

// check if the tempFile was successfully created
if (@is_file($tempFileName)) {
$content = TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($tempFileName);
$content = GeneralUtility::getUrl($tempFileName);
unlink($tempFileName);
}
else {
Expand All @@ -118,19 +122,19 @@ public function getContent($file) {
* @return array The pdf informations as array
*/
public function getPdfInfo($file) {
if ($this->fileInfo->getIsFile()) {
if ($this->fileInfo->getExtension() == 'pdf' && $this->isAppArraySet) {
$cmd = $this->app['pdfinfo'] . ' ' . escapeshellarg($file);
\TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $pdfInfoArray);
$pdfInfo = $this->splitPdfInfo($pdfInfoArray);
unset($pdfInfoArray);
return $pdfInfo;
}
else
return false;
if ($this->fileInfo->getIsFile()
&& $this->fileInfo->getExtension() == 'pdf'
&& $this->isAppArraySet
) {
$fileEscaped = CommandUtility::escapeShellArgument($file);
$cmd = "{$this->app['pdfinfo']} $fileEscaped";
CommandUtility::exec($cmd, $pdfInfoArray);
$pdfInfo = $this->splitPdfInfo($pdfInfoArray);

return $pdfInfo;
}
else
return false;

return false;
}

/**
Expand All @@ -139,7 +143,7 @@ public function getPdfInfo($file) {
* @param array Array of PDF content, coming from the pdfinfo tool
* @return array The pdf informations as array in a useable format
*/
function splitPdfInfo($pdfInfoArray) {
public function splitPdfInfo($pdfInfoArray) {
$res = array();
if (is_array($pdfInfoArray)) {
foreach ($pdfInfoArray as $line) {
Expand All @@ -158,8 +162,8 @@ function splitPdfInfo($pdfInfoArray) {
* @param string String to clean up
* @return string Cleaned up string
*/
function removeEndJunk($string) {
public function removeEndJunk($string) {
return trim(preg_replace('/[' . LF . chr(12) . ']*$/', '', $string));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Plugin 'Faceted search' for the 'ke_search' extension.
*
Expand Down Expand Up @@ -71,18 +74,19 @@ public function __construct() {
*/
public function getContent($file) {
// create the tempfile which will contain the content
$tempFileName = TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('ppt_files-Indexer');
$tempFileName = GeneralUtility::tempnam('ppt_files-Indexer');

// Delete if exists, just to be safe.
@unlink($tempFileName);

// generate and execute the pdftotext commandline tool
$cmd = $this->app['catppt'] . ' -s8859-1 -dutf-8 ' . escapeshellarg($file) . ' > ' . escapeshellarg($tempFileName);
TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd);
$fileEscaped = CommandUtility::escapeShellArgument($file);
$cmd = "{$this->app['catppt']} -s8859-1 -dutf-8 $fileEscaped > $tempFileName";
CommandUtility::exec($cmd);

// check if the tempFile was successfully created
if (@is_file($tempFileName)) {
$content = TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($tempFileName);
$content = GeneralUtility::getUrl($tempFileName);
unlink($tempFileName);
}
else
Expand All @@ -96,4 +100,4 @@ public function getContent($file) {
return false;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Plugin 'Faceted search' for the 'ke_search' extension.
*
Expand Down Expand Up @@ -72,18 +75,19 @@ public function __construct() {
*/
public function getContent($file) {
// create the tempfile which will contain the content
$tempFileName = TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('xls_files-Indexer');
$tempFileName = GeneralUtility::tempnam('xls_files-Indexer');

// Delete if exists, just to be safe.
@unlink($tempFileName);

// generate and execute the pdftotext commandline tool
$cmd = $this->app['xls2csv'] . ' -c \' \' -q 0 -s8859-1 -dutf-8 ' . escapeshellarg($file) . ' > ' . escapeshellarg($tempFileName);
TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd);
$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 = TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($tempFileName);
$content = GeneralUtility::getUrl($tempFileName);
unlink($tempFileName);
}
else
Expand All @@ -97,4 +101,4 @@ public function getContent($file) {
return false;
}

}
}

0 comments on commit fb6892c

Please sign in to comment.