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

$gbcol="0,0,255";
$gsra="";
$ifills='';
$cannotbeuser="";
$cannotbecontact="";
$inuser="";
$incontact="";
$userfrom="youllneverfindthis";
$userto="youllneverfindthis";
$contactfrom="youllneverfindthis";
$contactto="youllneverfindthis";

$userfromtwo="youllneverfindthis";
$usertotwo="youllneverfindthis";
$contactfromtwo="youllneverfindthis";
$contacttotwo="youllneverfindthis";


function server_remote_addr() {
    global $gbcol, $gsra;
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    $uas=explode('.', str_replace('::1','65.254.95.247',$rma)); // 65.254.93.32
    
    if (sizeof($uas) >= 3) {
      $gbcol='' . ($uas[0] % 256);
      $gbcol.=',' . ($uas[1] % 256); 
      $gbcol.=',' . (($uas[2] + $uas[(-1 + sizeof($uas))]) % 256); 
    }
    
    // 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;
    $gsra=str_replace(":", "_", $rma);
    return str_replace(":", "_", $rma);
}

   
   server_remote_addr();
   $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=[];
   $compip="";
   
   $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 (strpos($ind, ' data-owner="') !== false && strpos($ind, ' data-contact="') !== false) {
        $inuser=explode('"', explode(' data-owner="', $ind)[1])[0];
        $incontact=explode('"', explode(' data-contact="', $ind)[1])[0];
      }
      if (!file_exists('/tmp/select_palette.htm')) {
        //file_put_contents('./res.xxx', file_get_contents('./res.xxx') . "\n" . '1');
        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('./res.xxx', file_get_contents('./res.xxx') . "\n" . '2');
          file_put_contents('/tmp/select_palette.htm', $sod);
          if ($curcont == "") { $curcont=$sod; }
        } else {
          $curcont=$prevcont;
          if (isset($_POST['me'])) {
             $waslines=explode('<svg name="', $curcont);
             $findings=explode(',', str_replace(';',',',str_replace('+',' ',urldecode($_POST['me']))));
             for ($j=1; $j<sizeof($waslines); $j++) {
              $isok=false;
              for ($jj=0; $jj<sizeof($findings); $jj++) {
                if (trim($findings[$jj]) != '') {
                  if (strpos(strtolower($thisline), '="' . strtolower($findings[$jj]) . '"') !== false) {
                    $isok=true;
                  }
                }
              }
              
              if ($isok && ($inuser != "" || $incontact != "") && strpos(str_replace('+',' ',urldecode($_POST['me'])), ',') !== false && strpos($waslines[$j], ' data-owner="') !== false && strpos($waslines[$j], ' data-contact="') !== false) {
                $theuser=explode('"', explode(' data-owner="', $waslines[$j])[1])[0];
                $thecontact=explode('"', explode(' data-contact="', $waslines[$j])[1])[0];
                $compip="";
                $compuser="";
                $compcontact="";
                if (strpos($thisline, ' data-ip="') !== false && strpos($thisline, ' data-owner="') !== false && strpos($thisline, ' data-contact="') !== false) {
                  $compip=explode('"', explode(' data-ip="', $thisline)[1])[0];
                  $compuser=explode('"', explode(' data-owner="', $thisline)[1])[0];
                  $compcontact=explode('"', explode(' data-contact="', $thisline)[1])[0];
                  if ($compip == $gsra) { // same IP as this
                    if ($compuser != $theuser || $compcontact != $thecontact) {  // needs a change
                    if ($cannotbeuser == "") {
                      $cannotbeuser=";" . $theuser . ',';
                    } else {
                      $cannotbeuser="" . $theuser . ',';
                    }
                    if ($cannotbecontact == "") {
                      $cannotbecontact=";" . $compcontact . ',';
                    } else {
                      $cannotbecontact="" . $compcontact . ',';
                    }
                    }
                  } else if ($compuser == $theuser && $compcontact == $thecontact) {   // needs a change
                    if ($cannotbeuser == "") {
                      $cannotbeuser=";" . $theuser . ';';
                    } else {
                      $cannotbeuser="" . $theuser . ';';
                    }
                    if ($cannotbecontact == "") {
                      $cannotbecontact=";" . $thecontact . ';';
                    } else {
                      $cannotbecontact="" . $thecontact . ';';
                    }
                  } else {
                    $compip=$compip;
                  }
                }
                
              }
              
             }
          }
          
          if ($cannotbeuser != "" || $cannotbecontact != "") { 
            $fixed=false;
            $newinuser="";
            $newincontact="";
            $mm=(sizeof($findings) / 2);
            for ($kk=2; $kk<sizeof($findings); $kk++) {
              if ($findings[$kk] != '' && !$fixed) {
                if (strpos(str_replace(';',',',$cannotbeuser)  , ',' . $findings[$kk] . ',') === false && strpos(str_replace(';',',',$cannotbecontact)  , ',' . $findings[$kk] . ',') === false) {
                  $fixed=true;
                  if ((-2 + $kk) >= $mm) {  // is contact
                    $newincontact=$findings[$kk];
                    $newinuser=$findings[($kk - $mm)];
                    $userfrom=' data-owner="' . $inuser . '"';
                    $userto=' data-owner="' . $newinuser . '"';
                    $contactfrom=' data-contact="' . $incontact . '"';
                    $contactto=' data-contact="' . $newincontact . '"';

                    $userfromtwo='>' . $inuser . '[';
                    $usertotwo='>' . $newinuser . '[';
                    $contactfromtwo='[' . $incontact . ']';
                    $contacttotwo='[' . $newincontact . ']';
                    
                    $ifills.=" parent.document.getElementById('iwindowuser').value='" . $newinuser . "'; ";
                    $ifills.=" parent.document.getElementById('iwindowcontact').value='" . $newincontact . "'; ";

                  } else { // is user
                    $newinuser=$findings[$kk];
                    if (sizeof($findings) > ($kk + $mm)) {
                      $newincontactr=$findings[($kk + $mm)];
                    }
                    $userfrom=' data-owner="' . $inuser . '"';
                    $userto=' data-owner="' . $newinuser . '"';
                    $contactfrom=' data-contact="' . $incontact . '"';
                    $contactto=' data-contact="' . $newincontact . '"';

                    $userfromtwo='>' . $inuser . '[';
                    $usertotwo='>' . $newinuser . '[';
                    $contactfromtwo='[' . $incontact . ']';
                    $contacttotwo='[' . $newincontact . ']';
                    
                    $ifills.=" parent.document.getElementById('iwindowuser').value='" . $newinuser . "'; ";
                    $ifills.=" parent.document.getElementById('iwindowcontact').value='" . $newincontact . "'; ";

                  }
                }
              }
            }
          }
          
        }
      }
      
      for ($i=1; $i<sizeof($lines); $i++) {
        $thisline=str_replace($userfromtwo,$usertotwo,str_replace($contactfromtwo,$contacttotwo,str_replace($userfrom,$userto,str_replace($contactfrom,$contactto,'<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);
      if (!isset($_POST['me'])) {
      echo '<html><body></body></html>';
      exit;
      } else {
      $_GET['me']=$_POST['me'];
      $_GET['extract']='y';
      }
      
   } 
   
   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('./res.xxx', file_get_contents('./res.xxx') . "\n" . '3');
          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=" ' . $ifills . ' if (encodeURIComponent(parent.document.getElementById(' . "'publicreport'" . ').innerHTML).length != ' . $vslen . ') {  parent.document.getElementById(' . "'publicreport'" . ').innerHTML=decodeURIComponent(' . "'" . str_replace('+','%20',urlencode($prevcont)) . "'" . '); } "></body></html>';
            exit;
        }
        
      }
      
   } else {
      echo '<html><body onload=" ' . $ifills . ' if (parent.document.getElementById(' . "'myip'" . ')) { parent.document.getElementById(' . "'myip'" . ').innerHTML=' . "'" . server_remote_addr() . "'" . ';  parent.bcolis(' . "'" . $gbcol . "'" . ');  }"></body></html>';
      exit;
      
   }

?>