Advertisement
Neo-Craft

filterSql of Yii Frw

Mar 10th, 2012
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1.     public static function filterSql($val) {
  2.      // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  3.      // this prevents some character re-spacing such as <java\0script>
  4.      // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  5.      $val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val);
  6.      
  7.      //REMOVE SQL INJECTION
  8.      $val = preg_replace(@sql_regcase("/(\n|\r|%0a|%0d|Content-Type:|bcc:|to:|cc:|Autoreply:|from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/"), "", $val);
  9.      
  10.      //$val = stripslashes($val);
  11.      //$val = strip_tags($val); # Remove tags HTML e PHP.
  12.      //$val = addslashes($val); # Adiciona barras invertidas à uma string.
  13.      
  14.      
  15.      //$val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  16.  
  17.      // straight replacements, the user should never need these since they're normal characters
  18.      // this prevents like <IMG SRC=&#X40&#X61&#X76&#X61&#X73&#X63&#X72&#X69&#X70&#X74&#X3A&#X61&#X6C&#X65&#X72&#X74&#X28&#X27&#X58&#X53&#X53&#X27&#X29>
  19.      $search = 'abcdefghijklmnopqrstuvwxyz';
  20.      $search.= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  21.      $search.= '1234567890!@#$%^&*()';
  22.      $search.= '~`";:?+/={}[]-_|\'\\';
  23.  
  24.      for ($i = 0; $i < strlen($search); $i++) {
  25.              // ;? matches the ;, which is optional
  26.              // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  27.  
  28.              // &#x0040 @ search for the hex values
  29.              $val = preg_replace('/(&#[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
  30.              // &#00064 @ 0{0,7} matches '0' zero to seven times
  31.              $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  32.      }
  33.          
  34.      return "'$val'";
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement