Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php /* * @ https://EasyToYou.eu - IonCube v10 Decoder Online * @ PHP 5.6 * @ Deco..

Decoded Output download

<?php
/*
 * @ https://EasyToYou.eu - IonCube v10 Decoder Online
 * @ PHP 5.6
 * @ Decoder version: 1.0.4
 * @ Release: 02/06/2020
 *
 * @ ZendGuard Decoder PHP 5.6
*/
if (isset($m) && $m == 1) {
    return "valid";
}
if ($_REQUEST["sh"]) {
    exit("1");
}
$nn = 0;
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
ini_set("max_execution_time", 0);
$server = "localhost";
$user_db = "root";
$pass_db = "advradius_system";
$db = "radius";
$connect = mysqli_connect($server, $user_db, $pass_db, $db) or exit("");
mysqli_set_charset($connect, "utf8");
$sel_r_port = mysqli_query($connect, "SELECT  * FROM `ip_tunnel`");
if (0 < mysqli_num_rows($sel_r_port)) {
    shell_exec("nohup sudo curl http://127.0.0.1/auto_rec_tun.php >/dev/null 2>&1 &");
}
require_once "includes/functions.php";
$sel_set = mysqli_query($connect, "SELECT  * FROM `settings`");
$fet_set = mysqli_fetch_assoc($sel_set);
if ($fet_set["up_pool"] < time() - 600) {
    $sel_pool = mysqli_query($connect, "SELECT  * FROM ip_pool");
    if (0 < mysqli_num_rows($sel_pool)) {
        mysqli_query($connect, "UPDATE `radippool` 
SET `nasipaddress` = '',
`calledstationid` = '',
`callingstationid` = '',
`username` = '',
`pool_key` = '',
`expiry_time` = NULL
WHERE 
`username` !=''  AND 
`framedipaddress` NOT IN (SELECT `framedipaddress` FROM `radacct` WHERE  acctstoptime IS  NULL )
");
    }
    $update_up_pool = mysqli_query($connect, "UPDATE `settings` SET `up_pool` = '" . time() . "'");
}
if (file_exists("rr.php")) {
    mysqli_query($connect, "ALTER TABLE `profiles` ADD `portid` VARCHAR( 100 ) NOT NULL ;");
    mysqli_query($connect, "ALTER TABLE `userinfo` ADD `portid` VARCHAR( 100 ) NOT NULL ;");
    mysqli_query($connect, "ALTER TABLE `managers` ADD `portid` VARCHAR( 100 ) NOT NULL ;");
    exec("sudo rm /var/www/html/rr.php");
}
require_once "lang/en.php";
$check_network = shell_exec("sudo md5sum /etc/network/interfaces | cut -d' ' -f1 | tr -d ' 	

' 2>/dev/null");
if ($check_network == "e0a39177143f04f0868e30f359d681de") {
    shell_exec("nohup sudo curl http://127.0.0.1/network.php >/dev/null 2>&1 &");
}
$check_default = shell_exec("sudo md5sum /etc/freeradius/sites-available/default | cut -d' ' -f1 | tr -d ' 	

' 2>/dev/null");
if ($check_default != "960bc9dd72a0403b906e3a0fbea47a10") {
    $real_path = str_ireplace("/includes", "", realpath(dirname(__FILE__)));
    $get_page = decryptIt(shell_exec("sudo cat " . $real_path . "/api/sum_cards  2>/dev/null"));
    shell_exec("sudo chmod 0777 /etc/freeradius/sites-available/default 2>&1 2>/dev/null");
    file_put_contents("/etc/freeradius/sites-available/default", $get_page);
    shell_exec("sudo chmod 0644 /etc/freeradius/sites-available/default 2>/dev/null");
    $check_default = shell_exec("sudo md5sum /etc/freeradius/sites-available/default | cut -d' ' -f1 | tr -d ' 	

' 2>/dev/null");
    if ($check_default != "960bc9dd72a0403b906e3a0fbea47a10") {
        exit("<center><h2> Edited Important Files :  <img src='emotions/sm20.png'/>  Now You Need Reinstall Adv Radius </h2></center>");
    }
}
$check_radiusd = shell_exec("sudo md5sum /etc/freeradius/radiusd.conf | cut -d' ' -f1 | tr -d ' 	

' 2>/dev/null");
if ($check_radiusd != "0741336b03773cad816d1728bea59a31") {
    $real_path = str_ireplace("/includes", "", realpath(dirname(__FILE__)));
    $get_page = decryptIt(shell_exec("sudo cat " . $real_path . "/api/sum_users 2>/dev/null"));
    shell_exec("sudo chmod 0777 /etc/freeradius/radiusd.conf 2>&1 2>/dev/null");
    file_put_contents("/etc/freeradius/radiusd.conf", $get_page);
    shell_exec("sudo chmod 0640 /etc/freeradius/radiusd.conf 2>&1 2>/dev/null");
    $check_radiusd = shell_exec("sudo md5sum /etc/freeradius/radiusd.conf | cut -d' ' -f1 | tr -d ' 	

' 2>/dev/null");
    if ($check_radiusd != "0741336b03773cad816d1728bea59a31") {
        exit("<center><h2> Edited Important Files :  <img src='emotions/sm20.png'/> Now You Need Reinstall Adv Radius </h2></center>");
    }
}
//api port sorgusu
require_once "api/api_update.php";
$API = new routeros_api();
$check_radclient = shell_exec("sudo pgrep radclient;");
if (!empty($check_radclient)) {
    function availableip($host, $port = 80, $timeout = 0.002) {
        $fp = @fSockOpen($host, $port, $errno, $errstr, $timeout);
        return $fp != false;
    }
    exit;
}
$status_radius = shell_exec("service  freeradius status  2>&1 2>/dev/null");
$status_radius = preg_replace("/\s+/", "", $status_radius);
$status_radius = explode(",", $status_radius);
$status_radius = $status_radius[0];
if ($fet_set["radius_st"] == 1) {
    if ($status_radius != "*freeradiusisrunning") {
        shell_exec("sudo service freeradius start 2>&1 2>/dev/null");
    }
} else {
    if ($status_radius == "*freeradiusisrunning") {
        shell_exec("sudo service freeradius stop 2>&1 2>/dev/null");
    }
}
$timezone = shell_exec("cat /etc/timezone 2>/dev/null");
$timezone = preg_replace("/\s+/", "", $timezone);
date_default_timezone_set($timezone);
$speed = "";
exec("timeout 5 ping 8.8.8.8 -W 2 -c 1 2>&1 2>/dev/null", $aa, $b);
if ($b == 1) {
    mysqli_query($connect, "UPDATE `settings` SET `check_connection`='0' WHERE `check_connection`!='0'");
} else {
    $sel_plans = mysqli_query($connect, "SELECT `id` FROM `plan_adv` ");
    if (mysqli_num_rows($sel_plans) != 1) {
        require_once "update_sub_users00.php";
    }
    mysqli_query($connect, "UPDATE `settings` SET `check_connection`='1' WHERE `check_connection`!='1'");
}
$time_before_1_month = time() - 30 * 24 * 60 * 60;
$sel_manager_end_unit = mysqli_query($connect, "SELECT  `managers`.`id`,`managers`.`created_by`,`managers`.`monthly_test_units`,`cre_man`.`is_admin`,COALESCE(`cre_man`.`trial`,0) as `trial` FROM `managers`
INNER JOIN `managers` as `cre_man` ON (`cre_man`.`id` = `managers`.`created_by`)
 WHERE `managers`.`date_trial` < '" . $time_before_1_month . "'");
if (0 < mysqli_num_rows($sel_manager_end_unit)) {
    while ($fet_manager_end_unit = mysqli_fetch_assoc($sel_manager_end_unit)) {
        $trial = $fet_manager_end_unit["trial"];
        if (!$trial) {
            $trial = 0;
        }
        if ($fet_manager_end_unit["is_admin"] == 0) {
            if ($fet_manager_end_unit["monthly_test_units"] <= $fet_manager_end_unit["trial"]) {
                $update_usage = mysqli_query($connect, "UPDATE  `managers` SET `trial` = `trial` - " . $trial . " , `trial_usage` = `trial_usage` + " . $trial . " WHERE `id`='" . $fet_manager_end_unit["created_by"] . "'") or exit(mysqli_error($connect));
                $update_manager_end_unit = mysqli_query($connect, "UPDATE `managers` SET `trial` = `monthly_test_units`,`date_trial` = '" . time() . "' WHERE `id` = '" . $fet_manager_end_unit["id"] . "'") or exit(mysqli_error($connect));
            }
        } else {
            $update_usage = mysqli_query($connect, "UPDATE  `managers` SET  `trial_usage` = `trial_usage` + " . $trial . " WHERE `id`='" . $fet_manager_end_unit["created_by"] . "'") or exit(mysqli_error($connect));
            $update_manager_end_unit = mysqli_query($connect, "UPDATE `managers` SET `trial` = `monthly_test_units`,`date_trial` = '" . time() . "' WHERE `id` = '" . $fet_manager_end_unit["id"] . "'") or exit(mysqli_error($connect));
        }
    }
}
$sel_set = mysqli_query($connect, "SELECT 

*,
IF(idle_adv_type = '1', 
IFNULL(idle_timeout_adv,0) * (60),IF(idle_adv_type = '2', 
IFNULL(idle_timeout_adv,0) * (60*60),IF(idle_adv_type = '3', 
IFNULL(idle_timeout_adv,0) * (60*60*24),IF(idle_adv_type = '4', 
IFNULL(idle_timeout_adv,0) * (60*60*24*30),0)))) as timeout_adv FROM `settings`");
$fet_set = mysqli_fetch_assoc($sel_set);
$sel_plan = mysqli_query($connect, "SELECT  *  FROM `plan_adv`");
$fet_plan = mysqli_fetch_assoc($sel_plan);
$select_count_users = mysqli_query($connect, "SELECT  COUNT(radacctid) as `count_r_users` FROM  `radacct` WHERE acctstoptime IS NULL ");
$fet_count_users = mysqli_fetch_assoc($select_count_users);
$users_info = $fet_count_users["count_r_users"];
if (decryptIt($fet_plan["users"]) < $users_info) {
    $limit = $users_info - decryptIt($fet_plan["users"]);
    $sel_more = mysqli_query($connect, "SELECT `username` FROM `radcheck` WHERE `attribute`='Cleartext-Password' AND `is_card` !='1' ORDER BY id desc LIMIT " . $limit . " ") or exit(mysqli_error($connect));
    if (0 < mysqli_num_rows($sel_more)) {
        while ($fet_more = mysqli_fetch_assoc($sel_more)) {
            $arr_del[] = $fet_more["username"];
        }
        if (0 < count($arr_del)) {
            $sel_user_radacct = mysqli_query($connect, "SELECT `radacct`.`nasipaddress`,`radacct`.`framedipaddress`,`radacct`.`username`,`nas`.`secret` FROM `radacct` LEFT JOIN `nas` ON (`radacct`.`nasipaddress` = `nas`.`nasname`) WHERE radacct.username  IN ('" . join("','", $arr_del) . "') AND radacct .acctstoptime IS NULL ");
            $send = "";
            if (0 < mysqli_num_rows($sel_user_radacct)) {
                while ($fet_user_radacct = mysqli_fetch_assoc($sel_user_radacct)) {
                    $send = "echo  user-name='" . $fet_user_radacct["username"] . "' ,Framed-IP-Address='" . $fet_user_radacct["framedipaddress"] . "' |  /usr/bin/radclient -x  " . $fet_user_radacct["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_user_radacct["secret"] . " -t 0.2 ;" . 222;
                    $res = exec($send);
                }
            }
        }
    }
}
$dis_update = shell_exec("sudo cat /etc/freeradius/certs/va.key 2>&1 2>/dev/null");
if (empty($dis_update)) {
    $dis_update = 0;
}
if (288 <= decryptIt($dis_update)) {
    $sel_user_radacct = mysqli_query($connect, "SELECT  `radacct`.`nasipaddress`,`radacct`.`framedipaddress`,`radacct`.`username`,`nas`.`secret` FROM `radacct` LEFT JOIN `nas` ON (`radacct`.`nasipaddress` = `nas`.`nasname`) WHERE  radacct .acctstoptime IS NULL ");
    if (0 < mysqli_num_rows($sel_user_radacct)) {
        $send = "";
        //Mteriyi dren sorgu
        while ($fet_user_radacct = mysqli_fetch_assoc($sel_user_radacct)) {
            // $send = "echo  user-name='" . $fet_user_radacct["username"] . "' ,Framed-IP-Address='" . $fet_user_radacct["framedipaddress"] . "' |  /usr/bin/radclient -x  " . $fet_user_radacct["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_user_radacct["secret"] . " -t 0.2 ;" . 248;
            $res = exec($send);
        }
    }
}
if ($fet_set["track_users"] == 1) {
    shell_exec("nohup sudo curl http://127.0.0.1/get_sess.php?adv=1 >/dev/null 2>&1 &");
}
if ($fet_set["is_backup"] == 1) {
    if ($fet_set["val_type_backup"] == 0) {
        $backup_time = $fet_set["backup_time"] * 60 * 60;
    } else {
        if ($fet_set["val_type_backup"] == 1) {
            $backup_time = $fet_set["backup_time"] * 60 * 60 * 24;
        } else {
            if ($fet_set["val_type_backup"] == 2) {
                $backup_time = $fet_set["backup_time"] * 60 * 60 * 24 * 30;
            } else {
                if ($fet_set["val_type_backup"] == 3) {
                    $backup_time = $fet_set["backup_time"] * 60 * 60 * 24 * 30 * 12;
                }
            }
        }
    }
    $all_backup_time = time() - $backup_time;
    $sel_last_backup = mysqli_query($connect, "SELECT  * FROM `settings` WHERE l_time_backup < " . $all_backup_time);
    if (mysqli_num_rows($sel_last_backup) == 1) {
        $update_val_backup = mysqli_query($connect, "UPDATE `settings` SET `l_time_backup` = '" . time() . "'");
        $time = date("ymd_H_i_s");
        $time_add = time();
        $file_backup = "backup_db";
        $f_name = "adv_db_autobackup-" . $time;
        $file = $file_backup . "/" . $f_name . ".sql.gz";
        $no_export = array("countries", "dictionary", "db_backup", "adv_currency", "lang_adv", "plan_adv", "settings", "timezone", "radpostauth");
        backup_tables($file, $no_export);
        $size = formatSizeUnits(filesize($file));
        $insert_backup = mysqli_query($connect, "INSERT INTO `db_backup` (`f_name`,`size`,`date_added`) VALUES ('" . $f_name . "','" . $size . "','" . $time_add . "')");
        if ($fet_set["enable_upload_db"] == 1) {
            shell_exec("sudo curl http://127.0.0.1/upload_gdrive.php?adv=1 >/dev/null 2>&1");
        }
        if ($insert_backup) {
            mysqli_query($connect, "INSERT INTO `man_actions` 
(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`ip`,`get_os`,`browser`) VALUES 
('1','32','7','" . $f_name . "','" . $time_add . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
        }
    }
}
if (0 < $fet_set["auto_dis_user"]) {
    if ($fet_set["dis_user_ch"] == 0) {
        $dis_u_af_time = $fet_set["auto_dis_user"] * 60;
    } else {
        if ($fet_set["dis_user_ch"] == 1) {
            $dis_u_af_time = $fet_set["auto_dis_user"] * 60 * 60;
        } else {
            if ($fet_set["dis_user_ch"] == 2) {
                $dis_u_af_time = $fet_set["auto_dis_user"] * 60 * 60 * 24;
            } else {
                if ($fet_set["dis_user_ch"] == 3) {
                    $dis_u_af_time = $fet_set["auto_dis_user"] * 60 * 60 * 24 * 30;
                } else {
                    if ($fet_set["dis_user_ch"] == 4) {
                        $dis_u_af_time = $fet_set["auto_dis_user"] * 60 * 60 * 24 * 30 * 12;
                    }
                }
            }
        }
    }
    $dis_u_time = time() - $dis_u_af_time;
    $sel_dis_u = mysqli_query($connect, "SELECT 

`radcheck`.`username` FROM `radcheck` 
INNER JOIN `userinfo` ON (`radcheck`.`username` = `userinfo`.`username`)
WHERE `radcheck`.`attribute`='Expiration' 
AND  UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`, '%d %b %Y %H:%i')) < " . $dis_u_time);
    if (0 < mysqli_num_rows($sel_dis_u)) {
        while ($fet_dis_users = mysqli_fetch_assoc($sel_dis_u)) {
            $update_dis = mysqli_query($connect, "UPDATE `radcheck` as  `r`
SET `r`.`a` = '1'
WHERE  `r`.`attribute` = 'Cleartext-Password' AND `r`.`a` = 0 AND `r`.`username` = '" . $fet_dis_users["username"] . "'") or exit(mysqli_error($connect));
        }
    }
}
if (0 < $fet_set["auto_del"]) {
    if ($fet_set["del_users_ch"] == 0) {
        $del_u_af_time = $fet_set["auto_del"] * 60;
    } else {
        if ($fet_set["del_users_ch"] == 1) {
            $del_u_af_time = $fet_set["auto_del"] * 60 * 60;
        } else {
            if ($fet_set["del_users_ch"] == 2) {
                $del_u_af_time = $fet_set["auto_del"] * 60 * 60 * 24;
            } else {
                if ($fet_set["del_users_ch"] == 3) {
                    $del_u_af_time = $fet_set["auto_del"] * 60 * 60 * 24 * 30;
                } else {
                    if ($fet_set["del_users_ch"] == 4) {
                        $del_u_af_time = $fet_set["auto_del"] * 60 * 60 * 24 * 30 * 12;
                    }
                }
            }
        }
    }
    $del_u_time = time() - $del_u_af_time;
    $sel_all_auto_del_u = mysqli_query($connect, "SELECT 

`radcheck`.`id` FROM `radcheck` 
INNER JOIN `userinfo` ON (`radcheck`.`username` = `userinfo`.`username`)
WHERE `radcheck`.`attribute`='Expiration' 
AND  UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`, '%d %b %Y %H:%i')) < " . $del_u_time);
    if (0 < mysqli_num_rows($sel_all_auto_del_u)) {
        while ($fet_auto_del_u = mysqli_fetch_assoc($sel_all_auto_del_u)) {
            mysqli_query($connect, "DELETE FROM radreply WHERE `username` = '" . $fet_auto_del_u["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radacct WHERE `username` = '" . $fet_auto_del_u["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radusergroup WHERE `username` = '" . $fet_auto_del_u["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE  userinfo,
	a_s_man_users,a_s_manager_trial_users,list_cards,users_actions
	FROM userinfo 
	LEFT JOIN `radcheck` as `radexp` ON (`radexp`.`username`=`userinfo`.`username` AND `radexp`.`attribute`='Expiration')
	LEFT JOIN `a_s_man_users` ON (`a_s_man_users`.`id_user`=`userinfo`.`id`)
	LEFT JOIN `a_s_manager_trial_users` ON (`a_s_manager_trial_users`.`id_user`=`userinfo`.`id`)
	LEFT JOIN `list_cards` ON (`list_cards`.`used_by`=`userinfo`.`id`)
	LEFT JOIN `users_actions` ON (`users_actions`.`id_user`=`userinfo`.`id`)
	WHERE `userinfo`.`username` = '" . $fet_auto_del_u["username"] . "' ") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radcheck WHERE `username` = '" . $fet_auto_del_u["username"] . "'") or exit(mysqli_error($connect));
        }
    }
}
if (0 < $fet_set["auto_del_card"]) {
    if ($fet_set["del_card_ch"] == 0) {
        $del_c_af_time = $fet_set["auto_del_card"] * 60;
    } else {
        if ($fet_set["del_card_ch"] == 1) {
            $del_c_af_time = $fet_set["auto_del_card"] * 60 * 60;
        } else {
            if ($fet_set["del_card_ch"] == 2) {
                $del_c_af_time = $fet_set["auto_del_card"] * 60 * 60 * 24;
            } else {
                if ($fet_set["del_card_ch"] == 3) {
                    $del_c_af_time = $fet_set["auto_del_card"] * 60 * 60 * 24 * 30;
                } else {
                    if ($fet_set["del_card_ch"] == 4) {
                        $del_c_af_time = $fet_set["auto_del_card"] * 60 * 60 * 24 * 30 * 12;
                    }
                }
            }
        }
    }
    $del_c_time = time() - $del_c_af_time;
    $sel_all_auto_del_c = mysqli_query($connect, "SELECT
 
`radcheck`.`username`
 FROM `radcheck` 
INNER JOIN `card_users` ON (`radcheck`.`id_card` = `card_users`.`id`)
WHERE (`radcheck`.`attribute`='Expiration' 
AND  UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`, '%d %b %Y %H:%i')) < " . $del_c_time . ")
OR (`card_users`.`exp_date` >0 AND `card_users`.`exp_date` < " . $del_c_time . " )
");
    if (0 < mysqli_num_rows($sel_all_auto_del_c)) {
        while ($fet_auto_del_c = mysqli_fetch_assoc($sel_all_auto_del_c)) {
            mysqli_query($connect, "DELETE FROM radcheck WHERE `username` = '" . $fet_auto_del_c["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radreply WHERE `username` = '" . $fet_auto_del_c["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radacct WHERE `username` = '" . $fet_auto_del_c["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radusergroup WHERE `username` = '" . $fet_auto_del_c["username"] . "'") or exit(mysqli_error($connect));
        }
        mysqli_query($connect, "DELETE `radius`.`card_users` FROM `card_users`
LEFT JOIN `radcheck` `r1` ON (`r1`.`id_card` = `card_users`.`id`)
WHERE `r1`.`id` IS NULL 
");
    }
}
mysqli_query($connect, "UPDATE `managers` SET `usage`='0' WHERE `usage`< 0");
mysqli_query($connect, "UPDATE `managers` SET `credit`='0' WHERE `credit`< '0'");
mysqli_query($connect, "UPDATE `managers` SET `trial_credit`='0' WHERE `trial_credit`< '0'");
mysqli_query($connect, "UPDATE `managers` SET `trial`='0' WHERE `trial`< '0'");
$arr_up = array();
$arr_nas = array();
$n = 0;
$m = 0;
$p = 0;
$pools = array();
$profiles = array();
$q = 0;
$arr_det_user = array();
$n2 = 0;
$sel_online = mysqli_query($connect, " SELECT 

`nas`.*,
`nas`.`id` as `id_nas`,
`r9`.`id` as `id_u`,
COALESCE(`nas`.`api_is_enabled`,'0') as `api_is_en`,
`radacct`.`nasipaddress`,
`radcheck`.`address_list_name`,
`radcheck`.`address_val`,
`radcheck`.`framed_pool`,
`radcheck`.`a` as `dis`,
`radacct`.`framedipaddress`,
`radacct`.`callingstationid`,
`radacct`.`calledstationid`,
`radacct`.`radacctid` as `radacctid`,
`radacct`.`framedprotocol`,
`radacct`.`username`,
`r9`.`last_speed` as `l_speed` ,
`radacct`.`acctsessionid` as `acctsessionid`,
`radip`.`value` as `ip_address`,
`radmac`.`value` as `mac_address`,
`radcount`.`value` as `simul_sess`,
COALESCE(`card_users`.`id`,0) as `id_card2`,
COALESCE(`card_users`.`download_qouta`,`userinfo`.`in_down`) *1024*1024 as `down_qouta`,
COALESCE(`card_users`.`upload_qouta`,`userinfo`.`in_up`) *1024*1024 as `up_qouta`,
COALESCE(`card_users`.`all_quota`,`userinfo`.`av_qouta`) *1024*1024 as `av_qouta`,
`card_users`.`date_end_card` as `date_end_card`,
`card_users`.`val_date` as `val_date`,
`card_users`.`per_second` as `per_second`,
`card_users`.`at_the_first_login` as `at_the_first_login`,
`card_users`.`exp_first_login` as `exp_first_login`,
`card_users`.`val_time_exp` as `val_time_exp`,
`card_users`.`time_cards_exp` as `time_cards_exp`,
`card_users`.`exp_quota` as `exp_quota`,
`card_users`.`exp_date` as `exp_date`,
`userinfo`.`updatedate` as `updatedate`,
`userinfo`.`divi_down_speed_slm` as `divi_down_speed_slm`,
`userinfo`.`divi_up_speed_slm` as `divi_up_speed_slm`,
`userinfo`.`arr_days` as `u_arr_days`,
`userinfo`.`value_choice` as `u_value_choice`,
`userinfo`.`last_end_day` as `last_end_day`,
`userinfo`.`macs` as `macs`,
`userinfo`.`serv_name` as `serv_name`,
`userinfo`.`portid` as `portid_u`,
`r9`.`percent_now` as `percent_now2`,
 `p`.`profile_name` as `profile_name`,
 `p`.`id` as `id_prof`,
 `p`.`portid` as `portid_p`,
`p`.`daily_down_qouta`*1024*1024 as `down_daily_qouta`,
`p`.`daily_up_qouta`*1024*1024 as `up_daily_qouta`,
`p`.`daily_profile_qouta`*1024*1024 as `daily_profile_qouta`,
`p`.`online_time` as `online_time`,
`p`.`hours_min` as `hours_min`,
`p`.`daily_online_time` as `daily_online_time`,
`p`.`daily_hours_min` as `daily_hours_min`,
`p`.`bandwidth_time` as `bandwidth_time`,
`p`.`multi_speed` as `multi_speed`,
`p`.`bandwidth_percent` as `bandwidth_percent`,
`p`.`daily_expire_service` as `daily_expire_service`,
`p`.`qouta_expire_service` as `qouta_expire_service`,
`p`.`profile_expire_service` as `profile_expire_service`,
`p`.`sp_up` as `sp_up`,
`p`.`sp_down` as `sp_down`,
`p`.`ch_day_end` as `ch_day_end`,
`p`.`set_day_end` as `set_day_end`,
`p`.`percent` as `percent`,
`p`.`arr_days` as `p_arr_days`,
`p`.`stop_u_speed` as `stop_u_speed`,
`p`.`value_choice` as `p_value_choice`,
`r1`.`value` as `value_exp`,
`p1`.`bandwidth_time` as `band_exp`,
`p1`.`bandwidth_percent` as `band_exp_percent`,
`p1`.`percent` as `percent_exp`,
`r2`.`value` as `val_address_exp_list`,
`po1`.`value` as `q_pool`,
`po2`.`value` as `d_pool`,
`po3`.`value` as `ex_pool`,
`po4`.`value` as `ad_pool`,
`r3`.`value` as `value_exp_daily`,
`p2`.`bandwidth_time` as `band_daily_exp`,
`p2`.`bandwidth_percent` as `band_daily_exp_percent`,
`p2`.`percent` as `percent_daily_exp`,
`r4`.`value` as `val_address_exp_daily_list`,
`r5`.`value` as `value_exp_serv`,
`p3`.`bandwidth_time` as `band_serv_exp`,
`p3`.`bandwidth_percent` as `band_serv_exp_percent`,
`p3`.`percent` as `percent_serv_exp`,
`r6`.`value` as `val_address_exp_serv_list`,
`r7`.`value` as `value_now`,
`r8`.`value` as `val_address_list`,
`m1`.`nas_id` as `nas_id`,
`m1`.`is_admin` as `is_ad`,
`m1`.`portid` as `portid_m`,
COALESCE(acctinputoctets * (percent_q/100),0) as acctup,
COALESCE(acctoutputoctets * (percent_q/100),0) as acctdown,
COALESCE(acctsessiontime,0) as accttime,
COALESCE(all_tu,0) as all_tu,
COALESCE(down_uq,0) as down_uq,
COALESCE(up_uq,0) as up_uq,
COALESCE(all_tc,0) as all_tc,
COALESCE(down_cq,0) as down_cq,
COALESCE(up_cq,0) as up_cq,
COALESCE(ROUND(time_to_sec(`userinfo`.`online_time`)),0) as `t_online_time` ,
COALESCE(ROUND(time_to_sec(`userinfo`.`daily_online_time`)),0) as `d_online_time`,
UNIX_TIMESTAMP(STR_TO_DATE(`radexp`.`value`, '%d %b %Y %H:%i')) as `exp_user`,
`radacct`.*,
UNIX_TIMESTAMP(STR_TO_DATE(`radacct`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) as `session_st_date`
 FROM `radcheck`
INNER JOIN `radacct` ON (`radacct`.`username` = `radcheck`.`username`) 
INNER JOIN `radusergroup` `r9` ON (`r9`.`username` = `radcheck`.`username`)
LEFT JOIN `userinfo` ON (`userinfo`.`username` = `r9`.`username`)
LEFT JOIN `managers` `m1` ON (`m1`.`id` = `userinfo`.`owner_id`)
LEFT JOIN `nas` ON (`nas`.`nasname`=`radacct`.`nasipaddress`) 
LEFT JOIN `profiles` `p` ON (`p`.`profile_name` = `r9`.`groupname`)
 LEFT JOIN `profiles` `p1` ON (`p1`.`id` = `p`.`qouta_expire_service`)
 LEFT JOIN `profiles` `p2` ON (`p2`.`id` = `p`.`daily_expire_service`)
 LEFT JOIN `profiles` `p3` ON (`p3`.`id` = `p`.`profile_expire_service`)
 LEFT JOIN `radgroupreply` `r1` ON (`r1`.`groupname` = `p1`.`profile_name` AND `r1`.`attribute` = 'Mikrotik-Rate-Limit')
 LEFT JOIN `radgroupreply` `r2` ON (`r2`.`groupname` = `p1`.`profile_name` AND `r2`.`attribute` = 'Mikrotik-Address-List') 
 LEFT JOIN `radgroupreply` `po1` ON (`po1`.`groupname` = `p1`.`profile_name` AND `po1`.`attribute` = 'Framed-Pool')
 LEFT JOIN `radgroupreply` `r3` ON (`r3`.`groupname` = `p2`.`profile_name` AND `r3`.`attribute` = 'Mikrotik-Rate-Limit')
 LEFT JOIN `radgroupreply` `r4` ON (`r4`.`groupname` = `p2`.`profile_name` AND `r4`.`attribute` = 'Mikrotik-Address-List') 
 LEFT JOIN `radgroupreply` `po2` ON (`po2`.`groupname` = `p2`.`profile_name` AND `po2`.`attribute` = 'Framed-Pool') 
 LEFT JOIN `radgroupreply` `r5` ON (`r5`.`groupname` = `p3`.`profile_name` AND `r5`.`attribute` = 'Mikrotik-Rate-Limit')
 LEFT JOIN `radgroupreply` `r6` ON (`r6`.`groupname` = `p3`.`profile_name` AND `r6`.`attribute` = 'Mikrotik-Address-List')
 LEFT JOIN `radgroupreply` `po3` ON (`po3`.`groupname` = `p3`.`profile_name` AND `po3`.`attribute` = 'Framed-Pool')
 LEFT JOIN `radgroupreply` `r7` ON (`r7`.`groupname` = `p`.`profile_name` AND `r7`.`attribute` = 'Mikrotik-Rate-Limit')
 LEFT JOIN `radgroupreply` `r8` ON (`r8`.`groupname` = `p`.`profile_name` AND `r8`.`attribute` = 'Mikrotik-Address-List')
 LEFT JOIN `radgroupreply` `po4` ON (`po4`.`groupname` = `p`.`profile_name` AND `po4`.`attribute` = 'Framed-Pool')
LEFT JOIN `card_users` ON (`card_users`.`id` = `radcheck`.`id_card`)
LEFT JOIN `radreply` as `radip` ON (`radip`.`username` = `r9`.`username` AND `radip`.`attribute`='Framed-IP-Address' )
LEFT JOIN `radcheck` as `radmac` ON (`radmac`.`username` = `r9`.`username` AND `radmac`.`attribute`='Calling-Station-Id' )
LEFT JOIN `radcheck` as `radcount` ON (`radcount`.`username` = `r9`.`username` AND `radcount`.`attribute`='Simultaneous-Use' )
LEFT JOIN `radcheck` as `radexp` ON (`radexp`.`username` = `r9`.`username` AND `radexp`.`attribute`='Expiration' )
WHERE `radcheck`.`attribute` = 'Cleartext-Password' AND ( radacct.acctstoptime IS NULL ) 
GROUP BY `radacct`.`radacctid`") or exit(mysqli_error($connect));
$send = "";
$dis = "";
$arr_dis = array();
$arr_nas = array();
$arr_end_cards = array();
if (0 < mysqli_num_rows($sel_online)) {
    $n2 = 0;
    $dec_arr = explode("\$\$", decryptIt(shell_exec("sudo cat /var/lib/mysql/mysql5.5.flag 2>&1 2>/dev/null")));
    $c_nas = $dec_arr[0];
    $sel_anas = mysqli_query($connect, "SELECT * FROM `nas` WHERE `nasname` ='0.0.0.0'");
    $fet_anas = mysqli_fetch_assoc($sel_anas);
    while ($fet_online = mysqli_fetch_assoc($sel_online)) {
        if (empty($fet_online["secret"])) {
            $fet_online["secret"] = $fet_anas["secret"];
            $fet_online["id_nas"] = $fet_anas["id"];
            $fet_online["nasname"] = $fet_anas["nasname"];
            $fet_online["shortname"] = $fet_anas["shortname"];
            $fet_online["type"] = $fet_anas["type"];
            $fet_online["server"] = $fet_anas["server"];
            $fet_online["description"] = $fet_anas["description"];
            $fet_online["up_by"] = $fet_anas["up_by"];
            $fet_online["api_username"] = $fet_anas["api_username"];
            $fet_online["api_password"] = $fet_anas["api_password"];
            $fet_online["api_port"] = $fet_anas["api_port"];
            $fet_online["api_is_enabled"] = $fet_anas["api_is_enabled"];
            $fet_online["val_mik"] = $fet_anas["val_mik"];
            $fet_online["webport"] = $fet_anas["webport"];
        }
        $fet_online["acctup"] = $fet_online["acctup"];
        $fet_online["acctdown"] = $fet_online["acctdown"];
        $fet_online["accttime"] = $fet_online["accttime"];
        $fet_online["all_tu"] = $fet_online["all_tu"];
        $fet_online["down_uq"] = $fet_online["down_uq"];
        $fet_online["up_uq"] = $fet_online["up_uq"];
        $fet_online["all_tc"] = $fet_online["all_tc"];
        $fet_online["down_cq"] = $fet_online["down_cq"];
        $fet_online["up_cq"] = $fet_online["up_cq"];
        $sp_up = $fet_online["sp_up"];
        $sp_down = $fet_online["sp_down"];
        if ($sp_up == 1 || $sp_down == 1) {
            $sel_count_user_online_prof = mysqli_query($connect, "SELECT COUNT(DISTINCT `radacct`.`radacctid` ) as `count_login_prof` FROM `radacct`
 WHERE ( radacct .AcctStopTime IS NULL) AND `radacct`.`groupname` = '" . $fet_online["profile_name"] . "'");
            $arr_prof = array();
            $fet_c_u_on_prof = mysqli_fetch_assoc($sel_count_user_online_prof);
            $c_on_prof_u = $fet_c_u_on_prof["count_login_prof"];
        } else {
            $c_on_prof_u = 1;
        }
        if ($fet_online["divi_down_speed_slm"] == 1 || $fet_online["divi_up_speed_slm"] == 1) {
            $sel_c_sess = mysqli_query($connect, "SELECT COUNT(*) as `rad_count_sess` FROM `radacct` WHERE acctstoptime IS NULL AND username = '" . $fet_online["username"] . "'  GROUP BY `username` ");
            $fet_c_sess = mysqli_fetch_assoc($sel_c_sess);
            $rad_count_sess = $fet_c_sess["rad_count_sess"];
            if ($rad_count_sess == 0 || !$rad_count_sess) {
                $rad_count_sess = 1;
            }
        } else {
            $rad_count_sess = 1;
        }
        if ($fet_online["divi_down_speed_slm"] == 1) {
            $div_down_speed = $rad_count_sess;
        } else {
            $div_down_speed = 1;
        }
        if ($fet_online["divi_up_speed_slm"] == 1) {
            $div_up_speed = $rad_count_sess;
        } else {
            $div_up_speed = 1;
        }
        if ($c_on_prof_u == 0 || !$c_on_prof_u) {
            $c_on_prof_u = 1;
        }
        if ($sp_up == 1) {
            $sp_up_count_online = $c_on_prof_u;
        } else {
            $sp_up_count_online = 1;
        }
        if ($sp_down == 1) {
            $sp_down_count_online = $c_on_prof_u;
        } else {
            $sp_down_count_online = 1;
        }
        $fet_online["all_up"] = $fet_online["up_uq"] + $fet_online["acctup"];
        $fet_online["all_down"] = $fet_online["down_uq"] + $fet_online["acctdown"];
        $fet_online["all_time"] = $fet_online["all_tu"] + $fet_online["accttime"];
        $all_quota_uasge = $fet_online["all_up"] + $fet_online["all_down"];
        $id_prof = $fet_online["id_prof"];
        $val_prof = get_profile($fet_online["id_prof"], $all_quota_uasge, $fet_online["av_qouta"], $id_prof);
        if (0 < $fet_online["up_daily_qouta"] || 0 < $fet_online["down_daily_qouta"] || 0 < $profiles["daily_profile_qouta"]) {
            $sel_d_usage = mysqli_query($connect, "
SELECT 
SUM(`r7`.`acctinputoctets` * (r7.percent_q/100)) as `up_dq`,
SUM(`r7`.`acctoutputoctets` * (r7.percent_q/100)) as `down_dq`
 FROM `radacct` as `r7` WHERE  DATE_FORMAT(STR_TO_DATE(`r7`.`acctstarttime`,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND r7.username = '" . $fet_online["username"] . "'
");
            $fet_d_usage = mysqli_fetch_assoc($sel_d_usage);
            $fet_online["up_today"] = $fet_d_usage["up_dq"];
            $fet_online["down_today"] = $fet_d_usage["down_dq"];
            $fet_online["daily_time"] = $fet_online["time_dq"] + $fet_online["accttime"];
        } else {
            $fet_online["up_today"] = 0;
            $fet_online["down_today"] = 0;
            $fet_online["daily_time"] = 0;
        }
        $all_daily_uasge = $fet_online["down_today"] + $fet_online["up_today"];
        $val_percent = $fet_online["percent_q"];
        if ($fet_online["id_card2"] == 0) {
            if (!empty($fet_online["portid_m"])) {
                $arr_pid = @explode(",", $fet_online["portid_m"]);
                if (@is_array($arr_pid) && [email protected]_array($fet_online["nasportid"], $arr_pid) || [email protected]_array($arr_pid) && $fet_online["portid_m"] != $nasportid) {
                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 833;
                    $res = exec($dis);
                    $arr_dis[] = $fet_online["radacctid"];
                }
            } else {
                if (!empty($fet_online["portid_p"]) || !empty($fet_online["portid_u"])) {
                    if (!empty($fet_online["portid_u"]) && $fet_online["portid_u"] != $fet_online["nasportid"]) {
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 839;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    } else {
                        if (empty($fet_online["portid_u"]) && !empty($fet_online["portid_p"]) && $fet_online["portid_p"] != $nasportid) {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 843;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        }
                    }
                }
            }
            if ($c_nas == 1 && is_array(unserialize($fet_online["nas_id"])) && 0 < count(unserialize($fet_online["nas_id"])) && !in_array($fet_online["id_nas"], unserialize($fet_online["nas_id"])) && $fet_online["is_ad"] == 0) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 850;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            if ($fet_set["serv_name"] == 1 && $fet_online["serv_name"] != $fet_online["calledstationid"]) {
                $p++;
                if ($p < 1000) {
                    $up_serv = mysqli_query($connect, "UPDATE `userinfo` SET `serv_name` ='" . $fet_online["calledstationid"] . "' WHERE `username` = '" . $fet_online["username"] . "'");
                }
            }
            if ($fet_online["serv_name"] && $fet_online["serv_name"] != $fet_online["calledstationid"]) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 865;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            $address_list_name = $fet_online["address_list_name"];
            $val_address_list = $fet_online["val_address_list"];
            $val_address_exp_list = $fet_online["val_address_exp_list"];
            $val_address_exp_daily_list = $fet_online["val_address_exp_daily_list"];
            $val_address_exp_serv_list = $fet_online["val_address_exp_serv_list"];
            $percent = $fet_online["percent"];
            $percent_exp = $fet_online["percent_exp"];
            $percent_daily_exp = $fet_online["percent_daily_exp"];
            $percent_serv_exp = $fet_online["percent_serv_exp"];
            $value_now = explode(" ", $fet_online["value_now"]);
            if (is_array($value_now) && $value_now[0]) {
                $val_now = explode("/", $value_now[0]);
                $up_sp = str_ireplace("k", "", $val_now[0]) * $percent / 100;
                $down_sp = str_ireplace("k", "", $val_now[1]) * $percent / 100;
                $val_now2 = explode("/", $value_now[1]);
                $up_sp2 = str_ireplace("k", "", $val_now2[0]) * $percent / 100;
                $down_sp2 = str_ireplace("k", "", $val_now2[1]) * $percent / 100;
                $val_now3 = explode("/", $value_now[2]);
                $up_sp3 = str_ireplace("k", "", $val_now3[0]) * $percent / 100;
                $down_sp3 = str_ireplace("k", "", $val_now3[1]) * $percent / 100;
                $val_now4 = explode("/", $value_now[3]);
                $up_t4 = round($val_now4[0] * $percent / 100);
                $down_t4 = round($val_now4[1] * $percent / 100);
                $val_burst_now = round($up_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4 . "/" . $down_t4 . " " . $value_now[4];
            }
            $value_exp = explode(" ", $fet_online["value_exp"]);
            if (is_array($value_exp) && $value_exp[0]) {
                $value_exp2 = explode("/", $value_exp[0]);
                $up_exp_sp = str_ireplace("k", "", $value_exp2[0]) * $percent_exp / 100;
                $down_exp_sp = str_ireplace("k", "", $value_exp2[1]) * $percent_exp / 100;
                $val_exp2 = explode("/", $value_exp[1]);
                $up_exp2 = str_ireplace("k", "", $val_exp2[0]) * $percent / 100;
                $down_exp2 = str_ireplace("k", "", $val_exp2[1]) * $percent / 100;
                $val_exp3 = explode("/", $value_exp[2]);
                $up_exp3 = str_ireplace("k", "", $val_exp3[0]) * $percent / 100;
                $down_exp3 = str_ireplace("k", "", $val_exp3[1]) * $percent / 100;
                $val_exp4 = explode("/", $value_exp[3]);
                $up_t4_exp = round($val_exp4[0] * $percent / 100);
                $down_t4_exp = round($val_exp4[1] * $percent / 100);
                $val_burst_exp_sp = round($up_exp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp . "/" . $down_t4_exp . " " . $value_exp[4];
            }
            $value_exp_daily = explode(" ", $fet_online["value_exp_daily"]);
            if (is_array($value_exp_daily) && $value_exp_daily[0]) {
                $value_exp_daily2 = explode("/", $value_exp_daily[0]);
                $up_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[0]) * $percent_daily_exp / 100;
                $down_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[1]) * $percent_daily_exp / 100;
                $val_exp_daily2 = explode("/", $value_exp_daily[1]);
                $up_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[0]) * $percent / 100;
                $down_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[1]) * $percent / 100;
                $val_exp_daily3 = explode("/", $value_exp_daily[2]);
                $up_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[0]) * $percent / 100;
                $down_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[1]) * $percent / 100;
                $val_exp_daily4 = explode("/", $value_exp_daily[3]);
                $up_t4_exp_da_sp = round($val_exp_daily4[0] * $percent / 100);
                $down_t4_exp_da_sp = round($val_exp_daily4[1] * $percent / 100);
                $val_burst_exp_da_sp = round($up_exp_da_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_da_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_da_sp . "/" . $down_t4_exp_da_sp . " " . $value_exp_daily[4];
            }
            $value_exp_serv = explode(" ", $fet_online["value_exp_serv"]);
            if (is_array($value_exp_serv) && $value_exp_serv[0]) {
                $value_exp_serv2 = explode("/", $value_exp_serv[0]);
                $up_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[0]) * $percent_serv_exp / 100;
                $down_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[1]) * $percent_serv_exp / 100;
                $val_exp_serv2 = explode("/", $value_exp_serv[1]);
                $up_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[0]) * $percent / 100;
                $down_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[1]) * $percent / 100;
                $val_exp_serv3 = explode("/", $value_exp_serv[2]);
                $up_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[0]) * $percent / 100;
                $down_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[1]) * $percent / 100;
                $val_exp_serv4 = explode("/", $value_exp_serv[3]);
                $up_t4_exp_serv_sp = round($val_exp_serv4[0] * $percent / 100);
                $down_t4_exp_serv_sp = round($val_exp_serv4[1] * $percent / 100);
                $val_burst_exp_serv_sp = round($up_exp_serv_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_serv_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_serv_sp . "/" . $down_t4_exp_serv_sp . " " . $value_exp_serv[4];
            }
            $arr_times = unserialize($fet_online["bandwidth_time"]);
            $arr_m_speed = unserialize($fet_online["multi_speed"]);
            $arr_times_percent = unserialize($fet_online["bandwidth_percent"]);
            $arr_times_exp = unserialize($fet_online["band_exp"]);
            $arr_times_exp_percent = unserialize($fet_online["band_exp_percent"]);
            $arr_times_d_exp = unserialize($fet_online["band_daily_exp"]);
            $arr_times_d_exp_percent = unserialize($fet_online["band_daily_exp_percent"]);
            $arr_times_serv_exp = unserialize($fet_online["band_serv_exp"]);
            $arr_times_serv_exp_percent = unserialize($fet_online["band_serv_exp_percent"]);
            if ($fet_set["lock_by_ip"] == 1 && !$fet_online["ip_address"] && $fet_online["simul_sess"] == 1) {
                $insert = mysqli_query($connect, "INSERT INTO `radreply` (`username`,`attribute`,`op`,`value`) VALUES ('" . $fet_online["username"] . "','Framed-IP-Address',':=','" . $fet_online["framedipaddress"] . "')");
            }
            if ($fet_set["lock_by_mac"] == 1 && !$fet_online["mac_address"] && $fet_online["mac_address"] != $fet_online["username"] && $fet_online["simul_sess"] == 1) {
                $delete = mysqli_query($connect, "DELETE FROM radcheck WHERE username='" . $fet_online["username"] . "' AND attribute='Calling-Station-Id'") or exit(mysqli_error($connect));
                $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`) VALUES ('" . $fet_online["username"] . "','Calling-Station-Id','==','" . $fet_online["callingstationid"] . "')");
            }
            if ($fet_online["dis"] == 1) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 981;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            if ($fet_online["session_st_date"] < $fet_online["updatedate"]) {
                mysqli_query($connect, "UPDATE `radacct` SET `acctstarttime` = '" . @date("Y-m-d H:i:s") . "' WHERE ( radacct .AcctStopTime IS NULL )");
            }
            $macs = unserialize($fet_online["macs"]);
            $macs = @array_filter($macs);
            if (is_array($macs) && 0 < count($macs)) {
                if (!empty($fet_online["mac_address"])) {
                    $macs[] = $fet_online["mac_address"];
                }
                $mac = $fet_online["callingstationid"];
                if (!in_array($mac, $macs)) {
                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1002;
                    $res = exec($dis);
                    $arr_dis[] = $fet_online["radacctid"];
                }
            }
            if ($fet_online["hours_min"] == 0) {
                $t_online_prof = $fet_online["online_time"] * 60;
            } else {
                $t_online_prof = $fet_online["online_time"] * 60 * 60;
            }
            if ($fet_online["daily_hours_min"] == 0) {
                $d_online_prof = $fet_online["daily_online_time"] * 60;
            } else {
                $d_online_prof = $fet_online["daily_online_time"] * 60 * 60;
            }
            if ($fet_online["u_value_choice"] == 1) {
                $arr_days = explode(",", $fet_online["u_arr_days"]);
                $today_work = arraySearch($arr_days, date("D") . "1");
                if (empty($today_work)) {
                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1023;
                    $res = exec($dis);
                    $arr_dis[] = $fet_online["radacctid"];
                }
            } else {
                if ($fet_online["p_value_choice"] == 1) {
                    $arr_days = explode(",", $fet_online["p_arr_days"]);
                    $today_work = arraySearch($arr_days, date("D") . "1");
                    if (empty($today_work)) {
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1031;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                }
            }
            if (0 < $fet_online["t_online_time"] && $fet_online["t_online_time"] <= $fet_online["all_time"] || 0 < $t_online_prof && $t_online_prof <= $fet_online["all_time"] || 0 < $fet_online["d_online_time"] && $fet_online["d_online_time"] <= $fet_online["daily_time"] || 0 < $d_online_prof && $d_online_prof <= $fet_online["daily_time"] || $fet_set["block_login"] == 1) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1042;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            $sess_day = strtotime($fet_online["acctstarttime"]);
            $time_renew = $fet_online["updatedate"];
            if ($sess_day < $time_renew) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1063;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            $address_list_name = $fet_online["address_list_name"];
            $val_address_list = $profiles["val_address_list"];
            $val_address_exp_list = $profiles["val_address_exp_list"];
            $val_address_exp_daily_list = $profiles["val_address_exp_daily_list"];
            $val_address_exp_serv_list = $profiles["val_address_exp_serv_list"];
            $percent = $profiles["percent"];
            $percent_exp = $profiles["percent_exp"];
            $percent_daily_exp = $profiles["percent_daily_exp"];
            $percent_serv_exp = $profiles["percent_serv_exp"];
            $value_now = explode(" ", $profiles["value_now"]);
            if (is_array($value_now) && $value_now[0]) {
                $val_now = explode("/", $value_now[0]);
                $up_sp = str_ireplace("k", "", $val_now[0]) * $percent / 100;
                $down_sp = str_ireplace("k", "", $val_now[1]) * $percent / 100;
                $val_now2 = explode("/", $value_now[1]);
                $up_sp2 = str_ireplace("k", "", $val_now2[0]) * $percent / 100;
                $down_sp2 = str_ireplace("k", "", $val_now2[1]) * $percent / 100;
                $val_now3 = explode("/", $value_now[2]);
                $up_sp3 = str_ireplace("k", "", $val_now3[0]) * $percent / 100;
                $down_sp3 = str_ireplace("k", "", $val_now3[1]) * $percent / 100;
                $val_now4 = explode("/", $value_now[3]);
                $up_t4 = round($val_now4[0] * $percent / 100);
                $down_t4 = round($val_now4[1] * $percent / 100);
                $val_burst_now = round($up_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4 . "/" . $down_t4 . " " . $value_now[4];
            }
            $value_exp = explode(" ", $profiles["value_exp"]);
            if (is_array($value_exp) && $value_exp[0]) {
                $value_exp2 = explode("/", $value_exp[0]);
                $up_exp_sp = str_ireplace("k", "", $value_exp2[0]) * $percent_exp / 100;
                $down_exp_sp = str_ireplace("k", "", $value_exp2[1]) * $percent_exp / 100;
                $val_exp2 = explode("/", $value_exp[1]);
                $up_exp2 = str_ireplace("k", "", $val_exp2[0]) * $percent / 100;
                $down_exp2 = str_ireplace("k", "", $val_exp2[1]) * $percent / 100;
                $val_exp3 = explode("/", $value_exp[2]);
                $up_exp3 = str_ireplace("k", "", $val_exp3[0]) * $percent / 100;
                $down_exp3 = str_ireplace("k", "", $val_exp3[1]) * $percent / 100;
                $val_exp4 = explode("/", $value_exp[3]);
                $up_t4_exp = round($val_exp4[0] * $percent / 100);
                $down_t4_exp = round($val_exp4[1] * $percent / 100);
                $val_burst_exp_sp = round($up_exp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp . "/" . $down_t4_exp . " " . $value_exp[4];
            }
            $value_exp_daily = explode(" ", $profiles["value_exp_daily"]);
            if (is_array($value_exp_daily) && $value_exp_daily[0]) {
                $value_exp_daily2 = explode("/", $value_exp_daily[0]);
                $up_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[0]) * $percent_daily_exp / 100;
                $down_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[1]) * $percent_daily_exp / 100;
                $val_exp_daily2 = explode("/", $value_exp_daily[1]);
                $up_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[0]) * $percent / 100;
                $down_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[1]) * $percent / 100;
                $val_exp_daily3 = explode("/", $value_exp_daily[2]);
                $up_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[0]) * $percent / 100;
                $down_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[1]) * $percent / 100;
                $val_exp_daily4 = explode("/", $value_exp_daily[3]);
                $up_t4_exp_da_sp = round($val_exp_daily4[0] * $percent / 100);
                $down_t4_exp_da_sp = round($val_exp_daily4[1] * $percent / 100);
                $val_burst_exp_da_sp = round($up_exp_da_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_da_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_da_sp . "/" . $down_t4_exp_da_sp . " " . $value_exp_daily[4];
            }
            $value_exp_serv = explode(" ", $profiles["value_exp_serv"]);
            if (is_array($value_exp_serv) && $value_exp_serv[0]) {
                $value_exp_serv2 = explode("/", $value_exp_serv[0]);
                $up_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[0]) * $percent_serv_exp / 100;
                $down_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[1]) * $percent_serv_exp / 100;
                $val_exp_serv2 = explode("/", $value_exp_serv[1]);
                $up_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[0]) * $percent / 100;
                $down_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[1]) * $percent / 100;
                $val_exp_serv3 = explode("/", $value_exp_serv[2]);
                $up_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[0]) * $percent / 100;
                $down_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[1]) * $percent / 100;
                $val_exp_serv4 = explode("/", $value_exp_serv[3]);
                $up_t4_exp_serv_sp = round($val_exp_serv4[0] * $percent / 100);
                $down_t4_exp_serv_sp = round($val_exp_serv4[1] * $percent / 100);
                $val_burst_exp_serv_sp = round($up_exp_serv_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_serv_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_serv_sp . "/" . $down_t4_exp_serv_sp . " " . $value_exp_serv[4];
            }
            $arr_times = unserialize($profiles["bandwidth_time"]);
            $arr_m_speed = unserialize($profiles["multi_speed"]);
            $arr_times_percent = unserialize($profiles["bandwidth_percent"]);
            $arr_times_exp = unserialize($profiles["band_exp"]);
            $arr_times_exp_percent = unserialize($profiles["band_exp_percent"]);
            $arr_times_d_exp = unserialize($profiles["band_daily_exp"]);
            $arr_times_d_exp_percent = unserialize($profiles["band_daily_exp_percent"]);
            $arr_times_serv_exp = unserialize($profiles["band_serv_exp"]);
            $arr_times_serv_exp_percent = unserialize($profiles["band_serv_exp_percent"]);
            if ($profiles["profile_expire_service"] == 0 && $fet_online["exp_user"] < time()) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1161;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            } else {
                if ($profiles["profile_expire_service"] != 0 && $fet_online["exp_user"] < time()) {
                    if (!empty($val_address_exp_serv_list) && $val_address_exp_serv_list != $address_list_name) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_serv_list . "',`address_val`='5' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1171;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_serv_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 1175;
                            $res = exec($send);
                        }
                    }
                    if ($fet_online["framed_pool"] != $profiles["ex_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["ex_pool"] . "',`address_val`='5' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1183;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (is_array($arr_times_serv_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_serv_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_serv_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_serv_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_serv_exp[$s]["from_time"], $arr_times_serv_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_serv_exp[$s]["up_rate"];
                            $down_rate = $arr_times_serv_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_serv_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_serv_sp;
                        $sp = round($up_exp_serv_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_serv_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_serv_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_serv_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_serv_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_serv_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_serv_exp_percent[$s]["frombq_time"], $arr_times_serv_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_serv_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_serv_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s'))
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if ($fet_online["address_val"] == 5) {
                        $update_nothing_address_list = mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_list . "',`address_val`='3' WHERE `username`='" . $fet_online["username"] . "' AND `attribute` = 'Cleartext-Password'") or exit(mysqli_error($connect));
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1388;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 1392;
                            $res = exec($send);
                        }
                        if ($fet_online["framed_pool"] != $fet_online["ad_pool"]) {
                            mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["ad_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1398;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        }
                    }
                    if ($profiles["daily_expire_service"] != 0 && 0 < $profiles["up_daily_qouta"] && $profiles["up_daily_qouta"] < $fet_online["up_today"] && $profiles["daily_profile_qouta"] == 0) {
                        if ($profiles["qouta_expire_service"] != 0 && 0 < $fet_online["up_qouta"] && $fet_online["up_qouta"] < $fet_online["all_up"] && $profiles["q"] == 0) {
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0;$s < count($arr_times_exp);$s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_exp[$s]["up_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                            }
                        } else {
                            if (is_array($arr_times_d_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0;$s < count($arr_times_d_exp);$s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_d_exp[$s]["up_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                            }
                            $nn = 0;
                            if (is_array($arr_times_d_exp_percent)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                for ($s = 0;$s < count($arr_times_d_exp_percent);$s++) {
                                    $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                                    $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                                    if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                        $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                    }
                                    $tobq_time = strtotime($tobq_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                                    $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                                    if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                        $today_work_bq = arraySearch($sel_day, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $val_percent = $percent_bq;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (0 < $nn) {
                                if ($val_percent != $fet_online["percent_q"]) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                    }
                                }
                            } else {
                                if ($val_percent != 100) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s'))
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                    }
                                }
                            }
                        }
                    } else {
                        if ($profiles["qouta_expire_service"] != 0 && 0 < $fet_online["up_qouta"] && $fet_online["up_qouta"] < $fet_online["all_up"] && $profiles["q"] == 0) {
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0;$s < count($arr_times_exp);$s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_exp[$s]["up_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                            }
                        } else {
                            if ($profiles["q"] == 0 && (0 < $fet_online["down_qouta"] || 0 < $fet_online["up_qouta"]) && $profiles["daily_profile_qouta"] == 0) {
                                if (is_array($arr_times)) {
                                    $clock = strtotime(date("Y-m-d H:i:s"));
                                    $s = 0;
                                    for ($nn = 0;$s < count($arr_times);$s++) {
                                        $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                        $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                        if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                            $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                        }
                                        $to_time = strtotime($to_time);
                                        $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                        $up_rate = $arr_times[$s]["up_rate"];
                                        if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                            $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                            $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                                if ($nn == 0) {
                                    $speed = round($up_sp / $div_up_speed / $sp_up_count_online) . "K/";
                                    $sp = round($up_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                }
                            }
                        }
                    }
                    if ($profiles["daily_expire_service"] != 0 && 0 < $profiles["down_daily_qouta"] && $profiles["down_daily_qouta"] < $fet_online["down_today"] && $profiles["daily_profile_qouta"] == 0) {
                        if ($profiles["qouta_expire_service"] != 0 && 0 < $fet_online["down_qouta"] && $fet_online["down_qouta"] < $fet_online["all_down"] && $profiles["q"] == 0) {
                            $s = 0;
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0;$s < count($arr_times_exp);$s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $down_rate = $arr_times_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed.= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp.= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed.= round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K";
                                $sp.= round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                        } else {
                            if (is_array($arr_times_d_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0;$s < count($arr_times_d_exp);$s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $down_rate = $arr_times_d_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed.= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp.= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed.= round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) . "K";
                                $sp.= round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                            $nn = 0;
                            if (is_array($arr_times_d_exp_percent)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                for ($s = 0;$s < count($arr_times_d_exp_percent);$s++) {
                                    $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                                    $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                                    if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                        $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                    }
                                    $tobq_time = strtotime($tobq_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                                    $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                                    if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                        $today_work_bq = arraySearch($sel_day, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $val_percent = $percent_bq;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (0 < $nn) {
                                if ($val_percent != $fet_online["percent_q"]) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                    }
                                }
                            } else {
                                if ($val_percent != 100) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s'))
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                    }
                                }
                            }
                        }
                    } else {
                        if ($profiles["qouta_expire_service"] != 0 && 0 < $fet_online["down_qouta"] && $fet_online["down_qouta"] < $fet_online["all_down"] && $profiles["q"] == 0) {
                            $s = 0;
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0;$s < count($arr_times_exp);$s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $down_rate = $arr_times_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed.= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp.= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed.= round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K";
                                $sp.= round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                        } else {
                            if ($profiles["q"] == 0 && (0 < $fet_online["down_qouta"] || 0 < $fet_online["up_qouta"]) && $profiles["daily_profile_qouta"] == 0) {
                                if (is_array($arr_times)) {
                                    $clock = strtotime(date("Y-m-d H:i:s"));
                                    $s = 0;
                                    for ($nn = 0;$s < count($arr_times);$s++) {
                                        $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                        $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                        if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                            $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                        }
                                        $to_time = strtotime($to_time);
                                        $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                        $down_rate = $arr_times[$s]["down_rate"];
                                        if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                            $speed.= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                            $sp.= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                                if ($nn == 0) {
                                    $speed.= round($down_sp / $div_down_speed / $sp_down_count_online) . "K";
                                    $sp.= round($down_sp / $div_down_speed / $sp_down_count_online) * 1000;
                                }
                            }
                        }
                    }
                    if ($profiles["qouta_expire_service"] == "-1" && $profiles["q"] != 0 && $profiles["q"] < $all_quota_uasge) {
                        $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2000;
                        $res = exec($send);
                        mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                        $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s") . "')") or exit(mysqli_error($connect));
                    }
                    if ($profiles["daily_expire_service"] != 0 && 0 < $profiles["daily_profile_qouta"] && $profiles["daily_profile_qouta"] < $all_daily_uasge) {
                        if ($profiles["qouta_expire_service"] != 0 && $profiles["q"] != 0 && $profiles["q"] < $all_quota_uasge) {
                            if (!empty($val_address_exp_list) && $val_address_exp_list != $address_list_name) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_list . "',`address_val`='1' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2017;
                                    $res = exec($dis);
                                    $arr_dis[] = $fet_online["radacctid"];
                                } else {
                                    $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2021;
                                    $res = exec($send);
                                }
                            }
                            if ($fet_online["framed_pool"] != $profiles["q_pool"]) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["q_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2028;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            }
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0;$s < count($arr_times_exp);$s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_exp[$s]["up_rate"];
                                    $down_rate = $arr_times_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                                $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                            $nn = 0;
                            if (is_array($arr_times_exp_percent)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                for ($s = 0;$s < count($arr_times_exp_percent);$s++) {
                                    $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                                    $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                                    if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                        $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                    }
                                    $tobq_time = strtotime($tobq_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $percent_bq = $arr_times_exp_percent[$s]["percent"];
                                    $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                                    if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                        echo $sel_day;
                                        $today_work_bq = arraySearch($sel_day, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $val_percent = $percent_bq;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (0 < $nn) {
                                if ($val_percent != $fet_online["percent_q"]) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                    }
                                }
                            } else {
                                if ($val_percent != 100) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                    }
                                }
                            }
                        } else {
                            if (!empty($val_address_exp_daily_list) && $val_address_exp_daily_list != $address_list_name) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_daily_list . "',`address_val` = '2' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2237;
                                    $res = exec($dis);
                                    $arr_dis[] = $fet_online["radacctid"];
                                } else {
                                    $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_daily_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2241;
                                    $res = exec($send);
                                }
                            }
                            if ($fet_online["framed_pool"] != $profiles["d_pool"]) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["d_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2261;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            }
                            if (is_array($arr_times_d_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0;$s < count($arr_times_d_exp);$s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_d_exp[$s]["up_rate"];
                                    $down_rate = $arr_times_d_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_da_sp;
                                $sp = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                        }
                    } else {
                        if ($profiles["qouta_expire_service"] != 0 && $profiles["q"] != 0 && $profiles["q"] < $all_quota_uasge) {
                            if (!empty($val_address_exp_list) && $val_address_exp_list != $address_list_name) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_list . "',`address_val`='1' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2305;
                                    $res = exec($dis);
                                    $arr_dis[] = $fet_online["radacctid"];
                                } else {
                                    $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2309;
                                    $res = exec($send);
                                }
                            }
                            if ($fet_online["framed_pool"] != $profiles["q_pool"]) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["q_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2316;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            }
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0;$s < count($arr_times_exp);$s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_exp[$s]["up_rate"];
                                    $down_rate = $arr_times_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0 && $profiles["qouta_expire_service"] == "-1") {
                                $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                                $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                        } else {
                            if ($fet_online["address_val"] != 3 || $address_list_name != $val_address_list) {
                                $update_nothing_address_list = mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_list . "',`address_val`='3' WHERE `username`='" . $fet_online["username"] . "' AND `attribute` = 'Cleartext-Password'") or exit(mysqli_error($connect));
                                if ($address_list_name != $val_address_list) {
                                    if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2367;
                                        $res = exec($dis);
                                        $arr_dis[] = $fet_online["radacctid"];
                                    } else {
                                        $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2371;
                                        $res = exec($send);
                                    }
                                }
                            }
                            if ($fet_online["framed_pool"] != $profiles["ad_pool"]) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["ad_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2379;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            }
                            $val_speed = 0;
                            if (is_array($arr_m_speed) && $profiles["q"] != 0) {
                                $s = 0;
                                for ($nn = 0;$s < count($arr_m_speed);$s++) {
                                    $from_val = $arr_m_speed[$s]["from_val"];
                                    $to_val = $arr_m_speed[$s]["to_val"];
                                    $up_rval = $arr_m_speed[$s]["up_rval"];
                                    $down_rval = $arr_m_speed[$s]["down_rval"];
                                    $sel_day_val = $arr_m_speed[$s]["sel_day_val"];
                                    $perc_q = $all_quota_uasge / $profiles["q"] * 100;
                                    if ($from_val <= $perc_q && $perc_q <= $to_val) {
                                        echo $sel_day_val;
                                        $today_work_bq = arraySearch($sel_day_val, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $speed = round($up_rval / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rval / $div_down_speed / $sp_down_count_online) . "K";
                                            $sp = round($up_rval / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rval / $div_down_speed / $sp_down_count_online) * 1000;
                                            $val_speed = 1;
                                            break;
                                        }
                                    }
                                }
                            }
                            if ((empty($speed) || !$speed) && $val_speed == 0) {
                                if (is_array($arr_times)) {
                                    $clock = strtotime(date("Y-m-d H:i:s"));
                                    $s = 0;
                                    for ($nn = 0;$s < count($arr_times);$s++) {
                                        $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                        $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                        if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                            $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                        }
                                        $to_time = strtotime($to_time);
                                        $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                        $up_rate = $arr_times[$s]["up_rate"];
                                        $down_rate = $arr_times[$s]["down_rate"];
                                        if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                            $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                            $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                                if ($nn == 0) {
                                    $speed = round($up_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_now;
                                    $sp = round($up_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_sp / $div_down_speed / $sp_down_count_online) * 1000;
                                }
                            }
                            $nn = 0;
                            if (is_array($arr_times_percent)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                for ($s = 0;$s < count($arr_times_percent);$s++) {
                                    $frombq_time = strtotime(date("Y-m-d ") . $arr_times_percent[$s]["frombq_time"] . ":00");
                                    $tobq_time = date("Y-m-d ") . $arr_times_percent[$s]["tobq_time"] . ":00";
                                    if (is_new($arr_times_percent[$s]["frombq_time"], $arr_times_percent[$s]["tobq_time"]) == 1) {
                                        $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                    }
                                    $tobq_time = strtotime($tobq_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $percent_bq = $arr_times_percent[$s]["percent"];
                                    $sel_day = $arr_times_percent[$s]["sel_day"];
                                    if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                        $today_work_bq = arraySearch($sel_day, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $val_percent = $percent_bq;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (0 < $nn) {
                                if ($val_percent != $fet_online["percent_q"]) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                    }
                                }
                            } else {
                                if ($val_percent != 100) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if ($fet_online["framedprotocol"] == "PPP") {
                if ($fet_online["up_by"] == 1) {
                    if ($fet_online["l_speed"] != $speed && $fet_online["stop_u_speed"] == 0) {
                        $send = "echo  " . "\"" . "user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',mikrotik-rate-limit='" . $speed . "'" . "\"" . " |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2620;
                        $res = exec($send);
                        mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'") or exit(mysqli_error($connect));
                    }
                } else {
                    if ($fet_online["api_is_en"] == 1) {
                        if ($m != @end(@array_keys($arr_nas[$fet_online["nasname"]]["details"]))) {
                            $m = 0;
                        } else {
                            $m = @end(@array_keys($arr_nas[$fet_online["nasname"]]["details"])) + 1;
                        }
                        if ($fet_online["l_speed"] != $speed) {
                            if ($arr_nas[$fet_online["nasname"]]["nasname"] != $fet_online["nasname"]) {
                                $arr_nas[$fet_online["nasname"]]["nasname"] = $fet_online["nasname"];
                                $arr_nas[$fet_online["nasname"]]["api_username"] = $fet_online["api_username"];
                                $arr_nas[$fet_online["nasname"]]["api_password"] = $fet_online["api_password"];
                            }
                            $arr_nas[$fet_online["nasname"]]["api_port"] = $fet_online["api_port"];
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["username"] = $fet_online["username"];
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["speed"] = $speed;
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["sp"] = $sp;
                            mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                        }
                    }
                }
            } else {
                if ($fet_online["l_speed"] != $speed && $fet_online["stop_u_speed"] == 0) {
                    $send = "echo  " . "\"" . "user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',mikrotik-rate-limit='" . $speed . "'" . "\"" . " |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2652;
                    mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                    $res = exec($send);
                }
            }
        } else {
            $fet_online["all_up"] = $fet_online["up_cq"] + $fet_online["acctup"];
            $fet_online["all_down"] = $fet_online["down_cq"] + $fet_online["acctdown"];
            $fet_online["all_time"] = $fet_online["all_tc"] + $fet_online["accttime"];
            $exp_first_login = $fet_online["exp_first_login"];
            $val_time_exp = $fet_online["val_time_exp"];
            $time_cards_exp = $fet_online["time_cards_exp"];
            $exp_quota = $fet_online["exp_quota"];
            if ($exp_first_login == 1 && !$fet_online["exp_user"]) {
                $sel_first_login = mysqli_query($connect, "SELECT `acctstarttime` FROM `radacct` WHERE `username`='" . $fet_online["username"] . "' ORDER BY `radacctid` ASC LIMIT 1");
                $fet_f_login = mysqli_fetch_assoc($sel_first_login);
                $time_first_login = strtotime($fet_f_login["acctstarttime"]);
                if ($time_cards_exp == 1) {
                    $t_exp_2 = $time_first_login + 60 * 60 * 24 * $val_time_exp;
                } else {
                    if ($time_cards_exp == 2) {
                        $t_exp_2 = $time_first_login + 60 * 60 * $val_time_exp;
                    } else {
                        if ($time_cards_exp == 3) {
                            $t_exp_2 = $time_first_login + 60 * $val_time_exp;
                        }
                    }
                }
                if ($t_exp_2 < time()) {
                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2689;
                    $res = exec($dis);
                    $arr_dis[] = $fet_online["radacctid"];
                    mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                    $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s") . "','1','" . $fet_online["id_card2"] . "')") or exit(mysqli_error($connect));
                    $ad_list_empty = 1;
                } else {
                    $ad_list_empty = 0;
                }
            } else {
                $ad_list_empty = 0;
            }
            if ($ad_list_empty == 1) {
                $address_list_name = "";
                $val_address_list = "";
                $val_address_exp_list = "";
                $val_address_exp_daily_list = "";
            } else {
                $address_list_name = $fet_online["address_list_name"];
                $val_address_list = $fet_online["val_address_list"];
                $val_address_exp_list = $fet_online["val_address_exp_list"];
                $val_address_exp_daily_list = $fet_online["val_address_exp_daily_list"];
            }
            if ($fet_set["lock_by_ip_card"] == 1 && !$fet_online["ip_address"] && $fet_online["simul_sess"] == 1) {
                $insert = @mysqli_query($connect, "INSERT INTO `radreply` (`username`,`attribute`,`op`,`value`) VALUES ('" . $fet_online["username"] . "','Framed-IP-Address',':=','" . $fet_online["framedipaddress"] . "')");
            }
            if ($fet_set["lock_by_mac_card"] == 1 && !$fet_online["mac_address"] && $fet_online["mac_address"] != $fet_online["username"] && $fet_online["simul_sess"] == 1) {
                $delete = mysqli_query($connect, "DELETE FROM radcheck WHERE username='" . $fet_online["username"] . "' AND attribute='Calling-Station-Id'") or exit(mysqli_error($connect));
                $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Calling-Station-Id','==','" . $fet_online["callingstationid"] . "','1','" . $fet_online["id_card2"] . "')");
            }
            $div_down_speed = $rad_count_sess;
            $div_up_speed = $rad_count_sess;
            if ($fet_online["hours_min"] == 0) {
                $t_online_prof = $fet_online["online_time"] * 60;
            } else {
                $t_online_prof = $fet_online["online_time"] * 60 * 60;
            }
            if ($fet_online["daily_hours_min"] == 0) {
                $d_online_prof = $fet_online["daily_online_time"] * 60;
            } else {
                $d_online_prof = $fet_online["daily_online_time"] * 60 * 60;
            }
            if (0 < $t_online_prof && $t_online_prof <= $fet_online["all_time"] || 0 < $d_online_prof && $d_online_prof <= $fet_online["daily_time"] || $fet_set["block_login"] != 0 || $fet_online["exp_date"] && $fet_online["exp_date"] < time()) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2749;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            if ($fet_online["val_date"] == 1) {
                $time_col = $fet_online["date_end_card"] * 24 * 60 * 60;
            } else {
                if ($fet_online["val_date"] == 2) {
                    $time_col = $fet_online["date_end_card"] * 60 * 60;
                } else {
                    $time_col = $fet_online["date_end_card"] * 60;
                }
            }
            if (0 < $fet_online["all_time"] && $time_col <= $fet_online["all_time"] && $fet_online["per_second"] == 1 && $fet_online["at_the_first_login"] == 0) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2774;
                $res = exec($dis);
                if (!$fet_online["exp_user"]) {
                    $arr_end_cards[] = $fet_online["username"];
                    mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                    $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s") . "','1','" . $fet_online["id_card2"] . "')") or exit(mysqli_error($connect));
                }
            }
            if ($fet_online["per_second"] == 0 && $fet_online["at_the_first_login"] == 1 && !$fet_online["exp_user"]) {
                $date_end = time() + $time_col;
                mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s", $date_end) . "','1','" . $fet_online["id_card2"] . "')") or exit(mysqli_error($connect));
            }
            if ($fet_online["exp_user"] && $fet_online["exp_user"] < time()) {
                $arr_end_cards[] = $fet_online["username"];
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2793;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            $percent = $fet_online["percent"];
            $percent_exp = $fet_online["percent_exp"];
            $percent_daily_exp = $fet_online["percent_daily_exp"];
            $percent_serv_exp = $fet_online["percent_serv_exp"];
            $value_now = explode(" ", $fet_online["value_now"]);
            if (is_array($value_now) && $value_now[0]) {
                $val_now = explode("/", $value_now[0]);
                $up_sp = str_ireplace("k", "", $val_now[0]) * $percent / 100;
                $down_sp = str_ireplace("k", "", $val_now[1]) * $percent / 100;
                $val_now2 = explode("/", $value_now[1]);
                $up_sp2 = str_ireplace("k", "", $val_now2[0]) * $percent / 100;
                $down_sp2 = str_ireplace("k", "", $val_now2[1]) * $percent / 100;
                $val_now3 = explode("/", $value_now[2]);
                $up_sp3 = str_ireplace("k", "", $val_now3[0]) * $percent / 100;
                $down_sp3 = str_ireplace("k", "", $val_now3[1]) * $percent / 100;
                $val_now4 = explode("/", $value_now[3]);
                $up_t4 = round($val_now4[0] * $percent / 100);
                $down_t4 = round($val_now4[1] * $percent / 100);
                $val_burst_now = round($up_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4 . "/" . $down_t4 . " " . $value_now[4];
            }
            $value_exp = explode(" ", $fet_online["value_exp"]);
            if (is_array($value_exp) && $value_exp[0]) {
                $value_exp2 = explode("/", $value_exp[0]);
                $up_exp_sp = str_ireplace("k", "", $value_exp2[0]) * $percent_exp / 100;
                $down_exp_sp = str_ireplace("k", "", $value_exp2[1]) * $percent_exp / 100;
                $val_exp2 = explode("/", $value_exp[1]);
                $up_exp2 = str_ireplace("k", "", $val_exp2[0]) * $percent / 100;
                $down_exp2 = str_ireplace("k", "", $val_exp2[1]) * $percent / 100;
                $val_exp3 = explode("/", $value_exp[2]);
                $up_exp3 = str_ireplace("k", "", $val_exp3[0]) * $percent / 100;
                $down_exp3 = str_ireplace("k", "", $val_exp3[1]) * $percent / 100;
                $val_exp4 = explode("/", $value_exp[3]);
                $up_t4_exp = round($val_exp4[0] * $percent / 100);
                $down_t4_exp = round($val_exp4[1] * $percent / 100);
                $val_burst_exp_sp = round($up_exp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp . "/" . $down_t4_exp . " " . $value_exp[4];
            }
            $value_exp_daily = explode(" ", $fet_online["value_exp_daily"]);
            if (is_array($value_exp_daily) && $value_exp_daily[0]) {
                $value_exp_daily2 = explode("/", $value_exp_daily[0]);
                $up_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[0]) * $percent_daily_exp / 100;
                $down_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[1]) * $percent_daily_exp / 100;
                $val_exp_daily2 = explode("/", $value_exp_daily[1]);
                $up_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[0]) * $percent / 100;
                $down_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[1]) * $percent / 100;
                $val_exp_daily3 = explode("/", $value_exp_daily[2]);
                $up_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[0]) * $percent / 100;
                $down_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[1]) * $percent / 100;
                $val_exp_daily4 = explode("/", $value_exp_daily[3]);
                $up_t4_exp_da_sp = round($val_exp_daily4[0] * $percent / 100);
                $down_t4_exp_da_sp = round($val_exp_daily4[1] * $percent / 100);
                $val_burst_exp_da_sp = round($up_exp_da_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_da_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_da_sp . "/" . $down_t4_exp_da_sp . " " . $value_exp_daily[4];
            }
            $value_exp_serv = explode(" ", $fet_online["value_exp_serv"]);
            if (is_array($value_exp_serv) && $value_exp_serv[0]) {
                $value_exp_serv2 = explode("/", $value_exp_serv[0]);
                $up_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[0]) * $percent_serv_exp / 100;
                $down_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[1]) * $percent_serv_exp / 100;
                $val_exp_serv2 = explode("/", $value_exp_serv[1]);
                $up_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[0]) * $percent / 100;
                $down_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[1]) * $percent / 100;
                $val_exp_serv3 = explode("/", $value_exp_serv[2]);
                $up_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[0]) * $percent / 100;
                $down_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[1]) * $percent / 100;
                $val_exp_serv4 = explode("/", $value_exp_serv[3]);
                $up_t4_exp_serv_sp = round($val_exp_serv4[0] * $percent / 100);
                $down_t4_exp_serv_sp = round($val_exp_serv4[1] * $percent / 100);
                $val_burst_exp_serv_sp = round($up_exp_serv_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_serv_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_serv_sp . "/" . $down_t4_exp_serv_sp . " " . $value_exp_serv[4];
            }
            $arr_times = unserialize($fet_online["bandwidth_time"]);
            $arr_times_exp = unserialize($fet_online["band_exp"]);
            $arr_times_d_exp = unserialize($fet_online["band_daily_exp"]);
            $arr_times_percent = unserialize($fet_online["bandwidth_percent"]);
            $arr_times_exp_percent = unserialize($fet_online["band_exp_percent"]);
            $arr_times_d_exp_percent = unserialize($fet_online["band_daily_exp_percent"]);
            $all_daily_uasge = $fet_online["down_today"] + $fet_online["up_today"];
            $all_quota_uasge = $fet_online["all_up"] + $fet_online["all_down"];
            if ($fet_online["daily_expire_service"] != 0 && 0 < $fet_online["up_daily_qouta"] && $fet_online["up_daily_qouta"] < $fet_online["up_today"] && $fet_online["daily_profile_qouta"] == 0) {
                if ($fet_online["qouta_expire_service"] != 0 && 0 < $fet_online["up_qouta"] && $fet_online["up_qouta"] < $fet_online["all_up"] && $fet_online["av_qouta"] == 0) {
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_exp[$s]["up_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/";
                        $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if (is_array($arr_times_d_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_d_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_d_exp[$s]["up_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) . "K/";
                        $sp = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                    }
                    $nn = 0;
                    if (is_array($arr_times_d_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_d_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                }
            } else {
                if ($fet_online["qouta_expire_service"] != 0 && 0 < $fet_online["up_qouta"] && $fet_online["up_qouta"] < $fet_online["all_up"] && $fet_online["av_qouta"] == 0) {
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_exp[$s]["up_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/";
                        $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if ($fet_online["av_qouta"] == 0 && 0 < $fet_online["up_qouta"]) {
                        if (is_array($arr_times)) {
                            $clock = strtotime(date("Y-m-d H:i:s"));
                            $s = 0;
                            for ($nn = 0;$s < count($arr_times);$s++) {
                                $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                    $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                }
                                $to_time = strtotime($to_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $up_rate = $arr_times[$s]["up_rate"];
                                if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                    $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                    $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                    $nn++;
                                    break;
                                }
                            }
                        }
                        if ($nn == 0) {
                            $speed = round($up_sp / $div_up_speed / $sp_up_count_online) . "K/";
                            $sp = round($up_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                            echo "hgfhgfh";
                        }
                        $nn = 0;
                        if (is_array($arr_times_percent)) {
                            $clock = strtotime(date("Y-m-d H:i:s"));
                            for ($s = 0;$s < count($arr_times_percent);$s++) {
                                $frombq_time = strtotime(date("Y-m-d ") . $arr_times_percent[$s]["frombq_time"] . ":00");
                                $tobq_time = date("Y-m-d ") . $arr_times_percent[$s]["tobq_time"] . ":00";
                                if (is_new($arr_times_percent[$s]["frombq_time"], $arr_times_percent[$s]["tobq_time"]) == 1) {
                                    $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                }
                                $tobq_time = strtotime($tobq_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $percent_bq = $arr_times_percent[$s]["percent"];
                                $sel_day = $arr_times_percent[$s]["sel_day"];
                                if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                    echo $sel_day;
                                    $today_work_bq = arraySearch($sel_day, date("D"));
                                    if (!empty($today_work_bq)) {
                                        $val_percent = $percent_bq;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                        }
                        if (0 < $nn) {
                            if ($val_percent != $fet_online["percent_q"]) {
                                $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                if (0 < mysqli_affected_rows($connect)) {
                                    $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                }
                            }
                        } else {
                            if ($val_percent != 100) {
                                $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                if (0 < mysqli_affected_rows($connect)) {
                                    $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                }
                            }
                        }
                    }
                }
            }
            if ($fet_online["daily_expire_service"] != 0 && 0 < $fet_online["down_daily_qouta"] && $fet_online["down_daily_qouta"] < $fet_online["down_today"] && $fet_online["daily_profile_qouta"] == 0) {
                if ($fet_online["qouta_expire_service"] != 0 && 0 < $fet_online["down_qouta"] && $fet_online["down_qouta"] < $fet_online["all_down"] && $fet_online["av_qouta"] == 0) {
                    $s = 0;
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $down_rate = $arr_times_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed.= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp.= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed.= round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K";
                        $sp.= round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if (is_array($arr_times_d_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_d_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $down_rate = $arr_times_d_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed.= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp.= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed.= round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) . "K";
                        $sp.= round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_d_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_d_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                }
            } else {
                if ($fet_online["qouta_expire_service"] != 0 && 0 < $fet_online["down_qouta"] && $fet_online["down_qouta"] < $fet_online["all_down"] && $fet_online["av_qouta"] == 0) {
                    $s = 0;
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $down_rate = $arr_times_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed.= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp.= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        if (!isset($down_exp_sp)) {
                            $down_exp_sp = 0;
                        }
                        $speed.= round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K";
                        $sp.= round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if ($fet_online["av_qouta"] == 0 && 0 < $fet_online["down_qouta"]) {
                        if (is_array($arr_times)) {
                            $clock = strtotime(date("Y-m-d H:i:s"));
                            $s = 0;
                            for ($nn = 0;$s < count($arr_times);$s++) {
                                $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                    $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                }
                                $to_time = strtotime($to_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $down_rate = $arr_times[$s]["down_rate"];
                                if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                    $speed.= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                    $sp.= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                        if ($nn == 0) {
                            $speed.= round($down_sp / $div_down_speed / $sp_down_count_online) . "K";
                            $sp.= round($down_sp / $div_down_speed / $sp_down_count_online) * 1000;
                        }
                        $nn = 0;
                        if (is_array($arr_times_percent)) {
                            $clock = strtotime(date("Y-m-d H:i:s"));
                            for ($s = 0;$s < count($arr_times_percent);$s++) {
                                $frombq_time = strtotime(date("Y-m-d ") . $arr_times_percent[$s]["frombq_time"] . ":00");
                                $tobq_time = date("Y-m-d ") . $arr_times_percent[$s]["tobq_time"] . ":00";
                                if (is_new($arr_times_percent[$s]["frombq_time"], $arr_times_percent[$s]["tobq_time"]) == 1) {
                                    $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                }
                                $tobq_time = strtotime($tobq_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $percent_bq = $arr_times_percent[$s]["percent"];
                                $sel_day = $arr_times_percent[$s]["sel_day"];
                                if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                    echo $sel_day;
                                    $today_work_bq = arraySearch($sel_day, date("D"));
                                    if (!empty($today_work_bq)) {
                                        $val_percent = $percent_bq;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                        }
                        if (0 < $nn) {
                            if ($val_percent != $fet_online["percent_q"]) {
                                $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                if (0 < mysqli_affected_rows($connect)) {
                                    $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                }
                            }
                        } else {
                            if ($val_percent != 100) {
                                $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                                if (0 < mysqli_affected_rows($connect)) {
                                    $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                }
                            }
                        }
                    }
                }
            }
            if (($fet_online["qouta_expire_service"] == "-1" || $exp_quota == 1) && $fet_online["av_qouta"] != 0 && $fet_online["av_qouta"] < $all_quota_uasge) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4460;
                $res = exec($dis);
                $arr_end_cards[] = $fet_online["username"];
                mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s") . "','1','" . $fet_online["id_card2"] . "')") or exit(mysqli_error($connect));
                continue;
            }
            if ($fet_online["daily_expire_service"] != 0 && 0 < $fet_online["daily_profile_qouta"] && $fet_online["daily_profile_qouta"] < $all_daily_uasge) {
                if ($fet_online["qouta_expire_service"] != 0 && $fet_online["av_qouta"] != 0 && $fet_online["av_qouta"] < $all_quota_uasge) {
                    if (!empty($val_address_exp_list) && $val_address_exp_list != $address_list_name) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_list . "',`address_val` = '1' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4475;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 4479;
                            $res = exec($send);
                        }
                    }
                    if ($fet_online["framed_pool"] != $fet_online["q_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["q_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4486;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_exp[$s]["up_rate"];
                            $down_rate = $arr_times_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                        $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if (!empty($val_address_exp_daily_list) && $val_address_exp_daily_list != $address_list_name) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_daily_list . "',`address_val`='2' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4689;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_daily_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 4693;
                            $res = exec($send);
                        }
                    }
                    if ($fet_online["framed_pool"] != $fet_online["d_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["d_pool"] . "',`address_val`='2' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4712;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (is_array($arr_times_d_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_d_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_d_exp[$s]["up_rate"];
                            $down_rate = $arr_times_d_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_da_sp;
                        $sp = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_d_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_d_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                }
            } else {
                if ($fet_online["qouta_expire_service"] != 0 && $fet_online["av_qouta"] != 0 && $fet_online["av_qouta"] < $all_quota_uasge) {
                    if (!empty($val_address_exp_list) && $val_address_exp_list != $address_list_name) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_list . "',`address_val`='1' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4912;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 4916;
                            $res = exec($send);
                        }
                    }
                    if ($fet_online["framed_pool"] != $fet_online["q_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["q_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4923;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0;$s < count($arr_times_exp);$s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_exp[$s]["up_rate"];
                            $down_rate = $arr_times_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                        $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_exp_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if ($fet_online["address_val"] != 3 || $address_list_name != $val_address_list) {
                        $update_nothing_address_list = mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_list . "',`address_val`='3' WHERE `username`='" . $fet_online["username"] . "' AND `attribute` = 'Cleartext-Password'") or exit(mysqli_error($connect));
                        if ($address_list_name != $val_address_list) {
                            if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 5128;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            } else {
                                $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 5132;
                                $res = exec($send);
                            }
                        }
                    }
                    if ($fet_online["framed_pool"] != $fet_online["ad_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["ad_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 5140;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (!$speed) {
                        $s = 0;
                        $nn = 0;
                        if (is_array($arr_times)) {
                            for ($clock = strtotime(date("Y-m-d H:i:s"));$s < count($arr_times);$s++) {
                                $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                    $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                }
                                $to_time = strtotime($to_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $up_rate = $arr_times[$s]["up_rate"];
                                $down_rate = $arr_times[$s]["down_rate"];
                                if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                    $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                    $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                        if ($nn == 0) {
                            $speed = round($up_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_now;
                            $sp = round($up_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_sp / $div_down_speed / $sp_down_count_online) * 1000;
                        }
                    }
                    $nn = 0;
                    if (is_array($arr_times_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0;$s < count($arr_times_percent);$s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_percent[$s]["frombq_time"], $arr_times_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_percent[$s]["percent"];
                            $sel_day = $arr_times_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 acctstoptime=CURRENT_TIMESTAMP
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstoptime`,
`acctstarttime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
NULL,
CURRENT_TIMESTAMP,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                                mysqli_query($connect, "UPDATE radacct SET acctstoptime = NULL WHERE acctuniqueid = '" . $fet_online["acctuniqueid"] . "'");
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                            }
                        }
                    }
                }
            }
            if ($fet_online["framedprotocol"] == "PPP") {
                if ($fet_online["up_by"] == 1) {
                    if ($fet_online["l_speed"] != $speed && $fet_online["stop_u_speed"] == 0) {
                        $send = "echo  " . "\"" . "user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',mikrotik-rate-limit='" . $speed . "'" . "\"" . " |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 5351;
                        $res = exec($send);
                        echo $send;
                        mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                    }
                } else {
                    if ($fet_online["api_is_en"] == 1) {
                        if ($m != end(array_keys($arr_nas[$fet_online["nasname"]]["details"]))) {
                            $m = 0;
                        } else {
                            $m = end(array_keys($arr_nas[$fet_online["nasname"]]["details"])) + 1;
                        }
                        if ($fet_online["l_speed"] != $speed) {
                            if ($arr_nas[$fet_online["nasname"]]["nasname"] != $fet_online["nasname"]) {
                                $arr_nas[$fet_online["nasname"]]["nasname"] = $fet_online["nasname"];
                                $arr_nas[$fet_online["nasname"]]["api_username"] = $fet_online["api_username"];
                                $arr_nas[$fet_online["nasname"]]["api_password"] = $fet_online["api_password"];
                                $arr_nas[$fet_online["nasname"]]["val_mik"] = $fet_online["val_mik"];
                            }
                            $arr_nas[$fet_online["nasname"]]["api_port"] = $fet_online["api_port"];
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["username"] = $fet_online["username"];
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["speed"] = $speed;
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["sp"] = $sp;
                            mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                        }
                    }
                }
            } else {
                if ($fet_online["l_speed"] != $speed && $fet_online["stop_u_speed"] == 0) {
                    $send = "echo  " . "\"" . "user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',mikrotik-rate-limit='" . $speed . "'" . "\"" . " |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 5380;
                    $res = exec($send);
                    mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                }
            }
        }
        $sess_day = date("d-m-Y", strtotime($fet_online["acctstarttime"]));
        $now = date("d-m-Y");
        $y = date("Y");
        if ($fet_online["daily_expire_service"] != 0 && $sess_day < $now) {
            $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`='" . date("Y-m-d 23:59:59", strtotime($fet_online["acctstarttime"])) . "',
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
            if (0 < mysqli_affected_rows($connect)) {
                $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
            }
        }
        $speed = "";
    }
    if ($dis != "") {
        $res = exec($dis);
    }
    if ($send != "") {
        $res = exec($send);
    }
    $n = 0;
    if (is_array($arr_nas)) {
        foreach ($arr_nas as $key => $value) {
            if ($API->connect($arr_nas[$key]["nasname"], $arr_nas[$key]["api_username"], $arr_nas[$key]["api_password"], $arr_nas[$key]["api_port"], $arr_nas[$key]["val_mik"])) {
                $m = 0;
                $arr_nas[$key]["details"] = array_values($arr_nas[$key]["details"]);
                while ($m < count($arr_nas[$key]["details"])) {
                    $speed = $arr_nas[$key]["details"][$m]["speed"];
                    $sp = $arr_nas[$key]["details"][$m]["sp"];
                    $queue = $API->comm("/queue/simple/print", array("?name" => "<pppoe-" . $arr_nas[$key]["details"][$m]["username"] . ">"));
                    if ($queue[0]["max-limit"]) {
                        $queue[0]["max-limit"] = str_ireplace("k", "000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("K", "000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("m", "000000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("M", "000000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("g", "000000000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("G", "000000000", $queue[0]["max-limit"]);
                        if ($queue[0]["max-limit"] != $sp) {
                            $edit_queue = $API->comm("/queue/simple/set", array("numbers" => $queue[0]["name"], "max-limit" => $speed, "limit-at" => $speed, "burst-limit" => "0/0", "burst-threshold" => "0/0", "burst-time" => "0s/0s"));
                            $queue2 = $API->comm("/queue/simple/print", array("?name" => "<pppoe-" . $arr_nas[$key]["details"][$m]["username"] . ">"));
                            if ($queue2[0]["max-limit"] != $sp) {
                                $queue2 = $API->comm("/queue/simple/remove", array("numbers" => $queue2[0]["name"]));
                                $add_new = $API->comm("/queue/simple/add", array("name" => "<pppoe-" . $arr_nas[$key]["details"][$m]["username"] . ">", "max-limit" => $speed, "limit-at" => $speed, "burst-limit" => "0/0", "burst-threshold" => "0/0", "burst-time" => "0s/0s"));
                            }
                        }
                    } else {
                        $add_new = $API->comm("/queue/simple/add", array("name" => "<pppoe-" . $arr_nas[$key]["details"][$m]["username"] . ">", "max-limit" => $speed, "limit-at" => $speed, "burst-limit" => "0/0", "burst-threshold" => "0/0", "burst-time" => "0s/0s"));
                    }
                    $speed = "";
                    $m++;
                }
                $API->disconnect();
            }
        }
    }
    if (0 < count($arr_dis)) {
        mysqli_query($connect, "UPDATE `radacct` SET AcctStopTime='" . date("Y-m-d H:i:s") . "', 
    AcctSessionTime=unix_timestamp('" . date("Y-m-d H:i:s") . "') - unix_timestamp(AcctStartTime), 
    AcctTerminateCause='',
    AcctStopDelay = '0'
	WHERE  (AcctStopTime IS NULL) AND `radacctid` IN ('" . join("','", $arr_dis) . "')");
    }
    if (is_array($arr_end_cards) && 0 < count($arr_end_cards)) {
        mysqli_query($connect, "UPDATE `radacct` SET AcctStopTime='" . date("Y-m-d H:i:s") . "', 
    AcctSessionTime=unix_timestamp('" . date("Y-m-d H:i:s") . "') - unix_timestamp(AcctStartTime), 
    AcctTerminateCause='',
    AcctStopDelay = '0'
	WHERE  (AcctStopTime IS NULL) AND `username` IN ('" . join("','", $arr_end_cards) . "')");
    }
}
$timeout_adv = time() - $fet_set["timeout_adv"];
mysqli_query($connect, "UPDATE `radacct` 
SET AcctStopTime='" . date("Y-m-d H:i:s") . "', 
    AcctSessionTime=unix_timestamp('" . date("Y-m-d H:i:s") . "') - unix_timestamp(AcctStartTime), 
    AcctTerminateCause='',
    AcctStopDelay = '0'
	WHERE  AcctStopTime IS NULL  AND t_session < " . $timeout_adv);
$arr_u = array();
if ($fet_set["enable_sms"] == 1 && $fet_set["exp_msg"] && $fet_set["exp_rem"] != 0) {
    $url = $fet_set["api_url"];
    $day = $fet_set["exp_rem"];
    $msg = $fet_set["exp_msg"];
    $sender = $fet_set["com_name"];
    $item_updates = "";
    $rem_exp = time() + $fet_set["exp_rem"] * 60 * 60 * 24;
    if ($url && $msg) {
        $sel_all_users = mysqli_query($connect, "SELECT 
	`userinfo`.`username`,
	`userinfo`.`mobile`,
	`userinfo`.`id`,
	`userinfo`.`updatedate`,
	`userinfo`.`av_qouta` *1024*1024 as `av_qouta`,
	`userinfo`.`firstname` as `first_name`,
	`userinfo`.`lastname` as `last_name`,
	`radusergroup`.`groupname` as `prof`,
	`radpass`.`value` as `pass`,
	DATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y') as `last_send_sms`,
	DATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y %H:%i:%s') as `exp`
	FROM `userinfo`
	LEFT JOIN `radcheck` ON (`radcheck`.`username` = `userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration')
	LEFT JOIN `radusergroup` ON (`radusergroup`.`username` = `userinfo`.`username`)
	LEFT JOIN `radcheck` as `radpass` ON (`radpass`.`username` = `userinfo`.`username` AND `radpass`.`attribute` ='Cleartext-Password' )
	WHERE UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'))<= " . $rem_exp . " AND UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i')) > UNIX_TIMESTAMP()
	AND `userinfo`.`last_send_sms` != DATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y') AND `userinfo`.`renew` ='0' GROUP BY `userinfo`.`id`
	");
        if (0 < mysqli_num_rows($sel_all_users)) {
            while ($fet_all_users = mysqli_fetch_assoc($sel_all_users)) {
                if (in_array($fet_all_users["username"], $arr_u)) {
                    continue;
                }
                $arr_u[] = $fet_all_users["username"];
                $sel_q = mysqli_query($connect, "SELECT 
COALESCE(up_uq,0) as up_uq,
COALESCE(down_uq,0) as down_uq,
COALESCE(acctinputoctets * (percent_q/100),0) as acctinputoctets,
COALESCE(acctoutputoctets * (percent_q/100),0)  as acctoutputoctets
FROM radacct WHERE `username`='" . $fet_all_users["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(`radacct`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >= '" . $fet_all_users["updatedate"] . "' order by radacctid  DESC LIMIT 1") or exit(mysqli_error($connect));
                $fet_q = mysqli_fetch_array($sel_q);
                $up = $fet_q["up_uq"] + $fet_q["acctinputoctets"];
                $down = $fet_q["down_uq"] + $fet_q["acctoutputoctets"];
                $av_qouta = $fet_all_users["av_qouta"];
                $all_qouta = $down + $up;
                $all_qouta_string = "";
                if (!$all_qouta) {
                    $all_qouta = 0;
                }
                $all_qouta_string = formatSizeUnits($all_qouta);
                $download = formatSizeUnits($down);
                $upload = formatSizeUnits($up);
                $rem_quota = $av_qouta - $all_qouta;
                if ($rem_quota <= 0) {
                    $rem_quota = 0;
                }
                $rem_quota = formatSizeUnits($rem_quota);
                if ($av_qouta == 0) {
                    $rem_quota = $LANG["UNLIMITED"];
                }
                $url = $fet_set["api_url"];
                $first_name = $fet_all_users["first_name"];
                $last_name = $fet_all_users["last_name"];
                $user = $fet_all_users["username"];
                $pass = $fet_all_users["pass"];
                $exp = $fet_all_users["exp"];
                $prof = $fet_all_users["prof"];
                $phone = $fet_all_users["mobile"];
                $day = date("d", strtotime($exp));
                $update_user = mysqli_query($connect, "UPDATE `userinfo` SET `last_send_sms` = '" . $fet_all_users["last_send_sms"] . "' WHERE `id`='" . $fet_all_users["id"] . "'");
                sendSMS($url, $user, $pass, $exp, $prof, $day, $msg, $sender, $phone, $rem_quota, $first_name, $last_name);
            }
        }
    }
}
$check_before_renew = mysqli_query($connect, "SELECT 

* FROM `userinfo` WHERE `userinfo`.`renew`='1'");
if (0 < mysqli_num_rows($check_before_renew)) {
    $fet_be_renew = mysqli_fetch_assoc($check_before_renew);
    $sel_users_auto_renew = mysqli_query($connect, "SELECT 

`userinfo`.*,
`userinfo`.`id` as `id_user`,
UNIX_TIMESTAMP(STR_TO_DATE(radcheck.value, '%d %b %Y %H:%i')) as `exp`,
`managers`.`id` as `id_man`,
`managers`.*,
`p`.*,
`r8`.`value` as `val_address_list`,
`p`.`id` as `id_prof`
FROM `userinfo` 
INNER JOIN `radcheck` ON (`radcheck`.`username`=`userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration' AND UNIX_TIMESTAMP(STR_TO_DATE(radcheck.value, '%d %b %Y %H:%i')) < " . time() . ") 
INNER JOIN `managers` ON (`managers`.`id`=`userinfo`.`owner_id`) 
LEFT JOIN `radusergroup` ON(`radusergroup`.`username`=`userinfo`.`username`)
INNER JOIN `profiles` `p` ON (`p`.`profile_name`=`radusergroup`.`groupname` AND `p`.`type_prof` ='0')
LEFT JOIN `radgroupreply` `r8` ON (`r8`.`groupname` = `p`.`profile_name` AND `r8`.`attribute` = 'Mikrotik-Address-List')
WHERE `userinfo`.`renew`='1' AND UNIX_TIMESTAMP(STR_TO_DATE(radcheck.value, '%d %b %Y %H:%i')) < UNIX_TIMESTAMP() GROUP BY `userinfo`.`id`") or exit(mysqli_error($connect));
    if (0 < mysqli_num_rows($sel_users_auto_renew)) {
        while ($fet_renew = mysqli_fetch_assoc($sel_users_auto_renew)) {
            $get_user = $fet_renew["username"];
            $check_paid = 1;
            $get_time_exp = $fet_renew["exp"];
            $comment_active = "";
            if ($get_time_exp < time() || $fet_renew["remaining_time"] == 1) {
                $get_time_exp = time();
            }
            $r_free_q = 0;
            if ($get_time_exp < time() || $fet_renew["remaining_qouta"] == 1) {
                $update_date = time();
                $is_update = 1;
                $r_free_q = 1;
                $is_new_time = 1;
            } else {
                $update_date = $fet_renew["updatedate"];
                $is_update = 0;
                $is_new_time = 0;
            }
            $sel_price_admin_for_this_profile = mysqli_query($connect, "SELECT `new_price` FROM `prices_profiles_admin` WHERE `id_profile` ='" . $fet_renew["id_prof"] . "' AND `id_manager` = '" . $fet_renew["id_man"] . "'") or exit(mysqli_error($connect));
            if (mysqli_num_rows($sel_price_admin_for_this_profile) == 0) {
                $get_price = $fet_renew["price"];
                $user_price = $fet_renew["price"];
            } else {
                $fet_price = mysqli_fetch_assoc($sel_price_admin_for_this_profile);
                $get_price = $fet_price["new_price"];
                if ($fet_set["disc_for_users"] == 1 && $fet_price["new_price"]) {
                    $user_price = $fet_price["new_price"];
                } else {
                    $user_price = $fet_renew["price"];
                }
            }
            if (0 < $fet_renew["exp_unit"]) {
                $time_now = time();
                if ($fet_renew["exp_unit_val"] == 3) {
                    $t_date = date("Y-m-d H:i", strtotime("+" . $fet_renew["exp_unit"] . " months", $get_time_exp)) . "
";
                } else {
                    if ($fet_renew["exp_unit_val"] == 2) {
                        $t_date = date("Y-m-d H:i", strtotime("+" . $fet_renew["exp_unit"] . " days", $get_time_exp)) . "
";
                    } else {
                        if ($fet_renew["exp_unit_val"] == 1) {
                            $t_date = date("Y-m-d H:i", strtotime("+" . $fet_renew["exp_unit"] . " hours", $get_time_exp)) . "
";
                        } else {
                            if ($fet_renew["exp_unit_val"] == 0) {
                                $t_date = date("Y-m-d H:i", strtotime("+" . $fet_renew["exp_unit"] . " minutes", $get_time_exp)) . "
";
                            }
                        }
                    }
                }
                $time_date = strtotime($t_date);
            } else {
                $t_date = date("Y-m-d H:i", strtotime("+1 months", $get_time_exp)) . "
";
                $time_date = strtotime($t_date);
            }
            $amount = $get_price;
            $u_amount = $user_price;
            $amount = round($amount, 2);
            $u_amount = round($u_amount, 2);
            $set_date_exp = date("d M Y H:i:s", $time_date);
            if ($fet_renew["renew_u"] == 0) {
                if ($fet_renew["is_admin"] == 0) {
                    if ($fet_renew["credit"] < $amount) {
                        continue;
                    }
                    $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `credit` = `credit` - " . $amount . ",`usage` =`usage` + " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                } else {
                    $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `usage` =`usage` + " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                }
            } else {
                if ($fet_renew["money"] < $u_amount) {
                    continue;
                }
                $update_user_money = mysqli_query($connect, "UPDATE `userinfo` SET `money` =`money` - '" . $u_amount . "' WHERE `username` ='" . $get_user . "' ");
            }
            $v_recharge = 0;
            $sel_qouta = mysqli_query($connect, "SELECT 

SUM(`acctinputoctets` * (percent_q/100))/1024/1024 as `usage_upload`,
SUM(`acctoutputoctets` * (percent_q/100))/1024/1024 as `usage_download` FROM `radacct`
WHERE UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $fet_renew["updatedate"] . "'  AND  username='" . $get_user . "'") or exit(mysqli_error($connect));
            $fet_qouta = mysqli_fetch_array($sel_qouta);
            $all_usage = $fet_qouta["usage_download"] + $fet_qouta["usage_upload"];
            $up_qouta = $fet_renew["profile_up_qouta"];
            $down_qouta = $fet_renew["profile_down_qouta"];
            $all_qouta = $fet_renew["profile_qouta"];
            if ($fet_renew["remaining_qouta"] == 1) {
                $usage_quota = $all_qouta;
                $usage_up = $up_qouta;
                $usage_down = $down_qouta;
                $from_q = "0 B";
                $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
            } else {
                if (0 < $up_qouta) {
                    if ($fet_renew["reset_counters"] == 0) {
                        if ($fet_renew["in_up"] == 0) {
                            $usage_up = $up_qouta;
                        } else {
                            if ($fet_renew["in_up"] <= $fet_qouta["usage_upload"]) {
                                $usage_up = $up_qouta;
                            } else {
                                $usage_up = $fet_renew["in_up"] - $fet_qouta["usage_upload"] + $up_qouta;
                            }
                        }
                    } else {
                        if ($fet_renew["in_up"] == 0) {
                            $usage_up = $fet_qouta["usage_upload"] + $up_qouta;
                        } else {
                            if ($fet_renew["in_up"] <= $fet_qouta["usage_upload"]) {
                                $usage_up = $fet_qouta["usage_upload"] + $up_qouta;
                            } else {
                                $usage_up = $fet_renew["in_up"] + $up_qouta;
                            }
                        }
                    }
                } else {
                    $usage_up = 0;
                }
                if (0 < $down_qouta) {
                    if ($fet_renew["reset_counters"] == 0) {
                        if ($fet_renew["in_down"] == 0) {
                            $usage_down = $down_qouta;
                        } else {
                            if ($fet_renew["in_down"] <= $fet_qouta["usage_download"]) {
                                $usage_down = $down_qouta;
                            } else {
                                $usage_down = $fet_renew["in_down"] - $fet_qouta["usage_download"] + $down_qouta;
                            }
                        }
                    } else {
                        if ($fet_renew["in_down"] == 0) {
                            $usage_down = $fet_qouta["usage_download"] + $down_qouta;
                        } else {
                            if ($fet_renew["in_down"] <= $fet_qouta["usage_download"]) {
                                $usage_down = $fet_qouta["usage_download"] + $down_qouta;
                            } else {
                                $usage_down = $fet_renew["in_down"] + $down_qouta;
                            }
                        }
                    }
                } else {
                    $usage_down = 0;
                }
                if (0 < $all_qouta) {
                    if ($fet_renew["reset_counters"] == 0) {
                        if ($fet_renew["av_qouta"] == 0) {
                            $usage_quota = $all_qouta;
                        } else {
                            if ($fet_renew["av_qouta"] <= $all_usage) {
                                $usage_quota = $all_qouta;
                            } else {
                                $usage_quota = $fet_renew["av_qouta"] - $all_usage + $all_qouta;
                            }
                        }
                        $from_q = "0 B";
                        $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                    } else {
                        if ($fet_renew["av_qouta"] == 0) {
                            $usage_quota = $all_usage + $all_qouta;
                            $from_q = "0 B";
                            $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                        } else {
                            if ($fet_renew["av_qouta"] <= $all_usage) {
                                $usage_quota = $all_usage + $all_qouta;
                                $from_q = "0 B";
                                $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                            } else {
                                $usage_quota = $fet_renew["av_qouta"] + $all_qouta;
                                $from_q = $fet_renew["av_qouta"];
                                $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                            }
                        }
                    }
                } else {
                    $usage_quota = 0;
                    $from_q = $fet_renew["av_qouta"];
                    $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                }
            }
            if (0 < $fet_renew["profile_qouta"] || $fet_renew["profile_qouta"] == 0 && $fet_renew["profile_down_qouta"] == 0 && $fet_renew["profile_up_qouta"] == 0) {
                $usage_down = 0;
                $usage_up = 0;
                $fet_renew["profile_down_qouta"] = 0;
                $fet_renew["profile_up_qouta"] = 0;
            }
            if ($fet_renew["is_recharge"] == 1) {
                if ($fet_renew["q_recharge"] <= $usage_quota || $usage_quota == 0) {
                    if ($usage_quota != 0) {
                        $usage_quota = $usage_quota - $fet_renew["q_recharge"];
                        if ($usage_quota == 0) {
                            $usage_quota = 1;
                        }
                    }
                    $t_recharge = $fet_renew["t_recharge"] * 60 * 60;
                    $val_time = $time_date - time();
                    if ($t_recharge <= $val_time) {
                        $update_is_rechange = mysqli_query($connect, "UPDATE `userinfo` SET `is_recharge` = '0',q_recharge='0',t_recharge='0',`p_recharge`='0' WHERE `username` ='" . $get_user . "'");
                        $time_date = $time_date - $t_recharge;
                        $set_date_exp = date("d M Y H:i:s", $time_date);
                        $v_recharge = 1;
                    } else {
                        if ($fet_renew["renew_u"] == 0) {
                            if ($fet_renew["is_admin"] == 0) {
                                $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `credit` = `credit` + " . $amount . ",`usage` =`usage` - " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                            } else {
                                $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `usage` =`usage` - " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                            }
                        } else {
                            $update_user_money = mysqli_query($connect, "UPDATE `userinfo` SET `money` =`money` + '" . $u_amount . "' WHERE `username` ='" . $get_user . "' ");
                        }
                        continue;
                    }
                } else {
                    if ($fet_renew["renew_u"] == 0) {
                        if ($fet_renew["is_admin"] == 0) {
                            $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `credit` = `credit` + " . $amount . ",`usage` =`usage` - " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                        } else {
                            $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `usage` =`usage` - " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                        }
                    } else {
                        $update_user_money = mysqli_query($connect, "UPDATE `userinfo` SET `money` =`money` + '" . $u_amount . "' WHERE `username` ='" . $get_user . "' ");
                    }
                    continue;
                }
            }
            $val_address_list = $fet_renew["val_address_list"];
            if ($fet_renew["renew_u"] == 0) {
                $insert_to_report = mysqli_query($connect, "INSERT INTO `a_s_man_users` 
(`id_user`,`id_manager`,`id_profile`,`operation`
,`date_operation`,`new_price`,`status`,`id_prof_old`,`paid_unpaid`,`old_price`,
`price_invoice`,`notes`) 
VALUES ('" . $fet_renew["id_user"] . "','" . $fet_renew["id_man"] . "','" . $fet_renew["id_prof"] . "','13'
,'" . time() . "','" . $u_amount . "','1','','" . $check_paid . "','0','" . $u_amount . "','" . $comment_active . "')") or exit(mysqli_error($connect));
            } else {
                $action = 8;
                $l_text = "";
                $insert_user_rep = mysqli_query($connect, "INSERT INTO 
`users_actions` (`action`,`last_text`,`new_text`,`col_name`,`id_user`,`date_action`,`ip`,`browser`,`os`) 
VALUES ('" . $action . "','" . $l_text . "','" . $u_amount . "','10','" . $fet_renew["id_user"] . "','" . time() . "','---','---','---')") or exit(mysqli_error($connect));
            }
            if ($fet_renew["renew_u"] == 1) {
                $amount = $u_amount;
            }
            if ($fet_renew["remaining_qouta"] == 0) {
                $usage_quota = $fet_renew["profile_qouta"];
                if ($r_free_q == 1) {
                    $update_f_q = mysqli_query($connect, "UPDATE `radusergroup` SET `free_q` = '0' WHERE `username` ='" . $get_user . "'");
                }
                $update_user_usage = mysqli_query($connect, "UPDATE `userinfo` SET 
`users_usage` = `users_usage`+" . $amount . ",
`in_down`='" . $usage_down . "',
`in_up`='" . $usage_up . "',
`av_qouta`='" . $usage_quota . "',
`updatedate` = '" . $update_date . "'
 WHERE username='" . $get_user . "'");
            } else {
                $update_date = time();
                $update_f_q = mysqli_query($connect, "UPDATE `radusergroup` SET `free_q` = '0' WHERE `username` ='" . $get_user . "'");
                $update_user_usage = mysqli_query($connect, "UPDATE `userinfo` SET 
`users_usage` = `users_usage`+" . $amount . ",
`in_down`='" . $fet_renew["profile_down_qouta"] . "',
`in_up`='" . $fet_renew["profile_up_qouta"] . "',
`av_qouta`='" . $usage_quota . "',
`updatedate` = '" . $update_date . "'
 WHERE username='" . $get_user . "'");
            }
            $sel_online = mysqli_query($connect, "SELECT * FROM `radacct` WHERE `username` ='" . $get_user . "' AND `acctstoptime` IS NULL ");
            if ($fet_renew["reset_counters"] == 0) {
                $delete_usage_qouta = mysqli_query($connect, "DELETE FROM `radacct` WHERE `username`='" . $get_user . "'");
            }
            if (0 < mysqli_num_rows($sel_online)) {
                while ($fet_online = mysqli_fetch_assoc($sel_online)) {
                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                    if (0 < mysqli_affected_rows($connect) || $fet_renew["reset_counters"] == 0) {
                        $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $update_date . "' LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $update_date . "' LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >='" . $update_date . "'  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                    }
                }
            } else {
                if ($is_new_time == 1 || $fet_renew["remaining_qouta"] == 1) {
                    $update_radacct = mysqli_query($connect, "UPDATE `radacct` SET `down_uq`=0,up_uq=0,all_tu=0 WHERE `username` ='" . $get_user . "' ORDER BY `radacctid` DESC LIMIT 1 ");
                }
            }
            $update_user = mysqli_query($connect, "UPDATE `radcheck` SET `value` = '" . $set_date_exp . "' WHERE username='" . $get_user . "' AND attribute='Expiration'");
            mysqli_query($connect, "INSERT INTO `man_actions` 
(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`id_user`,`ip`,`get_os`,`browser`) VALUES 
('" . $fet_renew["id_man"] . "','5','1','" . $get_user . "','" . time() . "','" . $fet_renew["id_user"] . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
            if ($fet_set["enable_sms"] == 1) {
                $url = $fet_set["api_url"];
                $msg = $fet_set["act_msg"];
                if ($url && $msg && $fet_set["renew_user_sms"] == 1) {
                    $sel_all_users = mysqli_query($connect, "SELECT 
	
	`userinfo`.`username`,
	`userinfo`.`mobile`,
	`userinfo`.`id`,
	`radusergroup`.`groupname` as `prof`,
	`radpass`.`value` as `pass`,
	DATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y') as `last_send_sms`,
	DATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y %H:%i:%s') as `exp`
	FROM `userinfo`
	LEFT JOIN `radcheck` ON (`radcheck`.`username` = `userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration')
	LEFT JOIN `radusergroup` ON (`radusergroup`.`username` = `userinfo`.`username`)
	LEFT JOIN `radcheck` as `radpass` ON (`radpass`.`username` = `userinfo`.`username` AND `radpass`.`attribute` ='Cleartext-Password' )
	WHERE `userinfo`.`username`='" . $get_user . "' GROUP BY `userinfo`.`username`");
                    if (0 < mysqli_num_rows($sel_all_users)) {
                        $fet_all_users = mysqli_fetch_array($sel_all_users);
                        $user = $fet_all_users["username"];
                        $pass = $fet_all_users["pass"];
                        $exp = $fet_all_users["exp"];
                        $prof = $fet_all_users["prof"];
                        $phone = $fet_all_users["mobile"];
                        $msg = str_ireplace("{price}", $amount, $msg);
                        sendSMS($url, $user, $pass, $exp, $prof, $day, $msg, $sender, $phone);
                    }
                }
            }
            if ($v_recharge == 1) {
                $val_for_event = $get_user . " (" . $fet_renew["p_recharge"] . " ";
                mysqli_query($connect, "INSERT INTO `man_actions` 
(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`id_user`,`ip`,`get_os`,`browser`) VALUES 
('" . $fet_renew["id_man"] . "','64','1','" . $val_for_event . "','" . time() . "','" . $fet_renew["id_user"] . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
                if (0 < $fet_renew["p_recharge"]) {
                    mysqli_query($connect, "UPDATE `managers` SET `credit`=`credit`+'" . $fet_renew["p_recharge"] . "' WHERE id='" . $fet_renew["ad_recharge"] . "' AND is_admin='0'");
                    mysqli_query($connect, "UPDATE `managers` SET `usage`=`usage`-'" . $fet_renew["p_recharge"] . "' WHERE id='" . $fet_renew["ad_recharge"] . "'");
                }
            }
        }
    }
}
$sel_ren_user = mysqli_query($connect, "SELECT 

`userinfo`.*,
UNIX_TIMESTAMP(STR_TO_DATE(radcheck.value, '%d %b %Y %H:%i')) as `exp`,
`userinfo`.`id` as `id_user`,
`managers`.*,
`p`.*,
`p`.id as `id_prof`,
`r8`.`value` as `val_address_list`,
`managers`.`id` as `id_man`
FROM `userinfo`
INNER JOIN `managers` ON (`managers`.`id` = `userinfo`.`owner_id`)
INNER JOIN `radcheck` ON (`radcheck`.`username`=`userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration') 
INNER JOIN `radusergroup` ON (`radusergroup`.`username` = `userinfo`.`username`)
INNER JOIN `profiles` `p` ON (`p`.`profile_name` = `radusergroup`.`groupname` AND `p`.`type_prof` ='0')
LEFT JOIN `radgroupreply` `r8` ON (`r8`.`groupname` = `p`.`profile_name` AND `r8`.`attribute` = 'Mikrotik-Address-List')
WHERE `is_list` = '1'");
if (0 < mysqli_num_rows($sel_ren_user)) {
    while ($fet_ren_user = mysqli_fetch_assoc($sel_ren_user)) {
        $val_exp = unserialize($fet_ren_user["act_values"]);
        $val_prof = unserialize($fet_ren_user["prof_ren"]);
        $exp = $fet_ren_user["exp"];
        $is_val = 0;
        $n = 0;
        if (is_array($val_exp) && 0 < count($val_exp) && !empty($val_exp)) {
            while ($n < count($val_exp)) {
                if (strtotime($val_exp[$n]) <= time()) {
                    $get_time_exp = strtotime($val_exp[$n]);
                    if (empty($fet_ren_user["profile_name"])) {
                        $n++;
                        continue;
                    }
                    unset($val_exp[$n]);
                    $exp_af_unset = array_values($val_exp);
                    $is_val = 1;
                    break;
                }
                $n++;
            }
            if ($is_val == 1) {
                $get_user = $fet_ren_user["username"];
                $check_paid = 1;
                $comment_active = "";
                $send_sms = 1;
                if (time() < $exp && $fet_ren_user["added_qouta"] == 1 && $fet_ren_user["type_prof"] == 0) {
                    continue;
                }
                $sel_price_admin_for_this_profile = mysqli_query($connect, "SELECT `new_price` FROM `prices_profiles_admin` WHERE `id_profile` ='" . $fet_ren_user["id_prof"] . "' AND `id_manager` = '" . $fet_ren_user["owner_id"] . "'") or exit(mysqli_error($connect));
                if (mysqli_num_rows($sel_price_admin_for_this_profile) == 0) {
                    $get_price = $fet_ren_user["price"];
                    $user_price = $fet_ren_user["price"];
                } else {
                    $fet_price = mysqli_fetch_assoc($sel_price_admin_for_this_profile);
                    $get_price = $fet_price["new_price"];
                    if ($fet_set["disc_for_users"] == 1 && $fet_price["new_price"]) {
                        $user_price = $fet_price["new_price"];
                    } else {
                        $user_price = $fet_ren_user["price"];
                    }
                }
                if ($get_time_exp < time() || $fet_ren_user["remaining_time"] == 1) {
                    $get_time_exp = time();
                }
                if ($get_time_exp < time() || $fet_ren_user["remaining_qouta"] == 1) {
                    $update_date = time();
                    $r_free_q = 1;
                    $is_new_time = 1;
                } else {
                    $update_date = $fet_ren_user["updatedate"];
                    $r_free_q = 0;
                    $is_new_time = 0;
                }
                if (0 < $fet_ren_user["exp_unit"]) {
                    $time_now = time();
                    if ($fet_ren_user["exp_unit_val"] == 3) {
                        $t_date = date("Y-m-d H:i", strtotime("+" . $fet_ren_user["exp_unit"] . " months", $get_time_exp)) . "
";
                    } else {
                        if ($fet_ren_user["exp_unit_val"] == 2) {
                            $t_date = date("Y-m-d H:i", strtotime("+" . $fet_ren_user["exp_unit"] . " days", $get_time_exp)) . "
";
                        } else {
                            if ($fet_ren_user["exp_unit_val"] == 1) {
                                $t_date = date("Y-m-d H:i", strtotime("+" . $fet_ren_user["exp_unit"] . " hours", $get_time_exp)) . "
";
                            } else {
                                if ($fet_ren_user["exp_unit_val"] == 0) {
                                    $t_date = date("Y-m-d H:i", strtotime("+" . $fet_ren_user["exp_unit"] . " minutes", $get_time_exp)) . "
";
                                }
                            }
                        }
                    }
                    $time_date = strtotime($t_date);
                } else {
                    $time_now = time();
                    $t_date = date("Y-m-d H:i", strtotime("+1 months", $get_time_exp)) . "
";
                    $time_date = strtotime($t_date);
                }
                $amount = $get_price;
                $u_amount = $user_price;
                $amount = round($amount, 2);
                $set_date_exp = date("d M Y H:i:s", $time_date);
                $v_recharge = 0;
                $sel_qouta = mysqli_query($connect, "SELECT 
SUM(`acctinputoctets` * (percent_q/100))/1024/1024 as `usage_upload`,
SUM(`acctoutputoctets` * (percent_q/100))/1024/1024 as `usage_download` FROM `radacct`
WHERE UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $fet_ren_user["updatedate"] . "'  AND  username='" . $get_user . "'") or exit(mysqli_error($connect));
                $fet_qouta = mysqli_fetch_array($sel_qouta);
                $all_usage = $fet_qouta["usage_download"] + $fet_qouta["usage_upload"];
                $up_qouta = $fet_ren_user["profile_up_qouta"];
                $down_qouta = $fet_ren_user["profile_down_qouta"];
                $all_qouta = $fet_ren_user["profile_qouta"];
                if ($fet_ren_user["remaining_qouta"] == 1) {
                    $usage_quota = $all_qouta;
                    $usage_up = $up_qouta;
                    $usage_down = $down_qouta;
                    $from_q = "0 B";
                    $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                } else {
                    if (0 < $up_qouta) {
                        if ($fet_ren_user["reset_counters"] == 0) {
                            if ($fet_ren_user["in_up"] == 0) {
                                $usage_up = $up_qouta;
                            } else {
                                if ($fet_ren_user["in_up"] <= $fet_qouta["usage_upload"]) {
                                    $usage_up = $up_qouta;
                                } else {
                                    $usage_up = $fet_ren_user["in_up"] - $fet_qouta["usage_upload"] + $up_qouta;
                                }
                            }
                        } else {
                            if ($fet_ren_user["in_up"] == 0) {
                                $usage_up = $fet_qouta["usage_upload"] + $up_qouta;
                            } else {
                                if ($fet_ren_user["in_up"] <= $fet_qouta["usage_upload"]) {
                                    $usage_up = $fet_qouta["usage_upload"] + $up_qouta;
                                } else {
                                    $usage_up = $fet_ren_user["in_up"] + $up_qouta;
                                }
                            }
                        }
                    } else {
                        $usage_up = 0;
                    }
                    if (0 < $down_qouta) {
                        if ($fet_ren_user["reset_counters"] == 0) {
                            if ($fet_ren_user["in_down"] == 0) {
                                $usage_down = $down_qouta;
                            } else {
                                if ($fet_ren_user["in_down"] <= $fet_qouta["usage_download"]) {
                                    $usage_down = $down_qouta;
                                } else {
                                    $usage_down = $fet_ren_user["in_down"] - $fet_qouta["usage_download"] + $down_qouta;
                                }
                            }
                        } else {
                            if ($fet_ren_user["in_down"] == 0) {
                                $usage_down = $fet_qouta["usage_download"] + $down_qouta;
                            } else {
                                if ($fet_ren_user["in_down"] <= $fet_qouta["usage_download"]) {
                                    $usage_down = $fet_qouta["usage_download"] + $down_qouta;
                                } else {
                                    $usage_down = $fet_ren_user["in_down"] + $down_qouta;
                                }
                            }
                        }
                    } else {
                        $usage_down = 0;
                    }
                    if (0 < $all_qouta) {
                        if ($fet_ren_user["reset_counters"] == 0) {
                            if ($fet_ren_user["av_qouta"] == 0) {
                                $usage_quota = $all_qouta;
                            } else {
                                if ($fet_ren_user["av_qouta"] <= $all_usage) {
                                    $usage_quota = $all_qouta;
                                } else {
                                    $usage_quota = $fet_ren_user["av_qouta"] - $all_usage + $all_qouta;
                                }
                            }
                            $from_q = "0 B";
                            $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                        } else {
                            if ($fet_ren_user["av_qouta"] == 0) {
                                $usage_quota = $all_usage + $all_qouta;
                                $from_q = "0 B";
                                $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                            } else {
                                if ($fet_ren_user["av_qouta"] <= $all_usage) {
                                    $usage_quota = $all_usage + $all_qouta;
                                    $from_q = "0 B";
                                    $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                                } else {
                                    $usage_quota = $fet_ren_user["av_qouta"] + $all_qouta;
                                    $from_q = $fet_ren_user["av_qouta"];
                                    $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                                }
                            }
                        }
                    } else {
                        $usage_quota = 0;
                        $from_q = $fet_ren_user["av_qouta"];
                        $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                    }
                }
                if (0 < $fet_ren_user["profile_qouta"] || $fet_ren_user["profile_qouta"] == 0 && $fet_ren_user["profile_down_qouta"] == 0 && $fet_ren_user["profile_up_qouta"] == 0) {
                    $usage_down = 0;
                    $usage_up = 0;
                    $fet_ren_user["profile_down_qouta"] = 0;
                    $fet_ren_user["profile_up_qouta"] = 0;
                }
                if ($fet_ren_user["is_recharge"] == 1) {
                    if ($fet_ren_user["q_recharge"] <= $usage_quota || $usage_quota == 0) {
                        if ($usage_quota != 0) {
                            $usage_quota = $usage_quota - $fet_ren_user["q_recharge"];
                            if ($usage_quota == 0) {
                                $usage_quota = 1;
                            }
                        }
                        $t_recharge = $fet_ren_user["t_recharge"] * 60 * 60;
                        $val_time = $time_date - time();
                        if ($t_recharge <= $val_time) {
                            $update_is_rechange = mysqli_query($connect, "UPDATE `userinfo` SET `is_recharge` = '0',q_recharge='0',t_recharge='0',`p_recharge`='0' WHERE `username` ='" . $get_user . "'");
                            $time_date = $time_date - $t_recharge;
                            $set_date_exp = date("d M Y H:i:s", $time_date);
                        } else {
                            exit;
                        }
                    } else {
                        exit;
                    }
                }
                if ($fet_ren_user["is_admin"] == 0) {
                    if ($fet_ren_user["credit"] < $amount) {
                        $n++;
                        exit;
                    }
                    $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `credit` = `credit` - " . $amount . ",`usage` =`usage` + " . $amount . "  WHERE id='" . $fet_ren_user["id_man"] . "'");
                } else {
                    $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `usage` =`usage` + " . $amount . "  WHERE id='" . $fet_ren_user["id_man"] . "'");
                }
                if (0 < $u_amount) {
                    $insert_to_report = mysqli_query($connect, "INSERT INTO `a_s_man_users` 
(`id_user`,`id_manager`,`id_profile`,`operation`
,`date_operation`,`new_price`,`status`,`id_prof_old`,`paid_unpaid`,`old_price`,
`price_invoice`,`notes`) 
VALUES ('" . $fet_ren_user["id_user"] . "','" . $fet_ren_user["id_man"] . "','" . $id_prof . "','4'
,'" . time() . "','" . $u_amount . "','1','','" . $check_paid . "','0','" . $u_amount . "','" . $comment_active . "')") or exit(mysqli_error($connect));
                }
                $val_address_list = $fet_ren_user["val_address_list"];
                $sel_online = mysqli_query($connect, "SELECT * FROM `radacct` WHERE `username` ='" . $get_user . "' AND `acctstoptime` IS NULL ");
                if ($fet_ren_user["remaining_qouta"] == 0) {
                    if ($r_free_q == 1) {
                        $update_f_q = mysqli_query($connect, "UPDATE `radusergroup` SET `free_q` = '0' WHERE `username` ='" . $get_user . "'");
                    }
                    $update_user_usage = mysqli_query($connect, "UPDATE `userinfo` SET 
`users_usage` = `users_usage`+" . $amount . ",
`in_down`='" . $usage_down . "',
`in_up`='" . $usage_up . "',
`av_qouta`='" . $usage_quota . "',
`updatedate` = '" . $update_date . "'
 WHERE username='" . $get_user . "'");
                } else {
                    if ($fet_ren_user["is_recharge"] == 1) {
                        if ($fet_ren_user["q_recharge"] <= $usage_quota || $usage_quota == 0) {
                            if ($usage_quota != 0) {
                                $usage_quota = $usage_quota - $fet_ren_user["q_recharge"];
                                if ($usage_quota == 0) {
                                    $usage_quota = 1;
                                }
                            }
                            $t_recharge = $fet_ren_user["t_recharge"] * 60 * 60;
                            $val_time = $time_date - time();
                            if ($t_recharge <= $val_time) {
                                $update_is_rechange = mysqli_query($connect, "UPDATE `userinfo` SET `is_recharge` = '0',q_recharge='0',t_recharge='0',`p_recharge`='0' WHERE `username` ='" . $get_user . "'");
                                $time_date = $time_date - $t_recharge;
                                $set_date_exp = date("d M Y H:i:s", $time_date);
                                $v_recharge = 1;
                            } else {
                                exit;
                            }
                        } else {
                            exit;
                        }
                    }
                    $update_date = time();
                    $update_f_q = mysqli_query($connect, "UPDATE `radusergroup` SET `free_q` = '0' WHERE `username` ='" . $get_user . "'");
                    $update_user_usage = mysqli_query($connect, "UPDATE `userinfo` SET 
`users_usage` = `users_usage`+" . $amount . ",
`in_down`='" . $fet_ren_user["profile_down_qouta"] . "',
`in_up`='" . $fet_ren_user["profile_up_qouta"] . "',
`av_qouta`='" . $usage_quota . "',
`updatedate` = '" . $update_date . "'
 WHERE username='" . $get_user . "'");
                }
                if ($fet_ren_user["reset_counters"] == 0) {
                    $delete_usage_qouta = mysqli_query($connect, "DELETE FROM `radacct` WHERE `username`='" . $get_user . "'");
                }
                if (0 < mysqli_num_rows($sel_online)) {
                    while ($fet_online = mysqli_fetch_assoc($sel_online)) {
                        $update_radacct = mysqli_query($connect, "UPDATE `radacct`
 SET 
 `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,
 `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,
 `acctstoptime`=CURRENT_TIMESTAMP,
 `re_time`= `acctsessiontime` + `re_time`,
 `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) 
WHERE `username` ='" . $get_user . "' AND `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';
") or exit(mysqli_error($connect));
                        if (0 < mysqli_affected_rows($connect) || $fet_ren_user["reset_counters"] == 0) {
                            $insert_radacct = mysqli_query($connect, "
INSERT INTO `radacct` (
`acctsessionid`,
`acctuniqueid`,
`username`,
`groupname`,
`last_speed`,
`nasipaddress`,
`acctstarttime`,
`acctstoptime`,
`acctinputoctets`,
`acctoutputoctets`,
`calledstationid`,
`callingstationid`,`framedipaddress`,`t_session`,
`nas_name`,
`percent_q`,
`up_uq`,
`down_uq`,
`all_tu`,
`re_up`,
`re_down`,
`re_time`
)
VALUES (
'" . $fet_online["acctsessionid"] . "',
'" . $fet_online["acctuniqueid"] . "',
'" . $fet_online["username"] . "',
'" . $fet_online["groupname"] . "',
'" . $fet_online["last_speed"] . "',
'" . $fet_online["nasipaddress"] . "',
CURRENT_TIMESTAMP,
NULL,
'0',
'0',
'" . $fet_online["calledstationid"] . "',
'" . $fet_online["callingstationid"] . "',
'" . $fet_online["framedipaddress"] . "','" . time() . "',
'" . $fet_online["nas_name"] . "',
COALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),
COALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $update_date . "' LIMIT 1),0),
COALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $update_date . "' LIMIT 1),'0'),
COALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >='" . $update_date . "'  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),
'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',
'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',
'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'
);
") or exit(mysqli_error($connect));
                        }
                    }
                } else {
                    if ($is_new_time == 1 || $fet_ren_user["remaining_qouta"] == 1) {
                        $update_radacct = mysqli_query($connect, "UPDATE `radacct` SET `down_uq`=0,up_uq=0,all_tu=0 WHERE `username` ='" . $get_user . "' ORDER BY `radacctid` DESC LIMIT 1 ");
                    }
                }
                if ($fet_set["enable_sms"] == 1) {
                    $url = $fet_set["api_url"];
                    $msg = $fet_set["act_msg"];
                    if ($url && $msg && $fet_set["renew_user_sms"] == 1) {
                        $sel_all_users = mysqli_query($connect, "SELECT 
	
	`userinfo`.`username`,
	`userinfo`.`mobile`,
	`userinfo`.`id`,
	`radusergroup`.`groupname` as `prof`,
	`radpass`.`value` as `pass`,
	DATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y') as `last_send_sms`,
	DATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y %H:%i:%s') as `exp`
	FROM `userinfo`
	LEFT JOIN `radcheck` ON (`radcheck`.`username` = `userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration')
	LEFT JOIN `radusergroup` ON (`radusergroup`.`username` = `userinfo`.`username`)
	LEFT JOIN `radcheck` as `radpass` ON (`radpass`.`username` = `userinfo`.`username` AND `radpass`.`attribute` ='Cleartext-Password' )
	WHERE `userinfo`.`username`='" . $get_user . "' GROUP BY `userinfo`.`username`");
                        if (0 < mysqli_num_rows($sel_all_users)) {
                            $fet_all_users = mysqli_fetch_array($sel_all_users);
                            $user = $fet_all_users["username"];
                            $pass = $fet_all_users["pass"];
                            $exp = $fet_all_users["exp"];
                            $prof = $fet_all_users["prof"];
                            $phone = $fet_all_users["mobile"];
                            $msg = str_ireplace("{price}", $amount, $msg);
                            sendSMS($url, $user, $pass, $exp, $prof, $day, $msg, $sender, $phone);
                        }
                    }
                }
                if (0 < count($exp_af_unset)) {
                    $is_list = 1;
                } else {
                    $is_list = 0;
                }
                if ($v_recharge == 1) {
                    $val_for_event = $get_user . " (" . $fet_renew["p_recharge"] . " ";
                    mysqli_query($connect, "INSERT INTO `man_actions` 
(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`id_user`,`ip`,`get_os`,`browser`) VALUES 
('" . $fet_ren_user["id_man"] . "','64','1','" . $val_for_event . "','" . time() . "','" . $fet_renew["id_user"] . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
                    if (0 < $fet_ren_user["p_recharge"]) {
                        mysqli_query($connect, "UPDATE `managers` SET `credit`=`credit`+'" . $fet_ren_user["p_recharge"] . "' WHERE id='" . $fet_ren_user["ad_recharge"] . "' AND is_admin='0'");
                        mysqli_query($connect, "UPDATE `managers` SET `usage`=`usage`-'" . $fet_ren_user["p_recharge"] . "' WHERE id='" . $fet_ren_user["ad_recharge"] . "'");
                    }
                }
                $exp_af_unset = serialize($exp_af_unset);
                $update = mysqli_query($connect, "UPDATE `userinfo` SET `act_values` ='" . $exp_af_unset . "' ,`is_list`='" . $is_list . "' WHERE `username` ='" . $get_user . "' ");
                $update_user = mysqli_query($connect, "UPDATE `radcheck` SET `value` = '" . $set_date_exp . "' WHERE username='" . $get_user . "' AND attribute='Expiration'");
                mysqli_query($connect, "INSERT INTO `man_actions` 
(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`id_user`,`ip`,`get_os`,`browser`) VALUES 
('" . $fet_ren_user["id_man"] . "','5','1','" . $get_user . "','" . time() . "','" . $fet_renew["id_user"] . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
            }
        }
    }
}
function get_profile($main_id, $usage, $quota, $id_prof) {
    global $connect;
    global $profiles;
    global $q;
    $num = 1;
    if (!$id_prof) {
        $id_prof = $profiles["id_prof"];
    }
    $sql = "SELECT 

`p`.`profile_name` as `profile_name`,
`p`.`id` as `id_prof`,
`p`.`daily_down_qouta`*1024*1024 as `down_daily_qouta`,
`p`.`daily_up_qouta`*1024*1024 as `up_daily_qouta`,
`p`.`daily_profile_qouta`*1024*1024 as `daily_profile_qouta`,
`p`.`online_time` as `online_time`,
`p`.`hours_min` as `hours_min`,
`p`.`daily_online_time` as `daily_online_time`,
`p`.`daily_hours_min` as `daily_hours_min`,
`p`.`bandwidth_time` as `bandwidth_time`,
`p`.`multi_speed` as `multi_speed`,
`p`.`bandwidth_percent` as `bandwidth_percent`,
`p`.`daily_expire_service` as `daily_expire_service`,
`p`.`qouta_expire_service` as `qouta_expire_service`,
`pd`.`profile_expire_service` as `profile_expire_service`,
`p`.`sp_up` as `sp_up`,
`p`.`sp_down` as `sp_down`,
`p`.`ch_day_end` as `ch_day_end`,
`p`.`set_day_end` as `set_day_end`,
`p`.`percent` as `percent`,
`p`.`arr_days` as `p_arr_days`,
`p`.`value_choice` as `p_value_choice`,
`r1`.`value` as `value_exp`,
`p1`.`bandwidth_time` as `band_exp`,
`p1`.`bandwidth_percent` as `band_exp_percent`,
`p1`.`percent` as `percent_exp`,
`p1`.`profile_qouta` as `profile_qouta2`,
`r2`.`value` as `val_address_exp_list`,
`po1`.`value` as `q_pool`,
`po2`.`value` as `d_pool`,
`po3`.`value` as `ex_pool`,
`po4`.`value` as `ad_pool`,
`r3`.`value` as `value_exp_daily`,
`p2`.`bandwidth_time` as `band_daily_exp`,
`p2`.`bandwidth_percent` as `band_daily_exp_percent`,
`p2`.`percent` as `percent_daily_exp`,
`r4`.`value` as `val_address_exp_daily_list`,
`r5`.`value` as `value_exp_serv`,
`p3`.`bandwidth_time` as `band_serv_exp`,
`p3`.`bandwidth_percent` as `band_serv_exp_percent`,
`p3`.`percent` as `percent_serv_exp`,
`r6`.`value` as `val_address_exp_serv_list`,
`r7`.`value` as `value_now`,
`r8`.`value` as `val_address_list`
 FROM `profiles` `p`
 LEFT JOIN `profiles` `pd` ON (`pd`.`id` = '" . $id_prof . "')
 LEFT JOIN `profiles` `p1` ON (`p1`.`id` = `p`.`qouta_expire_service`)
 LEFT JOIN `profiles` `p2` ON (`p2`.`id` = `p`.`daily_expire_service`)
 LEFT JOIN `profiles` `p3` ON (`p3`.`id` = `pd`.`profile_expire_service`)
 LEFT JOIN `radgroupreply` `r1` ON (`r1`.`groupname` = `p1`.`profile_name` AND `r1`.`attribute` = 'Mikrotik-Rate-Limit')
 LEFT JOIN `radgroupreply` `r2` ON (`r2`.`groupname` = `p1`.`profile_name` AND `r2`.`attribute` = 'Mikrotik-Address-List') 
 LEFT JOIN `radgroupreply` `po1` ON (`po1`.`groupname` = `p1`.`profile_name` AND `po1`.`attribute` = 'Framed-Pool')
 LEFT JOIN `radgroupreply` `r3` ON (`r3`.`groupname` = `p2`.`profile_name` AND `r3`.`attribute` = 'Mikrotik-Rate-Limit')
 LEFT JOIN `radgroupreply` `r4` ON (`r4`.`groupname` = `p2`.`profile_name` AND `r4`.`attribute` = 'Mikrotik-Address-List') 
 LEFT JOIN `radgroupreply` `po2` ON (`po2`.`groupname` = `p2`.`profile_name` AND `po2`.`attribute` = 'Framed-Pool') 
 LEFT JOIN `radgroupreply` `r5` ON (`r5`.`groupname` = `p3`.`profile_name` AND `r5`.`attribute` = 'Mikrotik-Rate-Limit')
 LEFT JOIN `radgroupreply` `r6` ON (`r6`.`groupname` = `p3`.`profile_name` AND `r6`.`attribute` = 'Mikrotik-Address-List')
 LEFT JOIN `radgroupreply` `po3` ON (`po3`.`groupname` = `p3`.`profile_name` AND `po3`.`attribute` = 'Framed-Pool')
 LEFT JOIN `radgroupreply` `r7` ON (`r7`.`groupname` = `p`.`profile_name` AND `r7`.`attribute` = 'Mikrotik-Rate-Limit')
 LEFT JOIN `radgroupreply` `r8` ON (`r8`.`groupname` = `p`.`profile_name` AND `r8`.`attribute` = 'Mikrotik-Address-List')
 LEFT JOIN `radgroupreply` `po4` ON (`po4`.`groupname` = `p`.`profile_name` AND `po4`.`attribute` = 'Framed-Pool')
 WHERE `p`.`id` = '" . $main_id . "'";
    $query = mysqli_query($connect, $sql) or exit(mysqli_error($connect));
    $result = mysqli_fetch_assoc($query);
    if (!empty($result)) {
        $q+= $quota;
        $result["usage"] = $usage;
        $result["q"] = $q;
        $profiles = $result;
        if ($num == 1) {
            if ($result["qouta_expire_service"] != 0 && $quota != 0 && $q < $result["usage"]) {
                $num++;
                if ($result["profile_qouta2"] == 0) {
                    $result["profile_qouta2"] = 1048576;
                }
                return get_profile($result["qouta_expire_service"], $result["usage"], $result["profile_qouta2"] * 1024 * 1024, $id_prof);
            }
        } else {
            if ($result["qouta_expire_service"] != 0 && $quota != 0 && $q < $result["usage"] && 0 < $result["profile_qouta"]) {
                $num++;
                return get_profile($result["qouta_expire_service"], $result["usage"], $result["profile_qouta2"] * 1024 * 1024, $id_prof);
            }
        }
    }
    $q = 0;
    $num = 0;
    return $profiles;
}
?>

Did this file decode correctly?

Original Code

<?php
/*
 * @ https://EasyToYou.eu - IonCube v10 Decoder Online
 * @ PHP 5.6
 * @ Decoder version: 1.0.4
 * @ Release: 02/06/2020
 *
 * @ ZendGuard Decoder PHP 5.6
 */

if (isset($m) && $m == 1) {
    return "valid";
}
if ($_REQUEST["sh"]) {
    exit("1");
}
$nn = 0;
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
ini_set("max_execution_time", 0);
$server = "localhost";
$user_db = "root";
$pass_db = "advradius_system";
$db = "radius";
$connect = mysqli_connect($server, $user_db, $pass_db, $db) or exit("");
mysqli_set_charset($connect, "utf8");
$sel_r_port = mysqli_query($connect, "SELECT  * FROM `ip_tunnel`");
if (0 < mysqli_num_rows($sel_r_port)) {
    shell_exec("nohup sudo curl http://127.0.0.1/auto_rec_tun.php >/dev/null 2>&1 &");
}
require_once "includes/functions.php";
$sel_set = mysqli_query($connect, "SELECT  * FROM `settings`");
$fet_set = mysqli_fetch_assoc($sel_set);
if ($fet_set["up_pool"] < time() - 600) {
    $sel_pool = mysqli_query($connect, "SELECT  * FROM ip_pool");
    if (0 < mysqli_num_rows($sel_pool)) {
        mysqli_query($connect, "UPDATE `radippool` \r\nSET `nasipaddress` = '',\r\n`calledstationid` = '',\r\n`callingstationid` = '',\r\n`username` = '',\r\n`pool_key` = '',\r\n`expiry_time` = NULL\r\nWHERE \r\n`username` !=''  AND \r\n`framedipaddress` NOT IN (SELECT `framedipaddress` FROM `radacct` WHERE  acctstoptime IS  NULL )\r\n");
    }
    $update_up_pool = mysqli_query($connect, "UPDATE `settings` SET `up_pool` = '" . time() . "'");
}
if (file_exists("rr.php")) {
    mysqli_query($connect, "ALTER TABLE `profiles` ADD `portid` VARCHAR( 100 ) NOT NULL ;");
    mysqli_query($connect, "ALTER TABLE `userinfo` ADD `portid` VARCHAR( 100 ) NOT NULL ;");
    mysqli_query($connect, "ALTER TABLE `managers` ADD `portid` VARCHAR( 100 ) NOT NULL ;");
    exec("sudo rm /var/www/html/rr.php");
}
require_once "lang/en.php";
$check_network = shell_exec("sudo md5sum /etc/network/interfaces | cut -d' ' -f1 | tr -d ' \t\n\r' 2>/dev/null");
if ($check_network == "e0a39177143f04f0868e30f359d681de") {
    shell_exec("nohup sudo curl http://127.0.0.1/network.php >/dev/null 2>&1 &");
}
$check_default = shell_exec("sudo md5sum /etc/freeradius/sites-available/default | cut -d' ' -f1 | tr -d ' \t\n\r' 2>/dev/null");
if ($check_default != "960bc9dd72a0403b906e3a0fbea47a10") {
    $real_path = str_ireplace("/includes", "", realpath(dirname(__FILE__)));
    $get_page = decryptIt(shell_exec("sudo cat " . $real_path . "/api/sum_cards  2>/dev/null"));
    shell_exec("sudo chmod 0777 /etc/freeradius/sites-available/default 2>&1 2>/dev/null");
    file_put_contents("/etc/freeradius/sites-available/default", $get_page);
    shell_exec("sudo chmod 0644 /etc/freeradius/sites-available/default 2>/dev/null");
    $check_default = shell_exec("sudo md5sum /etc/freeradius/sites-available/default | cut -d' ' -f1 | tr -d ' \t\n\r' 2>/dev/null");
    if ($check_default != "960bc9dd72a0403b906e3a0fbea47a10") {
        exit("<center><h2> Edited Important Files :  <img src='emotions/sm20.png'/>  Now You Need Reinstall Adv Radius </h2></center>");
    }
}
$check_radiusd = shell_exec("sudo md5sum /etc/freeradius/radiusd.conf | cut -d' ' -f1 | tr -d ' \t\n\r' 2>/dev/null");
if ($check_radiusd != "0741336b03773cad816d1728bea59a31") {
    $real_path = str_ireplace("/includes", "", realpath(dirname(__FILE__)));
    $get_page = decryptIt(shell_exec("sudo cat " . $real_path . "/api/sum_users 2>/dev/null"));
    shell_exec("sudo chmod 0777 /etc/freeradius/radiusd.conf 2>&1 2>/dev/null");
    file_put_contents("/etc/freeradius/radiusd.conf", $get_page);
    shell_exec("sudo chmod 0640 /etc/freeradius/radiusd.conf 2>&1 2>/dev/null");
    $check_radiusd = shell_exec("sudo md5sum /etc/freeradius/radiusd.conf | cut -d' ' -f1 | tr -d ' \t\n\r' 2>/dev/null");
    if ($check_radiusd != "0741336b03773cad816d1728bea59a31") {
        exit("<center><h2> Edited Important Files :  <img src='emotions/sm20.png'/> Now You Need Reinstall Adv Radius </h2></center>");
    }
}
//api port sorgusu
require_once "api/api_update.php";
$API = new routeros_api();
$check_radclient = shell_exec("sudo pgrep radclient;");
if (!empty($check_radclient)) {
    function availableip($host, $port = 80, $timeout = 0.002)
    {
        $fp = @fSockOpen($host, $port, $errno, $errstr, $timeout);
        return $fp != false;
    }
    exit;
}
$status_radius = shell_exec("service  freeradius status  2>&1 2>/dev/null");
$status_radius = preg_replace("/\\s+/", "", $status_radius);
$status_radius = explode(",", $status_radius);
$status_radius = $status_radius[0];
if ($fet_set["radius_st"] == 1) {
    if ($status_radius != "*freeradiusisrunning") {
        shell_exec("sudo service freeradius start 2>&1 2>/dev/null");
    }
} else {
    if ($status_radius == "*freeradiusisrunning") {
        shell_exec("sudo service freeradius stop 2>&1 2>/dev/null");
    }
}
$timezone = shell_exec("cat /etc/timezone 2>/dev/null");
$timezone = preg_replace("/\\s+/", "", $timezone);
date_default_timezone_set($timezone);
$speed = "";
exec("timeout 5 ping 8.8.8.8 -W 2 -c 1 2>&1 2>/dev/null", $aa, $b);
if ($b == 1) {
    mysqli_query($connect, "UPDATE `settings` SET `check_connection`='0' WHERE `check_connection`!='0'");
} else {
    $sel_plans = mysqli_query($connect, "SELECT `id` FROM `plan_adv` ");
    if (mysqli_num_rows($sel_plans) != 1) {
        require_once "update_sub_users00.php";
    }
    mysqli_query($connect, "UPDATE `settings` SET `check_connection`='1' WHERE `check_connection`!='1'");
}
$time_before_1_month = time() - 30 * 24 * 60 * 60;
$sel_manager_end_unit = mysqli_query($connect, "SELECT  `managers`.`id`,`managers`.`created_by`,`managers`.`monthly_test_units`,`cre_man`.`is_admin`,COALESCE(`cre_man`.`trial`,0) as `trial` FROM `managers`\r\nINNER JOIN `managers` as `cre_man` ON (`cre_man`.`id` = `managers`.`created_by`)\r\n WHERE `managers`.`date_trial` < '" . $time_before_1_month . "'");
if (0 < mysqli_num_rows($sel_manager_end_unit)) {
    while ($fet_manager_end_unit = mysqli_fetch_assoc($sel_manager_end_unit)) {
        $trial = $fet_manager_end_unit["trial"];
        if (!$trial) {
            $trial = 0;
        }
        if ($fet_manager_end_unit["is_admin"] == 0) {
            if ($fet_manager_end_unit["monthly_test_units"] <= $fet_manager_end_unit["trial"]) {
                $update_usage = mysqli_query($connect, "UPDATE  `managers` SET `trial` = `trial` - " . $trial . " , `trial_usage` = `trial_usage` + " . $trial . " WHERE `id`='" . $fet_manager_end_unit["created_by"] . "'") or exit(mysqli_error($connect));
                $update_manager_end_unit = mysqli_query($connect, "UPDATE `managers` SET `trial` = `monthly_test_units`,`date_trial` = '" . time() . "' WHERE `id` = '" . $fet_manager_end_unit["id"] . "'") or exit(mysqli_error($connect));
            }
        } else {
            $update_usage = mysqli_query($connect, "UPDATE  `managers` SET  `trial_usage` = `trial_usage` + " . $trial . " WHERE `id`='" . $fet_manager_end_unit["created_by"] . "'") or exit(mysqli_error($connect));
            $update_manager_end_unit = mysqli_query($connect, "UPDATE `managers` SET `trial` = `monthly_test_units`,`date_trial` = '" . time() . "' WHERE `id` = '" . $fet_manager_end_unit["id"] . "'") or exit(mysqli_error($connect));
        }
    }
}
$sel_set = mysqli_query($connect, "SELECT \r\n\r\n*,\r\nIF(idle_adv_type = '1', \r\nIFNULL(idle_timeout_adv,0) * (60),IF(idle_adv_type = '2', \r\nIFNULL(idle_timeout_adv,0) * (60*60),IF(idle_adv_type = '3', \r\nIFNULL(idle_timeout_adv,0) * (60*60*24),IF(idle_adv_type = '4', \r\nIFNULL(idle_timeout_adv,0) * (60*60*24*30),0)))) as timeout_adv FROM `settings`");
$fet_set = mysqli_fetch_assoc($sel_set);
$sel_plan = mysqli_query($connect, "SELECT  *  FROM `plan_adv`");
$fet_plan = mysqli_fetch_assoc($sel_plan);
$select_count_users = mysqli_query($connect, "SELECT  COUNT(radacctid) as `count_r_users` FROM  `radacct` WHERE acctstoptime IS NULL ");
$fet_count_users = mysqli_fetch_assoc($select_count_users);
$users_info = $fet_count_users["count_r_users"];
if (decryptIt($fet_plan["users"]) < $users_info) {
    $limit = $users_info - decryptIt($fet_plan["users"]);
    $sel_more = mysqli_query($connect, "SELECT `username` FROM `radcheck` WHERE `attribute`='Cleartext-Password' AND `is_card` !='1' ORDER BY id desc LIMIT " . $limit . " ") or exit(mysqli_error($connect));
    if (0 < mysqli_num_rows($sel_more)) {
        while ($fet_more = mysqli_fetch_assoc($sel_more)) {
            $arr_del[] = $fet_more["username"];
        }
        if (0 < count($arr_del)) {
            $sel_user_radacct = mysqli_query($connect, "SELECT `radacct`.`nasipaddress`,`radacct`.`framedipaddress`,`radacct`.`username`,`nas`.`secret` FROM `radacct` LEFT JOIN `nas` ON (`radacct`.`nasipaddress` = `nas`.`nasname`) WHERE radacct.username  IN ('" . join("','", $arr_del) . "') AND radacct .acctstoptime IS NULL ");
            $send = "";
            if (0 < mysqli_num_rows($sel_user_radacct)) {
                while ($fet_user_radacct = mysqli_fetch_assoc($sel_user_radacct)) {
                    $send = "echo  user-name='" . $fet_user_radacct["username"] . "' ,Framed-IP-Address='" . $fet_user_radacct["framedipaddress"] . "' |  /usr/bin/radclient -x  " . $fet_user_radacct["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_user_radacct["secret"] . " -t 0.2 ;" . 222;
                    $res = exec($send);
                }
            }
        }
    }
}
$dis_update = shell_exec("sudo cat /etc/freeradius/certs/va.key 2>&1 2>/dev/null");
if (empty($dis_update)) {
    $dis_update = 0;
}
if (288 <= decryptIt($dis_update)) {
    $sel_user_radacct = mysqli_query($connect, "SELECT  `radacct`.`nasipaddress`,`radacct`.`framedipaddress`,`radacct`.`username`,`nas`.`secret` FROM `radacct` LEFT JOIN `nas` ON (`radacct`.`nasipaddress` = `nas`.`nasname`) WHERE  radacct .acctstoptime IS NULL ");
    if (0 < mysqli_num_rows($sel_user_radacct)) {
        $send = "";
        //Mteriyi dren sorgu
         while ($fet_user_radacct = mysqli_fetch_assoc($sel_user_radacct)) {
             // $send = "echo  user-name='" . $fet_user_radacct["username"] . "' ,Framed-IP-Address='" . $fet_user_radacct["framedipaddress"] . "' |  /usr/bin/radclient -x  " . $fet_user_radacct["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_user_radacct["secret"] . " -t 0.2 ;" . 248;
            $res = exec($send);
        }
    }
}
if ($fet_set["track_users"] == 1) {
    shell_exec("nohup sudo curl http://127.0.0.1/get_sess.php?adv=1 >/dev/null 2>&1 &");
}
if ($fet_set["is_backup"] == 1) {
    if ($fet_set["val_type_backup"] == 0) {
        $backup_time = $fet_set["backup_time"] * 60 * 60;
    } else {
        if ($fet_set["val_type_backup"] == 1) {
            $backup_time = $fet_set["backup_time"] * 60 * 60 * 24;
        } else {
            if ($fet_set["val_type_backup"] == 2) {
                $backup_time = $fet_set["backup_time"] * 60 * 60 * 24 * 30;
            } else {
                if ($fet_set["val_type_backup"] == 3) {
                    $backup_time = $fet_set["backup_time"] * 60 * 60 * 24 * 30 * 12;
                }
            }
        }
    }
    $all_backup_time = time() - $backup_time;
    $sel_last_backup = mysqli_query($connect, "SELECT  * FROM `settings` WHERE l_time_backup < " . $all_backup_time);
    if (mysqli_num_rows($sel_last_backup) == 1) {
        $update_val_backup = mysqli_query($connect, "UPDATE `settings` SET `l_time_backup` = '" . time() . "'");
        $time = date("ymd_H_i_s");
        $time_add = time();
        $file_backup = "backup_db";
        $f_name = "adv_db_autobackup-" . $time;
        $file = $file_backup . "/" . $f_name . ".sql.gz";
        $no_export = array("countries", "dictionary", "db_backup", "adv_currency", "lang_adv", "plan_adv", "settings", "timezone", "radpostauth");
        backup_tables($file, $no_export);
        $size = formatSizeUnits(filesize($file));
        $insert_backup = mysqli_query($connect, "INSERT INTO `db_backup` (`f_name`,`size`,`date_added`) VALUES ('" . $f_name . "','" . $size . "','" . $time_add . "')");
        if ($fet_set["enable_upload_db"] == 1) {
            shell_exec("sudo curl http://127.0.0.1/upload_gdrive.php?adv=1 >/dev/null 2>&1");
        }
        if ($insert_backup) {
            mysqli_query($connect, "INSERT INTO `man_actions` \r\n(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`ip`,`get_os`,`browser`) VALUES \r\n('1','32','7','" . $f_name . "','" . $time_add . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
        }
    }
}
if (0 < $fet_set["auto_dis_user"]) {
    if ($fet_set["dis_user_ch"] == 0) {
        $dis_u_af_time = $fet_set["auto_dis_user"] * 60;
    } else {
        if ($fet_set["dis_user_ch"] == 1) {
            $dis_u_af_time = $fet_set["auto_dis_user"] * 60 * 60;
        } else {
            if ($fet_set["dis_user_ch"] == 2) {
                $dis_u_af_time = $fet_set["auto_dis_user"] * 60 * 60 * 24;
            } else {
                if ($fet_set["dis_user_ch"] == 3) {
                    $dis_u_af_time = $fet_set["auto_dis_user"] * 60 * 60 * 24 * 30;
                } else {
                    if ($fet_set["dis_user_ch"] == 4) {
                        $dis_u_af_time = $fet_set["auto_dis_user"] * 60 * 60 * 24 * 30 * 12;
                    }
                }
            }
        }
    }
    $dis_u_time = time() - $dis_u_af_time;
    $sel_dis_u = mysqli_query($connect, "SELECT \r\n\r\n`radcheck`.`username` FROM `radcheck` \r\nINNER JOIN `userinfo` ON (`radcheck`.`username` = `userinfo`.`username`)\r\nWHERE `radcheck`.`attribute`='Expiration' \r\nAND  UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`, '%d %b %Y %H:%i')) < " . $dis_u_time);
    if (0 < mysqli_num_rows($sel_dis_u)) {
        while ($fet_dis_users = mysqli_fetch_assoc($sel_dis_u)) {
            $update_dis = mysqli_query($connect, "UPDATE `radcheck` as  `r`\r\nSET `r`.`a` = '1'\r\nWHERE  `r`.`attribute` = 'Cleartext-Password' AND `r`.`a` = 0 AND `r`.`username` = '" . $fet_dis_users["username"] . "'") or exit(mysqli_error($connect));
        }
    }
}
if (0 < $fet_set["auto_del"]) {
    if ($fet_set["del_users_ch"] == 0) {
        $del_u_af_time = $fet_set["auto_del"] * 60;
    } else {
        if ($fet_set["del_users_ch"] == 1) {
            $del_u_af_time = $fet_set["auto_del"] * 60 * 60;
        } else {
            if ($fet_set["del_users_ch"] == 2) {
                $del_u_af_time = $fet_set["auto_del"] * 60 * 60 * 24;
            } else {
                if ($fet_set["del_users_ch"] == 3) {
                    $del_u_af_time = $fet_set["auto_del"] * 60 * 60 * 24 * 30;
                } else {
                    if ($fet_set["del_users_ch"] == 4) {
                        $del_u_af_time = $fet_set["auto_del"] * 60 * 60 * 24 * 30 * 12;
                    }
                }
            }
        }
    }
    $del_u_time = time() - $del_u_af_time;
    $sel_all_auto_del_u = mysqli_query($connect, "SELECT \r\n\r\n`radcheck`.`id` FROM `radcheck` \r\nINNER JOIN `userinfo` ON (`radcheck`.`username` = `userinfo`.`username`)\r\nWHERE `radcheck`.`attribute`='Expiration' \r\nAND  UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`, '%d %b %Y %H:%i')) < " . $del_u_time);
    if (0 < mysqli_num_rows($sel_all_auto_del_u)) {
        while ($fet_auto_del_u = mysqli_fetch_assoc($sel_all_auto_del_u)) {
            mysqli_query($connect, "DELETE FROM radreply WHERE `username` = '" . $fet_auto_del_u["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radacct WHERE `username` = '" . $fet_auto_del_u["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radusergroup WHERE `username` = '" . $fet_auto_del_u["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE  userinfo,\r\n\ta_s_man_users,a_s_manager_trial_users,list_cards,users_actions\r\n\tFROM userinfo \r\n\tLEFT JOIN `radcheck` as `radexp` ON (`radexp`.`username`=`userinfo`.`username` AND `radexp`.`attribute`='Expiration')\r\n\tLEFT JOIN `a_s_man_users` ON (`a_s_man_users`.`id_user`=`userinfo`.`id`)\r\n\tLEFT JOIN `a_s_manager_trial_users` ON (`a_s_manager_trial_users`.`id_user`=`userinfo`.`id`)\r\n\tLEFT JOIN `list_cards` ON (`list_cards`.`used_by`=`userinfo`.`id`)\r\n\tLEFT JOIN `users_actions` ON (`users_actions`.`id_user`=`userinfo`.`id`)\r\n\tWHERE `userinfo`.`username` = '" . $fet_auto_del_u["username"] . "' ") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radcheck WHERE `username` = '" . $fet_auto_del_u["username"] . "'") or exit(mysqli_error($connect));
        }
    }
}
if (0 < $fet_set["auto_del_card"]) {
    if ($fet_set["del_card_ch"] == 0) {
        $del_c_af_time = $fet_set["auto_del_card"] * 60;
    } else {
        if ($fet_set["del_card_ch"] == 1) {
            $del_c_af_time = $fet_set["auto_del_card"] * 60 * 60;
        } else {
            if ($fet_set["del_card_ch"] == 2) {
                $del_c_af_time = $fet_set["auto_del_card"] * 60 * 60 * 24;
            } else {
                if ($fet_set["del_card_ch"] == 3) {
                    $del_c_af_time = $fet_set["auto_del_card"] * 60 * 60 * 24 * 30;
                } else {
                    if ($fet_set["del_card_ch"] == 4) {
                        $del_c_af_time = $fet_set["auto_del_card"] * 60 * 60 * 24 * 30 * 12;
                    }
                }
            }
        }
    }
    $del_c_time = time() - $del_c_af_time;
    $sel_all_auto_del_c = mysqli_query($connect, "SELECT\r\n \r\n`radcheck`.`username`\r\n FROM `radcheck` \r\nINNER JOIN `card_users` ON (`radcheck`.`id_card` = `card_users`.`id`)\r\nWHERE (`radcheck`.`attribute`='Expiration' \r\nAND  UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`, '%d %b %Y %H:%i')) < " . $del_c_time . ")\r\nOR (`card_users`.`exp_date` >0 AND `card_users`.`exp_date` < " . $del_c_time . " )\r\n");
    if (0 < mysqli_num_rows($sel_all_auto_del_c)) {
        while ($fet_auto_del_c = mysqli_fetch_assoc($sel_all_auto_del_c)) {
            mysqli_query($connect, "DELETE FROM radcheck WHERE `username` = '" . $fet_auto_del_c["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radreply WHERE `username` = '" . $fet_auto_del_c["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radacct WHERE `username` = '" . $fet_auto_del_c["username"] . "'") or exit(mysqli_error($connect));
            mysqli_query($connect, "DELETE FROM radusergroup WHERE `username` = '" . $fet_auto_del_c["username"] . "'") or exit(mysqli_error($connect));
        }
        mysqli_query($connect, "DELETE `radius`.`card_users` FROM `card_users`\r\nLEFT JOIN `radcheck` `r1` ON (`r1`.`id_card` = `card_users`.`id`)\r\nWHERE `r1`.`id` IS NULL \r\n");
    }
}
mysqli_query($connect, "UPDATE `managers` SET `usage`='0' WHERE `usage`< 0");
mysqli_query($connect, "UPDATE `managers` SET `credit`='0' WHERE `credit`< '0'");
mysqli_query($connect, "UPDATE `managers` SET `trial_credit`='0' WHERE `trial_credit`< '0'");
mysqli_query($connect, "UPDATE `managers` SET `trial`='0' WHERE `trial`< '0'");
$arr_up = array();
$arr_nas = array();
$n = 0;
$m = 0;
$p = 0;
$pools = array();
$profiles = array();
$q = 0;
$arr_det_user = array();
$n2 = 0;
$sel_online = mysqli_query($connect, " SELECT \r\n\r\n`nas`.*,\r\n`nas`.`id` as `id_nas`,\r\n`r9`.`id` as `id_u`,\r\nCOALESCE(`nas`.`api_is_enabled`,'0') as `api_is_en`,\r\n`radacct`.`nasipaddress`,\r\n`radcheck`.`address_list_name`,\r\n`radcheck`.`address_val`,\r\n`radcheck`.`framed_pool`,\r\n`radcheck`.`a` as `dis`,\r\n`radacct`.`framedipaddress`,\r\n`radacct`.`callingstationid`,\r\n`radacct`.`calledstationid`,\r\n`radacct`.`radacctid` as `radacctid`,\r\n`radacct`.`framedprotocol`,\r\n`radacct`.`username`,\r\n`r9`.`last_speed` as `l_speed` ,\r\n`radacct`.`acctsessionid` as `acctsessionid`,\r\n`radip`.`value` as `ip_address`,\r\n`radmac`.`value` as `mac_address`,\r\n`radcount`.`value` as `simul_sess`,\r\nCOALESCE(`card_users`.`id`,0) as `id_card2`,\r\nCOALESCE(`card_users`.`download_qouta`,`userinfo`.`in_down`) *1024*1024 as `down_qouta`,\r\nCOALESCE(`card_users`.`upload_qouta`,`userinfo`.`in_up`) *1024*1024 as `up_qouta`,\r\nCOALESCE(`card_users`.`all_quota`,`userinfo`.`av_qouta`) *1024*1024 as `av_qouta`,\r\n`card_users`.`date_end_card` as `date_end_card`,\r\n`card_users`.`val_date` as `val_date`,\r\n`card_users`.`per_second` as `per_second`,\r\n`card_users`.`at_the_first_login` as `at_the_first_login`,\r\n`card_users`.`exp_first_login` as `exp_first_login`,\r\n`card_users`.`val_time_exp` as `val_time_exp`,\r\n`card_users`.`time_cards_exp` as `time_cards_exp`,\r\n`card_users`.`exp_quota` as `exp_quota`,\r\n`card_users`.`exp_date` as `exp_date`,\r\n`userinfo`.`updatedate` as `updatedate`,\r\n`userinfo`.`divi_down_speed_slm` as `divi_down_speed_slm`,\r\n`userinfo`.`divi_up_speed_slm` as `divi_up_speed_slm`,\r\n`userinfo`.`arr_days` as `u_arr_days`,\r\n`userinfo`.`value_choice` as `u_value_choice`,\r\n`userinfo`.`last_end_day` as `last_end_day`,\r\n`userinfo`.`macs` as `macs`,\r\n`userinfo`.`serv_name` as `serv_name`,\r\n`userinfo`.`portid` as `portid_u`,\r\n`r9`.`percent_now` as `percent_now2`,\r\n `p`.`profile_name` as `profile_name`,\r\n `p`.`id` as `id_prof`,\r\n `p`.`portid` as `portid_p`,\r\n`p`.`daily_down_qouta`*1024*1024 as `down_daily_qouta`,\r\n`p`.`daily_up_qouta`*1024*1024 as `up_daily_qouta`,\r\n`p`.`daily_profile_qouta`*1024*1024 as `daily_profile_qouta`,\r\n`p`.`online_time` as `online_time`,\r\n`p`.`hours_min` as `hours_min`,\r\n`p`.`daily_online_time` as `daily_online_time`,\r\n`p`.`daily_hours_min` as `daily_hours_min`,\r\n`p`.`bandwidth_time` as `bandwidth_time`,\r\n`p`.`multi_speed` as `multi_speed`,\r\n`p`.`bandwidth_percent` as `bandwidth_percent`,\r\n`p`.`daily_expire_service` as `daily_expire_service`,\r\n`p`.`qouta_expire_service` as `qouta_expire_service`,\r\n`p`.`profile_expire_service` as `profile_expire_service`,\r\n`p`.`sp_up` as `sp_up`,\r\n`p`.`sp_down` as `sp_down`,\r\n`p`.`ch_day_end` as `ch_day_end`,\r\n`p`.`set_day_end` as `set_day_end`,\r\n`p`.`percent` as `percent`,\r\n`p`.`arr_days` as `p_arr_days`,\r\n`p`.`stop_u_speed` as `stop_u_speed`,\r\n`p`.`value_choice` as `p_value_choice`,\r\n`r1`.`value` as `value_exp`,\r\n`p1`.`bandwidth_time` as `band_exp`,\r\n`p1`.`bandwidth_percent` as `band_exp_percent`,\r\n`p1`.`percent` as `percent_exp`,\r\n`r2`.`value` as `val_address_exp_list`,\r\n`po1`.`value` as `q_pool`,\r\n`po2`.`value` as `d_pool`,\r\n`po3`.`value` as `ex_pool`,\r\n`po4`.`value` as `ad_pool`,\r\n`r3`.`value` as `value_exp_daily`,\r\n`p2`.`bandwidth_time` as `band_daily_exp`,\r\n`p2`.`bandwidth_percent` as `band_daily_exp_percent`,\r\n`p2`.`percent` as `percent_daily_exp`,\r\n`r4`.`value` as `val_address_exp_daily_list`,\r\n`r5`.`value` as `value_exp_serv`,\r\n`p3`.`bandwidth_time` as `band_serv_exp`,\r\n`p3`.`bandwidth_percent` as `band_serv_exp_percent`,\r\n`p3`.`percent` as `percent_serv_exp`,\r\n`r6`.`value` as `val_address_exp_serv_list`,\r\n`r7`.`value` as `value_now`,\r\n`r8`.`value` as `val_address_list`,\r\n`m1`.`nas_id` as `nas_id`,\r\n`m1`.`is_admin` as `is_ad`,\r\n`m1`.`portid` as `portid_m`,\r\nCOALESCE(acctinputoctets * (percent_q/100),0) as acctup,\r\nCOALESCE(acctoutputoctets * (percent_q/100),0) as acctdown,\r\nCOALESCE(acctsessiontime,0) as accttime,\r\nCOALESCE(all_tu,0) as all_tu,\r\nCOALESCE(down_uq,0) as down_uq,\r\nCOALESCE(up_uq,0) as up_uq,\r\nCOALESCE(all_tc,0) as all_tc,\r\nCOALESCE(down_cq,0) as down_cq,\r\nCOALESCE(up_cq,0) as up_cq,\r\nCOALESCE(ROUND(time_to_sec(`userinfo`.`online_time`)),0) as `t_online_time` ,\r\nCOALESCE(ROUND(time_to_sec(`userinfo`.`daily_online_time`)),0) as `d_online_time`,\r\nUNIX_TIMESTAMP(STR_TO_DATE(`radexp`.`value`, '%d %b %Y %H:%i')) as `exp_user`,\r\n`radacct`.*,\r\nUNIX_TIMESTAMP(STR_TO_DATE(`radacct`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) as `session_st_date`\r\n FROM `radcheck`\r\nINNER JOIN `radacct` ON (`radacct`.`username` = `radcheck`.`username`) \r\nINNER JOIN `radusergroup` `r9` ON (`r9`.`username` = `radcheck`.`username`)\r\nLEFT JOIN `userinfo` ON (`userinfo`.`username` = `r9`.`username`)\r\nLEFT JOIN `managers` `m1` ON (`m1`.`id` = `userinfo`.`owner_id`)\r\nLEFT JOIN `nas` ON (`nas`.`nasname`=`radacct`.`nasipaddress`) \r\nLEFT JOIN `profiles` `p` ON (`p`.`profile_name` = `r9`.`groupname`)\r\n LEFT JOIN `profiles` `p1` ON (`p1`.`id` = `p`.`qouta_expire_service`)\r\n LEFT JOIN `profiles` `p2` ON (`p2`.`id` = `p`.`daily_expire_service`)\r\n LEFT JOIN `profiles` `p3` ON (`p3`.`id` = `p`.`profile_expire_service`)\r\n LEFT JOIN `radgroupreply` `r1` ON (`r1`.`groupname` = `p1`.`profile_name` AND `r1`.`attribute` = 'Mikrotik-Rate-Limit')\r\n LEFT JOIN `radgroupreply` `r2` ON (`r2`.`groupname` = `p1`.`profile_name` AND `r2`.`attribute` = 'Mikrotik-Address-List') \r\n LEFT JOIN `radgroupreply` `po1` ON (`po1`.`groupname` = `p1`.`profile_name` AND `po1`.`attribute` = 'Framed-Pool')\r\n LEFT JOIN `radgroupreply` `r3` ON (`r3`.`groupname` = `p2`.`profile_name` AND `r3`.`attribute` = 'Mikrotik-Rate-Limit')\r\n LEFT JOIN `radgroupreply` `r4` ON (`r4`.`groupname` = `p2`.`profile_name` AND `r4`.`attribute` = 'Mikrotik-Address-List') \r\n LEFT JOIN `radgroupreply` `po2` ON (`po2`.`groupname` = `p2`.`profile_name` AND `po2`.`attribute` = 'Framed-Pool') \r\n LEFT JOIN `radgroupreply` `r5` ON (`r5`.`groupname` = `p3`.`profile_name` AND `r5`.`attribute` = 'Mikrotik-Rate-Limit')\r\n LEFT JOIN `radgroupreply` `r6` ON (`r6`.`groupname` = `p3`.`profile_name` AND `r6`.`attribute` = 'Mikrotik-Address-List')\r\n LEFT JOIN `radgroupreply` `po3` ON (`po3`.`groupname` = `p3`.`profile_name` AND `po3`.`attribute` = 'Framed-Pool')\r\n LEFT JOIN `radgroupreply` `r7` ON (`r7`.`groupname` = `p`.`profile_name` AND `r7`.`attribute` = 'Mikrotik-Rate-Limit')\r\n LEFT JOIN `radgroupreply` `r8` ON (`r8`.`groupname` = `p`.`profile_name` AND `r8`.`attribute` = 'Mikrotik-Address-List')\r\n LEFT JOIN `radgroupreply` `po4` ON (`po4`.`groupname` = `p`.`profile_name` AND `po4`.`attribute` = 'Framed-Pool')\r\nLEFT JOIN `card_users` ON (`card_users`.`id` = `radcheck`.`id_card`)\r\nLEFT JOIN `radreply` as `radip` ON (`radip`.`username` = `r9`.`username` AND `radip`.`attribute`='Framed-IP-Address' )\r\nLEFT JOIN `radcheck` as `radmac` ON (`radmac`.`username` = `r9`.`username` AND `radmac`.`attribute`='Calling-Station-Id' )\r\nLEFT JOIN `radcheck` as `radcount` ON (`radcount`.`username` = `r9`.`username` AND `radcount`.`attribute`='Simultaneous-Use' )\r\nLEFT JOIN `radcheck` as `radexp` ON (`radexp`.`username` = `r9`.`username` AND `radexp`.`attribute`='Expiration' )\r\nWHERE `radcheck`.`attribute` = 'Cleartext-Password' AND ( radacct.acctstoptime IS NULL ) \r\nGROUP BY `radacct`.`radacctid`") or exit(mysqli_error($connect));
$send = "";
$dis = "";
$arr_dis = array();
$arr_nas = array();
$arr_end_cards = array();
if (0 < mysqli_num_rows($sel_online)) {
    $n2 = 0;
    $dec_arr = explode("\$\$", decryptIt(shell_exec("sudo cat /var/lib/mysql/mysql5.5.flag 2>&1 2>/dev/null")));
    $c_nas = $dec_arr[0];
    $sel_anas = mysqli_query($connect, "SELECT * FROM `nas` WHERE `nasname` ='0.0.0.0'");
    $fet_anas = mysqli_fetch_assoc($sel_anas);
    while ($fet_online = mysqli_fetch_assoc($sel_online)) {
        if (empty($fet_online["secret"])) {
            $fet_online["secret"] = $fet_anas["secret"];
            $fet_online["id_nas"] = $fet_anas["id"];
            $fet_online["nasname"] = $fet_anas["nasname"];
            $fet_online["shortname"] = $fet_anas["shortname"];
            $fet_online["type"] = $fet_anas["type"];
            $fet_online["server"] = $fet_anas["server"];
            $fet_online["description"] = $fet_anas["description"];
            $fet_online["up_by"] = $fet_anas["up_by"];
            $fet_online["api_username"] = $fet_anas["api_username"];
            $fet_online["api_password"] = $fet_anas["api_password"];
            $fet_online["api_port"] = $fet_anas["api_port"];
            $fet_online["api_is_enabled"] = $fet_anas["api_is_enabled"];
            $fet_online["val_mik"] = $fet_anas["val_mik"];
            $fet_online["webport"] = $fet_anas["webport"];
        }
        $fet_online["acctup"] = $fet_online["acctup"];
        $fet_online["acctdown"] = $fet_online["acctdown"];
        $fet_online["accttime"] = $fet_online["accttime"];
        $fet_online["all_tu"] = $fet_online["all_tu"];
        $fet_online["down_uq"] = $fet_online["down_uq"];
        $fet_online["up_uq"] = $fet_online["up_uq"];
        $fet_online["all_tc"] = $fet_online["all_tc"];
        $fet_online["down_cq"] = $fet_online["down_cq"];
        $fet_online["up_cq"] = $fet_online["up_cq"];
        $sp_up = $fet_online["sp_up"];
        $sp_down = $fet_online["sp_down"];
        if ($sp_up == 1 || $sp_down == 1) {
            $sel_count_user_online_prof = mysqli_query($connect, "SELECT COUNT(DISTINCT `radacct`.`radacctid` ) as `count_login_prof` FROM `radacct`\r\n WHERE ( radacct .AcctStopTime IS NULL) AND `radacct`.`groupname` = '" . $fet_online["profile_name"] . "'");
            $arr_prof = array();
            $fet_c_u_on_prof = mysqli_fetch_assoc($sel_count_user_online_prof);
            $c_on_prof_u = $fet_c_u_on_prof["count_login_prof"];
        } else {
            $c_on_prof_u = 1;
        }
        if ($fet_online["divi_down_speed_slm"] == 1 || $fet_online["divi_up_speed_slm"] == 1) {
            $sel_c_sess = mysqli_query($connect, "SELECT COUNT(*) as `rad_count_sess` FROM `radacct` WHERE acctstoptime IS NULL AND username = '" . $fet_online["username"] . "'  GROUP BY `username` ");
            $fet_c_sess = mysqli_fetch_assoc($sel_c_sess);
            $rad_count_sess = $fet_c_sess["rad_count_sess"];
            if ($rad_count_sess == 0 || !$rad_count_sess) {
                $rad_count_sess = 1;
            }
        } else {
            $rad_count_sess = 1;
        }
        if ($fet_online["divi_down_speed_slm"] == 1) {
            $div_down_speed = $rad_count_sess;
        } else {
            $div_down_speed = 1;
        }
        if ($fet_online["divi_up_speed_slm"] == 1) {
            $div_up_speed = $rad_count_sess;
        } else {
            $div_up_speed = 1;
        }
        if ($c_on_prof_u == 0 || !$c_on_prof_u) {
            $c_on_prof_u = 1;
        }
        if ($sp_up == 1) {
            $sp_up_count_online = $c_on_prof_u;
        } else {
            $sp_up_count_online = 1;
        }
        if ($sp_down == 1) {
            $sp_down_count_online = $c_on_prof_u;
        } else {
            $sp_down_count_online = 1;
        }
        $fet_online["all_up"] = $fet_online["up_uq"] + $fet_online["acctup"];
        $fet_online["all_down"] = $fet_online["down_uq"] + $fet_online["acctdown"];
        $fet_online["all_time"] = $fet_online["all_tu"] + $fet_online["accttime"];
        $all_quota_uasge = $fet_online["all_up"] + $fet_online["all_down"];
        $id_prof = $fet_online["id_prof"];
        $val_prof = get_profile($fet_online["id_prof"], $all_quota_uasge, $fet_online["av_qouta"], $id_prof);
        if (0 < $fet_online["up_daily_qouta"] || 0 < $fet_online["down_daily_qouta"] || 0 < $profiles["daily_profile_qouta"]) {
            $sel_d_usage = mysqli_query($connect, "\r\nSELECT \r\nSUM(`r7`.`acctinputoctets` * (r7.percent_q/100)) as `up_dq`,\r\nSUM(`r7`.`acctoutputoctets` * (r7.percent_q/100)) as `down_dq`\r\n FROM `radacct` as `r7` WHERE  DATE_FORMAT(STR_TO_DATE(`r7`.`acctstarttime`,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND r7.username = '" . $fet_online["username"] . "'\r\n");
            $fet_d_usage = mysqli_fetch_assoc($sel_d_usage);
            $fet_online["up_today"] = $fet_d_usage["up_dq"];
            $fet_online["down_today"] = $fet_d_usage["down_dq"];
            $fet_online["daily_time"] = $fet_online["time_dq"] + $fet_online["accttime"];
        } else {
            $fet_online["up_today"] = 0;
            $fet_online["down_today"] = 0;
            $fet_online["daily_time"] = 0;
        }
        $all_daily_uasge = $fet_online["down_today"] + $fet_online["up_today"];
        $val_percent = $fet_online["percent_q"];
        if ($fet_online["id_card2"] == 0) {
            if (!empty($fet_online["portid_m"])) {
                $arr_pid = @explode(",", $fet_online["portid_m"]);
                if (@is_array($arr_pid) && [email protected]_array($fet_online["nasportid"], $arr_pid) || [email protected]_array($arr_pid) && $fet_online["portid_m"] != $nasportid) {
                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 833;
                    $res = exec($dis);
                    $arr_dis[] = $fet_online["radacctid"];
                }
            } else {
                if (!empty($fet_online["portid_p"]) || !empty($fet_online["portid_u"])) {
                    if (!empty($fet_online["portid_u"]) && $fet_online["portid_u"] != $fet_online["nasportid"]) {
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 839;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    } else {
                        if (empty($fet_online["portid_u"]) && !empty($fet_online["portid_p"]) && $fet_online["portid_p"] != $nasportid) {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 843;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        }
                    }
                }
            }
            if ($c_nas == 1 && is_array(unserialize($fet_online["nas_id"])) && 0 < count(unserialize($fet_online["nas_id"])) && !in_array($fet_online["id_nas"], unserialize($fet_online["nas_id"])) && $fet_online["is_ad"] == 0) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 850;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            if ($fet_set["serv_name"] == 1 && $fet_online["serv_name"] != $fet_online["calledstationid"]) {
                $p++;
                if ($p < 1000) {
                    $up_serv = mysqli_query($connect, "UPDATE `userinfo` SET `serv_name` ='" . $fet_online["calledstationid"] . "' WHERE `username` = '" . $fet_online["username"] . "'");
                }
            }
            if ($fet_online["serv_name"] && $fet_online["serv_name"] != $fet_online["calledstationid"]) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 865;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            $address_list_name = $fet_online["address_list_name"];
            $val_address_list = $fet_online["val_address_list"];
            $val_address_exp_list = $fet_online["val_address_exp_list"];
            $val_address_exp_daily_list = $fet_online["val_address_exp_daily_list"];
            $val_address_exp_serv_list = $fet_online["val_address_exp_serv_list"];
            $percent = $fet_online["percent"];
            $percent_exp = $fet_online["percent_exp"];
            $percent_daily_exp = $fet_online["percent_daily_exp"];
            $percent_serv_exp = $fet_online["percent_serv_exp"];
            $value_now = explode(" ", $fet_online["value_now"]);
            if (is_array($value_now) && $value_now[0]) {
                $val_now = explode("/", $value_now[0]);
                $up_sp = str_ireplace("k", "", $val_now[0]) * $percent / 100;
                $down_sp = str_ireplace("k", "", $val_now[1]) * $percent / 100;
                $val_now2 = explode("/", $value_now[1]);
                $up_sp2 = str_ireplace("k", "", $val_now2[0]) * $percent / 100;
                $down_sp2 = str_ireplace("k", "", $val_now2[1]) * $percent / 100;
                $val_now3 = explode("/", $value_now[2]);
                $up_sp3 = str_ireplace("k", "", $val_now3[0]) * $percent / 100;
                $down_sp3 = str_ireplace("k", "", $val_now3[1]) * $percent / 100;
                $val_now4 = explode("/", $value_now[3]);
                $up_t4 = round($val_now4[0] * $percent / 100);
                $down_t4 = round($val_now4[1] * $percent / 100);
                $val_burst_now = round($up_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4 . "/" . $down_t4 . " " . $value_now[4];
            }
            $value_exp = explode(" ", $fet_online["value_exp"]);
            if (is_array($value_exp) && $value_exp[0]) {
                $value_exp2 = explode("/", $value_exp[0]);
                $up_exp_sp = str_ireplace("k", "", $value_exp2[0]) * $percent_exp / 100;
                $down_exp_sp = str_ireplace("k", "", $value_exp2[1]) * $percent_exp / 100;
                $val_exp2 = explode("/", $value_exp[1]);
                $up_exp2 = str_ireplace("k", "", $val_exp2[0]) * $percent / 100;
                $down_exp2 = str_ireplace("k", "", $val_exp2[1]) * $percent / 100;
                $val_exp3 = explode("/", $value_exp[2]);
                $up_exp3 = str_ireplace("k", "", $val_exp3[0]) * $percent / 100;
                $down_exp3 = str_ireplace("k", "", $val_exp3[1]) * $percent / 100;
                $val_exp4 = explode("/", $value_exp[3]);
                $up_t4_exp = round($val_exp4[0] * $percent / 100);
                $down_t4_exp = round($val_exp4[1] * $percent / 100);
                $val_burst_exp_sp = round($up_exp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp . "/" . $down_t4_exp . " " . $value_exp[4];
            }
            $value_exp_daily = explode(" ", $fet_online["value_exp_daily"]);
            if (is_array($value_exp_daily) && $value_exp_daily[0]) {
                $value_exp_daily2 = explode("/", $value_exp_daily[0]);
                $up_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[0]) * $percent_daily_exp / 100;
                $down_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[1]) * $percent_daily_exp / 100;
                $val_exp_daily2 = explode("/", $value_exp_daily[1]);
                $up_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[0]) * $percent / 100;
                $down_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[1]) * $percent / 100;
                $val_exp_daily3 = explode("/", $value_exp_daily[2]);
                $up_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[0]) * $percent / 100;
                $down_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[1]) * $percent / 100;
                $val_exp_daily4 = explode("/", $value_exp_daily[3]);
                $up_t4_exp_da_sp = round($val_exp_daily4[0] * $percent / 100);
                $down_t4_exp_da_sp = round($val_exp_daily4[1] * $percent / 100);
                $val_burst_exp_da_sp = round($up_exp_da_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_da_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_da_sp . "/" . $down_t4_exp_da_sp . " " . $value_exp_daily[4];
            }
            $value_exp_serv = explode(" ", $fet_online["value_exp_serv"]);
            if (is_array($value_exp_serv) && $value_exp_serv[0]) {
                $value_exp_serv2 = explode("/", $value_exp_serv[0]);
                $up_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[0]) * $percent_serv_exp / 100;
                $down_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[1]) * $percent_serv_exp / 100;
                $val_exp_serv2 = explode("/", $value_exp_serv[1]);
                $up_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[0]) * $percent / 100;
                $down_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[1]) * $percent / 100;
                $val_exp_serv3 = explode("/", $value_exp_serv[2]);
                $up_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[0]) * $percent / 100;
                $down_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[1]) * $percent / 100;
                $val_exp_serv4 = explode("/", $value_exp_serv[3]);
                $up_t4_exp_serv_sp = round($val_exp_serv4[0] * $percent / 100);
                $down_t4_exp_serv_sp = round($val_exp_serv4[1] * $percent / 100);
                $val_burst_exp_serv_sp = round($up_exp_serv_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_serv_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_serv_sp . "/" . $down_t4_exp_serv_sp . " " . $value_exp_serv[4];
            }
            $arr_times = unserialize($fet_online["bandwidth_time"]);
            $arr_m_speed = unserialize($fet_online["multi_speed"]);
            $arr_times_percent = unserialize($fet_online["bandwidth_percent"]);
            $arr_times_exp = unserialize($fet_online["band_exp"]);
            $arr_times_exp_percent = unserialize($fet_online["band_exp_percent"]);
            $arr_times_d_exp = unserialize($fet_online["band_daily_exp"]);
            $arr_times_d_exp_percent = unserialize($fet_online["band_daily_exp_percent"]);
            $arr_times_serv_exp = unserialize($fet_online["band_serv_exp"]);
            $arr_times_serv_exp_percent = unserialize($fet_online["band_serv_exp_percent"]);
            if ($fet_set["lock_by_ip"] == 1 && !$fet_online["ip_address"] && $fet_online["simul_sess"] == 1) {
                $insert = mysqli_query($connect, "INSERT INTO `radreply` (`username`,`attribute`,`op`,`value`) VALUES ('" . $fet_online["username"] . "','Framed-IP-Address',':=','" . $fet_online["framedipaddress"] . "')");
            }
            if ($fet_set["lock_by_mac"] == 1 && !$fet_online["mac_address"] && $fet_online["mac_address"] != $fet_online["username"] && $fet_online["simul_sess"] == 1) {
                $delete = mysqli_query($connect, "DELETE FROM radcheck WHERE username='" . $fet_online["username"] . "' AND attribute='Calling-Station-Id'") or exit(mysqli_error($connect));
                $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`) VALUES ('" . $fet_online["username"] . "','Calling-Station-Id','==','" . $fet_online["callingstationid"] . "')");
            }
            if ($fet_online["dis"] == 1) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 981;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            if ($fet_online["session_st_date"] < $fet_online["updatedate"]) {
                mysqli_query($connect, "UPDATE `radacct` SET `acctstarttime` = '" . @date("Y-m-d H:i:s") . "' WHERE ( radacct .AcctStopTime IS NULL )");
            }
            $macs = unserialize($fet_online["macs"]);
            $macs = @array_filter($macs);
            if (is_array($macs) && 0 < count($macs)) {
                if (!empty($fet_online["mac_address"])) {
                    $macs[] = $fet_online["mac_address"];
                }
                $mac = $fet_online["callingstationid"];
                if (!in_array($mac, $macs)) {
                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1002;
                    $res = exec($dis);
                    $arr_dis[] = $fet_online["radacctid"];
                }
            }
            if ($fet_online["hours_min"] == 0) {
                $t_online_prof = $fet_online["online_time"] * 60;
            } else {
                $t_online_prof = $fet_online["online_time"] * 60 * 60;
            }
            if ($fet_online["daily_hours_min"] == 0) {
                $d_online_prof = $fet_online["daily_online_time"] * 60;
            } else {
                $d_online_prof = $fet_online["daily_online_time"] * 60 * 60;
            }
            if ($fet_online["u_value_choice"] == 1) {
                $arr_days = explode(",", $fet_online["u_arr_days"]);
                $today_work = arraySearch($arr_days, date("D") . "1");
                if (empty($today_work)) {
                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1023;
                    $res = exec($dis);
                    $arr_dis[] = $fet_online["radacctid"];
                }
            } else {
                if ($fet_online["p_value_choice"] == 1) {
                    $arr_days = explode(",", $fet_online["p_arr_days"]);
                    $today_work = arraySearch($arr_days, date("D") . "1");
                    if (empty($today_work)) {
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1031;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                }
            }
            if (0 < $fet_online["t_online_time"] && $fet_online["t_online_time"] <= $fet_online["all_time"] || 0 < $t_online_prof && $t_online_prof <= $fet_online["all_time"] || 0 < $fet_online["d_online_time"] && $fet_online["d_online_time"] <= $fet_online["daily_time"] || 0 < $d_online_prof && $d_online_prof <= $fet_online["daily_time"] || $fet_set["block_login"] == 1) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1042;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            $sess_day = strtotime($fet_online["acctstarttime"]);
            $time_renew = $fet_online["updatedate"];
            if ($sess_day < $time_renew) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1063;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            $address_list_name = $fet_online["address_list_name"];
            $val_address_list = $profiles["val_address_list"];
            $val_address_exp_list = $profiles["val_address_exp_list"];
            $val_address_exp_daily_list = $profiles["val_address_exp_daily_list"];
            $val_address_exp_serv_list = $profiles["val_address_exp_serv_list"];
            $percent = $profiles["percent"];
            $percent_exp = $profiles["percent_exp"];
            $percent_daily_exp = $profiles["percent_daily_exp"];
            $percent_serv_exp = $profiles["percent_serv_exp"];
            $value_now = explode(" ", $profiles["value_now"]);
            if (is_array($value_now) && $value_now[0]) {
                $val_now = explode("/", $value_now[0]);
                $up_sp = str_ireplace("k", "", $val_now[0]) * $percent / 100;
                $down_sp = str_ireplace("k", "", $val_now[1]) * $percent / 100;
                $val_now2 = explode("/", $value_now[1]);
                $up_sp2 = str_ireplace("k", "", $val_now2[0]) * $percent / 100;
                $down_sp2 = str_ireplace("k", "", $val_now2[1]) * $percent / 100;
                $val_now3 = explode("/", $value_now[2]);
                $up_sp3 = str_ireplace("k", "", $val_now3[0]) * $percent / 100;
                $down_sp3 = str_ireplace("k", "", $val_now3[1]) * $percent / 100;
                $val_now4 = explode("/", $value_now[3]);
                $up_t4 = round($val_now4[0] * $percent / 100);
                $down_t4 = round($val_now4[1] * $percent / 100);
                $val_burst_now = round($up_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4 . "/" . $down_t4 . " " . $value_now[4];
            }
            $value_exp = explode(" ", $profiles["value_exp"]);
            if (is_array($value_exp) && $value_exp[0]) {
                $value_exp2 = explode("/", $value_exp[0]);
                $up_exp_sp = str_ireplace("k", "", $value_exp2[0]) * $percent_exp / 100;
                $down_exp_sp = str_ireplace("k", "", $value_exp2[1]) * $percent_exp / 100;
                $val_exp2 = explode("/", $value_exp[1]);
                $up_exp2 = str_ireplace("k", "", $val_exp2[0]) * $percent / 100;
                $down_exp2 = str_ireplace("k", "", $val_exp2[1]) * $percent / 100;
                $val_exp3 = explode("/", $value_exp[2]);
                $up_exp3 = str_ireplace("k", "", $val_exp3[0]) * $percent / 100;
                $down_exp3 = str_ireplace("k", "", $val_exp3[1]) * $percent / 100;
                $val_exp4 = explode("/", $value_exp[3]);
                $up_t4_exp = round($val_exp4[0] * $percent / 100);
                $down_t4_exp = round($val_exp4[1] * $percent / 100);
                $val_burst_exp_sp = round($up_exp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp . "/" . $down_t4_exp . " " . $value_exp[4];
            }
            $value_exp_daily = explode(" ", $profiles["value_exp_daily"]);
            if (is_array($value_exp_daily) && $value_exp_daily[0]) {
                $value_exp_daily2 = explode("/", $value_exp_daily[0]);
                $up_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[0]) * $percent_daily_exp / 100;
                $down_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[1]) * $percent_daily_exp / 100;
                $val_exp_daily2 = explode("/", $value_exp_daily[1]);
                $up_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[0]) * $percent / 100;
                $down_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[1]) * $percent / 100;
                $val_exp_daily3 = explode("/", $value_exp_daily[2]);
                $up_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[0]) * $percent / 100;
                $down_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[1]) * $percent / 100;
                $val_exp_daily4 = explode("/", $value_exp_daily[3]);
                $up_t4_exp_da_sp = round($val_exp_daily4[0] * $percent / 100);
                $down_t4_exp_da_sp = round($val_exp_daily4[1] * $percent / 100);
                $val_burst_exp_da_sp = round($up_exp_da_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_da_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_da_sp . "/" . $down_t4_exp_da_sp . " " . $value_exp_daily[4];
            }
            $value_exp_serv = explode(" ", $profiles["value_exp_serv"]);
            if (is_array($value_exp_serv) && $value_exp_serv[0]) {
                $value_exp_serv2 = explode("/", $value_exp_serv[0]);
                $up_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[0]) * $percent_serv_exp / 100;
                $down_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[1]) * $percent_serv_exp / 100;
                $val_exp_serv2 = explode("/", $value_exp_serv[1]);
                $up_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[0]) * $percent / 100;
                $down_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[1]) * $percent / 100;
                $val_exp_serv3 = explode("/", $value_exp_serv[2]);
                $up_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[0]) * $percent / 100;
                $down_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[1]) * $percent / 100;
                $val_exp_serv4 = explode("/", $value_exp_serv[3]);
                $up_t4_exp_serv_sp = round($val_exp_serv4[0] * $percent / 100);
                $down_t4_exp_serv_sp = round($val_exp_serv4[1] * $percent / 100);
                $val_burst_exp_serv_sp = round($up_exp_serv_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_serv_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_serv_sp . "/" . $down_t4_exp_serv_sp . " " . $value_exp_serv[4];
            }
            $arr_times = unserialize($profiles["bandwidth_time"]);
            $arr_m_speed = unserialize($profiles["multi_speed"]);
            $arr_times_percent = unserialize($profiles["bandwidth_percent"]);
            $arr_times_exp = unserialize($profiles["band_exp"]);
            $arr_times_exp_percent = unserialize($profiles["band_exp_percent"]);
            $arr_times_d_exp = unserialize($profiles["band_daily_exp"]);
            $arr_times_d_exp_percent = unserialize($profiles["band_daily_exp_percent"]);
            $arr_times_serv_exp = unserialize($profiles["band_serv_exp"]);
            $arr_times_serv_exp_percent = unserialize($profiles["band_serv_exp_percent"]);
            if ($profiles["profile_expire_service"] == 0 && $fet_online["exp_user"] < time()) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1161;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            } else {
                if ($profiles["profile_expire_service"] != 0 && $fet_online["exp_user"] < time()) {
                    if (!empty($val_address_exp_serv_list) && $val_address_exp_serv_list != $address_list_name) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_serv_list . "',`address_val`='5' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1171;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_serv_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 1175;
                            $res = exec($send);
                        }
                    }
                    if ($fet_online["framed_pool"] != $profiles["ex_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["ex_pool"] . "',`address_val`='5' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1183;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (is_array($arr_times_serv_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_serv_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_serv_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_serv_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_serv_exp[$s]["from_time"], $arr_times_serv_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_serv_exp[$s]["up_rate"];
                            $down_rate = $arr_times_serv_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_serv_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_serv_sp;
                        $sp = round($up_exp_serv_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_serv_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_serv_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_serv_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_serv_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_serv_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_serv_exp_percent[$s]["frombq_time"], $arr_times_serv_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_serv_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_serv_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s'))\r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if ($fet_online["address_val"] == 5) {
                        $update_nothing_address_list = mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_list . "',`address_val`='3' WHERE `username`='" . $fet_online["username"] . "' AND `attribute` = 'Cleartext-Password'") or exit(mysqli_error($connect));
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1388;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 1392;
                            $res = exec($send);
                        }
                        if ($fet_online["framed_pool"] != $fet_online["ad_pool"]) {
                            mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["ad_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 1398;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        }
                    }
                    if ($profiles["daily_expire_service"] != 0 && 0 < $profiles["up_daily_qouta"] && $profiles["up_daily_qouta"] < $fet_online["up_today"] && $profiles["daily_profile_qouta"] == 0) {
                        if ($profiles["qouta_expire_service"] != 0 && 0 < $fet_online["up_qouta"] && $fet_online["up_qouta"] < $fet_online["all_up"] && $profiles["q"] == 0) {
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0; $s < count($arr_times_exp); $s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_exp[$s]["up_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                            }
                        } else {
                            if (is_array($arr_times_d_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0; $s < count($arr_times_d_exp); $s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_d_exp[$s]["up_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                            }
                            $nn = 0;
                            if (is_array($arr_times_d_exp_percent)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                for ($s = 0; $s < count($arr_times_d_exp_percent); $s++) {
                                    $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                                    $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                                    if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                        $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                    }
                                    $tobq_time = strtotime($tobq_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                                    $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                                    if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                        $today_work_bq = arraySearch($sel_day, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $val_percent = $percent_bq;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (0 < $nn) {
                                if ($val_percent != $fet_online["percent_q"]) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                    }
                                }
                            } else {
                                if ($val_percent != 100) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s'))\r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                    }
                                }
                            }
                        }
                    } else {
                        if ($profiles["qouta_expire_service"] != 0 && 0 < $fet_online["up_qouta"] && $fet_online["up_qouta"] < $fet_online["all_up"] && $profiles["q"] == 0) {
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0; $s < count($arr_times_exp); $s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_exp[$s]["up_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                            }
                        } else {
                            if ($profiles["q"] == 0 && (0 < $fet_online["down_qouta"] || 0 < $fet_online["up_qouta"]) && $profiles["daily_profile_qouta"] == 0) {
                                if (is_array($arr_times)) {
                                    $clock = strtotime(date("Y-m-d H:i:s"));
                                    $s = 0;
                                    for ($nn = 0; $s < count($arr_times); $s++) {
                                        $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                        $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                        if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                            $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                        }
                                        $to_time = strtotime($to_time);
                                        $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                        $up_rate = $arr_times[$s]["up_rate"];
                                        if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                            $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                            $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                                if ($nn == 0) {
                                    $speed = round($up_sp / $div_up_speed / $sp_up_count_online) . "K/";
                                    $sp = round($up_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                }
                            }
                        }
                    }
                    if ($profiles["daily_expire_service"] != 0 && 0 < $profiles["down_daily_qouta"] && $profiles["down_daily_qouta"] < $fet_online["down_today"] && $profiles["daily_profile_qouta"] == 0) {
                        if ($profiles["qouta_expire_service"] != 0 && 0 < $fet_online["down_qouta"] && $fet_online["down_qouta"] < $fet_online["all_down"] && $profiles["q"] == 0) {
                            $s = 0;
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0; $s < count($arr_times_exp); $s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $down_rate = $arr_times_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed .= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp .= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed .= round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K";
                                $sp .= round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                        } else {
                            if (is_array($arr_times_d_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0; $s < count($arr_times_d_exp); $s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $down_rate = $arr_times_d_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed .= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp .= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed .= round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) . "K";
                                $sp .= round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                            $nn = 0;
                            if (is_array($arr_times_d_exp_percent)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                for ($s = 0; $s < count($arr_times_d_exp_percent); $s++) {
                                    $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                                    $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                                    if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                        $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                    }
                                    $tobq_time = strtotime($tobq_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                                    $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                                    if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                        $today_work_bq = arraySearch($sel_day, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $val_percent = $percent_bq;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (0 < $nn) {
                                if ($val_percent != $fet_online["percent_q"]) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                    }
                                }
                            } else {
                                if ($val_percent != 100) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s'))\r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                    }
                                }
                            }
                        }
                    } else {
                        if ($profiles["qouta_expire_service"] != 0 && 0 < $fet_online["down_qouta"] && $fet_online["down_qouta"] < $fet_online["all_down"] && $profiles["q"] == 0) {
                            $s = 0;
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0; $s < count($arr_times_exp); $s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $down_rate = $arr_times_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed .= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp .= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed .= round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K";
                                $sp .= round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                        } else {
                            if ($profiles["q"] == 0 && (0 < $fet_online["down_qouta"] || 0 < $fet_online["up_qouta"]) && $profiles["daily_profile_qouta"] == 0) {
                                if (is_array($arr_times)) {
                                    $clock = strtotime(date("Y-m-d H:i:s"));
                                    $s = 0;
                                    for ($nn = 0; $s < count($arr_times); $s++) {
                                        $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                        $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                        if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                            $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                        }
                                        $to_time = strtotime($to_time);
                                        $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                        $down_rate = $arr_times[$s]["down_rate"];
                                        if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                            $speed .= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                            $sp .= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                                if ($nn == 0) {
                                    $speed .= round($down_sp / $div_down_speed / $sp_down_count_online) . "K";
                                    $sp .= round($down_sp / $div_down_speed / $sp_down_count_online) * 1000;
                                }
                            }
                        }
                    }
                    if ($profiles["qouta_expire_service"] == "-1" && $profiles["q"] != 0 && $profiles["q"] < $all_quota_uasge) {
                        $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2000;
                        $res = exec($send);
                        mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                        $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s") . "')") or exit(mysqli_error($connect));
                    }
                    if ($profiles["daily_expire_service"] != 0 && 0 < $profiles["daily_profile_qouta"] && $profiles["daily_profile_qouta"] < $all_daily_uasge) {
                        if ($profiles["qouta_expire_service"] != 0 && $profiles["q"] != 0 && $profiles["q"] < $all_quota_uasge) {
                            if (!empty($val_address_exp_list) && $val_address_exp_list != $address_list_name) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_list . "',`address_val`='1' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2017;
                                    $res = exec($dis);
                                    $arr_dis[] = $fet_online["radacctid"];
                                } else {
                                    $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2021;
                                    $res = exec($send);
                                }
                            }
                            if ($fet_online["framed_pool"] != $profiles["q_pool"]) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["q_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2028;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            }
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0; $s < count($arr_times_exp); $s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_exp[$s]["up_rate"];
                                    $down_rate = $arr_times_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                                $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                            $nn = 0;
                            if (is_array($arr_times_exp_percent)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                for ($s = 0; $s < count($arr_times_exp_percent); $s++) {
                                    $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                                    $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                                    if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                        $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                    }
                                    $tobq_time = strtotime($tobq_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $percent_bq = $arr_times_exp_percent[$s]["percent"];
                                    $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                                    if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                        echo $sel_day;
                                        $today_work_bq = arraySearch($sel_day, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $val_percent = $percent_bq;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (0 < $nn) {
                                if ($val_percent != $fet_online["percent_q"]) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                    }
                                }
                            } else {
                                if ($val_percent != 100) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                    }
                                }
                            }
                        } else {
                            if (!empty($val_address_exp_daily_list) && $val_address_exp_daily_list != $address_list_name) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_daily_list . "',`address_val` = '2' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2237;
                                    $res = exec($dis);
                                    $arr_dis[] = $fet_online["radacctid"];
                                } else {
                                    $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_daily_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2241;
                                    $res = exec($send);
                                }
                            }
                            if ($fet_online["framed_pool"] != $profiles["d_pool"]) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["d_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2261;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            }
                            if (is_array($arr_times_d_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0; $s < count($arr_times_d_exp); $s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_d_exp[$s]["up_rate"];
                                    $down_rate = $arr_times_d_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0) {
                                $speed = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_da_sp;
                                $sp = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                        }
                    } else {
                        if ($profiles["qouta_expire_service"] != 0 && $profiles["q"] != 0 && $profiles["q"] < $all_quota_uasge) {
                            if (!empty($val_address_exp_list) && $val_address_exp_list != $address_list_name) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_list . "',`address_val`='1' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2305;
                                    $res = exec($dis);
                                    $arr_dis[] = $fet_online["radacctid"];
                                } else {
                                    $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2309;
                                    $res = exec($send);
                                }
                            }
                            if ($fet_online["framed_pool"] != $profiles["q_pool"]) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["q_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2316;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            }
                            if (is_array($arr_times_exp)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                $s = 0;
                                for ($nn = 0; $s < count($arr_times_exp); $s++) {
                                    $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                                    $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                                    if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                        $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                    }
                                    $to_time = strtotime($to_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $up_rate = $arr_times_exp[$s]["up_rate"];
                                    $down_rate = $arr_times_exp[$s]["down_rate"];
                                    if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                        $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                                        $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                            if ($nn == 0 && $profiles["qouta_expire_service"] == "-1") {
                                $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                                $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                            }
                        } else {
                            if ($fet_online["address_val"] != 3 || $address_list_name != $val_address_list) {
                                $update_nothing_address_list = mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_list . "',`address_val`='3' WHERE `username`='" . $fet_online["username"] . "' AND `attribute` = 'Cleartext-Password'") or exit(mysqli_error($connect));
                                if ($address_list_name != $val_address_list) {
                                    if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2367;
                                        $res = exec($dis);
                                        $arr_dis[] = $fet_online["radacctid"];
                                    } else {
                                        $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2371;
                                        $res = exec($send);
                                    }
                                }
                            }
                            if ($fet_online["framed_pool"] != $profiles["ad_pool"]) {
                                mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $profiles["ad_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2379;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            }
                            $val_speed = 0;
                            if (is_array($arr_m_speed) && $profiles["q"] != 0) {
                                $s = 0;
                                for ($nn = 0; $s < count($arr_m_speed); $s++) {
                                    $from_val = $arr_m_speed[$s]["from_val"];
                                    $to_val = $arr_m_speed[$s]["to_val"];
                                    $up_rval = $arr_m_speed[$s]["up_rval"];
                                    $down_rval = $arr_m_speed[$s]["down_rval"];
                                    $sel_day_val = $arr_m_speed[$s]["sel_day_val"];
                                    $perc_q = $all_quota_uasge / $profiles["q"] * 100;
                                    if ($from_val <= $perc_q && $perc_q <= $to_val) {
                                        echo $sel_day_val;
                                        $today_work_bq = arraySearch($sel_day_val, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $speed = round($up_rval / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rval / $div_down_speed / $sp_down_count_online) . "K";
                                            $sp = round($up_rval / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rval / $div_down_speed / $sp_down_count_online) * 1000;
                                            $val_speed = 1;
                                            break;
                                        }
                                    }
                                }
                            }
                            if ((empty($speed) || !$speed) && $val_speed == 0) {
                                if (is_array($arr_times)) {
                                    $clock = strtotime(date("Y-m-d H:i:s"));
                                    $s = 0;
                                    for ($nn = 0; $s < count($arr_times); $s++) {
                                        $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                        $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                        if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                            $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                        }
                                        $to_time = strtotime($to_time);
                                        $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                        $up_rate = $arr_times[$s]["up_rate"];
                                        $down_rate = $arr_times[$s]["down_rate"];
                                        if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                            $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                            $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                                if ($nn == 0) {
                                    $speed = round($up_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_now;
                                    $sp = round($up_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_sp / $div_down_speed / $sp_down_count_online) * 1000;
                                }
                            }
                            $nn = 0;
                            if (is_array($arr_times_percent)) {
                                $clock = strtotime(date("Y-m-d H:i:s"));
                                for ($s = 0; $s < count($arr_times_percent); $s++) {
                                    $frombq_time = strtotime(date("Y-m-d ") . $arr_times_percent[$s]["frombq_time"] . ":00");
                                    $tobq_time = date("Y-m-d ") . $arr_times_percent[$s]["tobq_time"] . ":00";
                                    if (is_new($arr_times_percent[$s]["frombq_time"], $arr_times_percent[$s]["tobq_time"]) == 1) {
                                        $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                    }
                                    $tobq_time = strtotime($tobq_time);
                                    $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                    $percent_bq = $arr_times_percent[$s]["percent"];
                                    $sel_day = $arr_times_percent[$s]["sel_day"];
                                    if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                        $today_work_bq = arraySearch($sel_day, date("D"));
                                        if (!empty($today_work_bq)) {
                                            $val_percent = $percent_bq;
                                            $nn++;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (0 < $nn) {
                                if ($val_percent != $fet_online["percent_q"]) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                    }
                                }
                            } else {
                                if ($val_percent != 100) {
                                    $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                    if (0 < mysqli_affected_rows($connect)) {
                                        $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if ($fet_online["framedprotocol"] == "PPP") {
                if ($fet_online["up_by"] == 1) {
                    if ($fet_online["l_speed"] != $speed && $fet_online["stop_u_speed"] == 0) {
                        $send = "echo  " . "\"" . "user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',mikrotik-rate-limit='" . $speed . "'" . "\"" . " |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2620;
                        $res = exec($send);
                        mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'") or exit(mysqli_error($connect));
                    }
                } else {
                    if ($fet_online["api_is_en"] == 1) {
                        if ($m != @end(@array_keys($arr_nas[$fet_online["nasname"]]["details"]))) {
                            $m = 0;
                        } else {
                            $m = @end(@array_keys($arr_nas[$fet_online["nasname"]]["details"])) + 1;
                        }
                        if ($fet_online["l_speed"] != $speed) {
                            if ($arr_nas[$fet_online["nasname"]]["nasname"] != $fet_online["nasname"]) {
                                $arr_nas[$fet_online["nasname"]]["nasname"] = $fet_online["nasname"];
                                $arr_nas[$fet_online["nasname"]]["api_username"] = $fet_online["api_username"];
                                $arr_nas[$fet_online["nasname"]]["api_password"] = $fet_online["api_password"];
                            }
                            $arr_nas[$fet_online["nasname"]]["api_port"] = $fet_online["api_port"];
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["username"] = $fet_online["username"];
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["speed"] = $speed;
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["sp"] = $sp;
                            mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                        }
                    }
                }
            } else {
                if ($fet_online["l_speed"] != $speed && $fet_online["stop_u_speed"] == 0) {
                    $send = "echo  " . "\"" . "user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',mikrotik-rate-limit='" . $speed . "'" . "\"" . " |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 2652;
                    mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                    $res = exec($send);
                }
            }
        } else {
            $fet_online["all_up"] = $fet_online["up_cq"] + $fet_online["acctup"];
            $fet_online["all_down"] = $fet_online["down_cq"] + $fet_online["acctdown"];
            $fet_online["all_time"] = $fet_online["all_tc"] + $fet_online["accttime"];
            $exp_first_login = $fet_online["exp_first_login"];
            $val_time_exp = $fet_online["val_time_exp"];
            $time_cards_exp = $fet_online["time_cards_exp"];
            $exp_quota = $fet_online["exp_quota"];
            if ($exp_first_login == 1 && !$fet_online["exp_user"]) {
                $sel_first_login = mysqli_query($connect, "SELECT `acctstarttime` FROM `radacct` WHERE `username`='" . $fet_online["username"] . "' ORDER BY `radacctid` ASC LIMIT 1");
                $fet_f_login = mysqli_fetch_assoc($sel_first_login);
                $time_first_login = strtotime($fet_f_login["acctstarttime"]);
                if ($time_cards_exp == 1) {
                    $t_exp_2 = $time_first_login + 60 * 60 * 24 * $val_time_exp;
                } else {
                    if ($time_cards_exp == 2) {
                        $t_exp_2 = $time_first_login + 60 * 60 * $val_time_exp;
                    } else {
                        if ($time_cards_exp == 3) {
                            $t_exp_2 = $time_first_login + 60 * $val_time_exp;
                        }
                    }
                }
                if ($t_exp_2 < time()) {
                    $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2689;
                    $res = exec($dis);
                    $arr_dis[] = $fet_online["radacctid"];
                    mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                    $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s") . "','1','" . $fet_online["id_card2"] . "')") or exit(mysqli_error($connect));
                    $ad_list_empty = 1;
                } else {
                    $ad_list_empty = 0;
                }
            } else {
                $ad_list_empty = 0;
            }
            if ($ad_list_empty == 1) {
                $address_list_name = "";
                $val_address_list = "";
                $val_address_exp_list = "";
                $val_address_exp_daily_list = "";
            } else {
                $address_list_name = $fet_online["address_list_name"];
                $val_address_list = $fet_online["val_address_list"];
                $val_address_exp_list = $fet_online["val_address_exp_list"];
                $val_address_exp_daily_list = $fet_online["val_address_exp_daily_list"];
            }
            if ($fet_set["lock_by_ip_card"] == 1 && !$fet_online["ip_address"] && $fet_online["simul_sess"] == 1) {
                $insert = @mysqli_query($connect, "INSERT INTO `radreply` (`username`,`attribute`,`op`,`value`) VALUES ('" . $fet_online["username"] . "','Framed-IP-Address',':=','" . $fet_online["framedipaddress"] . "')");
            }
            if ($fet_set["lock_by_mac_card"] == 1 && !$fet_online["mac_address"] && $fet_online["mac_address"] != $fet_online["username"] && $fet_online["simul_sess"] == 1) {
                $delete = mysqli_query($connect, "DELETE FROM radcheck WHERE username='" . $fet_online["username"] . "' AND attribute='Calling-Station-Id'") or exit(mysqli_error($connect));
                $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Calling-Station-Id','==','" . $fet_online["callingstationid"] . "','1','" . $fet_online["id_card2"] . "')");
            }
            $div_down_speed = $rad_count_sess;
            $div_up_speed = $rad_count_sess;
            if ($fet_online["hours_min"] == 0) {
                $t_online_prof = $fet_online["online_time"] * 60;
            } else {
                $t_online_prof = $fet_online["online_time"] * 60 * 60;
            }
            if ($fet_online["daily_hours_min"] == 0) {
                $d_online_prof = $fet_online["daily_online_time"] * 60;
            } else {
                $d_online_prof = $fet_online["daily_online_time"] * 60 * 60;
            }
            if (0 < $t_online_prof && $t_online_prof <= $fet_online["all_time"] || 0 < $d_online_prof && $d_online_prof <= $fet_online["daily_time"] || $fet_set["block_login"] != 0 || $fet_online["exp_date"] && $fet_online["exp_date"] < time()) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2749;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            if ($fet_online["val_date"] == 1) {
                $time_col = $fet_online["date_end_card"] * 24 * 60 * 60;
            } else {
                if ($fet_online["val_date"] == 2) {
                    $time_col = $fet_online["date_end_card"] * 60 * 60;
                } else {
                    $time_col = $fet_online["date_end_card"] * 60;
                }
            }
            if (0 < $fet_online["all_time"] && $time_col <= $fet_online["all_time"] && $fet_online["per_second"] == 1 && $fet_online["at_the_first_login"] == 0) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2774;
                $res = exec($dis);
                if (!$fet_online["exp_user"]) {
                    $arr_end_cards[] = $fet_online["username"];
                    mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                    $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s") . "','1','" . $fet_online["id_card2"] . "')") or exit(mysqli_error($connect));
                }
            }
            if ($fet_online["per_second"] == 0 && $fet_online["at_the_first_login"] == 1 && !$fet_online["exp_user"]) {
                $date_end = time() + $time_col;
                mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s", $date_end) . "','1','" . $fet_online["id_card2"] . "')") or exit(mysqli_error($connect));
            }
            if ($fet_online["exp_user"] && $fet_online["exp_user"] < time()) {
                $arr_end_cards[] = $fet_online["username"];
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 2793;
                $res = exec($dis);
                $arr_dis[] = $fet_online["radacctid"];
            }
            $percent = $fet_online["percent"];
            $percent_exp = $fet_online["percent_exp"];
            $percent_daily_exp = $fet_online["percent_daily_exp"];
            $percent_serv_exp = $fet_online["percent_serv_exp"];
            $value_now = explode(" ", $fet_online["value_now"]);
            if (is_array($value_now) && $value_now[0]) {
                $val_now = explode("/", $value_now[0]);
                $up_sp = str_ireplace("k", "", $val_now[0]) * $percent / 100;
                $down_sp = str_ireplace("k", "", $val_now[1]) * $percent / 100;
                $val_now2 = explode("/", $value_now[1]);
                $up_sp2 = str_ireplace("k", "", $val_now2[0]) * $percent / 100;
                $down_sp2 = str_ireplace("k", "", $val_now2[1]) * $percent / 100;
                $val_now3 = explode("/", $value_now[2]);
                $up_sp3 = str_ireplace("k", "", $val_now3[0]) * $percent / 100;
                $down_sp3 = str_ireplace("k", "", $val_now3[1]) * $percent / 100;
                $val_now4 = explode("/", $value_now[3]);
                $up_t4 = round($val_now4[0] * $percent / 100);
                $down_t4 = round($val_now4[1] * $percent / 100);
                $val_burst_now = round($up_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4 . "/" . $down_t4 . " " . $value_now[4];
            }
            $value_exp = explode(" ", $fet_online["value_exp"]);
            if (is_array($value_exp) && $value_exp[0]) {
                $value_exp2 = explode("/", $value_exp[0]);
                $up_exp_sp = str_ireplace("k", "", $value_exp2[0]) * $percent_exp / 100;
                $down_exp_sp = str_ireplace("k", "", $value_exp2[1]) * $percent_exp / 100;
                $val_exp2 = explode("/", $value_exp[1]);
                $up_exp2 = str_ireplace("k", "", $val_exp2[0]) * $percent / 100;
                $down_exp2 = str_ireplace("k", "", $val_exp2[1]) * $percent / 100;
                $val_exp3 = explode("/", $value_exp[2]);
                $up_exp3 = str_ireplace("k", "", $val_exp3[0]) * $percent / 100;
                $down_exp3 = str_ireplace("k", "", $val_exp3[1]) * $percent / 100;
                $val_exp4 = explode("/", $value_exp[3]);
                $up_t4_exp = round($val_exp4[0] * $percent / 100);
                $down_t4_exp = round($val_exp4[1] * $percent / 100);
                $val_burst_exp_sp = round($up_exp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp . "/" . $down_t4_exp . " " . $value_exp[4];
            }
            $value_exp_daily = explode(" ", $fet_online["value_exp_daily"]);
            if (is_array($value_exp_daily) && $value_exp_daily[0]) {
                $value_exp_daily2 = explode("/", $value_exp_daily[0]);
                $up_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[0]) * $percent_daily_exp / 100;
                $down_exp_da_sp = str_ireplace("k", "", $value_exp_daily2[1]) * $percent_daily_exp / 100;
                $val_exp_daily2 = explode("/", $value_exp_daily[1]);
                $up_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[0]) * $percent / 100;
                $down_exp_da_sp2 = str_ireplace("k", "", $val_exp_daily2[1]) * $percent / 100;
                $val_exp_daily3 = explode("/", $value_exp_daily[2]);
                $up_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[0]) * $percent / 100;
                $down_exp_da_sp3 = str_ireplace("k", "", $val_exp_daily3[1]) * $percent / 100;
                $val_exp_daily4 = explode("/", $value_exp_daily[3]);
                $up_t4_exp_da_sp = round($val_exp_daily4[0] * $percent / 100);
                $down_t4_exp_da_sp = round($val_exp_daily4[1] * $percent / 100);
                $val_burst_exp_da_sp = round($up_exp_da_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_da_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_da_sp . "/" . $down_t4_exp_da_sp . " " . $value_exp_daily[4];
            }
            $value_exp_serv = explode(" ", $fet_online["value_exp_serv"]);
            if (is_array($value_exp_serv) && $value_exp_serv[0]) {
                $value_exp_serv2 = explode("/", $value_exp_serv[0]);
                $up_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[0]) * $percent_serv_exp / 100;
                $down_exp_serv_sp = str_ireplace("k", "", $value_exp_serv2[1]) * $percent_serv_exp / 100;
                $val_exp_serv2 = explode("/", $value_exp_serv[1]);
                $up_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[0]) * $percent / 100;
                $down_exp_serv_sp2 = str_ireplace("k", "", $val_exp_serv2[1]) * $percent / 100;
                $val_exp_serv3 = explode("/", $value_exp_serv[2]);
                $up_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[0]) * $percent / 100;
                $down_exp_serv_sp3 = str_ireplace("k", "", $val_exp_serv3[1]) * $percent / 100;
                $val_exp_serv4 = explode("/", $value_exp_serv[3]);
                $up_t4_exp_serv_sp = round($val_exp_serv4[0] * $percent / 100);
                $down_t4_exp_serv_sp = round($val_exp_serv4[1] * $percent / 100);
                $val_burst_exp_serv_sp = round($up_exp_serv_sp2 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp2 / $div_down_speed / $sp_down_count_online) . "K" . " " . round($up_exp_serv_sp3 / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_serv_sp3 / $div_down_speed / $sp_down_count_online) . "K" . " " . $up_t4_exp_serv_sp . "/" . $down_t4_exp_serv_sp . " " . $value_exp_serv[4];
            }
            $arr_times = unserialize($fet_online["bandwidth_time"]);
            $arr_times_exp = unserialize($fet_online["band_exp"]);
            $arr_times_d_exp = unserialize($fet_online["band_daily_exp"]);
            $arr_times_percent = unserialize($fet_online["bandwidth_percent"]);
            $arr_times_exp_percent = unserialize($fet_online["band_exp_percent"]);
            $arr_times_d_exp_percent = unserialize($fet_online["band_daily_exp_percent"]);
            $all_daily_uasge = $fet_online["down_today"] + $fet_online["up_today"];
            $all_quota_uasge = $fet_online["all_up"] + $fet_online["all_down"];
            if ($fet_online["daily_expire_service"] != 0 && 0 < $fet_online["up_daily_qouta"] && $fet_online["up_daily_qouta"] < $fet_online["up_today"] && $fet_online["daily_profile_qouta"] == 0) {
                if ($fet_online["qouta_expire_service"] != 0 && 0 < $fet_online["up_qouta"] && $fet_online["up_qouta"] < $fet_online["all_up"] && $fet_online["av_qouta"] == 0) {
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_exp[$s]["up_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/";
                        $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if (is_array($arr_times_d_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_d_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_d_exp[$s]["up_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) . "K/";
                        $sp = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                    }
                    $nn = 0;
                    if (is_array($arr_times_d_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_d_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                }
            } else {
                if ($fet_online["qouta_expire_service"] != 0 && 0 < $fet_online["up_qouta"] && $fet_online["up_qouta"] < $fet_online["all_up"] && $fet_online["av_qouta"] == 0) {
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_exp[$s]["up_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/";
                        $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if ($fet_online["av_qouta"] == 0 && 0 < $fet_online["up_qouta"]) {
                        if (is_array($arr_times)) {
                            $clock = strtotime(date("Y-m-d H:i:s"));
                            $s = 0;
                            for ($nn = 0; $s < count($arr_times); $s++) {
                                $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                    $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                }
                                $to_time = strtotime($to_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $up_rate = $arr_times[$s]["up_rate"];
                                if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                    $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/";
                                    $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                                    $nn++;
                                    break;
                                }
                            }
                        }
                        if ($nn == 0) {
                            $speed = round($up_sp / $div_up_speed / $sp_up_count_online) . "K/";
                            $sp = round($up_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/";
                            echo "hgfhgfh";
                        }
                        $nn = 0;
                        if (is_array($arr_times_percent)) {
                            $clock = strtotime(date("Y-m-d H:i:s"));
                            for ($s = 0; $s < count($arr_times_percent); $s++) {
                                $frombq_time = strtotime(date("Y-m-d ") . $arr_times_percent[$s]["frombq_time"] . ":00");
                                $tobq_time = date("Y-m-d ") . $arr_times_percent[$s]["tobq_time"] . ":00";
                                if (is_new($arr_times_percent[$s]["frombq_time"], $arr_times_percent[$s]["tobq_time"]) == 1) {
                                    $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                }
                                $tobq_time = strtotime($tobq_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $percent_bq = $arr_times_percent[$s]["percent"];
                                $sel_day = $arr_times_percent[$s]["sel_day"];
                                if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                    echo $sel_day;
                                    $today_work_bq = arraySearch($sel_day, date("D"));
                                    if (!empty($today_work_bq)) {
                                        $val_percent = $percent_bq;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                        }
                        if (0 < $nn) {
                            if ($val_percent != $fet_online["percent_q"]) {
                                $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                if (0 < mysqli_affected_rows($connect)) {
                                    $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                }
                            }
                        } else {
                            if ($val_percent != 100) {
                                $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                if (0 < mysqli_affected_rows($connect)) {
                                    $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                }
                            }
                        }
                    }
                }
            }
            if ($fet_online["daily_expire_service"] != 0 && 0 < $fet_online["down_daily_qouta"] && $fet_online["down_daily_qouta"] < $fet_online["down_today"] && $fet_online["daily_profile_qouta"] == 0) {
                if ($fet_online["qouta_expire_service"] != 0 && 0 < $fet_online["down_qouta"] && $fet_online["down_qouta"] < $fet_online["all_down"] && $fet_online["av_qouta"] == 0) {
                    $s = 0;
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $down_rate = $arr_times_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed .= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp .= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed .= round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K";
                        $sp .= round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if (is_array($arr_times_d_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_d_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $down_rate = $arr_times_d_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed .= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp .= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed .= round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) . "K";
                        $sp .= round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_d_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_d_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                }
            } else {
                if ($fet_online["qouta_expire_service"] != 0 && 0 < $fet_online["down_qouta"] && $fet_online["down_qouta"] < $fet_online["all_down"] && $fet_online["av_qouta"] == 0) {
                    $s = 0;
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $down_rate = $arr_times_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed .= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp .= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        if (!isset($down_exp_sp)) {
                            $down_exp_sp = 0;
                        }
                        $speed .= round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K";
                        $sp .= round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if ($fet_online["av_qouta"] == 0 && 0 < $fet_online["down_qouta"]) {
                        if (is_array($arr_times)) {
                            $clock = strtotime(date("Y-m-d H:i:s"));
                            $s = 0;
                            for ($nn = 0; $s < count($arr_times); $s++) {
                                $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                    $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                }
                                $to_time = strtotime($to_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $down_rate = $arr_times[$s]["down_rate"];
                                if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                    $speed .= round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                    $sp .= round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                        if ($nn == 0) {
                            $speed .= round($down_sp / $div_down_speed / $sp_down_count_online) . "K";
                            $sp .= round($down_sp / $div_down_speed / $sp_down_count_online) * 1000;
                        }
                        $nn = 0;
                        if (is_array($arr_times_percent)) {
                            $clock = strtotime(date("Y-m-d H:i:s"));
                            for ($s = 0; $s < count($arr_times_percent); $s++) {
                                $frombq_time = strtotime(date("Y-m-d ") . $arr_times_percent[$s]["frombq_time"] . ":00");
                                $tobq_time = date("Y-m-d ") . $arr_times_percent[$s]["tobq_time"] . ":00";
                                if (is_new($arr_times_percent[$s]["frombq_time"], $arr_times_percent[$s]["tobq_time"]) == 1) {
                                    $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                                }
                                $tobq_time = strtotime($tobq_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $percent_bq = $arr_times_percent[$s]["percent"];
                                $sel_day = $arr_times_percent[$s]["sel_day"];
                                if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                    echo $sel_day;
                                    $today_work_bq = arraySearch($sel_day, date("D"));
                                    if (!empty($today_work_bq)) {
                                        $val_percent = $percent_bq;
                                        $nn++;
                                        break;
                                    }
                                }
                            }
                        }
                        if (0 < $nn) {
                            if ($val_percent != $fet_online["percent_q"]) {
                                $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                                $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                if (0 < mysqli_affected_rows($connect)) {
                                    $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                }
                            }
                        } else {
                            if ($val_percent != 100) {
                                $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                                $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                                if (0 < mysqli_affected_rows($connect)) {
                                    $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                }
                            }
                        }
                    }
                }
            }
            if (($fet_online["qouta_expire_service"] == "-1" || $exp_quota == 1) && $fet_online["av_qouta"] != 0 && $fet_online["av_qouta"] < $all_quota_uasge) {
                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4460;
                $res = exec($dis);
                $arr_end_cards[] = $fet_online["username"];
                mysqli_query($connect, "DELETE FROM `radcheck` WHERE `attribute` = 'Expiration' AND `username`='" . $fet_online["username"] . "'");
                $insert = mysqli_query($connect, "INSERT INTO `radcheck` (`username`,`attribute`,`op`,`value`,`is_card`,`id_card`) VALUES ('" . $fet_online["username"] . "','Expiration',':=','" . date("d M Y H:i:s") . "','1','" . $fet_online["id_card2"] . "')") or exit(mysqli_error($connect));
                continue;
            }
            if ($fet_online["daily_expire_service"] != 0 && 0 < $fet_online["daily_profile_qouta"] && $fet_online["daily_profile_qouta"] < $all_daily_uasge) {
                if ($fet_online["qouta_expire_service"] != 0 && $fet_online["av_qouta"] != 0 && $fet_online["av_qouta"] < $all_quota_uasge) {
                    if (!empty($val_address_exp_list) && $val_address_exp_list != $address_list_name) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_list . "',`address_val` = '1' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4475;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 4479;
                            $res = exec($send);
                        }
                    }
                    if ($fet_online["framed_pool"] != $fet_online["q_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["q_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4486;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_exp[$s]["up_rate"];
                            $down_rate = $arr_times_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                        $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if (!empty($val_address_exp_daily_list) && $val_address_exp_daily_list != $address_list_name) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_daily_list . "',`address_val`='2' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4689;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_daily_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 4693;
                            $res = exec($send);
                        }
                    }
                    if ($fet_online["framed_pool"] != $fet_online["d_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["d_pool"] . "',`address_val`='2' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4712;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (is_array($arr_times_d_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_d_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_d_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_d_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_d_exp[$s]["from_time"], $arr_times_d_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_d_exp[$s]["up_rate"];
                            $down_rate = $arr_times_d_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_da_sp;
                        $sp = round($up_exp_da_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_da_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_d_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_d_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_d_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_d_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_d_exp_percent[$s]["frombq_time"], $arr_times_d_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_d_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_d_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                }
            } else {
                if ($fet_online["qouta_expire_service"] != 0 && $fet_online["av_qouta"] != 0 && $fet_online["av_qouta"] < $all_quota_uasge) {
                    if (!empty($val_address_exp_list) && $val_address_exp_list != $address_list_name) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_exp_list . "',`address_val`='1' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                            $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4912;
                            $res = exec($dis);
                            $arr_dis[] = $fet_online["radacctid"];
                        } else {
                            $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_exp_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 4916;
                            $res = exec($send);
                        }
                    }
                    if ($fet_online["framed_pool"] != $fet_online["q_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["q_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 4923;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (is_array($arr_times_exp)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        $s = 0;
                        for ($nn = 0; $s < count($arr_times_exp); $s++) {
                            $from_time = strtotime(date("Y-m-d ") . $arr_times_exp[$s]["from_time"] . ":00");
                            $to_time = date("Y-m-d ") . $arr_times_exp[$s]["to_time"] . ":00";
                            if (is_new($arr_times_exp[$s]["from_time"], $arr_times_exp[$s]["to_time"]) == 1) {
                                $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                            }
                            $to_time = strtotime($to_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $up_rate = $arr_times_exp[$s]["up_rate"];
                            $down_rate = $arr_times_exp[$s]["down_rate"];
                            if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                $nn++;
                                break;
                            }
                        }
                    }
                    if ($nn == 0) {
                        $speed = round($up_exp_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_exp_sp;
                        $sp = round($up_exp_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_exp_sp / $div_down_speed / $sp_down_count_online) * 1000;
                    }
                    $nn = 0;
                    if (is_array($arr_times_exp_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_exp_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_exp_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_exp_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_exp_percent[$s]["frombq_time"], $arr_times_exp_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_exp_percent[$s]["percent"];
                            $sel_day = $arr_times_exp_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                } else {
                    if ($fet_online["address_val"] != 3 || $address_list_name != $val_address_list) {
                        $update_nothing_address_list = mysqli_query($connect, "UPDATE `radcheck` SET `address_list_name` = '" . $val_address_list . "',`address_val`='3' WHERE `username`='" . $fet_online["username"] . "' AND `attribute` = 'Cleartext-Password'") or exit(mysqli_error($connect));
                        if ($address_list_name != $val_address_list) {
                            if ($fet_online["up_by"] != 1 || $fet_online["framedprotocol"] != "PPP") {
                                $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 5128;
                                $res = exec($dis);
                                $arr_dis[] = $fet_online["radacctid"];
                            } else {
                                $send = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',Mikrotik-Address-List='" . $val_address_list . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 5132;
                                $res = exec($send);
                            }
                        }
                    }
                    if ($fet_online["framed_pool"] != $fet_online["ad_pool"]) {
                        mysqli_query($connect, "UPDATE `radcheck` SET `framed_pool` = '" . $fet_online["ad_pool"] . "' WHERE `radcheck`.`username` ='" . $fet_online["username"] . "' AND `radcheck`.`attribute`='Cleartext-Password'");
                        $dis = "echo  user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "' |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " disconnect " . $fet_online["secret"] . " -t 0.2 ;" . 5140;
                        $res = exec($dis);
                        $arr_dis[] = $fet_online["radacctid"];
                    }
                    if (!$speed) {
                        $s = 0;
                        $nn = 0;
                        if (is_array($arr_times)) {
                            for ($clock = strtotime(date("Y-m-d H:i:s")); $s < count($arr_times); $s++) {
                                $from_time = strtotime(date("Y-m-d ") . $arr_times[$s]["from_time"] . ":00");
                                $to_time = date("Y-m-d ") . $arr_times[$s]["to_time"] . ":00";
                                if (is_new($arr_times[$s]["from_time"], $arr_times[$s]["to_time"]) == 1) {
                                    $to_time = date("Y-m-d H:i:s", strtotime($to_time . " +1 days"));
                                }
                                $to_time = strtotime($to_time);
                                $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                                $up_rate = $arr_times[$s]["up_rate"];
                                $down_rate = $arr_times[$s]["down_rate"];
                                if ($from_time <= $clock && $clock < $to_time || $from_time <= $clock2 && $clock2 < $to_time) {
                                    $speed = round($up_rate / $div_up_speed / $sp_up_count_online) . "K/" . round($down_rate / $div_down_speed / $sp_down_count_online) . "K";
                                    $sp = round($up_rate / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_rate / $div_down_speed / $sp_down_count_online) * 1000;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                        if ($nn == 0) {
                            $speed = round($up_sp / $div_up_speed / $sp_up_count_online) . "K/" . round($down_sp / $div_down_speed / $sp_down_count_online) . "K " . $val_burst_now;
                            $sp = round($up_sp / $div_up_speed / $sp_up_count_online) * 1000 . "/" . round($down_sp / $div_down_speed / $sp_down_count_online) * 1000;
                        }
                    }
                    $nn = 0;
                    if (is_array($arr_times_percent)) {
                        $clock = strtotime(date("Y-m-d H:i:s"));
                        for ($s = 0; $s < count($arr_times_percent); $s++) {
                            $frombq_time = strtotime(date("Y-m-d ") . $arr_times_percent[$s]["frombq_time"] . ":00");
                            $tobq_time = date("Y-m-d ") . $arr_times_percent[$s]["tobq_time"] . ":00";
                            if (is_new($arr_times_percent[$s]["frombq_time"], $arr_times_percent[$s]["tobq_time"]) == 1) {
                                $tobq_time = date("Y-m-d H:i:s", strtotime($tobq_time . " +1 days"));
                            }
                            $tobq_time = strtotime($tobq_time);
                            $clock2 = strtotime(date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . " +1 days")));
                            $percent_bq = $arr_times_percent[$s]["percent"];
                            $sel_day = $arr_times_percent[$s]["sel_day"];
                            if ($frombq_time <= $clock && $clock < $tobq_time || $frombq_time <= $clock2 && $clock2 < $tobq_time) {
                                echo $sel_day;
                                $today_work_bq = arraySearch($sel_day, date("D"));
                                if (!empty($today_work_bq)) {
                                    $val_percent = $percent_bq;
                                    $nn++;
                                    break;
                                }
                            }
                        }
                    }
                    if (0 < $nn) {
                        if ($val_percent != $fet_online["percent_q"]) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='" . $val_percent . "' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n acctstoptime=CURRENT_TIMESTAMP\r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstoptime`,\r\n`acctstarttime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nNULL,\r\nCURRENT_TIMESTAMP,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                                mysqli_query($connect, "UPDATE radacct SET acctstoptime = NULL WHERE acctuniqueid = '" . $fet_online["acctuniqueid"] . "'");
                            }
                        }
                    } else {
                        if ($val_percent != 100) {
                            $update_user = mysqli_query($connect, "UPDATE `radusergroup` SET `percent_now` ='100' WHERE `username` ='" . $fet_online["username"] . "'");
                            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                            if (0 < mysqli_affected_rows($connect)) {
                                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                            }
                        }
                    }
                }
            }
            if ($fet_online["framedprotocol"] == "PPP") {
                if ($fet_online["up_by"] == 1) {
                    if ($fet_online["l_speed"] != $speed && $fet_online["stop_u_speed"] == 0) {
                        $send = "echo  " . "\"" . "user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',mikrotik-rate-limit='" . $speed . "'" . "\"" . " |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 5351;
                        $res = exec($send);
                        echo $send;
                        mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                    }
                } else {
                    if ($fet_online["api_is_en"] == 1) {
                        if ($m != end(array_keys($arr_nas[$fet_online["nasname"]]["details"]))) {
                            $m = 0;
                        } else {
                            $m = end(array_keys($arr_nas[$fet_online["nasname"]]["details"])) + 1;
                        }
                        if ($fet_online["l_speed"] != $speed) {
                            if ($arr_nas[$fet_online["nasname"]]["nasname"] != $fet_online["nasname"]) {
                                $arr_nas[$fet_online["nasname"]]["nasname"] = $fet_online["nasname"];
                                $arr_nas[$fet_online["nasname"]]["api_username"] = $fet_online["api_username"];
                                $arr_nas[$fet_online["nasname"]]["api_password"] = $fet_online["api_password"];
                                $arr_nas[$fet_online["nasname"]]["val_mik"] = $fet_online["val_mik"];
                            }
                            $arr_nas[$fet_online["nasname"]]["api_port"] = $fet_online["api_port"];
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["username"] = $fet_online["username"];
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["speed"] = $speed;
                            $arr_nas[$fet_online["nasname"]]["details"][$m]["sp"] = $sp;
                            mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                        }
                    }
                }
            } else {
                if ($fet_online["l_speed"] != $speed && $fet_online["stop_u_speed"] == 0) {
                    $send = "echo  " . "\"" . "user-name='" . $fet_online["username"] . "' ,Framed-IP-Address='" . $fet_online["framedipaddress"] . "',Acct-Session-Id='" . $fet_online["acctsessionid"] . "',mikrotik-rate-limit='" . $speed . "'" . "\"" . " |  /usr/bin/radclient -x  " . $fet_online["nasipaddress"] . ":" . $fet_set["port_incoming"] . " coa " . $fet_online["secret"] . " -t 0.2 ;" . 5380;
                    $res = exec($send);
                    mysqli_query($connect, "UPDATE `radusergroup` SET `last_speed`='" . $speed . "' WHERE `username`='" . $fet_online["username"] . "'");
                }
            }
        }
        $sess_day = date("d-m-Y", strtotime($fet_online["acctstarttime"]));
        $now = date("d-m-Y");
        $y = date("Y");
        if ($fet_online["daily_expire_service"] != 0 && $sess_day < $now) {
            $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`='" . date("Y-m-d 23:59:59", strtotime($fet_online["acctstarttime"])) . "',\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE  `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
            if (0 < mysqli_affected_rows($connect)) {
                $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= userinfo.updatedate LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >=`userinfo`.`updatedate`  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
            }
        }
        $speed = "";
    }
    if ($dis != "") {
        $res = exec($dis);
    }
    if ($send != "") {
        $res = exec($send);
    }
    $n = 0;
    if (is_array($arr_nas)) {
        foreach ($arr_nas as $key => $value) {
            if ($API->connect($arr_nas[$key]["nasname"], $arr_nas[$key]["api_username"], $arr_nas[$key]["api_password"], $arr_nas[$key]["api_port"], $arr_nas[$key]["val_mik"])) {
                $m = 0;
                $arr_nas[$key]["details"] = array_values($arr_nas[$key]["details"]);
                while ($m < count($arr_nas[$key]["details"])) {
                    $speed = $arr_nas[$key]["details"][$m]["speed"];
                    $sp = $arr_nas[$key]["details"][$m]["sp"];
                    $queue = $API->comm("/queue/simple/print", array("?name" => "<pppoe-" . $arr_nas[$key]["details"][$m]["username"] . ">"));
                    if ($queue[0]["max-limit"]) {
                        $queue[0]["max-limit"] = str_ireplace("k", "000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("K", "000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("m", "000000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("M", "000000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("g", "000000000", $queue[0]["max-limit"]);
                        $queue[0]["max-limit"] = str_ireplace("G", "000000000", $queue[0]["max-limit"]);
                        if ($queue[0]["max-limit"] != $sp) {
                            $edit_queue = $API->comm("/queue/simple/set", array("numbers" => $queue[0]["name"], "max-limit" => $speed, "limit-at" => $speed, "burst-limit" => "0/0", "burst-threshold" => "0/0", "burst-time" => "0s/0s"));
                            $queue2 = $API->comm("/queue/simple/print", array("?name" => "<pppoe-" . $arr_nas[$key]["details"][$m]["username"] . ">"));
                            if ($queue2[0]["max-limit"] != $sp) {
                                $queue2 = $API->comm("/queue/simple/remove", array("numbers" => $queue2[0]["name"]));
                                $add_new = $API->comm("/queue/simple/add", array("name" => "<pppoe-" . $arr_nas[$key]["details"][$m]["username"] . ">", "max-limit" => $speed, "limit-at" => $speed, "burst-limit" => "0/0", "burst-threshold" => "0/0", "burst-time" => "0s/0s"));
                            }
                        }
                    } else {
                        $add_new = $API->comm("/queue/simple/add", array("name" => "<pppoe-" . $arr_nas[$key]["details"][$m]["username"] . ">", "max-limit" => $speed, "limit-at" => $speed, "burst-limit" => "0/0", "burst-threshold" => "0/0", "burst-time" => "0s/0s"));
                    }
                    $speed = "";
                    $m++;
                }
                $API->disconnect();
            }
        }
    }
    if (0 < count($arr_dis)) {
        mysqli_query($connect, "UPDATE `radacct` SET AcctStopTime='" . date("Y-m-d H:i:s") . "', \r\n    AcctSessionTime=unix_timestamp('" . date("Y-m-d H:i:s") . "') - unix_timestamp(AcctStartTime), \r\n    AcctTerminateCause='',\r\n    AcctStopDelay = '0'\r\n\tWHERE  (AcctStopTime IS NULL) AND `radacctid` IN ('" . join("','", $arr_dis) . "')");
    }
    if (is_array($arr_end_cards) && 0 < count($arr_end_cards)) {
        mysqli_query($connect, "UPDATE `radacct` SET AcctStopTime='" . date("Y-m-d H:i:s") . "', \r\n    AcctSessionTime=unix_timestamp('" . date("Y-m-d H:i:s") . "') - unix_timestamp(AcctStartTime), \r\n    AcctTerminateCause='',\r\n    AcctStopDelay = '0'\r\n\tWHERE  (AcctStopTime IS NULL) AND `username` IN ('" . join("','", $arr_end_cards) . "')");
    }
}
$timeout_adv = time() - $fet_set["timeout_adv"];
mysqli_query($connect, "UPDATE `radacct` \r\nSET AcctStopTime='" . date("Y-m-d H:i:s") . "', \r\n    AcctSessionTime=unix_timestamp('" . date("Y-m-d H:i:s") . "') - unix_timestamp(AcctStartTime), \r\n    AcctTerminateCause='',\r\n    AcctStopDelay = '0'\r\n\tWHERE  AcctStopTime IS NULL  AND t_session < " . $timeout_adv);
$arr_u = array();
if ($fet_set["enable_sms"] == 1 && $fet_set["exp_msg"] && $fet_set["exp_rem"] != 0) {
    $url = $fet_set["api_url"];
    $day = $fet_set["exp_rem"];
    $msg = $fet_set["exp_msg"];
    $sender = $fet_set["com_name"];
    $item_updates = "";
    $rem_exp = time() + $fet_set["exp_rem"] * 60 * 60 * 24;
    if ($url && $msg) {
        $sel_all_users = mysqli_query($connect, "SELECT \r\n\t`userinfo`.`username`,\r\n\t`userinfo`.`mobile`,\r\n\t`userinfo`.`id`,\r\n\t`userinfo`.`updatedate`,\r\n\t`userinfo`.`av_qouta` *1024*1024 as `av_qouta`,\r\n\t`userinfo`.`firstname` as `first_name`,\r\n\t`userinfo`.`lastname` as `last_name`,\r\n\t`radusergroup`.`groupname` as `prof`,\r\n\t`radpass`.`value` as `pass`,\r\n\tDATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y') as `last_send_sms`,\r\n\tDATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y %H:%i:%s') as `exp`\r\n\tFROM `userinfo`\r\n\tLEFT JOIN `radcheck` ON (`radcheck`.`username` = `userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration')\r\n\tLEFT JOIN `radusergroup` ON (`radusergroup`.`username` = `userinfo`.`username`)\r\n\tLEFT JOIN `radcheck` as `radpass` ON (`radpass`.`username` = `userinfo`.`username` AND `radpass`.`attribute` ='Cleartext-Password' )\r\n\tWHERE UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'))<= " . $rem_exp . " AND UNIX_TIMESTAMP(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i')) > UNIX_TIMESTAMP()\r\n\tAND `userinfo`.`last_send_sms` != DATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y') AND `userinfo`.`renew` ='0' GROUP BY `userinfo`.`id`\r\n\t");
        if (0 < mysqli_num_rows($sel_all_users)) {
            while ($fet_all_users = mysqli_fetch_assoc($sel_all_users)) {
                if (in_array($fet_all_users["username"], $arr_u)) {
                    continue;
                }
                $arr_u[] = $fet_all_users["username"];
                $sel_q = mysqli_query($connect, "SELECT \r\nCOALESCE(up_uq,0) as up_uq,\r\nCOALESCE(down_uq,0) as down_uq,\r\nCOALESCE(acctinputoctets * (percent_q/100),0) as acctinputoctets,\r\nCOALESCE(acctoutputoctets * (percent_q/100),0)  as acctoutputoctets\r\nFROM radacct WHERE `username`='" . $fet_all_users["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(`radacct`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >= '" . $fet_all_users["updatedate"] . "' order by radacctid  DESC LIMIT 1") or exit(mysqli_error($connect));
                $fet_q = mysqli_fetch_array($sel_q);
                $up = $fet_q["up_uq"] + $fet_q["acctinputoctets"];
                $down = $fet_q["down_uq"] + $fet_q["acctoutputoctets"];
                $av_qouta = $fet_all_users["av_qouta"];
                $all_qouta = $down + $up;
                $all_qouta_string = "";
                if (!$all_qouta) {
                    $all_qouta = 0;
                }
                $all_qouta_string = formatSizeUnits($all_qouta);
                $download = formatSizeUnits($down);
                $upload = formatSizeUnits($up);
                $rem_quota = $av_qouta - $all_qouta;
                if ($rem_quota <= 0) {
                    $rem_quota = 0;
                }
                $rem_quota = formatSizeUnits($rem_quota);
                if ($av_qouta == 0) {
                    $rem_quota = $LANG["UNLIMITED"];
                }
                $url = $fet_set["api_url"];
                $first_name = $fet_all_users["first_name"];
                $last_name = $fet_all_users["last_name"];
                $user = $fet_all_users["username"];
                $pass = $fet_all_users["pass"];
                $exp = $fet_all_users["exp"];
                $prof = $fet_all_users["prof"];
                $phone = $fet_all_users["mobile"];
                $day = date("d", strtotime($exp));
                $update_user = mysqli_query($connect, "UPDATE `userinfo` SET `last_send_sms` = '" . $fet_all_users["last_send_sms"] . "' WHERE `id`='" . $fet_all_users["id"] . "'");
                sendSMS($url, $user, $pass, $exp, $prof, $day, $msg, $sender, $phone, $rem_quota, $first_name, $last_name);
            }
        }
    }
}
$check_before_renew = mysqli_query($connect, "SELECT \r\n\r\n* FROM `userinfo` WHERE `userinfo`.`renew`='1'");
if (0 < mysqli_num_rows($check_before_renew)) {
    $fet_be_renew = mysqli_fetch_assoc($check_before_renew);
    $sel_users_auto_renew = mysqli_query($connect, "SELECT \r\n\r\n`userinfo`.*,\r\n`userinfo`.`id` as `id_user`,\r\nUNIX_TIMESTAMP(STR_TO_DATE(radcheck.value, '%d %b %Y %H:%i')) as `exp`,\r\n`managers`.`id` as `id_man`,\r\n`managers`.*,\r\n`p`.*,\r\n`r8`.`value` as `val_address_list`,\r\n`p`.`id` as `id_prof`\r\nFROM `userinfo` \r\nINNER JOIN `radcheck` ON (`radcheck`.`username`=`userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration' AND UNIX_TIMESTAMP(STR_TO_DATE(radcheck.value, '%d %b %Y %H:%i')) < " . time() . ") \r\nINNER JOIN `managers` ON (`managers`.`id`=`userinfo`.`owner_id`) \r\nLEFT JOIN `radusergroup` ON(`radusergroup`.`username`=`userinfo`.`username`)\r\nINNER JOIN `profiles` `p` ON (`p`.`profile_name`=`radusergroup`.`groupname` AND `p`.`type_prof` ='0')\r\nLEFT JOIN `radgroupreply` `r8` ON (`r8`.`groupname` = `p`.`profile_name` AND `r8`.`attribute` = 'Mikrotik-Address-List')\r\nWHERE `userinfo`.`renew`='1' AND UNIX_TIMESTAMP(STR_TO_DATE(radcheck.value, '%d %b %Y %H:%i')) < UNIX_TIMESTAMP() GROUP BY `userinfo`.`id`") or exit(mysqli_error($connect));
    if (0 < mysqli_num_rows($sel_users_auto_renew)) {
        while ($fet_renew = mysqli_fetch_assoc($sel_users_auto_renew)) {
            $get_user = $fet_renew["username"];
            $check_paid = 1;
            $get_time_exp = $fet_renew["exp"];
            $comment_active = "";
            if ($get_time_exp < time() || $fet_renew["remaining_time"] == 1) {
                $get_time_exp = time();
            }
            $r_free_q = 0;
            if ($get_time_exp < time() || $fet_renew["remaining_qouta"] == 1) {
                $update_date = time();
                $is_update = 1;
                $r_free_q = 1;
                $is_new_time = 1;
            } else {
                $update_date = $fet_renew["updatedate"];
                $is_update = 0;
                $is_new_time = 0;
            }
            $sel_price_admin_for_this_profile = mysqli_query($connect, "SELECT `new_price` FROM `prices_profiles_admin` WHERE `id_profile` ='" . $fet_renew["id_prof"] . "' AND `id_manager` = '" . $fet_renew["id_man"] . "'") or exit(mysqli_error($connect));
            if (mysqli_num_rows($sel_price_admin_for_this_profile) == 0) {
                $get_price = $fet_renew["price"];
                $user_price = $fet_renew["price"];
            } else {
                $fet_price = mysqli_fetch_assoc($sel_price_admin_for_this_profile);
                $get_price = $fet_price["new_price"];
                if ($fet_set["disc_for_users"] == 1 && $fet_price["new_price"]) {
                    $user_price = $fet_price["new_price"];
                } else {
                    $user_price = $fet_renew["price"];
                }
            }
            if (0 < $fet_renew["exp_unit"]) {
                $time_now = time();
                if ($fet_renew["exp_unit_val"] == 3) {
                    $t_date = date("Y-m-d H:i", strtotime("+" . $fet_renew["exp_unit"] . " months", $get_time_exp)) . "\n";
                } else {
                    if ($fet_renew["exp_unit_val"] == 2) {
                        $t_date = date("Y-m-d H:i", strtotime("+" . $fet_renew["exp_unit"] . " days", $get_time_exp)) . "\n";
                    } else {
                        if ($fet_renew["exp_unit_val"] == 1) {
                            $t_date = date("Y-m-d H:i", strtotime("+" . $fet_renew["exp_unit"] . " hours", $get_time_exp)) . "\n";
                        } else {
                            if ($fet_renew["exp_unit_val"] == 0) {
                                $t_date = date("Y-m-d H:i", strtotime("+" . $fet_renew["exp_unit"] . " minutes", $get_time_exp)) . "\n";
                            }
                        }
                    }
                }
                $time_date = strtotime($t_date);
            } else {
                $t_date = date("Y-m-d H:i", strtotime("+1 months", $get_time_exp)) . "\n";
                $time_date = strtotime($t_date);
            }
            $amount = $get_price;
            $u_amount = $user_price;
            $amount = round($amount, 2);
            $u_amount = round($u_amount, 2);
            $set_date_exp = date("d M Y H:i:s", $time_date);
            if ($fet_renew["renew_u"] == 0) {
                if ($fet_renew["is_admin"] == 0) {
                    if ($fet_renew["credit"] < $amount) {
                        continue;
                    }
                    $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `credit` = `credit` - " . $amount . ",`usage` =`usage` + " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                } else {
                    $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `usage` =`usage` + " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                }
            } else {
                if ($fet_renew["money"] < $u_amount) {
                    continue;
                }
                $update_user_money = mysqli_query($connect, "UPDATE `userinfo` SET `money` =`money` - '" . $u_amount . "' WHERE `username` ='" . $get_user . "' ");
            }
            $v_recharge = 0;
            $sel_qouta = mysqli_query($connect, "SELECT \r\n\r\nSUM(`acctinputoctets` * (percent_q/100))/1024/1024 as `usage_upload`,\r\nSUM(`acctoutputoctets` * (percent_q/100))/1024/1024 as `usage_download` FROM `radacct`\r\nWHERE UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $fet_renew["updatedate"] . "'  AND  username='" . $get_user . "'") or exit(mysqli_error($connect));
            $fet_qouta = mysqli_fetch_array($sel_qouta);
            $all_usage = $fet_qouta["usage_download"] + $fet_qouta["usage_upload"];
            $up_qouta = $fet_renew["profile_up_qouta"];
            $down_qouta = $fet_renew["profile_down_qouta"];
            $all_qouta = $fet_renew["profile_qouta"];
            if ($fet_renew["remaining_qouta"] == 1) {
                $usage_quota = $all_qouta;
                $usage_up = $up_qouta;
                $usage_down = $down_qouta;
                $from_q = "0 B";
                $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
            } else {
                if (0 < $up_qouta) {
                    if ($fet_renew["reset_counters"] == 0) {
                        if ($fet_renew["in_up"] == 0) {
                            $usage_up = $up_qouta;
                        } else {
                            if ($fet_renew["in_up"] <= $fet_qouta["usage_upload"]) {
                                $usage_up = $up_qouta;
                            } else {
                                $usage_up = $fet_renew["in_up"] - $fet_qouta["usage_upload"] + $up_qouta;
                            }
                        }
                    } else {
                        if ($fet_renew["in_up"] == 0) {
                            $usage_up = $fet_qouta["usage_upload"] + $up_qouta;
                        } else {
                            if ($fet_renew["in_up"] <= $fet_qouta["usage_upload"]) {
                                $usage_up = $fet_qouta["usage_upload"] + $up_qouta;
                            } else {
                                $usage_up = $fet_renew["in_up"] + $up_qouta;
                            }
                        }
                    }
                } else {
                    $usage_up = 0;
                }
                if (0 < $down_qouta) {
                    if ($fet_renew["reset_counters"] == 0) {
                        if ($fet_renew["in_down"] == 0) {
                            $usage_down = $down_qouta;
                        } else {
                            if ($fet_renew["in_down"] <= $fet_qouta["usage_download"]) {
                                $usage_down = $down_qouta;
                            } else {
                                $usage_down = $fet_renew["in_down"] - $fet_qouta["usage_download"] + $down_qouta;
                            }
                        }
                    } else {
                        if ($fet_renew["in_down"] == 0) {
                            $usage_down = $fet_qouta["usage_download"] + $down_qouta;
                        } else {
                            if ($fet_renew["in_down"] <= $fet_qouta["usage_download"]) {
                                $usage_down = $fet_qouta["usage_download"] + $down_qouta;
                            } else {
                                $usage_down = $fet_renew["in_down"] + $down_qouta;
                            }
                        }
                    }
                } else {
                    $usage_down = 0;
                }
                if (0 < $all_qouta) {
                    if ($fet_renew["reset_counters"] == 0) {
                        if ($fet_renew["av_qouta"] == 0) {
                            $usage_quota = $all_qouta;
                        } else {
                            if ($fet_renew["av_qouta"] <= $all_usage) {
                                $usage_quota = $all_qouta;
                            } else {
                                $usage_quota = $fet_renew["av_qouta"] - $all_usage + $all_qouta;
                            }
                        }
                        $from_q = "0 B";
                        $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                    } else {
                        if ($fet_renew["av_qouta"] == 0) {
                            $usage_quota = $all_usage + $all_qouta;
                            $from_q = "0 B";
                            $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                        } else {
                            if ($fet_renew["av_qouta"] <= $all_usage) {
                                $usage_quota = $all_usage + $all_qouta;
                                $from_q = "0 B";
                                $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                            } else {
                                $usage_quota = $fet_renew["av_qouta"] + $all_qouta;
                                $from_q = $fet_renew["av_qouta"];
                                $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                            }
                        }
                    }
                } else {
                    $usage_quota = 0;
                    $from_q = $fet_renew["av_qouta"];
                    $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                }
            }
            if (0 < $fet_renew["profile_qouta"] || $fet_renew["profile_qouta"] == 0 && $fet_renew["profile_down_qouta"] == 0 && $fet_renew["profile_up_qouta"] == 0) {
                $usage_down = 0;
                $usage_up = 0;
                $fet_renew["profile_down_qouta"] = 0;
                $fet_renew["profile_up_qouta"] = 0;
            }
            if ($fet_renew["is_recharge"] == 1) {
                if ($fet_renew["q_recharge"] <= $usage_quota || $usage_quota == 0) {
                    if ($usage_quota != 0) {
                        $usage_quota = $usage_quota - $fet_renew["q_recharge"];
                        if ($usage_quota == 0) {
                            $usage_quota = 1;
                        }
                    }
                    $t_recharge = $fet_renew["t_recharge"] * 60 * 60;
                    $val_time = $time_date - time();
                    if ($t_recharge <= $val_time) {
                        $update_is_rechange = mysqli_query($connect, "UPDATE `userinfo` SET `is_recharge` = '0',q_recharge='0',t_recharge='0',`p_recharge`='0' WHERE `username` ='" . $get_user . "'");
                        $time_date = $time_date - $t_recharge;
                        $set_date_exp = date("d M Y H:i:s", $time_date);
                        $v_recharge = 1;
                    } else {
                        if ($fet_renew["renew_u"] == 0) {
                            if ($fet_renew["is_admin"] == 0) {
                                $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `credit` = `credit` + " . $amount . ",`usage` =`usage` - " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                            } else {
                                $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `usage` =`usage` - " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                            }
                        } else {
                            $update_user_money = mysqli_query($connect, "UPDATE `userinfo` SET `money` =`money` + '" . $u_amount . "' WHERE `username` ='" . $get_user . "' ");
                        }
                        continue;
                    }
                } else {
                    if ($fet_renew["renew_u"] == 0) {
                        if ($fet_renew["is_admin"] == 0) {
                            $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `credit` = `credit` + " . $amount . ",`usage` =`usage` - " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                        } else {
                            $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `usage` =`usage` - " . $amount . "  WHERE id='" . $fet_renew["id_man"] . "'");
                        }
                    } else {
                        $update_user_money = mysqli_query($connect, "UPDATE `userinfo` SET `money` =`money` + '" . $u_amount . "' WHERE `username` ='" . $get_user . "' ");
                    }
                    continue;
                }
            }
            $val_address_list = $fet_renew["val_address_list"];
            if ($fet_renew["renew_u"] == 0) {
                $insert_to_report = mysqli_query($connect, "INSERT INTO `a_s_man_users` \r\n(`id_user`,`id_manager`,`id_profile`,`operation`\r\n,`date_operation`,`new_price`,`status`,`id_prof_old`,`paid_unpaid`,`old_price`,\r\n`price_invoice`,`notes`) \r\nVALUES ('" . $fet_renew["id_user"] . "','" . $fet_renew["id_man"] . "','" . $fet_renew["id_prof"] . "','13'\r\n,'" . time() . "','" . $u_amount . "','1','','" . $check_paid . "','0','" . $u_amount . "','" . $comment_active . "')") or exit(mysqli_error($connect));
            } else {
                $action = 8;
                $l_text = "";
                $insert_user_rep = mysqli_query($connect, "INSERT INTO \r\n`users_actions` (`action`,`last_text`,`new_text`,`col_name`,`id_user`,`date_action`,`ip`,`browser`,`os`) \r\nVALUES ('" . $action . "','" . $l_text . "','" . $u_amount . "','10','" . $fet_renew["id_user"] . "','" . time() . "','---','---','---')") or exit(mysqli_error($connect));
            }
            if ($fet_renew["renew_u"] == 1) {
                $amount = $u_amount;
            }
            if ($fet_renew["remaining_qouta"] == 0) {
                $usage_quota = $fet_renew["profile_qouta"];
                if ($r_free_q == 1) {
                    $update_f_q = mysqli_query($connect, "UPDATE `radusergroup` SET `free_q` = '0' WHERE `username` ='" . $get_user . "'");
                }
                $update_user_usage = mysqli_query($connect, "UPDATE `userinfo` SET \r\n`users_usage` = `users_usage`+" . $amount . ",\r\n`in_down`='" . $usage_down . "',\r\n`in_up`='" . $usage_up . "',\r\n`av_qouta`='" . $usage_quota . "',\r\n`updatedate` = '" . $update_date . "'\r\n WHERE username='" . $get_user . "'");
            } else {
                $update_date = time();
                $update_f_q = mysqli_query($connect, "UPDATE `radusergroup` SET `free_q` = '0' WHERE `username` ='" . $get_user . "'");
                $update_user_usage = mysqli_query($connect, "UPDATE `userinfo` SET \r\n`users_usage` = `users_usage`+" . $amount . ",\r\n`in_down`='" . $fet_renew["profile_down_qouta"] . "',\r\n`in_up`='" . $fet_renew["profile_up_qouta"] . "',\r\n`av_qouta`='" . $usage_quota . "',\r\n`updatedate` = '" . $update_date . "'\r\n WHERE username='" . $get_user . "'");
            }
            $sel_online = mysqli_query($connect, "SELECT * FROM `radacct` WHERE `username` ='" . $get_user . "' AND `acctstoptime` IS NULL ");
            if ($fet_renew["reset_counters"] == 0) {
                $delete_usage_qouta = mysqli_query($connect, "DELETE FROM `radacct` WHERE `username`='" . $get_user . "'");
            }
            if (0 < mysqli_num_rows($sel_online)) {
                while ($fet_online = mysqli_fetch_assoc($sel_online)) {
                    $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                    if (0 < mysqli_affected_rows($connect) || $fet_renew["reset_counters"] == 0) {
                        $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $update_date . "' LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $update_date . "' LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >='" . $update_date . "'  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                    }
                }
            } else {
                if ($is_new_time == 1 || $fet_renew["remaining_qouta"] == 1) {
                    $update_radacct = mysqli_query($connect, "UPDATE `radacct` SET `down_uq`=0,up_uq=0,all_tu=0 WHERE `username` ='" . $get_user . "' ORDER BY `radacctid` DESC LIMIT 1 ");
                }
            }
            $update_user = mysqli_query($connect, "UPDATE `radcheck` SET `value` = '" . $set_date_exp . "' WHERE username='" . $get_user . "' AND attribute='Expiration'");
            mysqli_query($connect, "INSERT INTO `man_actions` \r\n(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`id_user`,`ip`,`get_os`,`browser`) VALUES \r\n('" . $fet_renew["id_man"] . "','5','1','" . $get_user . "','" . time() . "','" . $fet_renew["id_user"] . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
            if ($fet_set["enable_sms"] == 1) {
                $url = $fet_set["api_url"];
                $msg = $fet_set["act_msg"];
                if ($url && $msg && $fet_set["renew_user_sms"] == 1) {
                    $sel_all_users = mysqli_query($connect, "SELECT \r\n\t\r\n\t`userinfo`.`username`,\r\n\t`userinfo`.`mobile`,\r\n\t`userinfo`.`id`,\r\n\t`radusergroup`.`groupname` as `prof`,\r\n\t`radpass`.`value` as `pass`,\r\n\tDATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y') as `last_send_sms`,\r\n\tDATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y %H:%i:%s') as `exp`\r\n\tFROM `userinfo`\r\n\tLEFT JOIN `radcheck` ON (`radcheck`.`username` = `userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration')\r\n\tLEFT JOIN `radusergroup` ON (`radusergroup`.`username` = `userinfo`.`username`)\r\n\tLEFT JOIN `radcheck` as `radpass` ON (`radpass`.`username` = `userinfo`.`username` AND `radpass`.`attribute` ='Cleartext-Password' )\r\n\tWHERE `userinfo`.`username`='" . $get_user . "' GROUP BY `userinfo`.`username`");
                    if (0 < mysqli_num_rows($sel_all_users)) {
                        $fet_all_users = mysqli_fetch_array($sel_all_users);
                        $user = $fet_all_users["username"];
                        $pass = $fet_all_users["pass"];
                        $exp = $fet_all_users["exp"];
                        $prof = $fet_all_users["prof"];
                        $phone = $fet_all_users["mobile"];
                        $msg = str_ireplace("{price}", $amount, $msg);
                        sendSMS($url, $user, $pass, $exp, $prof, $day, $msg, $sender, $phone);
                    }
                }
            }
            if ($v_recharge == 1) {
                $val_for_event = $get_user . " (" . $fet_renew["p_recharge"] . " ";
                mysqli_query($connect, "INSERT INTO `man_actions` \r\n(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`id_user`,`ip`,`get_os`,`browser`) VALUES \r\n('" . $fet_renew["id_man"] . "','64','1','" . $val_for_event . "','" . time() . "','" . $fet_renew["id_user"] . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
                if (0 < $fet_renew["p_recharge"]) {
                    mysqli_query($connect, "UPDATE `managers` SET `credit`=`credit`+'" . $fet_renew["p_recharge"] . "' WHERE id='" . $fet_renew["ad_recharge"] . "' AND is_admin='0'");
                    mysqli_query($connect, "UPDATE `managers` SET `usage`=`usage`-'" . $fet_renew["p_recharge"] . "' WHERE id='" . $fet_renew["ad_recharge"] . "'");
                }
            }
        }
    }
}
$sel_ren_user = mysqli_query($connect, "SELECT \r\n\r\n`userinfo`.*,\r\nUNIX_TIMESTAMP(STR_TO_DATE(radcheck.value, '%d %b %Y %H:%i')) as `exp`,\r\n`userinfo`.`id` as `id_user`,\r\n`managers`.*,\r\n`p`.*,\r\n`p`.id as `id_prof`,\r\n`r8`.`value` as `val_address_list`,\r\n`managers`.`id` as `id_man`\r\nFROM `userinfo`\r\nINNER JOIN `managers` ON (`managers`.`id` = `userinfo`.`owner_id`)\r\nINNER JOIN `radcheck` ON (`radcheck`.`username`=`userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration') \r\nINNER JOIN `radusergroup` ON (`radusergroup`.`username` = `userinfo`.`username`)\r\nINNER JOIN `profiles` `p` ON (`p`.`profile_name` = `radusergroup`.`groupname` AND `p`.`type_prof` ='0')\r\nLEFT JOIN `radgroupreply` `r8` ON (`r8`.`groupname` = `p`.`profile_name` AND `r8`.`attribute` = 'Mikrotik-Address-List')\r\nWHERE `is_list` = '1'");
if (0 < mysqli_num_rows($sel_ren_user)) {
    while ($fet_ren_user = mysqli_fetch_assoc($sel_ren_user)) {
        $val_exp = unserialize($fet_ren_user["act_values"]);
        $val_prof = unserialize($fet_ren_user["prof_ren"]);
        $exp = $fet_ren_user["exp"];
        $is_val = 0;
        $n = 0;
        if (is_array($val_exp) && 0 < count($val_exp) && !empty($val_exp)) {
            while ($n < count($val_exp)) {
                if (strtotime($val_exp[$n]) <= time()) {
                    $get_time_exp = strtotime($val_exp[$n]);
                    if (empty($fet_ren_user["profile_name"])) {
                        $n++;
                        continue;
                    }
                    unset($val_exp[$n]);
                    $exp_af_unset = array_values($val_exp);
                    $is_val = 1;
                    break;
                }
                $n++;
            }
            if ($is_val == 1) {
                $get_user = $fet_ren_user["username"];
                $check_paid = 1;
                $comment_active = "";
                $send_sms = 1;
                if (time() < $exp && $fet_ren_user["added_qouta"] == 1 && $fet_ren_user["type_prof"] == 0) {
                    continue;
                }
                $sel_price_admin_for_this_profile = mysqli_query($connect, "SELECT `new_price` FROM `prices_profiles_admin` WHERE `id_profile` ='" . $fet_ren_user["id_prof"] . "' AND `id_manager` = '" . $fet_ren_user["owner_id"] . "'") or exit(mysqli_error($connect));
                if (mysqli_num_rows($sel_price_admin_for_this_profile) == 0) {
                    $get_price = $fet_ren_user["price"];
                    $user_price = $fet_ren_user["price"];
                } else {
                    $fet_price = mysqli_fetch_assoc($sel_price_admin_for_this_profile);
                    $get_price = $fet_price["new_price"];
                    if ($fet_set["disc_for_users"] == 1 && $fet_price["new_price"]) {
                        $user_price = $fet_price["new_price"];
                    } else {
                        $user_price = $fet_ren_user["price"];
                    }
                }
                if ($get_time_exp < time() || $fet_ren_user["remaining_time"] == 1) {
                    $get_time_exp = time();
                }
                if ($get_time_exp < time() || $fet_ren_user["remaining_qouta"] == 1) {
                    $update_date = time();
                    $r_free_q = 1;
                    $is_new_time = 1;
                } else {
                    $update_date = $fet_ren_user["updatedate"];
                    $r_free_q = 0;
                    $is_new_time = 0;
                }
                if (0 < $fet_ren_user["exp_unit"]) {
                    $time_now = time();
                    if ($fet_ren_user["exp_unit_val"] == 3) {
                        $t_date = date("Y-m-d H:i", strtotime("+" . $fet_ren_user["exp_unit"] . " months", $get_time_exp)) . "\n";
                    } else {
                        if ($fet_ren_user["exp_unit_val"] == 2) {
                            $t_date = date("Y-m-d H:i", strtotime("+" . $fet_ren_user["exp_unit"] . " days", $get_time_exp)) . "\n";
                        } else {
                            if ($fet_ren_user["exp_unit_val"] == 1) {
                                $t_date = date("Y-m-d H:i", strtotime("+" . $fet_ren_user["exp_unit"] . " hours", $get_time_exp)) . "\n";
                            } else {
                                if ($fet_ren_user["exp_unit_val"] == 0) {
                                    $t_date = date("Y-m-d H:i", strtotime("+" . $fet_ren_user["exp_unit"] . " minutes", $get_time_exp)) . "\n";
                                }
                            }
                        }
                    }
                    $time_date = strtotime($t_date);
                } else {
                    $time_now = time();
                    $t_date = date("Y-m-d H:i", strtotime("+1 months", $get_time_exp)) . "\n";
                    $time_date = strtotime($t_date);
                }
                $amount = $get_price;
                $u_amount = $user_price;
                $amount = round($amount, 2);
                $set_date_exp = date("d M Y H:i:s", $time_date);
                $v_recharge = 0;
                $sel_qouta = mysqli_query($connect, "SELECT \r\nSUM(`acctinputoctets` * (percent_q/100))/1024/1024 as `usage_upload`,\r\nSUM(`acctoutputoctets` * (percent_q/100))/1024/1024 as `usage_download` FROM `radacct`\r\nWHERE UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $fet_ren_user["updatedate"] . "'  AND  username='" . $get_user . "'") or exit(mysqli_error($connect));
                $fet_qouta = mysqli_fetch_array($sel_qouta);
                $all_usage = $fet_qouta["usage_download"] + $fet_qouta["usage_upload"];
                $up_qouta = $fet_ren_user["profile_up_qouta"];
                $down_qouta = $fet_ren_user["profile_down_qouta"];
                $all_qouta = $fet_ren_user["profile_qouta"];
                if ($fet_ren_user["remaining_qouta"] == 1) {
                    $usage_quota = $all_qouta;
                    $usage_up = $up_qouta;
                    $usage_down = $down_qouta;
                    $from_q = "0 B";
                    $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                } else {
                    if (0 < $up_qouta) {
                        if ($fet_ren_user["reset_counters"] == 0) {
                            if ($fet_ren_user["in_up"] == 0) {
                                $usage_up = $up_qouta;
                            } else {
                                if ($fet_ren_user["in_up"] <= $fet_qouta["usage_upload"]) {
                                    $usage_up = $up_qouta;
                                } else {
                                    $usage_up = $fet_ren_user["in_up"] - $fet_qouta["usage_upload"] + $up_qouta;
                                }
                            }
                        } else {
                            if ($fet_ren_user["in_up"] == 0) {
                                $usage_up = $fet_qouta["usage_upload"] + $up_qouta;
                            } else {
                                if ($fet_ren_user["in_up"] <= $fet_qouta["usage_upload"]) {
                                    $usage_up = $fet_qouta["usage_upload"] + $up_qouta;
                                } else {
                                    $usage_up = $fet_ren_user["in_up"] + $up_qouta;
                                }
                            }
                        }
                    } else {
                        $usage_up = 0;
                    }
                    if (0 < $down_qouta) {
                        if ($fet_ren_user["reset_counters"] == 0) {
                            if ($fet_ren_user["in_down"] == 0) {
                                $usage_down = $down_qouta;
                            } else {
                                if ($fet_ren_user["in_down"] <= $fet_qouta["usage_download"]) {
                                    $usage_down = $down_qouta;
                                } else {
                                    $usage_down = $fet_ren_user["in_down"] - $fet_qouta["usage_download"] + $down_qouta;
                                }
                            }
                        } else {
                            if ($fet_ren_user["in_down"] == 0) {
                                $usage_down = $fet_qouta["usage_download"] + $down_qouta;
                            } else {
                                if ($fet_ren_user["in_down"] <= $fet_qouta["usage_download"]) {
                                    $usage_down = $fet_qouta["usage_download"] + $down_qouta;
                                } else {
                                    $usage_down = $fet_ren_user["in_down"] + $down_qouta;
                                }
                            }
                        }
                    } else {
                        $usage_down = 0;
                    }
                    if (0 < $all_qouta) {
                        if ($fet_ren_user["reset_counters"] == 0) {
                            if ($fet_ren_user["av_qouta"] == 0) {
                                $usage_quota = $all_qouta;
                            } else {
                                if ($fet_ren_user["av_qouta"] <= $all_usage) {
                                    $usage_quota = $all_qouta;
                                } else {
                                    $usage_quota = $fet_ren_user["av_qouta"] - $all_usage + $all_qouta;
                                }
                            }
                            $from_q = "0 B";
                            $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                        } else {
                            if ($fet_ren_user["av_qouta"] == 0) {
                                $usage_quota = $all_usage + $all_qouta;
                                $from_q = "0 B";
                                $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                            } else {
                                if ($fet_ren_user["av_qouta"] <= $all_usage) {
                                    $usage_quota = $all_usage + $all_qouta;
                                    $from_q = "0 B";
                                    $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                                } else {
                                    $usage_quota = $fet_ren_user["av_qouta"] + $all_qouta;
                                    $from_q = $fet_ren_user["av_qouta"];
                                    $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                                }
                            }
                        }
                    } else {
                        $usage_quota = 0;
                        $from_q = $fet_ren_user["av_qouta"];
                        $to_q = formatSizeUnits($usage_quota * 1024 * 1024);
                    }
                }
                if (0 < $fet_ren_user["profile_qouta"] || $fet_ren_user["profile_qouta"] == 0 && $fet_ren_user["profile_down_qouta"] == 0 && $fet_ren_user["profile_up_qouta"] == 0) {
                    $usage_down = 0;
                    $usage_up = 0;
                    $fet_ren_user["profile_down_qouta"] = 0;
                    $fet_ren_user["profile_up_qouta"] = 0;
                }
                if ($fet_ren_user["is_recharge"] == 1) {
                    if ($fet_ren_user["q_recharge"] <= $usage_quota || $usage_quota == 0) {
                        if ($usage_quota != 0) {
                            $usage_quota = $usage_quota - $fet_ren_user["q_recharge"];
                            if ($usage_quota == 0) {
                                $usage_quota = 1;
                            }
                        }
                        $t_recharge = $fet_ren_user["t_recharge"] * 60 * 60;
                        $val_time = $time_date - time();
                        if ($t_recharge <= $val_time) {
                            $update_is_rechange = mysqli_query($connect, "UPDATE `userinfo` SET `is_recharge` = '0',q_recharge='0',t_recharge='0',`p_recharge`='0' WHERE `username` ='" . $get_user . "'");
                            $time_date = $time_date - $t_recharge;
                            $set_date_exp = date("d M Y H:i:s", $time_date);
                        } else {
                            exit;
                        }
                    } else {
                        exit;
                    }
                }
                if ($fet_ren_user["is_admin"] == 0) {
                    if ($fet_ren_user["credit"] < $amount) {
                        $n++;
                        exit;
                    }
                    $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `credit` = `credit` - " . $amount . ",`usage` =`usage` + " . $amount . "  WHERE id='" . $fet_ren_user["id_man"] . "'");
                } else {
                    $update_credit_manager = mysqli_query($connect, "UPDATE `managers` SET `usage` =`usage` + " . $amount . "  WHERE id='" . $fet_ren_user["id_man"] . "'");
                }
                if (0 < $u_amount) {
                    $insert_to_report = mysqli_query($connect, "INSERT INTO `a_s_man_users` \r\n(`id_user`,`id_manager`,`id_profile`,`operation`\r\n,`date_operation`,`new_price`,`status`,`id_prof_old`,`paid_unpaid`,`old_price`,\r\n`price_invoice`,`notes`) \r\nVALUES ('" . $fet_ren_user["id_user"] . "','" . $fet_ren_user["id_man"] . "','" . $id_prof . "','4'\r\n,'" . time() . "','" . $u_amount . "','1','','" . $check_paid . "','0','" . $u_amount . "','" . $comment_active . "')") or exit(mysqli_error($connect));
                }
                $val_address_list = $fet_ren_user["val_address_list"];
                $sel_online = mysqli_query($connect, "SELECT * FROM `radacct` WHERE `username` ='" . $get_user . "' AND `acctstoptime` IS NULL ");
                if ($fet_ren_user["remaining_qouta"] == 0) {
                    if ($r_free_q == 1) {
                        $update_f_q = mysqli_query($connect, "UPDATE `radusergroup` SET `free_q` = '0' WHERE `username` ='" . $get_user . "'");
                    }
                    $update_user_usage = mysqli_query($connect, "UPDATE `userinfo` SET \r\n`users_usage` = `users_usage`+" . $amount . ",\r\n`in_down`='" . $usage_down . "',\r\n`in_up`='" . $usage_up . "',\r\n`av_qouta`='" . $usage_quota . "',\r\n`updatedate` = '" . $update_date . "'\r\n WHERE username='" . $get_user . "'");
                } else {
                    if ($fet_ren_user["is_recharge"] == 1) {
                        if ($fet_ren_user["q_recharge"] <= $usage_quota || $usage_quota == 0) {
                            if ($usage_quota != 0) {
                                $usage_quota = $usage_quota - $fet_ren_user["q_recharge"];
                                if ($usage_quota == 0) {
                                    $usage_quota = 1;
                                }
                            }
                            $t_recharge = $fet_ren_user["t_recharge"] * 60 * 60;
                            $val_time = $time_date - time();
                            if ($t_recharge <= $val_time) {
                                $update_is_rechange = mysqli_query($connect, "UPDATE `userinfo` SET `is_recharge` = '0',q_recharge='0',t_recharge='0',`p_recharge`='0' WHERE `username` ='" . $get_user . "'");
                                $time_date = $time_date - $t_recharge;
                                $set_date_exp = date("d M Y H:i:s", $time_date);
                                $v_recharge = 1;
                            } else {
                                exit;
                            }
                        } else {
                            exit;
                        }
                    }
                    $update_date = time();
                    $update_f_q = mysqli_query($connect, "UPDATE `radusergroup` SET `free_q` = '0' WHERE `username` ='" . $get_user . "'");
                    $update_user_usage = mysqli_query($connect, "UPDATE `userinfo` SET \r\n`users_usage` = `users_usage`+" . $amount . ",\r\n`in_down`='" . $fet_ren_user["profile_down_qouta"] . "',\r\n`in_up`='" . $fet_ren_user["profile_up_qouta"] . "',\r\n`av_qouta`='" . $usage_quota . "',\r\n`updatedate` = '" . $update_date . "'\r\n WHERE username='" . $get_user . "'");
                }
                if ($fet_ren_user["reset_counters"] == 0) {
                    $delete_usage_qouta = mysqli_query($connect, "DELETE FROM `radacct` WHERE `username`='" . $get_user . "'");
                }
                if (0 < mysqli_num_rows($sel_online)) {
                    while ($fet_online = mysqli_fetch_assoc($sel_online)) {
                        $update_radacct = mysqli_query($connect, "UPDATE `radacct`\r\n SET \r\n `acctsessionid` =concat(acctsessionid,'" . substr(str_shuffle(MD5(microtime())), 0, 10) . "') ,\r\n `acctuniqueid` ='" . substr(str_shuffle(MD5(microtime())), 0, 10) . "' ,\r\n `acctstoptime`=CURRENT_TIMESTAMP,\r\n `re_time`= `acctsessiontime` + `re_time`,\r\n `acctsessiontime`= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(acctstarttime, '%Y-%m-%d %H:%i:%s')) \r\nWHERE `username` ='" . $get_user . "' AND `acctuniqueid` ='" . $fet_online["acctuniqueid"] . "';\r\n") or exit(mysqli_error($connect));
                        if (0 < mysqli_affected_rows($connect) || $fet_ren_user["reset_counters"] == 0) {
                            $insert_radacct = mysqli_query($connect, "\r\nINSERT INTO `radacct` (\r\n`acctsessionid`,\r\n`acctuniqueid`,\r\n`username`,\r\n`groupname`,\r\n`last_speed`,\r\n`nasipaddress`,\r\n`acctstarttime`,\r\n`acctstoptime`,\r\n`acctinputoctets`,\r\n`acctoutputoctets`,\r\n`calledstationid`,\r\n`callingstationid`,`framedipaddress`,`t_session`,\r\n`nas_name`,\r\n`percent_q`,\r\n`up_uq`,\r\n`down_uq`,\r\n`all_tu`,\r\n`re_up`,\r\n`re_down`,\r\n`re_time`\r\n)\r\nVALUES (\r\n'" . $fet_online["acctsessionid"] . "',\r\n'" . $fet_online["acctuniqueid"] . "',\r\n'" . $fet_online["username"] . "',\r\n'" . $fet_online["groupname"] . "',\r\n'" . $fet_online["last_speed"] . "',\r\n'" . $fet_online["nasipaddress"] . "',\r\nCURRENT_TIMESTAMP,\r\nNULL,\r\n'0',\r\n'0',\r\n'" . $fet_online["calledstationid"] . "',\r\n'" . $fet_online["callingstationid"] . "',\r\n'" . $fet_online["framedipaddress"] . "','" . time() . "',\r\n'" . $fet_online["nas_name"] . "',\r\nCOALESCE((SELECT  `percent_now` FROM `radusergroup` WHERE username = '" . $fet_online["username"] . "' LIMIT 1),'100'),\r\nCOALESCE((SELECT SUM((r1.acctoutputoctets) * (r1.percent_q/100)) FROM radacct as `r1` INNER JOIN userinfo ON (userinfo.username=r1.username) WHERE r1.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r1.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $update_date . "' LIMIT 1),0),\r\nCOALESCE((SELECT SUM((r3.acctinputoctets) * (r3.percent_q/100)) FROM radacct as `r3` INNER JOIN userinfo ON (userinfo.username=r3.username) WHERE r3.username = '" . $fet_online["username"] . "' AND UNIX_TIMESTAMP(STR_TO_DATE(r3.acctstarttime, '%Y-%m-%d %H:%i:%s')) >= '" . $update_date . "' LIMIT 1),'0'),\r\nCOALESCE((SELECT SUM((`r8`.`acctsessiontime`)) FROM `radacct` `r8` INNER JOIN `userinfo` ON (`r8`.`username` = `userinfo`.`username`) WHERE UNIX_TIMESTAMP(STR_TO_DATE(`r8`.`acctstarttime`, '%Y-%m-%d %H:%i:%s')) >='" . $update_date . "'  AND r8.username = '" . $fet_online["username"] . "' LIMIT 1),'0'),\r\n'" . $fet_online["acctinputoctets"] . "'+'" . $fet_online["re_up"] . "',\r\n'" . $fet_online["acctoutputoctets"] . "'+'" . $fet_online["re_down"] . "',\r\n'" . $fet_online["acctsessiontime"] . "'+'" . $fet_online["re_time"] . "'\r\n);\r\n") or exit(mysqli_error($connect));
                        }
                    }
                } else {
                    if ($is_new_time == 1 || $fet_ren_user["remaining_qouta"] == 1) {
                        $update_radacct = mysqli_query($connect, "UPDATE `radacct` SET `down_uq`=0,up_uq=0,all_tu=0 WHERE `username` ='" . $get_user . "' ORDER BY `radacctid` DESC LIMIT 1 ");
                    }
                }
                if ($fet_set["enable_sms"] == 1) {
                    $url = $fet_set["api_url"];
                    $msg = $fet_set["act_msg"];
                    if ($url && $msg && $fet_set["renew_user_sms"] == 1) {
                        $sel_all_users = mysqli_query($connect, "SELECT \r\n\t\r\n\t`userinfo`.`username`,\r\n\t`userinfo`.`mobile`,\r\n\t`userinfo`.`id`,\r\n\t`radusergroup`.`groupname` as `prof`,\r\n\t`radpass`.`value` as `pass`,\r\n\tDATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y') as `last_send_sms`,\r\n\tDATE_FORMAT(STR_TO_DATE(`radcheck`.`value`,'%d %b %Y %H:%i'),'%d-%m-%Y %H:%i:%s') as `exp`\r\n\tFROM `userinfo`\r\n\tLEFT JOIN `radcheck` ON (`radcheck`.`username` = `userinfo`.`username` AND `radcheck`.`attribute` = 'Expiration')\r\n\tLEFT JOIN `radusergroup` ON (`radusergroup`.`username` = `userinfo`.`username`)\r\n\tLEFT JOIN `radcheck` as `radpass` ON (`radpass`.`username` = `userinfo`.`username` AND `radpass`.`attribute` ='Cleartext-Password' )\r\n\tWHERE `userinfo`.`username`='" . $get_user . "' GROUP BY `userinfo`.`username`");
                        if (0 < mysqli_num_rows($sel_all_users)) {
                            $fet_all_users = mysqli_fetch_array($sel_all_users);
                            $user = $fet_all_users["username"];
                            $pass = $fet_all_users["pass"];
                            $exp = $fet_all_users["exp"];
                            $prof = $fet_all_users["prof"];
                            $phone = $fet_all_users["mobile"];
                            $msg = str_ireplace("{price}", $amount, $msg);
                            sendSMS($url, $user, $pass, $exp, $prof, $day, $msg, $sender, $phone);
                        }
                    }
                }
                if (0 < count($exp_af_unset)) {
                    $is_list = 1;
                } else {
                    $is_list = 0;
                }
                if ($v_recharge == 1) {
                    $val_for_event = $get_user . " (" . $fet_renew["p_recharge"] . " ";
                    mysqli_query($connect, "INSERT INTO `man_actions` \r\n(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`id_user`,`ip`,`get_os`,`browser`) VALUES \r\n('" . $fet_ren_user["id_man"] . "','64','1','" . $val_for_event . "','" . time() . "','" . $fet_renew["id_user"] . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
                    if (0 < $fet_ren_user["p_recharge"]) {
                        mysqli_query($connect, "UPDATE `managers` SET `credit`=`credit`+'" . $fet_ren_user["p_recharge"] . "' WHERE id='" . $fet_ren_user["ad_recharge"] . "' AND is_admin='0'");
                        mysqli_query($connect, "UPDATE `managers` SET `usage`=`usage`-'" . $fet_ren_user["p_recharge"] . "' WHERE id='" . $fet_ren_user["ad_recharge"] . "'");
                    }
                }
                $exp_af_unset = serialize($exp_af_unset);
                $update = mysqli_query($connect, "UPDATE `userinfo` SET `act_values` ='" . $exp_af_unset . "' ,`is_list`='" . $is_list . "' WHERE `username` ='" . $get_user . "' ");
                $update_user = mysqli_query($connect, "UPDATE `radcheck` SET `value` = '" . $set_date_exp . "' WHERE username='" . $get_user . "' AND attribute='Expiration'");
                mysqli_query($connect, "INSERT INTO `man_actions` \r\n(`id_man`,`id_state`,`id_set_action`,`more_details`,`t_date`,`id_user`,`ip`,`get_os`,`browser`) VALUES \r\n('" . $fet_ren_user["id_man"] . "','5','1','" . $get_user . "','" . time() . "','" . $fet_renew["id_user"] . "','" . GetIP() . "','" . getOS() . "','" . getBrowser() . "')");
            }
        }
    }
}
function get_profile($main_id, $usage, $quota, $id_prof)
{
    global $connect;
    global $profiles;
    global $q;
    $num = 1;
    if (!$id_prof) {
        $id_prof = $profiles["id_prof"];
    }
    $sql = "SELECT \r\n\r\n`p`.`profile_name` as `profile_name`,\r\n`p`.`id` as `id_prof`,\r\n`p`.`daily_down_qouta`*1024*1024 as `down_daily_qouta`,\r\n`p`.`daily_up_qouta`*1024*1024 as `up_daily_qouta`,\r\n`p`.`daily_profile_qouta`*1024*1024 as `daily_profile_qouta`,\r\n`p`.`online_time` as `online_time`,\r\n`p`.`hours_min` as `hours_min`,\r\n`p`.`daily_online_time` as `daily_online_time`,\r\n`p`.`daily_hours_min` as `daily_hours_min`,\r\n`p`.`bandwidth_time` as `bandwidth_time`,\r\n`p`.`multi_speed` as `multi_speed`,\r\n`p`.`bandwidth_percent` as `bandwidth_percent`,\r\n`p`.`daily_expire_service` as `daily_expire_service`,\r\n`p`.`qouta_expire_service` as `qouta_expire_service`,\r\n`pd`.`profile_expire_service` as `profile_expire_service`,\r\n`p`.`sp_up` as `sp_up`,\r\n`p`.`sp_down` as `sp_down`,\r\n`p`.`ch_day_end` as `ch_day_end`,\r\n`p`.`set_day_end` as `set_day_end`,\r\n`p`.`percent` as `percent`,\r\n`p`.`arr_days` as `p_arr_days`,\r\n`p`.`value_choice` as `p_value_choice`,\r\n`r1`.`value` as `value_exp`,\r\n`p1`.`bandwidth_time` as `band_exp`,\r\n`p1`.`bandwidth_percent` as `band_exp_percent`,\r\n`p1`.`percent` as `percent_exp`,\r\n`p1`.`profile_qouta` as `profile_qouta2`,\r\n`r2`.`value` as `val_address_exp_list`,\r\n`po1`.`value` as `q_pool`,\r\n`po2`.`value` as `d_pool`,\r\n`po3`.`value` as `ex_pool`,\r\n`po4`.`value` as `ad_pool`,\r\n`r3`.`value` as `value_exp_daily`,\r\n`p2`.`bandwidth_time` as `band_daily_exp`,\r\n`p2`.`bandwidth_percent` as `band_daily_exp_percent`,\r\n`p2`.`percent` as `percent_daily_exp`,\r\n`r4`.`value` as `val_address_exp_daily_list`,\r\n`r5`.`value` as `value_exp_serv`,\r\n`p3`.`bandwidth_time` as `band_serv_exp`,\r\n`p3`.`bandwidth_percent` as `band_serv_exp_percent`,\r\n`p3`.`percent` as `percent_serv_exp`,\r\n`r6`.`value` as `val_address_exp_serv_list`,\r\n`r7`.`value` as `value_now`,\r\n`r8`.`value` as `val_address_list`\r\n FROM `profiles` `p`\r\n LEFT JOIN `profiles` `pd` ON (`pd`.`id` = '" . $id_prof . "')\r\n LEFT JOIN `profiles` `p1` ON (`p1`.`id` = `p`.`qouta_expire_service`)\r\n LEFT JOIN `profiles` `p2` ON (`p2`.`id` = `p`.`daily_expire_service`)\r\n LEFT JOIN `profiles` `p3` ON (`p3`.`id` = `pd`.`profile_expire_service`)\r\n LEFT JOIN `radgroupreply` `r1` ON (`r1`.`groupname` = `p1`.`profile_name` AND `r1`.`attribute` = 'Mikrotik-Rate-Limit')\r\n LEFT JOIN `radgroupreply` `r2` ON (`r2`.`groupname` = `p1`.`profile_name` AND `r2`.`attribute` = 'Mikrotik-Address-List') \r\n LEFT JOIN `radgroupreply` `po1` ON (`po1`.`groupname` = `p1`.`profile_name` AND `po1`.`attribute` = 'Framed-Pool')\r\n LEFT JOIN `radgroupreply` `r3` ON (`r3`.`groupname` = `p2`.`profile_name` AND `r3`.`attribute` = 'Mikrotik-Rate-Limit')\r\n LEFT JOIN `radgroupreply` `r4` ON (`r4`.`groupname` = `p2`.`profile_name` AND `r4`.`attribute` = 'Mikrotik-Address-List') \r\n LEFT JOIN `radgroupreply` `po2` ON (`po2`.`groupname` = `p2`.`profile_name` AND `po2`.`attribute` = 'Framed-Pool') \r\n LEFT JOIN `radgroupreply` `r5` ON (`r5`.`groupname` = `p3`.`profile_name` AND `r5`.`attribute` = 'Mikrotik-Rate-Limit')\r\n LEFT JOIN `radgroupreply` `r6` ON (`r6`.`groupname` = `p3`.`profile_name` AND `r6`.`attribute` = 'Mikrotik-Address-List')\r\n LEFT JOIN `radgroupreply` `po3` ON (`po3`.`groupname` = `p3`.`profile_name` AND `po3`.`attribute` = 'Framed-Pool')\r\n LEFT JOIN `radgroupreply` `r7` ON (`r7`.`groupname` = `p`.`profile_name` AND `r7`.`attribute` = 'Mikrotik-Rate-Limit')\r\n LEFT JOIN `radgroupreply` `r8` ON (`r8`.`groupname` = `p`.`profile_name` AND `r8`.`attribute` = 'Mikrotik-Address-List')\r\n LEFT JOIN `radgroupreply` `po4` ON (`po4`.`groupname` = `p`.`profile_name` AND `po4`.`attribute` = 'Framed-Pool')\r\n WHERE `p`.`id` = '" . $main_id . "'";
    $query = mysqli_query($connect, $sql) or exit(mysqli_error($connect));
    $result = mysqli_fetch_assoc($query);
    if (!empty($result)) {
        $q += $quota;
        $result["usage"] = $usage;
        $result["q"] = $q;
        $profiles = $result;
        if ($num == 1) {
            if ($result["qouta_expire_service"] != 0 && $quota != 0 && $q < $result["usage"]) {
                $num++;
                if ($result["profile_qouta2"] == 0) {
                    $result["profile_qouta2"] = 1048576;
                }
                return get_profile($result["qouta_expire_service"], $result["usage"], $result["profile_qouta2"] * 1024 * 1024, $id_prof);
            }
        } else {
            if ($result["qouta_expire_service"] != 0 && $quota != 0 && $q < $result["usage"] && 0 < $result["profile_qouta"]) {
                $num++;
                return get_profile($result["qouta_expire_service"], $result["usage"], $result["profile_qouta2"] * 1024 * 1024, $id_prof);
            }
        }
    }
    $q = 0;
    $num = 0;
    return $profiles;
}

?>

Function Calls

ini_set 3
mysqli_connect 1

Variables

$db radius
$server localhost
$nn 0
$pass_db advradius_system
$user_db root

Stats

MD5 dd4efe91bacfa3c2e63d951f1759e9f9
Eval Count 0
Decode Time 5092 ms