<?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";

$liveflag="";
$wasis=" was ";
if (isset($_POST['liveflag'])) $liveflag=urldecode($_POST['liveflag']);
if (isset($_GET['liveflag'])) $liveflag=urldecode($_GET['liveflag']);
if ($liveflag != "") $wasis=" is being ";

$remotemailbox="";
$mailboxout="";
$lwording="";

if (isset($_POST['lregardingmailbox']) || isset($_GET['lregardingmailbox'])) {
  if (isset($_POST['lregardingmailbox'])) $remotemailbox=urldecode($_POST['lregardingmailbox']);
  if (isset($_GET['lregardingmailbox'])) $remotemailbox=urldecode($_GET['lregardingmailbox']);
  if ($remotemailbox != "") {
    $mailboxout="mailbox/mb_out_" . short_server_remote_addr() . ".html";
    if (isset($_POST['lwords']) || isset($_GET['lwords'])) {
     if (isset($_POST['lwords'])) $lwording=urldecode($_POST['lwords']);
     if (isset($_GET['lwords'])) $lwording=urldecode($_GET['lwords']);
     if ($lwording != "") {
        $ourdtb="";
        $oursubject="";
        $bitsbefore="";
        $to="";
        if (isset($_POST['lsubject'])) $oursubject=urldecode($_POST['lsubject']);
        if (isset($_GET['lsubject'])) $oursubject=urldecode($_GET['lsubject']);
        if (isset($_POST['lemailname'])) $to=urldecode($_POST['lemailname']);
        if (isset($_GET['lemailname'])) $to=urldecode($_GET['lemailname']);
        if (isset($_POST['ldatetimebit'])) $ourdtb=urldecode($_POST['ldatetimebit']);
        if (isset($_GET['ldatetimebit'])) $ourdtb=urldecode($_GET['ldatetimebit']);
        if ($to != "") {
          $bitsbefore.="From: " . $to . "\n";
        } 
        if ($oursubject != "") {
          $bitsbefore.=$oursubject . "\n";
        } 
        if ($ourdtb != "") {
          $bitsbefore.="At: " . str_replace(" @ ","",$ourdtb) . "\n";
        } 
        file_put_contents($remotemailbox, $bitsbefore . $lwording);
        file_put_contents($mailboxout, $lwording);
     }
    }
  }
  exit;
}

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/mb_' . "'" . ') == -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;
      }
     }
     $plus=" (live)";
     $bits=explode('>' . $subject . $plus . '</option>', $contents);
     if (sizeof($bits) == 1) {
       $plus="";
       $bits=explode('>' . $subject . $plus . '</option>', $contents);
     }
     if (sizeof($bits) > 1) {
     if (strpos($bits[-1 + sizeof($bits)], "</option>") === false) {
     $prebits=explode("<option ", $bits[-2 + sizeof($bits)]);
     if ($wording == " ") {
     $contents=str_replace('<option ' . $prebits[-1 + sizeof($prebits)] . '>' . $subject . $plus . '</option>', '', $contents);
     } else {
     $contents=str_replace('<option ' . $prebits[-1 + sizeof($prebits)] . '>' . $subject . $plus . '</option>', '<option title="' . $identity . $dt . '" value="' . $wording . '">' . $subject . $liveflag . '</option>', $contents);
     }
     } else {
     $contents=str_replace('</select>', '<option title="' . $identity . $dt . '" value="' . $wording . '">' . $subject . $liveflag . '</option>' . "\n</select>", $contents);
     }
     } else {
     $contents=str_replace('</select>', '<option title="' . $identity . $dt . '" value="' . $wording . '">' . $subject . $liveflag . '</option>' . "\n</select>", $contents);
     }
     file_put_contents("broadcast_list.html", $contents);
     file_put_contents("mailbox/subject_" . strtolower(str_replace(" ", "", $subject)) . ".html", "mailbox/mb_in_" . short_server_remote_addr() . ".html");
     file_put_contents("mailbox/datetime_" . strtolower(str_replace(" ", "", str_replace("/", "", str_replace(":", "", str_replace(" @ ", "", $dt))))) . ".html", "mailbox/mb_in_" . short_server_remote_addr() . ".html");
     file_put_contents("mailbox/emailname_" . strtolower(str_replace(" ", "", str_replace("/", "", str_replace("[", "", str_replace(".", "", str_replace(":", "", str_replace("@", "", str_replace(" @ ", "", $identity)))))))) . ".html", "mailbox/mb_in_" . short_server_remote_addr() . ".html");
     echo '<html><body onload=" parent.document.getElementById(' . "'" . 'bstatus' . "'" . ').innerHTML=' . "'" . $subject . $wasis . 'broadcast to the listeners.' . "';" . '"></body></html>';
    }
  }
}

?>
