<?php
// fgc_utf_fix.php
// RJM Programming
// January, 2021
// Help out PHP file_get_content weaknesses regarding UTF-8 data

$myurl="";
$mycont="";
$mybsf="";

if (isset($_POST['myurl'])) {
$myurl=str_replace("+", " ",urldecode($_POST['myurl']));
$mycont=''; //file_get_contents("http://www.rjmprogramming.com.au/PHP/fgc/?tdinto=divinv&inurl=" . $_POST['myurl']);
$mybsf=''; //$mycont;

echo "<html>
<head>
<title>PHP file_get_contents and UTF-8 Contents via [iframe].srcdoc=[HTMLcontent] - RJM Programming - January, 2021</title>
<meta charset='UTF-8'>
<script type=text/javascript>

 var mybsf=\"" . $mybsf . "\";
 var absval='';
 var wasino=null;
 var zhr=null;

 function showStuff(evt) {
  if (zhr.readyState == 4) {
    if (zhr.status == 200) {
      absval=zhr.responseText;
      document.getElementById('tdxifsd').innerHTML='<iframe style=\"width:100%;height:800px;\" id=xifsd onload=checkif(this); src=\"data:text/html;base64,' + absval + '\"></iframe>';
    }
  }
 }
 
 function ajaxit() {
  zhr = new XMLHttpRequest();
  var form=new FormData();
  form.append('inurl', '" . str_replace("https:", "http:", $myurl) . "');
  form.append('tdinto', 'divinv');
  if (document.URL.indexOf('localhost:8888') != -1) {
  zhr.open('post', 'http://localhost:8888/fgc/?rand=' + Math.floor(Math.random() * 198765432), true);
  } else {
  zhr.open('post', '//www.rjmprogramming.com.au/PHP/fgc/?rand=' + Math.floor(Math.random() * 198765432), true);
  }
  zhr.onreadystatechange = showStuff;
  zhr.send(form);
  preshowBelow();
 }
 
 function ourdecodeURIComponent(xin) {
   return xin;
 }  
 
 function preshowBelow() {
   if (absval != '') {
    showBelow(wasino);
    absval='';
   } else {
    setTimeout(preshowBelow, 2000);
   }
 }

 function showBelow(ino) {
 }
  
 function repslist(insa) {  // Thanks to https://gist.github.com/kasperkamperman/198c6389840532b96069ba6a776d69e6
            var outsa=insa;
            var ists=['â', '-', 'â€š','‚', 'â€ž','„', 'â€¦','…', 'â€¡','‡',
            'â€°','‰', 'â€¹','‹', 'â€˜','‘', 'â€™','’',
            'â€œ','“', 'â€¢','•', 'â€“','–', 'â€”','—',
            'â„¢','™', 'â€º','›', 'â‚¬','€',
            'Ã‚' ,'Â', 'Æ’' ,'ƒ', 'Ãƒ' ,'Ã', 'Ã„' ,'Ä',
            'Ã…' ,'Å', 'â€' ,'†', 'Ã†' ,'Æ', 'Ã‡' ,'Ç',
            'Ë†' ,'ˆ', 'Ãˆ' ,'È', 'Ã‰' ,'É', 'ÃŠ' ,'Ê',
            'Ã‹' ,'Ë', 'Å’' ,'Œ', 'ÃŒ' ,'Ì', 'Å½' ,'Ž',
            'ÃŽ' ,'Î', 'Ã‘' ,'Ñ', 'Ã’' ,'Ò', 'Ã“' ,'Ó',
            'â€' ,'”', 'Ã”' ,'Ô', 'Ã•' ,'Õ', 'Ã–' ,'Ö',
            'Ã—' ,'×', 'Ëœ' ,'˜', 'Ã˜' ,'Ø', 'Ã™' ,'Ù',
            'Å¡' ,'š', 'Ãš' ,'Ú', 'Ã›' ,'Û', 'Å“' ,'œ',
            'Ãœ' ,'Ü', 'Å¾' ,'ž', 'Ãž' ,'Þ', 'Å¸' ,'Ÿ',
            'ÃŸ' ,'ß', 'Â¡' ,'¡', 'Ã¡' ,'á', 'Â¢' ,'¢',
            'Ã¢' ,'â', 'Â£' ,'£', 'Ã£' ,'ã', 'Â¤' ,'¤',
            'Ã¤' ,'ä', 'Â¥' ,'¥', 'Ã¥' ,'å', 'Â¦' ,'¦',
            'Ã¦' ,'æ', 'Â§' ,'§', 'Ã§' ,'ç', 'Â¨' ,'¨',
            'Ã¨' ,'è', 'Â©' ,'©', 'Ã©' ,'é', 'Âª' ,'ª',
            'Ãª' ,'ê', 'Â«' ,'«', 'Ã«' ,'ë', 'Â¬' ,'¬',
            'Ã¬' ,'ì', 'Â®' ,'®', 'Ã®' ,'î', 'Â¯' ,'¯',
            'Ã¯' ,'ï', 'Â°' ,'°', 'Ã°' ,'ð', 'Â±' ,'±',
            'Ã±' ,'ñ', 'Â²' ,'²', 'Ã²' ,'ò', 'Â³' ,'³',
            'Ã³' ,'ó', 'Â´' ,'´', 'Ã´' ,'ô', 'Âµ' ,'µ',
            'Ãµ' ,'õ', 'Â¶' ,'¶', 'Ã¶' ,'ö', 'Â·' ,'·',
            'Ã·' ,'÷', 'Â¸' ,'¸', 'Ã¸' ,'ø', 'Â¹' ,'¹',
            'Ã¹' ,'ù', 'Âº' ,'º', 'Ãº' ,'ú', 'Â»' ,'»',
            'Ã»' ,'û', 'Â¼' ,'¼', 'Ã¼' ,'ü', 'Â½' ,'½',
            'Ã½' ,'ý', 'Â¾' ,'¾', 'Ã¾' ,'þ', 'Ä', 'ć', 'Â¿' ,'¿',
            'Ã¿' ,'ÿ', 'Ã€' ,'À',
            'Ã','Á', 'Å','Š', 'Ã','Í', 'Ã','Ï',
            'Ã','Ð', 'Ã','Ý', 'Ã','à', 'Ã­','í'];
            
            for (var ijk=0; ijk<ists.length; ijk+=2) {
              while (outsa.indexOf(ists[ijk]) != -1) {
                outsa=outsa.replace(ists[ijk], ists[1 + ijk]);
              }
            }
            return outsa;
 }

 
 function checkif(iois) {
  if (iois != null) {
    //console.log('' + iois.src);
    if (('' + iois.src).indexOf('data:') == 0 && ('' + iois.src).indexOf(';base64,') != -1) {  // src=data:text/html;base64,[base64HTMLContent] comes here
     var ioissrc=('' + iois.src); 
     document.getElementById('tdxifsd').innerHTML='<iframe style=\"width:100%;height:800px;\" id=xifsd srcdoc=\"\"></iframe>';
     var atb=atob(('' + ioissrc).split(';base64,')[1]);
     document.getElementById('xifsd').srcdoc=atb;
     var xbodyt=repslist(atb);
     document.getElementById('ifsd').srcdoc=xbodyt;
     document.getElementById('ifsd').style.display='block';
    }
  }
 }

 
</script>
</head>
<body onload=ajaxit();>
<h1>PHP file_get_contents and UTF-8 Contents via [iframe].srcdoc=[HTMLcontent]</h1>
<h2>RJM Programming</h2>
<h3>January, 2021</h3>
<h4>Thanks to <a target=_blank title='Thanks to https://gist.github.com/kasperkamperman/198c6389840532b96069ba6a776d69e6' href='https://gist.github.com/kasperkamperman/198c6389840532b96069ba6a776d69e6'>https://gist.github.com/kasperkamperman/198c6389840532b96069ba6a776d69e6</a></h4>
<table border=5 style=width:95%;>
<tr><th>PHP file_get_contents <br>onto [iframe].srcdoc=[HTMLcontent] <br>(supplemented by UTF-8 mappings) <br>of " . $myurl . "</th><th>PHP file_get_contents <br>onto [iframe].srcdoc=[HTMLcontent] <br>of " . $myurl . "</th></tr>
<tr><td id=tdifsd><iframe style='width:100%;height:800px;display:none;' id=ifsd srcdoc=\"\"></iframe></td><td id=tdxifsd></td></tr>
</table>
</body>
</html>";

} else {

echo "<html>
<head>
<title>PHP file_get_contents and UTF-8 Contents via [iframe].srcdoc=[HTMLcontent] - RJM Programming - January, 2021</title>
<meta charset='UTF-8'>
<script type=text/javascript>

 function checkval(iois) {
   if (iois.value.trim() != '') {
     if (iois.value.indexOf('//') == 0) {
       iois.value='http:' + iois.value;
       document.getElementById('sbut').click();
     } else if (iois.value.toLowerCase().indexOf('http') == 0) { 
       document.getElementById('sbut').click();
     } else if (iois.value.indexOf('/') == 0) {
       iois.value='http://' + document.URL.split('//')[1].split('?')[0].split('#')[0].split('/')[0] + iois.value;
       document.getElementById('sbut').click();
     } else {
       iois.value='http://' + document.URL.split('//')[1].split('?')[0].split('#')[0].replace('fgc_utf_fix.php', iois.value);
       document.getElementById('sbut').click();
     }
   }
 }
  

</script>
</head>
<body>
<h1>PHP file_get_contents and UTF-8 Contents via [iframe].srcdoc=[HTMLcontent]</h1>
<h2>RJM Programming</h2>
<h3>January, 2021</h3>
<h4>Thanks to <a target=_blank title='Thanks to https://gist.github.com/kasperkamperman/198c6389840532b96069ba6a776d69e6' href='https://gist.github.com/kasperkamperman/198c6389840532b96069ba6a776d69e6'>https://gist.github.com/kasperkamperman/198c6389840532b96069ba6a776d69e6</a></h4>
<form action=./fgc_utf_fix.php method=POST>
<input type=url style=width:80%; onblur=checkval(this); name=myurl id=myurl value='' placeholder='Please enter an absolute URL to view ... eg. http://www.wikipedia.org/wiki/Einstein'></input>
<input type=submit id=sbut value=Submit style=display:none;></input>
</form>
<input type=text style='position:absolute;left-2345px;top:-456px;' value=''></input>
</body>
</html>";

}
?>
