<?php
// emoji_animation.php
// RJM Programming
// May, 2022
// Thanks to https://dev.to/anthonyjdella/how-to-animate-an-emoji-with-css-5eb9

$words="Hi";
  
$cssstyle="<style>
.waving:before {
content: attr(data-hover);
font-size:84px;
margin-top:150px;
margin-right:150px;
margin-left:150px;
margin-bottom:150px;
}

 .waving {
animation-name: wave-animation;
animation-duration: 3.0s;
animation-iteration-count: infinite;
transform-origin: 70% 70%;
}

@keyframes wave-animation {
0% {
transform: rotate( 0.0deg) }
10% {
tRansform: rotate(14.0deg) }
20% {
trAnsform: rotate(-8.0deg) }
30% {
traNsform: rotate(14.00deg) }
40% {
tranSform: rotate(-4.0deg) }
50% {
transForm: rotate(10.0deg) }
60% {
transfOrm: rotate( 0.00deg) }
100% {
transfoRm: rotate( 0.000deg) }
}
</style>
";
$precss=$cssstyle;

function viacolon($instuff) {

//21c
//  background: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' height='30' width='30'><text x='50%' y='50%' dominant-baseline='middle' text-anchor='middle'>🎄🥎</text></svg>");
//.
//13,14c
//.box:after {
//  content: "Well I never";
//.
//w

  $ldm=";";
  $prevln='';
  $inlns=explode("\n", $instuff);
  $lns=file('/tmp/emoji_animation.diff');
  foreach ($lns as $line_num => $ln) {
    if (strpos($ln, ':') !== false && (strpos($ln, ';') !== false || strpos($ln, '{') !== false)) {
      if (strpos($ln, 'background:') === false) {
        $bg=explode(':', $ln);
        if (strpos($instuff, $bg[0] . ':') !== false) {
          $ldm=";";
          if (strpos($bg[1], ";") === false && strpos($bg[1], "{") !== false) { $ldm="{"; }
          //if (strpos($bg[0], ".box") !== false) {  file_put_contents('ax.ax', $instuff . "\n\n\n" . $bg[0] . ':' . explode($ldm, explode($bg[0] . ':', $instuff)[1])[0] . ':' . "\n\n\n" . $bg[0] . ':' . explode($ldm, explode($bg[0] . ':', file_get_contents('/tmp/emoji_animation.diff'))[1])[0] . "\n\n\n" . str_replace($bg[0] . ':' . explode($ldm, explode($bg[0] . ':', $instuff)[1])[0],$bg[0] . ':' . explode($ldm, explode($bg[0] . ':', file_get_contents('/tmp/emoji_animation.diff'))[1])[0],$instuff));    }
          $instuff=str_replace($bg[0] . ':' . explode($ldm, explode($bg[0] . ':', $instuff)[1])[0],$bg[0] . ':' . explode($ldm, explode($bg[0] . ':', file_get_contents('/tmp/emoji_animation.diff'))[1])[0],$instuff);
        } else if (strpos($prevln, "a") !== false) {
          $instuff=str_replace(str_replace("\n", "", $inlns[-1 + explode("a", $prevln)[0]]), str_replace("\n", "", $inlns[-1 + explode("a", $prevln)[0]]) . " " . $bg[0] . ':' . explode(';', explode($bg[0] . ':', file_get_contents('/tmp/emoji_animation.diff'))[1])[0] . '; ', $instuff);
        }
      }
    }
    if (strpos($prevln, "d") !== false && strlen($prevln) <= 10 && strpos(trim($prevln), ' ') === false) {
      $instuff=str_replace(str_replace("\n", "", $inlns[-1 + explode("d", $prevln)[0]]), " ", $instuff); 
    } else if (strpos($prevln, "c") !== false && strlen($prevln) <= 10 && strpos(trim($prevln), ' ') === false) {
      $instuff=str_replace(str_replace("\n", "", $inlns[-1 + explode("c", $prevln)[0]]), str_replace("\n", "", $ln), $instuff); 
    } 
    $prevln=$ln;
  }
  return $instuff;
}

function command_exist($cmd) {
    $return = shell_exec(sprintf("which %s", escapeshellarg($cmd)));
    return !empty($return);
}

if (file_exists('/tmp/')) {
  if (!file_exists('/tmp/emoji_animation.before')) {
    file_put_contents('/tmp/emoji_animation.before', $cssstyle);
  }
  if (isset($_GET['reconstruct'])) {
    $emaila="&nbsp;&nbsp;<button onclick=emailit();>Email &#128231;</button>";
    $smsa="&nbsp;&nbsp;<button onclick=smsit();>SMS &#128223;</button>";
    $onl=" onload=\" newwin(document.getElementById('box')); \"";
    $r=str_replace("+"," ",urldecode($_GET['reconstruct']));
    if (strpos($r, "~!@") !== false) { 
      $prewords=explode("~!@", $r)[0]; 
      $r=substr($r, strlen($prewords . "~!@")); 
      $words=str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("   "," + ",str_replace("svg xml,","svg+xml,",$prewords)))));
    }
    file_put_contents('/tmp/emoji_animation.after', $precss);
    file_put_contents('/tmp/emoji_animation.diff', str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("   "," + ",str_replace("svg xml,","svg+xml,",$r)))))); 
    if (command_exist('ed')) {
    exec('ed - /tmp/emoji_animation.after < /tmp/emoji_animation.diff');
    } else if (strpos($precss, 'background:') !== false && strpos(file_get_contents('/tmp/emoji_animation.diff'), 'background:') !== false) {
    file_put_contents('/tmp/emoji_animation.after', viacolon(str_replace('background:' . explode(';', explode('background:', $precss)[1])[0],'background:' . explode(';', explode('background:', file_get_contents('/tmp/emoji_animation.diff'))[1])[0],$precss)));
    } else {
    file_put_contents('/tmp/emoji_animation.after', viacolon($precss));
    }
    $cssstyle=file_get_contents('/tmp/emoji_animation.after');
    //unlink('/tmp/emoji_animation.after');
  }
}

if (isset($_POST['email'])) {
    $myclick.="\n emis='" . str_replace('+',' ',urldecode($_POST['email'])) . "';  \n";
    $myclick.="\n emailit(); /" . "/ document.getElementById('myemail').click(); \n";
}
if (isset($_POST['sms'])) {
    $myclick.="\n smsis='" . str_replace('+',' ',urldecode($_POST['sms'])) . "';  \n";
    $myclick.="\n smsit(); /" . "/ document.getElementById('mysms').click(); \n";
}
if (isset($_GET['email'])) {
    $myclick.="\n emis='" . str_replace('+',' ',urldecode($_GET['email'])) . "';  \n";
    $myclick.="\n emailit(); /" . "/ document.getElementById('myemail').click(); \n";
}
if (isset($_GET['sms'])) {
    $myclick.="\n smsis='" . str_replace('+',' ',urldecode($_GET['sms'])) . "';  \n";
    $myclick.="\n smsit(); /" . "/ document.getElementById('mysms').click(); \n";
}

if (isset($_POST['css'])) {
  $was=$cssstyle;
  $cssstyle=str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("   "," + ",str_replace("svg xml,","svg+xml,",str_replace("+"," ",urldecode($_POST['css'])))))));
  if (file_exists('/tmp/')) {
  if (file_exists('/tmp/emoji_animation.after')) { unlink('/tmp/emoji_animation.after');    }
  if (!file_exists('/tmp/emoji_animation.after')) {
    file_put_contents('/tmp/emoji_animation.after', $cssstyle);
  }
  if ($was == $cssstyle) { 
    file_put_contents('/tmp/emoji_animation.diff', 'w');  
    $reconstruct=('?reconstruct=' . urlencode(file_get_contents('/tmp/emoji_animation.diff')));
  } else {
    $emaila="&nbsp;&nbsp;<button onclick=emailit();>Email &#128231;</button>";
    $smsa="&nbsp;&nbsp;<button onclick=smsit();>SMS &#128223;</button>";
    exec('diff --strip-trailing-cr -b -e /tmp/emoji_animation.before /tmp/emoji_animation.after >  /tmp/emoji_animation.diff');
    if (file_exists('/tmp/emoji_animation.diff')) { file_put_contents('/tmp/emoji_animation.diff', str_replace("\r","",file_get_contents('/tmp/emoji_animation.diff')) . "w\n"); $reconstruct=('?reconstruct=' . urlencode(file_get_contents('/tmp/emoji_animation.diff')));  } 
  }
  }
} else if (isset($_GET['css'])) {
  $was=$cssstyle;
  $cssstyle=str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("   "," + ",str_replace("svg xml,","svg+xml,",str_replace("+"," ",urldecode($_GET['css'])))))));
  if (file_exists('/tmp/')) {
  if (file_exists('/tmp/emoji_animation.after')) { unlink('/tmp/emoji_animation.after');    }
  if (!file_exists('/tmp/emoji_animation.after')) {
    file_put_contents('/tmp/emoji_animation.after', $cssstyle);
    $reconstruct=('?reconstruct=' . urlencode(file_get_contents('/tmp/emoji_animation.diff')));
  }
  if ($was == $cssstyle) { 
    file_put_contents('/tmp/emoji_animation.diff', 'w');  
  } else {
    $emaila="&nbsp;&nbsp;<button onclick=emailit();>Email &#128231;</button>";
    $smsa="&nbsp;&nbsp;<button onclick=smsit();>SMS &#128223;</button>";
    exec('diff --strip-trailing-cr -b -e /tmp/emoji_animation.before /tmp/emoji_animation.after >  /tmp/emoji_animation.diff');
    if (file_exists('/tmp/emoji_animation.diff')) { file_put_contents('/tmp/emoji_animation.diff', str_replace("\r","",file_get_contents('/tmp/emoji_animation.diff')) . "w\n"); $reconstruct=('?reconstruct=' . urlencode(file_get_contents('/tmp/emoji_animation.diff'))); } 
  }
  }
}

if (isset($_POST['words'])) {
  $words=str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("   "," + ",str_replace("svg xml,","svg+xml,",str_replace("+"," ",urldecode($_POST['words'])))))));
  if ($words != "Hi") { $reconstruct=str_replace(('?reconstruct='),('?reconstruct=') . urlencode($words . '~!@'), $reconstruct);   $emaila="&nbsp;&nbsp;<button onclick=emailit();>Email &#128231;</button>";
  $smsa="&nbsp;&nbsp;<button onclick=smsit();>SMS &#128223;</button>";
  }
} else if (isset($_GET['words'])) {
  $words=str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("\n + ","\n",str_replace("   "," + ",str_replace("svg xml,","svg+xml,",str_replace("+"," ",urldecode($_GET['words'])))))));
  if ($words != "Hi") { $reconstruct=str_replace(('?reconstruct='),('?reconstruct=') . urlencode($words . '~!@'), $reconstruct);   $emaila="&nbsp;&nbsp;<button onclick=emailit();>Email &#128231;</button>";
  $smsa="&nbsp;&nbsp;<button onclick=smsit();>SMS &#128223;</button>";
  }
} else if (!isset($_GET['reconstruct'])) {
if (file_exists('emoji_animation.htm')) {
  header('Location: ' . str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "" . str_replace('.php','.htm',$_SERVER['REQUEST_URI']))));
  exit;
}
}

if ($reconstruct != '') {
  $emaila="&nbsp;&nbsp;<button onclick=emailit();>Email &#128231;</button>";
  $smsa="&nbsp;&nbsp;<button onclick=smsit();>SMS &#128223;</button>";
}

if (1 == 2 && $myclick != "" && $reconstruct != "") {
  echo "<html><body onload=\" top.location.href='" . (str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "" . str_replace('.htm','.php',$_SERVER['REQUEST_URI'])))) . ($reconstruct) . "'; \"></body></html>";
} else {

echo "
<html>
<head>
<meta charset='utf-8'/>
<title>Emoji Animation - RJM Programming - May, 2022 ... thanks to https://dev.to/anthonyjdella/how-to-animate-an-emoji-with-css-5eb9</title>
" . $cssstyle . "
<script type='text/javascript'>
  var wasih='', isih='';
  var smsis='', emis='';
  var dho='';

if (!String.fromCodePoint) {   // thanks to http://xahlee.info/js/js_unicode_code_point.html
// ES6 Unicode Shims 0.1 , © 2012 Steven Levithan , MIT License
    String.fromCodePoint = function fromCodePoint () {
        var chars = [], point, offset, units, i;
        for (i = 0; i < arguments.length; ++i) {
            point = arguments[i];
            offset = point - 0x10000;
            units = point > 0xFFFF ? [0xD800 + (offset >> 10), 0xDC00 + (offset & 0x3FF)] : [point];
            chars.push(String.fromCharCode.apply(null, units));
        }
        return chars.join(\"\");
    }
}
  
  function checkpforparent() {
    if (window.parent != window) {
    console.log(1);
      dho=document.head.outerHTML.replace('<style' + document.head.outerHTML.split('<style')[1].split('</style>')[0] + '</style>', parent.document.getElementById('css').value);
    console.log(2);
      document.getElementById('spares').innerHTML=parent.document.getElementById('css').value;
    console.log(3);
      document.getElementById('css').value=parent.document.getElementById('css').value;
    console.log(4);
      document.getElementById('words').value=parent.document.getElementById('words').value;
    console.log(5);
      document.getElementById('box').innerHTML=parent.document.getElementById('box').innerHTML;
    console.log(6);
    parent.document.getElementById('toptable').style.opacity='0.0';
    console.log(7);
    //document.getElementById('jfp').src=document.URL.split('#')[0].split('?')[0] + '?rand=' + Math.floor(Math.random() * 19876564);
    if (1 == 11) {
    console.log(8);
    parent.document.getElementById('jfp').style.display='block';
    console.log(9);
    parent.document.getElementById('jfp').style.position='absolute';
    console.log(10);
    parent.document.getElementById('jfp').style.top='0px';
    console.log(11);
    parent.document.getElementById('jfp').style.left='0px';
    console.log(12);
    parent.document.getElementById('jfp').style.width='100%';
    console.log(13);
    parent.document.getElementById('jfp').style.height='100vh';
    console.log(14);
    parent.document.getElementById('jfp').style.zIndex='199';
    console.log(15);
    //document.getElementById('jfp').style.opacity='1.0';
    }
      showb();
    }
    " . $myclick . "
  }
  
  function emailit() {
    if (emis == '') { emis=prompt('Email address to send to?', ''); }
    if (emis != null) {
      if (emis.trim() != '') {
       if (emis.indexOf('@') != -1) {
        document.getElementById('myemail').href=document.getElementById('myemail').href.replace('mailto:', 'mailto:' + emis);
        document.getElementById('myemail').click();
       } else if (emis.replace(/0/g,'').replace(/1/g,'').replace(/2/g,'').replace(/3/g,'').replace(/4/g,'').replace(/5/g,'').replace(/6/g,'').replace(/7/g,'').replace(/8/g,'').replace(/9/g,'') == '') {
        document.getElementById('mysms').href=document.getElementById('mysms').href.replace('sms:', 'sms:' + emis);
        document.getElementById('mysms').click();
       } else {
        document.getElementById('myemail').click();
       }
      }
    }
  }
  
  function smsit() {
    if (smsis == '') { smsis=prompt('Mobile number to SMS to?', ''); }
    if (smsis != null) {
      if (smsis.trim() != '') {
       if (smsis.indexOf('@') != -1) {
        document.getElementById('myemail').href=document.getElementById('myemail').href.replace('mailto:', 'mailto:' + smsis);
        document.getElementById('myemail').click();
       } else if (smsis.replace(/0/g,'').replace(/1/g,'').replace(/2/g,'').replace(/3/g,'').replace(/4/g,'').replace(/5/g,'').replace(/6/g,'').replace(/7/g,'').replace(/8/g,'').replace(/9/g,'') == '') {
        document.getElementById('mysms').href=document.getElementById('mysms').href.replace('sms:', 'sms:' + smsis);
        document.getElementById('mysms').click();
       }
      }
    }
  }
  
  function newwin(divo) {
    var rectb=document.getElementById('box').getBoundingClientRect();
    var wo=window.open('','_blank','left=10,top=' + eval(200 + eval('' + rectb.height)) + ',width=' + eval(20 + eval('' + rectb.width)) + ',height=' + eval(20 + eval('' + rectb.height)));
    wo.document.write('<html>' + document.head.outerHTML + '<body>' + divo.outerHTML + '</body></html>');
  }
  
  function showb() {
    if (document.getElementById('bemail')) {
    document.getElementById('bemail').style.display='inline-block';
    document.getElementById('bsms').style.display='inline-block';
    }
  }
   
  
  setTimeout(checkpforparent, 2000);
</script>
</head>
<body" . $onl . ">
<table border=12 cellpadding=3 cellspacing=3><tr><td style=vertical-align:top; title='Click to show card above in new window' onclick=\"newwin(document.getElementById('box'));\" ondblclick=\"newwin(document.getElementById('box'));\">
<div data-hover='👋 ' id=box title='Double click to show in new window' onclick='event.stopPropagation();' ondblclick=newwin(this); class='waving' contenteditable=true onblur=\" document.getElementById('words').value=(this.innerText || this.contentWindow || this.contentDocument);  wasih=this.innerHTML; isih=wasih; if (document.getElementById('words').value.indexOf(String.fromCharCode(10)) != -1) { isih=document.getElementById('words').value;  }  if (isih.indexOf(String.fromCharCode(10)) != -1) { while (isih.indexOf(String.fromCharCode(10)) != -1) { isih=isih.replace(String.fromCharCode(10),'<br>');  }   this.innerHTML=isih;  } \">" . str_replace("\n", "<br>", $words) . "</div><br>
</td><td style=vertical-align:top;text-align:center;><h1>Emoji Animation Design" . $emaila . $smsa . "</h1><h3>RJM Programming - May, 2022</h3><h4>Thanks to <a target=_blank title=https://dev.to/anthonyjdella/how-to-animate-an-emoji-with-css-5eb9 href='https://dev.to/anthonyjdella/how-to-animate-an-emoji-with-css-5eb9'>How to Animate an Emoji with CSS</a></h4>
</td><td style=vertical-align:top;>
<form id=myform method=POST action=./emoji_animation.php>
<table border=18 style=background-color:orange;>
<tr><th>Words</th><th><input id=subbut type=submit value=Post style=background-color:pink;></input> CSS</th></tr>
<tr><td style=vertical-align:top;><textarea id=words name=words rows=37 onblur=\" isih=this.value; if (isih.indexOf(String.fromCharCode(10)) != -1) { while (isih.indexOf(String.fromCharCode(10)) != -1) { isih=isih.replace(String.fromCharCode(10),'<br>');  }  } document.getElementById('box').innerHTML=isih;  \">" . $words . "</textarea></td>
<td style=vertical-align:top;><textarea id=css name=css cols=52 rows=37 title='Can use, within that SVG defining the card border, an Emoji Menu entry (eg. control-command-space for macOS or Mac OS X, logo key + . (period) for Windows, control=space for iOS, top left + for Android keyboard).'>" . $cssstyle . "</textarea></td></tr></table>
<div id=eof style=display:none;></div>
</form>
</td></tr></table>
<a target=_top id=myemail style=display:none; href='mailto:?subject=My%20Emoji%20Animation&body=" . urlencode(str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "" . str_replace('.htm','.php',$_SERVER['REQUEST_URI'])))) . urlencode($reconstruct) . "'>Email</a>
<a target=_top id=mysms style=display:none; href='sms:&body=" . urlencode(str_replace(":80/","/",str_replace(":443/","/","http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "" . str_replace('.htm','.php',$_SERVER['REQUEST_URI'])))) . urlencode($reconstruct) . "'>Email</a>
<div id=iask style=display:none;></div>
<iframe name=ifp id=ifp style=display:none; src=''></iframe>
<div id=spares style=display:none;></div>
<iframe name=jfp id=jfp style=display:none; src=''></iframe>
</body>
</html>";
}
?>
