| Total Complexity | 83 |
| Total Lines | 551 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like InputValidation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InputValidation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class InputValidation extends \core\common\Entity |
||
| 31 | { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * returns a simple HTML <p> element with basic explanations about what was |
||
| 35 | * wrong with the input |
||
| 36 | * |
||
| 37 | * @param string $customtext explanation provided by the validator function |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | private function inputValidationError($customtext) |
||
| 41 | { |
||
| 42 | \core\common\Entity::intoThePotatoes(); |
||
| 43 | $retval = "<p>" . _("Input validation error: ") . $customtext . "</p>"; |
||
| 44 | \core\common\Entity::outOfThePotatoes(); |
||
| 45 | return $retval; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Is this a known Federation? Optionally, also check if the authenticated |
||
| 50 | * user is a federation admin of that federation |
||
| 51 | * @param mixed $input the ISO code of the federation |
||
| 52 | * @param string|NULL $owner the authenticated username, optional |
||
| 53 | * @return \core\Federation |
||
| 54 | * @throws Exception |
||
| 55 | */ |
||
| 56 | public function existingFederation($input, $owner = NULL) |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Is this a known IdP? Optionally, also check if the authenticated |
||
| 83 | * user is an admin of that IdP |
||
| 84 | * @param mixed $input the numeric ID of the IdP in the system |
||
| 85 | * @param string $owner the authenticated username, optional |
||
| 86 | * @param \core\Federation $claimedFedBinding if set, cross-check that IdP belongs to specified federation (useful in admin API mode) |
||
| 87 | * @return \core\IdP |
||
| 88 | * @throws Exception |
||
| 89 | */ |
||
| 90 | public function existingIdP($input, $owner = NULL, $claimedFedBinding = NULL) |
||
| 91 | { |
||
| 92 | $clean = $this->integer($input); |
||
| 93 | if ($clean === FALSE) { |
||
| 94 | throw new Exception($this->inputValidationError("Value for IdP is not an integer!")); |
||
| 95 | } |
||
| 96 | |||
| 97 | $temp = new \core\IdP($input); // constructor throws an exception if NX, game over |
||
| 98 | |||
| 99 | if ($owner !== NULL) { // check if the authenticated user is allowed to see this institution |
||
| 100 | foreach ($temp->listOwners() as $oneowner) { |
||
| 101 | if ($oneowner['ID'] == $owner) { |
||
| 102 | return $temp; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | throw new Exception($this->inputValidationError("This IdP identifier is not accessible!")); |
||
| 106 | } |
||
| 107 | if ($claimedFedBinding !== NULL && strtoupper($temp->federation) != strtoupper($claimedFedBinding->tld)) { |
||
| 108 | throw new Exception($this->inputValidationError("This IdP does not belong to the claimed federation!")); |
||
| 109 | } |
||
| 110 | return $temp; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Checks if the input refers to a known Profile. Optionally also takes an |
||
| 115 | * IdP identifier and then checks if the Profile belongs to the refernced |
||
| 116 | * IdP |
||
| 117 | * |
||
| 118 | * @param mixed $input the numeric ID of the Profile in the system |
||
| 119 | * @param int|NULL $idpIdentifier the numeric ID of the IdP in the system, optional |
||
| 120 | * @return \core\AbstractProfile |
||
| 121 | * @throws Exception |
||
| 122 | */ |
||
| 123 | public function existingProfile($input, $idpIdentifier = NULL) |
||
| 124 | { |
||
| 125 | $clean = $this->integer($input); |
||
| 126 | if ($clean === FALSE) { |
||
| 127 | throw new Exception("Non-integer was passed to Profile validator!"); |
||
| 128 | } |
||
| 129 | $temp = \core\ProfileFactory::instantiate($clean); // constructor throws an exception if NX, game over |
||
| 130 | |||
| 131 | if ($idpIdentifier !== NULL && $temp->institution != $idpIdentifier) { |
||
| 132 | throw new Exception($this->inputValidationError("The profile does not belong to the IdP!")); |
||
| 133 | } |
||
| 134 | return $temp; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Checks if the input refers to a known DeploymentManaged. Optionally also takes an |
||
| 139 | * IdP identifier and then checks if the Profile belongs to the refernced |
||
| 140 | * IdP |
||
| 141 | * |
||
| 142 | * @param mixed $input the numeric ID of the Deployment in the system |
||
| 143 | * @param \core\IdP $idp the IdP |
||
| 144 | * @return \core\DeploymentManaged |
||
| 145 | * @throws Exception |
||
| 146 | */ |
||
| 147 | public function existingDeploymentManaged($input, $idp) |
||
| 148 | { |
||
| 149 | $clean = $this->integer($input); |
||
| 150 | if ($clean === FALSE) { |
||
| 151 | throw new Exception("Non-integer was passed to Profile validator!"); |
||
| 152 | } |
||
| 153 | $temp = new \core\DeploymentManaged($idp, $clean); // constructor throws an exception if NX, game over |
||
| 154 | |||
| 155 | if ($temp->institution != $idp->identifier) { |
||
| 156 | throw new Exception($this->inputValidationError("The profile does not belong to the IdP!")); |
||
| 157 | } |
||
| 158 | return $temp; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Checks if this is a device known to the system |
||
| 163 | * @param mixed $input the name of the device (index in the Devices.php array) |
||
| 164 | * @return string returns the same string on success, throws an Exception on failure |
||
| 165 | * @throws Exception |
||
| 166 | */ |
||
| 167 | public function existingDevice($input) |
||
| 168 | { |
||
| 169 | $devicelist = \devices\Devices::listDevices(); |
||
| 170 | $keyArray = array_keys($devicelist); |
||
| 171 | if (!isset($devicelist[$input])) { |
||
| 172 | throw new Exception($this->inputValidationError("This device does not exist!")); |
||
| 173 | } |
||
| 174 | $correctIndex = array_search($input, $keyArray); |
||
| 175 | return $keyArray[$correctIndex]; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Checks if the input was a valid string. |
||
| 180 | * |
||
| 181 | * @param mixed $input a string to be made SQL-safe |
||
| 182 | * @param boolean $allowWhitespace whether some whitespace (e.g. newlines should be preserved (true) or redacted (false) |
||
| 183 | * @return string the massaged string |
||
| 184 | * @throws Exception |
||
| 185 | */ |
||
| 186 | public function string($input, $allowWhitespace = FALSE) |
||
| 187 | { |
||
| 188 | // always chop out invalid characters, and surrounding whitespace |
||
| 189 | $retvalStep0 = iconv("UTF-8", "UTF-8//TRANSLIT", $input); |
||
| 190 | if ($retvalStep0 === FALSE) { |
||
| 191 | throw new Exception("iconv failure for string sanitisation. With TRANSLIT, this should never happen!"); |
||
| 192 | } |
||
| 193 | $retvalStep1 = trim($retvalStep0); |
||
| 194 | // if some funny person wants to inject markup tags, remove them |
||
| 195 | $retval = filter_var($retvalStep1, FILTER_SANITIZE_STRING, ["flags" => FILTER_FLAG_NO_ENCODE_QUOTES]); |
||
| 196 | if ($retval === FALSE) { |
||
| 197 | throw new Exception("filter_var failure for string sanitisation."); |
||
| 198 | } |
||
| 199 | // unless explicitly wanted, take away intermediate disturbing whitespace |
||
| 200 | // a simple "space" is NOT disturbing :-) |
||
| 201 | if ($allowWhitespace === FALSE) { |
||
| 202 | $afterWhitespace = preg_replace('/(\0|\r|\x0b|\t|\n)/', '', $retval); |
||
| 203 | } else { |
||
| 204 | // even if we allow whitespace, not pathological ones! |
||
| 205 | $afterWhitespace = preg_replace('/(\0|\r|\x0b)/', '', $retval); |
||
| 206 | } |
||
| 207 | if (is_array($afterWhitespace)) { |
||
| 208 | throw new Exception("This function has to be given a string and returns a string. preg_replace has generated an array instead!"); |
||
| 209 | } |
||
| 210 | return (string) $afterWhitespace; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Is this an integer, or a string that represents an integer? |
||
| 215 | * |
||
| 216 | * @param mixed $input the raw input |
||
| 217 | * @return boolean|int returns the input, or FALSE if it is not an integer-like value |
||
| 218 | */ |
||
| 219 | public function integer($input) |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Is this a string representing a potentially more than 64-Bit length integer? |
||
| 229 | * |
||
| 230 | * @param string $input the input data which is possibly a really large integer |
||
| 231 | * @return boolean|string returns the input, or FALSE if it is not an integer-like string |
||
| 232 | */ |
||
| 233 | public function hugeInteger($input) |
||
| 234 | { |
||
| 235 | if (is_numeric($input)) { |
||
| 236 | return $input; |
||
| 237 | } |
||
| 238 | return FALSE; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Checks if the input is the hex representation of a Consortium OI (i.e. three |
||
| 243 | * or five bytes) |
||
| 244 | * |
||
| 245 | * @param mixed $input the raw input |
||
| 246 | * @return boolean|string returns the input, or FALSE on validation failure |
||
| 247 | */ |
||
| 248 | public function consortiumOI($input) |
||
| 249 | { |
||
| 250 | $shallow = $this->string($input); |
||
| 251 | if (strlen($shallow) != 6 && strlen($shallow) != 10) { |
||
| 252 | return FALSE; |
||
| 253 | } |
||
| 254 | if (!preg_match("/^[a-fA-F0-9]+$/", $shallow)) { |
||
| 255 | return FALSE; |
||
| 256 | } |
||
| 257 | return $shallow; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Is the input an NAI realm? Throws HTML error and returns FALSE if not. |
||
| 262 | * |
||
| 263 | * @param mixed $input the input to check |
||
| 264 | * @return boolean|string returns the realm, or FALSE if it was malformed |
||
| 265 | */ |
||
| 266 | public function realm($input) |
||
| 267 | { |
||
| 268 | \core\common\Entity::intoThePotatoes(); |
||
| 269 | if (strlen($input) == 0) { |
||
| 270 | echo $this->inputValidationError(_("Realm is empty!")); |
||
| 271 | \core\common\Entity::outOfThePotatoes(); |
||
| 272 | return FALSE; |
||
| 273 | } |
||
| 274 | |||
| 275 | // basic string checks |
||
| 276 | $check = $this->string($input); |
||
| 277 | // list of things to check, and the error they produce |
||
| 278 | $pregCheck = [ |
||
| 279 | "/@/" => _("Realm contains an @ sign!"), |
||
| 280 | "/^\./" => _("Realm begins with a . (dot)!"), |
||
| 281 | "/\.$/" => _("Realm ends with a . (dot)!"), |
||
| 282 | "/ /" => _("Realm contains spaces!"), |
||
| 283 | ]; |
||
| 284 | |||
| 285 | // bark on invalid constructs |
||
| 286 | foreach ($pregCheck as $search => $error) { |
||
| 287 | if (preg_match($search, $check) == 1) { |
||
| 288 | echo $this->inputValidationError($error); |
||
| 289 | \core\common\Entity::outOfThePotatoes(); |
||
| 290 | return FALSE; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | if (preg_match("/\./", $check) == 0) { |
||
| 295 | echo $this->inputValidationError(_("Realm does not contain at least one . (dot)!")); |
||
| 296 | \core\common\Entity::outOfThePotatoes(); |
||
| 297 | return FALSE; |
||
| 298 | } |
||
| 299 | |||
| 300 | // none of the special HTML entities should be here. In case someone wants |
||
| 301 | // to mount a CSS attack by providing something that matches the realm constructs |
||
| 302 | // below but has interesting stuff between, mangle the input so that these |
||
| 303 | // characters do not do any harm. |
||
| 304 | \core\common\Entity::outOfThePotatoes(); |
||
| 305 | return htmlentities($check, ENT_QUOTES); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * could this be a valid username? |
||
| 310 | * |
||
| 311 | * Only checks correct form, not if the user actually exists in the system. |
||
| 312 | * |
||
| 313 | * @param mixed $input the username |
||
| 314 | * @return string echoes back the input string, or throws an Exception if bogus |
||
| 315 | * @throws Exception |
||
| 316 | */ |
||
| 317 | public function syntaxConformUser($input) |
||
| 318 | { |
||
| 319 | $retvalStep0 = iconv("UTF-8", "UTF-8//TRANSLIT", $input); |
||
| 320 | if ($retvalStep0 === FALSE) { |
||
| 321 | throw new Exception("iconv failure for string sanitisation. With TRANSLIT, this should never happen!"); |
||
| 322 | } |
||
| 323 | $retvalStep1 = trim($retvalStep0); |
||
| 324 | |||
| 325 | $retval = preg_replace('/(\0|\r|\x0b|\t|\n)/', '', $retvalStep1); |
||
| 326 | if ($retval != "" && !ctype_print($retval)) { |
||
| 327 | throw new Exception($this->inputValidationError("The user identifier is not an ASCII string!")); |
||
| 328 | } |
||
| 329 | |||
| 330 | return $retval; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * could this be a valid token? |
||
| 335 | * |
||
| 336 | * Only checks correct form, not if the token actually exists in the system. |
||
| 337 | * @param mixed $input the raw input |
||
| 338 | * @return string echoes back the input string, or throws an Exception if bogus |
||
| 339 | * @throws Exception |
||
| 340 | */ |
||
| 341 | public function token($input) |
||
| 342 | { |
||
| 343 | $retval = $input; |
||
| 344 | if ($input != "" && preg_match('/[^0-9a-fA-F]/', $input) != 0) { |
||
| 345 | throw new Exception($this->inputValidationError("Token is not a hexadecimal string!")); |
||
| 346 | } |
||
| 347 | return $retval; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Is this be a valid coordinate vector on one axis? |
||
| 352 | * |
||
| 353 | * @param mixed $input a numeric value in range of a geo coordinate [-180;180] |
||
| 354 | * @return string returns back the input if all is good; throws an Exception if out of bounds or not numeric |
||
| 355 | * @throws Exception |
||
| 356 | */ |
||
| 357 | public function coordinate($input) |
||
| 358 | { |
||
| 359 | $oldlocale = setlocale(LC_NUMERIC, 0); |
||
| 360 | setlocale(LC_NUMERIC, "en_GB"); |
||
| 361 | if (!is_numeric($input)) { |
||
| 362 | throw new Exception($this->inputValidationError("Coordinate is not a numeric value!")); |
||
| 363 | } |
||
| 364 | setlocale(LC_NUMERIC, $oldlocale); |
||
| 365 | // lat and lon are always in the range of [-180;+180] |
||
| 366 | if ($input < -180 || $input > 180) { |
||
| 367 | throw new Exception($this->inputValidationError("Coordinate is out of bounds. Which planet are you from?")); |
||
| 368 | } |
||
| 369 | return $input; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Is this a valid coordinate pair in JSON encoded representation? |
||
| 374 | * |
||
| 375 | * @param mixed $input the string to be checked: is this a serialised array with lat/lon keys in a valid number range? |
||
| 376 | * @return string returns $input if checks have passed; throws an Exception if something's wrong |
||
| 377 | * @throws Exception |
||
| 378 | */ |
||
| 379 | public function coordJsonEncoded($input) |
||
| 380 | { |
||
| 381 | $tentative = json_decode($input, true); |
||
| 382 | if (is_array($tentative)) { |
||
| 383 | if (isset($tentative['lon']) && isset($tentative['lat']) && $this->coordinate($tentative['lon']) && $this->coordinate($tentative['lat'])) { |
||
| 384 | return $input; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | throw new Exception($this->inputValidationError("Wrong coordinate encoding (2.0 uses JSON, not serialize)!")); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * This checks the state of a HTML GET/POST "boolean". |
||
| 392 | * |
||
| 393 | * If not checked, no value is submitted at all; if checked, has the word "on". |
||
| 394 | * Anything else is a big error. |
||
| 395 | * |
||
| 396 | * @param mixed $input the string to test |
||
| 397 | * @return boolean TRUE if the input was "on". It is not possible in HTML to signal "off" |
||
| 398 | * @throws Exception |
||
| 399 | */ |
||
| 400 | public function boolean($input) |
||
| 401 | { |
||
| 402 | if ($input != "on") { |
||
| 403 | throw new Exception($this->inputValidationError("Unknown state of boolean option!")); |
||
| 404 | } |
||
| 405 | return TRUE; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * checks if we have the strings "IdP" "SP" or "IdPSP" |
||
| 410 | * |
||
| 411 | * @param string $partTypeRaw the string to be validated as participant type |
||
| 412 | * @return string validated result |
||
| 413 | * @throws Exception |
||
| 414 | */ |
||
| 415 | public function partType($partTypeRaw) |
||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | const TABLEMAPPING = [ |
||
| 430 | "IdP" => "institution_option", |
||
| 431 | "Profile" => "profile_option", |
||
| 432 | "FED" => "federation_option", |
||
| 433 | ]; |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Is this a valid database reference? Has the form <tablename>-<rowID> and there |
||
| 437 | * needs to be actual data at that place |
||
| 438 | * |
||
| 439 | * @param string $input the reference to check |
||
| 440 | * @return boolean|array the reference split up into "table" and "rowindex", or FALSE |
||
| 441 | */ |
||
| 442 | public function databaseReference($input) |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * is this a valid hostname? |
||
| 457 | * |
||
| 458 | * @param mixed $input the raw input |
||
| 459 | * @return boolean|string echoes the hostname, or FALSE if bogus |
||
| 460 | */ |
||
| 461 | public function hostname($input) |
||
| 462 | { |
||
| 463 | // is it a valid IP address (IPv4 or IPv6), or a hostname? |
||
| 464 | if (filter_var($input, FILTER_VALIDATE_IP) || filter_var($input, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { |
||
| 465 | // if it's a verified IP address or hostname then it does not contain |
||
| 466 | // rubbish of course. But just to be sure, run htmlspecialchars around it |
||
| 467 | return htmlspecialchars($input, ENT_QUOTES); |
||
| 468 | } |
||
| 469 | return FALSE; |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * is this a valid email address? |
||
| 474 | * |
||
| 475 | * @param mixed $input the raw input |
||
| 476 | * @return boolean|string echoes the mail address, or FALSE if bogus |
||
| 477 | */ |
||
| 478 | public function email($input) |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * is this a well-formed SMS number? Light massaging - leading + will be removed |
||
| 490 | * @param string $input the raw input |
||
| 491 | * @return boolean|string |
||
| 492 | */ |
||
| 493 | public function sms($input) |
||
| 494 | { |
||
| 495 | $number = str_replace(' ', '', str_replace(".", "", str_replace("+", "", $input))); |
||
| 496 | if (!is_numeric($number)) { |
||
| 497 | return FALSE; |
||
| 498 | } |
||
| 499 | return $number; |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Is this is a language we support? If not, sanitise to our configured default language. |
||
| 504 | * |
||
| 505 | * @param mixed $input the candidate language identifier |
||
| 506 | * @return string |
||
| 507 | * @throws Exception |
||
| 508 | */ |
||
| 509 | public function supportedLanguage($input) |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Makes sure we are not receiving a bogus option name. The called function throws |
||
| 525 | * an assertion if the name is not known. |
||
| 526 | * |
||
| 527 | * @param mixed $input the unvetted option name |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | public function optionName($input) |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Checks to see if the input is a valid image of sorts |
||
| 538 | * |
||
| 539 | * @param mixed $binary blob that may or may not be a parseable image |
||
| 540 | * @return boolean |
||
| 541 | */ |
||
| 542 | public function image($binary) |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * searches for values in GET and POST, and filters the value according to |
||
| 557 | * which kind of data is expected |
||
| 558 | * |
||
| 559 | * @param string $varName name of the variable in GET/POST |
||
| 560 | * @param string $filter which type of filter to apply (safe_text / int) |
||
| 561 | * @return NULL|string|integer the returned value |
||
| 562 | */ |
||
| 563 | public function simpleInputFilter($varName, $filter) |
||
| 581 | } |
||
| 582 | |||
| 583 | } |
||
| 584 |