<?php
// slotin.php
// RJM Programming
// September, 2015
// Needs slotin.html

$beforeselected="";
$rest="";
$resttop="";
$urlis="";
$position="Nearby";
 if (isset($_GET['position'])) { if (strlen($_GET['position']) > 0) $position=$_GET['position']; }
 if (isset($_POST['position'])) { if (strlen($_POST['position']) > 0) $position=$_POST['position']; }
 if ($position == "Before") $beforeselected=" selected";

function relative_to_absolute($inth) {
    global $urlis;
    $ub="http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
    if ($urlis != "") {
      $ubf=explode("?", $urlis);
      $ubff=explode("/", $ubf[0]);
      if (strpos($ubff[sizeof($ubff) - 1], ".") !== false) {
       $ub=str_replace("/" . $ubff[sizeof($ubff) - 1], "/", $ubf[0]);
     } else {
       $ub=str_replace("@$@", "", str_replace("/@$@", "/", ($ubf[0] . "@$@")));
      }
      $urlis="";
    }
    $ubf=explode("?", $ub);
    $udirname=str_replace("/slot" . "in.php", "/", $ubf[0]);
    //$low = strtolower($inth);
    $outth = $inth;
    $ideas = array(" action='", ' action="', " Action='", ' Action="', " ACTION='", ' ACTION="', " action=", " Action=", " ACTION=", " href='", ' href="', " Href='", ' Href="', " HREF='", ' HREF="', " href=", " Href=", " HREF=", " src='", ' src="', " Src='", ' Src="', " SRC='", ' SRC="', " src=", " Src=", " SRC=");
    if ($udirname != "") {
     for ($m=0; $m<sizeof($ideas); $m++) {
      $huhs = explode($ideas[$m], $inth);
      if (sizeof($huhs) > 1) {
        for ($ii=(sizeof($huhs) - 1); $ii>=1; $ii--) {
          if (strtolower(str_replace("file", "http", strtolower(substr($huhs[$ii], 0, 4)))) == "http") {
            $outth = $outth;
          } else if (substr($huhs[$ii], 0, 1) != "/" && substr($huhs[$ii], 0, 1) != "'" && substr($huhs[$ii], 0, 1) != '"') {
            $outth = str_replace($huhs[$ii], $udirname . $huhs[$ii], $outth);
          } else if (substr($huhs[$ii], 1, 1) != "/" && substr($huhs[$ii], 0, 1) != "'" && substr($huhs[$ii], 0, 1) != '"') {
            $outth = str_replace($huhs[$ii], substr($udirname, 0, (strlen($udirname) - 1)) . $huhs[$ii], $outth);
          }
        }
      }
     }
    }
    return $outth;
}

function decide($suff) {
 global $resttop, $rest, $urlis, $position;
 if (strlen($suff) > 1300 || $position == "Before") {
  if ($position == "Before") {
    $resttop.=relative_to_absolute(urldecode($suff));
  } else {
    $rest.=relative_to_absolute(urldecode($suff));
  }
  return "";
 } 
 return urlencode(relative_to_absolute(urldecode($suff)));
}

function massage($ins) {
 global $resttop, $rest, $urlis;
 $newins=str_replace("<BODY", "<body", str_replace("</BODY", "</body", $ins));
 $prestuff=explode("contentvia%3D", $newins);
 if (sizeof($prestuff) > 1) {
  for ($ii=1; $ii<sizeof($prestuff); $ii++) {
   $bits=explode("&", $prestuff[$ii]);
   $cis=@file_get_contents($bits[0]);
   $urlis=$bits[0];
   $precis=explode("</body>", $cis);
   $postcis=explode("<body", $precis[0]);
   $pcis=explode(">", $postcis[1]);
   $newins=str_replace("via%3D" . $bits[0], "%3D" . decide(urlencode(str_replace($postcis[0], "", str_replace("<body" . $pcis[0] . ">","",$precis[0])))), $newins);
  }
 }
 $prestuff=explode("contentvia=", $newins);
 if (sizeof($prestuff) > 1) {
  for ($ii=1; $ii<sizeof($prestuff); $ii++) {
   $bits=explode("&", $prestuff[$ii]);
   $cis=@file_get_contents($bits[0]);
   $urlis=$bits[0];
   $precis=explode("</body>", $cis);
   $postcis=explode("<body", $precis[0]);
   $pcis=explode(">", $postcis[1]);
   $newins=str_replace("via=" . $bits[0], "=" . decide(urlencode(str_replace($postcis[0], "", str_replace("<body" . $pcis[0] . ">","",$precis[0])))), $newins);
  }
 }
 $stuff=explode("content%3D", $newins);
 if (sizeof($stuff) > 1) {
  if ($stuff[1] != "") return $stuff[0] . "content%3D" . decide(str_replace("&", "%26", str_replace("?", "%3f", str_replace("=", "%3d", str_replace(">", "%3e", str_replace("<", "%3c", str_replace($stuff[0] . "content%3D","",$newins)))))));
 }
 $stuff=explode("content=", $newins);
 if (sizeof($stuff) > 1) {
  if ($stuff[1] != "") return $stuff[0] . "content=" . decide(str_replace("&", "%26", str_replace("?", "%3f", str_replace("=", "%3d", str_replace(">", "%3e", str_replace("<", "%3c", str_replace($stuff[0] . "content=","",$newins)))))));
 }
 return str_replace(">", "%3e", str_replace("<", "%3c", $newins));
}

$phpis=@file_get_contents("slotin.php");
$htmlis=@file_get_contents("slotin.html");
$p1='?total=9&delay=1100&url=&content=';
$htmlis=str_replace($p1, "<?php echo \$p1; ?>", $htmlis);
 if (isset($_GET['p1'])) { if (strlen($_GET['p1']) > 0) $p1=massage($_GET['p1']); }
 if (isset($_POST['p1'])) { if (strlen($_POST['p1']) > 0) $p1=massage($_POST['p1']); }
$p2='?total=9&delay=1200&url=&content=';
$htmlis=str_replace($p2, "<?php echo \$p2; ?>", $htmlis);
 if (isset($_GET['p2'])) { if (strlen($_GET['p2']) > 0) $p2=massage($_GET['p2']); }
 if (isset($_POST['p2'])) { if (strlen($_POST['p2']) > 0) $p2=massage($_POST['p2']); }
$p3='?total=9&delay=1300&url=&content=';
$htmlis=str_replace($p3, "<?php echo \$p3; ?>", $htmlis);
 if (isset($_GET['p3'])) { if (strlen($_GET['p3']) > 0) $p3=massage($_GET['p3']); }
 if (isset($_POST['p3'])) { if (strlen($_POST['p3']) > 0) $p3=massage($_POST['p3']); }
$p4='?total=9&delay=1400&url=&content=';
$htmlis=str_replace($p4, "<?php echo \$p4; ?>", $htmlis);
 if (isset($_GET['p4'])) { if (strlen($_GET['p4']) > 0) $p4=massage($_GET['p4']); }
 if (isset($_POST['p4'])) { if (strlen($_POST['p4']) > 0) $p4=massage($_POST['p4']); }
$p5='?total=9&delay=1500&url=&content=';
$htmlis=str_replace($p5, "<?php echo \$p5; ?>", $htmlis);
 if (isset($_GET['p5'])) { if (strlen($_GET['p5']) > 0) $p5=massage($_GET['p5']); }
 if (isset($_POST['p5'])) { if (strlen($_POST['p5']) > 0) $p5=massage($_POST['p5']); }
$p6='?total=9&delay=1600&url=&content=';
$htmlis=str_replace($p6, "<?php echo \$p6; ?>", $htmlis);
 if (isset($_GET['p6'])) { if (strlen($_GET['p6']) > 0) $p6=massage($_GET['p6']); }
 if (isset($_POST['p6'])) { if (strlen($_POST['p6']) > 0) $p6=massage($_POST['p6']); }
$p7='?total=9&delay=1700&url=&content=';
$htmlis=str_replace($p7, "<?php echo \$p7; ?>", $htmlis);
 if (isset($_GET['p7'])) { if (strlen($_GET['p7']) > 0) $p7=massage($_GET['p7']); }
 if (isset($_POST['p7'])) { if (strlen($_POST['p7']) > 0) $p7=massage($_POST['p7']); }
$p8='?total=9&delay=1800&url=&content=';
$htmlis=str_replace($p8, "<?php echo \$p8; ?>", $htmlis);
 if (isset($_GET['p8'])) { if (strlen($_GET['p8']) > 0) $p8=massage($_GET['p8']); }
 if (isset($_POST['p8'])) { if (strlen($_POST['p8']) > 0) $p8=massage($_POST['p8']); }
$p9='?total=9&delay=1900&url=&content=';
$htmlis=str_replace($p9, "<?php echo \$p9; ?>", $htmlis);
 if (isset($_GET['p9'])) { if (strlen($_GET['p9']) > 0) $p9=massage($_GET['p9']); }
 if (isset($_POST['p9'])) { if (strlen($_POST['p9']) > 0) $p9=massage($_POST['p9']); }
 
 $phpform="<div style='background-color:orange;'><form action='./slot" . "in.php' method='POST'><input type='hidden' name='position' id='position' value='Nearby'></input>";
 for ($iy=1; $iy<=9; $iy++) {
   $phpform.="<br>Optional Info " . $iy . ": <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p" . $iy . "' value='?total=9&delay=1" . $iy . "00&url=&content='></input>";
 }
 $phpform.="<br><br><input onclick=\" document.getElementById('position').value=document.getElementById('mode').value; \" title='Process external Javascript calls above' style='background-color:teal;' type='submit' value='Look for HTML for URLs Above and/or Slotting Into HTML Content[via=[URL]]'></input></form></div>";
 
 if (strpos(str_replace($phpform,"",$phpis), str_replace(">Bef" . "ore (", "<?php global \$beforeselected; echo \$beforeselected; ?>>Bef" . "ore (",str_replace("<!-" . "-bo" . "dy ","<?php global \$resttop; echo \$resttop; ?><!--bo" . "dy ",str_replace("</body>","<?php global \$rest; echo \$rest; ?></body>",$htmlis)))) === false || isset($_GET['reset'])) {
   $prefix="";
   if (strpos($phpis, "<!doctype" . " html>") !== false) {
     $prefix=substr($phpis, 0, strpos($phpis, "<!doctype" . " html>"));
   }
   file_put_contents("slotin.php", $prefix . str_replace("</select><br>" . "<br>Slo" . "t Into HTML", "</select><br>" . $phpform . "<br>Slo" . "t Into HTML", str_replace(">Bef" . "ore (", "<?php global \$beforeselected; echo \$beforeselected; ?>>Bef" . "ore (",str_replace("<!-" . "-bo" . "dy ","<?php global \$resttop; echo \$resttop; ?><!-" . "-bo" . "dy ",str_replace("</body>","<?php global \$rest; echo \$rest; ?></body>",$htmlis)))));
   header("Location: slotin.php");
 } 
     ?>
<!doctype html>
<html>
<head>
<title>Slotting In HTML Elements - RJM Programming - September, 2015</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<script data-type='script' id='myscr1' src="slotin.js<?php echo $p1; ?>" type="text/javascript"></script>
<script data-type='script' id='myscr2' src="slotin.js<?php echo $p2; ?>" type="text/javascript"></script>
<script data-type='script' id='myscr3' src="slotin.js<?php echo $p3; ?>" type="text/javascript"></script>
<script data-type='script' id='myscr4' src="slotin.js<?php echo $p4; ?>" type="text/javascript"></script>
<script data-type='script' id='myscr5' src="slotin.js<?php echo $p5; ?>" type="text/javascript"></script>
<script data-type='script' id='myscr6' src="slotin.js<?php echo $p6; ?>" type="text/javascript"></script>
<script data-type='script' id='myscr7' src="slotin.js<?php echo $p7; ?>" type="text/javascript"></script>
<script data-type='script' id='myscr8' src="slotin.js<?php echo $p8; ?>" type="text/javascript"></script>
<script data-type='script' id='myscr9' src="slotin.js<?php echo $p9; ?>" type="text/javascript"></script>
</script>
</head>
<body style='background-color: lightgreen;'>
<?php global $resttop; echo $resttop; ?><!--body onload="firstoff();">
<a href='#' title='here'>here</a><br>
<h1 id='myh1'>myh1</h1><br>
<h2 id='myh2'>myh2</h2><br>
<input id='mybut' type='button' value='button'>but</input>
<p>and there</p-->
<h1 id='myh1' class='h1' align='center'>Slotting In HTML Elements <select id='mode' class='select'><option value='Nearby'>Nearby</option><option value='Before'<?php global $beforeselected; echo $beforeselected; ?>>Before (default for innerHTML blank)</option><option value='After'>After (default for innerHTML not blank)</option></select></h1>
<h3 id='myh3' class='h3' align='center'>RJM Programming</h3>
<h4 id='myh4' class='h4' align='center'>September, 2015</h4>
<p id='myp' class='p' align='center'>...</p>
<div id='mydiv' class='div' align='center' style='background-color:teal;'>
<table id='mytable' class='table' style='width:98%;' cellpadding=2 cellspacing=2>
<tbody id='mytbody' class='tbody' style='background-color: yellow;'>
<tr id='mytrth' class='tr'>
<th id='leftth' class='th' style='width:50%;'>HTML Elements with<a id='lefta' class='a' target=_blank title='HTML DOM innerHTML Property information from w3schools' href='http://www.w3schools.com/jsref/prop_html_innerhtml.asp'> innerHTML </a>not Blank</th>
<th id='rightth' class='th' style='background-color: pink;'>HTML Elements with Blank <a id='righta' class='a' target=_blank title='HTML DOM innerHTML Property information from w3schools' href='http://www.w3schools.com/jsref/prop_html_innerhtml.asp'>innerHTML</a></th>
</tr>
<tr id='mytrtd' class='tr'>
<td id='lefttd' class='td' valign='top'><form id='leftform' class='form'><br><br><span style='margin-top:0px;'>Slot In (<a href='#' title='Optionally choose a URL in which to extract HTML elements available to slot into others here' onclick="changedhtml('');">Url</a>'s) HTML Elements <input id='leftintext' onblur='changedhtml(this.value);' title="id=leftintext class=input type=text" class='input' type='text' style='width:80%;' value="&lt;select&gt;&lt;option value='wow'&gt;wow [action]&lt;/option&gt;&lt;option value='cow'&gt;cow&lt;/option&gt;&lt;/select&gt;" /></span><br><br><select id='leftfromselect' onchange='placeit(this,this.id,this.value);' style='display:none;' class='select'><option value=''>Please, optionally, select element to slot in ...</option></select><br><form action="./slotin.php" method="POST"><br>Optional Info 1: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p1' value='?total=9&delay=1100&url=&content='></input><br>Optional Info 2: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p2' value='?total=9&delay=1200&url=&content='></input><br>Optional Info 3: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p3' value='?total=9&delay=1300&url=&content='></input><br>Optional Info 4: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p4' value='?total=9&delay=1400&url=&content='></input><br>Optional Info 5: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p5' value='?total=9&delay=1500&url=&content='></input><br>Optional Info 6: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p6' value='?total=9&delay=1600&url=&content='></input><br>Optional Info 7: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p7' value='?total=9&delay=1700&url=&content='></input><br>Optional Info 8: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p8' value='?total=9&delay=1800&url=&content='></input><br>Optional Info 9: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p9' value='?total=9&delay=1900&url=&content='></input><br><br><input title='Process external Javascript calls above' style='background-color:teal;' type='submit' value='Look for HTML for URLs Above and/or Slotting Into HTML Content[via=[URL]]'></input></form><br>Slot Into HTML Element <select id='leftintoselect' class='select' style='background-color:cyan;' onchange="raction=this.value; bigthing=this.value; onclickf(document.getElementById('leftintext').value, this.value); "><option value='  ' class='option'>Please choose an action below ...</option><option value='' class='option'>Append HTML</option><option value=' ' class='option'>Place HTML at Top</option><option value="document.getElementsByTagName('th')" class='option' title="document.getElementsByTagName('th')">All TH</option><option value="document.getElementsByTagName('a')" class='option' title="document.getElementsByTagName('a')">All A</option><option id='myoption' value="document.getElementById('myoption')" class='option' title="document.getElementById('myoption')">This OPTION (example of one that is impossible)</option><option value="document.getElementById('mydiv')" class='option' title="document.getElementById('mydiv')">This DIV</option><option value="docgetclass('td','td')" class='option' title="docgetclass('td','td')">Class TD</option><option value="docgetclass('h1','h1')" class='option' title="docgetclass('h1','h1')">Class H1</option><option value="document.getElementById('myh3')" class='option' title="document.getElementById('myh3')">The H3</option><option value="docgetclass('h4','h4')" class='option' title="docgetclass('h4','h4')">Class H4</option><option value="document.getElementsByTagName('p')" class='option' title="document.getElementsByTagName('p')">All P</option></select><br><br></form></td>
<td id='righttd' class='td' style='background-color: pink;' valign='top'><form id='rightform' class='form'><br><br><span style='margin-top:0px;'>Slot In (<a href='#' title='Optionally choose a URL in which to extract HTML elements available to slot into others here' onclick="changedhtml('');">Url</a>'s) HTML Elements <input id='rightintext' onblur='changedhtml(this.value);' title="id=rightintext class=input type=text"  class='input' type='text' style='width:80%;' value="&lt;select&gt;&lt;option value='wow'&gt;wow [action]&lt;/option&gt;&lt;option value='cow'&gt;cow&lt;/option&gt;&lt;/select&gt;" /></span><br><br><select id='rightfromselect' onchange='placeit(this,this.id,this.value);' style='display:none;' class='select'><option value=''>Please, optionally, select element to slot in ...</option></select><br><form action="./slotin.php" method="POST"><br>Optional Info 1: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p1' value='?total=9&delay=1100&url=&content='></input><br>Optional Info 2: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p2' value='?total=9&delay=1200&url=&content='></input><br>Optional Info 3: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p3' value='?total=9&delay=1300&url=&content='></input><br>Optional Info 4: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p4' value='?total=9&delay=1400&url=&content='></input><br>Optional Info 5: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p5' value='?total=9&delay=1500&url=&content='></input><br>Optional Info 6: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p6' value='?total=9&delay=1600&url=&content='></input><br>Optional Info 7: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p7' value='?total=9&delay=1700&url=&content='></input><br>Optional Info 8: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p8' value='?total=9&delay=1800&url=&content='></input><br>Optional Info 9: <input title='Optionally change content= to contentvia=[url] to derive content=[url-content]' style='width:80%;' type='text' name='p9' value='?total=9&delay=1900&url=&content='></input><br><br><input title='Process external Javascript calls above' style='background-color:teal;' type='submit' value='Look for HTML for URLs Above and/or Slotting Into HTML Content[via=[URL]]'></input></form><br>Slot Into HTML Element <select id='rightintoselect' class='select' style='background-color:cyan;' onchange="raction=this.value; bigthing=this.value; onclickf(document.getElementById('rightintext').value, this.value); "><option value='  ' class='option'>Please choose an action below ...</option><option value='' class='option'>Append HTML</option><option value=' ' class='option'>Place HTML at Top</option><option value="document.getElementsByTagName('input')" class='option' title="document.getElementsByTagName('input')">All INPUT</option><option value="docgetclass('input','input')" class='option' title="docgetclass('input','input')">Class INPUT</option><option value="document.getElementsByTagName('img')" class='option' title="document.getElementsByTagName('img')">All IMG</option></select><br><br></form></td>
</tr>
</tbody>
</table>
<iframe id='myiframe' style='display:none;' src=''></iframe>
</div>
<br><br><img id='myimg' class='img' align='center' src='//www.rjmprogramming.com.au/Welcome_files/shapeimage_1d.jpg' title='My Image'></img>
<?php global $rest; echo relative_to_absolute($rest); ?></body>
</html>
