Skip to content

Commit

Permalink
[WIP][CLEANUP] Refactor Code
Browse files Browse the repository at this point in the history
Related: #1
  • Loading branch information
Daniel Siepmann committed Oct 31, 2015
1 parent ad85d3e commit 3f30893
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 53 deletions.
68 changes: 15 additions & 53 deletions Classes/Generator/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* The TYPO3 project - inspiring people to share!
*/

use WebVision\WvPdfgen\Utility\UrlUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand All @@ -40,6 +41,11 @@ class Pdf
*/
public $cObj;

/**
* @var UrlUtility
*/
protected $urlUtility;

/**
* Userfunction for TYPO3. Which will generate the PDF for the current url
* and take $conf into account.
Expand All @@ -55,9 +61,11 @@ class Pdf
*/
public function main($content, array $conf)
{
$this->urlUtility = GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager')
->get('WebVision\WvPdfgen\Utility\UrlUtility');
$this->processConfiguration($conf);

if(!$this->pageAvailable()) {
if(!$this->urlUtility->urlAvailable($this->getUrlForGeneration())) {
$GLOBALS['TSFE']->pageNotFoundAndExit();
}
$this->generatePdf();
Expand Down Expand Up @@ -110,46 +118,7 @@ protected function getFileName()
*/
protected function getPdfUrl()
{
return $this->getDomain() . str_replace(PATH_site, '', $this->getFileName());
}

protected function pageAvailable()
{
$responseHeader = array();
\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(
$this->getUrlForGeneration(),
2,
array(),
$responseHeader
);
return ($responseHeader['http_code'] === '200');
}

protected function filterUrl($urlToFilter)
{
$filteredUrl = '';
$parsedUrl = parse_url($urlToFilter);
if(!isset($parsedUrl['query'])) {
return $urlToFilter;
}

$filteredUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
$urlQuery = array_filter(
explode('&', $parsedUrl['query']),
function ($queryParameter) {
list($parameterName) = explode('=', $queryParameter);
if(!in_array($parameterName, $this->configuration['parameterWhitelist'])) {
return false;
}
return true;
}
);

if(count($urlQuery) > 0) {
$filteredUrl .= '?' . implode('&', $urlQuery);
}

return $filteredUrl;
return $this->urlUtility->getDomain() . str_replace(PATH_site, '', $this->getFileName());
}

/**
Expand All @@ -161,7 +130,10 @@ function ($queryParameter) {
*/
protected function getUrlForGeneration()
{
$urlToConvert = $this->filterUrl($this->getDomain() . $GLOBALS['TSFE']->siteScript);
$urlToConvert = $this->urlUtility->filterUrl(
$this->urlUtility->getDomain() . $GLOBALS['TSFE']->siteScript,
$this->configuration['parameterWhitelist']
);

// Remove type parameter to generate the real url, not the PDF (= endless loop)
$urlToConvert = str_ireplace('type=' . $this->configuration['typeNum'], '', $urlToConvert);
Expand Down Expand Up @@ -208,7 +180,7 @@ protected function processConfiguration(array $configuration)
$this->configuration[$key] = $value;

if($key === 'parameterWhitelist') {
$this->configuration[$key] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $value);
$this->configuration[$key] = GeneralUtility::trimExplode(',', $value);
}
}

Expand All @@ -232,14 +204,4 @@ protected function processConfiguration(array $configuration)

return $this;
}

/**
* Get the current active domain, with protocol.
*
* @return string
*/
protected function getDomain()
{
return GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
}
}
89 changes: 89 additions & 0 deletions Classes/Utility/UrlUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
namespace WebVision\WvPdfgen\Utility;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Utility to ease work with urls.
*
* @author Daniel Siepmann <[email protected]>
*/
class UrlUtility
{
/**
* Check whether the given URL is available (=returns 200).
*
* @param string $urlToCheck
*
* @return bool
*/
public function urlAvailable($urlToCheck)
{
$responseHeader = array();
GeneralUtility::getUrl(
$urlToCheck,
2,
array(),
$responseHeader
);
return ($responseHeader['http_code'] === '200');
}

/**
* Remove all parameter from query, except if they are white listed.
*
* @param string $urlToFilter The url to filter.
* @param array $parameterWhitelist The list of parameter which are white listed.
*
* @return string
*/
public function filterUrl($urlToFilter, array $parameterWhitelist)
{
$filteredUrl = '';
$parsedUrl = parse_url($urlToFilter);
if(!isset($parsedUrl['query'])) {
return $urlToFilter;
}

$filteredUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
$urlQuery = array_filter(
explode('&', $parsedUrl['query']),
function ($queryParameter) {
list($parameterName) = explode('=', $queryParameter);
if(!in_array($parameterName, $parameterWhitelist)) {
return false;
}
return true;
}
);

if(count($urlQuery) > 0) {
$filteredUrl .= '?' . implode('&', $urlQuery);
}

return $filteredUrl;
}

/**
* Get the current active domain, with protocol.
*
* @return string
*/
public function getDomain()
{
return GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
}
}

0 comments on commit 3f30893

Please sign in to comment.