ParameterReader   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 22
dl 0
loc 107
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parseUrl() 0 3 1
A decodeStr() 0 8 2
A decoderUtf8Str() 0 14 3
A getRequestParameter() 0 7 4
A decodeRequestParameter() 0 9 2
A uri() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * ParameterReader.php
5
 *
6
 * This class reads the input arguments from the GET or POST data of the request.
7
 *
8
 * @package jaxon-core
9
 * @author Jared White
10
 * @author J. Max Wilson
11
 * @author Joseph Woolley
12
 * @author Steffen Konerow
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-core
19
 */
20
21
namespace Jaxon\Request\Handler;
22
23
use Jaxon\App\Config\ConfigManager;
24
use Jaxon\App\I18n\Translator;
25
use Jaxon\Di\Container;
26
use Jaxon\Exception\RequestException;
27
use Jaxon\Utils\Http\UriDetector;
28
use Jaxon\Utils\Http\UriException;
29
use Psr\Http\Message\ServerRequestInterface;
30
31
use function function_exists;
32
use function iconv;
33
use function is_array;
34
use function is_string;
35
use function json_decode;
36
use function mb_convert_encoding;
37
use function strlen;
38
use function strncmp;
39
use function urldecode;
40
41
class ParameterReader
42
{
43
    /**
44
     * The constructor
45
     *
46
     * @param Container $di
47
     * @param Translator $xTranslator
48
     * @param ConfigManager $xConfigManager
49
     * @param UriDetector $xUriDetector
50
     */
51
    public function __construct(private Container $di, private Translator $xTranslator,
52
        private ConfigManager $xConfigManager, private UriDetector $xUriDetector)
53
    {}
54
55
    /**
56
     * Decode input data.
57
     *
58
     * @param string $sStr
59
     *
60
     * @return string
61
     */
62
    private function decodeStr(string $sStr): string
63
    {
64
        $aServerParams = $this->di->getServerParams();
65
        $sContentType = $aServerParams['CONTENT_TYPE'] ?? $aServerParams['HTTP_CONTENT_TYPE'] ?? '';
66
        $sType = 'multipart/form-data';
67
        // Parameters are url encoded when uploading files
68
        return strncmp($sContentType, $sType, strlen($sType)) !== 0 ?
69
            $sStr : urldecode($sStr);
70
    }
71
72
    /**
73
     * Decode input data.
74
     *
75
     * @param string $sStr
76
     *
77
     * @return string
78
     * @throws RequestException
79
     */
80
    private function decoderUtf8Str(string $sStr): string
81
    {
82
        $sEncoding = $this->xConfigManager->getOption('core.encoding', '');
83
        if(function_exists('iconv'))
84
        {
85
            return iconv("UTF-8", $sEncoding . '//TRANSLIT', $sStr);
86
        }
87
        if(function_exists('mb_convert_encoding'))
88
        {
89
            return mb_convert_encoding($sStr, $sEncoding, "UTF-8");
0 ignored issues
show
Bug Best Practice introduced by
The expression return mb_convert_encodi...r, $sEncoding, 'UTF-8') could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
90
        }
91
        // By default, no decoding
92
        // return $sStr;
93
        throw new RequestException($this->xTranslator->trans('errors.request.conversion'));
94
    }
95
96
    /**
97
     * Choose the function to use to decode input data.
98
     *
99
     * @param string $sParam
100
     *
101
     * @return string
102
     */
103
    private function decodeRequestParameter(string $sParam): string
104
    {
105
        $sParam = $this->decodeStr($sParam);
106
        if(!$this->xConfigManager->getOption('core.decode_utf8'))
107
        {
108
            return $sParam;
109
        }
110
        $this->xConfigManager->setOption('core.decode_utf8', false);
111
        return $this->decoderUtf8Str($sParam);
112
    }
113
114
    /**
115
     * @param ServerRequestInterface $xRequest
116
     *
117
     * @return array|null
118
     */
119
    public function getRequestParameter(ServerRequestInterface $xRequest): ?array
120
    {
121
        $aBody = $xRequest->getParsedBody();
122
        $aParams = is_array($aBody) ? $aBody : $xRequest->getQueryParams();
123
        // Check if Jaxon call parameters are present.
124
        return !isset($aParams['jxncall']) || !is_string($aParams['jxncall']) ? null :
125
            json_decode($this->decodeRequestParameter($aParams['jxncall']), true);
126
    }
127
128
    /**
129
     * Get the URI of the current request
130
     *
131
     * @throws UriException
132
     */
133
    public function uri(): string
134
    {
135
        return $this->xUriDetector->detect($this->di->getServerParams());
136
    }
137
138
    /**
139
     * Make the specified URL suitable for redirect
140
     *
141
     * @param string $sURL    The relative or fully qualified URL
142
     *
143
     * @return string
144
     */
145
    public function parseUrl(string $sURL): string
146
    {
147
        return $this->xUriDetector->redirect($sURL, $this->di->getServerParams());
148
    }
149
}
150