<?php
// putElementsBy.php
// Set up opposite idea to getElementsBy
// RJM Programming
// November, 2014

$url = "";
$selclass = "";
$selid = "";
$seltag = "";
$onload = "";
if (isset($_GET['url'])) {
  $url = urldecode($_GET['url']);
} else if (isset($_POST['url'])) {
  $url = urldecode($_POST['url']);
}
if (isset($_GET['selid'])) {
  $selid = urldecode($_GET['selid']);
}
if (isset($_POST['selid'])) {
  $selid = urldecode($_POST['selid']);
}
if ($selid != "") {
  $onload = " onload='preaddele(\"" . $selid . "\",\"id\",\"" . $url . "\");'";
  $selid = "";
}
if (isset($_GET['selclass'])) {
  $selclass = urldecode($_GET['selclass']);
}
if (isset($_POST['selclass'])) {
  $selclass = urldecode($_POST['selclass']);
}
if ($selclass != "") {
  $onload = " onload='preaddele(\"" . $selclass . "\",\"class\",\"" . $url . "\");'";
  $selclass = "";
}
if (isset($_GET['seltag'])) {
  $seltag = urldecode($_GET['seltag']);
}
if (isset($_POST['seltag'])) {
  $seltag = urldecode($_POST['seltag']);
}
if ($seltag != "") {
  $onload = " onload='preaddele(\"" . $seltag . "\",\"tag\",\"" . $url . "\");'";
  $seltag = "";
}


function overview($stuff) {
  global $url;
  if (strpos($stuff, "Please ") !== false || 1 == 1) {
    return "<form action='./putElementsBy.php?justlook=y' method='POST'><input type='hidden' id='url' name='url' value='" . str_replace("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']), "", $url) . "'></input>" . $stuff . "<input type='submit' id='submit' value='Submit' style='display:none;'></input>";
  } 
  return $stuff;
}

if (isset($_GET['justlook']) || isset($_GET['url']) || (!isset($_GET['justlooking']) && !isset($_GET['url']) && !isset($_POST['url']))) {
$classarray = array();
$sclass = array(" class=", " CLASS=", " Class=");
$selclass = "<select name='selclass' id='selclass' onchange='addele(this.value,\"class\");' value='' style='display:none;'></select><br>";
$idarray = array();
$sid = array(" id=", " ID=", " Id=");
$selid = "<select name='selid' id='selid' onchange='addele(this.value,\"id\");' value='' style='display:none;'></select><br>";
$tagarray = array();
$stag = array("<");
$seltag = "<select name='seltag' id='seltag' onchange='addele(this.value,\"tag\");' value='' style='display:none;'></select><br>";
$outtag = array("head", "script", "link", "meta", "html", "!doctype", "body", "style", "title");

$url = "";
$acont = "";
$cont = "";
if (isset($_GET['justlooking'])) {
  if (!isset($_GET['url']) && !isset($_POST['url'])) {
   $url = "";
   $selclass = "";
   $selid = "";
   $seltag = "";
  }
} 
if (isset($_GET['url'])) {
  $url = urldecode($_GET['url']);
  if ($url != "") {
   $acont = file_get_contents($url);
   $cont = str_replace(" = ", "=", $acont);
  } else {
   $acont = file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']) . "?justlooking=");
   $cont = str_replace(" = ", "=", $acont);
  }
} else if (isset($_POST['url'])) {
  $url = urldecode($_POST['url']);
  if ($url != "") {
   $acont = file_get_contents($url);
   $cont = str_replace(" = ", "=", $acont);
  } else {
   $acont = file_get_contents("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']) . "?justlooking=");
   $cont = str_replace(" = ", "=", $acont);
  }
} else {
  $url = "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']);
  $acont = file_get_contents($url . "?justlooking=");
  $cont = str_replace(" = ", "=", $acont);
}

function classadd($thing) {
    global $classarray, $selclass;
    if (!in_array($thing, $classarray)) {
     $selclass = str_replace(" style='display:none;'>", "><option value=''>Please optionally select class names to put</option>", $selclass);
     $classarray[sizeof($classarray)] = $thing;
     $selclass = str_replace("</select>", "<option value='" . $thing . "'>" . $thing . "</option></select>", $selclass);
    }
}

function idadd($thing) {
    global $idarray, $selid;
    if (!in_array($thing, $idarray)) {
     $selid = str_replace(" style='display:none;'>", "><option value=''>Please optionally select an id to put</option>", $selid);
     $idarray[sizeof($idarray)] = $thing;
     $selid = str_replace("</select>", "<option value='" . $thing . "'>" . $thing . "</option></select>", $selid);
    }
}

function tagadd($thing) {
    global $tagarray, $seltag, $outtag;
    if (!in_array(strtolower($thing), $tagarray)) {
     if (substr($thing, 0, 1) != "/" && substr($thing, 0, 1) != "!") {
      if (!in_array(strtolower($thing), $outtag)) {
       $seltag = str_replace(" style='display:none;'>", "><option value=''>Please optionally select tag names to put</option>", $seltag);
       $tagarray[sizeof($tagarray)] = strtolower($thing);
       $seltag = str_replace("</select>", "<option value='" . strtolower($thing) . "'>" . strtolower($thing) . "</option></select>", $seltag);
      }
     }
    }
}

if (isset($_GET['justlook']) || $cont != "") { //$url != "" && !isset($_GET['url']) && !isset($_POST['url'])) {
$xtag = explode($stag[0], str_replace(">", " ", str_replace("/>", "  ", $cont)));
for ($i=1; $i<sizeof($xtag); $i++) {
 $xxtag = explode(" ", $xtag[$i]);
 tagadd($xxtag[0]);
}

for ($m=0; $m<sizeof($sclass); $m++) {
 $xclass = explode($sclass[$m], $cont);
 for ($i=1; $i<sizeof($xclass); $i++) {
  $k = 1;
  if (substr($xclass[$i], 0, 1) == "'") {
   $xxxclass = explode("'", substr($xclass[$i], 1));
   $xxclass = explode(" ", $xxxclass[0]);
   for ($j=0; $j<($k * sizeof($xxclass)); $j++) {
    classadd($xxclass[$j]);
   }
  } else if (substr($xclass[$i], 0, 1) == '"') {
   $xxxclass = explode('"', substr($xclass[$i], 1));
   $xxclass = explode(" ", $xxxclass[0]);
   for ($j=0; $j<($k * sizeof($xxclass)); $j++) {
    classadd($xxclass[$j]);
   }
  } else {
   $k = 0;
   $xxclass = explode(" ", $xclass[$i]);
   classadd($xxclass[0]);
  }
 }
}

for ($m=0; $m<sizeof($sid); $m++) {
 $xid = explode($sid[$m], $cont);
 for ($i=1; $i<sizeof($xid); $i++) {
  $k = 1;
  if (substr($xid[$i], 0, 1) == "'") {
   $xxxid = explode("'", substr($xid[$i], 1));
   $xxid = explode(" ", $xxxid[0]);
   for ($j=0; $j<($k * sizeof($xxid)); $j++) {
    idadd($xxid[$j]);
   }
  } else if (substr($xid[$i], 0, 1) == '"') {
   $xxxid = explode('"', substr($xid[$i], 1));
   $xxid = explode(" ", $xxxid[0]);
   for ($j=0; $j<($k * sizeof($xxid)); $j++) {
    idadd($xxid[$j]);
   }
  } else {
   $k = 0;
   $xxid = explode(" ", $xid[$i]);
   idadd($xxid[0]);
  }
 }
}
}

}

?>
<!doctype HTML>
<html>
<head>
<title>The putElement(s)By Webpage - RJM Programming - November, 2014</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<script type='text/javascript'>

var cis = null;
var t = null;
var m = null;
var w = null;

function callpreaddele() {
  preaddele("", "", "");
}

function preaddele(tval, mode, wurl) {
  var wis = window.top;
  if (tval == "" && mode == "" && wurl == "") {
    tval = t;
    mode = m;
    wurl = w;
  } else {
    t = tval;
    m = mode;
    w = wurl;
  }
  if (wurl != "") {
    if (cis == null) cis = window.open(wurl, "_blank", "top=150,left=600,width=50,height=50");
    wis = cis;
  }
  try {
  if (mode == "class") {
    putElementsByClassName(tval);
  } else if (mode == "id") {
    putElementById(tval);
  } else if (mode == "tag") {
    putElementsByTagName(tval);
  }
  } catch (ex) {
    if (1 == 2) document.body.innerHTML = document.body.innerHTML + "<p>" + ex.message + "</p>";
    setTimeout(callpreaddele, 1000);
  }
}

function putElementById(ttval) {
    var wis = window.top;
    if (cis != null) wis = cis;
    var mycs = wis.document.getElementById(ttval);
    if (mycs == null && cis != null) {
     setTimeout(callpreaddele, 1000);
    } else if (mycs.length == 0 && cis != null) {
     setTimeout(callpreaddele, 1000);
    } else {
     document.body.innerHTML = document.body.innerHTML + mycs.outerHTML;
    }
}


function putElementsByClassName(ttval) {
    var wis = window.top;
    if (cis != null) wis = cis;
    var mycs = wis.document.getElementsByClassName(ttval);
    if (mycs == null && cis != null) {
    setTimeout(callpreaddele, 1000);
    } else if (mycs.length == 0 && cis != null) {
    setTimeout(callpreaddele, 1000);
    } else {
    for (var ic=(mycs.length - 1); ic>=0; ic--) {
     document.body.innerHTML = document.body.innerHTML + mycs[ic].outerHTML;
    }
    }
}

function putElementsByTagName(ttval) {
    var wis = window.top;
    if (cis != null) wis = cis;
    var mycs = wis.document.getElementsByTagName(ttval);
    if (mycs == null && cis != null) {
    setTimeout(callpreaddele, 1000);
    } else if (mycs.length == 0 && cis != null) {
    setTimeout(callpreaddele, 1000);
    } else {
    for (var ic=(mycs.length - 1); ic>=0; ic--) {
     document.body.innerHTML = document.body.innerHTML + mycs[ic].outerHTML;
    }
    }
}

function addele(tval, mode) {
  if (mode == "class") {
    document.getElementById('submit').click();
    putElementsByClassName(tval);
  } else if (mode == "id") {
    document.getElementById('submit').click();
    putElementById(tval);
  } else if (mode == "tag") {
    document.getElementById('submit').click();
    putElementsByTagName(tval);
  }
}

</script>
</head>
<body<?php echo $onload; ?>>
<h1>The putElement(s)By Webpage - <br>RJM Programming - November, 2014</h1>
<?php echo overview($selclass . $selid . $seltag); ?>
<div id='ways' class='w'>
ways
</div>
<div id='parts' class='p'>
parts
</div>
<hr>
<p>Under line under here are putElement(s)By HTML elements</p>
<hr>
</body>
</html>
