<?php

namespace App\Http\Controllers\Radius;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class Get_Monitoring extends Controller
{

    public function monitoring_down()
    {
            $link = mysqli_connect("172.27.0.5", "anwar", "4nsw3r", "radius");

            /* check connection */
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }

            $current_timestamp = time();
            $range_start_timestamp = $current_timestamp - 300;
            $range_start = date('Y-m-d H:i:s', $range_start_timestamp);
            $query = "SELECT `username`,`framedipaddress`,`acctterminatecause` FROM radacct WHERE acctstoptime BETWEEN '$range_start' AND NOW()";

            if ($result = mysqli_query($link, $query)) {

                // $newArr = array();
                /* fetch associative array */
                while ($db_field = mysqli_fetch_assoc($result)) {
                    $username = $db_field['username'];
                    $ipaddr = $db_field['framedipaddress'];
                    $cause = $db_field['acctterminatecause'];
                    //ngambil group id
                    $groupid_query = "SELECT `groupid` FROM `rm_users` WHERE `username` = '$username'";
                    $result1 = mysqli_query($link, $groupid_query);
                    $result_assoc = mysqli_fetch_assoc($result1);
                    $groupid = $result_assoc['groupid'];
                    //mengambil group name
                    $group_query = "SELECT `groupname` FROM `rm_usergroups` WHERE `groupid` = $groupid ";
                    $result2 = mysqli_query($link, $group_query);
                    $result_assoc = mysqli_fetch_assoc($result2);
                    $group = $result_assoc['groupname'];
                    //menghitung total user
                    $totalonline = "SELECT username FROM rm_onlineradius";
                    $result3 = mysqli_query($link, $totalonline);
                    $usernames = array();
                    while ($row = mysqli_fetch_assoc($result3)) {
                        $usernames[] = $row['username'];
                    }
                    $usernames = "'" . implode("','", $usernames) . "'";
                    $hasil= "SELECT COUNT(*) as online FROM rm_users WHERE groupid = $groupid AND username IN ($usernames)";
                    $count_result = mysqli_query($link, $hasil);
                    $count_row = mysqli_fetch_assoc($count_result);
                    $online = $count_row['online'];
                    //total client aktif
                    $total_query = "SELECT COUNT(*) as total FROM rm_users WHERE groupid =$groupid AND `enableuser` = 1 ";
                    $result4 = mysqli_query($link, $total_query);
                    $result_assoc = mysqli_fetch_assoc($result4);
                    $total = $result_assoc['total'];

                    //total client nonaktif
                    $totaloff_query = "SELECT COUNT(*) as totaloff FROM rm_users WHERE groupid =$groupid AND `enableuser` = 0 ";
                    $result5 = mysqli_query($link, $totaloff_query);
                    $result_assoc = mysqli_fetch_assoc($result5);
                    $totaloff = $result_assoc['totaloff'];
                    
                    $url = "https://e.ebilling.id/bot/?account=2428&nama=$username&status=Down&ip=$ipaddr&totalaktif=$online&identity=$group%09%09Cause=$cause&totaluser=Aktif%20$total%20dan%20Nonaktif%20$totaloff";
                    $ch2 = curl_init(); // Initialize cURL
                    curl_setopt($ch2, CURLOPT_URL, $url);
                    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
                    curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 2);
                    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "GET");
                    curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
                        'Content-Type: application/json',
                        'accept:application/json'
                    ));
                    curl_exec($ch2);
                    curl_close($ch2);

                // }
                }
                mysqli_free_result($result);
            } else {
                printf("Error: %s\n", mysqli_error($link));
            }

            mysqli_close($link);

    }
     public function monitoring_up()
    {
            $link = mysqli_connect("172.27.0.5", "anwar", "4nsw3r", "radius");

            /* check connection */
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }

            $current_timestamp = time();
            $range_start_timestamp = $current_timestamp - 300;
            $range_start = date('Y-m-d H:i:s', $range_start_timestamp);
            echo $range_start;
            $query = "SELECT `username`,`framedipaddress`,`acctterminatecause` FROM radacct WHERE acctstarttime BETWEEN '$range_start' AND NOW()";

            if ($result = mysqli_query($link, $query)) {

                // $newArr = array();
                /* fetch associative array */
                while ($db_field = mysqli_fetch_assoc($result)) {
                    $username = $db_field['username'];
                    $ipaddr = $db_field['framedipaddress'];
                    $cause = $db_field['acctterminatecause'];
                    //ngambil group id
                    $groupid_query = "SELECT `groupid` FROM `rm_users` WHERE `username` = '$username'";
                    $result1 = mysqli_query($link, $groupid_query);
                    $result_assoc = mysqli_fetch_assoc($result1);
                    $groupid = $result_assoc['groupid'];
                    //mengambil group name
                    $group_query = "SELECT `groupname` FROM `rm_usergroups` WHERE `groupid` = $groupid ";
                    $result2 = mysqli_query($link, $group_query);
                    $result_assoc = mysqli_fetch_assoc($result2);
                    $group = $result_assoc['groupname'];
                    //menghitung total user
                    $totalonline = "SELECT username FROM rm_onlineradius";
                    $result3 = mysqli_query($link, $totalonline);
                    $usernames = array();
                    while ($row = mysqli_fetch_assoc($result3)) {
                        $usernames[] = $row['username'];
                    }
                    $usernames = "'" . implode("','", $usernames) . "'";
                    $hasil= "SELECT COUNT(*) as online FROM rm_users WHERE groupid = $groupid AND username IN ($usernames)";
                    $count_result = mysqli_query($link, $hasil);
                    $count_row = mysqli_fetch_assoc($count_result);
                    $online = $count_row['online'];
                    //total client aktif
                    $total_query = "SELECT COUNT(*) as total FROM rm_users WHERE groupid =$groupid AND `enableuser` = 1 ";
                    $result4 = mysqli_query($link, $total_query);
                    $result_assoc = mysqli_fetch_assoc($result4);
                    $total = $result_assoc['total'];

                    //total client nonaktif
                    $totaloff_query = "SELECT COUNT(*) as totaloff FROM rm_users WHERE groupid =$groupid AND `enableuser` = 0 ";
                    $result5 = mysqli_query($link, $totaloff_query);
                    $result_assoc = mysqli_fetch_assoc($result5);
                    $totaloff = $result_assoc['totaloff'];
                    
                    $url = "https://e.ebilling.id/bot/?account=2428&nama=$username&status=UP&ip=$ipaddr&totalaktif=$online&identity=$group&totaluser=Enable%20$total%20dan%20Disable%20$totaloff";
                    $ch2 = curl_init(); // Initialize cURL
                    curl_setopt($ch2, CURLOPT_URL, $url);
                    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
                    curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 2);
                    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "GET");
                    curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
                        'Content-Type: application/json',
                        'accept:application/json'
                    ));
                    curl_exec($ch2);
                    curl_close($ch2);
                // }
                }
                mysqli_free_result($result);
            } else {
                printf("Error: %s\n", mysqli_error($link));
            }

            mysqli_close($link);

    }
}
