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

$url = "";
$selclass = "";
$selid = "";
$seltag = "";
$xselclass = "";
$xselid = "";
$xseltag = "";
$onload = "";
$bextras = "<div id='pextras' style='display:none;'> </div> ";
$firsttag = "";
//$doc = new DOMDocument();
$docb = false;
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 != "") {
  $xselid = $selid;
  $onload = " onload='document.getElementById(\"uluu\").innerHTML = document.getElementById(\"uluu\").innerHTML + \" ... id=\" + \"" . $selid . "\"; preaddele(\"" . $selid . "\",\"id\",\"" . $url . "\");'";
  $selid = "";
}
if (isset($_GET['selclass'])) {
  $selclass = urldecode($_GET['selclass']);
}
if (isset($_POST['selclass'])) {
  $selclass = urldecode($_POST['selclass']);
}
if ($selclass != "") {
  $xselclass = $selclass;
  $onload = " onload='document.getElementById(\"uluu\").innerHTML = document.getElementById(\"uluu\").innerHTML + \" ... class=\" + \"" . $selclass . "\"; preaddele(\"" . $selclass . "\",\"class\",\"" . $url . "\");'";
  $selclass = "";
}
if (isset($_GET['seltag'])) {
  $seltag = urldecode($_GET['seltag']);
}
if (isset($_POST['seltag'])) {
  $seltag = urldecode($_POST['seltag']);
}
if ($seltag != "") {
  $xseltag = $seltag;  
  $onload = " onload='document.getElementById(\"uluu\").innerHTML = document.getElementById(\"uluu\").innerHTML + \" ... tag is \" + \"" . $seltag . "\"; preaddele(\"" . $seltag . "\",\"tag\",\"" . $url . "\");'";
  $seltag = "";
}



function overview($stuff) {
  global $url;
  if (strpos($stuff, "Please ") !== false || 1 == 1) {
    return "<form action='" . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']) . "?justlook=y' method='POST'><br>Url (optional): <input onblur=' location.href=\"" . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI'])  . "?url=\" + encodeURIComponent(this.value); ' type='text' id='url' name='url' value='" . str_replace("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']), "", $url) . "' size=100></input><br><br>" . $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);
}
if (str_replace("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']), "", $url) != "") {
  try {
  //$ch = curl_init();
  //$timeout = 5;
  //curl_setopt($ch, CURLOPT_URL, $url);
  //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  //$html = curl_exec($ch);
  //curl_close($ch);
  //echo $acont;
  $doc = new DOMDocument();
  $doc->preserveWhiteSpace = FALSE;
  $before = libxml_use_internal_errors(true);
  $doc->loadHTML($acont);
  libxml_use_internal_errors($before);  
  $docb = true;
  } catch (Exception $e) {
  throw new Exception( 'Something really wrong', 0, $e);
  }
}

function classadd($thing, $fthing) {
    global $classarray, $selclass, $xselclass, $cont, $bextras, $docb, $doc;
    if ($thing == $xselclass) {
      if ($docb && 1 == 2) {
        foreach ($doc->getElementsByClassName($thing) as $link) {
          $bextras = str_replace(" </div> ", $link->C14N() . "<br> </div> ", $bextras);
		}
		//foreach ($doc->getElementsByTagName($thing)->item(0)->childNodes as $link) {
        //  $bextras = str_replace(" </div> ", $doc->saveXML($link) . "<br> </div> ", $bextras);
        //}
      } else {
      $huh = strpos($cont, $fthing);
      if ($huh !== false) {
        $bthings = explode("<", substr($cont, 0, ($huh + strlen($fthing))));
        $bis = "<" . $bthings[sizeof($bthings) - 1] . " class=" . substr($cont, (($huh + strlen($fthing)) + 8));
        $words = explode(" ", $bthings[sizeof($bthings) - 1]);
        $huh2 = strpos($bis, "</" . $words[0] . ">");
        if ($huh2 !== false) {
          $bextras = str_replace(" </div> ", substr($bis, 0, ($huh2 + strlen("</" . $words[0] . ">"))) . " </div> ", $bextras);
        } else {
          $huh2 = strpos($bis, "/>");
          if ($huh2 !== false) {
            $bextras  = str_replace(" </div> ", substr($bis, 0, ($huh2 + strlen("/>"))) . " </div> ", $bextras);
          } else {
            $huh2 = strpos($bis, ">");
            if ($huh2 !== false) $bextras  = str_replace(" </div> ", substr($bis, 0, ($huh2 + strlen(">"))) . " </div> ", $bextras);
          }
        }
        
      }
      }
    }
    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, $fthing) {
    global $idarray, $selid, $xselid, $cont, $bextras, $docb, $doc;
    if ($thing == $xselid) {
    
      if ($docb) {
       $dobj = $doc->getElementById($thing);
       $bextras  = str_replace(" </div> ", $dobj->C14N() . "<br> </div> ", $bextras);
	   //foreach ($doc->getElementById($thing)->item(0)->childNodes as $link) {
         // $bextras = str_replace(" </div> ", $doc->saveXML($doc->getElementById($thing)->item(0)->childNodes) . "<br> </div> ", $bextras);
       //}
      } else {
    
      $huh = strpos($cont, $fthing);
      if ($huh !== false) {
        $bthings = explode("<", substr($cont, 0, ($huh + strlen($fthing))));
        $bis = "<" . $bthings[sizeof($bthings) - 1] . " id=" . substr($cont, (($huh + strlen($fthing)) + 5));
        $words = explode(" ", $bthings[sizeof($bthings) - 1]);
        $huh2 = strpos($bis, "</" . $words[0] . ">");
        if ($huh2 !== false) {
          $bextras  = str_replace(" </div> ", substr($bis, 0, ($huh2 + strlen("</" . $words[0] . ">"))) . " </div> ", $bextras);
        } else {
          $huh2 = strpos($bis, "/>");
          if ($huh2 !== false) {
            $bextras  = str_replace(" </div> ", substr($bis, 0, ($huh2 + strlen("/>"))) . " </div> ", $bextras);
          } else {
            $huh2 = strpos($bis, ">");
            if ($huh2 !== false) $bextras  = str_replace(" </div> ", substr($bis, 0, ($huh2 + strlen(">"))) . " </div> ", $bextras);
          }
        }
        
        
      }
      }   
     }
    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, $fthing, $xcont) {
    global $tagarray, $seltag, $outtag, $xseltag, $cont, $bextras, $firsttag, $docb, $doc;
    if (($firsttag != "" || $thing == "body" || $thing == "BODY") && strpos($thing, "/") === false && strpos($thing, "*") === false && strpos($thing, "&") === false && strpos($thing, "|") === false && strpos($thing, ",") === false && strpos($thing, "{") === false && strpos($thing, "[") === false && strpos($thing, "(") === false && substr($thing, 0, 1) != "/" && substr($thing, 0, 1) != "!") {
    $firsttag = "body";
    if ($thing == $xseltag) {
      if ($docb) {
        foreach ($doc->getElementsByTagName($thing) as $link) {
          $bextras = str_replace(" </div> ", $link->C14N() . "<br> </div> ", $bextras);
		}
		//foreach ($doc->getElementsByTagName($thing)->item(0)->childNodes as $link) {
        //  $bextras = str_replace(" </div> ", $doc->saveXML($link) . "<br> </div> ", $bextras);
        //}
      } else {
      $huh = strpos($xcont, $fthing . "<");
      if ($huh !== false) {
        $bthings = explode("<", substr($cont, 0, ($huh + strlen($fthing) + 1)));
        $bis = "<" . $bthings[sizeof($bthings) - 1] . $thing . substr($cont, (($huh + strlen($fthing)) + 1 + strlen($thing)));
        $words = explode(" ", substr($bis,1));
        $huh2 = strpos($bis, "</" . $words[0] . ">");
        if ($huh2 !== false) {
          $bextras = str_replace(" </div> ", substr($bis, 0, ($huh2 + strlen("</" . $words[0] . ">"))) . " </div> ", $bextras);
        } else {
          $huh2 = strpos($bis, "/>");
          if ($huh2 !== false) {
            $bextras = str_replace(" </div> ", substr($bis, 0, ($huh2 + strlen("/>"))) . " </div> ", $bextras);
          } else {
            $huh2 = strpos($bis, ">");
            if ($huh2 !== false) $bextras = str_replace(" </div> ", substr($bis, 0, ($huh2 + strlen(">"))) . " </div> ", $bextras);
          }
        }
        
      }
      }
    }
    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'])) {
if (999 == 999) {
$xtag = explode($stag[0], str_replace(">", " ", str_replace("/>", "  ", $cont)));
for ($i=1; $i<sizeof($xtag); $i++) {
    $xxtag = explode(" ", $xtag[$i]);
    if ($docb) {
    try {
    $items = $doc->getElementsByTagName($xxtag[0]);
    if (count($items) > 0) tagadd($xxtag[0], $xid[$i - 1], str_replace(">", " ", str_replace("/>", "  ", $cont)));
    } catch (Exception $ee) {
    }
    } else {
    tagadd($xxtag[0], $xtag[$i - 1], str_replace(">", " ", str_replace("/>", "  ", $cont)));
    }
    
}

}

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++) {
    if ($docb && 1 == 7) {
    $items = $doc->getElementsByClassName($xxclass[$j]);
    if (count($items) > 0) classadd($xxclass[$j], $xclass[$i - 1]);
    } else {
    classadd($xxclass[$j], $xclass[$i - 1]);
    }
   }
  } 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++) {
    if ($docb && 1 == 7) {
    $items = $doc->getElementsByClassName($xxclass[$j]);
    if (count($items) > 0) classadd($xxclass[$j], $xclass[$i - 1]);
    } else {
    classadd($xxclass[$j], $xclass[$i - 1]);
    }
   }
  } else {
   $k = 0;
   $xzxclass = explode(">", $xclass[$i]);
   $xxclass = explode(" ", $xclass[$i]);
    if ($docb && 1 == 7) {
    $items = $doc->getElementsByClassName($xxclass[$j]);
    if (count($items) > 0) classadd($xxclass[0], $xclass[$i - 1]);
    } else if (strlen($xzxclass[0]) < strlen($xxclass[0])) {
    classadd($xzxclass[0], $xclass[$i - 1]);
    } else {
    classadd($xxclass[0], $xclass[$i - 1]);
    }
  }
 }
}

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++) {
    if ($docb) {
    $item = $doc->getElementById($xxid[$j]);
    if ($item) idadd($xxid[$j], $xid[$i - 1]);
    } else {
    idadd($xxid[$j], $xid[$i - 1]);
    }
   }
  } 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++) {
    if ($docb) {
    $item = $doc->getElementById($xxid[$j]);
    if ($item) idadd($xxid[$j], $xid[$i - 1]);
    } else {
    idadd($xxid[$j], $xid[$i - 1]);
    }
   }
  } else {
   $k = 0;
   $xzxid = explode(">", $xid[$i]);
   $xxid = explode(" ", $xid[$i]);
    if ($docb) {
    $item = $doc->getElementById($xxid[$j]);
    if ($item) idadd($xxid[0], $xid[$i - 1]);
    } else if (strlen($xzxid[0]) < strlen($xxid[0])) {
    idadd($xzxid[0], $xid[$i - 1]);
    } else {
    idadd($xxid[0], $xid[$i - 1]);
    }
  }
 }
}
}



}

?>
<!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;
var errcnt = 0;

function callpreaddele() {
  //document.title = document.title.replace("The putElement", ".The putElement");
  //if (document.title.indexOf("..........") != -1) document.title = document.title.replace("..........", ".");
  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") {
    //if (cis != null) document.title = document.title.replace("The putElement", "@The putElement");
    putElementById(tval);
  } else if (mode == "tag") {
    putElementsByTagName(tval);
  }
  } catch (ex) {
    if (1 == 2) document.body.innerHTML = document.body.innerHTML + "<p>" + ex.message + "</p>";
    //if (cis != null) document.title = document.title.replace("The putElement", "=The putElement");
    errcnt = errcnt + 1;
    if (errcnt == 3) {
      document.getElementById('pextras').style.display = 'block';
      if (cis) {
        cis.close();
        cis = null;
      } else if (cis) {
        setTimeout(callpreaddele, 1000);
      }
    } else if (cis) {
      setTimeout(callpreaddele, 1000);
    }
  }
}

function putElementById(ttval) {
    var wis = window.top;
    if (cis != null) wis = cis;
    //if (cis != null) document.title = document.title.replace("The putElement", "-The putElement");
    var myxcs = wis.document.getElementById(ttval);
    //if (cis != null) document.title = document.title.replace("The putElement", "+The putElement");
    if (myxcs == null && cis != null) {
     //document.title = document.title.replace("The putElement", ttval + "!The putElement");
     //document.getElementById('pextras').style.display = 'block';
     setTimeout(callpreaddele, 1000);
    } else {
    try {
     //document.title = document.title.replace("The putElement", ":The putElement");
     document.body.innerHTML = document.body.innerHTML + myxcs.outerHTML;
      if (cis) {
        cis.close();
        cis = null;
      }
    } catch (ex) {
    //document.title = document.title.replace("The putElement", ex.message + "The putElement");
    setTimeout(callpreaddele, 1000);
    }
    }
}


function putElementsByClassName(ttval) {
    var wis = window.top;
    if (cis != null) wis = cis;
    var mycs = wis.document.getElementsByClassName(ttval);
    if (mycs == null && cis != null) {
    //document.getElementById('pextras').style.display = 'block';
    setTimeout(callpreaddele, 1000);
    } else if (mycs.length == 0 && cis != null) {
    setTimeout(callpreaddele, 1000);
    } else {
    try {
    for (var ic=(mycs.length - 1); ic>=0; ic--) {
     document.body.innerHTML = document.body.innerHTML + mycs[ic].outerHTML;
    }
      if (cis) {
        cis.close();
        cis = null;
      }
    } catch (ex) {
    //document.title = document.title.replace("The putElement", ex.message + "The putElement");
    setTimeout(callpreaddele, 1000);
    }
    }
}

function putElementsByTagName(ttval) {
    var wis = window.top;
    if (cis != null) wis = cis;
    var mycs = wis.document.getElementsByTagName(ttval);
    if (mycs == null && cis != null) {
    //document.getElementById('pextras').style.display = 'block';
    setTimeout(callpreaddele, 1000);
    } else if (mycs.length == 0 && cis != null) {
    setTimeout(callpreaddele, 1000);
    } else {
    try {
    for (var ic=(mycs.length - 1); ic>=0; ic--) {
     document.body.innerHTML = document.body.innerHTML + mycs[ic].outerHTML;
    }
      if (cis) {
        cis.close();
        cis = null;
      }
    } catch (ex) {
    //document.title = document.title.replace("The putElement", ex.message + "The putElement");
    setTimeout(callpreaddele, 1000);
    }
    }
}

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 id='uluu'>Under line under here are putElement(s)By HTML elements</p>
<hr>
<?php echo $bextras; ?>
</body>
</html>
