Convert eID-scripts to support PSR-7.

With TYPO3 7.4 "Feature: #68186 - PSR-7 support for eID" was added. With
TYPO3 8.7 the simple old style scripts did still work. As of TYPO 9.5
these scripts fail due to missing response object.

This patch converts the eID-scripts to make it work with TYPO3 8.7 and
9.5.
This commit is contained in:
Alexander Bigga 2020-02-05 15:04:47 +01:00
parent 281c3c81c2
commit 69fa811461
5 changed files with 91 additions and 52 deletions

View File

@ -12,6 +12,10 @@
namespace Kitodo\Dlf\Plugin\Eid;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* eID image proxy for plugin 'Page View' of the 'dlf' extension
*
@ -20,36 +24,51 @@ namespace Kitodo\Dlf\Plugin\Eid;
* @subpackage dlf
* @access public
*/
class PageViewProxy extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin
class PageViewProxy
{
public $scriptRelPath = 'Classes/Plugin/Eid/PageViewProxy.php';
/**
* The main method of the eID script
*
* @access public
*
* @return string
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function main()
public function main(ServerRequestInterface $request)
{
$this->cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
$header = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('header');
$url = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('url');
$fetchedData = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($url, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($header, 0, 2, 0));
// Add some header tags
header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . 'GMT');
header('Cache-Control: max-age=3600, must-revalidate');
header('Content-Length: ' . strlen($fetchedData));
header('Content-Type: ' . finfo_buffer(finfo_open(FILEINFO_MIME), $fetchedData));
// Get last modified date from request header
$fetchedHeader = explode("\n", \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($url, 2));
foreach ($fetchedHeader as $headerline) {
if (stripos($headerline, 'Last-Modified:') !== false) {
header($headerline);
break;
// header parameter for getUrl(); allowed values 0,1,2; default 0
$header = (int)$request->getQueryParams()['header'];
$header = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($header, 0, 2, 0);
// the URI to fetch data or header from
$url = (string)$request->getQueryParams()['url'];
if (!GeneralUtility::isValidUrl($url)) {
throw new \InvalidArgumentException('No valid url passed!', 1580482805);
}
// fetch the requested data or header
$fetchedData = GeneralUtility::getUrl($url, $header);
// Fetch header data separately to get "Last-Modified" info
if ($header === 0) {
$fetchedHeader = explode("\n", GeneralUtility::getUrl($url, 2));
foreach ($fetchedHeader as $headerline) {
if (stripos($headerline, 'Last-Modified:') !== false) {
$lastModified = trim(substr($headerline, strpos($headerline, ':') + 1));
break;
}
}
}
echo $fetchedData;
// create response object
/** @var Response $response */
$response = GeneralUtility::makeInstance(Response::class);
$response->getBody()->write($fetchedData);
$response = $response->withHeader('Content-Type', finfo_buffer(finfo_open(FILEINFO_MIME), $fetchedData));
if ($header === 0 && !empty($lastModified)) {
$response = $response->withHeader('Last-Modified', $lastModified);
}
return $response;
}
}
}

View File

@ -14,6 +14,10 @@ namespace Kitodo\Dlf\Plugin\Eid;
use Kitodo\Dlf\Common\Helper;
use Kitodo\Dlf\Common\Solr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
@ -24,31 +28,38 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
* @subpackage dlf
* @access public
*/
class SearchInDocument extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin
class SearchInDocument
{
public $scriptRelPath = 'Classes/Plugin/Eid/SearchInDocument.php';
/**
* The main method of the eID script
*
* @access public
*
* @return string JSON response of search suggestions
* @param ServerRequestInterface $request
* @return ResponseInterface JSON response of search suggestions
*/
public function main()
public function main(ServerRequestInterface $request)
{
if (
GeneralUtility::_GP('encrypted') != ''
&& GeneralUtility::_GP('hashed') != ''
) {
$core = Helper::decrypt(GeneralUtility::_GP('encrypted'), GeneralUtility::_GP('hashed'));
$parameters = $request->getParsedBody();
$encrypted = (string)$parameters['encrypted'];
$hashed = (string)$parameters['hashed'];
if (empty($encrypted) || empty($hashed)) {
throw new \InvalidArgumentException('No valid parameter passed!', 1580585079);
}
$core = Helper::decrypt($encrypted, $hashed);
if (!empty($core)) {
$url = trim(Solr::getSolrUrl($core), '/') . '/select?wt=json&q=fulltext:(' . Solr::escapeQuery(GeneralUtility::_GP('q')) . ')%20AND%20uid:' . GeneralUtility::_GP('uid')
$query = (string)$parameters['q'];
$uid = (string)$parameters['uid'];
$start = (string)$parameters['start'];
$url = trim(Solr::getSolrUrl($core), '/') . '/select?wt=json&q=fulltext:(' . Solr::escapeQuery($query) . ')%20AND%20uid:' . $uid
. '&hl=on&hl.fl=fulltext&fl=uid,id,page&hl.method=fastVector'
. '&start=' . GeneralUtility::_GP('start') . '&rows=20';
. '&start=' . $start . '&rows=20';
$output = GeneralUtility::getUrl($url);
}
echo $output;
// create response object
/** @var Response $response */
$response = GeneralUtility::makeInstance(Response::class);
$response->getBody()->write($output);
return $response;
}
}

View File

@ -14,6 +14,10 @@ namespace Kitodo\Dlf\Plugin\Eid;
use Kitodo\Dlf\Common\Helper;
use Kitodo\Dlf\Common\Solr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
@ -25,29 +29,34 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
* @subpackage dlf
* @access public
*/
class SearchSuggest extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin
class SearchSuggest
{
public $scriptRelPath = 'Classes/Plugin/Eid/SearchSuggest.php';
/**
* The main method of the eID script
*
* @access public
*
* @return string XML response of search suggestions
* @param ServerRequestInterface $request
* @return ResponseInterface XML response of search suggestions
*/
public function main()
public function main(ServerRequestInterface $request)
{
if (
GeneralUtility::_GP('encrypted') != ''
&& GeneralUtility::_GP('hashed') != ''
) {
$core = Helper::decrypt(GeneralUtility::_GP('encrypted'), GeneralUtility::_GP('hashed'));
$parameters = $request->getParsedBody();
$encrypted = (string)$parameters['encrypted'];
$hashed = (string)$parameters['hashed'];
if (empty($encrypted) || empty($hashed)) {
throw new \InvalidArgumentException('No valid parameter passed!', 1580585079);
}
$core = Helper::decrypt($encrypted, $hashed);
if (!empty($core)) {
$url = trim(Solr::getSolrUrl($core), '/') . '/suggest/?wt=xml&q=' . Solr::escapeQuery(GeneralUtility::_GP('q'));
$query = (string)$parameters['q'];
$url = trim(Solr::getSolrUrl($core), '/') . '/suggest/?wt=xml&q=' . Solr::escapeQuery($query);
$output = GeneralUtility::getUrl($url);
}
echo $output;
// create response object
/** @var Response $response */
$response = GeneralUtility::makeInstance(Response::class);
$response->getBody()->write($output);
return $response;
}
}

View File

@ -17,7 +17,7 @@
<input type="hidden" name="tx_dlf[start]" value="0" />
<input type="hidden" name="tx_dlf[id]" value="###CURRENT_DOCUMENT###" />
<input type="hidden" name="tx_dlf[encrypted]" value="###SOLR_ENCRYPTED###" />
<input type="hidden" name="tx_dlf[hash]" value="###SOLR_HASH###" />
<input type="hidden" name="tx_dlf[hashed]" value="###SOLR_HASH###" />
</div>
</form>
<div id="tx-dlf-search-in-document-loading" style="display: none;">###LABEL_LOADING###...</div>

View File

@ -65,7 +65,7 @@ $(document).ready(function() {
uid: $( "input[name='tx_dlf[id]']" ).val(),
start: $( "input[name='tx_dlf[start]']" ).val(),
encrypted: $( "input[name='tx_dlf[encrypted]']" ).val(),
hashed: $( "input[name='tx_dlf[hash]']" ).val(),
hashed: $( "input[name='tx_dlf[hashed]']" ).val(),
},
function(data) {
var resultItems = [];