<!DOCTYPE html>
<html>
<head>
<title>PE1RXF APRS server</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/styles.css">
<link rel="stylesheet" href="css/popup.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
</head>
<body>

<?php 

    // Prevents a repost when the user refreshes the page.
    // Also it handles the post itself.
    session_start();

    if(count($_POST) > 0) {
        // figure out which form was entered   
        if ($_POST['comment_ax0']) {
            $_SESSION['comment_ax0'] = $_POST['comment_ax0'];
            $_SESSION['period_ax0'] = $_POST['period_ax0'];
            $_SESSION['path_ax0'] = $_POST['path_ax0'];
            
        }
        if ($_POST['comment_ax1']) {
            $_SESSION['comment_ax1'] = $_POST['comment_ax1'];
            $_SESSION['period_ax1'] = $_POST['period_ax1'];
            $_SESSION['path_ax1'] = $_POST['path_ax1'];            
        }

        header("HTTP/1.1 303 See Other");
        header("Location: http://$_SERVER[HTTP_HOST]/index.php");
        die();
    }
    else { 

        if (isset($_SESSION['comment_ax0'])){
            $comment_ax0 = $_SESSION['comment_ax0'];
            $period_ax0 = $_SESSION['period_ax0'];
            $path_ax0 = $_SESSION['path_ax0'];
            //echo "Set ax0: $comment_ax0, $period_ax0, $path_ax0";
            save_settings("ax0", $period_ax0, 0, $path_ax0, 0, $comment_ax0);
            # Wait for the changes to take effect
            sleep(1);
        }
        
        else if (isset($_SESSION['comment_ax1'])){
            $comment_ax1 = $_SESSION['comment_ax1'];
            $period_ax1 = $_SESSION['period_ax1'];
            $path_ax1 = $_SESSION['path_ax1'];            
            //echo "Set ax1: $comment_ax1, $period_ax1, $path_ax1";
            save_settings("ax1", $period_ax1, 0, $path_ax1, 0, $comment_ax1);
            # Wait for the changes to take effect
            sleep(1);
        }

        session_unset();
        session_destroy();

    }
    // END: Prevents a repost when the user refreshes the page.
 
    # Reads configuration file from pe1rxf-aprs-server directory, makes changes if necesary
    # and writes the newly constructed file to a temporary config-file. It does not overwrite
    # the original file, because user www-data can only read the program directory, not write to it.
    # It's a safety thing...
    # The main loop of pe1rxf-aprs-server than copies it to the program directory and removes the temporary file.
    function change_parameter_value($parameter, $value)
    {
        $config_file = '/var/www/html/config/test.cfg';
        $tmp_config_file = '/var/www/html/config/test.cfg.tmp';
        $reading = fopen($config_file, 'r');
        $writing = fopen($tmp_config_file, 'w');

        $replaced = false;
        while (!feof($reading)) {

            $line = fgets($reading);
            if (stristr($line,$parameter)) {
                $line = "$parameter=$value\n";
                //echo "New value: ", "$line";
                $replaced = true;
            }
            fputs($writing, $line);
        }
        fclose($reading); fclose($writing);
        
        // might as well not overwrite the file if we didn't replace anything
        if ($replaced) 
        {
            shell_exec("mv $tmp_config_file $config_file");
        } else {
            shell_exec("rm $tmp_config_file");
        }
    }
    
    # Saves setting for given form: ax0 or ax1
    # $a = beacon_time
    # $b = beacon_destination
    # $c = beacon_path
    # $d = beacon_position
    # $e = beacon_comment
    # $f = beacon_file
    # $g = transmit_directory
    function save_settings($form, $a, $b=0, $c=0, $d=0, $e=0, $f=0, $g=0)
    {
        # Copy original config file to temporary place on web server.
        # From here we can change the values
        $original_config_file = '/home/marcel/ham/aprs_utils/pe1rxf-aprs-server.cfg';
        $destination_config_file = '/var/www/html/config/test.cfg';
        $newly_made_config_file =  '/var/www/html/config/pe1rxf-aprs-server.cfg';

        $fileCopied = copy("$original_config_file", "$destination_config_file");
        if($fileCopied) {
        //echo "# $a, $b, $c, $d, $e, $f, $g";
            if ($form == "ax0" ) {
                change_parameter_value("ax0_beacon_time", $a);
                if ($c) change_parameter_value("ax0_beacon_path", $c);
                if ($e) change_parameter_value("ax0_beacon_comment", $e);
            }
            else if ($form == "ax1" ) {
                change_parameter_value("ax1_beacon_time", $a);
                if ($c) change_parameter_value("ax1_beacon_path", $c);
                if ($e) change_parameter_value("ax1_beacon_comment", $e);                
            }

            # now make new file with new configuration for the main loop to find.
            shell_exec("cp $destination_config_file $newly_made_config_file");
            # and remove working file:
            shell_exec("rm $destination_config_file");

        } else{
            #echo "Error";
        }
    }
    
    # Reads configuration file from pe1rxf-aprs-server directory.
    # Search this file for given paramter and returns its value.
    function search_parameter_in_config_file($parameter)
    {
        $config_file = '/home/marcel/ham/aprs_utils/pe1rxf-aprs-server.cfg';
        $reading = fopen($config_file, 'r');
        while (!feof($reading)) {
        
            $line = fgets($reading);
            if ( strstr($line,$parameter) ) {

                $line = substr( $line, strlen($parameter)+1 );
                
                # Found match. Stop further search.
                fclose($reading);
                return rtrim($line);
            }
        }
        
    }

    $ax0_name = search_parameter_in_config_file("ax0_name");
    $ax0_frequency = search_parameter_in_config_file("ax0_frequency");
    $ax0_beacon_comment =  search_parameter_in_config_file("ax0_beacon_comment");
    $ax0_beacon_time =  search_parameter_in_config_file("ax0_beacon_time");
    $ax0_beacon_path =  search_parameter_in_config_file("ax0_beacon_path");

    $ax1_name = search_parameter_in_config_file("ax1_name");
    $ax1_frequency = search_parameter_in_config_file("ax1_frequency");
    $ax1_beacon_comment =  search_parameter_in_config_file("ax1_beacon_comment");
    $ax1_beacon_time =  search_parameter_in_config_file("ax1_beacon_time");
    $ax1_beacon_path =  search_parameter_in_config_file("ax1_beacon_path");

?>

<div class="header">
  <a href="index.php" >
    <h1><i class="fa fa-home" aria-hidden="true"></i>&nbsp; PE1RXF APRS server</h1>
  </a>
</div>

<div class="row">
  <div class="col-3 col-s-3 menu">
    <ul>
      <li><h3><?php echo $ax0_name ?> (<?php echo $ax0_frequency ?>)</h3></li>
      <li onclick="openForm_ax0()">Beacon: <b><?php echo $ax0_beacon_comment; ?></b></li>
      <li onclick="openForm_ax0()">Beacon interval: <b><?php if ($ax0_beacon_time == 0) echo "OFF";else echo $ax0_beacon_time; ?></b></li>
      <li onclick="openForm_ax0()">Path: <b><?php echo $ax0_beacon_path; ?></b></li>
    </ul>
  </div>

  <div class="col-6 col-s-9"  id="main_page">

    <div class="col-6 col-s-12">
     <a href="send_message.php"><img src="images/mail-out.svg" /></a>
     <h1>Send a message</h1>

     
     <img src="images/mail-in.svg" style="cursor:pointer;" onclick="show_received_messages()" />
     <h1>Received messages</h1>

    </div>

    <div class="col-6 col-s-12">
     <a href="telemetry.php"><img src="images/telemetry.svg" /></a>
     <h1>Telemetry</h1>

     <img src="images/received.svg" style="cursor:pointer;" onclick="show_heard_stations()" />
     <h1>Heard stations</h1>

    </div>
  
  </div>
  
  <div class="col-6 col-s-9" id="heard_stations" style="display:none">
  
    <h1>Heard last 24 hours</h1>

    <div class="col-6 col-s-12">
     <h1><?php echo $ax0_frequency ?></h1>
     <h3 align="center">
        <b style="color:Blue">DIRECT</b><br><br>
        <?php
        $csv = array_map('str_getcsv', file("/home/marcel/ham/aprs_utils/aprs_log/aprs_stations_heard_direct.log"));

        for ($i = 0; $i < count($csv); $i++) {
            if ($csv[$i][1] == "ax0") {
                echo $csv[$i][2];
                echo "<br>";
            }
        }
        echo "<br><b style=\"color:Blue\">DIGIPEATED</b><br><br>";
        $csv = array_map('str_getcsv', file("/home/marcel/ham/aprs_utils/aprs_log/aprs_stations_heard_digipeated.log"));

        for ($i = 0; $i < count($csv); $i++) {
            if ($csv[$i][1] == "ax0") {
                echo $csv[$i][2];  
                echo "<br>";
            }
        }
        
        ?> 
    </h3>
    </div>

    <div class="col-6 col-s-12">
     <h1><?php echo $ax1_frequency ?></h1>
     <h3 align="center">
        <b style="color:Blue">DIRECT</b><br><br>
        <?php
        $csv = array_map('str_getcsv', file("/home/marcel/ham/aprs_utils/aprs_log/aprs_stations_heard_direct.log"));

        for ($i = 0; $i < count($csv); $i++) {
            if ($csv[$i][1] == "ax1") {
                echo $csv[$i][2];
                echo "<br>";
            }
        }
        echo "<br><b style=\"color:Blue\">DIGIPEATED</b><br><br>";
        $csv = array_map('str_getcsv', file("/home/marcel/ham/aprs_utils/aprs_log/aprs_stations_heard_digipeated.log"));

        for ($i = 0; $i < count($csv); $i++) {
            if ($csv[$i][1] == "ax1") {
                echo $csv[$i][2];  
                echo "<br>";
            }
        }
        
        ?> 
    </h3>
    </div>
       <a href=""><h1 style="color:Black;" onclick="show_main_page()"><i class="fa fa-arrow-left" aria-hidden="true"></i>&nbsp; Back</h1></a>
  </div>

  <div class="col-6 col-s-9" id="received_messages" style="display:none">
  
    <h1>Received messages</h1>


     <h2 align="left"><?php echo $ax0_name ?> (<?php echo $ax0_frequency ?>)</h2>
     <h3 align="left">
        <?php
        $csv = array_map('str_getcsv', file("/home/marcel/ham/aprs_utils/aprs_log/aprs_received_messages.log"));

        for ($i = 0; $i < count($csv); $i++) {
            if ($csv[$i][1] == "ax0") {
                echo $csv[$i][2];
                echo ": ";
                echo $csv[$i][4];
                echo "<br>";
            }
        }
        
        ?> 
    </h3>



     <h2 align="left"><?php echo $ax1_name ?> (<?php echo $ax1_frequency ?>)</h2>
     <h3 align="left">
        <?php
        $csv = array_map('str_getcsv', file("/home/marcel/ham/aprs_utils/aprs_log/aprs_received_messages.log"));

        for ($i = 0; $i < count($csv); $i++) {
            if ($csv[$i][1] == "ax1") {
                echo $csv[$i][2];
                echo ": ";
                echo $csv[$i][4];
                echo "<br>";
            }
        }
        
        ?> 
    </h3>

       <a href=""><h1 style="color:Black;" onclick="show_main_page()"><i class="fa fa-arrow-left" aria-hidden="true"></i>&nbsp;  Back</h1></a>
  </div>
  
  <div class="col-3 col-s-12 menu">
      <ul>
      <li><h3><?php echo $ax1_name ?> (<?php echo $ax1_frequency ?>)</h3></li>
      <li onclick="openForm_ax1()">Beacon: <b><?php echo $ax1_beacon_comment; ?></b></li>
      <li onclick="openForm_ax1()">Beacon interval: <b><?php if ($ax1_beacon_time == 0) echo "OFF";else echo $ax1_beacon_time; ?></b></li>
      <li onclick="openForm_ax1()">Path: <b><?php echo $ax1_beacon_path; ?></b></li>
    </ul>
  </div>
</div>

<div class="loginPopup">
      <div class="formPopup" id="popupForm_ax0_beacon">
        <form action="" class="formContainer" method="post">
          <h2>Beacon <?php echo $ax0_frequency ?></h2>
          
          <label for="email">
            <strong>Comment</strong>
          </label>
          <input type="text" id="email" placeholder="Comment" name="comment_ax0" value="<?php echo $ax0_beacon_comment; ?>" required>
          <label for="psw">
            <strong>Period (minutes)</strong>
          </label>
          <input type="number" id="psw" placeholder="Minutes" name="period_ax0" min="0" max="60" step="10" value="<?php echo $ax0_beacon_time; ?>" required>
          
          <label for="path">
            <strong>Path<br></strong>
          </label>
          <div class="radioLeft">
            <input type="radio" class="formContainer radio" id="path_none" name="path_ax0" value="none"<?php if ($ax0_beacon_path == "none") echo "checked"; ?>>
            <label for="path_none">none</label><br>
            <input type="radio" class="formContainer radio" id="path_wide21" name="path_ax0" value="WIDE2-1" <?php if ($ax0_beacon_path == "WIDE2-1") echo "checked"; ?> >
            <label for="path_wide21">WIDE2-1</label><br>  
            <input type="radio" class="formContainer radio" id="path_wide22" name="path_ax0" value="WIDE2-2" <?php if ($ax0_beacon_path == "WIDE2-2") echo "checked"; ?> >
            <label for="path_wide22">WIDE2-2</label><br>
            <input type="radio" class="formContainer radio" id="path_wide33" name="path_ax0" value="WIDE3-3" <?php if ($ax0_beacon_path == "WIDE3-3") echo "checked"; ?> >
            <label for="path_wide33">WIDE3-3</label><br>
          </div>


          <div id="submit_ax0"><button type="submit" class="btn" onclick="SubmitClicked_ax0()">Submit</button></div>
          <div id="submit_done_ax0" style="display:none;"><button type="button" class="btn wait"><img src="images/preload.gif">Please wait...</button></div>
          <div id="cancel_ax0"><button type="button" class="btn cancel" onclick="closeForm_ax0()">Cancel</button>
        </form>
      </div>
    </div>
</div>
    <div class="loginPopup">
      <div class="formPopup" id="popup_ax1_beacon">
        <form action="" class="formContainer" method="post">
          <h2>Beacon <?php echo $ax1_frequency ?></h2>
          <label for="email">
            <strong>Comment</strong>
          </label>
          <input type="text" id="email" placeholder="comment" name="comment_ax1" value="<?php echo $ax1_beacon_comment; ?>" required>
          <label for="psw">
            <strong>Period</strong>
          </label>
          <input type="number" id="psw" placeholder="Period" name="period_ax1" min="0" max="60" step="10" value="<?php echo $ax1_beacon_time; ?>" required>
          
          <label for="path">
            <strong>Path<br></strong>
          </label>
          <div class="radioLeft">
            <input type="radio" class="formContainer radio" id="path_none" name="path_ax1" value="none"<?php if ($ax1_beacon_path == "none") echo "checked"; ?>>
            <label for="path_none">none</label><br>
            <input type="radio" class="formContainer radio" id="path_wide21" name="path_ax1" value="WIDE2-1" <?php if ($ax1_beacon_path == "WIDE2-1") echo "checked"; ?> >
            <label for="path_wide21">WIDE2-1</label><br>  
            <input type="radio" class="formContainer radio" id="path_wide22" name="path_ax1" value="WIDE2-2" <?php if ($ax1_beacon_path == "WIDE2-2") echo "checked"; ?> >
            <label for="path_wide22">WIDE2-2</label><br>
            <input type="radio" class="formContainer radio" id="path_wide33" name="path_ax1" value="WIDE3-3" <?php if ($ax1_beacon_path == "WIDE3-3") echo "checked"; ?> >
            <label for="path_wide33">WIDE3-3</label><br>
          </div>
          <div id="submit_ax1"><button type="submit" class="btn" onclick="SubmitClicked_ax1()">Submit</button></div>
          <div id="submit_done_ax1" style="display:none;"><button type="button" class="btn wait"><img src="images/preload.gif">Please wait...</button></div>
          <div id="cancel_ax1"><button type="button" class="btn cancel" onclick="closeForm_ax1()">Cancel</button>
        </form>
      </div>
    </div>
</div>
<div class="footer">
  <p>Open source hardware and software. Visit https://www.meezenest.nl/mees for more information.</p>
</div>

<script>
      function openForm_ax0() {
        document.getElementById("popupForm_ax0_beacon").style.display = "block";
      }
      function closeForm_ax0() {
        document.getElementById("popupForm_ax0_beacon").style.display = "none";
      }
      function openForm_ax1() {
        document.getElementById("popup_ax1_beacon").style.display = "block";
      }
      function closeForm_ax1() {
        document.getElementById("popup_ax1_beacon").style.display = "none";
      }
      
      function show_heard_stations() {
        document.getElementById("main_page").style.display = "none";
        document.getElementById("heard_stations").style.display = "block";
        document.getElementById("received_messages").style.display = "none";
      }
      function show_received_messages() {
        document.getElementById("main_page").style.display = "none";
        document.getElementById("heard_stations").style.display = "none";
        document.getElementById("received_messages").style.display = "block";
      }
      
      function show_main_page() {
        document.getElementById("main_page").style.display = "block";
        document.getElementById("heard_stations").style.display = "none";
        document.getElementById("received_messages").style.display = "none";
      }
      
      
        /*
        Replacing Submit Button with 'Loading' Image
        Version 2.0
        December 18, 2012

        Will Bontrager Software, LLC
        https://www.willmaster.com/
        Copyright 2012 Will Bontrager Software, LLC

        This software is provided "AS IS," without 
        any warranty of any kind, without even any 
        implied warranty such as merchantability 
        or fitness for a particular purpose.
        Will Bontrager Software, LLC grants 
        you a royalty free license to use or 
        modify this software provided this 
        notice appears on all copies. 
        */
        function SubmitClicked_ax1()
        {
            document.getElementById("submit_ax1").style.display = "none"; // to undisplay
            document.getElementById("submit_done_ax1").style.display = "block"; // to display
            document.getElementById("cancel_ax1").style.display = "none"; // to display            
            return true;
        }
        
        function SubmitClicked_ax0()
        {
            document.getElementById("submit_ax0").style.display = "none"; // to undisplay
            document.getElementById("submit_done_ax0").style.display = "block"; // to display
            document.getElementById("cancel_ax0").style.display = "none"; // to display            
            return true;
        }
    </script>
</body>
</html>


