<?php /* ***************************************************************
* PHF-Get - 0.3.2 (beta)
* Copyright (C) 2005 - GreyWyvern
*
* Licenced under the BSD Licence
*  - http://www.greywyvern.com/code/bsd.txt
*
* Requires PHP 4.3.0+
********************************************************************* */

/* ****************************************************************** */
/* ***** Script Setup Defaults ************************************** */
class Accept {
  var 
$extension;
  var 
$texttype false;
  var 
$htmlpage false;
  var 
$priority 1;

  function 
Accept($extension$texttype false$htmlpage false$priority 1) {
    
$this->extension $extension;
    
$this->texttype $texttype;
    
$this->htmlpage $htmlpage;
    
$this->priority round($priority1);
  }
}
$_PHF['mark'] = array_sum(explode(" "microtime()));
$_PHF['version'] = "0.3.2 (beta)";
$_PHF['index'] = array();
$_PHF['failed'] = array();
$_PHF['collapse'] = true;    // collapse original page HTML for smaller file size
$_PHF['datalimit'] = 204800// bytes allowed per single file
$_PHF['filename'] = "page";  // default output filename
$_PHF['useragent'] = "phf_get {$_PHF['version']}";
ini_set("user_agent"$_PHF['useragent']);

$_PHF['accept']['text/css'] = new Accept("css"true);
$_PHF['accept']['text/html'] = new Accept("html"truetrue);
$_PHF['accept']['text/javascript'] = new Accept("js"true);
$_PHF['accept']['text/xml'] = new Accept("xml"truetrue);
$_PHF['accept']['application/xml'] = new Accept("xml"truetrue0.9);
$_PHF['accept']['application/xhtml+xml'] = new Accept("xhtml"truetrue);
$_PHF['accept']['application/javascript'] = new Accept("js"true);
$_PHF['accept']['application/x-javascript'] = new Accept("js"true);
$_PHF['accept']['image/gif'] = new Accept("gif");
$_PHF['accept']['image/jpeg'] = new Accept("jpg");
$_PHF['accept']['image/png'] = new Accept("png");
$_PHF['accept']['image/tiff'] = new Accept("tif");
$_PHF['accept']['image/x-xbitmap'] = new Accept("bmp");
$_PHF['accept']['application/x-shockwave-flash'] = new Accept("swf");
// $_PHF['accept']['*/*'] = new Accept("", false, false, 0.1);

$_PHF['acceptheader'] = "";
while (list(
$key$value) = each($_PHF['accept']))
  
$_PHF['acceptheader'] .= $key.(($value->priority 1) ? "; q={$value->priority}" "").", ";
$_PHF['acceptheader'] = "{$_PHF['acceptheader']}*/*;q=0.1";


/* ****************************************************************** */
/* ***** Incoming URI Data ****************************************** */
if (isset($_GET['phf'])) $_POST['phf'] = $_GET['phf'];
if (isset(
$_GET['collapse'])) $_POST['collapse'] = $_GET['collapse'];


/* ****************************************************************** */
/* ***** Incoming Form Data ***************************************** */
$_PAGE = (isset($_POST['phf'])) ? rawurldecode($_POST['phf']) : "";
if (!
$_PAGE) die("Submit URIs in the form: http://...");

if (isset(
$_POST['agent'])) {
  switch (
$_POST['agent']) {
    case 
"--default--": break;
    case 
"--yours--"$_PHF['useragent'] = $_SERVER['HTTP_USER_AGENT']; break;
    case 
"--custom--": if (isset($_POST['customagent'])) $_PHF['useragent'] = str_replace(array("\n""\r"), "" $_POST['customagent']);
  }
}
if (!isset(
$_POST['collapse'])) $_PHF['collapse'] = false;


/* ***** Timer ****************************************************** */
$_TIMER = array(
  
"__log" => array_sum(explode(" "microtime())),  // Time script began
  
"Initx" => 0,  // Script setup and initiation
  
"HTTPx" => 0,  // HTTP transactions
  
"PHPxx" => 0  // Everything else
);

function 
addTime($wish) { /* ***** Record time split ************* */
  
global $_TIMER;

  
$_TIMER[$wish] += array_sum(explode(" "microtime())) - $_TIMER['__log'];
  
$_TIMER['__log'] = array_sum(explode(" "microtime()));
}


/* ****************************************************************** */
/* ***** Functions ************************************************** */
function parseHTMLTag($tag$debug false) {
  
$output = array("closing" => false);
  
$loaf $tag trim($tag);

  if (
$tag{0} == "<" && $tag{strlen($tag) - 1} == ">") {
    
$tag preg_replace(array("/^<\s+\//","/\s*=\s*/"), array("</""="), $tag);
    if (
$tag{1} == "/"$output['closing'] = true;
    
str_replace(array("\x05""\x06"), ""$tag);

    
preg_match("/^<\/?([\w\-]+?)(\s+|>)/"$tag$element);
    if (isset(
$element[1]) && $element[1]) {
      
$output['element'] = $element[1];
      
$loaf preg_replace("/\s*\/?>$/"""substr($loafstrlen($element[0])));

      if (
strlen($loaf) >= 1) {
        
preg_match_all("/=\s*('[^']*')/"$loaf$qsin);
        
$loaf preg_replace("/=\s*('[^']*')/""=\x05"$loaf);
        
$qsin $qsin[1];
        
array_unshift($qsin"");
        
reset($qsin);

        
preg_match_all("/=\s*(\"[^\"]*\")/"$loaf$qdub);
        
$loaf preg_replace("/=\s*(\"[^\"]*\")/""=\x06"$loaf);
        
$qdub $qdub[1];
        
array_unshift($qdub"");
        
reset($qdub);

        
$loaf preg_replace(array("/\s*=\s*/""/\s\s+/"), array("="" "), $loaf);
        
$loaf explode(" "$loaf);
        foreach (
$loaf as $slice) {
          
$slice explode("="$slice2);
          if (isset(
$slice[0]) && preg_match("/^[\w\-]+$/"$slice[0])) {
            if (
count($slice) == 2) {
              
$slice[1] = preg_replace(array("/^\x05$/e""/\x05/e"), array('trim(next($qsin), "\'");''next($qsin);'), $slice[1]);
              
$slice[1] = preg_replace(array("/^\x06$/e""/\x06/e"), array('trim(next($qdub), "\"");''next($qdub);'), $slice[1]);
              
$output[strtolower($slice[0])] = $slice[1];
            } else if (
count($slice) == 1$output[$slice[0]] = true;
          } else if (
$debugtrigger_error("parseHTMLTag: Invalid attribute name ".htmlspecialchars($slice[0]));
        }
      }
      return 
$output;

    } else if (
$debugtrigger_error("parseHTMLTag: Invalid element name");
  } else if (
$debugtrigger_error("parseHTMLTag: Input does not begin or end with angle brackets");
  return 
false;
}

function 
get_Data($fulluri$original$encode true$basepage false) {
  global 
$_PHF;
  static 
$_CHAIN = array();

  
$tries 0;

  if (
in_array($fulluri$_CHAIN)) return $original;
  if (
count($_CHAIN) > 8) return $original;
  
$_CHAIN[] = $fulluri;

  while (
$tries++ < 10) {
    if (isset(
$_PHF['index'][$fulluri])) {
      
array_pop($_CHAIN);
      return 
$_PHF['index'][$fulluri];
    }

    if (!
preg_match("/^\w{3,6}:\/\/$/"$fulluri)) {
      
$_URI parse_url($fulluri);
      if (!isset(
$_URI['path']) || !$_URI['path']) $_URI['path'] = "/";
      if (
$basepage) {
        
$_PHF['file'] = substr($_URI['path'], strrpos($_URI['path'], "/") + 1);
        if (
strlen($_PHF['file'])) {
          if (
strpos($_PHF['file'], ".") !== false) {
            if (
$_PHF['file']{0} != "."$_PHF['filename'] = substr($_PHF['file'], 0strrpos($_PHF['file'], "."));
          } else 
$_PHF['filename'] = $_PHF['file'];
        }
      }
      
$_URI['port'] = (isset($_URI['port'])) ? $_URI['port'] : "80";
      if (isset(
$_URI['scheme'])) {
        
$_URI['home'] = "{$_URI['scheme']}://{$_URI['host']}".(($_URI['port'] != "80") ? ":".$_URI['port'] : "");
        
$_URI['base'] = $_URI['home'].substr($_URI['path'], 0strrpos($_URI['path'], "/") + 1);
        if (isset(
$_URI['query'])) $_URI['path'] .= "?{$_URI['query']}";
        if (isset(
$_URI['host'])) {


       
addTime("PHPxx");  /* ***** Record time split **************** */


          
if ($gdat = @fsockopen($_URI['host'], $_URI['port'], $errno$erstr5)) {
            
$status socket_get_status($gdat);
            if (!
$status['blocked']) socket_set_blocking($gdattrue);
            
socket_set_timeout($gdat5);

            
fwrite($gdat"GET {$_URI['path']} HTTP/1.0\r\nHost: {$_URI['host']}\r\nUser-Agent: {$_PHF['useragent']}\r\nAccept: {$_PHF['acceptheader']}\r\n\r\n");
            
$_URI['mime'] = "unknown";
            
$_URI['hlink'] = array();
            
$_URI['bad'] = false;
            while (
$data fgets($gdat)) {
              
$status socket_get_status($gdat);

              if (
$status['timed_out'] || preg_match("/^HTTP\/1\.\d ([^23]\d\d)/i"$data$code)) {
                if (
$basepage) {
                  die(
htmlspecialchars($code[1])." error accessing URI: ".htmlspecialchars($_URI['home'].$_URI['path']));
                } else 
$_URI['bad'] = true;
              }
              if (
preg_match("/^Location:\s*([^\r\n]+?);?\r?\n$/i"$data$redirect)) {
                
$fulluri = ($redirect[1]{0} == "/") ? $_URI['home'].$redirect[1] : $redirect[1];
                if (
in_array($fulluri$_CHAIN)) return $original;
                continue 
2;
              }
              if (
preg_match("/^Content-length:\s?([^;\s]+)/i"$data$match)) {
                if ((int)
$match[1] > $_PHF['datalimit']) {
                  if (
$basepage) {
                    die(
"Page larger than size limit! ({$_PHF['datalimit']} bytes)");
                  } else break;
                }
              }
              if (
preg_match("/^Content-type:\s?([^;\s]+)/i"$data$match)) {
                
$_URI['mime'] = $match[1];
                if (!isset(
$_PHF['accept'][$_URI['mime']])) break;
              }
              if (
$basepage)
                if (
preg_match("/Link:\s?<\"?([^\">]+)\"?>\s?;\s?rel=([^;]+)/i"$data$match))
                  if (
strpos($match[2], "stylesheet") !== false)
                    
$_URI['hlink'][] = array(trim($match[2], "\"\r\n\t"), $match[1]);
              if (
preg_match("/^\r?\n$/"$data)) {
                
$_URI['body'] = "";
                while (
$data fgets($gdat)) {
                  
$_URI['body'] .= $data;
                  if (
strlen($_URI['body']) > $_PHF['datalimit']) {
                    
$_URI['body'] = ($_PHF['accept'][$_URI['mime']]->texttype) ? substr($_URI['body'], 0$_PHF['datalimit']) : "";
                    break;
                  }
                }
                break;
              }
            }
            
fclose($gdat);


       
addTime("HTTPx");  /* ***** Record time split **************** */


            
if ($_URI['bad']) break;

            if (isset(
$_PHF['accept'][$_URI['mime']]) && (!$basepage || $_PHF['accept'][$_URI['mime']]->htmlpage)) {
              if (isset(
$_URI['body']) && $_URI['body']) {
                if (
strpos($_URI['mime'], "javascript") !== false) {
                  
$_URI['mime'] = "text/javascript";
                  
$_URI['body'] = preg_replace("/\n\s+/s""\n"$_URI['body']);
                  
// $_URI['body'] = preg_replace("/\/\*.*?\*\//s", "", $_URI['body']);
                  // $_URI['body'] = preg_replace("/\/\/[^\r\n]*\r?\n/", "", $_URI['body']);
                  // $encode = false;

                
} else if ($_URI['mime'] == "text/css") {
                  
$_URI['body'] = preg_replace("/\s\s+/s"" "$_URI['body']);
                  
$_URI['body'] = preg_replace("/\/\*.*?\*\//s"""$_URI['body']);

                  
/* ***** url() ************************************ */
                  
preg_match_all("/[\s:]url\(([^\)]+)\)/i"$_URI['body'], $match1);
                  for (
$x 0$x count($match1[0]); $x++) {
                    
$match1[1][$x] = trim($match1[1][$x], "'\"");
                    
$http false;
                    if (
$match1[1][$x]{0} != "#" && (!preg_match("/^\w{3,6}:/"$match1[1][$x]) || $http preg_match("/^http:\/\//"$match1[1][$x]))) {
                      
$base = ($http) ? "" : (($match1[1][$x]{0} == "/") ? $_URI['home'] : $_URI['base']);
                      
$match1[2][$x] = str_replace($match1[1][$x], get_Data($base.$match1[1][$x], $match1[1][$x]), $match1[0][$x]);
                    }
                  }
                  if (
$x$_URI['body'] = str_replace($match1[0], $match1[2], $_URI['body']);

                  
/* ***** @import "" ******************************* */
                  
preg_match_all("/@import\s+[\"']([^\"']+)[\"']/i"$_URI['body'], $match1);
                  for (
$x 0$x count($match1[1]); $x++) {
                    
$http false;
                    if (
$match1[1][$x]{0} != "#" && (!preg_match("/^\w{3,6}:/"$match1[1][$x]) || $http preg_match("/^http:\/\//"$match1[1][$x]))) {
                      
$base = ($http) ? "" : (($match1[1][$x]{0} == "/") ? $_URI['home'] : $_URI['base']);
                      
$match1[2][$x] = str_replace($match1[1][$x], get_Data($base.$match1[1][$x], $match1[1][$x]), $match1[0][$x]);
                    }
                  }
                  if (
$x$_URI['body'] = str_replace($match1[0], $match1[2], $_URI['body']);
                  

                } else if (
$_PHF['accept'][$_URI['mime']]->htmlpage) {
                  if (
$_PHF['collapse'] || !$basepage$_URI['body'] = preg_replace("/\s\s+/s"" "$_URI['body']);
                  if (
$basepage$_PHF['extension'] = $_PHF['accept'][$_URI['mime']]->extension;
                  
$_URI['bodyout'] = $_URI['body'];
                  
$_URI['bodyunc'] = preg_replace("/<!--.*?-->/s"""$_URI['body']);

                  
/* ***** <BASE> *********************************** */
                  
preg_match("/<head[^>]*>(.*?)<\/head>/si"$_URI['bodyunc'], $_URI['head']);
                  
$_URI['head'] = $_URI['head'][1];
                  
preg_match_all("/<base\s[^>]*>/i"$_URI['head'], $bases);
                  for (
$x 0$x count($bases[0]); $x++) {
                    
$tag parseHTMLTag($bases[0][$x]);
                    if (isset(
$tag['href']) && $tag['href']{0} != "#" && preg_match("/^\w{3,6}:\/\//"$tag['href']) && !preg_match("/^\w{3,6}:\/\/$/"$tag['href'])) {
                      
$_URI array_merge($_URIparse_url($tag['href']));
                      if (!
$_URI['path']) $_URI['path'] = "/";
                      
$_URI['port'] = (isset($_URI['port'])) ? $_URI['port'] : "80";
                      
$_URI['home'] = "{$_URI['scheme']}://{$_URI['host']}".(($_URI['port'] != "80") ? ":".$_URI['port'] : "");
                      
$_URI['base'] = $_URI['home'].substr($_URI['path'], 0strrpos($_URI['path'], "/") + 1);
                      if (isset(
$_URI['query'])) $_URI['path'] .= "?{$_URI['query']}";
                    }
                  }

                  
/* ***** <META HTTP-EQUIV="LINK"> ***************** */
                  
$mct false;
                  
preg_match_all("/<meta\s[^>]*>/i"$_URI['head'], $metas);
                  
$metas[1] = $metas[0];
                  for (
$x 0$x count($metas[0]); $x++) {
                    
$tag parseHTMLTag($metas[0][$x]);
                    if (isset(
$tag['http-equiv'])) {
                      if (
strtolower($tag['http-equiv']) == "link" && isset($tag['content'])) {
                        
$content array_map("trim"explode(";"html_entity_decode($tag['content'])));
                        if (
count(preg_grep("/^rel\s*=.*?stylesheet/i"$content))) {
                          
$link trim($content[0], "<>\t\r\n");
                          
$http false;
                          if (
$link{0} != "#" && (!preg_match("/^\w{3,6}:/"$link) || $http preg_match("/^http:\/\//"$link))) {
                            
$base = ($http) ? "" : (($link{0} == "/") ? $_URI['home'] : $_URI['base']);
                            
$metas[1][$x] = str_replace($linkget_Data($base.$link$link), $metas[0][$x]);
                          }
                        }
                      } else if (
strtolower($tag['http-equiv']) == "content-type" && isset($tag['content']))
                        if (
preg_match("/charset=[a-z\-0-9]+/i"$tag['content'])) $mct true;
                    }
                  }
                  if (
$x$_URI['bodyout'] = str_replace($metas[0], $metas[1], $_URI['bodyout']);

                  
/* ***** <LINK> *********************************** */
                  
preg_match_all("/<link\s[^>]+>/i"$_URI['head'], $links);
                  
$links[1] = $links[0];
                  for (
$x 0$x count($links[0]); $x++) {
                    
$tag parseHTMLTag($links[0][$x]);
                    if (isset(
$tag['rel']) && strpos(strtolower($tag['rel']), "stylesheet") !== false && isset($tag['href'])) {
                      
$http false;
                      if (
$tag['href']{0} != "#" && (!preg_match("/^\w{3,6}:/"$tag['href']) || $http preg_match("/^http:\/\//"$tag['href']))) {
                        
$base = ($http) ? "" : (($tag['href']{0} == "/") ? $_URI['home'] : $_URI['base']);
                        
$links[1][$x] = str_replace($tag['href'], get_Data($base.$tag['href'], $tag['href']), $links[0][$x]);
                      }
                    }
                  }
                  if (
$x$_URI['bodyout'] = str_replace($links[0], $links[1], $_URI['bodyout']);

                  
/* ***** <?xml-stylesheet ?> ********************** */
                  
preg_match_all("/<\?xml-stylesheet\s[^>]*href=['\"]([^'\"]+)['\"]/i"$_URI['bodyunc'], $xmls);
                  
$xmls[2] = $xmls[0];
                  for (
$x 0$x count($xmls[0]); $x++) {
                    
$http false;
                    if (
$xmls[1][$x]{0} != "#" && (!preg_match("/^\w{3,6}:/"$xmls[1][$x]) || $http preg_match("/^http:\/\//"$xmls[1][$x]))) {
                      
$base = ($http) ? "" : (($xmls[1][$x]{0} == "/") ? $_URI['home'] : $_URI['base']);
                      
$xmls[2][$x] = str_replace($xmls[1][$x], get_Data($base.$xmls[1][$x], $xmls[1][$x]), $xmls[0][$x]);
                    }
                  }
                  if (
$x$_URI['bodyout'] = str_replace($xmls[0], $xmls[2], $_URI['bodyout']);

                  
/* ***** <STYLE>@import url()</STYLE> ************* */
                  
preg_match_all("/<style[^>]*>(.*?)<\/style>/si"$_URI['body'], $styles);
                  
$styles[2] = $styles[0];
                  for (
$x 0$x count($styles[1]); $x++) {
                    
preg_match_all("/@import\s+url\(([^\)]+)\)/i"$styles[1][$x], $imports);
                    
$imports[2] = $imports[1];
                    for (
$y 0$y count($imports[1]); $y++) {
                      
$imports[1][$x] = trim($imports[1][$x], "'\"");
                      
$http false;
                      if (
$imports[1][$y]{0} != "#" && (!preg_match("/^\w{3,6}:/"$imports[1][$y]) || $http preg_match("/^http:\/\//"$imports[1][$y]))) {
                        
$base = ($http) ? "" : (($imports[1][$y]{0} == "/") ? $_URI['home'] : $_URI['base']);
                        
$imports[2][$y] = str_replace($imports[1][$y], get_Data($base.$imports[1][$y], $imports[1][$y]), $imports[0][$y]);
                      }
                    }
                    if (
$y$styles[2][$x] = str_replace($imports[0], $imports[2], $styles[0][$x]);
                  }
                  if (
$x$_URI['bodyout'] = str_replace($styles[0], $styles[2], $_URI['bodyout']);

                  
/* ***** <STYLE>@import ""</STYLE> **************** */
                  
for ($x 0$x count($styles[1]); $x++) {
                    
preg_match_all("/@import\s+[\"']([^\"']+)[\"']/i"$styles[1][$x], $imports);
                    
$imports[2] = $imports[1];
                    for (
$y 0$y count($imports[1]); $y++) {
                      
$http false;
                      if (
$imports[1][$y] != "#" && (!preg_match("/^\w{3,6}:/"$imports[1][$y]) || $http preg_match("/^http:\/\//"$imports[1][$y]))) {
                        
$base = ($http) ? "" : (($imports[1][$y]{0} == "/") ? $_URI['home'] : $_URI['base']);
                        
$imports[2][$y] = str_replace($imports[1][$y], get_Data($base.$imports[1][$y], $imports[1][$y]), $imports[0][$y]);
                      }
                    }
                    if (
$y$styles[2][$x] = str_replace($imports[0], $imports[2], $styles[0][$x]);
                  }
                  if (
$x$_URI['bodyout'] = str_replace($styles[0], $styles[2], $_URI['bodyout']);

                  
/* ***** LINK: ************************************ */
                  
if (count($_URI['hlink'])) {
                    
$build "";
                    foreach (
$_URI['hlink'] as $hlink) {
                      
$http false;
                      if (
$hlink[1]{0} != "#" && (!preg_match("/^\w{3,6}:/"$hlink[1]) || $http preg_match("/^http:\/\//"$hlink[1]))) {
                        
$base = ($http) ? "" : (($hlink[1]{0} == "/") ? $_URI['home'] : $_URI['base']);
                        
$build .= "  <link rel=\"{$hlink[0]}\" type=\"text/css\" href=\"".get_Data($base.$hlink[1], $hlink[1])."\" ".(($bits['ext'] != "html") ? "/" "").">\n";
                      }
                    }
                    if (
preg_match("/<head[^>]*>/i"$_URI['bodyout'])) {
                      
$_URI['bodyout'] = preg_replace("/(<head[^>]*>)/i""$1\n$build\n"$_URI['bodyout'], 1);
                    } else if (
preg_match("/<title/i"$_URI['bodyout'])) {
                      
$_URI['bodyout'] = preg_replace("/(<title)/i""$build$1"$_URI['bodyout'], 1);
                    } else 
$_URI['bodyout'] = $build.$_URI['bodyout'];
                  }

                  
/* ***** No <META> Content-type ******************* */
                  
if (!$mct && $_URI['mime'] != "unknown" && $_URI['charset'] != "unknown") {
                    
$_URI['bodyout'] = preg_replace("/<meta[^>]+?http-equiv=[^>]*?Content-type[^>]+?>/i"""$_URI['bodyout']);
                    
$build "  <meta http-equiv=\"Content-Type\" content=\"{$_URI['mime']}; charset={$_URI['charset']}\" />\n";
                    if (
preg_match("/<head[^>]*>/i"$_URI['bodyout'])) {
                      
$_URI['bodyout'] = preg_replace("/(<head[^>]*>)/i""$1\n$build\n"$_URI['bodyout'], 1);
                    } else if (
preg_match("/<title/i"$_URI['bodyout'])) {
                      
$_URI['bodyout'] = preg_replace("/(<title)/i""$build$1"$_URI['bodyout'], 1);
                    } else 
$_URI['bodyout'] = $build.$_URI['bodyout'];
                  }

                  
/* ***** No <BASE> ******************************** */
                  
if (!count($bases[0])) {
                    
$build "  <base href=\"{$_URI['base']}\" />\n";
                    if (
preg_match("/<head[^>]*>/i"$_URI['bodyout'])) {
                      
$_URI['bodyout'] = preg_replace("/(<head[^>]*>)/i""$1\n$build\n"$_URI['bodyout'], 1);
                    } else if (
preg_match("/<title/i"$_URI['bodyout'])) {
                      
$_URI['bodyout'] = preg_replace("/(<title)/i""$build$1"$_URI['bodyout'], 1);
                    } else 
$_URI['bodyout'] = $build.$_URI['bodyout'];
                  }

                  
/* ***** <FRAME> ********************************** */
                  
preg_match("/<frameset[^>]*>(.*?)<\/frameset>/si"$_URI['bodyunc'], $_URI['frameset']);
                  if (isset(
$_URI['frameset'][1])) {
                    
$_URI['frameset'] = $_URI['frameset'][1];
                    
preg_match_all("/<frame\s[^>]+>/i"$_URI['frameset'], $frames);
                    
$frames[1] = $frames[0];
                    for (
$x 0$x count($frames[0]); $x++) {
                      
$tag parseHTMLTag($frames[0][$x]);
                      if (isset(
$tag['src'])) {
                        
$http false;
                        if (
$tag['src']{0} != "#" && (!preg_match("/^\w{3,6}:/"$tag['src']) || $http preg_match("/^http:\/\//"$tag['src']))) {
                          
$base = ($http) ? "" : (($tag['src']{0} == "/") ? $_URI['home'] : $_URI['base']);
                          
$frames[1][$x] = str_replace($tag['src'], get_Data($base.$tag['src'], $tag['src']), $frames[0][$x]);
                        }
                      }
                    }
                    if (
$x$_URI['bodyout'] = str_replace($frames[0], $frames[1], $_URI['bodyout']);
                  }

                  
/* ***** <IFRAME> ********************************* */
                  
preg_match_all("/<iframe\s[^>]+>/i"$_URI['bodyunc'], $iframes);
                  
$iframes[1] = $iframes[0];
                  for (
$x 0$x count($iframes[0]); $x++) {
                    
$tag parseHTMLTag($iframes[0][$x]);
                    if (isset(
$tag['src'])) {
                      
$http false;
                      if (
$tag['src']{0} != "#" && (!preg_match("/^\w{3,6}:/"$tag['src']) || $http preg_match("/^http:\/\//"$tag['src']))) {
                        
$base = ($http) ? "" : (($tag['src']{0} == "/") ? $_URI['home'] : $_URI['base']);
                        
$iframes[1][$x] = str_replace($tag['src'], get_Data($base.$tag['src'], $tag['src']), $iframes[0][$x]);
                      }
                    }
                  }
                  if (
$x$_URI['bodyout'] = str_replace($iframes[0], $iframes[1], $_URI['bodyout']);


                  
/* ***** <SCRIPT> ********************************* */
                  
preg_match_all("/<script\s[^>]+>(<\/script>)?/i"$_URI['body'], $scripts);
                  
$scripts[2] = $scripts[0];
                  for (
$x 0$x count($scripts[0]); $x++) {
                    
$tag parseHTMLTag($scripts[0][$x]);
                    if (isset(
$tag['src'])) {
                      
$http false;
                      if (
$tag['src']{0} != "#" && (!preg_match("/^\w{3,6}:/"$tag['src']) || $http preg_match("/^http:\/\//"$tag['src']))) {
                        
$base = ($http) ? "" : (($tag['src']{0} == "/") ? $_URI['home'] : $_URI['base']);
                        
$getData get_Data($base.$tag['src'], $tag['src']);
                        if (
strpos($getData"data:text/javascript;base64,") === &&
                            
$scripts[1][$x] == "</script>" &&
                            
count($_CHAIN) > 1) {
                          
$scripts[2][$x] = str_replace($tag['src'], ""$scripts[0][$x]);
                          
$scripts[2][$x] = preg_replace("/\ssrc=['\"]*(\s|>)/"" $1"$scripts[2][$x]);
                          
$scripts[2][$x] .= "<!--\n".base64_decode(str_replace("data:text/javascript;base64,"""$getData))."\n//--></script>";
                        } else 
$scripts[2][$x] = str_replace($tag['src'], $getData$scripts[0][$x]).$scripts[1][$x];
                      }
                    }
                  }
                  if (
$x$_URI['bodyout'] = str_replace($scripts[0], $scripts[2], $_URI['bodyout']);

                  
/* ***** <IMG> ************************************ */
                  
preg_match_all("/<img\s[^>]+>/i"$_URI['bodyunc'], $images);
                  
$images[1] = $images[0];
                  for (
$x 0$x count($images[0]); $x++) {
                    
$tag parseHTMLTag($images[0][$x]);
                    if (isset(
$tag['src'])) {
                      
$http false;
                      if (
$tag['src']{0} != "#" && (!preg_match("/^\w{3,6}:/"$tag['src']) || $http preg_match("/^http:\/\//"$tag['src']))) {
                        
$base = ($http) ? "" : (($tag['src']{0} == "/") ? $_URI['home'] : $_URI['base']);
                        
$images[1][$x] = str_replace($tag['src'], get_Data($base.$tag['src'], $tag['src']), $images[0][$x]);
                      }
                    }
                  }
                  if (
$x$_URI['bodyout'] = str_replace($images[0], $images[1], $_URI['bodyout']);

                  
/* ***** <INPUT TYPE="IMAGE"> ********************* */
                  
preg_match_all("/<input\s[^>]+>/i"$_URI['bodyunc'], $images);
                  
$images[1] = $images[0];
                  for (
$x 0$x count($images[0]); $x++) {
                    
$tag parseHTMLTag($images[0][$x]);
                    if (isset(
$tag['type']) && strtolower($tag['type']) == "image" && isset($tag['src'])) {
                      
$http false;
                      if (
$tag['src']{0} != "#" && (!preg_match("/^\w{3,6}:/"$tag['src']) || $http preg_match("/^http:\/\//"$tag['src']))) {
                        
$base = ($http) ? "" : (($tag['src']{0} == "/") ? $_URI['home'] : $_URI['base']);
                        
$images[1][$x] = str_replace($tag['src'], get_Data($base.$tag['src'], $tag['src']), $images[0][$x]);
                      }
                    }
                  }
                  if (
$x$_URI['bodyout'] = str_replace($images[0], $images[1], $_URI['bodyout']);

                  
/* ***** <____ BACKGROUND=""> ********************* */
                  
preg_match_all("/<[^>]+\sbackground=[^>]+>/i"$_URI['bodyunc'], $backgrounds);
                  
$backgrounds[1] = $backgrounds[0];
                  for (
$x 0$x count($backgrounds[0]); $x++) {
                    
$tag parseHTMLTag($backgrounds[0][$x]);
                    if (isset(
$tag['background'])) {
                      
$http false;
                      if (
$tag['background']{0} != "#" && (!preg_match("/^\w{3,6}:/"$tag['background']) || $http preg_match("/^http:\/\//"$tag['background']))) {
                        
$base = ($http) ? "" : (($tag['background']{0} == "/") ? $_URI['home'] : $_URI['base']);
                        
$backgrounds[1][$x] = str_replace($tag['background'], get_Data($base.$tag['background'], $tag['background']), $backgrounds[0][$x]);
                      }
                    }
                  }
                  if (
$x$_URI['bodyout'] = str_replace($backgrounds[0], $backgrounds[1], $_URI['bodyout']);


                  
$_URI['body'] = $_URI['bodyout'];
                  if (
$basepage) return $_URI['body'];
                }

                
$_PHF['index'][$fulluri] = ($encode) ? "data:{$_URI['mime']};base64,".base64_encode($_URI['body']) : $_URI['body'];
                
array_pop($_CHAIN);


       
addTime("PHPxx");  /* ***** Record time split **************** */


                
return $_PHF['index'][$fulluri];

              } else if (
$basepage) die("Document contained no data");
            } else if (
$basepage) die("Unacceptable mime-type: ".htmlspecialchars($_URI['mime']));
          } else if (
$basepage) die("Unable to contact ".htmlspecialchars($_URI['host']));
        } else if (
$basepage) die("Invalid hostname");
      } else if (
$basepage) die("Invalid protocol");
    } else if (
$basepage) die("Invalid URI");
  }
  if (!
$basepage) {
    
array_pop($_CHAIN);
    return 
$original;
  } else die(
"Could not open ".htmlspecialchars(rawurldecode($_POST['phf'])));
}


/* ***** Execute **************************************************** */


       
addTime("Initx");  /* ***** Record time split **************** */


$output get_Data($_PAGE$_PAGEfalsetrue);

$output .= "
<!-- Generated in "
.sprintf("%01.3f"array_sum(explode(" "microtime())) - $_PHF['mark'])."s by PHF-Get {$_PHF['version']} -->
<!-- "
.date("r")." -->
<!-- "
.print_r($_TIMERtrue)." -->";


       
addTime("PHPxx");  /* ***** Record time split **************** */


// uncomment and edit the line below if you would like to be emailed each time this script is run
// mail("phf@example.com", "PHF Tool Use", "IP: {$_SERVER['REMOTE_ADDR']}\n\nThe tool downloaded:\n$_PAGE\n\nThe file generated was ".strlen($output)." bytes.\n\nIt took ".sprintf("%01.3f", array_sum(explode(" ", microtime())) - $_PHF['mark'])."s of processing time");


header("Content-Type: application/x-phf-html");
header("Content-Length: ".strlen($output).";\r\n");
header("Content-Disposition: attachment; filename=\"{$_PHF['filename']}.phf.{$_PHF['extension']}\";\r\n");

echo 
$output;


?>