<?php
// say.php
// Relies on Mac OS X operating system underneath
// RJM Programming
// May, 2016
$title='';
$wordset=["","","","","","","","","",""];

$gleanblurb="Playing 500 bridge card game,";  // 0
$gleanblurb.=" you could follow suit,"; // 1
$gleanblurb.=" 51 with bid 1 clubs,"; // 2
$gleanblurb.=" jack of spades,"; // 3
$gleanblurb.=" queen of hearts,"; // 4
$gleanblurb.=" king of diamonds,"; // 5
$gleanblurb.=" ace of clubs,"; // 6
$gleanblurb.=" 41 passed,"; // 7
$gleanblurb.=" 31 doubled,"; // 8
$gleanblurb.=" 21 redoubled,"; // 9
$gleanblurb.=" is incorrect,"; // 10
$gleanblurb.=" and we show leading bidder partner cards to everyone,"; // 11
$gleanblurb.=" and plays 2 to start trick 9,"; // 12
$gleanblurb.=" then play 91 of clubs,"; // 13
$gleanblurb.=" so score is 0 0 0 0,"; // 14
$gleanblurb.=" game score is 456,"; // 15
$gleanblurb.=" 7 of clubs,"; // 16
$gleanblurb.=" 7 of spades,"; // 17
$gleanblurb.=" 7 of hearts,"; // 18
$gleanblurb.=" 7 of diamonds,"; // 19
$gleanblurb.=" joker,"; // 20
$gleanblurb.=" bridge card game,"; // 21
$gleanblurb.=" card game,"; // 22
$gleanblurb.=" in trick 5,"; // 23
$gleanblurb.=" team score is 8 0"; // 24

$transblurb='';
$contis='';
$transbits=[];
$people=["","","","","","","","","",""];
$ipeople=1;

   
function maybevoice($mvin) {
  global $transblurb, $gleanblurb, $contis, $transbits, $people;
  $sayvoices=["Alex;en_US", 
"Alice;it_IT", 
"Alva;sv_SE", 
"Amelie;fr_CA", 
"Anna;de_DE", 
"Carmit;he_IL", 
"Damayanti;id_ID", 
"Daniel;en_GB", 
"Diego;es_AR", 
"Ellen;nl_BE", 
"Fiona;en_scotland", 
"Fred;en_US", 
"Ioana;ro_RO", 
"Joana;pt_PT", 
"Jorge;es_ES", 
"Juan;es_MX", 
"Kanya;th_TH", 
"Karen;en_AU", 
"Kyoko;ja_JP", 
"Laura;sk_SK", 
"Lekha;hi_IN", 
"Luca;it_IT", 
"Luciana;pt_BR", 
"Maged;ar_SA", 
"Mariska;hu_HU", 
"Mei-Jia;zh_TW", 
"Melina;el_GR", 
"Milena;ru_RU", 
"Moira;en_IE", 
"Monica;es_ES", 
"Nora;nb_NO", 
"Paulina;es_MX", 
"Samantha;en_US", 
"Sara;da_DK", 
"Satu;fi_FI", 
"Sin-ji;zh_HK", 
"Tessa;en_ZA", 
"Thomas;fr_FR", 
"Ting-Ting;zh_CN", 
"Veena;en_IN", 
"Victoria;en_US", 
"Xander;nl_NL", 
"Yelda;tr_TR", 
"Yuna;ko_KR", 
"Yuri;ru_RU", 
"Zosia;pl_PL", 
"Zuzana;cs_CZ"
];

  $lcode='';
  $found=false;
  $mvins=explode("_",trim($mvin));
  if ($mvins[0] == "-v" && sizeof($mvins) > 1) {
    $voice=$mvins[1];
    for ($ilx=0; $ilx<sizeof($sayvoices); $ilx++) {
     if (substr($sayvoices[$ilx],0,strlen(($voice . ';'))) == ($voice . ';')) {
       $lcode=explode('_', explode(';', $sayvoices[$ilx])[1])[0];
       if (strpos($contis, '$' . $lcode . '="') !== false) {
         $transblurb=explode('"', explode('$' . $lcode . '="', $contis)[1])[0];
         $transbits=explode(',', $transblurb);
         if (sizeof($transbits) >= 23) {
           $found=true;
         }
       }
     }
    }
    if (!$found) {
     $tvmm=file_get_contents('http://api.mymemory.translated.net/get?q=' . str_replace('+','%20',urlencode($gleanblurb)) . '&langpair=en|' . $lcode);
     //$tvmn="Giocando a 500 carte bridge, potresti seguire il seme, 51 con dichiarazione di 1 fiori, jack di picche, regina di cuori, re di quadri, asso di fiori, 41 passati, 31 raddoppiati, 21 raddoppiati, non è corretto e mostriamo il miglior offerente carte partner a tutti e gioca 2 per iniziare la presa 9, quindi gioca 91 di fiori, quindi il punteggio è 0 0 0 0, il punteggio del gioco è 456, 7 di fiori, 7 di picche, 7 di cuori, 7 di quadri, jolly, gioco di carte bridge, gioco di carte, nella presa 5, il punteggio della squadra è 8 0";
     //$transblurb=$tvmn;
     if (strpos($tvmm, '"translatedText":"') !== false || $transblurb != '') {
         if ($transblurb == '') {
         $transblurb=explode('"', explode('"translatedText":"', $tvmm)[1])[0];
         } 
         $transbits=explode(',', $transblurb);
         if (sizeof($transbits) >= 23) {
           $found=true;
           file_put_contents('./sayspare.php', $contis);
           file_put_contents('./say.php', str_replace('?' . '>', "\n $" . $lcode . '="' . $transblurb . '";' . "\n" . '?' . '>', $contis));
         }
     }
    }
    $fromb=str_replace("_"," ",str_replace("-v_" . $mvins[1] . "_","",trim($mvin)));
    $frombs=explode(",", $fromb);
    if ($found) {
     // Playing 500 or bridge card game
     $pbcg=trim(str_replace(" 500 "," ",$transbits[0]));
     $pg=trim(str_replace($transbits[21],$transbits[22],$transbits[0]));
     if (strpos($fromb, "In your bridge card game") !== false) {
       $pg=$pbcg;
     }
     $intrick=trim(str_replace("23","",$transbits[5]));
     $passedword=trim(str_replace("41","",$transbits[7]));
     $doubleword=trim(str_replace("31","",$transbits[8]));
     $redoubleword=trim(str_replace("21","",$transbits[9]));
     $bidword="with bid";
     $tostarttrick="to start trick";
     $andword="and";
     $andweshow=trim($transbits[11]);
     
     $ofclubs=trim(str_replace("7","",$transbits[16]));
     $ofspades=trim(str_replace("7","",$transbits[17]));
     $ofhearts=trim(str_replace("7","",$transbits[18]));
     $ofdiamonds=trim(str_replace("7","",$transbits[19]));
     $joker=trim($transbits[20]);
     $jack=str_replace($ofspades,"",$transbits[3]);
     $queen=str_replace($ofhearts,"",$transbits[4]);
     $king=str_replace($ofdiamonds,"",$transbits[5]);
     $ace=str_replace($ofclubs,"",$transbits[6]);
     
     if (strpos($transbits[2], "51") !== false && strpos($transbits[2], " 1") !== false) {
       if (strpos($transbits[2], "51") < strpos($transbits[2], " 1")) {
         $bidword=trim(explode(" 1", explode("51", $transbits[2])[1])[0]);
       } else {
         $bidword=trim(explode("51", explode(" 1", $transbits[2])[1])[0]);
       }
     }
     if (strpos($transbits[12], " 2") !== false && strpos($transbits[12], " 9") !== false) {
       $andword=explode(" ",trim($transbits[12]))[0];
       if (strpos($transbits[12], " 2") < strpos($transbits[12], " 9")) {
         $tostarttrick=trim(explode(" 9", explode(" 2", $transbits[12])[1])[0]);
       } else {
         $tostarttrick=trim(explode(" 2", explode(" 9", $transbits[12])[1])[0]);
       }
     }
     
     
     if (strpos($fromb, ' follow suit') !== false) {
      if (sizeof($frombs) > 1) {
      $fromb=$frombs[1] . ', ' . $transbits[1];
      } else {
      $fromb=$people[0] . ', ' . $transbits[1];
      }
     } else if (strpos($fromb, 'incorrect') !== false) {
      if (sizeof($frombs) > 1) {
      $fromb=$frombs[1] . ', ' . $transbits[10];
      } else {
      $fromb=$people[0] . ', ' . $transbits[10];
      }
     }
     // In your bridge card game with final bid 1 Diamonds by Player 2 Player 2 started trick 1 with 2 of Diamonds
     // In your bridge card game with final bid 1 Diamonds by Player 2 Player 2 started trick 1 with 2 of Diamonds and Player 3 started trick 1 with 3 of Spades
     // Congratulations, Player Player 2, you win Trick 1 () lead off by Player Player 2 and Trumps Diamonds), and lead off, as applicable, for the next trick.  Scores are 0.0,1.0,0.0,0.0
     // In your bridge card game with final bid 1 Diamonds by Player 2 Congratulations, Player 2, you win Trick 1  after Player 2 started trick 1 with 2 of Diamonds and Player 3 started trick 1 with 3 of Spades and Player 4 started trick 1 with 5 of Spades and Player 1 started trick 1 with King of Hearts making trick scores 0 1 0 0 game score 0 0 0 0 and team scores 0 0
     
    }
    return "-v " . $voice . " \"" . $fromb . "\"";
  } else {
    return $mvin;
  }
}

if (isset($_GET['macbeth'])) {
 $wordset[0]="When shall we three meet again. In thunder, lightning, or in rain?";
 $wordset[1]="When the hurlyburly's done. When the battle's lost and won.";
 $wordset[2]="That will be ere the set of sun.";
 $wordset[3]="Where the place?";
 $wordset[4]="Upon the heath.";
 $wordset[5]="There to meet with Macbeth.";
 $wordset[6]="I come, Graymalkin!";
 $wordset[7]="Paddock calls.";
 $wordset[8]="Anon.";
 $wordset[9]="Fair is foul, and foul is fair.  Hover through the fog and filthy air.";
}

while (isset($_GET['n' . $ipeople])) {
  $people[-1 + $ipeople]=str_replace('+',' ',urldecode($_GET['n' . $ipeople]));
  $ipeople++;
}

if (isset($_GET['translate'])) {
  if (strlen(('' . $_GET['translate'])) == 2 && ('' . $_GET['translate']) == strtolower(('' . $_GET['translate']))) {
     $lcode=('' . $_GET['translate']);
     $contis=file_get_contents('./say.php');
     //file_put_contents("xxx.xxx", 'http://api.mymemory.translated.net/get?q=' . str_replace('+','%20',urlencode($gleanblurb)) . '&langpair=en|' . $lcode);
     $xtvmm=file_get_contents('http://api.mymemory.translated.net/get?q=' . str_replace('+','%20',urlencode($gleanblurb)) . '&langpair=en|' . $lcode);
     //$xtvmn="Giocando a 500 carte bridge, potresti seguire il seme, 51 con dichiarazione di 1 fiori, jack di picche, regina di cuori, re di quadri, asso di fiori, 41 passati, 31 raddoppiati, 21 raddoppiati, non è corretto e mostriamo il miglior offerente carte partner a tutti e gioca 2 per iniziare la presa 9, quindi gioca 91 di fiori, quindi il punteggio è 0 0 0 0, il punteggio del gioco è 456, 7 di fiori, 7 di picche, 7 di cuori, 7 di quadri, jolly, gioco di carte bridge, gioco di carte, nella presa 5, il punteggio della squadra è 8 0";
     //file_put_contents("xx.xx", $xtvmm);
     //$transblurb=$xtvmn;
     if (strpos($xtvmm, '"translatedText":"') !== false || $transblurb != '') {
         if ($transblurb == '') {
         $transblurb=explode('"', explode('"translatedText":"', $xtvmm)[1])[0];
         }
         $transbits=explode(',', $transblurb);
         if (sizeof($transbits) == 12) {
           file_put_contents('./sayspare.php', $contis);
           file_put_contents('./say.php', str_replace('?' . '>', "\n $" . $lcode . '="' . $transblurb . '";' . "\n" . '?' . '>', $contis));
         }
     }
  }
}

if (isset($_GET['say'])) {
  if ($_GET['say'] == "") {
    exec("say hello world");
    $title="Just said ... hello world";
  } else {
    $cexec='';
    $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say']))) . ";");
    $title="Just said ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say'])));

    if (isset($_GET['say2'])) {
     $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say2']))) . ";");
     $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say2'])));
     if (isset($_GET['say3'])) {
      $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say3']))) . ";");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say3'])));
     if (isset($_GET['say4'])) {
      $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say4']))) . ";");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say4'])));
     if (isset($_GET['say5'])) {
      $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say5']))) . ";");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say5'])));
     if (isset($_GET['say6'])) {
      $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say6']))) . ";");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say6'])));
     if (isset($_GET['say7'])) {
      $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say7']))) . ";");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say7'])));
     if (isset($_GET['say8'])) {
      $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say8']))) . ";");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say8'])));
     if (isset($_GET['say9'])) {
      $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say9']))) . ";");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say9'])));
     if (isset($_GET['say10'])) {
      $cexec.=("say " . maybevoice(str_replace(" ","_",urldecode($_GET['say10']))) . ";");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_GET['say10'])));
     }
     }
     }
     }
     }
     }
     }
     }
    }
    //echo $cexec;
    //exit;
    if ($cexec != '') exec($cexec);


  }
} else if (isset($_POST['say'])) {
  if ($_POST['say'] == "") {
    exec("say hello world");
    $title="Just said ... hello world";
  } else {
    exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say']))) . "");
    $title="Just said ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say'])));
    if (isset($_POST['say2'])) {
     exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say2']))) . "");
     $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say2'])));
     if (isset($_POST['say3'])) {
      exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say3']))) . "");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say3'])));
     if (isset($_POST['say4'])) {
      exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say4']))) . "");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say4'])));
     if (isset($_POST['say5'])) {
      exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say5']))) . "");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say5'])));
     if (isset($_POST['say6'])) {
      exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say6']))) . "");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say6'])));
     if (isset($_POST['say7'])) {
      exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say7']))) . "");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say7'])));
     if (isset($_POST['say8'])) {
      exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say8']))) . "");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say8'])));
     if (isset($_POST['say9'])) {
      exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say9']))) . "");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say9'])));
     if (isset($_POST['say10'])) {
      exec("say " . maybevoice(str_replace(" ","_",urldecode($_POST['say10']))) . "");
      $title.=" ... and ... " . maybevoice(str_replace(" ","_",urldecode($_POST['say10'])));
     }
     }
     }
     }
     }
     }
     }
     }
    }
  }
} 

if (1 == 1) {
  echo "
<html>
<head>
<title>" . $title . "</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<script type='text/javascript'>

</script>
</head>
<body style='background-color:orange;'>
<h1 style='text-align:center; width:100%;'>Text to English Speech via MAMP Web Server</h1>
<div style='text-align:center; width:100%;'>
<form style='text-align:center; width:100%;' action='./say.php' method='POST'>
What You Want to Hear: <input style='width:80%;' name='say' id='say' type='text' value=\"" . $wordset[0] . "\"></input><br>
And Then: <input style='width:80%;' name='say2' id='say2' type='text' value=\"" . $wordset[1] . "\"></input><br>
And Back to You: <input style='width:80%;' name='say3' id='say3' type='text' value=\"" . $wordset[2] . "\"></input><br>
More: <input style='width:80%;' name='say4' id='say4' type='text' value=\"" . $wordset[3] . "\"></input><br>
More: <input style='width:80%;' name='say5' id='say5' type='text' value=\"" . $wordset[4] . "\"></input><br>
More: <input style='width:80%;' name='say6' id='say6' type='text' value=\"" . $wordset[5] . "\"></input><br>
More: <input style='width:80%;' name='say7' id='say7' type='text' value=\"" . $wordset[6] . "\"></input><br>
More: <input style='width:80%;' name='say8' id='say8' type='text' value=\"" . $wordset[7] . "\"></input><br>
More: <input style='width:80%;' name='say9' id='say9' type='text' value=\"" . $wordset[8] . "\"></input><br>
More: <input style='width:80%;' name='say10' id='say10' type='text' value=\"" . $wordset[9] . "\"></input><br><br><input type='submit' value='Hear This'></input>
</form>
</div>
</body>
</html> 
";
}

 $it="Giocando a 500 carte bridge, potresti seguire il seme, 51 con dichiarazione di 1 fiori, jack di picche, regina di cuori, re di quadri, asso di fiori, 41 passati, 31 raddoppiati, 21 raddoppiati, non è corretto e mostriamo il miglior offerente carte partner a tutti e gioca 2 per iniziare la presa 9, quindi gioca 91 di fiori, quindi il punteggio è 0 0 0 0, il punteggio del gioco è 456, 7 di fiori, 7 di picche, 7 di cuori, 7 di quadri, jolly, gioco di carte bridge, gioco di carte, nella presa 5, il punteggio della squadra è 8 0";
?>
