<?php
// mapper.php
// RJM Programming
// April, 2017
// Blog match functionality

function someof($sbits, $viapm) {
 $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>";
   for ($j=1; $j<sizeof($zobits); $j++) {
     $title=explode(">",$zobits[$j])[-1 + sizeof(explode(">",$zobits[$j]))];
     if (eregi($viapm, $title)) {
       $obits.="\n<option " . str_replace("_blank","myi",str_replace("windowopen","window.open",explode("<option ", $zobits[$j])[-1 + sizeof(explode("<option ", $zobits[$j]))])) . "</option>";
     }
   }
   $obits.="</select>";
 }
 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) {
   $allselbits="<select onchange='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/itblog/match/\" + encodeURIComponent(huh); }  }  } ' size=1 id='tuts'" . someof(explode('</select>', $bits[1])[0], urldecode($_GET['pm'])) . '</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 {
   echo 'huh' . strlen($pmval);
 }
} else {  // got here via .htaccess in http://www.rjmprogramming.com.au/itblog/match/ web server directory
 echo "
<!doctype html>
<html>
<head>
<script type='text/javascript'>

var pmval='';

function atmatch() {
 if (document.URL.indexOf('/itblog/match/') != -1) {
  pmval=document.URL.split('/itblog/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/itblog/match/mapper.php?pm=' + encodeURIComponent(pmval);
 }
}

</script>
</head>
<body onload=' atmatch(); '>
</body>
</html>
";
}
?>

