<?php
// select_palette.php
// RJM Programming
// May, 2025


   $sod='<svg name="20250516000000" data-public="y" data-contact="" data-owner="" data-othercontact="" data-otherowner="" data-dts="20250516000000" onmousedown=" event.stopPropagation(); event.preventDefault(); preadd=0; setTimeout(preaddcontents,2000); " ontouchdown=" event.stopPropagation(); event.preventDefault();  preadd=0; setTimeout(preaddcontents,2000); " id="20250516000000" ontouchend="treg(this);" oncontextmenu="treg(this);" xmlns="http://www.w3.org/2000/svg" width="1380" height="96" viewport="0 0 100 100" style="border-top-left-radius:35px;border-bottom-right-radius:35px;border-top-right-radius:35px;background-color:rgba(0,0,255,0.3);fill:black;font-family:Verdana;font-size:17px;"><title>Welcome to Bulletin Board dated 20250516 ...</title><text stroke="blue" x="5%" y="90%" xml:space="preserve">Welcome to Bulletin Board dated 20250516 ...</text></svg><br><br><br>';

   $prevcont="";
   $curcont="";
   $curgmt=gmdate("Ymd");
   $curdstamp=gmdate("l jS \of F Y");
   $newlines=[];
   $findings=[];
   
   $sod=str_replace('20250516', $curgmt, str_replace(' 20250516 ', ' ' . $curdstamp . ' 00:00:00 AM GMT ', $sod));

   if (isset($_POST['indata'])) {
      $ind=str_replace('+',' ',urldecode($_POST['indata']));
      $lines=explode('<svg name="', $ind);
      if (!file_exists('/tmp/select_palette.htm')) {
        file_put_contents('/tmp/select_palette.htm', $sod);
        if ($curcont == "") { $curcont=$sod; }
      } else {
        $prevcont=file_get_contents('/tmp/select_palette.htm');
        if (strpos($prevcont, '<svg name="' . $curgmt) === false) {
          file_put_contents('/tmp/select_palette.htm', $sod);
          if ($curcont == "") { $curcont=$sod; }
        } else {
          $curcont=$prevcont;
        }
      }
      
      for ($i=1; $i<sizeof($lines); $i++) {
        $thisline='<svg name="' . explode('</svg>', $lines[$i])[0] . '</svg><br><br><br>';
        if (strpos($thisline, $curgmt) !== false && strpos($thisline, ' data-public="n"') === false && (strpos($thisline, ' data-owner="') !== false && strpos($thisline, ' data-owner=""') === false) || (strpos($thisline, ' data-contact="') !== false && strpos($thisline, ' data-contact=""') === false)) {
          if (strpos($curcont, $thisline) === false) {
            $curcont.=$thisline;
          }
        }
      }
      file_put_contents('/tmp/select_palette.htm', $curcont);
      echo '<html><body></body></html>';
      exit;
      
   } else if (isset($_GET['extract'])) {
      if (file_exists('/tmp/select_palette.htm')) {
        $prevcont=file_get_contents('/tmp/select_palette.htm');
        if (strpos($prevcont, '<svg name="' . $curgmt) === false) {
          file_put_contents('/tmp/select_palette.htm', $sod);
          if ($curcont == "") { $curcont=$sod; }
        }
        $lines=explode('<svg name="', $prevcont);
        rsort($lines);

        if (!isset($_GET['owner']) && isset($_GET['me']) && !isset($_GET['contact'])) {
            $findings=explode(',', str_replace(';',',',str_replace('+',' ',urldecode($_GET['me']))));
        }
        
        if (!isset($_GET['owner']) && !isset($_GET['me']) && !isset($_GET['contact'])) {
            $prevcont="";
            for ($i=0; $i<sizeof($lines); $i++) {
              $thisline='<svg name="' . explode('</svg>', $lines[$i])[0] . '</svg><br><br><br>';
              if (strpos($thisline, $curgmt) !== false) {
                $prevcont.=$thisline;
              }
            }
            $vslen=strlen(str_replace('+','%20',urlencode($prevcont)));
            echo '<html><body onload=" if (encodeURIComponent(parent.document.getElementById(' . "'publicreport'" . ').innerHTML).length != ' . $vslen . ') { parent.document.getElementById(' . "'publicreport'" . ').innerHTML=decodeURIComponent(' . "'" . str_replace('+','%20',urlencode($prevcont)) . "'" . '); } "></body></html>';
            exit;
        } else {
            $prevcont="";
            for ($i=0; $i<sizeof($lines); $i++) {
              $thisline='<svg name="' . explode('</svg>', $lines[$i])[0] . '</svg><br><br><br>';
              if (strpos($thisline, $curgmt) !== false) {
              $isok=true;
              if (strpos($thisline, '000000"') !== false) {
              $isok=true;
              } else if (sizeof($findings) > 0) {
              $isok=false;
              for ($jj=0; $jj<sizeof($findings); $jj++) {
                if (trim($findings[$jj]) != '') {
                  if (strpos(strtolower($thisline), '="' . strtolower($findings[$jj]) . '"') !== false) {
                    $isok=true;
                  }
                }
              }
              } else {
              if (isset($_GET['owner']) && strpos(strtolower($thisline), ' data-owner="' . strtolower(str_replace('+',' ',urldecode($_GET['owner'])) . '"')) === false) { 
                $isok=false;  
              }
              if (isset($_GET['contact']) && strpos(strtolower($thisline), ' data-contact="' . strtolower(str_replace('+',' ',urldecode($_GET['contact'])) . '"')) === false) { 
                $isok=false;  
              }
              if (isset($_GET['me']) && strpos(strtolower($thisline), strtolower('="' . str_replace('+',' ',urldecode($_GET['me'])) . '"')) === false) { 
                $isok=false;  
              } else if (isset($_GET['me'])) {
                $isok=true;
              }
              }
              if ($isok) { $prevcont.=$thisline; }
              }
            }
            $vslen=strlen(str_replace('+','%20',urlencode($prevcont)));
            echo '<html><body onload=" if (encodeURIComponent(parent.document.getElementById(' . "'publicreport'" . ').innerHTML).length != ' . $vslen . ') {  parent.document.getElementById(' . "'publicreport'" . ').innerHTML=decodeURIComponent(' . "'" . str_replace('+','%20',urlencode($prevcont)) . "'" . '); } "></body></html>';
            exit;
        }
        
      }
      
   }

?>