phpDocumentor general
[ class tree: general ] [ index: general ] [ all elements ]

Source for file funzioni_url.inc

Documentation is available at funzioni_url.inc

  1. <?php
  2. /**
  3.  * Funzioni di utilita' per la codifica degli url ai fini di protezione delle operazioni
  4.  * @author Ubik <emiliano.leporati@gmail.com>
  5.  * @package general
  6.  */
  7.  
  8. /**
  9.  * implica che $location contiene una url con & e viene restituita com &amp;
  10.  */
  11. define("URL_AMP_DEFAULT"0);
  12. /**
  13.  * la url viene passata con &amp; e restituita con &amp;
  14.  */
  15. define("URL_AMP_XML"1);
  16. /**
  17.  * la url viene passata con &amp; e restituita con &
  18.  */
  19. define("URL_AMP_PHP"2);
  20.  
  21.  
  22. define("PROTO"isset($_SERVER['https']'https://' 'http://');
  23.  
  24. define("AMP_SEARCH"'/&(?!(?U)[\w-&]{0,6};)/');
  25. define("AMP_REPLACE"'&amp;');
  26.  
  27. /**
  28.  * Collega due stringhe nella formazione di un percorso corretto (es: path('c:\pippo','bravo.xml') = 'c:/pippo/bravo.xml')
  29.  * @param string $inizio 
  30.  * @param string $fine 
  31.  * @return string 
  32.  */
  33. function path($inizio$fine '')
  34. {
  35.     if (substr($inizio-1== "/"{
  36.         $inizio substr($inizio0-1);
  37.     }
  38.     if (strlen($fine&& $fine[0== "/"{
  39.         $fine substr($fine1);
  40.     }
  41.     return str_replace("\\","/",$inizio."/".$fine);
  42. }
  43.  
  44. function pathl()
  45. {
  46.     $args func_get_args();
  47.     $args array_flatten($args);
  48.     $res "";
  49.     $res $last array_shift($args);
  50.     
  51.     while(count($args)) {
  52.         $res path($resarray_shift($args));
  53.     }
  54.  
  55.     return $res;
  56. }
  57.  
  58. # per ajax
  59. function correct_get_post()
  60. {
  61.     foreach($_POST as $key => $value{
  62.         if (inizia_per($key'amp;')) {
  63.             unset($_POST[$key]);
  64.             $key substr($key4);
  65.             $_GET[$key$value;
  66.             $_POST[$key$value;
  67.         else {
  68.             $_GET[$key$value;
  69.         }
  70.     }
  71. }
  72.  
  73. /**
  74.  * Costruisce l'url che punta al gateway (file php) passato
  75.  * @param string $file 
  76.  * @return string 
  77.  */
  78. function gateway($file)
  79. {
  80.     if (strcasecmp(PROTO'https://'== 0{
  81.         $host PROTO.$_SERVER['HTTP_HOST'];
  82.  
  83.         if (substr($host-1== "/"{
  84.             $host substr($host0-1);
  85.         }
  86.         if ($file[0== "/"{
  87.             $file substr($file1);
  88.         }
  89.         if (defined("VDIR")) {
  90.             if (substr(VDIR-1== "/"{
  91.                 $vdir substr(VDIR0-1);
  92.             else {
  93.                 $vdir VDIR;
  94.             }
  95.             if (!inizia_per($file$vdir)) {
  96.                 $file path($vdir$file);
  97.             }
  98.         }
  99.     
  100.         return path($host$file);
  101.     else {
  102.         return basename($file);
  103.     }
  104.  
  105. }
  106.  
  107. /**
  108.  * genera una URL con checksum per impedire la modifica manuale della URL
  109.  * @param string $location la stringa contenente la url da codificare
  110.  * @param integer $replace_amp indica come trasformare le & (vedere {@link URL_AMP_DEFAULT}{@link URL_AMP_XML}{@link URL_AMP_PHP}
  111.  * @return string 
  112.  */
  113. function url_codifica($location$replace_amp URL_AMP_DEFAULT)
  114. {
  115.     // se arriva una stringa vuota o non devo fare nulla, non faccio nulla
  116.     if (strlen($location== || !constant_true('CHECKSUM_LINK')) {
  117.         return $location;
  118.     }
  119.     // se arriva una stringa senza ? viene accodato
  120.     if (strpos($location'?'=== FALSE{
  121.         $location $location.'?';
  122.     }
  123.  
  124.     $location preg_replace(AMP_SEARCHAMP_REPLACE$location);
  125.     $location str_replace(' ''%20'$location);
  126.     
  127.     if !inizia_per($locationPROTO) ) {
  128.         if (defined('VDIR'&& strlen(VDIR))
  129.             $location path(PROTO.$_SERVER['HTTP_HOST']path(VDIR$location));
  130.         else
  131.             $location path(PROTO.$_SERVER['HTTP_HOST']$location);
  132.     }
  133.  
  134.     $location .= "&amp;CHK=".bin2hex(mhash(MHASH_MD5$locationCHECKSUM_PASSWORD));
  135.  
  136.     if ($replace_amp == URL_AMP_PHP)
  137.         $location str_replace('&amp;''&'$location);
  138.  
  139.     return $location;
  140. }
  141.  
  142. /**
  143.  * Controlla se la url passata e' coerente con il checksum previsto
  144.  * @param string $location la stringa contenente la url da verificare (se null, viene presa l'ultima pagina richiesta)
  145.  * @return boolean 
  146.  */
  147. function url_valida($location NULL)
  148. {
  149.     // se non devo fare nulla, va bene dibbase
  150.     if (!constant_true('CHECKSUM_LINK'))
  151.         return TRUE;
  152.  
  153.     // se location e' null, faccio la validazione sulla pagina corrente
  154.     if (strlen($location== 0)
  155.         $location PROTO.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  156.  
  157.     // rimetto a posto le &
  158.     $location preg_replace(AMP_SEARCHAMP_REPLACE$location);
  159.  
  160.     if (strpos($location'&amp;CHK'=== FALSE)
  161.         return TRUE;
  162.  
  163.     // tolgo la checksum e ricalcolo l'hash
  164.     $location_attesa explode("&amp;CHK",$location);
  165.     $location_attesa url_codifica($location_attesa[0]);
  166.  
  167.     if ($location == $location_attesa{
  168.         return TRUE;
  169.     else {
  170.         log_value("attesa  = ".$location_attesa);
  171.         log_value("passata = ".$location);
  172.         return FALSE;
  173.     }
  174. }
  175.  
  176. ?>

Documentation generated on Thu, 25 Sep 2008 23:29:31 +0200 by phpDocumentor 1.4.0