<?php
// prepend.php
// RJM Programming
// October, 2025
// Message Board helper to talk_about_it.php

function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    //$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if (1 == 2) {
    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;
    }
    return str_replace(':','_',str_replace('.','_',$rma));
}

function dtrework($bigixnts) {
  $ixnts=$bigixnts;
  if (strpos(strtolower($bigixnts), ' says at ') !== false) {
    $ixnts=explode(' ...',explode(' says at ', strtolower($bigixnts))[1])[0];
  }
  $slbits=explode('/', $ixnts);
  $scbits=explode(':', $ixnts);
  return explode(' ',$slbits[2])[0] . $slbits[1] . $slbits[0] . explode(':', explode(' ',$slbits[2])[1])[0] . explode(' ',$scbits[1])[0];
}

$myip=server_remote_addr();
$regarding=$myip;
if (isset($_POST['nickname'])) {
  if (strlen($_POST['nickname']) > 0) {
    $regarding=str_replace('+', ' ', urldecode($_POST['nickname']));
  }
} else if (isset($_GET['nickname'])) {
  if (strlen($_GET['nickname']) > 0) {
    $regarding=str_replace('+', ' ', urldecode($_GET['nickname']));
  }
}

$tabcol="yellow";
$groupis="";
$grpsuff="";
$grouptb="";
$ntry=0;
$sback="";
$isfilter=false;
$filterarr=[];
$recsarr=[];
date_default_timezone_set("UTC");
$localtime_assoc = localtime(time(), false);
$twoday=substr(('0' . $localtime_assoc[3]), -2, 2);
$twomon=substr(('0' . (1 + $localtime_assoc[4])), -2, 2);
$twohour=substr(('0' . $localtime_assoc[2]), -2, 2);
$twomin=substr(('0' . $localtime_assoc[1]), -2, 2);

if (isset($_POST['groupis'])) {
  $tabcol="orange";
  $groupis=str_replace('+', ' ', urldecode($_POST['groupis']));
  $grouptb="<input type=hidden id=groupis name=groupis value='" . $groupis . "'></input>";
  $regarding.=" group is " . $groupis;
  $grpsuff="_" . base64_encode($groupis);
  if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday_" . $ntry . ".txt")) {
   $sback=shell_exec('fgrep -l "' . $groupis . '" ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday_*.txt");
  }
  if (strpos($sback, "talktoday_") === false) {
  while (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday_" . $ntry . ".txt")) {
   $ntry++;
  }
  $grpsuff="_" . $ntry;
  file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday" . $grpsuff . ".txt", $groupis . "\n");
  } else {
   $grpsuff=explode('.', explode(':', explode("talktoday", $sback)[1])[0])[0];
  }
} else if (isset($_GET['groupis'])) {
  $tabcol="orange";
  $groupis=str_replace('+', ' ', urldecode($_GET['groupis']));
  $grouptb="<input type=hidden id=groupis name=groupis value='" . $groupis . "'></input>";
  $regarding.=" group is " . $groupis;
  $grpsuff="_" . base64_encode($groupis);
  if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday_" . $ntry . ".txt")) {
   $sback=shell_exec('fgrep -l "' . $groupis . '" ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday_*.txt");
  }
  if (strpos($sback, "talktoday_") === false) {
  while (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday_" . $ntry . ".txt")) {
   $ntry++;
  }
  $grpsuff="_" . $ntry;
  file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday" . $grpsuff . ".txt", $groupis . "\n");
  } else {
   $grpsuff=explode('.', explode(':', explode("talktoday", $sback)[1])[0])[0];
  }
}

if (isset($_POST['filteris'])) {
  if ($tabcol == "orange") {
  $tabcol="lightgreen";
  } else {
  $tabcol="lightblue";
  }
  $isfilter=true;
  $filterarr=explode(';', str_replace('+',' ',urldecode($_POST['filteris'])));
} else if (isset($_GET['filteris'])) {
  if ($tabcol == "orange") {
  $tabcol="lightgreen";
  } else {
  $tabcol="lightblue";
  }
  $isfilter=true;
  $filterarr=explode(';', str_replace('+',' ',urldecode($_GET['filteris'])));
}

$prerel="";
if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday" . $grpsuff . ".txt")) {
  $prerel=str_replace("~~", "\n", file_get_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday" . $grpsuff . ".txt"));
}
if (isset($_POST['msg'])) {
  if (strlen($_POST['msg']) > 0) {
   $recpref="";
   if (str_replace("~~", "\n", str_replace('+', ' ', urldecode($_POST['msg']))) != str_replace('+', ' ', urldecode($_POST['msg']))) {
     $recpref="\n";
   }
   if (isset($_POST['nickname'])) {
    $regarding=str_replace('+', ' ', urldecode($_POST['nickname']));
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talk_today_" . $myip . ".txt", str_replace('+', ' ', urldecode($_POST['nickname'])) . " says at " . $twoday . '/' . $twomon . '/' . (1900 + $localtime_assoc[5]) . ' ' . $twohour . ':' . $twomin . " GMT ... " . $recpref . str_replace("~~", "\n", str_replace('+', ' ', urldecode($_POST['msg']))));
   } else {
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talk_today_" . $myip . ".txt", $myip . " says at " . $twoday . '/' . $twomon . '/' . (1900 + $localtime_assoc[5]) . ' ' . $twohour . ':' . $twomin . " GMT ... " . $recpref . str_replace("~~", "\n", str_replace('+', ' ', urldecode($_POST['msg']))));
   }
  }
}
$tahtml="<textarea style=width:95%;height:500px; id=myta value=''></textarea>";
$reltalk="";
$it = new GlobIterator($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talk_today*.txt");
$last2mis = 9020;
foreach ( $it as $file ) {
    $reltalk.=file_get_contents($file) . "\n";
    if ((time() - $file->getMTime()) < $last2mis) {
      unlink($file);
    }
}

if ($reltalk != "") {
  $prerel.="\n" . $reltalk . "\n";
}

if ($isfilter) {
  $recsarr=explode("\n", $prerel);
  $recisokay=false;
  $ist=0;
  if ($groupis != "") {
  $ist=1;
  if (strpos($prerel, $groupis) !== false) {
    $prerel=substr($prerel, strlen($groupis)) . "\n\n";
  } else {
    $prerel="";
  }
  } else {
    $prerel="";
  }

  for ($ih=0; $ih<sizeof($recsarr); $ih++) {
    $recisokay=true;
    $numis=0;
    $unitis='';
    $versuspart='';
    for ($ib=$ist; $ib<sizeof($filterarr); $ib++) {
      if (strpos(strtolower($recsarr[$ih]), ' says at ') === false) {
        $recisokay=false;
      } else if (trim($filterarr[$ib]) == '') {
        $recisokay=$recisokay;
      } else if (strpos(strtolower($filterarr[$ib]), 'last ') !== false) {
        //$versuspart=dtrework(explode(' ...',explode(strtolower($recsarr[$ih]), ' says at ')[1])[0]);
        $versuspart=dtrework($recsarr[$ih]);
        $numis=explode(' ',explode('last ', strtolower($filterarr[$ib]))[1])[0];
        if (trim($numis) != '' && substr(($numis . ' '),0,1) >= '0' && substr(($numis . ' '),0,1) <= '9') {
         $unitis=strtolower(explode(' ', trim($filterarr[$ib]))[-1 + sizeof(explode(' ', trim($filterarr[$ib])))]);
         if ($unitis == 'm' || strpos($unitis, 'minute') !== false) {
          $nlocaltime_assoc = localtime(((-60 * $numis) + time()), false);
         } else if ($unitis == 's' || strpos($unitis, 'second') !== false) {
          $nlocaltime_assoc = localtime(((-1 * $numis) + time()), false);
         } else if ($unitis == 'h' || strpos($unitis, 'hour') !== false) {
          $nlocaltime_assoc = localtime(((-3600 * $numis) + time()), false);
         } else if ($unitis == 'd' || strpos($unitis, 'day') !== false) {
          $nlocaltime_assoc = localtime(((-86400 * $numis) + time()), false);
         } else if ($unitis == 'y' || strpos($unitis, 'year') !== false) {
          $nlocaltime_assoc = localtime(((-31536 * $numis) + time()), false);
         } else if (strpos($unitis, 'month') !== false) {
          $nlocaltime_assoc = localtime(((-2628 * $numis) + time()), false);
         } else if ($numis == $unitis) {
          $nlocaltime_assoc = localtime(((-60 * $numis) + time()), false);
         } else {
          $unitis='';
         }
         if ($unitis != '') {
          $ntwoday=substr(('0' . $nlocaltime_assoc[3]), -2, 2);
          $ntwomon=substr(('0' . (1 + $nlocaltime_assoc[4])), -2, 2);
          $ntwohour=substr(('0' . $nlocaltime_assoc[2]), -2, 2);
          $ntwomin=substr(('0' . $nlocaltime_assoc[1]), -2, 2);
          if ($versuspart < dtrework($ntwoday . '/' . $ntwomon . '/' . (1900 + $nlocaltime_assoc[5]) . ' ' . $ntwohour . ':' . $ntwomin)) {
            $recisokay=false;
          }
         }
        }
      } else if (substr($filterarr[$ib], 0, 1) != '/' && (substr($filterarr[$ib], 0, 1) == '^' || substr($filterarr[$ib], -1, 1) == '$' || strpos($filterarr[$ib], '*') !== false || strpos($filterarr[$ib], '?') !== false || strpos($filterarr[$ib], ']') !== false)) {
        if (substr($filterarr[$ib], 0, 1) == '^' && substr($filterarr[$ib], -1, 1) == '$') {
          //echo "pattern is " . 'ynft' . substr($filterarr[$ib], 1, (-2 + strlen($filterarr[$ib]))) . 'ynft' . " regarding ynft" . $recsarr[$ih] . 'ynft';
          if (!fnmatch('ynft' . substr($filterarr[$ib], 1, (-2 + strlen($filterarr[$ib]))) . 'ynft', 'ynft' . $recsarr[$ih] . 'ynft')) {
          //if (strpos('~@#' . $recsarr[$ih] . '~@#', '~@#' . substr($filterarr[$ib], 1, (-2 + strlen($filterarr[$ib]))) . '~@#') === false) {
               $recisokay=false;
          }
        } else if (substr($filterarr[$ib], 0, 1) == '^') {
          //echo "pattern is " . '' . substr($filterarr[$ib], 1, (-1 + strlen($filterarr[$ib]))) . (substr($filterarr[$ib],-1,1) != '*' ? '*' : '') . " regarding " . $recsarr[$ih] . '';
          if (!fnmatch('' . substr($filterarr[$ib], 1, (-1 + strlen($filterarr[$ib]))) . (substr($filterarr[$ib],-1,1) != '*' ? '*' : ''), '' . $recsarr[$ih] . '')) {
          //if (strpos(substr($recsarr[$ih],0,(-1 + strlen($filterarr[$ib]))), substr($filterarr[$ib], 1)) === false) {
               $recisokay=false;
          }
        } else if (substr($filterarr[$ib], -1, 1) == '$') {
          //echo "pattern is " . (substr($filterarr[$ib],0,1) != '*' ? '*' : '') . substr($filterarr[$ib], 0, (-1 + strlen($filterarr[$ib]))) . 'ynft' . " regarding ynft" . $recsarr[$ih] . 'ynft';
          if (!fnmatch((substr($filterarr[$ib],0,1) != '*' ? '*' : '') . substr($filterarr[$ib], 0, (-1 + strlen($filterarr[$ib]))) . 'ynft', '' . $recsarr[$ih] . 'ynft')) {
          //if (strpos($recsarr[$ih] . '~@#', substr($filterarr[$ib], 0, (-1 + strlen($filterarr[$ib]))) . '~@#') === false) {
               $recisokay=false;
          }
        } else {
          if (!fnmatch((substr($filterarr[$ib],0,1) != '*' ? '*' : '') . $filterarr[$ib] . (substr($filterarr[$ib],-1,1) != '*' ? '*' : ''), $recsarr[$ih])) {
               $recisokay=false;
          }
        }
      } else if (substr($filterarr[$ib], 0, 1) == '/' && (substr($filterarr[$ib], 0, 2) == '/^' || substr($filterarr[$ib], -2, 2) == '$/' || substr($filterarr[$ib], 0, 1) == substr($filterarr[$ib], -1, 1))) {
        if (substr($filterarr[$ib], 0, 2) == '/^' && substr($filterarr[$ib], -2, 2) == '$/') {
          if (!fnmatch('ynft' . str_replace('/^','',str_replace('$/','',$filterarr[$ib])) . 'ynft', 'ynft' . $recsarr[$ih] . 'ynft')) {
               $recisokay=false;
          }
        } else if (substr($filterarr[$ib], 0, 2) == '/^') {
          //echo "pattern is " . 'ynft' . str_replace('/','',str_replace('/^','',$filterarr[$ib])) . (substr($filterarr[$ib],-2,1) != '*' ? '*' : '') . " regarding ynft" . $recsarr[$ih];
          if (!fnmatch('ynft' . str_replace('/','',str_replace('/^','',$filterarr[$ib])) . (substr($filterarr[$ib],-2,1) != '*' ? '*' : ''), 'ynft' . $recsarr[$ih])) {
               $recisokay=false;
               //echo "\noopS\n";
          }
        } else if (substr($filterarr[$ib], -2, 2) == '$/') {
          //echo "pattern is " . (substr($filterarr[$ib],1,1) != '*' ? '*' : '') . str_replace('/','',str_replace('$/','',$filterarr[$ib])) . 'ynft' . " regarding ynft" . $recsarr[$ih] . "ynft";
          if (!fnmatch((substr($filterarr[$ib],1,1) != '*' ? '*' : '') . str_replace('/','',str_replace('$/','',$filterarr[$ib])) . 'ynft', $recsarr[$ih] . 'ynft')) {
               $recisokay=false;
          }
        } else {
          //echo "pattern is " . (substr($filterarr[$ib],1,1) != '*' ? '*' : '') . substr($filterarr[$ib], 1, (-2 + strlen($filterarr[$ib]))) . " regarding " . $recsarr[$ih];
          if (!fnmatch((substr($filterarr[$ib],1,1) != '*' ? '*' : '') . substr($filterarr[$ib], 1, (-2 + strlen($filterarr[$ib]))) . (substr($filterarr[$ib],-2,1) != '*' ? '*' : ''), $recsarr[$ih])) {
               $recisokay=false;
          //echo "\n" . "oops\n";
          }
        }
      }
      
      
    }
    if ($recisokay) {
      $prerel.=$recsarr[$ih] . "\n\n";
    }
  }
}

$bigprerel=$prerel;
if ($groupis != "") {
  if (strpos($prerel, $groupis) !== false) {
    $prerel=substr($prerel, strlen($groupis));
  }
}

if ($prerel != "") {
 if ($reltalk != "") {
  $tahtml="<textarea data_refresh=y data-name=myta" . $myip . " style=background-color:" . $tabcol . ";width:95%;height:400px; id=myta value=''>" . $prerel . "</textarea>";
  file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "talktoday" . $grpsuff . ".txt", $bigprerel);
 } else {
  $tahtml="<textarea data-name=myta" . $myip . " style=background-color:" . $tabcol . ";width:95%;height:400px; id=myta value=''>" . $prerel . "</textarea>";
 }
}

?>