<?php
// mapper.php
// RJM Programming
// April, 2017
// Blog match functionality
$onclick=" onclick=";

function someof($sbits, $viapm) {
 global $onclick;
 $obits="";
 $obits=str_replace('></option>','></OPTION>',$sbits);
 $zobits=explode("</option>",$obits);
 if (sizeof($zobits) > 1) {
   $obits=str_replace("Tutorials (show blog, toggle sort order) ...","Tutorials about " . $viapm . " (or ask about another topic)",$zobits[0]) . "</option>";
   if ($viapm != "") {
   for ($j=1; $j<sizeof($zobits); $j++) {
     $title=explode(">",$zobits[$j])[-1 + sizeof(explode(">",$zobits[$j]))];
     //if (eregi($viapm, $title)) {
     if (preg_match('/' . $viapm . '/i', $title)) {
       $onclick=" onchange=";
       $obits.="\n<option " . str_replace("_blank","myi",str_replace("windowopen","window.open",explode("<option ", $zobits[$j])[-1 + sizeof(explode("<option ", $zobits[$j]))])) . "</option>";
     }
   }
   } 
 }
 return $obits;
}

if (isset($_GET['pm'])) {  // recall scenario
 $pmval=@file_get_contents("http://www.rjmprogramming.com.au/Welcome.html");
 $bits=explode("id='tuts'", $pmval);
 if (sizeof($bits) > 1) {
   $ssomeof=someof(explode('</select>', $bits[1])[0], urldecode($_GET['pm']));
   $allselbits="<select" . $onclick . "'if (this.value.trim().length > 0) { document.getElementById(\"myi\").src=this.value; } else { var huh=prompt(\"Tutorials regarding?\",\"" . urldecode($_GET['pm']) . "\");  if (huh != null) { if (huh != \"\") { if (huh.indexOf(\".\") == 0) { huh=String.fromCharCode(92) + huh; } location.href=\"http://www.rjmprogramming.com.au/itnewblog/match/\" + encodeURIComponent(huh); }  }  } ' size=1 id='tuts'" . $ssomeof . '</select><br><iframe name="myi" id="myi" style="width:100%;height:1400px;" src="http://www.rjmprogramming.com.au/ITblog" title="Robert James Metcalfe Blog"></iframe>';
   echo '<!doctype html><html><head><title>Robert James Metcalfe Blog - Match Blog Title via RegExp</title></head><body>' . $allselbits . '</body></html>';
 }
} else if (isset($_POST['pm'])) {  // recall scenario
 $pmval=@file_get_contents("http://www.rjmprogramming.com.au/Welcome.html");
 $bits=explode("id='tuts'", $pmval);
 if (sizeof($bits) > 1) {  
   $ssomeof=someof(explode('</select>', $bits[1])[0], urldecode($_POST['pm']));
   $allselbits="<select" . $onclick . "'if (this.value.trim().length > 0) { document.getElementById(\"myi\").src=this.value; } else { var huh=prompt(\"Tutorials regarding?\",\"" . urldecode($_POST['pm']) . "\");  if (huh != null) { if (huh != \"\") { if (huh.indexOf(\".\") == 0) { huh=String.fromCharCode(92) + huh; } location.href=\"http://www.rjmprogramming.com.au/itnewblog/match/\" + encodeURIComponent(huh); }  }  } ' size=1 id='tuts'" . $ssomeof . '</select><br><iframe name="myi" id="myi" style="width:100%;height:1400px;" src="http://www.rjmprogramming.com.au/ITblog" title="Robert James Metcalfe Blog"></iframe>';
   echo '<!doctype html><html><head><title>Robert James Metcalfe Blog - Match Blog Title via RegExp</title></head><body>' . $allselbits . '</body></html>';
 } 
} else {  // got here via .htaccess in http://www.rjmprogramming.com.au/itnewblog/match/ web server directory
 echo "
<!doctype html>
<html>
<head>
<script type='text/javascript'>

var pmval='';

function atmatch() {
 var eqis,deqis='',deq=0,lh=[],rh=[],delim='/',pmthere=false,ttype='text readonly style=width:100%; ',bdelim='=';
 if (document.URL.indexOf('/itnewblog/match/') != -1) {
  if (decodeURIComponent(document.URL).indexOf(bdelim) == -1) bdelim='~';
  if (decodeURIComponent(document.URL).indexOf(bdelim) != -1) {
  eqis=decodeURIComponent(document.URL).replace('?#','&').replace('?','&').replace('#','&').split(bdelim); 
  if ('pm' == eqis[0].split(delim)[eval(-1 + eqis[0].split(delim).length)].replace('mapper.php?','').replace('mapper.php&','').toLowerCase()) { 
  pmthere=true;
  ttype='hidden';
  } else {
  deqis='<table border=12><tbody></tbody></table>';
  }
  //alert(eqis[eval(1 + deq)].split('&')[0] + ' vS ' + eqis[0].split(delim)[eval(-1 + eqis[0].split(delim).length)].replace('mapper.php?','').replace('mapper.php&','') + ' Vs ' + eqis.length + ' VS ' + document.URL + ' vs ' + decodeURIComponent(document.URL));
  for (deq=0; deq<eval(-1 + eqis.length); deq++) {
  if (pmthere) {
  deqis+=\"<input type=\" + ttype + \" id=\" + eqis[deq].split(delim)[eval(-1 + eqis[deq].split(delim).length)].replace('mapper.php?','').replace('mapper.php&','') + \" name=\" + eqis[deq].split(delim)[eval(-1 + eqis[deq].split(delim).length)].replace('mapper.php?','').replace('mapper.php&','') + \" value=''></input><br>\";
  } else {
  //alert(eqis[eval(1 + deq)].split('&')[0] + ' vS ' + eqis[0].split(delim)[eval(-1 + eqis[0].split(delim).length)].replace('mapper.php?','').replace('mapper.php&','') + ' Vs ' + eqis.length + ' VS ' + document.URL + ' vs ' + decodeURIComponent(document.URL));
  deqis=deqis.replace('</tbody>','<tr><td id=lh' + eqis[deq].split(delim)[eval(-1 + eqis[deq].split(delim).length)].replace('mapper.php?','').replace('mapper.php&','') + '></td><td id=rh' + eqis[deq].split(delim)[eval(-1 + eqis[deq].split(delim).length)].replace('mapper.php?','').replace('mapper.php&','') + '></td></tr></tbody>');
  }
  lh.push(eqis[deq].split(delim)[eval(-1 + eqis[deq].split(delim).length)].replace('mapper.php?','').replace('mapper.php&',''));
  rh.push(eqis[eval(1 + deq)].split('&')[0]);
  //alert(lh[deq] + '=' + rh[deq]);
  delim='&';
  }
  document.getElementById('dpm').innerHTML=deqis;
  for (deq=0; deq<eval(-1 + eqis.length); deq++) {
  if (pmthere) {
  document.getElementById(lh[deq]).value=rh[deq];
  } else if (1 == 1) {   
  document.getElementById('lh' + lh[deq]).innerHTML='<b>' + lh[deq].replace(/_/g,' ').replace(/-/g,'/') + '</b>';
  document.getElementById('rh' + lh[deq]).innerHTML=rh[deq];
  } else {
  document.getElementById(lh[deq]).value=lh[deq].replace(/_/g,' ').replace(/-/g,'/') + ': ' + rh[deq];
  }
  }
  if (pmthere) {
    document.getElementById('sm').click();
  } else {
    document.getElementById('myform').style.display='block';
  }
  pmval='';
  } else if (location.hash.indexOf('~') != -1) {
  eqis=location.hash.replace('#','').split('~');  
  for (deq=0; deq<eval(-1 + eqis.length); deq++) {
  deqis+=\"<input type=hidden id=\" + eqis[deq] + \" name=\" + eqis[deq] + \" value=''></input>\";
  lh.push(eqis[deq].split('|')[eval(-1 + eqis[deq].split('|').length)]);
  rh.push(eqis[eval(1 + deq)].split('|')[0]);
  }
  document.getElementById('dpm').innerHTML=deqis;
  for (deq=0; deq<eval(-1 + eqis.length); deq++) {
  document.getElementById(lh[deq]).value=rh[deq];
  }
  document.getElementById('sm').click();
  pmval='';
  } else if (location.hash.indexOf('=') != -1) {
  eqis=location.hash.replace('#','').split('=');  
  for (deq=0; deq<eval(-1 + eqis.length); deq++) {
  deqis+=\"<input type=hidden id=\" + eqis[deq] + \" name=\" + eqis[deq] + \" value=''></input>\";
  lh.push(eqis[deq].split('&')[eval(-1 + eqis[deq].split('&').length)]);
  rh.push(eqis[eval(1 + deq)].split('&')[0]);
  }
  document.getElementById('dpm').innerHTML=deqis;
  for (deq=0; deq<eval(-1 + eqis.length); deq++) {
  document.getElementById(lh[deq]).value=rh[deq];
  }
  document.getElementById('sm').click();
  pmval='';
  } else if (location.hash.indexOf('pm=') != -1) {
  pmval=location.hash.split('pm=')[1];
  if (pmval.indexOf('.') == 0) pmval=String.fromCharCode(92) + pmval;
  document.getElementById('dpm').innerHTML=\"<input type=hidden id=pm name=pm value=''></input>\";
  document.getElementById('pm').value=pmval;
  document.getElementById('sm').click();
  pmval='';
  } else {
  pmval=document.URL.split('/itnewblog/match/')[1].split('#')[0];
  }
 } else if (document.URL.indexOf('/ITblog/match/') != -1) {
  pmval=document.URL.split('/ITblog/match/')[1].split('#')[0];
 } else if (location.hash.indexOf('pm=') == 0) {
  pmval=location.hash.replace('pm=','');
 } else if (location.hash.indexOf('pm') == 0) {
  pmval=location.hash.replace('pm','');
 }
 if (pmval.indexOf('.') == 0) pmval=String.fromCharCode(92) + pmval;
 if (pmval != '') {
  location.href='http://www.rjmprogramming.com.au/itnewblog/match/mapper.php?pm=' + encodeURIComponent(pmval);
 }
}

</script>
</head>
<body onload=' atmatch(); '>
<form action='./mapper.php' method='POST' id='myform' style='display:none;'>
<div id='dpm' style='width:100%;'>
</div>
<input type=submit id=sm value=Submit style='display:none;'></input>
</form>
</body>
</html>
";
}
?>

