<?php
// test_broadcast_listen.php
// RJM Programming
// October, 2015
// Test broadcast_listen.html and broadcast_listen.php

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);
}


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) {
    $twi="";
    if (isset($_GET['testwordsin'])) $twi=urldecode($_GET['testwordsin']);
    if (isset($_GET['testwordsin']) && $twi != "") {
      $prefix="";
      if (file_exists("mailbox/mb_in_" . short_server_remote_addr() . ".html")) {
       $prefix=urldecode(file_get_contents("mailbox/mb_in_" . short_server_remote_addr() . ".html"));
       if ($prefix != "") $prefix.="\n";
      }
      file_put_contents("mailbox/mb_in_" . short_server_remote_addr() . ".html", $prefix . urldecode($_GET['testwordsin']));
      echo "<!doctype html><html><body><form action='./test_broadcast_listen.php' method='GET'><br>Words to send to inbox: <input type=text name=testwordsin value=''></input><br><br>Words to send to outbox: <input type=text name=testwordsout value=''></input><br><br><input type=submit value=Inbox></input>&nbsp;&nbsp;<input type=submit value=Outbox></input></form></body></html>";
    } else if (isset($_GET['testwordsout'])) {
      $prefix="";
      if (file_exists("mailbox/mb_out_" . short_server_remote_addr() . ".html")) {
       $prefix=urldecode(file_get_contents("mailbox/mb_out_" . short_server_remote_addr() . ".html"));
       if ($prefix != "") $prefix.="\n";
      }
      file_put_contents("mailbox/mb_out_" . short_server_remote_addr() . ".html", $prefix . urldecode($_GET['testwordsout']));
      echo "<!doctype html><html><body><form action='./test_broadcast_listen.php' method='GET'><br>Words to send to inbox: <input type=text name=testwordsin value=''></input><br><br>Words to send to outbox: <input type=text name=testwordsout value=''></input><br><br><input type=submit value=Inbox></input>&nbsp;&nbsp;<input type=submit value=Outbox></input></form></body></html>";
    } else {
      echo "<!doctype html><html><body><form action='./test_broadcast_listen.php' method='GET'><br>Words to send to inbox: <input type=text name=testwordsin value=''></input><br><br>Words to send to outbox: <input type=text name=testwordsout value=''></input><br><br><input type=submit value=Inbox></input>&nbsp;&nbsp;<input type=submit value=Outbox></input></form></body></html>";
    }    
  }
}

?>
