<?php
// emojiicon.php
// RJM Programming
// March, 2025

$usuffix='';
$iponl='';
$rma='';
$ask='';

  function massage($inidea) {
   if (isset($_GET['ididea'])) {
     return str_replace("document.body", "document.getElementById('" . str_replace('+',' ',urldecode($_GET['ididea'])) . "')", $inidea);
   }
   return str_replace('(7 == 7)', '(56 == 67)',$inidea);
  }

  function server_remote_addr() {
    global $iponl, $rma;
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    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;
    $rma=str_replace(':','_',$rma);
    $iponl=massage(" onload=\" if (window.parent) { if (parent.document.body.outerHTML.split('>')[0].indexOf(' data-ip=') == -1) { if (7 == 7) { parent.document.body.title=('' + parent.document.body.title) + ' +++ _" . $rma . "'; }  parent.document.body.setAttribute('data-ip', '_" . $rma . "');  } } \"");
    return $rma;
  }

  if (isset($_GET['mysvg'])) {
    $usuffix='_' . server_remote_addr();
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon' . $usuffix . '.svg', str_replace('   ',' + ',str_replace('+',' ',$_GET['mysvg'])));
    if ($iponl != '' && file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon_' . server_remote_addr() . '.svg')) {
      $iponl=massage(" onload=\" if (window.parent) { if (parent.document.body.outerHTML.split('>')[0].indexOf(' data-ip=') == -1) { parent.document.body.setAttribute('data-ip', '_" . $rma . "');  parent.document.body.setAttribute('data-firstip', 'y');  } } \"");
    }
  } else if (isset($_GET['codepoint'])) {
    if (strlen($_GET['codepoint']) > 0) {
     if (isset($_GET['ishex']) || isset($_GET['tohex']) || isset($_GET['hex'])) {
      $usuffix='_' . server_remote_addr();
      file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon' . $usuffix . '.svg', str_replace('&#x1f1e6;','&#127462;&#127465;','<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg"><text y="32" font-size="32">&#x' . str_replace('.',';&#x',str_replace(',',';&#x',str_replace(';','',str_replace('&#','',str_replace('+',' ',urldecode($_GET['codepoint'])))))) . ';</text></svg>'));
     } else {
      $usuffix='_' . server_remote_addr();
      file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon' . $usuffix . '.svg', '<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg"><text y="32" font-size="32">&#' . str_replace('.',';&#',str_replace(',',';&#',str_replace(';','',str_replace('&#','',str_replace('+',' ',urldecode($_GET['codepoint'])))))) . ';</text></svg>');
     }
    } else if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon_' . server_remote_addr() . '.svg')) {
     $usuffix='_' . server_remote_addr();
    } else {
     file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon' . $usuffix . '.svg', '<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg"><text y="32" font-size="32">X</text></svg>');
     if ($iponl != '' && file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon_' . server_remote_addr() . '.svg')) {
      $iponl=massage(" onload=\" if (window.parent) { if (parent.document.body.outerHTML.split('>')[0].indexOf(' data-ip=') == -1) { parent.document.body.setAttribute('data-ip', '_" . $rma . "');  parent.document.body.setAttribute('data-firstip', 'y');  } } \"");
     }
    }
  } else if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon_' . server_remote_addr() . '.svg')) {
    $usuffix='_' . server_remote_addr();
  } else {
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon' . $usuffix . '.svg', '<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg"><text y="32" font-size="32">X</text></svg>');
    if ($iponl != '' && file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'emojiicon' . DIRECTORY_SEPARATOR . 'favicon_' . server_remote_addr() . '.svg')) {
     $iponl=massage(" onload=\" if (window.parent) { if (parent.document.body.outerHTML.split('>')[0].indexOf(' data-ip=') == -1) { parent.document.body.setAttribute('data-ip', '_" . $rma . "');  parent.document.body.setAttribute('data-firstip', 'y');  } } \"");
    }
  }

  if (('' . $_SERVER['QUERY_STRING']) == '') {
     $ask="<br><div id=dform><form target=_self id=myform onsubmit=\"  document.getElementById('codepoint').value=document.getElementById('codepoint').value.replace(/^\.46\./g, '46.').replace(/\.46\.$/g, '.46');  if (('' + document.getElementById('codepoint').value).indexOf('-') != -1 && !viasvg) { return false; } setTimeout(doviasvg,2000); return false;\" method=GET action=./emojiicon.php><input title='Emoji codepoint as web browser tab icon (ie. look up to see after clicking display)' type=number onchange=emnone(); onblur=emnone(); id=codepoint name=codepoint step='1' value='-1' min='-1'></input> or <input type=text placeholder=Emoji onblur=cpit(this); maxlength=4 value='' id=anemoji title='Emoji ... perhaps via control-command-space for macOS or Mac OS X, logo key + . (period) for Windows, control=space for iOS, top left + for Android keyboard ... but please note not all International codes work with XML 1.0'></input><br><br>
<textarea rows=9 cols=120 style=width:95%; id=mysvg><?xml version=\"1.0\" standalone=\"no\"?>
<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" 
\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> 
<svg width=\"100\" height=\"100\" viewbox=\"0 0 100 100\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">
<rect x=\"-50\" y=\"-50\" width=\"100\" height=\"100\" id=\"square\" fill=\"red\" transform=\"translate(50, 50) rotate(45)\"></rect>
</svg></textarea><br>
<br><input onmouseover='viasvg=false;' onmousedown='viasvg=false;' ontouchdown='viasvg=false;' style=background-color:yellow; type=submit value='Display icon as positive codepoint Number above'></input>&nbsp;<input onmousedown='viasvg=true;' onmouseover='viasvg=true;' ontouchdown='viasvg=true;' style=background-color:yellow; type=submit name=svgvia value='Display icon as SVG above'></input></form></div>";
  }
 
echo "<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
<scr" . "ipt type=text/javascript>
   var viasvg=false, prevval='', prevcp='', initdiv='', wasval='';
  
   /** 
   * Convert a string to HTML entities ... thanks to https://stackoverflow.com/questions/18749591/encode-html-entities-in-javascript
   */
   String.prototype.toHtmlEntities = function() {
    return this.replace(/./gm, function(s) {
        // return \"&#\" + s.charCodeAt(0) + \";\";
        return (s.match(/[a-z0-9\s]+/i)) ? s : \"&#\" + s.charCodeAt(0) + \";\";
    });
   }; 

   
   function emnone() {
     document.getElementById('anemoji').value='';
   }
   
   function nononnum(insag) {
     var pfx='', sfx='';
     if (insag.indexOf('&#') > 0 && wasval.indexOf('&#') == -1) { insag=insag.replace('&#',''); }
     if (insag.indexOf(';') > 0 && wasval.indexOf(';') == -1) { insag=insag.replace(/\;/g,''); }
     insag=insag.replace(/^\.46\./g, '46.').replace(/\.46\.$/g, '.46');
     if (wasval != wasval.replace(/^\./g,'')) { pfx='.';  }
     if (wasval != wasval.replace(/\.$/g,'')) { sfx='.';  }
     var outsag=insag;
     for (var jj=0; jj<insag.length; jj++) {
       if (insag.substring(jj).substring(0,1) != '.') {
        if (insag.substring(jj).substring(0,1) < '0' || insag.substring(jj).substring(0,1) > '9') {
         outsag=outsag.replace(insag.substring(jj).substring(0,1), ('.' + insag.substring(jj).substring(0,1).charCodeAt() + '.'));
        }
       } 
     }
     return (pfx + outsag.replace(/^\./g,'').replace(/\.$/g,'').replace(/\.\./g,'.').replace(/\.\./g,'.').replace(/\.\./g,'.').replace(/\.\./g,'.').replace(/\.\./g,'.') + sfx).replace(/^\.46\./g, '46.').replace(/\.46\.$/g, '.46');
   }
   
   function cpit(otb) {
     if (otb.value != '') {
       if (otb.value.toHtmlEntities().indexOf('&#x') != -1) {
       } else if (otb.value.toHtmlEntities().indexOf('&#') != -1) {
        document.getElementById('codepoint').type='text';
        //alert('' + otb.value.toHtmlEntities().replace(/^\&\#/g,'').replace(/\;$/g,'').replace(/\;\&\#/g,'.'));
        wasval='' + otb.value;
        document.getElementById('codepoint').value=nononnum('' + otb.value.toHtmlEntities().replace(/^\.\&\#/g,'.').replace(/^\&\#/g,'').replace(/\;\.$/g,'.').replace(/\;$/g,'').replace(/\;\&\#/g,'.').replace(/^\.46\./g, '46.').replace(/\.46\.$/g, '.46'));
        //document.getElementById('codepoint').value=document.getElementById('codepoint').value.replace(/^\.46\./g, '46.').replace(/\.46\.$/g, '.46'); 
        } else {
        document.getElementById('codepoint').value='';
        for (var jj=0; jj<otb.value.length; jj++) {
        if (document.getElementById('codepoint').value == '') {
        document.getElementById('codepoint').value='' + otb.value.substring(jj).substring(0,1).charCodeAt();
        } else {
        document.getElementById('codepoint').type='text';
        document.getElementById('codepoint').value+='.' + otb.value.substring(jj).substring(0,1).charCodeAt();
        }
        }
       }
       //alert(otb.value.toHtmlEntities());
     }
   }
   
   function doviasvg() {
    if (viasvg) {  
      document.getElementById('mysvg').name='mysvg'; 
      document.getElementById('myform').submit(); 
      setTimeout(function(){ document.getElementById('dform').innerHTML=initdiv; }, 7000);
    } else if (('' + document.getElementById('codepoint').value).indexOf('-') == -1) {
      document.getElementById('myform').submit();
      setTimeout(function(){ document.getElementById('dform').innerHTML=initdiv; }, 7000);
    }
   }
   
   setTimeout(function(){
   if (window.parent) {
     if (window.parent != window.self) {
        document.getElementById('dform').innerHTML=document.getElementById('dform').innerHTML.replace('_self','_blank'); //document.getElementById('myform').target='_top';
     }
   }
   initdiv=document.getElementById('dform').innerHTML;
   }, 3000);
   
</scr" . "ipt>
<title>Emoji Icon - RJM Programming - March, 2025</title>
<link rel='icon' href='/emojiicon/favicon" . $usuffix . ".svg?rand=" . rand(0,3456789) . "' sizes='any' type='image/svg+xml'>
</head>
<body" . $iponl . ">
<h1>Emoji Icon <a target=_blank href=./emojiicon.php title=Reset>-</a> RJM Programming - March, 2025</h1>" . $ask . "
</body>
</html>";
?>
