<?php
  // rhopalic_letter_sentence.php
  // Rhopalic Letter Sentence
  // RJM Programming
  // February, 2015
  
  $tois = "";
  $ideatois = "";
  if (file_exists("rls_" . server_remote_addr() . ".huh")) {
        $ideatois = @file_get_contents("rls_" . server_remote_addr() . ".huh");
  } 


function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if (1 == 2) {
    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 $rma;
}

  
  if (isset($_GET['to']) && isset($_GET['subject']) && isset($_GET['body'])) {
   $to = urldecode($_GET['to']);
   if (strpos($to, "@") !== false) {
        @file_put_contents("rls_" . server_remote_addr() . ".huh", $to);
   } 
   $subject = urldecode($_GET['subject']);
   $body = urldecode($_GET['body']);
   if (strpos($subject, "My Rhopalic Letter Sentence") !== false) {
    if (strpos($body, "http://www.rjmprogramming.com.au/PHP/rhopalic_letter_sentence.php?") !== false && strpos($body, "w1=") !== false && strpos($body, "&w2=") !== false) {
     $huhs = explode("w1=", $body);
     if (strpos($huhs[1], "%20") === false && strpos($huhs[1], " ") === false) {
      mail($to, $subject, str_replace(" ", "+", str_replace("tois=","toisx=",$body)  . urlencode("&tois=" . $to)));
     } else if (1 == 2) {
        @file_put_contents("aaa.ddd", $huhs[1]);
     }
    } else if (1 == 4) {
        @file_put_contents("aaa.ccc", $body);
    }
   } else if (1 == 9) {
        @file_put_contents("aaa.bbb", $subject);
   }
   exit;
  } else {
  
  $words = array("A", "go", "for", "them", "would", "really", "improve", "fairness", "standards", "constantly", "threatening", "cohesiveness", "", "", "", "", "", "", "", "");
  $wordd = array("inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "none", "none", "none", "none", "none", "none", "none", "inline");
  $ending = ".";
  
  $score = 0;
  $goes = 0;
  
  $targeteq = "";
  
  $kk = -1;
  
  $isvalid = "y";
  
  if (isset($_GET['tois'])) {
   if (strpos(urldecode($_GET['tois']), "@") !== false) {
    $tois = urldecode($_GET['tois']);
   }
  }
  if (isset($_GET['w21'])) $ending = urldecode($_GET['w21']);
  
  if (isset($_GET['score'])) $score = $_GET['score'];
  if (isset($_GET['goes'])) $goes = $_GET['goes'];
  
  $rest = $_GET['w1'] . "~" . $_GET['w2'] . "~" . $_GET['w3'] . "~" . $_GET['w4'] . "~" . $_GET['w5'] . "~" . $_GET['w6'] . "~" . $_GET['w7'] . "~" . $_GET['w8'] . "~" . $_GET['w9'] . "~" . $_GET['w10'] . "~" . $_GET['w11'] . "~" . $_GET['w12'] . "~" . $_GET['w13'] . "~" . $_GET['w14'] . "~" . $_GET['w15'] . "~" . $_GET['w16'] . "~" . $_GET['w17'] . "~" . $_GET['w18'] . "~" . $_GET['w19'] . "~" . $_GET['w20'];
  $westart = 0;
  $tstart = -1;
  $roff = 0;
  
  if (strlen($_SERVER['QUERY_STRING']) > 0) {
    $w = explode("~", $rest);
    for ($j=0; $j<20; $j++) {
     $words[$j] = "";
     if (isset($w[$j])) $words[$j] = $w[$j];
     if (strlen($words[$j]) >= 1) {
        if (strlen($words[$j]) != ($j + 1)) $westart = -1;
        $startword = file_get_contents(str_replace(":443","","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT']) . "/PHP/surprise.php?huh=" . strtolower($words[$j]) . "&youllneverfindthis=y");
        if ($startword == "true") {
          $startword = $startword;
        } else {
          $startword = file_get_contents(str_replace(":443","","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT']) . "/PHP/surprise.php?huh=" . str_replace("*","",str_replace("s*","",(strtolower($words[$j]) . "*"))) . "&youllneverfindthis=y");
        if ($startword == "true") {
          $startword = $startword;
        } else {
                  if ($startword == "false") $roff += strlen($words[$j]);
                  if ($startword == "false") $words[$j]='';
        }
        }
     }
     if (strlen($words[$j]) != 0) {
      if ($tstart < 0) {
        $tstart=$j;
        if ($j > 0 && $westart == 0) {
          for ($jj=0; $jj<$j; $jj++) { $wordd[$jj] = "inline"; }
        }
      }
      if ($westart == 0) $westart++;
      if ($westart > 1) {
        $westart=-1;
        for ($jj=$tstart; $jj<$j; $jj++) { $wordd[$jj] = "inline"; }
      }
      if (strlen($rest) > 19) { 
       $wordd[$j] = "inline";
       $wordd[$j + 1] = "inline";
       if (isset($_GET['email'])) $kk=($j + 1);
      }
     } else {
      if ($westart > 0) $westart++;
      for ($jj=$j; $jj<20; $jj++) { if (strlen($words[$jj - 1]) == 0) { if (strlen($rest) > 19) { $wordd[$jj] = "none"; } } }
     }
    }
  }
  
  if ($kk >= 0) $words[$kk] = $ending;
  $emailee = "";
  $onload = "" +  ' onload=" setTimeout(andthen,1000); emailto(' + "''" + '); "';
  
  if (isset($_GET['submit']) || isset($_GET['submitemail'])) {
   $goes = $goes + 1;
  }
    
  if ((isset($_GET['submit']) || isset($_GET['submitemail'])) && $westart >= 0 && ((strlen($rest) - 19) - $roff) > 0) {
   $score = $score + (strlen($rest) - 19) - $roff;
   $emailee = $_GET['submitemail'];
   if (isset($_GET['submitemail'])) {
     if (strpos($emailee, "@") === false) $emailee = $ideatois;
     $onload = ' onload="  setTimeout(andthen,1000); emailto(' . "'" . $emailee  . "'" . '); "';
   }
  } 
  
  $showing = "";
  for ($j=0; $j<20; $j++) {
   if ($wordd[$j] != "none") $showing = "y";
  }
  
  if ($showing == "") {
   for ($j=0; $j<12; $j++) {
    $wordd[$j] = "inline";
   }
  }

 }
?>
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<title>My Rhopalic Letter Sentence Game - RJM Programming - January, 2015</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<script type='text/javascript'>

var thattd;
var thattdi;
var potentialscore=<?php echo strlen($rest) - 19; ?>;
var score=<?php echo $score; ?>;
var goes=<?php echo $goes; ?>;
var emailee='<?php echo $tois; ?>';

function ask(fromid) {
  if (fromid == "submitemail" && navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { 
   emailee = prompt("... and your email is going to?", "<?php echo $ideatois; ?>");
   if (emailee == null) emailee = "";
   if (emailee.indexOf("@") != -1) {
    document.getElementById(fromid).value = document.getElementById(fromid).value.replace(/ /g, '');
    document.getElementById('tois').value = emailee;
    document.getElementById('imya').src = './rhopalic_letter_sentence.php?to=' + encodeURIComponent(emailee) + '&subject=My%20Rhopalic%20Letter%20Sentence&body=' + encodeURIComponent(document.URL.replace("submitemail","email=y&submit").replace(/ /g, "%20"));
   }
   return true;
  } else if (fromid != "submitemail") {
  var ii;
  var upto = 1;
  var urlsofar = './rhopalic_letter_sentence.php';
  var thesentence = prompt("Please type in your rhopalic sentence to be validated.", "");
  if (thesentence == null) {
  upto = upto;
  } else {
  if (thesentence != '') {
    thesentence = thesentence.replace(/,/g, "");
    var ois = document.getElementById(fromid);
    if ((thesentence + '*') != (thesentence + '*').replace(".*","")) {
      urlsofar += '?w21=' + encodeURIComponent(".") + "&" + ois.name + "=" + ois.value;
      thesentence = (thesentence + '*').replace(".*","");
    } else if ((thesentence + '*') != (thesentence + '*').replace("!*","")) {
      urlsofar += '?w21=' + encodeURIComponent("!") + "&" + ois.name + "=" + ois.value;
      thesentence = (thesentence + '*').replace("!*","");
    } else if ((thesentence + '*') != (thesentence + '*').replace("?*","")) {
      urlsofar += '?w21=' + encodeURIComponent("?") + "&" + ois.name + "=" + ois.value;
      thesentence = (thesentence + '*').replace("?*","");
    } else {
      urlsofar += '?w21=' + encodeURIComponent(".") + "&" + ois.name + "=" + ois.value;
    }
    var wordsare = thesentence.split(" ");
    for (ii=1; ii<wordsare[0].length; ii++) {
      urlsofar += ("&w" + upto + "=");
      upto = upto + 1;
    }
    var rupto=upto;
    for (ii=wordsare[0].length; ii<=20; ii++) {
     if (eval(ii - rupto) < wordsare.length) {
      urlsofar += ("&w" + upto + "=" + wordsare[ii - rupto]);
      upto = upto + 1;
     } else {
      urlsofar += ("&w" + upto + "=");
      upto = upto + 1;
     }
    }
  }
  //var ourtois = emailee;
  //if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i) && ourtois == "") { 
  //  ourtois = prompt("... and your email is going to?", "");
  //}
  //if (ourtois == null) ourtois = "";
  location.href = urlsofar.replace("&w1=undefined&", "&w1="); // + "&tois=" + ourtois;
  return false;
  }
  return true;
  }
  return true;
}

function andthen() {
  if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { 
        document.getElementById('myh1').innerHTML = "My Rhopalic Letter English Sentence Game";
  }
}

function emailto(toemail) {
  andthen();
  if (toemail != '' || emailee != '') {
  if (emailee == '') emailee = toemail;
  if (emailee.indexOf('@') != -1) {
   if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { 
    //alert('./rhopalic_letter_sentence.php?to=' + encodeURIComponent(emailee) + '&subject=My%20Rhopalic%20Letter%20Sentence&body=' + encodeURIComponent(document.URL.replace("submitemail","email=y&submit")));
    document.getElementById('imya').src = './rhopalic_letter_sentence.php?to=' + encodeURIComponent(emailee) + '&subject=My%20Rhopalic%20Letter%20Sentence&body=' + encodeURIComponent(document.URL.replace("submitemail","email=y&submit"));
   } else {
    document.getElementById('mya').href = 'mail' + 'to:' + emailee + '?subject=My%20Rhopalic%20Letter%20Sentence&body=' + encodeURIComponent(document.URL.replace("submitemail","email=y&submit"));
   }
  } else if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i) && '<?php echo $tois; ?>' != '') {
    document.getElementById('imya').src = './rhopalic_letter_sentence.php?to=' + encodeURIComponent('<?php echo $tois; ?>') + '&subject=My%20Rhopalic%20Letter%20Sentence&body=' + encodeURIComponent(document.URL.replace("submitemail","email=y&submit"));
  } else {
   document.getElementById('mya').href = 'mail' + 'to:' + '' + '?subject=My%20Rhopalic%20Letter%20Sentence&body=' + encodeURIComponent(document.URL.replace("submitemail","email=y&submit"));
  }
  if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)) { 
    toemail = toemail;
  } else {
    document.getElementById('mya').click(); 
  }
  }
}


function check(thisi,thisid,thisvalue) {
  thattd = document.getElementById(thisid.replace('i',''));
  var ii=1;
  var mm=0;
  mm = eval(thisid.replace('i',''));
  ii=eval(ii + eval(thisid.replace('i','')));
  thattdi = document.getElementById(ii);
  if (thisvalue == '') {
    //thattd.innerHTML = '';
    thisi.value = '';
  } else if (thisvalue.length == mm) {
    //thattd.innerHTML = thisi.value;
    thisi.value = thisvalue;
    thattdi.style.display = 'inline';
  } else {
    thisi.value = '';
  }
}

</script>
<body style='background-color:teal;'<?php echo $onload; ?>>
<?php
 if (isset($_GET['email'])) {
   echo "
<h1 align='center'>Your Rhopalic Letter English Sentence Game</h1>
<h2 align='center' id='yourh2'>Score: " . $score . " / Goes: " . $goes . "</h2>
<div align='center' style='background-color:yellow;'>
<table id='yourtable'>
<tbody id='yourtbody'>
";
    for ($j=0; $j<20; $j++) {
      echo "\n<td id='x" . ($j + 1) . "' style='display:" . $wordd[$j] . ";'><input size=" . ($j + 1 + 1) . " id='xi" . ($j + 1) . "' value='" . $words[$j] . "'></input></td>";
    }
   echo "
</tbody>
</table>
</div>
<br><hr><br>
";
  $words = array("A", "go", "for", "them", "would", "really", "improve", "fairness", "standards", "constantly", "threatening", "cohesiveness", "", "", "", "", "", "", "", "");
  $wordd = array("inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "inline", "none", "none", "none", "none", "none", "none", "none", "inline");
  $ending = ".";
  
  $score = 0;
  $goes = 0;
  
  $isvalid = "y";
  $targeteq = " target='myi' ";

  echo "<iframe id='myi' name='myi' style='width:100%;height:400px;' src='./rhopalic_letter_sentence.php'></iframe><br><div align='center' style='display:none;'>";
 } else {
  echo "<div align='center'>";
 }

?>
<h1 id='myh1' align='center'>My <a target=_blank style='text-decoration:none;' title='rhopalic' href='http://en.wiktionary.org/wiki/rhopalic'>Rhopalic</a> Letter English Sentence Game</h1>
<h2 align='center' id='myh2'>Score: <?php echo $score; ?> / Goes: <?php echo $goes; ?></h2>
<div align='center'>
<form id='myform' method='GET' action='./rhopalic_letter_sentence.php'<?php echo $targeteq; ?>>
<input type='hidden' name='tois' id='tois' value='<?php echo $tois; ?>'></input>
<table id='mytable'>
<tbody id='mytbody'>
<tr>
<?php
    for ($j=0; $j<20; $j++) {
      echo "\n<td id='" . ($j + 1) . "' style='display:" . $wordd[$j] . ";'><input size=" . ($j + 1 + 1) . " name='w" . ($j + 1) . "' id='i" . ($j + 1) . "' onblur='check(this,this.id,this.value);' value='" . $words[$j] . "'></input></td>";
    }
?>
<td id='21'><select name='w21' id='i21'><option value='<?php echo $ending; ?>'><?php echo $ending; ?></option><option value='.'>.</option><option value='?'>?</option><option value='!'>!</option></select></td>
</tr>
</tbody>
</table>
<input type='hidden' value='<?php echo $score; ?>' id='score' name='score'></input><input type='hidden' value='<?php echo $goes; ?>' id='goes' name='goes'></input>
<br><input onclick='return ask(this.id);' type='submit' name='submit' id='submitsentence' value='Validate Sentence'></input>&nbsp;&nbsp;<input onclick='return ask(this.id);' type='submit' name='submitemail' id='submitemailsentence' value='Validate Sentence and Email'></input>&nbsp;&nbsp;<input type='submit' name='submit' id='submit' value='Validate'></input>&nbsp;&nbsp;<input onclick='return ask(this.id);' type='submit' name='submitemail' id='submitemail' value='Validate and Email'></input>&nbsp;&nbsp;<input type='button' id='reset' value='Reset' onclick='location.href="./rhopalic_letter_sentence.php?w1=&w2=&w3=&w4=&w5=&w6=&w7=&w8=&w9=&w10=&w11=&w12=&w13=&w14=&w15=&w16=&w17=&w18=&w19=&w20=&w21=.<?php echo '&score=' . $score . '&goes=' . $goes; ?>";'></input>
</form>
</div>
<a id='mya' title='Email' href='#' style='display:none;'>Email</a>
<iframe id='imya' title='Email' href='#' style='display:none;'>Email</iframe>
</div>
<script type='text/javascript'>
  setTimeout(andthen,1000);
</script>
</body>
</html>

