1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser; |
4
|
|
|
|
5
|
|
|
/*************************************************************** |
6
|
|
|
* Copyright notice |
7
|
|
|
* |
8
|
|
|
* (c) 2015-2017 Timo Hund <[email protected]> |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
29
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Applies htmlspecialschars on documents of a solr response. |
33
|
|
|
* |
34
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser |
35
|
|
|
*/ |
36
|
|
|
class DocumentEscapeService { |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var TypoScriptConfiguration|null |
40
|
|
|
*/ |
41
|
|
|
protected $typoScriptConfiguration = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* DocumentEscapeService constructor. |
45
|
|
|
* @param TypoScriptConfiguration|null $typoScriptConfiguration |
46
|
|
|
*/ |
47
|
|
|
public function __construct(TypoScriptConfiguration $typoScriptConfiguration = null) { |
48
|
|
|
$this->typoScriptConfiguration = is_null($typoScriptConfiguration) ? Util::getSolrConfiguration() : $typoScriptConfiguration; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* This method is used to apply htmlspecialchars on all document fields that |
53
|
|
|
* are not configured to be secure. Secure mean that we know where the content is coming from. |
54
|
|
|
* |
55
|
|
|
* @param array $documents |
56
|
|
|
* @return \Apache_Solr_Document[] |
57
|
|
|
*/ |
58
|
|
|
public function applyHtmlSpecialCharsOnAllFields(array $documents) |
59
|
|
|
{ |
60
|
|
|
$trustedSolrFields = $this->typoScriptConfiguration->getSearchTrustedFieldsArray(); |
61
|
|
|
|
62
|
|
|
foreach ($documents as $key => $document) { |
63
|
|
|
$fieldNames = $document->getFieldNames(); |
64
|
|
|
|
65
|
|
|
foreach ($fieldNames as $fieldName) { |
66
|
|
|
if (is_array($trustedSolrFields) && in_array($fieldName, $trustedSolrFields)) { |
67
|
|
|
// we skip this field, since it was marked as secure |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$document->{$fieldName} = $this->applyHtmlSpecialCharsOnSingleFieldValue($document->{$fieldName}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$documents[$key] = $document; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $documents; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Applies htmlspecialchars on all items of an array of a single value. |
82
|
|
|
* |
83
|
|
|
* @param $fieldValue |
84
|
|
|
* @return array|string |
85
|
|
|
*/ |
86
|
|
|
protected function applyHtmlSpecialCharsOnSingleFieldValue($fieldValue) |
87
|
|
|
{ |
88
|
|
|
if (is_array($fieldValue)) { |
89
|
|
|
foreach ($fieldValue as $key => $fieldValueItem) { |
90
|
|
|
$fieldValue[$key] = htmlspecialchars($fieldValueItem, null, null, false); |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
$fieldValue = htmlspecialchars($fieldValue, null, null, false); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $fieldValue; |
97
|
|
|
} |
98
|
|
|
} |