Advertisement
sebbu

test urls

Nov 4th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.23 KB | None | 0 0
  1. <?php
  2. if(true) { //config
  3. $current_url='http://my_domain.tld/dir1/dir2/my_file.my_ext?page=my_page&p=2#my_anchor';
  4. $hrefs=array(
  5.     'http://my_domain.tld/dir1/dir2/my_file.my_ext?page=my_page&p=2#my_anchor' => 'http://my_domain.tld/dir1/dir2/my_file.my_ext?page=my_page&p=2#my_anchor',
  6.     'http://my_domain.tld/dir1/dir2/my_file.my_ext?page=my_page&p=2' => 'http://my_domain.tld/dir1/dir2/my_file.my_ext?page=my_page&p=2',
  7.     'http://my_domain.tld/dir1/dir2/my_file.my_ext?page=my_page' => 'http://my_domain.tld/dir1/dir2/my_file.my_ext?page=my_page',
  8.     'http://my_domain.tld/dir1/dir2/my_file.my_ext' => 'http://my_domain.tld/dir1/dir2/my_file.my_ext',
  9.     'http://my_domain.tld/dir3/dir4/my_file.my_ext' => 'http://my_domain.tld/dir3/dir4/my_file.my_ext',
  10.     '?page=my_2nd_page#my_2nd_anchor' => 'http://my_domain.tld/dir1/dir2/my_file.my_ext?page=my_2nd_page#my_2nd_anchor',
  11.     '#my_2nd_anchor' => 'http://my_domain.tld/dir1/dir2/my_file.my_ext?page=my_page&p=2#my_2nd_anchor',
  12.     '?p=1' => 'http://my_domain.tld/dir1/dir2/my_file.my_ext?p=1',
  13.     'my_2nd_file.my_ext' => 'http://my_domain.tld/dir1/dir2/my_2nd_file.my_ext',
  14.     'my_2nd_file.my_ext?p=1' => 'http://my_domain.tld/dir1/dir2/my_2nd_file.my_ext?p=1',
  15.     'my_2nd_file.my_ext#test' => 'http://my_domain.tld/dir1/dir2/my_2nd_file.my_ext#test',
  16.     'my_2nd_file.my_ext?p=1#test' => 'http://my_domain.tld/dir1/dir2/my_2nd_file.my_ext?p=1#test',
  17.     '../another_file.my_ext' => 'http://my_domain.tld/dir1/another_file.my_ext',
  18.     '../../../../../file.ext' => 'http://my_domain.tld/file.ext',
  19.     '/image.jpg' => 'http://my_domain.tld/image.jpg',
  20.     '/../image.jpg' => 'http://my_domain.tld/image.jpg',
  21.     '/dir1/image.jpg' => 'http://my_domain.tld/dir1/image.jpg',
  22.     '/dir1/../image.jpg' => 'http://my_domain.tld/image.jpg',
  23.     '/dir1/../../image.jpg' => 'http://my_domain.tld/image.jpg',
  24.     '/dir1/../dir2/../image.jpg' => 'http://my_domain.tld/image.jpg',
  25.     '/dir1/../dir2/image.jpg' => 'http://my_domain.tld/dir2/image.jpg',
  26. );
  27. $urls = array(
  28.     "protocol://my_domain.tld/dir1/dir2/test.htm" => "protocol://my_domain.tld/dir1/dir2/test.htm",
  29.     "protocol://my_domain.tld/dir1/dir2/../test.htm" => "protocol://my_domain.tld/dir1/test.htm",
  30.     "protocol://my_domain.tld/dir1/dir2/../../test.htm" => "protocol://my_domain.tld/test.htm",
  31.     "protocol://my_domain.tld/dir1/dir2/../../../../../../../test.htm" => "protocol://my_domain.tld/test.htm",
  32.     "protocol://my_domain.tld/../../../../../../../test.htm" => "protocol://my_domain.tld/test.htm",
  33.     "protocol://my_domain.tld/../../../../../../../dir1/dir2/test.htm" => "protocol://my_domain.tld/dir1/dir2/test.htm",
  34.     "protocol://my_domain.tld/../../../../../../../dir1/../dir2/test.htm" => "protocol://my_domain.tld/dir2/test.htm",
  35.     "protocol://my_domain.tld/../../../../../../../dir1/../dir2/../test.htm" => "protocol://my_domain.tld/test.htm",
  36.     "protocol://user:pass@my_domain.tld/dir1/dir2/test.htm" => "protocol://user:pass@my_domain.tld/dir1/dir2/test.htm",
  37. );
  38. }
  39. function url_array_to_string($array) {
  40.     $string='';
  41.     if(array_key_exists('scheme', $array)) $string.=$array['scheme'].'://';
  42.     if(array_key_exists('user', $array)) $string.=$array['user'];
  43.     if(array_key_exists('pass', $array)) $string.=':'.$array['pass'];
  44.     if(array_key_exists('user', $array) || array_key_exists('pass', $array)) $string.='@';
  45.     if(array_key_exists('host', $array)) $string.=$array['host'];
  46.     if(array_key_exists('port', $array)) $string.=':'.$array['port'];
  47.     if(array_key_exists('path', $array)) $string.=$array['path'];
  48.     if(array_key_exists('query', $array)) $string.='?'.$array['query'];
  49.     if(array_key_exists('fragment', $array)) $string.='#'.$array['fragment'];
  50.     return $string;
  51. }
  52. //static $flags=HTTP_URL_JOIN_PATH;
  53. //static $flags=HTTP_URL_JOIN_PATH|HTTP_URL_JOIN_QUERY;
  54. function relative_to_absolute($href, $current_url) {
  55.     global $flags;
  56.     $orig=parse_url($current_url);
  57.     $href=parse_url($href);
  58.     $link=array();
  59.     if(array_key_exists('scheme', $href)) $link['scheme']=$href['scheme'];
  60.     else $link['scheme']=$orig['scheme'];
  61.     if(array_key_exists('user', $href)) $link['user']=$href['user'];
  62.     else if(array_key_exists('user',$orig)) $link['user']=$orig['user'];
  63.     if(array_key_exists('pass', $href)) $link['pass']=$href['pass'];
  64.     else if(array_key_exists('pass',$orig)) $link['pass']=$orig['pass'];
  65.     if(array_key_exists('host', $href)) $link['host']=$href['host'];
  66.     else $link['host']=$orig['host'];
  67.     if(array_key_exists('path',$href)) {
  68.         if($href['path'][0]!='/') {
  69.             $link['path']=substr($orig['path'],0,strrpos($orig['path'],'/')).'/'.$href['path'];
  70.         }
  71.         else {
  72.             $link['path']=$href['path'];
  73.         }
  74.     }
  75.     else {
  76.         $link['path']=$orig['path'];
  77.     }
  78.     if(!array_key_exists('scheme',$href) && !array_key_exists('host',$href) && !array_key_exists('path',$href)) {
  79.         if(array_key_exists('query', $href)) $link['query']=$href['query'];
  80.         else $link['query']=$orig['query'];
  81.     }
  82.     else {
  83.         if(array_key_exists('query', $href)) $link['query']=$href['query'];
  84.     }
  85.     if(array_key_exists('fragment',$href)) $link['fragment']=$href['fragment'];
  86.     return url_array_to_string($link);
  87. }
  88. function absolute1($url) {
  89.     $ar=parse_url($url);
  90.     while(substr($ar['path'],0,4)=='/../') $ar['path']=substr($ar['path'],3);
  91.     while(strpos($ar['path'],'../')!==FALSE) {
  92.         $ar['path']=preg_replace('#/([^/]+)/../#','/', $ar['path']);
  93.         while(substr($ar['path'],0,4)=='/../') $ar['path']=substr($ar['path'],3);
  94.     }
  95.     $link=url_array_to_string($ar);
  96.     return $link;
  97. }
  98. function absolute2($url) {
  99.     $ar=parse_url($url);
  100.     $ar['path']=preg_replace('#^/(../)+#','/', $ar['path']);
  101.     while(strpos($ar['path'],'../')!==FALSE) {
  102.         $ar['path']=preg_replace('#/([^/]+)/../#','/', $ar['path']);
  103.         while(substr($ar['path'],0,4)=='/../') $ar['path']=substr($ar['path'],3);
  104.     }
  105.     $link=url_array_to_string($ar);
  106.     return $link;
  107. }
  108. echo '<h1>Relative to Absolute</h1>'."\r\n";
  109. if(true) { //relative to absolute
  110.     $total=count($hrefs);
  111.     $i=0;
  112.     $success=0;
  113.      foreach($hrefs as $k=>$v) {
  114.         echo '<span style="color: blue">ORIGINAL URL</span> : ';
  115.         echo $current_url.'<br/>'."\r\n";
  116.         echo '<span style="color: blue">LINK</span> : ';
  117.         echo $k.'<br/>'."\r\n";
  118.         echo '<span style="color: blue">SIMPLIFIED URL</span> : ';
  119.         echo '=> '.$v.'<br/>'."\r\n";
  120.         $res=relative_to_absolute($k, $current_url);
  121.         echo '<span style="color: '.(($res==$v)?'green':'red').'">RESULT 1 URL</span> : ';
  122.         echo $res.'<br/>'."\r\n";
  123.         $res=absolute1($res);
  124.         echo '<span style="color: '.(($res==$v)?'green':'red').'">RESULT 2 URL</span> : ';
  125.         echo $res.'<br/>'."\r\n";
  126.         $res=($res==$v);
  127.         echo $i.' '.($res?'OK':'FAIL').'<br/>'."\r\n";
  128.         if($res) $success++;
  129.         $i++;
  130.     }
  131.     echo 'total : '.$total.'<br/>'."\r\n";
  132.     echo 'success : '.$success.'<br/>'."\r\n";
  133.     echo 'failure : '.($total-$success).'<br/>'."\r\n";
  134.     echo 'score : '.number_format(($success/$total)*100,2).' %<br/>'."\r\n";
  135. }
  136. echo '<h1>Absolute1</h1>'."\r\n";
  137. if(true) { //absolute1
  138.     $total=count($urls);
  139.     $i=0;
  140.     $success=0;
  141.      foreach($urls as $k=>$v) {
  142.         echo '<span style="color: blue">ORIGINAL URL</span> : ';
  143.         echo $k.'<br/>'."\r\n";
  144.         echo '<span style="color: blue">SIMPLIFIED URL</span> : ';
  145.         echo '=> '.$v.'<br/>'."\r\n";
  146.         $res=absolute1($k);
  147.         echo '<span style="color: '.(($res==$v)?'green':'red').'">RESULT URL</span> : ';
  148.         echo $res.'<br/>'."\r\n";
  149.         $res=($res==$v);
  150.         echo $i.' '.($res?'OK':'FAIL').'<br/>'."\r\n";
  151.         if($res) $success++;
  152.         $i++;
  153.     }
  154.     echo 'total : '.$total.'<br/>'."\r\n";
  155.     echo 'success : '.$success.'<br/>'."\r\n";
  156.     echo 'failure : '.($total-$success).'<br/>'."\r\n";
  157.     echo 'score : '.number_format(($success/$total)*100,2).' %<br/>'."\r\n";
  158. }
  159. echo '<h1>Absolute2</h1>'."\r\n";
  160. if(true) { //absolute2
  161.     $total=count($urls);
  162.     $i=0;
  163.     $success=0;
  164.      foreach($urls as $k=>$v) {
  165.         echo '<span style="color: blue">ORIGINAL URL</span> : ';
  166.         echo $k.'<br/>'."\r\n";
  167.         echo '<span style="color: blue">SIMPLIFIED URL</span> : ';
  168.         echo '=> '.$v.'<br/>'."\r\n";
  169.         $res=absolute2($k);
  170.         echo '<span style="color: '.(($res==$v)?'green':'red').'">RESULT URL</span> : ';
  171.         echo $res.'<br/>'."\r\n";
  172.         $res=($res==$v);
  173.         echo $i.' '.($res?'OK':'FAIL').'<br/>'."\r\n";
  174.         if($res) $success++;
  175.         $i++;
  176.     }
  177.     echo 'total : '.$total.'<br/>'."\r\n";
  178.     echo 'success : '.$success.'<br/>'."\r\n";
  179.     echo 'failure : '.($total-$success).'<br/>'."\r\n";
  180.     echo 'score : '.number_format(($success/$total)*100,2).' %<br/>'."\r\n";
  181. }
  182. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement