<?php

// Yahoo Yql Google for Two Translate Tutorial - RJM Programming - 2014

function unicode2html($str) {  // thanks to http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha
    $i=65535;
    while($i>0){
        $hex=dechex($i);
        if (strlen($hex) == 3) {
        $str=str_replace("\u0$hex","&#$i;",$str);
        } else if (strlen($hex) == 2) {
        $str=str_replace("\u00$hex","&#$i;",$str);
        } else if (strlen($hex) == 1) {
        $str=str_replace("\u000$hex","&#$i;",$str);
        } else {
        $str=str_replace("\u$hex","&#$i;",$str);
        }
        $i--;
     }
     return $str;
}

if (isset($_GET['justaaa'])) file_put_contents("aaa.aaa", $_GET['justaaa']);

$ta11 = "";
$language1 = "en";
$x = "";
$y = "";
if (isset($_POST['ta11'])) $ta11 = str_replace(" ","%20",$_POST['ta11']);
if (isset($_GET['ta11'])) $ta11 = str_replace(" ","%20",$_GET['ta11']);
if (isset($_POST['language1'])) $x = "language2";
if (isset($_GET['language1'])) $x = "language2";
if (isset($_POST['language1'])) $language1 = str_replace(" ","%20",$_POST['language1']);
if (isset($_GET['language1'])) $language1 = str_replace(" ","%20",$_GET['language1']);
if (isset($_POST['ta22'])) $ta11 = str_replace(" ","%20",$_POST['ta22']);
if (isset($_GET['ta22'])) $ta11 = str_replace(" ","%20",$_GET['ta22']);
if (isset($_POST['language2'])) $language1 = str_replace(" ","%20",$_POST['language2']);
if (isset($_GET['language2'])) $language1 = str_replace(" ","%20",$_GET['language2']);



$bcolours = array("white", "pink", "aqua", "lime", "silver", "lightyellow", "cyan", "fuchsia", "orange", "lightblue", "lightgreen", "lightgray");

if ($ta11 != "") {
 $pageContent = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20google.translate%20where%20q%3D%22" . $ta11 . "%22%20and%20target%3D%22" . $language1 . "%22%3B&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");

//if (strpos($pageContent, ',"error":"Invalid JSON document') !== false) {
if (strpos($pageContent, 'Invalid JSON document') !== false) {

  if ($language1 != '') {
    if ($language1 != 'en') file_put_contents("aaa.aaa", $language1);
  } else if ($language2 != '') {
    $language1=$language2;
  } else if (file_exists("aaa.aaa")) {
    $language1=file_get_contents("aaa.aaa");
  }
  
  if ($language1 == 'en') {
  if (file_exists("aaa.aaa")) {
  $pageContent = file_get_contents("http://api.mymemory.translated.net/get?q=" . urlencode(urldecode($ta11)) . "&langpair=" . file_get_contents("aaa.aaa") . "|en");
  } else {
  $pageContent = '{"translatedText":"translation":"' . urldecode($ta11) . '"';
  }
  } else {
  $pageContent = file_get_contents("http://api.mymemory.translated.net/get?q=" . urlencode(urldecode($ta11)) . "&langpair=en|" . $language1);
  }
  
  $one=1;
  $done=false;
  
//file_put_contents("bb.bb", "lone=" . $language1 . " ltwo=" . $language2 . " other=" . file_get_contents("aaa.aaa") . " http://api.mymemory.translated.net/get?q=" . urlencode(urldecode($ta11)) . "&langpair=en|" . $language1); 
  
  // https://translate.google.com/#fr/es/bonjour
  // http://api.mymemory.translated.net/get?q=hello&langpair=en|it
  // echo "https://translate.google.com/#" .  substr($languag2,0,2)  . "/" . substr($language,0,2) . "/" . urldecode(urldecode($selnam2));
  //$pageContent = file_get_contents("http://api.mymemory.translated.net/get?q=" . urlencode(urldecode($ta11)) . "&langpair=en|" . $language1);
  //echo "http://api.mymemory.translated.net/get?q=" . urldecode(urldecode($selnam2)) . "&langpair=" . substr($languag2,0,2)  . "/" . substr($language,0,2);
  //exit;
  //$trans=explode(',"translation":"', $pageContent);
  $pretrans=explode('{"translatedText":"', $pageContent);
  if (sizeof($pretrans) > 1) {
    $trans=explode('"translation":"', $pretrans[1]);
  if (sizeof($trans) > 1) {
    for ($j=1; $j<sizeof($trans); $j++) {
    if (!$done) {
    $wtrans=explode('"', $trans[$j]);
    if (substr($wtrans[0],0,1) >= 'A' && substr($wtrans[0],0,1) <= 'Z') {
    if ($one == 1 && $j == 1) $one=-1;
    } else if ($one <= 1) {
    $one=$j;
    $done=true;
    }
    }
    }
    $wtrans=explode('"', $trans[abs($one)]);
  } else {
    $wtrans=explode('"', $pretrans[1]);
  }
    if ($wtrans[0] == 'AP2') $wtrans[0] = urldecode($ta11);
    
    if ($x != "") {
      echo "<html><body onload=' var sl=parent.document.getElementById(\"" . $x . "\"); if (sl != null) { if (sl.value == \"\") { sl.value=\"" . $ta11 . "\";  }  }  '>" . unicode2html($wtrans[0]) . "</body></html>";
    } else {
      echo "<html><body>" . unicode2html($wtrans[0]) . "</body></html>";
    }
  }
} else {


 $json_output = json_decode($pageContent);  
  
 // Confirm that results were returned before parsing
 if (!is_null($json_output->query->results)) {
    if ($x != "") {
      $y = $json_output->query->results->json->src;
      echo "<html><body onload=' var sl=parent.document.getElementById(\"" . $x . "\"); if (sl != null) { if (sl.value == \"\") { sl.value=\"" . $y . "\";  }  }  '>" . $json_output->query->results->json->sentences->trans . "</body></html>";
    } else {
      echo "<html><body>" . $json_output->query->results->json->sentences->trans . "</body></html>";
    }
 }
}

} else if (isset($_POST['preta11']) || isset($_GET['preta11'])) {
str_replace("\\\"", "\"", 
 $cont = file_get_contents(str_replace("/PHP/YQL/GoogleTranslate/PHP/YQL/GoogleTranslate/", "/PHP/YQL/GoogleTranslate/", dirname(__FILE__) . str_replace(".php", ".html", str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI'])))));
 //$cont = str_replace ('\\', '', $cont);
 
 $uis = "http://" . str_replace("/PHP/YQL/GoogleTranslate/PHP/YQL/GoogleTranslate/", "/PHP/YQL/GoogleTranslate/", dirname(__FILE__) . str_replace(".php", ".html", str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI'])));
 if (isset($_POST['preta11'])) {
   $cont = str_replace('<td id="preta11" align="center"><', '<td id="preta11" align="center">' . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_POST['preta11'])))) . '<', $cont);
 }
 if (isset($_POST['preta12'])) {
   $cont = str_replace('<td id="preta12" align="center"><', '<td id="preta12" align="center">' . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_POST['preta12'])))) . '<', $cont);
 }
 if (isset($_POST['preta21'])) {
   $cont = str_replace('<td id="preta21" align="center"><', '<td id="preta21" align="center">' . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_POST['preta21'])))) . '<', $cont);
 }
 if (isset($_POST['preta22'])) {
   $cont = str_replace('<td id="preta22" align="center"><', '<td id="preta22" align="center">' . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_POST['preta22'])))) . '<', $cont);
 }
 if (isset($_GET['preta11'])) {
   $cont = str_replace('<td id="preta11" align="center"><', '<td id="preta11" align="center">' . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_GET['preta11'])))) . '<', $cont);
 }
 if (isset($_GET['preta12'])) {
   $cont = str_replace('<td id="preta12" align="center"><', '<td id="preta12" align="center">' . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_GET['preta12'])))) . '<', $cont);
 }
 if (isset($_GET['preta21'])) {
   $cont = str_replace('<td id="preta21" align="center"><', '<td id="preta21" align="center">' . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_GET['preta21'])))) . '<', $cont);
 }
 if (isset($_GET['preta22'])) {
   $cont = str_replace('<td id="preta22" align="center"><', '<td id="preta22" align="center">' . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_GET['preta22'])))) . '<', $cont);
 }



 if (isset($_POST['preta11'])) {
   $cont = str_replace("<td align='center' id='preta11'><", "<td id='preta11' align='center'>" . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_POST['preta11'])))) . '<', $cont);
 }
 if (isset($_POST['preta12'])) {
   $cont = str_replace("<td align='center' id='preta12'><", "<td id='preta12' align='center'>" . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_POST['preta12'])))) . '<', $cont);
 }
 if (isset($_POST['preta21'])) {
   $cont = str_replace("<td align='center' id='preta21'><", "<td id='preta21' align='center'>" . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_POST['preta21'])))) . '<', $cont);
 }
 if (isset($_POST['preta22'])) {
   $cont = str_replace("<td align='center' id='preta22'><", "<td id='preta22' align='center'>" . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_POST['preta22'])))) . '<', $cont);
 }
 if (isset($_GET['preta11'])) {
   $cont = str_replace("<td align='center' id='preta11'><", "<td id='preta11' align='center'>" . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_GET['preta11'])))) . '<', $cont);
 }
 if (isset($_GET['preta12'])) {
   $cont = str_replace("<td align='center' id='preta12'><", "<td id='preta12' align='center'>" . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_GET['preta12'])))) . '<', $cont);
 }
 if (isset($_GET['preta21'])) {
   $cont = str_replace("<td align='center' id='preta21'><", "<td id='preta21' align='center'>" . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_GET['preta21'])))) . '<', $cont);
 }
 if (isset($_GET['preta22'])) {
   $cont = str_replace("<td align='center' id='preta22'><", "<td id='preta22' align='center'>" . str_replace("&lt;","<", str_replace("&gt;",">", str_replace('\\', '', urldecode($_GET['preta22'])))) . '<', $cont);
 }


 if (isset($_GET['prelanguage1'])) {
   $cont = str_replace("''.replace('TjunkL','')", "'" . $_GET['prelanguage1'] . "'.replace('TjunkL','')", $cont);
 }

 if (isset($_GET['prelanguage2'])) {
   $cont = str_replace("''.replace('SjunkL','')", "'" . $_GET['prelanguage2'] . "'.replace('SjunkL','')", $cont);
 }

 if (isset($_POST['prelanguage1'])) {
   $cont = str_replace("''.replace('TjunkL','')", "'" . $_POST['prelanguage1'] . "'.replace('TjunkL','')", $cont);
 }

 if (isset($_POST['prelanguage2'])) {
   $cont = str_replace("''.replace('SjunkL','')", "'" . $_POST['prelanguage2'] . "'.replace('SjunkL','')", $cont);
 }


 if (isset($_GET['nextfocus'])) {
   if ($_GET['nextfocus'] == "ta22") {
     $cont = str_replace("lightgreen;border:3px yellow solid;", "lightgreen;", $cont);
     $cont = str_replace("lightblue;", "lightblue;border:3px yellow solid;", $cont);
   }
 }

 if (isset($_POST['nextfocus'])) {
   if ($_POST['nextfocus'] == "ta22") {
     $cont = str_replace("lightgreen;border:3px yellow solid;", "lightgreen;", $cont);
     $cont = str_replace("lightblue;", "lightblue;border:3px yellow solid;", $cont);
   }
 }

 if (isset($_GET['icols'])) {
     $cont = str_replace("var icols=0;", "var icols=" . $_GET['icols'] . ";", $cont);
 }

 if (isset($_POST['icols'])) {
     $cont = str_replace("var icols=0;", "var icols=" . $_POST['icols'] . ";", $cont);
 }

 date_default_timezone_set('Australia/Perth');
 $dsuffix = "_" . date('Ymdhis');
 if (strlen(str_replace("rjmprogramming.com.au", "", $_SERVER['SERVER_NAME'])) != strlen($_SERVER['SERVER_NAME'])) {
 $uis = "http://" . str_replace("/PHP/YQL/GoogleTranslate/PHP/YQL/GoogleTranslate/", "/PHP/YQL/GoogleTranslate/", str_replace("//", "/", $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/PHP/YQL/GoogleTranslate/" . str_replace("/PHP/YQL/GoogleTranslate/", "", str_replace(".php", $dsuffix . ".html", str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI'])))));
 } else {
 $uis = "http://" . str_replace("//", "/", $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/" . str_replace("/PHP/YQL/GoogleTranslate/", "", str_replace(".php", $dsuffix . ".html", str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']))));
 }
 
 $fis = str_replace("/PHP/YQL/GoogleTranslate/PHP/YQL/GoogleTranslate/", "/PHP/YQL/GoogleTranslate/", dirname(__FILE__) . str_replace(".php", $dsuffix . ".html", str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI'])));
 
 file_put_contents($fis, $cont);
 
 $cont = str_replace("</body>", "<a id='email' href='mailto:please.change@email.address?subject=Email%20Saved%20Conversation%20Translation%20session%20...%20" . $uis . "&body=" . $uis . "' title='Email Saved Conversation' style='display:none;'>Email Saved Conversation</a></body>", $cont);
 $cont = str_replace("function justchecking() {", "function justchecking() { document.getElementById('email').click(); ", $cont);

 echo $cont;
}
?> 

