Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [funkcja] walidacja super globalnych
Forum PHP.pl > Forum > Gotowe rozwiązania > Algorytmy, klasy, funkcje
Rafiki
Może komuś sie przyda, aktualnie tworze klase ktora sprawdzania dane min ze zmiennych superglobalnych a do tego dane z formularze takie jak adres email, www i inne.

Proszę też o jakies sugestie smile.gif

  1. <?php
  2.  
  3. /*
  4. * Autor: Rafał Robakowski
  5. * Kontakt: rafal.robakowski@gmail.com
  6. */
  7.  
  8. function validation_superglobals() {
  9. if ( !get_magic_quotes_gpc() ) {
  10.  
  11. /*
  12. * Filtrowanie tablicy GET
  13. */
  14. if ( is_array($_GET) ) {
  15. while ( list($key_1, $value_1) = each($_GET)) {
  16. if ( is_array($_GET[$key_1]) ) {
  17. while ( list($key_2, $value_2) = each($_GET[$key_2]) ) {
  18. $_GET[$key_1][$key_2] = addslashes($value_2);
  19. }
  20.  
  21. @reset($_GET[$key_1]);
  22. } else {
  23. $_GET[$key_1] = addslashes($value_1);
  24. }
  25. }
  26. @reset($_GET);
  27. }
  28.  
  29. /*
  30. * Filtrowanie tablicy POST
  31. */
  32. if ( is_array($_POST) ) {
  33. while ( list($key_1, $value_1) = each($_POST)) {
  34. if ( is_array($_POST[$key_1]) ) {
  35. while ( list($key_2, $value_2) = each($_POST[$key_2]) ) {
  36. $_POST[$key_1][$key_2] = addslashes($value_2);
  37. }
  38.  
  39. @reset($_POST[$key_1]);
  40. } else {
  41. $_POST[$key_1] = addslashes($value_1);
  42. }
  43. }
  44. @reset($_POST);
  45. }
  46.  
  47. /*
  48. * Filtrowanie tablicy COOKIE
  49. */
  50. if ( is_array($_COOKIE) ) {
  51. while ( list($key_1, $value_1) = each($_COOKIE)) {
  52. if ( is_array($_COOKIE[$key_1]) ) {
  53. while ( list($key_2, $value_2) = each($_COOKIE[$key_2]) ) {
  54. $_COOKIE[$key_1][$key_2] = addslashes($value_2);
  55. }
  56.  
  57. @reset($_COOKIE[$key_1]);
  58. } else {
  59. $_COOKIE[$key_1] = addslashes($value_1);
  60. }
  61. }
  62. @reset($_COOKIE);
  63. }
  64. }
  65. }
  66.  
  67. ?>


Mysle ze zasada wykorzystania jest jasna poprostu wywolac funkcje nie trzeba podawac zadnych paramterow.
Spirit86
wydaje mi się, że nie będzie to działać, tak jak powinno, o ile wrzucisz to do funkcji. Zmienne, ktore tworzysz będą miały zasięg tylko w tej funkcji.
mike
1. Nie zauważyłes że te trzy bloki kodu są niemal identczne? Może warto zastąpić je jedną funkcją tongue.gif
2. Całość i tak jest bezużyteczna. Filtowanie wszystkiego jak leci jest bez sensu i jest niepotrzebne. Tym bardziej że powstaje pytanie: Niby po co mam stawiać slash'e wszędzie do danych przychodzących? Równie dobrze mogę sobie to pozamieniać na encje, też na nic mi się to nie przyda tongue.gif
bela
ee, co to jest? Nie łatwiej zrobić to rekurencyjnie?
NuLL
Proste filtrowanie superglobali moze wygladac np tak :

( Pisanie w notatniku z palca u kumpla winksmiley.jpg )

  1. <?php
  2.  
  3. class httpRequest
  4. {
  5.     private static 
  6.     
  7.         $loaded=false,
  8.         $data=array(),
  9.          $filters=array();
  10.     
  11.     const 
  12.         POST=1,
  13.         GET=2,
  14.         BOTH=3,
  15.         COOKIE=4;
  16.     
  17.     private static function load()
  18.     {
  19.         self::$data['post']=$_POST;
  20.         self::$data['get']=$_GET;
  21.         self::$data['cookie']=$_COOKIE;
  22.         
  23.         self::$filters=array('string','alphanumeric','special_chars','unsafe_raw','email','url','number_int',
  24.             'number_float','magic_quotes','int','boolean','float','form_action');
  25.             
  26.         self::$loaded=true;
  27.         
  28.         //cya
  29.         unset($_GET,$_POST,$_COOKIE);
  30.     }
  31.     
  32.     public static function map($name,$source,$filters)
  33.     {
  34.         if(!self::$loaded) self::load();
  35.         
  36.         switch ($source)
  37.         {
  38.             case httpRequest::POST :
  39.                 $variable=isset(self::$data['post'][$name]) ? self::$data['post'][$name] : null ;
  40.             break;
  41.             
  42.             case httpRequest::GET :
  43.                 $variable=isset(self::$data['get'][$name]) ? self::$data['get'][$name] : null ;
  44.             break;
  45.             
  46.             case httpRequest::BOTH :
  47.                 $variable=isset(self::$data['get'][$name]) ? self::$data['get'][$name] :
  48.                          isset(self::$data['post'][$name]) ? self::$data['post'][$name] : null ;
  49.             break;
  50.             
  51.             case httpRequest::COOKIE :
  52.                 $variable=isset(self::$data['cookie'][$name]) ? self::$data['cookie'][$name] : null ;
  53.             break;
  54.         }
  55.         
  56.         if(is_string($filters))
  57.         {
  58.             $_filters[]=$filters;
  59.         }
  60.         else
  61.         {
  62.             $_filters=$filters;
  63.         }
  64.         
  65.         return self::filter($variable,$_filters);
  66.     }
  67.     
  68.     private static function filter($variable,$filters)
  69.     {
  70.         if(is_array($filters) && count($filters)>0)
  71.         {
  72.             foreach ($filters as $filter)
  73.             {
  74.                 if(in_array($filter,self::$filters))
  75.                 {
  76.                     switch ($filter)
  77.                     {
  78.                         case 'string':
  79.                             $variable=(strlen($variable)>0) ? strip_tags($variable) : '' ;
  80.                         break;
  81.                     
  82.                         case 'special_chars':
  83.                             $variable=htmlspecialchars($variable);
  84.                         break;
  85.                     
  86.                         case 'unsafe_raw':
  87.                             //hmm
  88.                         break;
  89.                     
  90.                         case 'alphanumeric':
  91.                             $variable=strip_tags($variable);
  92.                             $variable=preg_replace( "/[^a-zA-Z0-9\-\_]/", "" , $variable);
  93.                         break;
  94.                         
  95.                         case 'magic_quotes':
  96.                             $variable=stripslashes($variable);
  97.                         break;
  98.                         
  99.                         case 'int':
  100.                             $variable=isset($variable) ? intval($variable) : 0 ;
  101.                         break;
  102.                         
  103.                         case 'float':
  104.                             $variable=isset($variable) ? floatval($variable) : 0.;
  105.                         break;
  106.                         
  107.                         case 'boolean':
  108.                             $variable=intval((boolean)$variable);
  109.                         break;
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.         return $variable;        
  115.     }
  116.     
  117.     public static function has($name,$forcedSource=0)
  118.     {
  119.         if(!self::$loaded) self::load();
  120.         
  121.         if($forcedSource==0)
  122.         {
  123.             if(isset(self::$data['post'][$name]) || isset(self::$data['get'][$name]) || isset(self::$data['cookie'][$name]))
  124.             {
  125.                 return true;
  126.             }
  127.         }
  128.         else
  129.         {
  130.             switch ((int)$forcedSource)
  131.             {
  132.             case httpRequest::POST :
  133.                 return isset(self::$data['post'][$name]);
  134.             break;
  135.             
  136.             case httpRequest::GET :
  137.                 return isset(self::$data['get'][$name]);
  138.             break;
  139.             
  140.             case httpRequest::COOKIE :
  141.                 return isset(self::$data['cookie'][$name]);
  142.             break;
  143.             }
  144.         }
  145.         
  146.         return false;
  147.     }
  148. }
  149.  
  150. ?>


Zastosowanie
  1. <?php
  2.  
  3. if(httpRequest::has('costam'))
  4. {
  5.     $variable=httpRequest::map('costam',httpRequest::GET,'int');
  6. }
  7.  
  8. ?>

Jest to tylko przyklad - metoda map() zmusza programiste do podania filtra - w ten sposob zmienna pobierana na czysto jest pobierana dzieki podaniu filtra unsafe_raw -czyli programista o tym doskonale wie ze tworzy sobie niebezpieczenstwo. Filtry mozna rowniez podawac jako tablica. Napisanie takiego prostego wrapperka pozwala na ominiecie mnostwo isset w kodzie oraz brak potrzeby stosowania striptags albo intval();

Nie traktujcie tego jako gotowiec bo ma byc to tylko przyklad smile.gif
Neotion
yyyy... komentowałeś kiedyś kod?
lenczewski
ja filtruje w ten sposób
  1. <?php
  2. $param = array_keys($_POST);
  3. $ile = count($param);
  4.  
  5. for($i=0; $i <= $ile; $i++) {
  6. $_POST[$param[$i]] = ereg_replace("(t)+", "t", $_POST[$param[$i]]);
  7. $_POST[$param[$i]] = ereg_replace(" +", " ", $_POST[$param[$i]]);
  8.  
  9. $_POST[$param[$i]] = str_replace("",  "", ereg_replace("(rn|n|r)", "<br />", htmlspecialchars($_POST[$param[$i]])));
  10.  
  11. }
  12. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.