PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/filter-wrapper.php

https://github.com/wadd0032/mtm1531-homework-6
PHP | 152 lines | 105 code | 29 blank | 18 comment | 34 complexity | f47175a1df48c63847d2f4bc3c6756f7 MD5 | raw file
  1. <?php
  2. /**
  3. * Check for the existence some of the PHP filters
  4. * If they don't exist recreate them so we can use them
  5. */
  6. if (!defined('FILTER_VALIDATE_BOOLEAN')) define('FILTER_VALIDATE_BOOLEAN', 'FILTER_VALIDATE_BOOLEAN');
  7. if (!defined('FILTER_VALIDATE_EMAIL')) define('FILTER_VALIDATE_EMAIL', 'FILTER_VALIDATE_EMAIL');
  8. if (!defined('FILTER_VALIDATE_FLOAT')) define('FILTER_VALIDATE_FLOAT', 'FILTER_VALIDATE_FLOAT');
  9. if (!defined('FILTER_VALIDATE_INT')) define('FILTER_VALIDATE_INT', 'FILTER_VALIDATE_INT');
  10. if (!defined('FILTER_VALIDATE_REGEXP')) define('FILTER_VALIDATE_REGEXP', 'FILTER_VALIDATE_REGEXP');
  11. if (!defined('FILTER_VALIDATE_URL')) define('FILTER_VALIDATE_URL', 'FILTER_VALIDATE_URL');
  12. if (!defined('FILTER_SANITIZE_EMAIL')) define('FILTER_SANITIZE_EMAIL', 'FILTER_SANITIZE_EMAIL');
  13. if (!defined('FILTER_SANITIZE_NUMBER_FLOAT')) define('FILTER_SANITIZE_NUMBER_FLOAT', 'FILTER_SANITIZE_NUMBER_FLOAT');
  14. if (!defined('FILTER_FLAG_ALLOW_FRACTION')) define('FILTER_FLAG_ALLOW_FRACTION', 'FILTER_FLAG_ALLOW_FRACTION');
  15. if (!defined('FILTER_SANITIZE_NUMBER_INT')) define('FILTER_SANITIZE_NUMBER_INT', 'FILTER_SANITIZE_NUMBER_INT');
  16. if (!defined('FILTER_SANITIZE_STRING')) define('FILTER_SANITIZE_STRING', 'FILTER_SANITIZE_STRING');
  17. if (!defined('FILTER_SANITIZE_SPECIAL_CHARS')) define('FILTER_SANITIZE_SPECIAL_CHARS', 'FILTER_SANITIZE_SPECIAL_CHARS');
  18. if (!defined('FILTER_SANITIZE_URL')) define('FILTER_SANITIZE_URL', 'FILTER_SANITIZE_URL');
  19. if (!defined('FILTER_UNSAFE_RAW')) define('FILTER_UNSAFE_RAW', 'FILTER_UNSAFE_RAW');
  20. if (!defined('FILTER_DEFAULT')) define('FILTER_DEFAULT', FILTER_UNSAFE_RAW);
  21. if (!defined('INPUT_GET')) define('INPUT_GET', 'INPUT_GET');
  22. if (!defined('INPUT_POST')) define('INPUT_POST', 'INPUT_POST');
  23. if (!defined('INPUT_COOKIE')) define('INPUT_COOKIE', 'INPUT_COOKIE');
  24. if (!defined('INPUT_SERVER')) define('INPUT_SERVER', 'INPUT_SERVER');
  25. if (!defined('INPUT_ENV')) define('INPUT_ENV', 'INPUT_ENV');
  26. /**
  27. * Check if the filter_input and filter_var functions exist
  28. * If they do not exist create them
  29. */
  30. if (!function_exists('filter_input')) {
  31. function filter_input($type, $variable_name, $filter = FILTER_DEFAULT, $options = array()) {
  32. return filter_input_wrapper($type, $variable_name, $filter, $options);
  33. }
  34. }
  35. if (!function_exists('filter_var')) {
  36. function filter_var($variable, $filter = FILTER_DEFAULT, $options = array()) {
  37. return filter_var_wrapper($variable, $filter, $options);
  38. }
  39. }
  40. /**
  41. * Mock replacement for filter_input
  42. * Sends values to filter_var for filtering
  43. */
  44. function filter_input_wrapper ($type, $variable_name, $filter = FILTER_DEFAULT, $options = array()) {
  45. $input = $_GET;
  46. switch ($type) {
  47. case 'INPUT_POST' :
  48. $input = $_POST;
  49. break;
  50. case 'INPUT_COOKIE' :
  51. $input = $_COOKIE;
  52. break;
  53. case 'INPUT_SERVER' :
  54. $input = $_SERVER;
  55. break;
  56. case 'INPUT_ENV' :
  57. $input = $_ENV;
  58. break;
  59. }
  60. return isset($input[$variable_name]) ? filter_var_wrapper($input[$variable_name], $filter, $options) : null;
  61. }
  62. /**
  63. * Mock replacement for filter_var
  64. * Uses built in PHP functions or regular expressions to perform similar actions on the data
  65. */
  66. function filter_var_wrapper ($variable, $filter = FILTER_DEFAULT, $options = array()) {
  67. switch ($filter) {
  68. case FILTER_SANITIZE_EMAIL :
  69. return preg_replace(">[^a-z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~\@\.\[\]]>iu", '', $variable);
  70. break;
  71. case FILTER_SANITIZE_NUMBER_FLOAT :
  72. $decimal = ($options == FILTER_FLAG_ALLOW_FRACTION) ? '\.' : '';
  73. return preg_replace('@[^\d\+\-' . $decimal . ']@', '', $variable);
  74. break;
  75. case FILTER_SANITIZE_NUMBER_INT :
  76. return preg_replace('@[^\d\+\-]@', '', $variable);
  77. break;
  78. case FILTER_SANITIZE_SPECIAL_CHARS :
  79. return htmlspecialchars($s, ENT_QUOTES, 'UTF-8', false);
  80. break;
  81. case FILTER_SANITIZE_STRING :
  82. return strip_tags($variable);
  83. break;
  84. case FILTER_SANITIZE_URL :
  85. return preg_replace(">[^a-z0-9\$\-\_\.\+\!\*\'\(\)\,\{\}\|\\\\^\~\[\]\`\<\>\#\%\"\;\/\?\:\@\&\=\.]>iu", '', $variable);
  86. break;
  87. case FILTER_VALIDATE_BOOLEAN :
  88. $input = (is_string($variable)) ? trim(strtolower($variable)) : $variable;
  89. if ($input === 1 || $input === true)
  90. return true;
  91. return (bool)in_array($input, array('1', 'true', 'on', 'yes'));
  92. break;
  93. case FILTER_VALIDATE_EMAIL :
  94. // From HTML5 spec: http://html5.org/tools/web-apps-tracker?from=6883&to=6884o
  95. // And: http://blog.gerv.net/2011/05/html5_email_address_regexp/
  96. return (bool)preg_match(">^[a-zA-Z0-9.!#$%&'*+/=?\^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$>", $variable);
  97. break;
  98. case FILTER_VALIDATE_FLOAT :
  99. return (bool)is_numeric($variable);
  100. break;
  101. case FILTER_VALIDATE_INT :
  102. if (!is_numeric($variable) || floor($variable) != $variable)
  103. return false;
  104. $min_range = (isset($options['options']['min_range'])) ? $options['options']['min_range'] : null;
  105. if ($min_range !== null && $variable < $min_range)
  106. return false;
  107. $max_range = (isset($options['options']['max_range'])) ? $options['options']['max_range'] : null;
  108. if ($max_range !== null && $variable > $max_range)
  109. return false;
  110. return true;
  111. break;
  112. case FILTER_VALIDATE_REGEXP :
  113. return (bool)preg_match($options['options']['regexp'], $variable);
  114. break;
  115. case FILTER_VALIDATE_URL :
  116. return (bool)preg_match('@^[a-z][\w-]+:(?:/{1,3})?[^\s()<>]+(\.[^\s()<>]+(/[^\s]*)?)?$@iu', $variable);
  117. break;
  118. case FILTER_UNSAFE_RAW :
  119. case FILTER_DEFAULT :
  120. default:
  121. return $variable;
  122. }
  123. }