<?php
// putElementsBy.php
// Set up opposite idea to getElement(s)By
// RJM Programming
// December, 2014

set_time_limit(0);
$isxml = false;
$fb = "";
$getacont = "";
$postacont = "";
$udirname = "";
if (isset($_POST['fil'])) {
  if (urldecode($_POST['fil']) != "") $fb = " ... file=" . urldecode($_POST['fil']);
  if (strpos(urldecode($_POST['fil']) . "?", ".xml?") !== false) $isxml = true;
} else if (isset($_GET['fil'])) {
  if (urldecode($_GET['fil']) != "") $fb = " ... file=" . urldecode($_GET['fil']);
  if (strpos(urldecode($_GET['fil']) . "?", ".xml?") !== false) $isxml = true;
}
if (isset($_POST['url'])) {
  if (strpos(urldecode($_POST['url']) . "?", ".xml?") !== false) $isxml = true;
  if ($isxml && urldecode($_POST['url']) != "") $fb = " ... file=" . urldecode($_POST['url']);
  $hparts = j_parseUrl(urldecode($_POST['url']));
  $udirs = explode("/", urldecode($_POST['url']));
  if ($hparts['authority'] == $hparts['host'] && $hparts['userinfo'] == "") $hparts['authority'] = "";
  if ($udirs[sizeof($udirs) - 1] == "") {
    $udirname = urldecode($_POST['url']);
  } else if (strpos($hparts['path'], "/") !== false) {
    $udirname = $hparts['scheme'] . "://" . $hparts['userinfo'] . $hparts['authority'] . $hparts['host'] . $hparts['port'] . str_replace("//", "/", str_replace("/" . $udirs[sizeof($udirs) - 1], "/", $hparts['path']));
  } else {
    $udirname = $hparts['scheme'] . "://" . $hparts['userinfo'] . $hparts['authority'] . $hparts['host'] . $hparts['port'] . $hparts['path'] . "/";
  }
  //echo $udirname;
} else if (isset($_GET['url'])) {
  if (strpos(urldecode($_GET['url']) . "?", ".xml?") !== false) $isxml = true;
  if ($isxml && urldecode($_GET['url']) != "") $fb = " ... file=" . urldecode($_GET['url']);
  $hparts = j_parseUrl(urldecode($_GET['url']));
  $udirs = explode("/", urldecode($_GET['url']));
  if ($hparts['authority'] == $hparts['host'] && $hparts['userinfo'] == "") $hparts['authority'] = "";
  if ($udirs[sizeof($udirs) - 1] == "") {
    $udirname = urldecode($_GET['url']);
  } else if (strpos($hparts['path'], "/") !== false) {
    $udirname = $hparts['scheme'] . "://" . $hparts['userinfo'] . $hparts['authority'] . $hparts['host'] . $hparts['port'] . str_replace("//", "/", str_replace("/" . $udirs[sizeof($udirs) - 1], "/", $hparts['path']));
  } else {
    $udirname = $hparts['scheme'] . "://" . $hparts['userinfo'] . $hparts['authority'] . $hparts['host'] . $hparts['port'] . $hparts['path'] . "/";
  }
  //echo $udirname;
}
$url = "";
$acont = "";
$cont = "";
$selclass = "";
$selid = "";
$seltag = "";
$xselclass = "";
$xselid = "";
$xseltag = "";
$onload = " onload='doonload();' ";
$bextras = "<div id='pextras' style='display:none;background-color:white;'> </div> ";
$firsttag = "";
//$doc = new DOMDocument();
$docb = false;
if (isset($_GET['url'])) {
  $url = urldecode($_GET['url']);
  if (strpos(strtolower("*" . $url), "*http:") === false && strpos(strtolower("*" . $url), "*https:") === false && strlen($url) > 0) {
     if (strpos(strtolower("*" . $url), "*file:") !== false) {
      $url = str_replace("file:", "", str_replace("file://", "", $url));
      if (substr($url,0,3) == "../" && strpos(strtolower($_SERVER['SERVER_NAME']), "rjmprogramming.com.au") !== false) {
          $jsspec = str_replace("/../", "/", (str_replace("*../", str_replace("//", "/", str_replace("/PHP/", "/", str_replace("/PHP//", "/", str_replace("putElementsBy.php", "", "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']))))), "*" . $url)));
          $url = str_replace("/../", "/", (str_replace("*../", str_replace("//", "/", str_replace("/PHP/", "/", str_replace("/PHP//", "/", (dirname(__FILE__) . "/")))), "*" . $isspec[$ki])));
      } else if (substr($url,0,2) == "./" && strpos(strtolower($_SERVER['SERVER_NAME']), "rjmprogramming.com.au") !== false) {
          $jsspec = str_replace("/../", "/", (str_replace("*./", str_replace("//", "/", str_replace("/PHjunkP/", "/", str_replace("/PHjunkP//", "/", str_replace("putElementsBy.php", "", "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']))))), "*" . $url)));
          $url = str_replace("/../", "/", (str_replace("*./", str_replace("//", "/", str_replace("/PHjunkP/", "/", str_replace("/PHjunkP//", "/", (dirname(__FILE__) . "/")))), "*" . $isspec[$ki])));
      }
      $fb = " ... file=" . $_GET['url'];
      if (strpos($_GET['url'] . "?", ".xml?") !== false) $isxml = true;
      $acont = file_get_contents($url);
      $cont = str_replace(" = ", "=", $acont);
     } else if (file_exists($url)) {
      $fb = " ... file=" . $_GET['url'];
      if (strpos($_GET['url'] . "?", ".xml?") !== false) $isxml = true;
      $acont = file_get_contents($url);
      $cont = str_replace(" = ", "=", $acont);
     } else {
      $acont = $url; //str_replace("\\" . '"', '"', $url);
      if ($getacont != "") {
        $getacont = $url;
      } else {
        $postacont = $url;
      }
      $cont = str_replace(" = ", "=", $acont);
      $url = "";
     }
  }
} else if (isset($_POST['url'])) {
  $url = urldecode($_POST['url']);
  if (strpos(strtolower("*" . $url), "*http:") === false && strpos(strtolower("*" . $url), "*https:") === false && strlen($url) > 0) {
     if (strpos(strtolower("*" . $url), "*file:") !== false) {
      $url = str_replace("file:", "", str_replace("file://", "", $url));
      if (substr($url,0,3) == "../" && strpos(strtolower($_SERVER['SERVER_NAME']), "rjmprogramming.com.au") !== false) {
          $jsspec = str_replace("/../", "/", (str_replace("*../", str_replace("//", "/", str_replace("/PHP/", "/", str_replace("/PHP//", "/", str_replace("putElementsBy.php", "", "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']))))), "*" . $url)));
          $url = str_replace("/../", "/", (str_replace("*../", str_replace("//", "/", str_replace("/PHP/", "/", str_replace("/PHP//", "/", (dirname(__FILE__) . "/")))), "*" . $isspec[$ki])));
      } else if (substr($url,0,2) == "./" && strpos(strtolower($_SERVER['SERVER_NAME']), "rjmprogramming.com.au") !== false) {
          $jsspec = str_replace("/../", "/", (str_replace("*./", str_replace("//", "/", str_replace("/PHjunkP/", "/", str_replace("/PHjunkP//", "/", str_replace("putElementsBy.php", "", "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']))))), "*" . $url)));
          $url = str_replace("/../", "/", (str_replace("*./", str_replace("//", "/", str_replace("/PHjunkP/", "/", str_replace("/PHjunkP//", "/", (dirname(__FILE__) . "/")))), "*" . $isspec[$ki])));
      }
      $fb = " ... file=" . $_POST['url'];
      if (strpos($_POST['url'] . "?", ".xml?") !== false) $isxml = true;
      $acont = file_get_contents($url);
      $cont = str_replace(" = ", "=", $acont);
     } else if (file_exists($url)) {
      $fb = " ... file=" . $_POST['url'];
      if (strpos($_POST['url'] . "?", ".xml?") !== false) $isxml = true;
      $acont = file_get_contents($url);
      $cont = str_replace(" = ", "=", $acont);
     } else {
      $acont = $url; //str_replace("\\" . '"', '"', $url);
      if ($getacont != "") {
        $getacont = $url;
      } else {
        $postacont = $url;
      }
      $cont = str_replace(" = ", "=", $acont);
      //echo $cont;
      $url = "";
     }
  }
}
if (strpos(strtolower($url) . "?", ".xml?") !== false) {
  $isxml = true;
  $firsttag = "body";
  $bextras = "<div id='pextras' style='display:block;background-color:yellow;'> </div> ";
}
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 + \"" . $fb . " ... 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 + \"" . $fb . " ... 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 + \"" . $fb . " ... tag is \" + \"" . $seltag . "\"; preaddele(\"" . $seltag . "\",\"tag\",\"" . $url . "\");'";
  $seltag = "";
}


function j_parseUrl($jurl) { // thanks to http://php.net/manual/en/function.parse-url.php#86115
  $r  = "(?:([a-z0-9+-._]+)://)?";
  $r .= "(?:";
  $r .=   "(?:((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9a-f]{2})*)@)?";
  $r .=   "((?:[a-z0-9-._~!$&'()*+,;=]|%[0-9a-f]{2})*)";
  $r .=   "(?::(\d*))?";
  $r .=   "(/(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9a-f]{2})*)?";
  $r .=   "|";
  $r .=   "(/?";
  $r .=     "(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+";
  $r .=     "(?:[a-z0-9-._~!$&'()*+,;=:@\/]|%[0-9a-f]{2})*";
  $r .=    ")?";
  $r .= ")";
  $r .= "(?:\?((?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9a-f]{2})*))?";
  $r .= "(?:#((?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9a-f]{2})*))?";
  preg_match("`$r`i", $jurl, $match);
  $parts = array(
            "scheme"=>'',
            "userinfo"=>'',
            "authority"=>'',
            "host"=> '',
            "port"=>'',
            "path"=>'',
            "query"=>'',
            "fragment"=>'');
  switch (count ($match)) {
    case 9: $parts['fragment'] = $match[8];
    case 8: $parts['query'] = $match[7];
    case 7: $parts['path'] =  $match[6];
    case 6: $parts['path'] =  $match[5] . $parts['path'];
    case 5: $parts['port'] =  $match[4];
    case 4: $parts['host'] =  $match[3];
    case 3: $parts['userinfo'] =  $match[2];
    case 2: $parts['scheme'] =  $match[1];
  }
  $parts['authority'] = ($parts['userinfo']?$parts['userinfo']."@":"").
                         $parts['host'].
                        ($parts['port']?":".$parts['port']:"");
  return $parts;
}

function overview($stuff) {
  global $url, $getacont, $postacont;
  $qurl = $url;
  if (isset($_GET['fil'])) {
    if ($_GET['fil'] != "") $qurl = $_GET['fil'];
    if (strpos($_GET['fil'] . "?", ".xml?") !== false) $isxml = true;
  }
  if (isset($_POST['fil'])) {
    if ($_POST['fil'] != "") $qurl = $_POST['fil'];
    if (strpos($_POST['fil'] . "?", ".xml?") !== false) $isxml = true;
  }
  if (strpos($stuff, "Please ") !== false || 1 == 1) {
    return "<hr style='color:red;'><div style='background-color:yellow; padding-left:20px; padding-top: 20px;'><input id='file' type='file' name='file'><span id='mode' name='mode'>&nbsp;&nbsp;Parse file</span>:<span class='readBytesButtons'><button style='display:none;' data-endbyte='4' data-startbyte='0'>1-5</button><button style='display:none;' data-endbyte='14' data-startbyte='5'>6-15</button><button style='display:none;' data-endbyte='7' data-startbyte='6'>7-8</button><button onclick='readBlob(0,0);'>Process</button></span><br><br><div id='byte_range' style='display:none;'></div><div id='byte_content' style='display:none;'></div></div><br><hr style='color:green;'><form style='background-color:lightgray;padding-left:20px;' action='" . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']) . "?justlook=y' method='POST'><input type='hidden' id='fil' name='fil' value='' /><input type='hidden' id='acont' name='acont' value='" . urlencode($getacont . $postacont) . "' /><br>... or 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']), "", $qurl) . "' size=60></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='t=this.value; m=\"class\"; if (ww != null) { preaddele(\"\",\"\",\"\"); } else { addele(this.value,\"class\"); }' value='' style='display:none;'></select><br>";
$idarray = array();
$sid = array(" id=", " ID=", " Id=");
$selid = "<select name='selid' id='selid' onchange='t=this.value; m=\"id\"; if (ww != null) { preaddele(\"\",\"\",\"\"); } else {  addele(this.value,\"id\"); }' value='' style='display:none;'></select><br>";
$tagarray = array();
$stag = array("<");
$seltag = "<select name='seltag' id='seltag' onchange='t=this.value; m=\"tag\"; if (ww != null) { preaddele(\"\",\"\",\"\"); } else {  addele(this.value,\"tag\"); }' value='' style='display:none;'></select><br>";
$outtag = array("head", "script", "link", "meta", "html", "!doctype", "body", "style", "title");

$url = "";
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 != "" && $cont == "") {
   $acont = file_get_contents($url);
   $cont = str_replace(" = ", "=", $acont);
  } else if ($cont == "") {
   $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 ($cont != "") {
   $url = "";
  }
} else if (isset($_POST['url'])) {
  $url = urldecode($_POST['url']);
  if ($url != "" && $cont == "") {
   $acont = file_get_contents($url);
   $cont = str_replace(" = ", "=", $acont);
  } else if ($cont == "")  {
   $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 ($cont != "") {
   $url = "";
  }
} else {
  $url = "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']);
  if ($cont == "")  {
    $acont = file_get_contents($url . "?justlooking=");
    $cont = str_replace(" = ", "=", $acont);
  }
}

if (isset($_GET['acont'])) {
 if ($_GET['acont'] != "") {
 $getacont = urldecode($_GET['acont']);
 $acont = $getacont;
 $cont = str_replace(" = ", "=", $acont);
 }
} else if (isset($_POST['acont'])) {
 if ($_POST['acont'] != "") {
 $postacont = urldecode($_POST['acont']);
 $acont = $postacont;
 $cont = str_replace(" = ", "=", $acont);
         // file_put_contents(dirname(__FILE__) . "/abba9.xml", "*" . $acont . "*");
 }
}

if ($isxml) $firsttag = "body";


if ($acont != "" || 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);
  if ($firsttag == "") {
  $doc->loadHTML($acont);
  } else {
       //   file_put_contents(dirname(__FILE__) . "/abba1.xml", "*" . $acont . "*");
  $doc->loadXML($acont);
       //   file_put_contents(dirname(__FILE__) . "/abba2.xml", "*" . $acont . "*");
  }
  libxml_use_internal_errors($before);  
  $docb = true;
       //   file_put_contents(dirname(__FILE__) . "/abba3.xml", "*" . $_GET['fil'] . "*" . $_POST['fil'] . "%" . $_GET['url'] . "*" . $_POST['url']);
  } catch (Exception $e) {
  throw new Exception( 'Something really wrong', 0, $e);
  }
}

function relative_to_absolute($inth) {
    global $udirname;
    //$low = strtolower($inth);
    $outth = $inth;
    $ideas = array(" 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 classadd($thing, $fthing) {
    global $classarray, $selclass, $xselclass, $cont, $bextras, $docb, $doc;
    $thing = str_replace("'", "", str_replace("\\", '', str_replace('"', '', $thing)));
    if ($thing == $xselclass) {
      if ($docb && 1 == 2) {
        foreach ($doc->getElementsByClassName($thing) as $link) {
          $bextras = str_replace(" </div> ", relative_to_absolute($link->C14N()) . "<br> </div> ", $bextras);
		}
		//foreach ($doc->getElementsByTagName($thing)->item(0)->childNodes as $link) {
        //  $bextras = str_replace(" </div> ", relative_to_absolute($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> ", relative_to_absolute(substr($bis, 0, ($huh2 + strlen("</" . $words[0] . ">")))) . " </div> ", $bextras);
        } else {
          $huh2 = strpos($bis, "/>");
          if ($huh2 !== false) {
            $bextras  = str_replace(" </div> ", relative_to_absolute(substr($bis, 0, ($huh2 + strlen("/>")))) . " </div> ", $bextras);
          } else {
            $huh2 = strpos($bis, ">");
            if ($huh2 !== false) $bextras  = str_replace(" </div> ", relative_to_absolute(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;
    $thing = str_replace("'", "", str_replace("\\", '', str_replace('"', '', $thing)));
    if ($thing == $xselid) {
    
      if ($docb) {
       $dobj = $doc->getElementById($thing);
       $bextras  = str_replace(" </div> ", relative_to_absolute($dobj->C14N()) . "<br> </div> ", $bextras);
	   //foreach ($doc->getElementById($thing)->item(0)->childNodes as $link) {
         // $bextras = str_replace(" </div> ", relative_to_absolute($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> ", relative_to_absolute(substr($bis, 0, ($huh2 + strlen("</" . $words[0] . ">")))) . " </div> ", $bextras);
        } else {
          $huh2 = strpos($bis, "/>");
          if ($huh2 !== false) {
            $bextras  = str_replace(" </div> ", relative_to_absolute(substr($bis, 0, ($huh2 + strlen("/>")))) . " </div> ", $bextras);
          } else {
            $huh2 = strpos($bis, ">");
            if ($huh2 !== false) $bextras  = str_replace(" </div> ", relative_to_absolute(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, $isxml;
    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 && strpos($thing, "(") === false && substr($thing, 0, 1) != "/" && substr($thing, 0, 1) != "!") {
    $firsttag = "body";
        //  file_put_contents(dirname(__FILE__) . "/aaba.xml", "*" . $thing . "* vs *" . $xseltag . "*");
    if ($thing == $xseltag) {
      if ($docb) {
        foreach ($doc->getElementsByTagName($thing) as $link) {
         if ($isxml) {
        //  file_put_contents(dirname(__FILE__) . "/aabb.xml", $thing);
          $bextras = str_replace(" </div> ", relative_to_absolute(str_replace("$!bi$!", "</i></b></font>", str_replace("@!bi@!", "<font color=red><b><i>", str_replace("<", "&lt;", str_replace(">", "&gt;", str_replace("<" . $thing . " ", "@!bi@!&lt;" . $thing . " $!bi$!", str_replace("<" . $thing . ">", "@!bi@!&lt;" . $thing . "&gt;$!bi$!", str_replace("</" . $thing . ">", "@!bi@!&lt;/" . $thing . "&gt;$!bi$!", $link->C14N())))))))) . "<br> </div> ", $bextras);
        //  file_put_contents(dirname(__FILE__) . "/aaab.xml", $bextras);
         } else {
          $bextras = str_replace(" </div> ", relative_to_absolute($link->C14N()) . "<br> </div> ", $bextras);
         }
		}
		//foreach ($doc->getElementsByTagName($thing)->item(0)->childNodes as $link) {
        //  $bextras = str_replace(" </div> ", relative_to_absolute($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> ", relative_to_absolute(substr($bis, 0, ($huh2 + strlen("</" . $words[0] . ">")))) . " </div> ", $bextras);
        } else {
          $huh2 = strpos($bis, "/>");
          if ($huh2 !== false) {
            $bextras = str_replace(" </div> ", relative_to_absolute(substr($bis, 0, ($huh2 + strlen("/>")))) . " </div> ", $bextras);
          } else {
            $huh2 = strpos($bis, ">");
            if ($huh2 !== false) $bextras = str_replace(" </div> ", relative_to_absolute(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 {
      //    file_put_contents(dirname(__FILE__) . "/abba.xml", "*" . $xxtag[0] . "*");
    $items = $doc->getElementsByTagName($xxtag[0]);
     //     file_put_contents(dirname(__FILE__) . "/abbb.xml", "*" . $xxtag[0] . "*" . count($items));
    if (count($items) > 0) tagadd($xxtag[0], $xtag[$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 ww = null;
var errcnt = 0;
var done = 0;
var mycont = "";
var useajax = false;
var divajax = "";
<?php
  echo "\n var udirname='" . $udirname . "'; \n";
?>


function relative_to_absolute(inth) {
    var outth = inth;
    var ideas = [" href='", ' href="', " Href='", ' Href="', " HREF='", ' HREF="', " href=", " Href=", " HREF=", " src='", ' src="', " Src='", ' Src="', " SRC='", ' SRC="', " src=", " Src=", " SRC="];
    if (udirname != "") {
     for (var m=0; m<ideas.length; m++) {
      var huhs = inth.split(ideas[m]);
      if (huhs.length > 1) {
        for (ii=(huhs.length - 1); ii>=1; ii--) {
          if (huhs[ii].substring(0, 4).toLowerCase().replace("file", "http") == "http") {
            outth = outth;
          } else if (huhs[ii].substring(0, 1) != "/" && huhs[ii].substring(0, 1) != "'" && huhs[ii].substring(0, 1) != '"') {
            outth = outth.replace(huhs[ii], udirname + huhs[ii]);
          } else if (huhs[ii].substring(1, 1) != "/" && huhs[ii].substring(0, 1) != "'" && huhs[ii].substring(0, 1) != '"') {
            outth = outth.replace(huhs[ii], udirname.substring(0, (udirname.length - 1)) + huhs[ii]);
          }
        }
      }
     }
    }
    return outth;
}

  
function process(infilecontents) {
    if (infilecontents != null) {
        //alert(document.getElementById('be').innerHTML);
        //location.href = "./putElementsBy.php?x=x";
        //document.getElementById("submit").click();
    }
}

function readBlob(opt_startByte, opt_stopByte) {

    var files = document.getElementById("file").files;
    if (!files.length) {
      alert("Please select a file!");
      return;
    } else {
      w = files[0].name;
      document.getElementById("fil").value = w;
    }

    var file = files[0];
    var start = parseInt(opt_startByte) || 0;
    var stop = parseInt(opt_stopByte) || file.size - 1;

    var reader = new FileReader();

    // If we use onloadend, we need to check the readyState.
    reader.onloadend = function(evt) {
      if (evt.target.readyState == FileReader.DONE) { // DONE == 2
        //cis = window.open("", "_blank", "top=20,left=20,width=200,height=200");
        //ww = cis;
        //cis.document.write(evt.target.result);
        document.getElementById("url").value = evt.target.result;
        document.getElementById("acont").value = evt.target.result;
        document.getElementById("submit").click();
        //cis.document.write(evt.target.result.replace(/</g, "&lt;").replace(/>/g, "&gt;"));
        //document.body.innerHTML = document.body.innerHTML + relative_to_absolute("<div id=be style=display:block;>" + evt.target.result.replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</div>");
        document.getElementById("byte_content").textContent = evt.target.result;
        document.getElementById("byte_range").textContent = 
            ["Read bytes: ", start + 1, " - ", stop + 1,
             " of ", file.size, " byte file"].join("");
        process(evt.target.result);
      }
    };

    var blob = file.slice(start, stop + 1);
    reader.readAsBinaryString(blob);
}


function callpreaddele() {
  //document.title = document.title.replace("The putElement", ".The putElement");
  //if (document.title.indexOf("..........") != -1) document.title = document.title.replace("..........", ".");
  preaddele("", "", "");
}

function doonload() {
if (done == 0) {
  <?php if ($isxml) { echo "\n   document.getElementById('pextras').style.display = 'block'; \n"; } ?> 

done = 1;
if (window.File && window.FileReader && window.FileList && window.Blob) {
  done = done;
} else {
  document.getElementById("file").style.display = "none";
  if (1 == 2) { alert("The File APIs are not fully supported in this browser."); }
}

  document.querySelector('.readBytesButtons').addEventListener('click', function(evt) {
    if (evt.target.tagName.toLowerCase() == "button") {
      var startByte = evt.target.getAttribute("data-startbyte");
      var endByte = evt.target.getAttribute("data-endbyte");
      readBlob(startByte, endByte);
    }
  }, false);
}
}

function preaddele(tval, mode, wurl) {
  doonload();
  <?php if ($isxml) { echo "\n if (1 == 7) { \n"; } ?> 
  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);
    }
  }
  <?php if ($isxml) { echo "\n } \n"; } ?> 
}

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 + relative_to_absolute(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 + relative_to_absolute(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 + relative_to_absolute(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") {
    if (ww == null) document.getElementById('submit').click();
    putElementsByClassName(tval);
  } else if (mode == "id") {
    if (ww == null) document.getElementById('submit').click();
    putElementById(tval);
  } else if (mode == "tag") {
    if (ww == null) 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'>
Internal use only: ways
</div>
<div id='parts' class='p'>
Internal use only: parts
</div>
<hr>
<p id='uluu' style='background-color:pink;padding-top:10px;padding-left:10px;'>Under line under here are putElement(s)By <?php if ($isxml) { echo "XML"; } else { echo "HTML"; } ?> elements</p>
<hr style="height:5px;background-color:black;">
<?php echo $bextras; ?>
</body>
</html>
