<?php
// fillin_gaps_with_emojis.php
// RJM Programming
// June, 2021

 $objphp='';
 $emlist='';
 $intext='';

 if (file_exists('./fillin_gaps_with_emojis.tt')) {
   $objphp=file_get_contents('./fillin_gaps_with_emojis.tt');
 } else if (file_exists('./fillin_gaps_with_emojis.txt')) {
   $objphp=file_get_contents('./fillin_gaps_with_emojis.txt');
 } else if (!file_exists('./fillin_gaps_with_emojis.txt')) {
   $gcx=['objects', 'people', 'nature', 'food-drink', 'activity', 'travel-places', 'symbols', 'flags'];
   for ($jb=0; $jb<sizeof($gcx); $jb++) {
     $objphp.=file_get_contents('http://emojipedia.org/' . $gcx[$jb] . '/');
   }
   file_put_contents('./fillin_gaps_with_emojis.txt', $objphp);
 }
 $rot="";
 if (isset($_POST['rotate']) || isset($_GET['rotate'])) {
 if (isset($_POST['rotate'])) {
   $rot=('' . str_replace('+',' ',urldecode($_POST['rotate'])) . '');
 } else {
   $rot=('' . str_replace('+',' ',urldecode($_GET['rotate'])) . '');
 }
 }
 if (isset($_POST['intext']) || isset($_GET['intext'])) {
 if (isset($_POST['intext'])) {
   $intext=' ' . str_replace('+',' ',urldecode($_POST['intext'])) . ' ';
 } else {
   $intext=' ' . str_replace('+',' ',urldecode($_GET['intext'])) . ' ';
 }
 $phpsc='HTTP://www.rjmprogramming.com.au/PHP/short_codes.php';
 if (strpos($_SERVER['SERVER_NAME'], "localhost") !== false) {
   $phpsc=str_replace(':80/', '/', 'http://localhost:' . $_SERVER['SERVER_PORT'] . '/short_codes.php');
   //echo $phpsc;
   //exit;
 }
 $outtxt=file_get_contents($phpsc . '?intext=' . urlencode($intext));
 if ($outtxt != "") { //file_exists($phpsc)) { //'../PHP/short_codes/php')) {
  if ($intext != '') {
   $outtext=$intext;
   if (intval($rot) < 90) {
   $xp="60";
   $yp="20";
   } else if (intval($rot) < 180) {
   $xp="80";
   $yp="60";
   } else if (intval($rot) < 270) {
   $xp="60";
   $yp="80";
   } else {
   $xp="40";
   $yp="50";
   }
   if ($outtxt == "") { $outtxt=file_get_contents($phpsc . '?intext=' . urlencode($intext)); }
   if (strpos($outtxt, '</body>') !== false) { 
   $bod='<body' . explode('>', explode('<body', $outtxt)[1])[0] . '>';
   $outtext=str_replace('<p>', '', str_replace('</p>', '', explode($bod, explode('</body>', $outtxt)[0])[1]));
   if ($outtext != $intext) {
   if ($rot != "" && $rot != "0") {
   //if (strlen($rot) === 3) {  $xp=80";   }
   $oneis=explode('.', ('' . sin(deg2rad($rot)) * 100.0))[0];
   $twois=explode('.', ('' . cos(deg2rad($rot)) * 100.0))[0];
   $threeis=explode('.', ('' . (0.0 - sin(deg2rad($rot))) * 100.0))[0];
   $fouris=explode('.', ('' . (0.0 - cos(deg2rad($rot))) * 100.0))[0];
   //echo "<html><head><title>Emoji-ize Text - RJM Programming - June, 2021</title><meta charset='UTF-8'></head><body style='width:100%;'><p>" . str_replace("\n","<br>",str_replace("$@!"," ",trim($outtext))) . "</p></body></html>";
   echo "<html><head><title>Emoji-ize Text - RJM Programming - June, 2021</title><meta charset='UTF-8'><script type='text/javascript'>  
   
function getTopLeft(width, height, theta ) { // thanks to https://board.flashkit.com/board/showthread.php?775357-Find-four-points-of-rotated-rectangle
	// half width and height directed appropriately (for top left)
	var hw = -width / 2;
	var hh = -height / 2;
	var xcos = Math.cos( theta );
	var xsin = Math.sin( theta );
	//alert('hw=' + hw + ' height=' + height + ' theta=' + theta + ' ' + ('' + eval(-1 * eval(hw * xsin + hh * xcos ))).split('.')[0] + '.' + ('' + eval(1 * eval(hw * xcos - hh * xsin))).split('.')[0]);
	//return ('' + eval(-1 * eval(hw * xcos - hh * xsin))).split('.')[0] + '.' + ('' + eval(-1 * eval(hw * xsin + hh * xcos ))).split('.')[0];
	//return ('' + eval(-1 * eval(hw * xsin + hh * xcos ))).split('.')[0] + '.' + ('' + eval(1 * eval(hw * xcos - hh * xsin))).split('.')[0];
	return ('' + eval(hw * xcos - hh * xsin)).split('.')[0] + '.' + ('' + eval(hw * xsin + hh * xcos )).split('.')[0];
}
   
    if (window.parent) {  if (parent.document.getElementById('divinvis')) {  var tts='' + parent.document.getElementById('divinvis').getBoundingClientRect().width, tst='' + parent.document.getElementById('divinvis').getBoundingClientRect().height, ttis=getTopLeft(parent.document.getElementById('divinvis').getBoundingClientRect().width,parent.document.getElementById('divinvis').getBoundingClientRect().height," . deg2rad($rot) . "); parent.document.getElementById('divinvis').style.position='fixed'; parent.document.getElementById('divinvis').style.top='" . $yp . "%'; parent.document.getElementById('divinvis').style.left='" . $xp . "%';  if (1 == 4) { parent.document.getElementById('divinvis').style.transformOrigin='center center'; } var xxx='translate(' + eval(('' + ttis).split('.')[0]) + ',' + eval(('' + ttis).split('.')[1]) + ') ';  if (1 == 1) { xxx=''; } parent.document.getElementById('divinvis').style.transform=xxx + ' rotate(" . $rot . "deg) ' + ''; parent.document.getElementById('divinvis').style.opacity='0.5';   }  } </script></head><body style='width:100%;'><p>" . str_replace("\n","<br>",str_replace("$@!"," ",trim($outtext))) . "</p></body></html>";
   } else {
   echo "<html><head><title>Emoji-ize Text - RJM Programming - June, 2021</title><meta charset='UTF-8'></head><body style='width:100%;'><p>" . str_replace("\n","<br>",str_replace("$@!"," ",trim($outtext))) . "</p></body></html>";
   }
   exit;
   }
   }
  }
 }
 $endspan="</span> ";
 $enda="</a>";
 $ea=">";
 $za="<";
 $wastext=$intext;
 if (strpos($objphp, $enda) === false && strpos($objphp, ':') !== false) {
   $enda='~';
   $endspan='|';
   $ea="~";
   $za="|";
   $emarrpre=explode($ea, $objphp);
   $objphp=$ea;
   $emarr=[];
   $emarrlens=[];
   for ($ib=0; $ib<sizeof($emarrpre); $ib++) {
     array_push($emarrlens, ('' . (99 - strlen($emarrpre[$ib]))) . $emarrpre[$ib]);
   }
   sort($emarrlens);
   for ($ib=0; $ib<sizeof($emarrlens); $ib++) {
     $objphp.=substr($emarrlens[$ib], 2) . '~'; //array_push($emarr, substr($emarrlens[$ib], 2));
   }
   $emarr=explode($endspan, $objphp);
 } else {
   $emarr=explode($endspan, $objphp);
 }
 for ($ib=1; $ib<sizeof($emarr); $ib++) {
   $tonexta=explode($enda, $emarr[$ib])[0];
   $fromprevspan=explode($ea, $emarr[-1 + $ib])[-1 + sizeof(explode($ea, $emarr[-1 + $ib]))];
   if (strpos($tonexta, $za) === false && strpos($fromprevspan, ' ') === false) {
     //$emlist.=$tonexta . ':' . $fromprevspan . ';';
     $emlist.=$fromprevspan . '|' . $tonexta . '~';
     if (stripos($intext, $tonexta) !== false) {
       if ($tonexta != strtolower($tonexta)) {
         $thingos=explode(strtolower($tonexta), strtolower($intext));
         $lb=sizeof($thingos);
         for ($kb=1; $kb<$lb; $kb++) {
          if ($kb == 1) {
           array_push($thingos, ' ');
          }
          if ((substr(str_replace('/','a',(' ' . $thingos[-1 + $kb])), -1) < 'a' || substr(str_replace('/','a',(' ' . $thingos[-1 + $kb])), -1) > 'z') && (substr(($thingos[0 + $kb] . ' '), 0, 1) < 'a' || substr(($thingos[0 + $kb] . ' '), 0, 1) > 'z')) {
           $pref=substr((' ' . $thingos[-1 + $kb]), -1);
           $suff=substr(($thingos[0 + $kb] . ' '), 0, 1);
           //echo "pref=" . $pref . ' and tonexta=' . $tonexta . ' and thingos[-1 + kb]=' . $thingos[-1 + $kb] . '?';
           //exit;
           //$intext=str_replace($tonexta, strtolower($tonexta), $intext);
           //$intext=str_replace(strtolower($tonexta), '<SPAN TITLE="' . str_replace(" ","_",str_replace('"','',str_replace("'","",strtoupper($tonexta)))) . '">' . $fromprevspan . '</SPAN>', $intext);
           $intext=str_replace(($pref . $tonexta . $suff), strtolower(($pref . $tonexta . $suff)), $intext);
           $intext=str_replace(strtolower($pref . $tonexta . $suff), $pref . '<SPAN TITLE="' . str_replace(" ","$@!",str_replace('"','',str_replace("'","",strtoupper($tonexta)))) . '">' . $fromprevspan . '</SPAN>' . $suff, $intext);
          }
         }
       } else {
         $intext=str_replace($tonexta, '<SPAN TITLE="' . str_replace(" ","$@!",str_replace('"','',str_replace("'","",strtolower($tonexta)))) . '">' . $fromprevspan . '</SPAN>', $intext);
       }
     }
   }
 }
 if (!file_exists('./fillin_gaps_with_emojis.tt') && $emlist != '') {
   file_put_contents('./fillin_gaps_with_emojis.tt', '~' . $emlist);
 }
 }
 if ($intext != '') {
   echo "<html><head><title>Emoji-ize Text - RJM Programming - June, 2021</title><meta charset='UTF-8'></head><body style='width:100%;'><p>" . str_replace("\n","<br>",str_replace("$@!"," ",trim($intext))) . "</p></body></html>";
 }
 //echo "\n\n" . $emlist;

 exit;
?>
