PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/authsite/lib/sanitize.inc.php

http://myrpm.googlecode.com/
PHP | 159 lines | 125 code | 3 blank | 31 comment | 6 complexity | f8313cb11f8c2bb9bf44f9a7b5f80463 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. ///////////////////////////////////////
  3. // sanitize.inc.php
  4. // Sanitization functions for PHP
  5. // by: Gavin Zuchlinski, Jamie Pratt, Hokkaido
  6. // webpage: http://libox.net
  7. // Last modified: September 27, 2003
  8. //
  9. // Many thanks to those on the webappsec list for helping me improve these functions
  10. ///////////////////////////////////////
  11. // Function list:
  12. // sanitize_paranoid_string($string) -- input string, returns string stripped of all non
  13. // alphanumeric
  14. // sanitize_system_string($string) -- input string, returns string stripped of special
  15. // characters
  16. // sanitize_sql_string($string) -- input string, returns string with slashed out quotes
  17. // sanitize_html_string($string) -- input string, returns string with html replacements
  18. // for special characters
  19. // sanitize_int($integer) -- input integer, returns ONLY the integer (no extraneous
  20. // characters
  21. // sanitize_float($float) -- input float, returns ONLY the float (no extraneous
  22. // characters)
  23. // sanitize($input, $flags) -- input any variable, performs sanitization
  24. // functions specified in flags. flags can be bitwise
  25. // combination of PARANOID, SQL, SYSTEM, HTML, INT, FLOAT, LDAP,
  26. // UTF8
  27. ///////////////////////////////////////
  28. define("PARANOID", 1);
  29. define("SQL", 2);
  30. define("SYSTEM", 4);
  31. define("HTML", 8);
  32. define("INT", 16);
  33. define("FLOAT", 32);
  34. define("LDAP", 64);
  35. define("UTF8", 128);
  36. // internal function for utf8 decoding
  37. // thanks to Jamie Pratt for noticing that PHP's function is a little
  38. // screwy
  39. function my_utf8_decode($string)
  40. {
  41. return strtr($string,
  42. "??????????ÁÂ?Ä??Ç?É?Ë?ÍÎ????ÓÔ?Ö??Ú?ÜÝß?áâ?ä??ç?é?ë?íî????óô?ö??ú?üý?",
  43. "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");
  44. }
  45. // paranoid sanitization -- only let the alphanumeric set through
  46. function sanitize_paranoid_string($string, $min='', $max='')
  47. {
  48. $string = preg_replace("/[^a-zA-Z0-9]/", "", $string);
  49. $len = strlen($string);
  50. if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max)))
  51. return FALSE;
  52. return $string;
  53. }
  54. // sanitize a string in prep for passing a single argument to system() (or similar)
  55. function sanitize_system_string($string, $min='', $max='')
  56. {
  57. $pattern = '/(;|\||`|>|<|&|^|"|'."\n|\r|'".'|{|}|[|]|\)|\()/i'; // no piping, passing possible environment variables ($),
  58. // seperate commands, nested execution, file redirection,
  59. // background processing, special commands (backspace, etc.), quotes
  60. // newlines, or some other special characters
  61. $string = preg_replace($pattern, '', $string);
  62. $string = '"'.preg_replace('/\$/', '\\\$', $string).'"'; //make sure this is only interpretted as ONE argument
  63. $len = strlen($string);
  64. if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max)))
  65. return FALSE;
  66. return $string;
  67. }
  68. // sanitize a string for SQL input (simple slash out quotes and slashes)
  69. function sanitize_sql_string($string, $min='', $max='')
  70. {
  71. $pattern[0] = '/(\\\\)/';
  72. $pattern[1] = "/\"/";
  73. $pattern[2] = "/'/";
  74. $replacement[0] = '\\\\\\';
  75. $replacement[1] = '\"';
  76. $replacement[2] = "\\'";
  77. $len = strlen($string);
  78. if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max)))
  79. return FALSE;
  80. return preg_replace($pattern, $replacement, $string);
  81. }
  82. // sanitize a string for SQL input (simple slash out quotes and slashes)
  83. function sanitize_ldap_string($string, $min='', $max='')
  84. {
  85. $pattern = '/(\)|\(|\||&)/';
  86. $len = strlen($string);
  87. if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max)))
  88. return FALSE;
  89. return preg_replace($pattern, '', $string);
  90. }
  91. // sanitize a string for HTML (make sure nothing gets interpretted!)
  92. function sanitize_html_string($string)
  93. {
  94. $pattern[0] = '/\&/';
  95. $pattern[1] = '/</';
  96. $pattern[2] = "/>/";
  97. $pattern[3] = '/\n/';
  98. $pattern[4] = '/"/';
  99. $pattern[5] = "/'/";
  100. $pattern[6] = "/%/";
  101. $pattern[7] = '/\(/';
  102. $pattern[8] = '/\)/';
  103. $pattern[9] = '/\+/';
  104. $pattern[10] = '/-/';
  105. $replacement[0] = '&amp;';
  106. $replacement[1] = '&lt;';
  107. $replacement[2] = '&gt;';
  108. $replacement[3] = '<br>';
  109. $replacement[4] = '&quot;';
  110. $replacement[5] = '&#39;';
  111. $replacement[6] = '&#37;';
  112. $replacement[7] = '&#40;';
  113. $replacement[8] = '&#41;';
  114. $replacement[9] = '&#43;';
  115. $replacement[10] = '&#45;';
  116. return preg_replace($pattern, $replacement, $string);
  117. }
  118. // make int int!
  119. function sanitize_int($integer, $min='', $max='')
  120. {
  121. $int = intval($integer);
  122. if((($min != '') && ($int < $min)) || (($max != '') && ($int > $max)))
  123. return FALSE;
  124. return $int;
  125. }
  126. // make float float!
  127. function sanitize_float($float, $min='', $max='')
  128. {
  129. $float = floatval($float);
  130. if((($min != '') && ($float < $min)) || (($max != '') && ($float > $max)))
  131. return FALSE;
  132. return $float;
  133. }
  134. // glue together all the other functions
  135. function sanitize($input, $flags, $min='', $max='')
  136. {
  137. if($flags & UTF8) $input = my_utf8_decode($input);
  138. if($flags & PARANOID) $input = sanitize_paranoid_string($input, $min, $max);
  139. if($flags & INT) $input = sanitize_int($input, $min, $max);
  140. if($flags & FLOAT) $input = sanitize_float($input, $min, $max);
  141. if($flags & HTML) $input = sanitize_html_string($input, $min, $max);
  142. if($flags & SQL) $input = sanitize_sql_string($input, $min, $max);
  143. if($flags & LDAP) $input = sanitize_ldap_string($input, $min, $max);
  144. if($flags & SYSTEM) $input = sanitize_system_string($input, $min, $max);
  145. return $input;
  146. }
  147. ?>