Skip to content

Instantly share code, notes, and snippets.

@anselmobattisti
Created May 20, 2009 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anselmobattisti/114931 to your computer and use it in GitHub Desktop.
Save anselmobattisti/114931 to your computer and use it in GitHub Desktop.
<?php
/**
* Sistema_Variavel
*
* @abstract Prover uma classe que abstraia algumas questões importantes
* e perigosas que surgem durante o desenvolvimento de sistemas web.
*
* A primera questão é sobre variáveis, elas podem ser passadas por POST, GET e
* por SESSION. Essa classe irá simplificar o acesso as mesmas através do
* método estático get.
*
* O segundo método desta classe fornece segurança contra injections URL/SQL
*
* @example Para receber a variável $_POST['cod']
* Sistema_Variavel::get('cod');
*
* @author Anselmo Battisti
* @since 1 - 16/01/2008
* */
class Sistema_Variavel
{
/**
* get
*
* @abstract Retorna o valor de uma variável independente de onde
* ela esteja armazenada.
*
* @param string variavel Nome da variável que está sendo procurada
* @param string local Local onde a variável está armazenada, este parâmetro
* por padrão é null, porém se existir duas variáveis com o mesmo nome
* mas em dois locais diferentes deve ser especificado o POST, GET ou SESSION
* */
public static function get($variavel,$local=""){
$valor = "";
# acesso com o local sendo informado, usado apenas quando
if($local){
switch ($local) {
case "GET":
$valor = self::validaParametro($_GET[$variavel]);
break;
case "POST":
$valor = self::validaParametro($_POST[$variavel]);
break;
case "SESSION":
$valor = self::validaParametro($_SESSION[$variavel]);
break;
default:
break;
}
return $valor;
} else {
$achouVariavel = 0;
# é post
if($_POST[$variavel]){
$achouVariavel++;
$valor = $_POST[$variavel];
}
# é post
if($_GET[$variavel]){
$achouVariavel++;
$valor = $_GET[$variavel];
}
# é post
if($_SESSION[$variavel]){
$achouVariavel++;
$valor = $_SESSION[$variavel];
}
if($achouVariavel > 1){
die("ERRO : Esta variável existe em dois locais.");
} else {
return self::validaParametro($valor);
}
}
}
/**
* validaParametro
*
* @abstract Faz o tratamento de vetores com relação a sql injection
*
* @param array vetor Vetor que será verificado
* */
public static function validaParametro($vetor){
if(is_array($vetor)){
foreach ($vetor as $chave => $valor){
if (is_array($valor)){
$vetor[$chave] = self::validaParametro($valor);
} else {
$vetor[$chave] = self::antiInjection($valor);
}
}
} else {
return self::antiInjection($vetor);
}
return $vetor;
}
/**
* antiInjection
*
* @abstract Verifica se existe algum tipo de URL ou SQL injection
* no valor que está sendo analizado
*
* @param mix valor Valor que está sendo analizado
* */
public static function antiInjection($str){
# Remove palavras suspeitas de injection.
$str = preg_replace(sql_regcase("/(\n|\r|%0a|%0d|Content-Type:|bcc:|to:|cc:|Autoreply:|from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/"), "", $str);
$str = trim($str); # Remove espaços vazios.
$str = strip_tags($str); # Remove tags HTML e PHP.
$str = addslashes($str); # Adiciona barras invertidas à uma string.
return $str;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment