<?php
// content_bargraph.php
// Read popular content word numbers into bargraph
// RJM Programming
// May, 2015

set_time_limit(0);
$harray = array();
$cstring = ",0,#word,";
$one=1;
$url = "";
$cont = "";
$goes = 0;
$biggest=-1;
$smallest=-1;
$fz = 0;
$data = "";
$lenlim=0;

$im = null;

// Thanks to http://stackoverflow.com/questions/3163047/how-to-check-whether-every-character-is-alpha-numeric-in-php
function alpha($string){
    global $lenlim;
    if (strlen($string) <= $lenlim) return false;
    if (function_exists('ctype_alnum')) {
      $ret = ctype_alpha($string);
    } else {
      $ret = preg_match('/^[a-zA-Z]+$/i', $string) > 0;
    }
    if ($ret) {
      $ret=strpos($string, "a") | strpos($string, "e") | strpos($string, "i") | strpos($string, "o") | strpos($string, "u") | strpos($string, "y") | strpos($string, "A") | strpos($string, "E") | strpos($string, "I") | strpos($string, "O") | strpos($string, "U") | strpos($string, "Y");
    }
    return $ret;
}

function htmlis() {
  global $url, $cstring, $lenlim;
  
  $things = explode(",#", $cstring);
  
  $hstr="<html><head><script type='text/javascript'> function onld() { if (document.URL.indexOf('?d=0') != -1) {   top.document.getElementById('aread').value=1.0; top.document.getElementById('mymeter').value=1.0;   } location.href='http://www.rjmprogramming.com.au/PHP/BarChart/bar_chart.php?title=" . str_replace(" ", "%20", $url) . "&label=Word%20Frequency&value=Frequency&data=";
  // ,%20[~ffffff~,11]%20,%20[~000000~,22]
  for ($ii=1; $ii<sizeof($things); $ii++) {
   $nums=explode(",", $things[-1 + $ii]);
   $cols=explode(",", $things[$ii]);
   if (strpos($hstr, ",%20[~" . $cols[0] . "~,") === false && strpos($cstring, ",#" . $cols[0] . ",") !== false) $hstr .= ",%20[~" . $cols[0] . "~," . $nums[sizeof($nums) - 1] . "]";
  }
  $hstr .= "'; } </script></head><body onload=' onld(); '></body></html> \n";
  try {
    file_put_contents("wordco" . server_remote_addr() . ".htm", $hstr);
  } catch(Exception $e) {
  }
  try {
    file_put_contents("wordco" . server_remote_addr() . ".html", $hstr);
  } catch(Exception $ee) {
  }
}


function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    return str_replace(":", "_", str_replace(".", "_", $rma));
}

if (isset($_GET['longerthan'])) $lenlim=$_GET['longerthan'];
if (isset($_POST['longerthan'])) $lenlim=$_POST['longerthan'];
if (isset($_GET['reading'])) {
  $grst = urldecode($_GET['reading']);
  $grs = explode(" ", $grst);
  $gr = $grs[0];
  if (file_exists("wordco" . server_remote_addr() . ".far")) {
    $gr = "";
    while ($gr == "") {
      if (!file_exists("wordco" . server_remote_addr() . ".far")) {
        $goes++;
        if ($goes == 5) $gr = "1.0";
      } else {
        $goes = 0;
        $grst = @file_get_contents("wordco" . server_remote_addr() . ".far");
        $grs = explode(" ", $grst);
        $gr = $grs[0];
      }
    }
    echo "<!doctype html><html><body onload=\"top.document.getElementById('aread').value=" . $gr . "; top.document.getElementById('mymeter').value=" . $gr . ";\"></body></html>"; 
  } else if ($gr == "1.0" || $gr == "1") {
    echo "<!doctype html><html><body onload=\"top.document.getElementById('aread').value=1.0; top.document.getElementById('mymeter').value=1.0; parent.document.getElementById('myiframetwo').style.display='block';  parent.document.getElementById('myiframe').style.display='NONE'; parent.document.getElementById('myiframe').src='http://www.rjmprogramming.com.au';\"></body></html>"; 
  } else {
    echo "<!doctype html><html><body onload=\"top.document.getElementById('aread').value=" . $gr . "; top.document.getElementById('mymeter').value=" . $gr . ";\"></body></html>"; 
  }
  exit;
} else if (isset($_GET['url'])) {
  if (file_exists("wordco" . server_remote_addr() . ".far")) unlink("wordco" . server_remote_addr() . ".far");
  try {
  file_put_contents("wordco" . server_remote_addr() . ".html", "<html><body><div align='center'>Bar Graph Coming Soon ...</div></body></html>");
  } catch (Exception $aq1) { }
  try {
  file_put_contents("wordco" . server_remote_addr() . ".htm", "<html><body><div align='center'>Bar Graph Coming Soon ...</div></body></html>");
  } catch (Exception $aq2) { }
  $url = urldecode($_GET['url']);
} else if (isset($_POST['url'])) {
  if (file_exists("wordco" . server_remote_addr() . ".far")) unlink("wordco" . server_remote_addr() . ".far");
  try {
  file_put_contents("wordco" . server_remote_addr() . ".html", "<html><body><div align='center'>Bar Graph Coming Soon ...</div></body></html>");
  } catch (Exception $aq3) { }
  try {
  file_put_contents("wordco" . server_remote_addr() . ".htm", "<html><body><div align='center'>Bar Graph Coming Soon ...</div></body></html>");
  } catch (Exception $aq4) { }
  $url = urldecode($_POST['url']);
} else {
  if (file_exists("wordco" . server_remote_addr() . ".far")) {
    unlink("wordco" . server_remote_addr() . ".far");
  }
  try {
  file_put_contents("wordco" . server_remote_addr() . ".html", "<html><body><div align='center'>Bar Graph Coming Soon ...</div></body></html>");
  } catch (Exception $aq5) { }
  try {
  file_put_contents("wordco" . server_remote_addr() . ".htm", "<html><body><div align='center'>Bar Graph Coming Soon ...</div></body></html>");
  } catch (Exception $aq6) { }
  echo "<!doctype html><html><head><title>Read content popular English words into bargraph with progress bar - RJM Programming - May, 2015</title><link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'><script type='text/javascript'> var sto=null; var cnt=1; var suffix='l'; var aread=0; function check() { aread=document.getElementById('aread').value; var isd=new Date().toJSON().slice(0,21); if (aread >= 1.0) { isd='0'; }   parent.document.getElementById('myiframetwo').style.display='block';  if (isd.indexOf('5.') != -1 || isd.indexOf('1.') != -1 || aread == 1.0) { parent.document.getElementById('myiframetwo').src='wordco" . server_remote_addr() . ".htm' + suffix + '?d=' + isd; if (suffix == 'l') { suffix=''; } else { suffix='l'; } } document.getElementById('myiframe').src='./content_bargraph.php?reading=' + aread; if (aread == 1.0) { clearInterval(sto); aread=1.01; }  } </script></head><body style=' background-color:#f0f0f0; ' onload='sto=setInterval(check,7000);'><h1 align='center'>Content Popular English Words Bar Graph - RJM Programming - May, 2015</h1><br><div align='center'><form target='myiframe' action='./content_bargraph.php' method='POST'>Content File (http:// or file:// syntax accepted (or a local HTML file)): <input type='text' size=60 id='url' name='url' value='http://'></input><input type='hidden' id='aread' name='aread' value='0'></input>&nbsp;&nbsp;<select id='longerthan' name='longerthan'><option value=0>No length limit</option><option value=1>Longer than 1</option><option value=2>Longer than 2</option><option value=3>Longer than 3</option><option value=4>Longer than 4</option><option value=5>Longer than 5</option><option value=6>Longer than 6</option><option value=7>Longer than 7</option><option value=8>Longer than 8</option><option value=9>Longer than 9</option></select>&nbsp;&nbsp;<input onclick=\"document.getElementById('myiframe').src='http://www.rjmprogramming.com.au'; if (sto != null) { clearInterval(sto); sto=setInterval(check,7000); }  \" type='submit' value='Create Bar Graph'></input></form><br><meter id='mymeter' value='0'></meter><br><br><iframe height=400 width=780 id='myiframetwo' src='javascript:none;' style='display:none;'></iframe><br><iframe id='myiframe' src='javascript:none;' style='display:none;'></iframe></div></body></html>"; 
  exit;
}

if ($url != "") {
   $data = "";
   $url = "";
 if ((isset($_GET['url']) || isset($_POST['url'])) && !isset($_GET['data']) && !isset($_POST['data'])) {
  if (isset($_GET['url'])) $url = urldecode($_GET['url']);
  if (isset($_POST['url'])) $url = urldecode($_POST['url']);
  if (strpos(("*" . $url), "*file:") !== false) {
       $data = file_get_contents(dirname(__FILE__) . "/" . str_replace("file://", "", str_replace("file:///", "/", str_replace("/../../../", "/", urldecode($_GET['url'])))));   // eg. file://c/windows/proxy.pac
  } else {
       $data = file_get_contents($url);
  }
 }
 if (isset($_GET['data'])) {
  if (isset($_GET['url'])) $url = urldecode($_GET['url']);
  if (isset($_POST['url'])) $url = urldecode($_POST['url']);
   $data = urldecode($_GET['data']);
 } else if (isset($_POST['data'])) {
  if (isset($_GET['url'])) $url = urldecode($_GET['url']);
  if (isset($_POST['url'])) $url = urldecode($_POST['url']);
   $data = urldecode($_POST['data']);
 } // else {
 if (1 == 1) {
 $adata = $data;
 if ((isset($_GET['url']) || isset($_POST['url'])) && (($data != "") || strpos((strtolower($url) . "*"), ".html*") !== false || strpos((strtolower($url) . "*"), ".htm*") !== false || strpos((strtolower($url) . "*"), ".xml*") !== false    )) {
  if (1 == 11) {
  $data = "  ['test three'], ";
  } else {
  try {
  $doc = new DOMDocument();
  $doc->preserveWhiteSpace = FALSE;
  $before = libxml_use_internal_errors(true);
  if (strpos(strtolower($url) . "?", ".xml?") !== false) {
  $isxml = true;
  $doc->loadXML($data);
  $predata = "";
  $data = $doc->textContent;
  $cont = $data;
  $suffix = ".";
  $phrases = explode(".", $data);
  $data = ""; //$predata;
  for ($i=0; $i<sizeof($phrases); $i++) {
    if (($i + 1) >= sizeof($phrases)) $suffix = "";
    $predata .= "  ['";
    $predata .= $phrases[$i] . $suffix . "'], \n";
  }
  $data = $predata;
  $done = true;
  $adata = "";
  } else {
  $parts = explode("head>", $data);
  if (sizeof($parts) > 1) {
    $data = substr($parts[0], 0, (strlen($parts[0]) - 1)) . $parts[2];
  }
  $doc->loadHTML($data);
  $data = $doc->textContent;
  $cont = $data;
  $predata = "";
  $suffix = ".";
  $lines = explode("\n", $data);
  $data = ""; //$predata;
  for ($l=0; $l<sizeof($lines); $l++) {
  if (str_replace(" ","",str_replace("\r","",$lines[$l])) != "") {
  $phrases = explode(".", str_replace("\r","",$lines[$l]));
  for ($i=0; $i<sizeof($phrases); $i++) {
    if (($i + 1) >= sizeof($phrases)) $suffix = "";
    $semic = explode(";", $phrases[$i]);
    for ($ii=0; $ii<sizeof($semic); $ii++) {
    $predata .= "  ['";
    $predata .= str_replace("  ", " ", str_replace("  ", " ", str_replace("  ", " ", str_replace("  ", " ", str_replace("\n", " ", str_replace("[", " ", str_replace("]", " ", str_replace("'", " ", $semic[$ii])))))))) . "'], \n";
    }
  }
  }
  }
  $data = $predata;
  $done = true;
  $adata = "";
  }
  libxml_use_internal_errors($before);
  $docb = true;
  } catch (Exception $e) {
  throw new Exception( 'Something really wrong', 0, $e);
  }
  }
  }
  $fz = strlen($cont);
  
  }

if ($cont == "" && $url != "") {
   file_put_contents("wordco" . server_remote_addr() . ".farit", "-2");
  $cont = @file_get_contents($url);
   file_put_contents("wordco" . server_remote_addr() . ".farit", "-1");
  $fz = strlen($cont);
   file_put_contents("wordco" . server_remote_addr() . ".farit", "00" . $fz . $url);
} else {

   file_put_contents("wordco" . server_remote_addr() . ".farit", "0" . $fz);
}

if ($cont != "" && ($fz > 0 || $fz < 0)) {

   file_put_contents("wordco" . server_remote_addr() . ".farit", "1");

  $k = 0;
  $words = explode(" ", $cont);
  $isw = 0 + sizeof($words);
  $sw = 0 + sizeof($words);
  $x = 0;
  for($x = 0; $x < $isw; $x++) {
     $sw--;
     if (alpha($words[$x])) {
     $k++;
     try {
      if (($k % 20) == 0) {
   file_put_contents("wordco" . server_remote_addr() . ".farit", $k . " then ");
       file_put_contents("wordco" . server_remote_addr() . ".far", ((1.0 + $x) / (0.0 + $isw)) . " " . $cstring);
   file_put_contents("wordco" . server_remote_addr() . ".farit", $k . " Then ");
       htmlis();
   file_put_contents("wordco" . server_remote_addr() . ".farit", $k . " TheN ");
      }
        $thiscol = $words[$x];
        if ($harray[$thiscol] == null) $harray[$thiscol] = 0;
        if (1 == 1) {
          $pos=strpos($cstring, "," . $harray[$thiscol] . ",#");
          if ($pos === false) $pos = -1;
          if (strpos($cstring, "," . $harray[$thiscol] . ",#" . $thiscol . ",") !== false) {
            str_replace("," . $harray[$thiscol] . ",#" . $thiscol . ",", "," . ($harray[$thiscol] + 1) . ",#" . $thiscol . ",", $cstring);
          } else if ($pos == 0) {
            $xstring="," . ($harray[$thiscol] + 1) . ",#" . $thiscol . substr($cstring,0,100);
            $cstring=$xstring;
            $one++;
          } else if ($pos > 0 && $pos < 70) {
            $xstring=substr($cstring,0,$pos) . "," . ($harray[$thiscol] + 1) . ",#" . $thiscol . substr($cstring,$pos,(100 - $pos));
            $cstring=$xstring;
            $one++;
          }
          $harray[$thiscol] += 1;
        }
      
     } catch (Exception $ew) {
     }
    }
     if ($sw < 10) {
       file_put_contents("wordco" . server_remote_addr() . ".far", ((1.0 + $x) / (0.0 + $isw)) . " " . $cstring);
       htmlis();
     }
  }

   unlink("wordco" . server_remote_addr() . ".far");
   file_put_contents("wordco" . server_remote_addr() . ".fargo", $cstring);
   htmlis();
   echo "<!doctype html><html><body onload=\"  top.document.getElementById('aread').value=1.0; top.document.getElementById('mymeter').value=1.0; parent.document.getElementById('myiframetwo').src='wordco" . server_remote_addr() . ".htm' + suffix + '?d=0';\"></body></html>"; 
   exit;
  }

}

?>
