Public paste
URI
By: fruffl | Date: Feb 17 2010 17:00 | Format: PHP | Expires: never | Size: 7.56 KB | Hits: 877

  1. <?php
  2.  
  3.  
  4.         CLASS ILLI_SequenceBoot_LinkKernel_SegmentUri EXTENDS ILLI_SequenceBoot_LinkKernel_Segment
  5.         {
  6.        
  7.                 private $forwardToUri = '';
  8.        
  9.                 public function init(){}
  10.                
  11.                 public function run()
  12.                 {
  13.                
  14.                         $REGISTRY = $this->_root->Boot->System->Registry;
  15.                         $SESSION  = $this->_root->Boot->Kernel->Params->Session;
  16.                        
  17.                         $this->forwardTo = $this->repair
  18.                         (
  19.                                 $REGISTRY->get()->URI->PATH,
  20.                                 $REGISTRY->get()->URI->ROOT,
  21.                                 $REGISTRY->get()->URI->BASE,
  22.                                 $REGISTRY->get()->PARAMS->GET
  23.                         );
  24.                                
  25.                         $forward = FALSE;
  26.                        
  27.                         if($REGISTRY->get()->SERVER->REQUEST == 'POST')
  28.                         {
  29.                                
  30.                                 $SESSION->add
  31.                                 (
  32.                                         'POST',
  33.                                         $REGISTRY->get()->PARAMS->POST
  34.                                 );
  35.                                
  36.                        
  37.                                 if($this->forwardTo === FALSE)
  38.                                 {
  39.                                         // url accepted: forward to self-uri
  40.                                         $this->forwardTo = $REGISTRY->get()->URI->ROOT;
  41.                                         $forward = TRUE;
  42.                                 }
  43.                                 else
  44.                                 {
  45.                                         // url contains wrong params; forward to repaired uri
  46.                                         $forward = TRUE;
  47.                                 }
  48.                         }
  49.                        
  50.                        
  51.                         if($REGISTRY->get()->SERVER->REQUEST == 'GET')
  52.                         {
  53.                                 if($this->forwardTo === FALSE)
  54.                                 {
  55.                                         // url accepted
  56.                                        
  57.                                         ### PASSED
  58.                                        
  59.                                         // load POST-data from SESSION
  60.                                         $REGISTRY->remove
  61.                                         (
  62.                                                 'PARAMS',
  63.                                                 'POST'
  64.                                         )->add
  65.                                         (
  66.                                                 'PARAMS',
  67.                                                 'POST',
  68.                                                 $SESSION->get('POST')
  69.                                         );
  70.                                        
  71.                                         $SESSION->remove('POST');
  72.                                        
  73.                                 }
  74.                                 else
  75.                                 {
  76.                                         // url contains wrong params; forward to repaired uri
  77.                                         $forward = TRUE;
  78.                                 }
  79.                         }
  80.                        
  81.                         if($forward)
  82.                                 $REGISTRY->add
  83.                                 (
  84.                                         'HEADER',
  85.                                         'location',
  86.                                         $this->forwardTo
  87.                                 );
  88.                 }
  89.  
  90.                 private function repair($env_path, $env_uri, $env_base, ILLI_ArrayObject $clean_query)
  91.                 {
  92.  
  93.                         // repair broken uri-parts
  94.                        
  95.                         // global: url-part wich contains .php are forbidden; base ends alway with '/' to simulate directories
  96.                         // if mod_rewrite is disabled: first arg must be {curpage}.php
  97.                         // create new query-string from cleaned get-vars
  98.                        
  99.                         // kill query-string to cleanup
  100.                         $uri = $env_uri;
  101.                         if(false !== ($i = strpos($uri, '?')))
  102.                                 $uri = substr($env_uri, 0, ($i));                      
  103.                         $uri = rtrim($uri, '/');
  104.                         $valide_uri = $uri;
  105.                        
  106.                        
  107.                         $qry = '';     
  108.                         if($clean_query->count() > 0)
  109.                         {                              
  110.                                 foreach($clean_query as $k => $v)
  111.                                 {
  112.                                         if(is_array($v))
  113.                                         {
  114.                                                 $str .= empty($str) ? '' : '&';
  115.                                                 $str .= $k.'='.implode('|', $clean_query[$k]);
  116.                                         }
  117.                                        
  118.                                         if(is_scalar($v) && $v !== NULL)
  119.                                         {
  120.                                                 $str .= empty($str) ? '' : '&';
  121.                                                 $str .= $k.'='.$v;
  122.                                         }
  123.                                        
  124.                                         if($v === NULL)
  125.                                         {
  126.                                                 $str .= empty($str) ? '' : '&';
  127.                                                 $str .= $k;
  128.                                         }
  129.                                 }                      
  130.                                        
  131.                                 $qry = '?'.$qry.$str;
  132.                         }
  133.                        
  134.                        
  135.                         // environment-base ends never with '.php' if mod_rewrite is enabled: kill it
  136.                                
  137.                         // forward
  138.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php
  139.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/
  140.                         // to
  141.                         // http://localhost/ILLI/vc4000/public_html2/
  142.                        
  143.                         // forward
  144.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php?page=1
  145.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/?page=1
  146.                         // to
  147.                         // http://localhost/ILLI/vc4000/public_html2/?page=1
  148.                        
  149.                         // forward
  150.                         // http://localhost/ILLI/vc4000/public_html2/test
  151.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test
  152.                         // to
  153.                         // http://localhost/ILLI/vc4000/public_html2/test/
  154.                        
  155.                         // forward
  156.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test/foo.php?page=1
  157.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test/foo.php/?page=1
  158.                         // http://localhost/ILLI/vc4000/public_html2/test?page=1
  159.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test?page=1
  160.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test/?page=1
  161.                         // to
  162.                         // http://localhost/ILLI/vc4000/public_html2/test/?page=1
  163.                        
  164.                         if(substr(strtolower(pathinfo($env_base, PATHINFO_EXTENSION)), -3) != 'php')
  165.                         {
  166.                                 // find and kill rootpage:
  167.                                 // http://localhost/ILLI/vc4000/public_html/index.php/foo/bar
  168.                                 // to
  169.                                 // http://localhost/ILLI/vc4000/public_html/foo/bar
  170.                                
  171.                                 $_base = explode('/', $env_base);
  172.                                 $_uri = explode('/', $uri);
  173.                                
  174.                                 $diff = array_merge(array_diff(explode('/', $uri), explode('/', $env_base)));
  175.                                 if(substr(strtolower(pathinfo($diff[0], PATHINFO_EXTENSION)), -3) == 'php')
  176.                                         array_shift($diff);
  177.                                
  178.                                 $uri = implode('/', $_base).'/'.implode('/', $diff);
  179.                                
  180.                                 // find and kill php-ending base-parts:
  181.                                 // http://localhost/ILLI/vc4000/public_html/index.php/foo/bar.php
  182.                                 // to
  183.                                 // http://localhost/ILLI/vc4000/public_html/foo/
  184.                                
  185.                                 if(substr(strtolower(pathinfo($uri, PATHINFO_EXTENSION)), -3) == 'php')
  186.                                 {
  187.                                         $valide_uri = str_replace
  188.                                         (
  189.                                                 '/'.substr
  190.                                                 (
  191.                                                         $uri,
  192.                                                         (
  193.                                                                 strlen
  194.                                                                 (
  195.                                                                         strtolower
  196.                                                                         (
  197.                                                                                 pathinfo
  198.                                                                                 (
  199.                                                                                         $uri,
  200.                                                                                         PATHINFO_FILENAME
  201.                                                                                 )
  202.                                                                         ).'.php'
  203.                                                                 ) * -1
  204.                                                         )
  205.                                                 ),
  206.                                                 NULL,
  207.                                                 $uri
  208.                                         );
  209.                                        
  210.                                 }
  211.                                
  212.                                 $valide_uri = $valide_uri.'/'.$qry;
  213.                         }
  214.                        
  215.                         // mod_rewrite is disabled: kill all *.php-named args (ignore the location '{install_dir}/{curpage}.php')
  216.                                
  217.                         // forward
  218.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/
  219.                         // to
  220.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php
  221.                        
  222.                         // forward
  223.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/?page=1
  224.                         // to
  225.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php?page=1
  226.                        
  227.                         // forward
  228.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test
  229.                         // to
  230.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test/
  231.                        
  232.                         // forward
  233.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test/foo.php?page=1
  234.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test/foo.php/?page=1
  235.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test?page=1
  236.                         // to
  237.                         // http://localhost/ILLI/vc4000/public_html2/{curpage}.php/test/?page=1
  238.                        
  239.                         else
  240.                         {
  241.                                 // if current loacation is "{install_dir}/" forward to {install_dir}/{curpage}.php[/?query-string]
  242.                                 if($env_path == $uri)
  243.                                         $valide_uri =  $env_base;
  244.                                
  245.                                 // if path = '/' your current location is {install_dir}/index.php
  246.                                 if($env_path != '/')
  247.                                 {
  248.                                         // the last part in path can't be an php-file-name
  249.                                         if(substr(strtolower(pathinfo($uri, PATHINFO_EXTENSION)), -3) == 'php')
  250.                                         {
  251.                                                 // the base ({install_dir}/index.php)
  252.                                                 // + path without the last part
  253.                                                 //
  254.                                                 // {install_dir}/index.php/test.php, {install_dir}/index.php/site/test.php...
  255.                                                 // kill test.php and forward to the next parent
  256.                                                 if($uri != $env_base)
  257.                                                 {
  258.                                                         $valide_uri = str_replace
  259.                                                         (
  260.                                                                 '/'.substr
  261.                                                                 (
  262.                                                                         $uri,
  263.                                                                         (
  264.                                                                                 strlen
  265.                                                                                 (
  266.                                                                                         strtolower
  267.                                                                                         (
  268.                                                                                                 pathinfo
  269.                                                                                                 (
  270.                                                                                                         $uri,
  271.                                                                                                         PATHINFO_FILENAME
  272.                                                                                                 )
  273.                                                                                         ).'.php'
  274.                                                                                 ) * -1
  275.                                                                         )
  276.                                                                 ),
  277.                                                                 NULL,
  278.                                                                 $uri
  279.                                                         );
  280.                                                 }
  281.                                                
  282.                                         }
  283.                                 }
  284.                                
  285.                                
  286.                                 if(substr(strtolower(pathinfo($valide_uri, PATHINFO_EXTENSION)), -3) == 'php')
  287.                                 {
  288.                                         if(!empty($qry))
  289.                                                 $valide_uri = $valide_uri.$qry;
  290.                                 }
  291.                                 else
  292.                                 {
  293.                                         if(!empty($qry))
  294.                                                 $valide_uri = $valide_uri.'/'.$qry;
  295.                                 }
  296.                         }
  297.                        
  298.                         if(preg_match('#(.*)?.php#', $env_uri))
  299.                         {
  300.                         }
  301.                        
  302.                        
  303.                         if($_SERVER['REQUEST_URI'] != $valide_uri)
  304.                                 return $valide_uri;
  305.                        
  306.                         return false;
  307.                 }              
  308.         }