<?php
// start_word_for_wordle_helper.php
// RJM Programming
// February, 2022
// Help suggest a start five letter word for Wordle
// https://www.thefreedictionary.com/snurt vs https://www.thefreedictionary.com/wilga

error_reporting(0);

$ondc=" ondblclick=dictmaybe(this); ";
if (isset($_GET['nodouble']) || isset($_POST['nodouble'])) { $ondc="";   }
$sofar="";
$sugnum=10;
$tablebit="<table style='width:90%;' border=20 cellpadding=5 cellspacing=5><tr><th>Suggested Word</th><th>Weirdness Score</th></tr></table>";
$letters='a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z';
$letscores='1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10';
$letscs=explode(',', $letscores);
$letss=explode(',', $letters);
$sortit=false;
$uniquifyit=false;
$commonality=false;
if (isset($_POST['others'])) { 
  if (strpos(strtolower(urldecode($_POST['others'])), 'sort') !== false) { $sortit=true; }
  if (strpos(strtolower(urldecode($_POST['others'])), 'uniqu') !== false) { $uniquifyit=true; }
  if (strpos(strtolower(urldecode($_POST['others'])), 'nodouble') !== false) { $ondc=""; }
  if (strpos(strtolower(urldecode($_POST['others'])), 'common') !== false) { $commonality=true; }
}
if (isset($_GET['others'])) { 
  if (strpos(strtolower(urldecode($_GET['others'])), 'sort') !== false) { $sortit=true; }
  if (strpos(strtolower(urldecode($_GET['others'])), 'uniqu') !== false) { $uniquifyit=true; }
  if (strpos(strtolower(urldecode($_GET['others'])), 'nodouble') !== false) { $ondc=""; }
  if (strpos(strtolower(urldecode($_GET['others'])), 'common') !== false) { $commonality=true; }
}
$arris=[];
$hsc="<head>
<title>Start five letter word for Wordle - RJM Programming - February, 2022</title>
<meta id='myviewport' name='viewport' content='width=device-width, initial-scale=1, minimum-scale=0.1, maximum-scale=8, user-scalable=yes' >
<style>
  td { 
    text-align: center;
  }

  td:nth-child(2n+1) {  
    background-color: #f3f7fa;
  }

  th:nth-child(2n+1) {  
    background-color: #f3f7fa;
  }
  
  body {
    background-color: #f7f7f7;
  }
</style>
<script type='text/javascript'>
  var wois=null;
  var sortrecs=[];
  var tables=[];
  var tbodies=[];
  var theads=[];
  var ths=[];
  var trs=[];
  var tds=[];
  var parti=0;
  var lessis=1;
  var tabih='';
  var woare=null;
  var adc=null;
  
  var pointerX = -1;
  var pointerY = -1;

  function pointerCheck() {
	if (pointerX != -1) {
	  document.getElementById('cwords').style.position='absolute';
	  document.getElementById('cwords').style.top='' + eval(0 + eval('' + pointerY)) + 'px';
	  document.getElementById('cwords').style.left='' + eval(30 + eval('' + pointerX)) + 'px';
	  document.getElementById('cwords').style.zIndex='99';
	  document.getElementById('cwords').style.backgroundColor='orange';
	  document.getElementById('cwords').innerHTML=document.getElementById('lsbits').title;
	}
  }
  
  function onl() {
   if (('' + localStorage.getItem('start_word_for_wordle_helper')).replace(/^null$/g,'') != '') {
     document.getElementById('myform').method='GET';
     document.getElementById('lsbits').title=decodeURIComponent(localStorage.getItem('start_word_for_wordle_helper')).replace('&sugnum=',' ').replace(encodeURIComponent('&sugnum='),' ');
     if (document.URL.indexOf('others=') == -1) {
       document.body.style.cursor='progress';
       document.onmousemove = function(event) {  // thanks to https://www.codegrepper.com/code-examples/javascript/get+cursor+position+javascript
       if (event.clientX || event.clientY) {
        pointerX = event.clientX;
        pointerY = event.clientY;
       } else {
	    pointerX = event.pageX;
	    pointerY = event.pageY;
	   }
       };
       setInterval(pointerCheck, 1000);
       location.href=(document.URL + '&others=' + (localStorage.getItem('start_word_for_wordle_helper'))).replace('.php&','.php?').replace(encodeURIComponent('&sugnum='),'&sugnum=');
     }
   }
   
   if (window.self !== window.top) { 
    document.body.innerHTML = ('' + document.body.innerHTML).replace(/h1/g, 'h2'); 
   }
   makesortable();
  }
  
  function gebtn() {
   theads=document.getElementsByTagName('thead');
   tables=document.getElementsByTagName('table');
   trs=document.getElementsByTagName('tr');
   tds=document.getElementsByTagName('td');
   tbodies=document.getElementsByTagName('tbody');
   ths=document.getElementsByTagName('th');
  }
  
  function makesortable() {
   var itd=0;
   var jtd=0;
   var recis='';
   var thisih='';
   var nonhead='';
   var origsrl=eval(sortrecs.length);
   gebtn();
   if (eval('' + theads.length) == 0 && eval('' + tbodies.length) > 0 && eval('' + tables.length) > 0 && eval('' + tds.length) > 0 && eval('' + ths.length) > 0 && eval('' + trs.length) > 0) {
     if (tables[0].innerHTML.indexOf('<tbody>') != -1) {
      tables[0].innerHTML=tables[0].innerHTML.replace('<tbody>','').replace('</tbody>','').replace(trs[0].outerHTML, '<thead>' + trs[0].outerHTML + '</thead>');
      theads=document.getElementsByTagName('thead');
      tbodies=document.getElementsByTagName('tbody');
     }
   }
   if (eval('' + tbodies.length) == 0 && eval('' + tables.length) > 0 && eval('' + tds.length) > 0) {
     if (eval('' + ths.length) > 0 && eval('' + trs.length) > 0) {
       if (eval('' + theads.length) > 0) {
         nonhead=tables[0].innerHTML.replace(theads[0].outerHTML,'');
       } else {
         nonhead=tables[0].innerHTML.replace(trs[0].outerHTML,'');
       }
       if (nonhead != '') {
         tables[0].innerHTML=tables[0].innerHTML.replace(nonhead, '<tbody id=mytbody>' + nonhead + '</tbody>');
         tbodies=document.getElementsByTagName('tbody');
       }
     }
   } 
   if (eval('' + tbodies.length) > 0 && eval('' + tables.length) > 0 && eval('' + tds.length) > 0) {
     if (eval('' + ths.length) > 0 && eval('' + trs.length) > 0) {
       recis='';
       for (itd=0; itd<eval('' + tds.length); itd+=eval('' + ths.length)) {
         if (itd == 0) {
           for (jtd=0; jtd<eval('' + ths.length); jtd++) {
            ths[jtd].title='Click this column ' + eval(1 + jtd) + ' to sort from highest to lowest.';
            ths[jtd].onclick = function() { sortcol(this);  };
           }
         }
         for (jtd=itd; jtd<eval(itd + eval('' + ths.length)); jtd++) {
         thisih=tds[jtd].innerHTML;
         if (thisih.trim() != '') {
           if (thisih.trim().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,'') == '') {
             recis+=('                    ' + thisih).slice(-20);
           } else {
             recis+=(thisih + '                    ').substring(0,20);
           }
         } else {
           recis+='                    ';
         }
         if (eval(eval(1 + jtd) % eval('' + ths.length)) == 0) {
           if (origsrl == 0) { sortrecs.push(recis); }
           recis='';
         }
         }
       }
     }
     if (origsrl == 0) { 
       tabih=tables[0].innerHTML;
       tables[0].ondblclick = function() { tables[0].innerHTML=tabih; makesortable();  };
       tables[0].title='Double click me to restore to original sort order.'; 
     }
   }
  }
  
  function compareparts(a, b) {
    if (a.substring(eval(20 * parti),eval(20 + eval(20 * parti))) == b.substring(eval(20 * parti),eval(20 + eval(20 * parti)))) { 
      if (parti > 0) {  
        if (a.substring(0,eval(20 * parti)) < b.substring(0,eval(20 * parti))) { 
          return lessis; 
        } else if (a.substring(0,eval(20 * parti)) > b.substring(0,eval(20 * parti))) { 
          return -lessis; 
        } 
      }
      return 0; 
    }
    if (a.substring(eval(20 * parti),eval(20 + eval(20 * parti))) < b.substring(eval(20 * parti),eval(20 + eval(20 * parti)))) { return lessis; }
    return -lessis;
  }
  
  function dictmaybe(intdo) {
    if (('' + intdo.innerHTML) != '' && ('' + intdo.innerHTML).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,'') != '') {
     if (woare) {  if (!woare.closed) {  woare.close(); woare=null;  }  }
     woare=window.open('//www.thefreedictionary.com/' + encodeURIComponent(intdo.innerHTML), '_blank', 'top=100,left=' + eval(eval('' + screen.width) / 2) + ',width=' + eval(eval('' + screen.width) / 2) + ',height=600');
    }
  }
  
  function sortcol(tho) {
   var ij=0;
   var kl=0;
   var tb='';
   parti=0;
   if (('' + tho.title).indexOf('Click this column ') == 0 && ('' + tho.title).indexOf(' to sort from ') != -1) {
     parti=eval(-1 + eval(('' + tho.title).split('Click this column ')[1].split(' ')[0]));
     if (eval('' + tbodies.length) > 0 && eval('' + tables.length) > 0 && eval('' + tds.length) > 0) {
       if (('' + tho.title).indexOf(' highest to lowest') != -1) {
         sortrecs.sort(compareparts);
         for (ij=0; ij<eval('' + ths.length); ij++) {
           ths[ij].title=ths[ij].title.replace(' highest to lowest', ' lowest to highest');
         }
         tb='';
         for (kl=0; kl<eval('' + sortrecs.length); kl++) {
           tb+='<tr></tr>';
           for (ij=0; ij<eval('' + ths.length); ij++) {
             tb=tb.replace('</tr>', '<td" . $ondc . ">' + sortrecs[kl].substring(eval(20 * ij),eval(20 + eval(20 * ij))).trim() + '</td></tr>');
           }
           tb=tb.replace('</tr>', '</TR>');
         }
         lessis=-lessis;
       } else {
         sortrecs.sort(compareparts);
         for (ij=0; ij<eval('' + ths.length); ij++) {
           ths[ij].title=ths[ij].title.replace(' lowest to highest', ' highest to lowest');
         }
         tb='';
         for (kl=0; kl<eval('' + sortrecs.length); kl++) {
           tb+='<tr></tr>';
           for (ij=0; ij<eval('' + ths.length); ij++) {
             tb=tb.replace('</tr>', '<td" . $ondc . ">' + sortrecs[kl].substring(eval(20 * ij),eval(20 + eval(20 * ij))).trim() + '</td></tr>');
           }
           tb=tb.replace('</tr>', '</TR>');
         }
         lessis=-lessis;
       }
       tbodies[0].innerHTML=tb.replace('</tr>', '</TR>');
     }
   }
  }

  function mixitup() {
   var others=prompt('Space separate the word(s) \"sort\" to sort for Weirdness and/or  \"common\" to appear in The Free Dictionary and/or \"unique\" for five letter words with no repeated letters.  And \"standing\" to cement these settings in for next time(s) or \"reset\" to undo any cementing.  Optionally precede by that number of five letter word suggestions to create for use with Wordle, followed by a space character.', '');
   var clickthis='';
   var pfx='';
   if (others == null) { others=''; }
   if (others.toLowerCase().indexOf('reset') != -1) {
   if (('' + localStorage.getItem('start_word_for_wordle_helper')).replace(/^null$/g,'') != '') {
     localStorage.removeItem('start_word_for_wordle_helper');
   }
   } 
   others=others.replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),'');
   others=others.toLowerCase().replace('sort',' sort').replace('uniqu',' uniqu');
   if (others.trim() != '') {
     var aothers=others.trim().split(' ');
     if (aothers[0].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('defopt').value='-' + aothers[0];
        document.getElementById('defopt').innerHTML=aothers[0];
        document.getElementById('sugnum').value='-' + aothers[0];
        pfx='&sugnum=' + aothers[0];
        others=others.replace(aothers[0],'').trim();
        clickthis='mysub';
     }
   }
   if (others.trim() != '') {
     var sv=document.getElementById('sugnum').value;
   if (others.toLowerCase().indexOf('standing') != -1) {
   if (('' + localStorage.getItem('start_word_for_wordle_helper')).replace(/^null$/g,'') != '') {
     localStorage.removeItem('start_word_for_wordle_helper');
   }
   console.log('localStorage put ' + encodeURIComponent(others.replace('standing','').replace('reset','') + pfx));
     localStorage.setItem('start_word_for_wordle_helper', encodeURIComponent(others.replace('standing','').replace('reset','') + pfx));
   } 
     document.getElementById('myform').innerHTML+='<input type=hidden name=others value=\"' + others.replace('standing','').replace('reset','') + '\"></input>';
     document.getElementById('sugnum').value=sv;
   }
   if (clickthis != '') {  document.getElementById(clickthis).click();  }
  }
    
</script>
</head>";

if (isset($_POST['sugnum'])) { $sugnum=str_replace('-','',str_replace('+',' ',urldecode($_POST['sugnum'])));   }
if (isset($_GET['sugnum'])) { $sugnum=str_replace('-','',str_replace('+',' ',urldecode($_GET['sugnum'])));   }

function ourstrtolower($insis) {
  global $uniquifyit, $sofar, $commonality;
  $outsis=strtolower($insis);
  if ($commonality) {
    $comtest=file_get_contents("HTTP://thefreedictionary.com/" . $outsis);
    //file_put_contents('xz.xz', $comtest);
    if (strpos($comtest, '<i>') === false) {  return ' '; }
    sleep(2);
  }
  if (strpos($sofar, ' ' . $outsis) !== false) { return ' '; }
  if (!$uniquifyit) { $sofar.=' ' . $outsis . ' '; return $outsis; }
  $okay=true;
  for ($m=0; $m<(-1 + strlen($insis)); $m++) {
    if (strpos(substr($outsis,(1 + $m)), substr($outsis,$m,1)) !== false) { $okay=false; }
  }
  if ($okay) { $sofar.=' ' . $outsis . ' '; return $outsis; }
  return ' ';
}

for ($i=0; $i<$sugnum; $i++) {
  $aword = ' ';
  while ($aword == ' ') {
    $aword=ourstrtolower(file_get_contents('http://www.rjmprogramming.com.au/PHP/surprise.php?min=6&max=6&youllneverfindthis=y'));
  }
  $iscore=0;
  for ($j=0; $j<strlen($aword); $j++) {
   for ($k=0; $k<sizeof($letss); $k++) {
    if (substr($aword,$j,1) == $letss[$k]) { $iscore+=$letscs[$k]; } 
   }
  }
  if (!$sortit) {
    $tablebit=str_replace('</table>', '<tr><td' . $ondc . '>' . $aword . '</td><td' . $ondc . '>' . $iscore . '</td></tr></table>', $tablebit);
  } else {
    array_push($arris, substr("0000000000" . $iscore, -10,10) . $aword);
  }
}

if ($sortit) {
  sort($arris);
  for ($i=0; $i<$sugnum; $i++) {
    $tablebit=str_replace('</table>', '<tr><td' . $ondc . '>' . substr($arris[$i],10) . '</td><td' . $ondc . '>' . ltrim(substr($arris[$i],0,10), '0') . '</td></tr></table>', $tablebit);
  }
}


echo "<html>" . $hsc . "<body onload=onl();><h1><span>Start five letter word for <a target=_blank href='//www.powerlanguage.co.uk/wordle/'>Wordle</a> ... </span><input onclick=mixitup(); title='Allow for Unique Letters, Weirdness Sorting' type=button style=display:inline-block; value='via '></input></span><form id=myform style=display:inline-block; action=./start_word_for_wordle_helper.php method=POST><input id=mysub type=submit value=Submit style=display:none;></input><select style=display:inline-block; id=sugnum name=sugnum onchange=document.getElementById('mysub').click();><option id=defopt value=-" . $sugnum . ">" . $sugnum . "</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option><option value=6>6</option><option value=7>7</option><option value=8>8</option><option value=9>9</option><option value=10>10</option><option value=20>20</option><option value=25>25</option><option value=50>50</option><option value=100>100</option></select><span id=lsbits> suggestions ...</span></h1><h3>RJM Programming ... February, 2022</h3><h4>Suggestions below (bigger associated number, the weirder is the suggestion) ...</h4><br>" . $tablebit . "<div id=cwords></div></body></html>";

?>