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
2 years 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
2 years 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
2 years ago
Translation
2 years ago
Twig
2 years ago
UpdateCheck
2 years ago
Updater
2 years ago
Updates
2 years 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
2 years ago
DbHelper.php
2 years 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
2 years ago
Version.php
2 years ago
View.php
2 years ago
bootstrap.php
2 years ago
dispatch.php
2 years ago
testMinimumPhpVersion.php
2 years ago
Request.php
281 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | namespace Piwik; |
| 11 | |
| 12 | use InvalidArgumentException; |
| 13 | /** |
| 14 | * Provides (type safe) access methods for request parameters. |
| 15 | * |
| 16 | * Ensure to handle parameters received with this class with care. |
| 17 | * Especially parameters received as string, array or json might contain malicious content. Those should never be used |
| 18 | * raw in templates or other output. |
| 19 | * |
| 20 | * Note: For security reasons this class will automatically remove null byte sequences from string values. |
| 21 | * |
| 22 | * @api |
| 23 | */ |
| 24 | class Request |
| 25 | { |
| 26 | /** |
| 27 | * @var array |
| 28 | */ |
| 29 | protected $requestParameters; |
| 30 | private static $exceptionMsg = "The parameter '%s' isn't set in the Request and a default value wasn't provided."; |
| 31 | public function __construct(array $requestParameters) |
| 32 | { |
| 33 | $this->requestParameters = $requestParameters; |
| 34 | } |
| 35 | /** |
| 36 | * Creates a request object using GET and POST parameters of the current request |
| 37 | * |
| 38 | * @return static |
| 39 | */ |
| 40 | public static function fromRequest() : self |
| 41 | { |
| 42 | return new self($_GET + $_POST); |
| 43 | } |
| 44 | /** |
| 45 | * Creates a request object using only GET parameters of the current request |
| 46 | * |
| 47 | * @return static |
| 48 | */ |
| 49 | public static function fromGet() : self |
| 50 | { |
| 51 | return new self($_GET); |
| 52 | } |
| 53 | /** |
| 54 | * Creates a request object using only POST parameters of the current request |
| 55 | * |
| 56 | * @return static |
| 57 | */ |
| 58 | public static function fromPost() : self |
| 59 | { |
| 60 | return new self($_POST); |
| 61 | } |
| 62 | /** |
| 63 | * Creates a request object using the parameters that can be extracted from the provided query string |
| 64 | * |
| 65 | * @return static |
| 66 | */ |
| 67 | public static function fromQueryString(string $queryString) : self |
| 68 | { |
| 69 | $requestParameters = []; |
| 70 | parse_str($queryString, $requestParameters); |
| 71 | // If a querystring is provided urlencode'd parse_str will not be able to parse it correctly. |
| 72 | // A querystring like `method%3dVisitsSummary.get%26idSite%3d1` would result in |
| 73 | // an array like `['method=VisitsSummary.get&idSite=1' => '']` |
| 74 | // In this case we try to parse the urldecode'd string to get proper results |
| 75 | // Note: We can't always perform a urldecode, as this might otherwise destroy urlencoded values containing a & |
| 76 | if (1 === count($requestParameters) && '' === end($requestParameters)) { |
| 77 | $requestParameters = []; |
| 78 | parse_str(urldecode($queryString), $requestParameters); |
| 79 | } |
| 80 | return new self($requestParameters); |
| 81 | } |
| 82 | /** |
| 83 | * Returns the requested parameter from the request object. |
| 84 | * If the requested parameter can't be found and no default is provided an exception will be thrown |
| 85 | * |
| 86 | * Note: It's recommend to use one of type-safe methods instead, if a certain type is expected: |
| 87 | * @see getIntegerParameter |
| 88 | * @see getFloatParameter |
| 89 | * @see getStringParameter |
| 90 | * @see getArrayParameter |
| 91 | * @see getJSONParameter |
| 92 | * |
| 93 | * @param string $name |
| 94 | * @param mixed $default |
| 95 | * @return mixed |
| 96 | * @throws InvalidArgumentException |
| 97 | */ |
| 98 | public function getParameter(string $name, $default = null) |
| 99 | { |
| 100 | if (!strlen($name)) { |
| 101 | throw new InvalidArgumentException('Invalid request parameter. Parameter name required.'); |
| 102 | } |
| 103 | if (array_key_exists($name, $this->requestParameters) && $this->requestParameters[$name] !== null) { |
| 104 | return $this->filterNullBytes($this->requestParameters[$name]); |
| 105 | } |
| 106 | if (null !== $default) { |
| 107 | return $default; |
| 108 | } |
| 109 | throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name)); |
| 110 | } |
| 111 | /** |
| 112 | * Returns the requested parameter from the request object. |
| 113 | * If no default is provided and the requested parameter either can't be found or is not of type integer an |
| 114 | * exception will be thrown |
| 115 | * |
| 116 | * @param string $name |
| 117 | * @param int|null $default |
| 118 | * @return int |
| 119 | * @throws InvalidArgumentException |
| 120 | */ |
| 121 | public function getIntegerParameter(string $name, ?int $default = null) : int |
| 122 | { |
| 123 | $parameter = $this->getParameter($name, $default); |
| 124 | if ((is_string($parameter) || is_numeric($parameter)) && (string) $parameter === (string) (int) $parameter) { |
| 125 | return (int) $parameter; |
| 126 | } |
| 127 | if (null !== $default) { |
| 128 | return $default; |
| 129 | } |
| 130 | throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name)); |
| 131 | } |
| 132 | /** |
| 133 | * Returns the requested parameter from the request object. |
| 134 | * If no default is provided and the requested parameter either can't be found or is not of type float an |
| 135 | * exception will be thrown |
| 136 | * |
| 137 | * @param string $name |
| 138 | * @param float|null $default |
| 139 | * @return float |
| 140 | * @throws InvalidArgumentException |
| 141 | */ |
| 142 | public function getFloatParameter(string $name, ?float $default = null) : float |
| 143 | { |
| 144 | $parameter = $this->getParameter($name, $default); |
| 145 | if ((is_string($parameter) || is_numeric($parameter)) && ((string) $parameter === (string) (float) $parameter || (string) $parameter === (string) (int) $parameter)) { |
| 146 | return (float) $parameter; |
| 147 | } |
| 148 | if (null !== $default) { |
| 149 | return $default; |
| 150 | } |
| 151 | throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name)); |
| 152 | } |
| 153 | /** |
| 154 | * Returns the requested parameter from the request object. |
| 155 | * If no default is provided and the requested parameter either can't be found or is not of type string an |
| 156 | * exception will be thrown |
| 157 | * |
| 158 | * @param string $name |
| 159 | * @param string|null $default |
| 160 | * @return string |
| 161 | * @throws InvalidArgumentException |
| 162 | */ |
| 163 | public function getStringParameter(string $name, ?string $default = null) : string |
| 164 | { |
| 165 | $parameter = $this->getParameter($name, $default); |
| 166 | if (is_string($parameter) || is_numeric($parameter)) { |
| 167 | return $this->filterNullBytes((string) $parameter); |
| 168 | } |
| 169 | if (null !== $default) { |
| 170 | return $default; |
| 171 | } |
| 172 | throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name)); |
| 173 | } |
| 174 | /** |
| 175 | * Returns the requested parameter from the request object. |
| 176 | * If no default is provided and the requested parameter either can't be found or can't be converted to boolean |
| 177 | * exception will be thrown |
| 178 | * |
| 179 | * Values accepted as bool-ish: |
| 180 | * true: true, 'true', '1', 1 |
| 181 | * false: false, 'false', '0', 0 |
| 182 | * |
| 183 | * @param string $name |
| 184 | * @param bool|null $default |
| 185 | * @return bool |
| 186 | * @throws InvalidArgumentException |
| 187 | */ |
| 188 | public function getBoolParameter(string $name, ?bool $default = null) : bool |
| 189 | { |
| 190 | $parameter = $this->getParameter($name, $default); |
| 191 | if ($parameter === false || $parameter === true) { |
| 192 | return $parameter; |
| 193 | } |
| 194 | if (\is_string($parameter) && \strtolower($parameter) === 'false' || $parameter === '0' || $parameter === 0) { |
| 195 | return false; |
| 196 | } |
| 197 | if (\is_string($parameter) && \strtolower($parameter) === 'true' || $parameter === '1' || $parameter === 1) { |
| 198 | return true; |
| 199 | } |
| 200 | if (null !== $default) { |
| 201 | return $default; |
| 202 | } |
| 203 | throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name)); |
| 204 | } |
| 205 | /** |
| 206 | * Returns the requested parameter from the request object. |
| 207 | * If no default is provided and the requested parameter either can't be found or is not of type array an |
| 208 | * exception will be thrown |
| 209 | * |
| 210 | * @param string $name |
| 211 | * @param array|null $default |
| 212 | * @return array |
| 213 | * @throws InvalidArgumentException |
| 214 | */ |
| 215 | public function getArrayParameter(string $name, ?array $default = null) : array |
| 216 | { |
| 217 | $parameter = $this->getParameter($name, $default); |
| 218 | if (is_array($parameter)) { |
| 219 | return $this->filterNullBytes($parameter); |
| 220 | } |
| 221 | if (null !== $default) { |
| 222 | return $default; |
| 223 | } |
| 224 | throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name)); |
| 225 | } |
| 226 | /** |
| 227 | * Returns the requested parameter from the request object. |
| 228 | * If no default is provided and the requested parameter either can't be found or can't be json_decode'd an |
| 229 | * exception will be thrown |
| 230 | * |
| 231 | * @param string $name |
| 232 | * @param mixed $default |
| 233 | * @return mixed |
| 234 | * @throws InvalidArgumentException |
| 235 | */ |
| 236 | public function getJsonParameter(string $name, $default = null) |
| 237 | { |
| 238 | try { |
| 239 | // Note we can't simply pass the default to getParameter here, in case the default would be string |
| 240 | // we would otherwise try to parse it as json below, which might result in unexpected behavior |
| 241 | $parameter = $this->getParameter($name); |
| 242 | } catch (InvalidArgumentException $e) { |
| 243 | $parameter = null; |
| 244 | if ($default !== null) { |
| 245 | return $default; |
| 246 | } |
| 247 | } |
| 248 | if (is_string($parameter)) { |
| 249 | $decodedValue = \json_decode($parameter, true); |
| 250 | if ($decodedValue !== null && $decodedValue !== '') { |
| 251 | return $this->filterNullBytes($decodedValue); |
| 252 | } |
| 253 | } |
| 254 | if (null !== $default) { |
| 255 | return $default; |
| 256 | } |
| 257 | throw new InvalidArgumentException(sprintf(self::$exceptionMsg, $name)); |
| 258 | } |
| 259 | private function filterNullBytes($value) |
| 260 | { |
| 261 | if (is_array($value)) { |
| 262 | $result = []; |
| 263 | foreach ($value as $key => $arrayValue) { |
| 264 | $result[$key] = $this->filterNullBytes($arrayValue); |
| 265 | } |
| 266 | return $result; |
| 267 | } else { |
| 268 | return is_string($value) ? \Piwik\Common::sanitizeNullBytes($value) : $value; |
| 269 | } |
| 270 | } |
| 271 | /** |
| 272 | * Returns an array containing all parameters of the request object |
| 273 | * |
| 274 | * @return array |
| 275 | */ |
| 276 | public function getParameters() : array |
| 277 | { |
| 278 | return $this->requestParameters; |
| 279 | } |
| 280 | } |
| 281 |