<?php
// Evaluate text file with mathematical operators or map to operators if csv.  R.Metcalfe 21/8/2013
$comma = -1;
$ok = "ok";
$replacewith = "";

function analyze($inl) {
  global $replacewith, $comma;
  $def = "";
  $prefix = "";
  $isn = true;
  $outl = $inl;
  if (!isset($_GET['leaveinquotes'])) {
    $inl = str_replace(' ', "", str_replace("'", "", str_replace('"', "", $outl)));
  }
  if ($comma < 0) {
    $comma = 0;
    $inl = str_replace(' ', "", str_replace("'", "", str_replace('"', "", $outl)));
    $huh = strpos($inl, ",");
    while ($huh !== false) {
      $comma = $comma + 1;
      $def = $def . "+";
      $preinl = substr($inl, 0, ($huh - 0));
      if ($isn) $isn = is_numeric($preinl);
      
      $prefix .= " " . $preinl . "+" . $isn . " ";
      
      if (strlen($inl) <= ($comma + 1)) {
        $inl = "";
      } else {
        $inl = substr($inl, ($huh + 1));
      }
      $huh = strpos($inl, ",");
    }
    if ($comma > 0) {
      echo " replacewith = prompt('Please supply " . $comma . " character Mathematical operator string (no spaces please) for your calculations out of + - * / & | ^ ~ % (and will allow << >> >>> for other shift operators, if last to do) ', '" . substr($def, 0, $comma) . "'); " . "\n";
    }
  }
  if (!$isn && !isset($_GET['leaveinquotes'])) {
    return "";
  }
  return $outl;
}

if (!isset($_GET['infile'])) {
  echo "<html><head>" . "</head><body id='xbod'>" . "<s" . "cript> var inf=prompt('Please enter Mathematical input filename or URL full of lines like 45*34/56= or CSV lines like 45,34,56',''); if (inf.length > 1) window.location = './eval_js_php.php?infile=' + inf; " . "</s" . "cript></body></html> \n";
}

if (isset($_GET['infile'])) {
$getinfile = $_GET['infile'];

$httphuh = strpos($getinfile, "http");
$filehuh = strpos($getinfile, "file:");

if ($httphuh !== false || $filehuh !== false) {
  $myc = file_get_contents($getinfile);
  $fh = fopen("youllneverbelieve.this", "w");
  fwrite($fh, $myc);
  fclose($fh);
  $getinfile = "youllneverbelieve.this";
}

if (!file_exists($getinfile)) {
die("Please use syntax like eval_js_php.php" . "?infile=aninfile.txt&interactive=y&leaveinquote=y&justanswer=n with entries like 6 * 5 / 4= (where the file name really exists)");
} else {
$lines = file($getinfile);
if ($getinfile == "youllneverbelieve.this") {
  unlink("youllneverbelieve.this");
}
$top200 = array_slice($lines,0,200);
echo "<html><head>" . "</head><body id='xbod'><p id='replacewith' style='display:none;'></p>" . "<s" . "cript> " . "\n";
?>

var replacewith = '';

function anal(inf,way) {
<?php
 if (isset($_GET['justanswer'])) {
  if (substr($_GET['justanswer'],0,1) == "y" || substr($_GET['justanswer'],0,1) == "Y") {
   echo " if (way == 0) way = way - 1; " . "\n";
  }
 }
?>
 var outf = inf;
 var j;
 if (replacewith != '') {
   outf = '';
   var readInStrings = inf.split(/,/);
   outf = readInStrings[0];
   for (var i=1; i < readInStrings.length; i++) {
     j = i;
     if ((i+1) == readInStrings.length) {
       j = replacewith.length;
     }
     outf = outf + replacewith.substring((i - 1), j) + readInStrings[i];
   }
   if (way == 1) {
     //alert(inf + " vs " + outf + " via " + replacewith);
     outf = eval(outf);
   } else if (way < 0) {
     outf = "";
   } else {
     outf = outf + "=";
   }
 }
 return outf;
}

<?php
echo " var xb=document.getElementById('xbod'); \n";
foreach($top200 as $line)
{
$ok = "ok";
if ($comma < 0) {
  $ok = analyze($line);
}
if ($ok != "") {
if (isset($_GET['interactive'])) {
if ($comma <= 0) {
echo "alert(anal('" . str_replace("\n", "", analyze($line)) . "',0) + eval(" . str_replace("\n", "", str_replace("=", "", analyze($line))) . ")); ";
} else {
echo "alert(anal('" . str_replace("\n", "", analyze($line)) . "',0) + anal('" . str_replace("\n", "", str_replace("=", "", analyze($line))) . "',1)); ";
}
} else if ($comma <= 0) {
eval('$a='.str_replace("\n", "", str_replace("=", "", analyze($line))).';');
echo "xb.innerHTML = xb.innerHTML + anal('<p>" . str_replace("\n", "", analyze($line)) . $a . "</p><br>'); ";
} else {
//eval('$a='.str_replace("\n", "", str_replace("=", "", analyze($line))).';');
echo "xb.innerHTML = xb.innerHTML + anal('<p>" . str_replace("\n", "", analyze($line)) . "',0) + anal('" . str_replace("\n", "", analyze($line)) . "',1) + '</p><br>'; ";
}
}
}
echo "</s" . "cript></body></html>" . "\n";
}
} else {
die("Please use syntax like eval_js_php.php" . "?infile=aninfile.txt&interactive=y&leaveinquote=y&justanswer=n with entries like 6 * 5 / 4=");
}
?>
