#!/usr/local/bin/perl
# Redirecting to MGW ##IP Main Page

require 'ippbx-lib.pl';
ui_print_header(undef, $module_info{'desc'}, "", undef, 1, 1);
use strict;
use warnings;
use feature 'say';
use Mojo::UserAgent;
my $ua   = Mojo::UserAgent->new;
my $data = $ua->get('http://##IP/api.php?admin=token')->res->json;
my $res = $data->{data};
if ($res ne "")
{
use CGI;
use URI;
my $requested = URI->new( CGI::url() );
$requested->query( $ENV{QUERY_STRING} || $ENV{REDIRECT_QUERY_STRING} );
my $hideurl = ":7999/ippbx/##IP_mgw_single_sign_on.cgi";
$requested =~ s/$hideurl//ig;
my $static = "/##IP/validation.php?token=$res";
print qq(
<div style="height:800px; width:1000px;">
<iframe src="$requested$static"></iframe>
</div>
);
}
else
{
use Net::Ping;
my $pinger = Net::Ping->new();
if ($pinger->ping('##IP')) {
        print 'Media Gateway ##IP is Pingable but not responded\n';
} else {
        print 'Media Gateway ##IP is Not Pingable\n';
}
}
