<?php
 
//include_once('db_config_01.php');
//ini_set('max_execution_time', 120);

$gw = isset($_GET['gw']) ? $_GET['gw'] : "1";

if( $argc > 1 ) 
   $gw = $argv[1];

/* ------------------------------------------------------------------
   GET Status
 ------------------------------------------------------------------ */
// https://10.0.66.105/api/get_port_info?port=1,2,3&info_type=imei,imsi,iccid,smsc,type,number,reg,slot,callstate,signal,gprs,remain_credit,remain_monthly_credit,remain_daily_credit:,remain_daily_calltime,remain_hourly_calltime,remain_daily_connect


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

echo "\n";

switch( $gw ) {
   case "1":
      $url = "http://10.0.66.104/api/get_port_info"; 
      echo "Gateway 1\n\n";
      break;

   case "2":
      $url = "http://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 = "http://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" );
}         


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

$logged = false;
$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 = [];

/*
reg:
POWER_OFF, NO_SIM, PIN_REQUIRE,
PUK_REQUIRE, UNREGISTER,
SEARCHING_NETWORK, REGISTER_OK or
UNKNOWN

callstate:
Idle, Processing, Ringing, Active, Alerting, Call
Waiting, Call holding or Unknown.

signal:
0-31
*/

//echo "\n";
//var_dump ( $status );
//echo  "\n";
//var_dump ( $data );
echo  "\n";


/* ----------------------------------
  Test SIM REGISTER in gateway
----------------------------------- */
$number = 1;

if( $data->error_code == 200 ) {

   $active = 0;
   $conference = 0; 
   $idle = 0; 
   $ringing = 0; 
   $alerting = 0;     
   $unknown = 0; 
   $processing = 0;  

   foreach ($data->info as $valor) {

       $sl = getSlot( $valor->port, $valor->slot );
//       echo sprintf( "%02d. %s %d %5s %s(dBm) %-20s %-10s\n" , $number, $sl, $valor->type, $valor->imei, $valor->signal, $valor->callstate, $valor->reg );
       echo sprintf( "%s %s %5s %s(dBm) %-20s %-10s => %s\n" , $sl, $valor->type, $valor->imei, $valor->signal, $valor->callstate, $valor->reg, $valor->imsi );

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


//Idle, Processing, Ringing, Active, Alerting, Call
//Waiting, Call holding or Unknown.
       }


       $port_status[$valor->port] = $valor->callstate;
       $number++;
   }

   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 );
}
else {
   echo "Query error: " . $data->error_code . "\n";
}
curl_close($curl);

if( $logged == true ) writeToLog( $logfile, "", 1 ); 

   function changeEven( $n ) {
      switch( $n ) {
         case 0: $dc = 0; break;
         case 1: $dc = 2; break;
         case 2: $dc = 4; break;
         case 3: $dc = 6; break;
         case 4: $dc = 8; break;
         case 5: $dc = 1; break;
         case 6: $dc = 3; break;
         case 7: $dc = 5; break;
         case 8: $dc = 7; break;
         case 9: $dc = 9; break;
      }
      return $dc;
   }


   function sendSms( $number, $text ) {

      $url = "https://10.0.66.104/api/send_sms"; 
      $postfields = array
      (
          'text' => $text,
         // 'port' => array(22,23),
          'param' => Array
          (
              array('number' => $number, 'text_param' => array('A'), 'user_id' => 1)
          )
      );   
      $content = json_encode($postfields);
      $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, "admin:pepito20"); 
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

      $json_response = curl_exec($curl);
      $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

//      echo $json_response."\n";
      echo $response = json_decode($json_response, true);
      echo $status;
      curl_close($curl);

   }

   function getSlot( $port, $val ) {

      $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; 
      }
      $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 calculateImei($imei) {

      $change =  array(  0, 2, 4, 6, 8, 1, 3, 5, 7, 9 );

      /* ------------------------------------------------------
        1. Compute the sum of all digits on odd places.
        2. Replace the digits on even places by the formula:

           0=>0, 1=>2, 2=>4, 3=>6, 4=>8, 5=>1, 6=>3, 7=>5, 
           8=>7, 9=>9 and summarize them.

        3. Summarize the two results.
        4. In case the sum of digits ends in 0, 0 is the 
           check digit. Otherwise, the checksum is equal to 
           the number that needs to be added to the result 
           to get the next highest "round" number.
      ------------------------------------------------------ */

      $newImei = $imei;

      $odd = intval( $newImei[0] ) + intval( $newImei[2] ) + intval( $newImei[4] ) + intval( $newImei[6] ) +
             intval( $newImei[8] ) + intval( $newImei[10] ) + intval( $newImei[12] );


      $even = $change[intval( $newImei[1] )] + $change[intval( $newImei[3] )] + $change[intval( $newImei[5] )] +
              $change[intval( $newImei[7] )] + $change[intval( $newImei[9] )] + $change[intval( $newImei[11] )] + $change[intval( $newImei[13] )];

      $sum = $odd + $even;
      $sum = $sum % 10;
      $chk = 10 - $sum;
      $newImei = substr($imei,0,14) . $chk;
      return $newImei;
   }

   function changeIMEI( $port, $imei ) {

   //   990002616314546
      $url = "https://10.0.66.104/api/set_port_info"; 
      $auth_basic = base64_encode("admin:pepito20");

      $d = rand(1,999);
      $data = sprintf("%03.0lf",$d);

      /* ------------------------------------------------------
         1. Compute the sum of all digits on odd places.
         2. Replace the digits on even places by the formula:

            0=>0, 1=>2, 2=>4, 3=>6, 4=>8, 5=>1, 6=>3, 7=>5, 
            8=>7, 9=>9 and summarize them.

         3. Summarize the two results.
         4. In case the sum of digits ends in 0, 0 is the 
            check digit. Otherwise, the checksum is equal to 
            the number that needs to be added to the result 
            to get the next highest "round" number.
      ------------------------------------------------------ */

      $newImei = substr( $imei, 0, 8 ) . $data . substr( $imei, 11, 3 );
      $calculated = calculateImei($newImei);

      $content = "port=" . $port . "&action=imei" . "&imei=" . $calculated . "&param=" . $calculated; ;
      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_USERPWD, "admin:pepito20"); 
      curl_setopt($curl, CURLOPT_POST, true);
//      curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json;"));
      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);

      if( $data->error_code == 200 ) {
         echo "IMEI change succesful\n";
         curl_close($curl);
         return $calculated;
      }
      curl_close($curl);
      return "Error";
   }

   function getPortInfo( $port ) {

      $url = "https://10.0.66.104/api/get_port_info"; 
      $auth_basic = base64_encode("admin:pepito20");
      $content = "port=$port&info_type=type,imei,number,reg,slot,callstate,signal";

      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_USERPWD, "admin:pepito20"); 
      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);
      return( $data );
   }
?>
