| Total Complexity | 72 |
| Total Lines | 468 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 29 | class InputValidation extends \core\common\Entity { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * returns a simple HTML <p> element with basic explanations about what was |
||
| 33 | * wrong with the input |
||
| 34 | * |
||
| 35 | * @param string $customtext explanation provided by the validator function |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | private function inputValidationError($customtext) { |
||
| 39 | \core\common\Entity::intoThePotatoes(); |
||
| 40 | $retval = "<p>" . _("Input validation error: ") . $customtext . "</p>"; |
||
| 41 | \core\common\Entity::outOfThePotatoes(); |
||
| 42 | return $retval; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Is this a known Federation? Optionally, also check if the authenticated |
||
| 47 | * user is a federation admin of that federation |
||
| 48 | * @param mixed $input the ISO code of the federation |
||
| 49 | * @param string|NULL $owner the authenticated username, optional |
||
| 50 | * @return \core\Federation |
||
| 51 | * @throws Exception |
||
| 52 | */ |
||
| 53 | public function Federation($input, $owner = NULL) { |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Is this a known IdP? Optionally, also check if the authenticated |
||
| 79 | * user is an admin of that IdP |
||
| 80 | * @param mixed $input the numeric ID of the IdP in the system |
||
| 81 | * @param string $owner the authenticated username, optional |
||
| 82 | * @return \core\IdP |
||
| 83 | * @throws Exception |
||
| 84 | */ |
||
| 85 | public function IdP($input, $owner = NULL) { |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Checks if the input refers to a known Profile. Optionally also takes an |
||
| 106 | * IdP identifier and then checks if the Profile belongs to the refernced |
||
| 107 | * IdP |
||
| 108 | * |
||
| 109 | * @param mixed $input the numeric ID of the Profile in the system |
||
| 110 | * @param int|NULL $idpIdentifier the numeric ID of the IdP in the system, optional |
||
| 111 | * @return \core\AbstractProfile |
||
| 112 | * @throws Exception |
||
| 113 | */ |
||
| 114 | public function Profile($input, $idpIdentifier = NULL) { |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Checks if the input refers to a known DeploymentManaged. Optionally also takes an |
||
| 129 | * IdP identifier and then checks if the Profile belongs to the refernced |
||
| 130 | * IdP |
||
| 131 | * |
||
| 132 | * @param mixed $input the numeric ID of the Deployment in the system |
||
| 133 | * @param int $idpIdentifier the numeric ID of the IdP in the system |
||
| 134 | * @return \core\DeploymentManaged |
||
| 135 | * @throws Exception |
||
| 136 | */ |
||
| 137 | public function DeploymentManaged($input, $idpIdentifier) { |
||
| 138 | $clean = $this->integer($input); |
||
| 139 | if ($clean === FALSE) { |
||
| 140 | throw new Exception("Non-integer was passed to Profile validator!"); |
||
| 141 | } |
||
| 142 | $temp = new \core\DeploymentManaged($idpIdentifier, $clean); // constructor throws an exception if NX, game over |
||
| 143 | |||
| 144 | if ($temp->institution != $idpIdentifier) { |
||
| 145 | throw new Exception($this->inputValidationError("The profile does not belong to the IdP!")); |
||
| 146 | } |
||
| 147 | return $temp; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Checks if this is a device known to the system |
||
| 152 | * @param mixed $input the name of the device (index in the Devices.php array) |
||
| 153 | * @return string returns the same string on success, throws an Exception on failure |
||
| 154 | * @throws Exception |
||
| 155 | */ |
||
| 156 | public function Device($input) { |
||
| 157 | $devicelist = \devices\Devices::listDevices(); |
||
| 158 | $keyArray = array_keys($devicelist); |
||
| 159 | if (!isset($devicelist[$input])) { |
||
| 160 | throw new Exception($this->inputValidationError("This device does not exist!")); |
||
| 161 | } |
||
| 162 | $correctIndex = array_search($input, $keyArray); |
||
| 163 | return $keyArray[$correctIndex]; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Checks if the input was a valid string. |
||
| 168 | * |
||
| 169 | * @param mixed $input a string to be made SQL-safe |
||
| 170 | * @param boolean $allowWhitespace whether some whitespace (e.g. newlines should be preserved (true) or redacted (false) |
||
| 171 | * @return string the massaged string |
||
| 172 | */ |
||
| 173 | public function string($input, $allowWhitespace = FALSE) { |
||
| 174 | // always chop out invalid characters, and surrounding whitespace |
||
| 175 | $retvalStep0 = iconv("UTF-8", "UTF-8//TRANSLIT", $input); |
||
| 176 | if ($retvalStep0 === FALSE) { |
||
| 177 | throw new Exception("iconv failure for string sanitisation. With TRANSLIT, this should never happen!"); |
||
| 178 | } |
||
| 179 | $retvalStep1 = trim($retvalStep0); |
||
| 180 | // if some funny person wants to inject markup tags, remove them |
||
| 181 | $retval = filter_var($retvalStep1, FILTER_SANITIZE_STRING, ["flags" => FILTER_FLAG_NO_ENCODE_QUOTES]); |
||
| 182 | if ($retval === FALSE) { |
||
| 183 | throw new Exception("filter_var failure for string sanitisation."); |
||
| 184 | } |
||
| 185 | // unless explicitly wanted, take away intermediate disturbing whitespace |
||
| 186 | // a simple "space" is NOT disturbing :-) |
||
| 187 | if ($allowWhitespace === FALSE) { |
||
| 188 | $afterWhitespace = preg_replace('/(\0|\r|\x0b|\t|\n)/', '', $retval); |
||
| 189 | } else { |
||
| 190 | // even if we allow whitespace, not pathological ones! |
||
| 191 | $afterWhitespace = preg_replace('/(\0|\r|\x0b)/', '', $retval); |
||
| 192 | } |
||
| 193 | if (is_array($afterWhitespace)) { |
||
| 194 | throw new Exception("This function has to be given a string and returns a string. preg_replace has generated an array instead!"); |
||
| 195 | } |
||
| 196 | return (string) $afterWhitespace; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Is this an integer, or a string that represents an integer? |
||
| 201 | * |
||
| 202 | * @param mixed $input the raw input |
||
| 203 | * @return boolean|int returns the input, or FALSE if it is not an integer-like value |
||
| 204 | */ |
||
| 205 | public function integer($input) { |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Is this a string representing a potentially more than 64-Bit length integer? |
||
| 214 | * |
||
| 215 | * @param string $input the input data which is possibly a really large integer |
||
| 216 | * @return boolean|string returns the input, or FALSE if it is not an integer-like string |
||
| 217 | */ |
||
| 218 | public function hugeInteger($input) { |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Checks if the input is the hex representation of a Consortium OI (i.e. three |
||
| 227 | * or five bytes) |
||
| 228 | * |
||
| 229 | * @param mixed $input the raw input |
||
| 230 | * @return boolean|string returns the input, or FALSE on validation failure |
||
| 231 | */ |
||
| 232 | public function consortiumOI($input) { |
||
| 233 | $shallow = $this->string($input); |
||
| 234 | if (strlen($shallow) != 6 && strlen($shallow) != 10) { |
||
| 235 | return FALSE; |
||
| 236 | } |
||
| 237 | if (!preg_match("/^[a-fA-F0-9]+$/", $shallow)) { |
||
| 238 | return FALSE; |
||
| 239 | } |
||
| 240 | return $shallow; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Is the input an NAI realm? Throws HTML error and returns FALSE if not. |
||
| 245 | * |
||
| 246 | * @param mixed $input the input to check |
||
| 247 | * @return boolean|string returns the realm, or FALSE if it was malformed |
||
| 248 | */ |
||
| 249 | public function realm($input) { |
||
| 250 | \core\common\Entity::intoThePotatoes(); |
||
| 251 | if (strlen($input) == 0) { |
||
| 252 | echo $this->inputValidationError(_("Realm is empty!")); |
||
| 253 | \core\common\Entity::outOfThePotatoes(); |
||
| 254 | return FALSE; |
||
| 255 | } |
||
| 256 | |||
| 257 | // basic string checks |
||
| 258 | $check = $this->string($input); |
||
| 259 | // list of things to check, and the error they produce |
||
| 260 | $pregCheck = [ |
||
| 261 | "/@/" => _("Realm contains an @ sign!"), |
||
| 262 | "/^\./" => _("Realm begins with a . (dot)!"), |
||
| 263 | "/\.$/" => _("Realm ends with a . (dot)!"), |
||
| 264 | "/ /" => _("Realm contains spaces!"), |
||
| 265 | ]; |
||
| 266 | |||
| 267 | // bark on invalid constructs |
||
| 268 | foreach ($pregCheck as $search => $error) { |
||
| 269 | if (preg_match($search, $check) == 1) { |
||
| 270 | echo $this->inputValidationError($error); |
||
| 271 | \core\common\Entity::outOfThePotatoes(); |
||
| 272 | return FALSE; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | if (preg_match("/\./", $check) == 0) { |
||
| 277 | echo $this->inputValidationError(_("Realm does not contain at least one . (dot)!")); |
||
| 278 | \core\common\Entity::outOfThePotatoes(); |
||
| 279 | return FALSE; |
||
| 280 | } |
||
| 281 | |||
| 282 | // none of the special HTML entities should be here. In case someone wants |
||
| 283 | // to mount a CSS attack by providing something that matches the realm constructs |
||
| 284 | // below but has interesting stuff between, mangle the input so that these |
||
| 285 | // characters do not do any harm. |
||
| 286 | \core\common\Entity::outOfThePotatoes(); |
||
| 287 | return htmlentities($check, ENT_QUOTES); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * could this be a valid username? |
||
| 292 | * |
||
| 293 | * Only checks correct form, not if the user actually exists in the system. |
||
| 294 | * |
||
| 295 | * @param mixed $input the username |
||
| 296 | * @return string echoes back the input string, or throws an Exception if bogus |
||
| 297 | * @throws Exception |
||
| 298 | */ |
||
| 299 | public function User($input) { |
||
| 300 | $retvalStep0 = iconv("UTF-8", "UTF-8//TRANSLIT", $input); |
||
| 301 | if ($retvalStep0 === FALSE) { |
||
| 302 | throw new Exception("iconv failure for string sanitisation. With TRANSLIT, this should never happen!"); |
||
| 303 | } |
||
| 304 | $retvalStep1 = trim($retvalStep0); |
||
| 305 | |||
| 306 | $retval = preg_replace('/(\0|\r|\x0b|\t|\n)/', '', $retvalStep1); |
||
| 307 | if ($retval != "" && !ctype_print($retval)) { |
||
| 308 | throw new Exception($this->inputValidationError("The user identifier is not an ASCII string!")); |
||
| 309 | } |
||
| 310 | |||
| 311 | return $retval; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * could this be a valid token? |
||
| 316 | * |
||
| 317 | * Only checks correct form, not if the token actually exists in the system. |
||
| 318 | * @param mixed $input the raw input |
||
| 319 | * @return string echoes back the input string, or throws an Exception if bogus |
||
| 320 | * @throws Exception |
||
| 321 | */ |
||
| 322 | public function token($input) { |
||
| 323 | $retval = $input; |
||
| 324 | if ($input != "" && preg_match('/[^0-9a-fA-F]/', $input) != 0) { |
||
| 325 | throw new Exception($this->inputValidationError("Token is not a hexadecimal string!")); |
||
| 326 | } |
||
| 327 | return $retval; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Is this be a valid coordinate vector on one axis? |
||
| 332 | * |
||
| 333 | * @param mixed $input a numeric value in range of a geo coordinate [-180;180] |
||
| 334 | * @return string returns back the input if all is good; throws an Exception if out of bounds or not numeric |
||
| 335 | * @throws Exception |
||
| 336 | */ |
||
| 337 | public function coordinate($input) { |
||
| 338 | $oldlocale = setlocale(LC_NUMERIC, 0); |
||
| 339 | setlocale(LC_NUMERIC, "en_GB"); |
||
| 340 | if (!is_numeric($input)) { |
||
| 341 | throw new Exception($this->inputValidationError("Coordinate is not a numeric value!")); |
||
| 342 | } |
||
| 343 | setlocale(LC_NUMERIC, $oldlocale); |
||
| 344 | // lat and lon are always in the range of [-180;+180] |
||
| 345 | if ($input < -180 || $input > 180) { |
||
| 346 | throw new Exception($this->inputValidationError("Coordinate is out of bounds. Which planet are you from?")); |
||
| 347 | } |
||
| 348 | return $input; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Is this a valid coordinate pair in JSON encoded representation? |
||
| 353 | * |
||
| 354 | * @param mixed $input the string to be checked: is this a serialised array with lat/lon keys in a valid number range? |
||
| 355 | * @return string returns $input if checks have passed; throws an Exception if something's wrong |
||
| 356 | * @throws Exception |
||
| 357 | */ |
||
| 358 | public function coordJsonEncoded($input) { |
||
| 359 | $tentative = json_decode($input, true); |
||
| 360 | if (is_array($tentative)) { |
||
| 361 | if (isset($tentative['lon']) && isset($tentative['lat']) && $this->coordinate($tentative['lon']) && $this->coordinate($tentative['lat'])) { |
||
| 362 | return $input; |
||
| 363 | } |
||
| 364 | } |
||
| 365 | throw new Exception($this->inputValidationError("Wrong coordinate encoding (2.0 uses JSON, not serialize)!")); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * This checks the state of a HTML GET/POST "boolean". |
||
| 370 | * |
||
| 371 | * If not checked, no value is submitted at all; if checked, has the word "on". |
||
| 372 | * Anything else is a big error. |
||
| 373 | * |
||
| 374 | * @param mixed $input the string to test |
||
| 375 | * @return boolean TRUE if the input was "on". It is not possible in HTML to signal "off" |
||
| 376 | * @throws Exception |
||
| 377 | */ |
||
| 378 | public function boolean($input) { |
||
| 379 | if ($input != "on") { |
||
| 380 | throw new Exception($this->inputValidationError("Unknown state of boolean option!")); |
||
| 381 | } |
||
| 382 | return TRUE; |
||
| 383 | } |
||
| 384 | |||
| 385 | const TABLEMAPPING = [ |
||
| 386 | "IdP" => "institution_option", |
||
| 387 | "Profile" => "profile_option", |
||
| 388 | "FED" => "federation_option", |
||
| 389 | ]; |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Is this a valid database reference? Has the form <tablename>-<rowID> and there |
||
| 393 | * needs to be actual data at that place |
||
| 394 | * |
||
| 395 | * @param mixed $input the reference to check |
||
| 396 | * @return boolean|array the reference split up into "table" and "rowindex", or FALSE |
||
| 397 | */ |
||
| 398 | public function databaseReference($input) { |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * is this a valid hostname? |
||
| 408 | * |
||
| 409 | * @param mixed $input the raw input |
||
| 410 | * @return boolean|string echoes the hostname, or FALSE if bogus |
||
| 411 | */ |
||
| 412 | public function hostname($input) { |
||
| 413 | // is it a valid IP address (IPv4 or IPv6), or a hostname? |
||
| 414 | if (filter_var($input, FILTER_VALIDATE_IP) || $this->email("stefan@" . $input) !== FALSE) { |
||
| 415 | // if it's a verified IP address or hostname then it does not contain |
||
| 416 | // rubbish of course. But just to be sure, run htmlspecialchars around it |
||
| 417 | return htmlspecialchars($input, ENT_QUOTES); |
||
| 418 | } |
||
| 419 | return FALSE; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * is this a valid email address? |
||
| 424 | * |
||
| 425 | * @param mixed $input the raw input |
||
| 426 | * @return boolean|string echoes the mail address, or FALSE if bogus |
||
| 427 | */ |
||
| 428 | public function email($input) { |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * is this a well-formed SMS number? Light massaging - leading + will be removed |
||
| 439 | * @param string $input the raw input |
||
| 440 | * @return boolean|string |
||
| 441 | */ |
||
| 442 | public function sms($input) { |
||
| 443 | $number = str_replace(' ', '', str_replace(".", "", str_replace("+", "", $input))); |
||
| 444 | if (!is_numeric($number)) { |
||
| 445 | return FALSE; |
||
| 446 | } |
||
| 447 | return $number; |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Is this is a language we support? If not, sanitise to our configured default language. |
||
| 452 | * |
||
| 453 | * @param mixed $input the candidate language identifier |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | public function supportedLanguage($input) { |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Makes sure we are not receiving a bogus option name. The called function throws |
||
| 471 | * an assertion if the name is not known. |
||
| 472 | * |
||
| 473 | * @param mixed $input the unvetted option name |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public function optionName($input) { |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Checks to see if the input is a valid image of sorts |
||
| 483 | * |
||
| 484 | * @param mixed $binary blob that may or may not be a parseable image |
||
| 485 | * @return boolean |
||
| 486 | */ |
||
| 487 | public function image($binary) { |
||
| 497 | } |
||
| 498 | |||
| 499 | } |
||
| 500 |