<?php
// cowsay.php
// RJM Programming
// February, 2025
// Thanks to https://www.linuxquestions.org/questions/programming-9/shell-question-pad-end-of-each-line-with-spaces-to-%3D-80-chars-875082/
   $suff='';
   $three=''; 
   $serious=false;
   if (isset($_GET['inwords'])) {
     if (isset($_GET['chr'])) {
       $suff=' -c "' . str_replace('+',' ',urldecode($_GET['chr'])) . '" ';
     }
     if (!file_exists('/tmp/cowsay.wds')) {
         $serious=true; 
         file_put_contents('/tmp/cowsay.wds', $_GET['inwords']);  
     } else if (substr(file_get_contents('/tmp/cowsay.wds'),0,strlen($_GET['inwords'])) == $_GET['inwords']) {
         $serious=false;
     } else {
         $serious=true; 
         file_put_contents('/tmp/cowsay.wds', $_POST['inwords']);  
     }
     if (isset($_GET['and']) && file_exists('/tmp/cowsay.txt')) {
     if (isset($_GET['outdrawing'])) {
       file_put_contents('/tmp/cowsay.txt', str_replace('+',' ',urldecode($_GET['outdrawing'])));
     }
     if (file_exists('/tmp/cowsaytwo.txt')) { unlink('/tmp/cowsaytwo.txt');  }
     if (file_exists('/tmp/cowsaythree.txt')) { unlink('/tmp/cowsaythree.txt');  }
     if (file_exists('/tmp/cowsayzero.txt')) { unlink('/tmp/cowsayzero.txt');  }
     exec("awk '{print length}' /tmp/cowsay.txt |sort -nr|head -1 | sed 's/[^0-9]//g' > /tmp/cowsayzn.txt");
     if (isset($_GET['maxl'])) {
     exec("cat /tmp/cowsay.txt | sed 's/ /:/g' | awk '{printf \"%-" . $_GET['maxl'] . "s\n\", $1}' | sed 's/:/ /g' > /tmp/cowsayzero.txt");
     } else {
     exec("cat /tmp/cowsay.txt | sed 's/ /:/g' | awk '{printf \"%-`cat /tmp/cowsayzn.txt | sed 's/[^0-9]//g'`s\n\", $1}' | sed 's/:/ /g' > /tmp/cowsayzero.txt");
     }
     exec("cat /tmp/cowsayzero.txt | head -1 | sed 's/[^\ ]/\ /g' > /tmp/cowsaynone.txt");
     for ($i=0; $i<=100; $i++) {
        exec("cat /tmp/cowsaynone.txt >> /tmp/cowsayzero.txt");
     }
     if ($serious) {
     $three='three';
     exec('cowsay' . $suff . ' -t "' . str_replace('+',' ',urldecode($_GET['inwords'])) . '" > /tmp/cowsaytwo.txt ; pr -m -t -w 500 /tmp/cowsayzero.txt /tmp/cowsaytwo.txt > /tmp/cowsaythree.txt'); // ; cp -f /tmp/cowsaythree.txt /tmp/cowsay.txt');
     } else {
     exec('cowsay' . $suff . ' -t "' . str_replace('+',' ',urldecode($_GET['inwords'])) . '" > /tmp/cowsaytwo.txt ; pr -m -t -w 500 /tmp/cowsayzero.txt /tmp/cowsaytwo.txt > /tmp/cowsaythree.txt');
     }
     //if (file_exists('/tmp/cowsaytwo.txt')) { unlink('/tmp/cowsaytwo.txt');  }
     echo "<html><head><title>Cow Says ... thanks to https://pypi.org/project/cowsay</title><meta charset='UTF-8'/></head><body onload=\"if (window.parent) { if (parent.document.getElementById('result')) {  parent.document.getElementById('result').style.textShadow='-1px 1px 1px #952dff'; parent.document.getElementById('result').value=decodeURIComponent('" . urlencode(file_get_contents('/tmp/cowsay' . $three . '.txt')) . "').replace(/\+/g,' '); parent.document.getElementById('result').style.fontFamily='Courier New';  parent.document.getElementById('result').style.fontSize='8px';  parent.document.getElementById('result').title='Double click for more ... thanks to https://pypi.org/project/cowsay'; parent.document.getElementById('result').ondblclick=function(){ window.open('/cowsay.php','_blank','top=100,left=100,width=600,height=600'); };   }  }\"><textarea id=dta title='Double click for another Cow Says pearl of wisdom.' ondblclick=\"location.href='/cowsay.php';\" style='width:90%;height:90%;border:2px dotted red;font-size:8px;'>" . file_get_contents('/tmp/cowsaythree.txt') . "</textarea></body></html>";
     } else {
     exec('cowsay' . $suff . ' -t "' . str_replace('+',' ',urldecode($_GET['inwords'])) . '" > /tmp/cowsay.txt');
     echo "<html><head><title>Cow Says ... thanks to https://pypi.org/project/cowsay</title><meta charset='UTF-8'/></head><body onload=\"if (window.parent) { if (parent.document.getElementById('result')) {  parent.document.getElementById('result').style.textShadow='-1px 1px 1px #952dff'; parent.document.getElementById('result').value=decodeURIComponent('" . urlencode(file_get_contents('/tmp/cowsay.txt')) . "').replace(/\+/g,' '); parent.document.getElementById('result').style.fontFamily='Courier New';  parent.document.getElementById('result').style.fontSize='8px';  parent.document.getElementById('result').title='Double click for more ... thanks to https://pypi.org/project/cowsay'; parent.document.getElementById('result').ondblclick=function(){ window.open('/cowsay.php','_blank','top=100,left=100,width=600,height=600'); };   }  }\"><textarea id=dta title='Double click for another Cow Says pearl of wisdom.' ondblclick=\"location.href='/cowsay.php';\" style='width:90%;height:90%;border:2px dotted red;font-size:8px;'>" . file_get_contents('/tmp/cowsay.txt') . "</textarea></body></html>";
     }
     exit;
   } else if (isset($_POST['inwords'])) {
     if (isset($_POST['chr'])) {
       $suff=' -c "' . str_replace('+',' ',urldecode($_POST['chr'])) . '" ';
     }
     if (!file_exists('/tmp/cowsay.wds')) {
         $serious=true; 
         file_put_contents('/tmp/cowsay.wds', $_POST['inwords']);  
     } else if (substr(file_get_contents('/tmp/cowsay.wds'),0,strlen($_POST['inwords'])) == $_POST['inwords']) {
         $serious=false;
     } else {
         $serious=true; 
         file_put_contents('/tmp/cowsay.wds', $_POST['inwords']);  
     }
     if (isset($_POST['and']) && file_exists('/tmp/cowsay.txt')) {
     if (isset($_POST['outdrawing'])) {
       file_put_contents('/tmp/cowsay.txt', str_replace('+',' ',urldecode($_POST['outdrawing'])));
     }
     if (file_exists('/tmp/cowsaytwo.txt')) { unlink('/tmp/cowsaytwo.txt');  }
     if (file_exists('/tmp/cowsaythree.txt')) { unlink('/tmp/cowsaythree.txt');  }
     if (file_exists('/tmp/cowsayzero.txt')) { unlink('/tmp/cowsayzero.txt');  }
     exec("awk '{print length}' /tmp/cowsay.txt |sort -nr|head -1 | sed 's/[^0-9]//g' > /tmp/cowsayzn.txt");
     if (isset($_POST['maxl'])) {
     exec("cat /tmp/cowsay.txt | sed 's/ /:/g' | awk '{printf \"%-" . $_POST['maxl'] . "s\n\", $1}' | sed 's/:/ /g' > /tmp/cowsayzero.txt");
     } else {
     exec("cat /tmp/cowsay.txt | sed 's/ /:/g' | awk '{printf \"%-`cat /tmp/cowsayzn.txt | sed 's/[^0-9]//g'`s\n\", $1}' | sed 's/:/ /g' > /tmp/cowsayzero.txt");
     }
     exec("cat /tmp/cowsayzero.txt | head -1 | sed 's/[^\ ]/\ /g' > /tmp/cowsaynone.txt");
     for ($i=0; $i<=100; $i++) {
        exec("cat /tmp/cowsaynone.txt >> /tmp/cowsayzero.txt");
     }
     if ($serious) {
     $three='three';
     exec('cowsay' . $suff . ' -t "' . str_replace('+',' ',urldecode($_POST['inwords'])) . '" > /tmp/cowsaytwo.txt ; pr -m -t -w 500 /tmp/cowsayzero.txt /tmp/cowsaytwo.txt > /tmp/cowsaythree.txt'); // ; cp -f /tmp/cowsaythree.txt /tmp/cowsay.txt');
     } else {
     exec('cowsay' . $suff . ' -t "' . str_replace('+',' ',urldecode($_POST['inwords'])) . '" > /tmp/cowsaytwo.txt ; pr -m -t -w 500 /tmp/cowsayzero.txt /tmp/cowsaytwo.txt > /tmp/cowsaythree.txt');
     }
     //if (file_exists('/tmp/cowsaytwo.txt')) { unlink('/tmp/cowsaytwo.txt');  }
     echo "<html><head><title>Cow Says ... thanks to https://pypi.org/project/cowsay</title><meta charset='UTF-8'/></head><body onload=\"if (window.parent) { if (parent.document.getElementById('outdrawing')) {  parent.document.getElementById('outdrawing').value=parent.setlenthree(" . str_replace("\n","",file_get_contents('/tmp/cowsayzn.txt')) . ",decodeURIComponent('" . urlencode(file_get_contents('/tmp/cowsay.txt')) . "').replace(/\+/g,' '),decodeURIComponent('" . urlencode(file_get_contents('/tmp/cowsaytwo.txt')) . "').replace(/\+/g,' ')); } } if (window.parent) { if (parent.document.getElementById('result')) {  parent.document.getElementById('result').value=parent.setlen(" . str_replace("\n","",file_get_contents('/tmp/cowsayzn.txt')) . ",decodeURIComponent('" . urlencode(file_get_contents('/tmp/cowsay.txt')) . "').replace(/\+/g,' ')); parent.document.getElementById('result').style.fontFamily='Courier New';  parent.document.getElementById('result').style.fontSize='8px';  parent.document.getElementById('result').title='Double click for more ... thanks to https://pypi.org/project/cowsay'; parent.document.getElementById('result').ondblclick=function(){ window.open('/cowsay.php','_blank','top=100,left=100,width=600,height=600'); };   }  }\"><textarea id=dta title='Double click for another Cow Says pearl of wisdom.' ondblclick=\"location.href='/cowsay.php';\" style='width:90%;height:90%;border:2px dotted red;font-size:8px;'>" . file_get_contents('/tmp/cowsaythree.txt') . "</textarea></body></html>";
     } else {
     exec('cowsay' . $suff . ' -t "' . str_replace('+',' ',urldecode($_POST['inwords'])) . '" > /tmp/cowsay.txt');
     echo "<html><head><title>Cow Says ... thanks to https://pypi.org/project/cowsay</title><meta charset='UTF-8'/></head><body onload=\"if (window.parent) { if (parent.document.getElementById('outdrawing')) {  parent.document.getElementById('outdrawing').value=parent.setlen(" . str_replace("\n","",file_get_contents('/tmp/cowsayzn.txt')) . ",decodeURIComponent('" . urlencode(file_get_contents('/tmp/cowsay.txt')) . "').replace(/\+/g,' ')); } } if (window.parent) { if (parent.document.getElementById('result')) {  parent.document.getElementById('result').value=parent.setlen(" . str_replace("\n","",file_get_contents('/tmp/cowsayzn.txt')) . ",decodeURIComponent('" . urlencode(file_get_contents('/tmp/cowsay.txt')) . "').replace(/\+/g,' ')); parent.document.getElementById('result').style.fontFamily='Courier New';  parent.document.getElementById('result').style.fontSize='8px';  parent.document.getElementById('result').title='Double click for more ... thanks to https://pypi.org/project/cowsay'; parent.document.getElementById('result').ondblclick=function(){ window.open('/cowsay.php','_blank','top=100,left=100,width=600,height=600'); };   }  }\"><textarea id=dta title='Double click for another Cow Says pearl of wisdom.' ondblclick=\"location.href='/cowsay.php';\" style='width:90%;height:90%;border:2px dotted red;font-size:8px;'>" . file_get_contents('/tmp/cowsay.txt') . "</textarea></body></html>";
     }
     exit;
   }
   
   echo "<html><head><title>Cow Says ... thanks to https://pypi.org/project/cowsay</title><meta charset='UTF-8'/>
<script type=text/javascript>
   var gnewwithwhat='';
   var aconto=null;
   var eighty=0;
   
  
  function rtrim(inzx) {
    var outzx=inzx;
    while ((' ' + outzx).slice(-1) == ' ' && outzx != '') {
     if (outzx != '') {
      if (('' + outzx.length) == '1') {
      outzx=='';
      } else {
      outzx=outzx.substring(0,eval(-1 + outzx.length));
      }
     }
    }
    return outzx;
  }

  function chkit(iois) {
       aconto = (iois.contentWindow || iois.contentDocument);
       if (aconto != null) {
         if (aconto.document) { aconto = aconto.document; }
       }       
   }
   
   function setlen(towhat, withwhat) {
     var newwithwhat=withwhat, onerec='';
     var minmaxlen=0, maxlen=0, irecs=0;
     var recs=withwhat.split(String.fromCharCode(13) + String.fromCharCode(10));
     for (irecs=0; irecs<recs.length; irecs++) {
        if (eval('' + recs[irecs].length) > maxlen) { maxlen=eval('' + recs[irecs].length);  }
        //if (eval('' + rtrim(recs[irecs]).length) > minmaxlen) { minmaxlen=eval('' + rtrim(recs[irecs]).length);  }
     }
     eighty=0;
     if (minmaxlen != 0) { maxlen=minmaxlen; }
     //alert('Maxlen=' + maxlen);
     document.getElementById('maxl').value='' + maxlen;
     newwithwhat='';
     while (eval('' + newwithwhat.length) < maxlen) {
       newwithwhat+=' ';
     }
     onerec=newwithwhat;
     newwithwhat='';
     for (irecs=0; irecs<recs.length; irecs++) {
        if (eval('' + recs[irecs].length) > maxlen) { 
          recs[irecs]=recs[irecs].substring(0,maxlen);
        }
        if (eval('' + recs[irecs].length) < maxlen) { 
          while (eval('' + recs[irecs].length) < maxlen) { 
            recs[irecs]+=' ';
          }
          newwithwhat+=recs[irecs] + String.fromCharCode(13) + String.fromCharCode(10);
        } else {
          newwithwhat+=recs[irecs] + String.fromCharCode(13) + String.fromCharCode(10);
        }
     }
     for (var jj=0; jj<100; jj++) {
        newwithwhat+=onerec + String.fromCharCode(13) + String.fromCharCode(10);
     }
     if (aconto.getElementById('dta')) {
       gnewwithwhat=newwithwhat;
       setTimeout(function(){ aconto.getElementById('dta').innerHTML=''; aconto.getElementById('dta').value=gnewwithwhat; }, 5000);
     }
     if (document.getElementById('outdrawing')) {
       document.getElementById('outdrawing').value=newwithwhat;
     }
     return newwithwhat;
   }

   function setlenthree(towhat, withwhat, three) {
     var newwithwhat=withwhat, onerec='';
     var minmaxlen=0, maxlen=0, irecs=0;
     var recs=[];
     
     if (withwhat.indexOf(String.fromCharCode(13) + String.fromCharCode(10)) != -1) {
       recs=withwhat.split(String.fromCharCode(13) + String.fromCharCode(10));
     } else if (withwhat.indexOf(String.fromCharCode(10) + String.fromCharCode(13)) != -1) {
       recs=withwhat.split(String.fromCharCode(10) + String.fromCharCode(13));
     } else if (withwhat.indexOf(String.fromCharCode(10)) != -1) {
       recs=withwhat.split(String.fromCharCode(10));
     } else if (withwhat.indexOf(String.fromCharCode(13)) != -1) {
       recs=withwhat.split(String.fromCharCode(13));
     } else {
       recs=[withwhat];
     } 
     //alert('' + recs[0].slice(-1).charCodeAt() + ' !' + recs[0] + '!');

     var recsextra=three.split(String.fromCharCode(10));
     for (irecs=0; irecs<recs.length; irecs++) {
        if (eval('' + recs[irecs].length) > maxlen) { maxlen=eval('' + recs[irecs].length);  }
        //if (eval('' + rtrim(recs[irecs]).length) > minmaxlen) { minmaxlen=eval('' + rtrim(recs[irecs]).length);  }
     }
     //alert('minmaxlen=' + minmaxlen + ' and maxlen=' + maxlen);
     if (minmaxlen != 0) { maxlen=minmaxlen; }
     eighty+=60;
     maxlen=eighty;
     document.getElementById('maxl').value='' + maxlen;
     newwithwhat='';
     while (eval('' + newwithwhat.length) < maxlen) {
       newwithwhat+=' ';
     }
     onerec=newwithwhat;
     newwithwhat='';
     for (irecs=0; irecs<recs.length; irecs++) {
        if (eval('' + recs[irecs].length) > maxlen) { 
          recs[irecs]=recs[irecs].substring(0,maxlen);
        }
        if (eval('' + recs[irecs].length) < maxlen) { 
          while (eval('' + recs[irecs].length) < maxlen) { 
            recs[irecs]+=' ';
          }
          if (eval('' + recsextra.length) > irecs) {
          newwithwhat+=recs[irecs] + recsextra[irecs] + String.fromCharCode(13) + String.fromCharCode(10);
          console.log('recS[' + irecs + ']=' + recs[irecs] + recsextra[irecs]);
          } else {
          newwithwhat+=recs[irecs] + String.fromCharCode(13) + String.fromCharCode(10);
          console.log('reCS[' + irecs + ']=' + recs[irecs]);
          }
        } else {
          if (eval('' + recsextra.length) > irecs) {
          newwithwhat+=recs[irecs] + recsextra[irecs] + String.fromCharCode(13) + String.fromCharCode(10);
          console.log('rEcs[' + irecs + ']=' + recs[irecs] + recsextra[irecs]);
          } else {
          newwithwhat+=recs[irecs] + String.fromCharCode(13) + String.fromCharCode(10);
          console.log('REcs[' + irecs + ']=' + recs[irecs]);
          }
        }
     }
     for (var jj=recs.length; jj<eval(recs.length + 100); jj++) {
        if (eval('' + recsextra.length) > jj) {
        newwithwhat+=onerec + recsextra[jj] + String.fromCharCode(13) + String.fromCharCode(10);
        } else {
        newwithwhat+=onerec + String.fromCharCode(13) + String.fromCharCode(10);
        }
     }
     if (aconto.getElementById('dta')) {
       gnewwithwhat=newwithwhat;
       //var woo=window.open('','_blank','top=10,left=10,width=500,height=500');
       //woo.document.write('<p>' + gnewwithwhat + '</p>');
       setTimeout(function(){ aconto.getElementById('dta').innerHTML=''; aconto.getElementById('dta').value=gnewwithwhat; }, 5000);
     } //else {
       //var woo=window.open('','_blank','top=10,left=310,width=500,height=500');
       //woo.document.write('<p>' + newwithwhat + '</p>');
     //}
     if (document.getElementById('outdrawing')) {
       document.getElementById('outdrawing').value=newwithwhat;
     }
     return newwithwhat;
   }

</script>  
</head><body><h1>Cow Says ...</h1><h3>RJM Programming - February, 2025</h3><h4>Thanks to <a target=_blank title='https://pypi.org/project/cowsay/' href='//pypi.org/project/cowsay/'>https://pypi.org/project/cowsay/</a></h4><form onsubmit=\"document.getElementById('myiframe').style.display='block'; return true;\" target=myiframe method=POST action='/cowsay.php'><input type=hidden name=maxl id=maxl value=0></input><textarea style=display:none; id=outdrawing name=outdrawing></textarea><textarea id=myta name=inwords></textarea><br><select name=chr>
<option value='cow'>Cow</option>
<option value='beavis'>Beavis</option>
<option value='cheese'>Cheese</option>
<option value='daemon'>Daemon</option>
<option value='dragon'>Dragon</option>
<option value='fox'>Fox</option>
<option value='ghostbusters'>Ghostbusters</option>
<option value='kitty'>Kitty</option>
<option value='meow'>Meow</option>
<option value='miki'>Miki</option>
<option value='milk'>Milk</option>
<option value='octopus'>Octopus</option>
<option value='pig'>Pig</option>
<option value='stegosaurus'>Stegosaurus</option>
<option value='stimpy'>Stimpy</option>
<option value='trex'>TRex</option>
<option value='turkey'>Turkey</option>
<option value='turtle'>Turtle</option>
<option value='tux'>Tux</option>
   </select><br><br><input type=submit value='Cow Says ...' style=background-color:yellow;></input>&nbsp;&nbsp;<input type=submit value='Cow Says ... and ...' style=background-color:orange; name=and></input></form><br><br><iframe onload=chkit(this); name=myiframe id=myiframe frameborder=0 style=display:none;width:100%;height:100%; src='/About_Us.html'></iframe></body></html>";
?>