<?php
// broadcast_listen.php
// RJM Programming
// October, 2015
// Help out broadcast_listen.html to achieve broadcast wording saves to broadcast_list.html

function short_server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // 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;
    return str_replace(":", "_", $rma);
}

function any_thing_for_me() {
  $retval="";
  return $retval;
}


function some_thing_for_somebody($inthingtowrite, $toperson, $subj, $ourdt) {
  $retval="";
  return $retval;
}

$wording="";
$dt="";
$identity="";
$subject="Broadcast";

if (isset($_POST['listen']) || isset($_GET['listen'])) {
  echo any_thing_for_me();
  exit;
}

if (isset($_POST['write']) || isset($_GET['write'])) {
  $ourdtb="";
  $oursubject="";
  $to="";
  $thingtowrite="";
  if (isset($_POST['write'])) $thingtowrite=urldecode($_POST['write']);
  if (isset($_GET['write'])) $thingtowrite=urldecode($_GET['write']);
  if (isset($_POST['emailname'])) $to=urldecode($_POST['emailname']);
  if (isset($_GET['emailname'])) $to=urldecode($_GET['emailname']);
  if (isset($_POST['to'])) $to=urldecode($_POST['to']);
  if (isset($_GET['to'])) $to=urldecode($_GET['to']);
  if (isset($_POST['subject'])) $oursubject=urldecode($_POST['subject']);
  if (isset($_GET['subject'])) $oursubject=urldecode($_GET['subject']);
  if (isset($_POST['datetimebit'])) $ourdtb=urldecode($_POST['datetimebit']);
  if (isset($_GET['datetimebit'])) $ourdtb=urldecode($_GET['datetimebit']);
  echo some_thing_for_somebody($thingtowrite, $to, $oursubject, $ourdtb);
  exit;
}

if (file_exists("broadcast_list.html")) {
  $contents=@file_get_contents("broadcast_list.html");
  if (!file_exists("broadcast_list.htm")) file_put_contents("broadcast_list.htm", $contents);
  if (strpos($contents, "</select>") !== false) {
    if (isset($_POST['words'])) {
      $wording=str_replace("</select>", "", str_replace("<br>", "\\n", str_replace("\n", "\\n", urldecode($_POST['words']))));
    } else if (strlen($_SERVER['QUERY_STRING']) == 0) { // first call ... check mailbox existance
      if (!file_exists("mailbox/mb_in_" . short_server_remote_addr() . ".html")) {
        file_put_contents("mailbox/mb_in_" . short_server_remote_addr() . ".html", "");
      }
      if (!file_exists("mailbox/mb_out_" . short_server_remote_addr() . ".html")) {
        file_put_contents("mailbox/mb_out_" . short_server_remote_addr() . ".html", "");
      }
      echo '<html><body onload=" var huh=parent.document.body.innerHTML; if (huh.indexOf(' . "'" . 'mailbox/' . "'" . ') == -1) { parent.document.getElementById(' . "'" . 'mailinout' . "'" . ').innerHTML=' . "'" . '<iframe onload=check_mail_in(this) style=display:none id=inbox src=mailbox/mb_in_' . short_server_remote_addr() . '.html title=inbox></iframe><iframe onload=check_mail_out(this) style=display:none id=outbox src=mailbox/mb_out_' . short_server_remote_addr() . '.html title=outbox></iframe>' . "'" . '; } "></body></html>';
    } else if (strpos($_SERVER['QUERY_STRING'], "rnum=-1") !== false) { // subsequent call ... clear inbox
      file_put_contents("mailbox/mb_in_" . short_server_remote_addr() . ".html", "");
      echo "";
    } else if (strpos($_SERVER['QUERY_STRING'], "rnum=-2") !== false) { // subsequent call ... clear outbox
      file_put_contents("mailbox/mb_out_" . short_server_remote_addr() . ".html", "");
      echo "";
    } else if (isset($_GET['words'])) {
      $wording=str_replace("</select>", "", str_replace("<br>", "\\n", str_replace("\n", "\\n", urldecode($_GET['words']))));
    }
    if (strlen($wording) > 0) {
     if (isset($_POST['subject'])) {
      $subject=str_replace("</select>", "", urldecode($_POST['subject']));
     } else if (isset($_GET['datetimebit'])) {
      $subject=str_replace("</select>", "", urldecode($_GET['subject']));
     }
     if (isset($_POST['datetimebit'])) {
      $dt=str_replace("</select>", "", urldecode($_POST['datetimebit']));
     } else if (isset($_GET['datetimebit'])) {
      $dt=str_replace("</select>", "", urldecode($_GET['datetimebit']));
     }
     if ($dt == " @ ") $dt="";
     if (isset($_POST['emailname'])) {
      $identity=str_replace("</select>", "", urldecode($_POST['emailname']));
      if ($subject == "Broadcast") {
        if ($identity != "") $subject .= " by " . $identity;
        if ($dt != "") $subject .= " at " . $dt;
      }
     } else if (isset($_GET['emailname'])) {
      $identity=str_replace("</select>", "", urldecode($_GET['emailname']));
      if ($subject == "Broadcast") {
        if ($identity != "") $subject .= " by " . $identity;
        if ($dt != "") $subject .= " at " . $dt;
      }
     }
     $contents=str_replace('</select>', '<option title="' . $identity . $dt . '" value="' . $wording . '">' . $subject . '</option>' . "\n</select>", $contents);
     file_put_contents("broadcast_list.html", $contents);
     echo '<html><body onload=" parent.document.getElementById(' . "'" . 'bstatus' . "'" . ').innerHTML=' . "'" . $subject . ' was broadcast to the listeners.' . "';" . '"></body></html>';
    }
  }
}

?>
