Passed
Push — master ( f63a3e...201a83 )
by Timo
34:43
created

ParameterKeepingFormModifier::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1.0156
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Plugin\Results;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012-2015 Michel Tremblay <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Plugin\CommandPluginAware;
28
use ApacheSolrForTypo3\Solr\Plugin\CommandPluginBase;
29
use ApacheSolrForTypo3\Solr\Plugin\FormModifier;
30
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
31
use ApacheSolrForTypo3\Solr\Template;
32
use ApacheSolrForTypo3\Solr\Util;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
/**
36
 * A form modifier to carry over GET parameters from one request to another if
37
 * the option plugin.tx_solr.search.keepExistingParametersForNewSearches is
38
 * enabled.
39
 *
40
 * @author Michel Tremblay <[email protected]>
41
 * @author Ingo Renner <[email protected]>
42
 */
43
class ParameterKeepingFormModifier implements FormModifier, CommandPluginAware
44
{
45
46
    /**
47
     * Configuration
48
     *
49
     * @var TypoScriptConfiguration
50
     */
51
    protected $configuration;
52
53
    /**
54
     * The currently active plugin
55
     *
56
     * @var CommandPluginBase
57
     */
58
    protected $parentPlugin;
59
60
    /**
61
     * Constructor
62
     *
63
     */
64 25
    public function __construct()
65
    {
66 25
        $this->configuration = Util::getSolrConfiguration();
67 25
    }
68
69
    /**
70
     * Sets the currently active parent plugin.
71
     *
72
     * @param CommandPluginBase $parentPlugin Currently active parent plugin
73
     */
74 25
    public function setParentPlugin(CommandPluginBase $parentPlugin)
75
    {
76 25
        $this->parentPlugin = $parentPlugin;
77 25
    }
78
79
    /**
80
     * Modifies the search form by providing hidden form fields to transfer
81
     * parameters to a news search.
82
     *
83
     * @param array $markers An array of existing form markers
84
     * @param Template $template An instance of the template engine
85
     * @return array Array with additional markers for suggestions
86
     */
87 25
    public function modifyForm(array $markers, Template $template)
88
    {
89 25
        $hiddenFields = [];
90
91 25
        if ($this->parentPlugin instanceof Results && $this->configuration->getSearchKeepExistingParametersForNewSearches()) {
92 23
            foreach ($this->parentPlugin->piVars as $key => $value) {
93 2
                if ($key == 'page') {
94
                    // must reset page
95 1
                    continue;
96
                }
97
98 1
                $name = $this->parentPlugin->prefixId . '[' . $this->cleanFormValue($key) . ']';
99
100 1
                if (is_array($value)) {
101 1
                    foreach ($value as $k => $v) {
102 1
                        $hiddenFields[] = '<input type="hidden" name="' . $name . '[' . $this->cleanFormValue($k) . ']" value="' . $this->cleanFormValue($v) . '" />';
103
                    }
104
                } else {
105 1
                    $hiddenFields[] = '<input type="hidden" name="' . $name . '" value="' . $this->cleanFormValue($value) . '" />';
106
                }
107
            }
108
        }
109
110 25
        $markers['hidden_parameter_fields'] = implode("\n", $hiddenFields);
111
112 25
        return $markers;
113
    }
114
115
    /**
116
     * Cleans a form value that needs to be carried over to the next request
117
     * from potential XSS.
118
     *
119
     * @param string $value Possibly malicious form field value
120
     * @return string Cleaned value
121
     */
122 1
    private function cleanFormValue($value)
123
    {
124 1
        $value = urldecode($value);
125
126 1
        $value = filter_var(strip_tags($value), FILTER_SANITIZE_STRING);
127 1
        $value = GeneralUtility::removeXSS($value);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Core\Utility\GeneralUtility::removeXSS() has been deprecated with message: since TYPO3 v8, will be removed in TYPO3 v9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
128
129 1
        return urlencode($value);
130
    }
131
}
132