PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.1.4
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.1.4
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Request.php
matomo / app / core Last commit date
API 2 years ago Access 2 years ago Application 2 years ago Archive 2 years ago ArchiveProcessor 2 years ago Archiver 2 years ago AssetManager 2 years ago Auth 2 years ago Category 2 years ago Changes 2 years ago CliMulti 2 years ago Columns 2 years ago Concurrency 2 years ago Config 2 years ago Container 2 years ago CronArchive 2 years ago DataAccess 2 years ago DataFiles 2 years ago DataTable 2 years ago Db 1 year ago DeviceDetector 2 years ago Email 2 years ago Exception 2 years ago Http 2 years ago Intl 2 years ago Log 2 years ago Mail 2 years ago Measurable 2 years ago Menu 2 years ago Metrics 2 years ago Notification 2 years ago Period 1 year ago Plugin 2 years ago ProfessionalServices 2 years ago Report 2 years ago ReportRenderer 2 years ago Scheduler 2 years ago Segment 2 years ago Session 2 years ago Settings 2 years ago Tracker 1 year ago Translation 2 years ago Twig 2 years ago UpdateCheck 2 years ago Updater 1 year ago Updates 1 year ago Validators 2 years ago View 2 years ago ViewDataTable 2 years ago Visualization 2 years ago Widget 2 years ago .htaccess 2 years ago Access.php 2 years ago Archive.php 2 years ago ArchiveProcessor.php 2 years ago AssetManager.php 2 years ago Auth.php 2 years ago AuthResult.php 2 years ago BaseFactory.php 2 years ago Cache.php 2 years ago CacheId.php 2 years ago CliMulti.php 2 years ago Common.php 2 years ago Config.php 2 years ago Console.php 2 years ago Context.php 2 years ago Cookie.php 2 years ago CronArchive.php 2 years ago DI.php 2 years ago DataArray.php 2 years ago DataTable.php 2 years ago Date.php 2 years ago Db.php 1 year ago DbHelper.php 1 year ago Development.php 2 years ago ErrorHandler.php 2 years ago EventDispatcher.php 2 years ago ExceptionHandler.php 2 years ago FileIntegrity.php 2 years ago Filechecks.php 2 years ago Filesystem.php 2 years ago FrontController.php 2 years ago Http.php 2 years ago IP.php 2 years ago Log.php 2 years ago LogDeleter.php 2 years ago Mail.php 2 years ago Metrics.php 2 years ago NoAccessException.php 2 years ago Nonce.php 2 years ago Notification.php 2 years ago NumberFormatter.php 2 years ago Option.php 2 years ago Period.php 2 years ago Piwik.php 2 years ago Plugin.php 2 years ago Profiler.php 2 years ago ProxyHeaders.php 2 years ago ProxyHttp.php 2 years ago QuickForm2.php 2 years ago RankingQuery.php 2 years ago ReportRenderer.php 2 years ago Request.php 2 years ago Segment.php 2 years ago Sequence.php 2 years ago Session.php 2 years ago SettingsPiwik.php 2 years ago SettingsServer.php 2 years ago Singleton.php 2 years ago Site.php 2 years ago SiteContentDetector.php 2 years ago SupportedBrowser.php 2 years ago TCPDF.php 2 years ago Theme.php 2 years ago Timer.php 2 years ago Tracker.php 2 years ago Twig.php 2 years ago Unzip.php 2 years ago UpdateCheck.php 2 years ago Updater.php 2 years ago UpdaterErrorException.php 2 years ago Updates.php 2 years ago Url.php 2 years ago UrlHelper.php 1 year ago Version.php 1 year ago View.php 2 years ago bootstrap.php 2 years ago dispatch.php 2 years ago testMinimumPhpVersion.php 2 years ago
Request.php
286 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik;
10
11 use InvalidArgumentException;
12 /**
13 * Provides (type safe) access methods for request parameters.
14 *
15 * Ensure to handle parameters received with this class with care.
16 * Especially parameters received as string, array or json might contain malicious content. Those should never be used
17 * raw in templates or other output.
18 *
19 * Note: For security reasons this class will automatically remove null byte sequences from string values.
20 *
21 * @api
22 */
23 class Request
24 {
25 /**
26 * @var array
27 */
28 protected $requestParameters;
29 private static $exceptionMsg = "The parameter '%s' isn't set in the Request and a default value wasn't provided.";
30 public function __construct(array $requestParameters)
31 {
32 $this->requestParameters = $requestParameters;
33 }
34 /**
35 * Creates a request object using GET and POST parameters of the current request
36 *
37 * @return static
38 */
39 public static function fromRequest() : self
40 {
41 return new self($_GET + $_POST);
42 }
43 /**
44 * Creates a request object using only GET parameters of the current request
45 *
46 * @return static
47 */
48 public static function fromGet() : self
49 {
50 return new self($_GET);
51 }
52 /**
53 * Creates a request object using only POST parameters of the current request
54 *
55 * @return static
56 */
57 public static function fromPost() : self
58 {
59 return new self($_POST);
60 }
61 /**
62 * Creates a request object using the parameters that can be extracted from the provided query string
63 *
64 * @return static
65 */
66 public static function fromQueryString(string $queryString) : self
67 {
68 $requestParameters = [];
69 parse_str($queryString, $requestParameters);
70 // If a querystring is provided urlencode'd parse_str will not be able to parse it correctly.
71 // A querystring like `method%3dVisitsSummary.get%26idSite%3d1` would result in
72 // an array like `['method=VisitsSummary.get&idSite=1' => '']`
73 // In this case we try to parse the urldecode'd string to get proper results
74 // Note: We can't always perform a urldecode, as this might otherwise destroy urlencoded values containing a &
75 if (1 === count($requestParameters) && '' === end($requestParameters)) {
76 $requestParameters = [];
77 parse_str(urldecode($queryString), $requestParameters);
78 }
79 return new self($requestParameters);
80 }
81 /**
82 * Returns the requested parameter from the request object.
83 * If the requested parameter can't be found and no default is provided an exception will be thrown
84 *
85 * Note: It's recommend to use one of type-safe methods instead, if a certain type is expected:
86 * @see getIntegerParameter
87 * @see getFloatParameter
88 * @see getStringParameter
89 * @see getArrayParameter
90 * @see getJSONParameter
91 *
92 * @param string $name
93 * @param mixed $default
94 * @return mixed
95 * @throws InvalidArgumentException
96 */
97 public function getParameter(string $name, $default = null)
98 {
99 if (!strlen($name)) {
100 throw new InvalidArgumentException('Invalid request parameter. Parameter name required.');
101 }
102 if (array_key_exists($name, $this->requestParameters) && $this->requestParameters[$name] !== null) {
103 return $this->filterNullBytes($this->requestParameters[$name]);
104 }
105 if (null !== $default) {
106 return $default;
107 }
108 throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name));
109 }
110 /**
111 * Returns the requested parameter from the request object.
112 * If no default is provided and the requested parameter either can't be found or is not of type integer an
113 * exception will be thrown
114 *
115 * @param string $name
116 * @param int|null $default
117 * @return int
118 * @throws InvalidArgumentException
119 */
120 public function getIntegerParameter(string $name, ?int $default = null) : int
121 {
122 $parameter = $this->getParameter($name, $default);
123 if ((is_string($parameter) || is_numeric($parameter)) && (string) $parameter === (string) (int) $parameter) {
124 return (int) $parameter;
125 }
126 if (null !== $default) {
127 return $default;
128 }
129 throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name));
130 }
131 /**
132 * Returns the requested parameter from the request object.
133 * If no default is provided and the requested parameter either can't be found or is not of type float an
134 * exception will be thrown
135 *
136 * @param string $name
137 * @param float|null $default
138 * @return float
139 * @throws InvalidArgumentException
140 */
141 public function getFloatParameter(string $name, ?float $default = null) : float
142 {
143 $parameter = $this->getParameter($name, $default);
144 if (is_float($parameter) || is_int($parameter)) {
145 return (float) $parameter;
146 }
147 // Regex for all supported float notations in PHP (see https://www.php.net/manual/en/language.types.float.php)
148 $floatRegex = "/^[-+]?((([0-9]+(_[0-9]+)*)|(([0-9]+(_[0-9]+)*)?\\.([0-9]+(_[0-9]+)*))|(([0-9]+(_[0-9]+)*)\\.([0-9]+(_[0-9]+)*)?))([eE][+-]?([0-9]+(_[0-9]+)*))?)\$/";
149 if (is_string($parameter) && preg_match($floatRegex, $parameter)) {
150 // underscores would break numbers if not removed before
151 return (float) str_replace('_', '', $parameter);
152 }
153 if (null !== $default) {
154 return $default;
155 }
156 throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name));
157 }
158 /**
159 * Returns the requested parameter from the request object.
160 * If no default is provided and the requested parameter either can't be found or is not of type string an
161 * exception will be thrown
162 *
163 * @param string $name
164 * @param string|null $default
165 * @return string
166 * @throws InvalidArgumentException
167 */
168 public function getStringParameter(string $name, ?string $default = null) : string
169 {
170 $parameter = $this->getParameter($name, $default);
171 if (is_string($parameter) || is_numeric($parameter)) {
172 return $this->filterNullBytes((string) $parameter);
173 }
174 if (null !== $default) {
175 return $default;
176 }
177 throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name));
178 }
179 /**
180 * Returns the requested parameter from the request object.
181 * If no default is provided and the requested parameter either can't be found or can't be converted to boolean
182 * exception will be thrown
183 *
184 * Values accepted as bool-ish:
185 * true: true, 'true', '1', 1
186 * false: false, 'false', '0', 0
187 *
188 * @param string $name
189 * @param bool|null $default
190 * @return bool
191 * @throws InvalidArgumentException
192 */
193 public function getBoolParameter(string $name, ?bool $default = null) : bool
194 {
195 $parameter = $this->getParameter($name, $default);
196 if ($parameter === false || $parameter === true) {
197 return $parameter;
198 }
199 if (\is_string($parameter) && \strtolower($parameter) === 'false' || $parameter === '0' || $parameter === 0) {
200 return false;
201 }
202 if (\is_string($parameter) && \strtolower($parameter) === 'true' || $parameter === '1' || $parameter === 1) {
203 return true;
204 }
205 if (null !== $default) {
206 return $default;
207 }
208 throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name));
209 }
210 /**
211 * Returns the requested parameter from the request object.
212 * If no default is provided and the requested parameter either can't be found or is not of type array an
213 * exception will be thrown
214 *
215 * @param string $name
216 * @param array|null $default
217 * @return array
218 * @throws InvalidArgumentException
219 */
220 public function getArrayParameter(string $name, ?array $default = null) : array
221 {
222 $parameter = $this->getParameter($name, $default);
223 if (is_array($parameter)) {
224 return $this->filterNullBytes($parameter);
225 }
226 if (null !== $default) {
227 return $default;
228 }
229 throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name));
230 }
231 /**
232 * Returns the requested parameter from the request object.
233 * If no default is provided and the requested parameter either can't be found or can't be json_decode'd an
234 * exception will be thrown
235 *
236 * @param string $name
237 * @param mixed $default
238 * @return mixed
239 * @throws InvalidArgumentException
240 */
241 public function getJsonParameter(string $name, $default = null)
242 {
243 try {
244 // Note we can't simply pass the default to getParameter here, in case the default would be string
245 // we would otherwise try to parse it as json below, which might result in unexpected behavior
246 $parameter = $this->getParameter($name);
247 } catch (InvalidArgumentException $e) {
248 $parameter = null;
249 if ($default !== null) {
250 return $default;
251 }
252 }
253 if (is_string($parameter)) {
254 $decodedValue = \json_decode($parameter, true);
255 if ($decodedValue !== null && $decodedValue !== '') {
256 return $this->filterNullBytes($decodedValue);
257 }
258 }
259 if (null !== $default) {
260 return $default;
261 }
262 throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name));
263 }
264 private function filterNullBytes($value)
265 {
266 if (is_array($value)) {
267 $result = [];
268 foreach ($value as $key => $arrayValue) {
269 $result[$key] = $this->filterNullBytes($arrayValue);
270 }
271 return $result;
272 } else {
273 return is_string($value) ? \Piwik\Common::sanitizeNullBytes($value) : $value;
274 }
275 }
276 /**
277 * Returns an array containing all parameters of the request object
278 *
279 * @return array
280 */
281 public function getParameters() : array
282 {
283 return $this->requestParameters;
284 }
285 }
286