<?php
session_start();
if( $_SESSION["user"] == "" || $_SESSION["passwd"] == "" ) {
//   header("Location: traficoIndex.php");
}


if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){
   $screen_width =  $_SESSION['screen_width'];
   $screen_height =  $_SESSION['screen_height']; 
} else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) {
    $_SESSION['screen_width'] = $_REQUEST['width'];
    $_SESSION['screen_height'] = $_REQUEST['height'];
    header('Location: ' . $_SERVER['PHP_SELF']);
} else {
    echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';
}

$width = "98%";
$IP = $_SERVER['REMOTE_ADDR'];
//$informes = $_SESSION["informes"];
$trama = isset($_POST['t']) ? $_POST["t"]  : "";
if( $trama == "" ) {
   if( ( $trama = $_SESSION['t'] ) == "" ) $trama = "0";
}
$_SESSION['t'] = $trama;


//include_once('db_ouichat.php');
//ini_set('max_execution_time', 120);

$page = $_SERVER['PHP_SELF'];
$sec = "10";
header("Refresh: $sec; url=$page");

include 'traficoHeader.php'; 
?>
<style type="text/css">
  .btn, .btn-large, .btn-small {
     text-decoration: none;
     color: #fff;
     background-color: #d50000;
     background-color: #b71c1c;
     height: 2.5em;
     border-radius: 1.50em;
     text-align: center;
     letter-spacing: .5px;
     -webkit-transition: background-color .2s ease-out;
     transition: background-color .2s ease-out;
     cursor: pointer;
     line-height:2.5em;
   }
   
   .btn:hover, .btn-large:hover, .btn-small:hover {
     background-color: #dd2c00;
      }
  p {
      display: block;
      -webkit-margin-before: 0em;
      -webkit-margin-after: 0.3em;
      -webkit-margin-start: 0px;
      -webkit-margin-end: 0px;
  }

   #toast-container {
       min-width: 10%;
       top: 50%;
       right: 50%;
       transform: translateX(50%) translateY(50%);
    }   

    a {
        text-decoration: none;
    }

   .btn, .btn-large, .btn-small {
     text-decoration: none;
     color: #fff;
     background-color: #b71c1c;
     height: 2.5em;
     border-radius: 1.25em;
     text-align: center;
     letter-spacing: .5px;
     -webkit-transition: background-color .2s ease-out;
     transition: background-color .2s ease-out;
     cursor: pointer;
     line-height:2.2em;
   }

   .btn:hover, .btn-large:hover, .btn-small:hover {
     background-color: #dd2c00 ;
   }

   .butn {
      color:black;
   }

   .butn:hover {
     color: red;
   }


.tabs {
  height: 40px !important;
}

.tabs.tabs-transparent {
  background-color: transparent;
}

.tabs.tabs-transparent .tab a,
.tabs.tabs-transparent .tab.disabled a,
.tabs.tabs-transparent .tab.disabled a:hover {
  color: rgba(255, 255, 255, 0.7);
}

.tabs.tabs-transparent .tab a:hover,
.tabs.tabs-transparent .tab a.active {
  color: #fff;
}

.tabs.tabs-transparent .indicator {
  background-color: #fff;
}

.tabs.tabs-fixed-width {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
}

.tabs.tabs-fixed-width .tab {
  -webkit-box-flex: 1;
  -webkit-flex-grow: 1;
      -ms-flex-positive: 1;
          flex-grow: 1;
}

.tabs .tab {
  line-height: 40px !important;
  height: 40px !important;
/*  text-transform: uppercase; */
  text-transform: none !important;
}

.tabs .tab a {
  color: #16a2d6 !important;
  -webkit-transition: color .28s ease, background-color .28s ease;
  transition: color .28s ease, background-color .28s ease;
}

.tabs .tab a:focus, .tabs .tab a:focus.active {
  background-color: rgba(246, 178, 181, 0.2);
  background-color: #16a2d6 !important;
  background-color: rgba(22, 162, 214, 0.9) !important; 
  color: white !important;
  outline: none;
}

.tabs .tab a:hover, .tabs .tab a.active {
  color: #16a2d6 !important;
}

.tabs .tab.disabled a,
.tabs .tab.disabled a:hover {
  color: #16a2d6 !important;
}

.tabs .indicator {
  position: absolute;
  bottom: 0;
  height: 2px;
  background-color: #16a2d6;
  will-change: left, right;
}

.active {
   border: none !important;

}

.card {
-webkit-box-shadow: 0px 12px 15px -5px rgba(0,0,0,0.59); 
box-shadow: 0px 12px 15px -5px rgba(0,0,0,0.59);

}

th, td {
   padding: 0px;
}

#modal_wait {
    background: transparent; 
    width: 0px;
    height: 0px;
    position: absolute;
    border-radius:10px;
    z-index:1000;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}

</style>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script> 
<script type="text/javascript" src="materialize/1.0.0/js/materialize.js"></script>
<script type="text/javascript">

   var width;

   $(document).ready(function() {

      var trama = <?php echo json_encode($trama); ?>;

      console.log("Ready!");

      width = $(window).width();
      console.log( width );

      $('.sidenav').sidenav();
      $('.dropdown-trigger').dropdown({
         "hover": true,
         "constrainWidth": false,
         "coverTrigger": false
      });

      $('select').formSelect();
      $('.collapsible').collapsible();
      $('.modal-trigger').modal({
          dismissible: false,
          preventScrolling: true,
          opacity: .7, // Opacity of modal background
          in_duration: 300, // Transition in duration
          out_duration: 200, // Transition out duration
          starting_top: '40%'
      });      
      
      $('.tabs').tabs();
      $('#tabs_menu').tabs();
      $('ul.tabs').tabs();

      $('.tooltiped').tooltip({exitDelay:0,enterDelay:0,inDuration:10,outDuration:10});
      console.log( trama );
  //   $( "#modal_wait" ).css( 'display', 'block' );

   });   

   function onClick(e) {

      switch( e.id ) {
         case "0":
            post("traficoPanel.php", {t:0});
            break;

         case "1":
            post("traficoPanel.php", {t:1});
            break;

         case "2":
//            post("traficoPanel.php", {t:2});
            post("traficoGateways.php", {t:2});
            break;

         case "3":
            post("traficoLlamados.php", {t:3});
            break;

         case "5":
            post("traficoOpciones.php", {t:5});
            break;

         case "4":
            post("traficoConference.php", {t:4});
            break;
      }
   }


   function post(path, parameters) {
      var form = $('<form></form>');

      form.attr("method", "post");
      form.attr("action", path);

      $.each(parameters, function(key, value) {
          var field = $('<input></input>');
          field.attr("type", "hidden");
          field.attr("name", key);
          field.attr("value", value);
          form.append(field);
      });
      $(document.body).append(form);
      form.submit();
   };
</script>

<?php  
   $logfile = "/var/www/html/dinstar/logs/dinstar_status.log";
   $auth_basic = base64_encode("admin:pepito20");
   $auth = "admin:pepito20";

   $gw = "4";

   switch( $gw ) {
      case "1":
         $url = "https://10.0.66.104/api/get_port_info"; 
//         echo "Gateway 1\n\n";
         break;
   
      case "2":
         $url = "https://10.0.66.105/api/get_port_info"; 
//         echo "Gateway 2\n\n";
         break;
   
      case "3":
         $url = "http://200.114.225.132:36080/api/get_port_info"; 
//         echo "Gateway Diego 32\n\n";
         $auth_basic = base64_encode("admin:admincarlos");
         $auth = "admin:admincarlos";
         break;
   
      case "4":
         $url = "http://190.19.247.27:8989/api/get_port_info"; 
//         echo "Gateway David\n\n";
         $auth_basic = base64_encode("admin:carlosadmin");
         $auth = "admin:carlosadmin";
         break;
   
      case "5":
         $url = "https://200.114.225.132:35080/api/get_port_info"; 
//         echo "Gateway Diego 16\n\n";
         $auth_basic = base64_encode("admin:admin1234");
         $auth = "admin:admin1234";
         break;
   
      default:
         die( "Invalid Gateway\n\n" );
   }         
   
   
   
   $logged = false;
   for ($i = 0; $i < 400; $i++) {
       $types[$i] = "";
       $signals[$i] = "";
       $ports[$i] = "";
       $state[$i] = "";
   }


   $content = "info_type=type,imei,imsi,iccid,number,reg,slot,callstate,signal,gprs,remain_credit,remain_monthly_credit,remain_daily_credit,remain_daily_calltime,remain_hourly_calltime,remain_daily_connect";
   $content = "info_type=type,imei,number,reg,slot,callstate,signal,imsi";


   /*
   $curl = curl_init($url);
   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
   //curl_setopt($curl, CURLOPT_HEADER, true);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   //curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
   curl_setopt($curl, CURLOPT_USERPWD, $auth ); 
   curl_setopt($curl, CURLOPT_POST, true);
   curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
   curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($curl, CURLINFO_HEADER_OUT, true);
   $json_response = curl_exec($curl);
   $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
   $data = json_decode($json_response);
   $port_status = [];
   */

/*
   $active = 0;
   $conference = 0; 
   $idle = 0; 
   $ringing = 0; 
   $alerting = 0;     
   $unknown = 0; 
   $processing = 0;  
   */
   $x = 0;


   $url = "http://10.0.66.104/api/get_port_info"; 
   $auth = "admin:pepito20";
   $data = getStatus( $url, $auth, $content );
   
   if( $data->error_code == 200 ) {
   
      foreach ($data->info as $valor) {
          $n = $valor->port + ( $x * 32 );
          $sl = getSlot( $valor->port, $valor->slot, 1 );
   //       echo sprintf( "%02d. %s %d %5s %s(dBm) %-20s %-10s\n" , $number, $sl, $valor->type, $valor->imei, $valor->signal, $valor->callstate, $valor->reg );

          $types[$n] =  $valor->type;
          $states[$n] =  $valor->callstate;
          $signals[$n] =  $valor->signal . " (dBm)";
          $signals[$n] =  $valor->signal;
          $reg[$n] =  $valor->reg;
          $ports[$n] = $sl;

          switch( $valor->callstate ) {
            case "Active":          ++$active[$x];      break;
            case "Call Conference": ++$conference[$x];  break;
            case "Idle":            ++$idle[$x];        break;
            case "Ringing":         ++$ringing[$x];     break;
            case "Alerting":        ++$alerting[$x];    break;
            case "Processing":      ++$processing[$x];  break;
            default:                ++$unknown[$x];     break; 
         }
      }
   }

   $url = "http://10.0.66.105/api/get_port_info"; 
   $auth = "admin:pepito20";
   $data = getStatus( $url, $auth, $content );
   $x = 1;
   
   if( $data->error_code == 200 ) {
   
      foreach ($data->info as $valor) {
          $n = $valor->port + ( $x * 32 );
          $sl = getSlot( $valor->port, $valor->slot, 1 );
   //       echo sprintf( "%02d. %s %d %5s %s(dBm) %-20s %-10s\n" , $number, $sl, $valor->type, $valor->imei, $valor->signal, $valor->callstate, $valor->reg );

          $types[$n] =  $valor->type;
          $states[$n] =  $valor->callstate;
          $signals[$n] =  $valor->signal . " (dBm)";
          $signals[$n] =  $valor->signal;
          $reg[$n] =  $valor->reg;
          $ports[$n] = $sl;

          switch( $valor->callstate ) {
            case "Active":          ++$active[$x];      break;
            case "Call Conference": ++$conference[$x];  break;
            case "Idle":            ++$idle[$x];        break;
            case "Ringing":         ++$ringing[$x];     break;
            case "Alerting":        ++$alerting[$x];    break;
            case "Processing":      ++$processing[$x];  break;
            default:                ++$unknown[$x];     break; 
         }
      }
   }
//   curl_close($curl);


   $x = 2;
   $url = "http://200.114.225.132:36080/api/get_port_info"; 
   $auth = "admin:admincarlos";
   $data = getStatus( $url, $auth, $content );
   
   if( $data->error_code == 200 ) {
   
      foreach ($data->info as $valor) {
          $n = $valor->port + ( $x * 32 );
          $sl = getSlot( $valor->port, $valor->slot, 0  );
   //       echo sprintf( "%02d. %s %d %5s %s(dBm) %-20s %-10s\n" , $number, $sl, $valor->type, $valor->imei, $valor->signal, $valor->callstate, $valor->reg );

          $types[$n] =  $valor->type;
          $states[$n] =  $valor->callstate;
          $signals[$n] =  $valor->signal . " (dBm)";
          $signals[$n] =  $valor->signal;
          $reg[$n] =  $valor->reg;
          $ports[$n] = $sl;

          switch( $valor->callstate ) {
            case "Active":          ++$active[$x];      break;
            case "Call Conference": ++$conference[$x];  break;
            case "Idle":            ++$idle[$x];        break;
            case "Ringing":         ++$ringing[$x];     break;
            case "Alerting":        ++$alerting[$x];    break;
            case "Processing":      ++$processing[$x];  break;
            default:                ++$unknown[$x];     break; 
         }
      }
   }
//   curl_close($curl);

   $x = 3;
   $url = "http://190.19.247.27:8989/api/get_port_info"; 
   $auth = "admin:carlosadmin";
   $data = getStatus( $url, $auth, $content );

   if( $data->error_code == 200 ) {
   
      foreach ($data->info as $valor) {
          $n = $valor->port + ( $x * 32 );
          $sl = getSlot( $valor->port, $valor->slot, 0 );
   //       echo sprintf( "%02d. %s %d %5s %s(dBm) %-20s %-10s\n" , $number, $sl, $valor->type, $valor->imei, $valor->signal, $valor->callstate, $valor->reg );

          $types[$n] =  $valor->type;
          $states[$n] =  $valor->callstate;
          $signals[$n] =  $valor->signal . " (dBm)";
          $signals[$n] =  $valor->signal;
          $reg[$n] =  $valor->reg;
          $ports[$n] = $sl;

          switch( $valor->callstate ) {
            case "Active":          ++$active[$x];      break;
            case "Call Conference": ++$conference[$x];  break;
            case "Idle":            ++$idle[$x];        break;
            case "Ringing":         ++$ringing[$x];     break;
            case "Alerting":        ++$alerting[$x];    break;
            case "Processing":      ++$processing[$x];  break;
            default:                ++$unknown[$x];     break; 
         }
      }
   }
//   curl_close($curl);


   $x = 4;
   $url = "http://200.114.225.132:35081/api/get_port_info"; 
//   $url = "http://10.0.66.111/dinstar/dinstar_info.php?gw=5";
   $auth = "admin:admin1234";
   $data = getStatus( $url, $auth, $content );
//   $data = json_decode($json);

//   echo $json;
   for ($i = 0; $i < 32; $i++) {
       $n = $i + ( 4 * 32 );
       $types[$n] = "&nbsp;";
       $signals[$n] = "&nbsp;";
       $ports[$n] = $i;
       $states[$n] = "&nbsp;";
   }

   if( $data->error_code == 200 ) {
   
      foreach ($data->info as $valor) {
          $n = $valor->port + ( $x * 32 );
          $sl = getSlot( $valor->port, $valor->slot, 0 );
          $types[$n] =  $valor->type;
          $states[$n] =  $valor->callstate;
          $signals[$n] =  $valor->signal . " (dBm)";
          $signals[$n] =  $valor->signal;
          $reg[$n] =  $valor->reg;
          $ports[$n] = $sl;

          switch( $valor->callstate ) {
            case "Active":          ++$active[$x];      break;
            case "Call Conference": ++$conference[$x];  break;
            case "Idle":            ++$idle[$x];        break;
            case "Ringing":         ++$ringing[$x];     break;
            case "Alerting":        ++$alerting[$x];    break;
            case "Processing":      ++$processing[$x];  break;
            default:                ++$unknown[$x];     break; 
         }
      }
   }

   
   /*
      echo sprintf( "\n\n" );
      echo sprintf( "Active: %2d\n", $active );
      echo sprintf( "Conference: %2d\n", $conference );
      echo sprintf( "Idle: %2d\n", $idle );
      echo sprintf( "Ringing: %2d\n", $ringing );
      echo sprintf( "Alerting: %2d\n", $alerting );
      echo sprintf( "Processing: %2d\n\n", $processing );
      */
//   curl_close($curl);

//   $( "#modal_wait" ).css( 'display', 'none' );

   $c = 5;
   
   echo "<div class='card white' style='margin:0px;'>"; 
      echo "<div class='card-content black-text' style='border-top:none !important; padding-top:0 !important;'>"; 
         echo "<table>";
            echo "<tr style='line-height:2em; font-size:8pt; padding-left:0; border-bottom:1px solid rgba(0, 0, 0, 0.12); '>";
            echo "<td>&nbsp;</td>";
            echo "</tr>";


//           echo "<tr style='line-height:2em; font-size:8pt; border:none; padding-left:0;'";
/*
           echo "<tr>";
           echo "<th >Gateway 1</th>";
           echo "<th >Gateway 2</th>";
           echo "<th >Gateway Diego 32</th>";
           echo "<th >Gateway David</th>";
           echo "<th >Gateway Diego 16</th>";
           echo "</tr>";
           */

            for( $i = 0; $i < 32; ++$i ) {

               echo "<tr style='line-height:2em; font-size:8pt; padding-left:0; border-bottom:1px solid rgba(0, 0, 0, 0.12); '>";
                  for( $x = 0; $x < $c; ++$x ) {
                     $n = $i + ( $x * 32 );

                     switch( $states[$n] ) {
                        case "Active":          $color = "blue";  break;
                        case "Call Conference": $color = "green"; $states[$n] = "In Call"; break; 
                        case "Idle":            $color = "magenta"; break; 
                        case "Ringing":         $color = "salmon";  break; 
                        case "Alerting":        $color = "red";   break; 
                        case "Processing":      $color = "salmon"; break; 
                        case "No Information":  $color = "red"; break; 
                        default:                
                     }

                     if( $signals[$n] < 30 ) $scolor = "teal";
                     else if( $signals[$n] < 35 ) $scolor = "orange";
                          else $scolor = "red";
  

                     if( $reg[$n] != "REGISTER_OK" ) $scolor = "red";
                     else $scolor = "teal";

                     if( $states[$n] != "&nbsp;" )
                        $state = strtoupper( $states[$n] );
                     else $state = $states[$n]; 
                     echo "<td style='width:15px; text-align: right; font-size:8pt;'>$ports[$n]</td>";
                     echo "<td style='width:1%;'>&nbsp;</td>";
                     echo "<td style='width:3%;'>$types[$n]</td>";
                     echo "<td style='width:7%; color:$color;'>$state</td>";

/*

POWER_OFF, NO_SIM, PIN_REQUIRE,
PUK_REQUIRE, UNREGISTER,
SEARCHING_NETWORK, REGISTER_OK or
UNKNOWN
*/
                     
                     echo "<td style='width:3%; color:$scolor;'>$reg[$n]</td>";
  
  /*                   
                     if( $signals[$n] > 0 ) 
                        echo "<td style='width:3%; color:$scolor;'>$signals[$n] (dBm)</td>";
                     else echo "<td style='width:3%; color:$scolor;'>&nbsp;</td>";
                     */



//                     echo "<td style='width:16%; border-bottom:1px solid rgba(0, 0, 0, 0.12); color:$color;'><span class='tooltiped' data-position='bottom' data-tooltip='$app[$n]: $cid[$n]' style='cursor:pointer;'>$mensaje[$n]</span></td>";


                     $color = "blue";
//                     if( $dnis[$n] == "0074" ) $color = "SALMON";
//                     else if( $dnis[$n] == "00018" ) $color = "green";
//
//                     if( $dnis[$n] == "1157" || $dnis[$n] == "1323" || $dnis[$n] == "2615" || $dnis[$n] == "2846" ) $color = "green";
//                     else if( $dnis[$n] == "7182" || $dnis[$n] == "0121" || $dnis[$n] == "1204" || $dnis[$n] == "2054" ) $color = "#64b5f6";
//                          else if( $dnis[$n] == "0046" || $dnis[$n] == "0051" || $dnis[$n] == "0058" || $dnis[$n] == "0062" ) $color = "#ef6c00";


                     echo "<td style='width:2px; border: none;'>";
                     echo "</td>"; 
                     echo "</td>";
                  }
               echo "</tr>";
            }
         echo "</table>";

//         $Otras = $Total_Lines - $Total_0045 - $Total_0074;
         echo "<div style='height:10px;'></div>";
         echo "<div class='row' style='line-height:0.2em !important;'>";
            echo "<table style='width:100%;'>";
               echo "<tr style='line-height:3em; font-size:10pt; border:none;'>";

                  echo "<td style='width:5%;'>&nbsp;</td>";
                  $name[0] = "Gateway 1";
                  $name[1] = "Gateway 2";
                  $name[2] = "Gateway Diego 32";
                  $name[3] = "Gateway David";
                  $name[4] = "Gateway Diego 16";
                  for( $i = 0; $i < 5; $i++ ) {
                     $total[$i] = intval( $idle[$i] ) + intval( $alerting[$i] ) + intval( $ringing[$i] ) + intval( $processing[$i] ) + intval( $unknown[$i] );
//                   echo sprintf( "<td style='width:12%%;'>%s: (%d/<p style='color:red;'>%d</p>/%d)</td>", $name[$i], intval($active[$i]) + intval($conference[$i]), $total[$i], intval($conference[$i]) ); 
                     echo sprintf( "<td style='width:12%%;'>%s: (%d/%d/%d)</td>", $name[$i], intval($active[$i]) + intval($conference[$i]), $total[$i], intval($conference[$i]) ); 
                     echo "<td style='width:8.7%;'>&nbsp;</td>";
                  }


/*
                   echo "<td style='width:12%;'>5500-0045: ($Total_0045)</td>";
                   echo "<td style='width:1%;'>&nbsp;</td>";
                   echo "<td style='width:12%;'>5172-0074: ($Total_0074)</td>";
                   echo "<td style='width:1%;'>&nbsp;</td>";
                   echo "<td style='width:12%;'>Otras: ($Otras)</td>";
                   echo "<td style='width:1%;'>&nbsp;</td>";

                   echo "<td style='width:12%;'>mod_singles: ($total_mod_singles)</td>";
                   echo "<td style='width:1%;'>&nbsp;</td>";

                   echo "<td style='width:12%;'>mod_directo: ($total_mod_directo)</td>";

//                   echo "<td style='width:12%;'>Discador: ($total_discador/$total_conference  $total_conf1/$total_conf2/$total_diego)</td>";
                   echo "<td style='width:1%;'>&nbsp;</td>";
                   */

                   echo "<td style='width:35%; text-align:right; padding-right:20px;'><a class='butn' href='traficoLogout.php'>LOGOUT</a></td>";
               echo "</tr>";
            echo "</table>";
         echo "</div>";
      echo "</div>"; 
   echo "</div>"; 


   echo "<div id='modal_wait' style='display:none;'>";
      echo "<div class='modal-content' style='background-color:transparent; border-radius:10px;'>";
         echo "<div class='preloader-wrapper big active'>";
         echo "<div class='spinner-layer spinner-blue-only'>"; 
         echo "<div class='circle-clipper left'>"; 
             echo "<div class='circle'></div>"; 
         echo "</div><div class='gap-patch'>"; 
            echo "<div class='circle'></div>"; 
            echo "</div><div class='circle-clipper right'>"; 
            echo "<div class='circle'></div>"; 
         echo "</div>"; 
      echo "</div>"; 
//   echo "</div>"; 

   //      echo "</div>"; 
//      echo "</div>"; 

//      mysqli_close($conn);


   function getSlot( $port, $val, $mode ) {

      if( $mode ) {
         $data = "";
         $sl = "A";
         switch( $val ) {
            case 0: $sl = " A"; break;
            case 1: $sl = " B"; break; 
            case 2: $sl = " C"; break; 
            case 3: $sl = " D"; break; 
         }
      }
      else $sl = "  ";
      $data = sprintf( "%2d%s", intval( $port ), $sl );
      return $data;
   }

   function writeToLog( $filename, $text, $mode ) {

     if( $myfile = fopen( $filename, "a+") ) {

         if( $mode == 1 ) $text = "+------------------------------------------------------------------+";
//         if( $mode == 1 ) $text = "+-----------------------------------------------------+";
         date_default_timezone_set("America/Buenos_Aires");
         $hoy = date("Y-m-d H:i:s", time() ); 
         fprintf( $myfile, "%s: %s\n", $hoy, $text );
         fclose( $myfile );
     }
   }

   function getStatus( $url, $auth, $content ) {

      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
      //curl_setopt($curl, CURLOPT_HEADER, true);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      //curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
      curl_setopt($curl, CURLOPT_USERPWD, $auth ); 
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
      curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($curl, CURLINFO_HEADER_OUT, true);
      $json_response = curl_exec($curl);
      $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
      $data = json_decode($json_response);
      $port_status = [];
      curl_close($curl);
      return $data;
   }

?>

<!--    <div id='ip'></div> -->
</body>
</html>

