2017-03-11 03:51:06 +08:00
|
|
|
<?php
|
2023-03-06 15:49:44 +08:00
|
|
|
|
2017-03-11 03:51:06 +08:00
|
|
|
/**
|
2023-10-12 16:55:42 +08:00
|
|
|
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
|
|
|
|
* by https://t.me/ibnux
|
2023-03-06 15:49:44 +08:00
|
|
|
**/
|
2017-03-11 03:51:06 +08:00
|
|
|
|
2023-10-12 16:55:42 +08:00
|
|
|
|
2022-09-21 15:15:00 +08:00
|
|
|
// on some server, it getting error because of slash is backwards
|
|
|
|
function _autoloader($class)
|
|
|
|
{
|
|
|
|
if (strpos($class, '_') !== false) {
|
|
|
|
$class = str_replace('_', DIRECTORY_SEPARATOR, $class);
|
2023-10-04 16:07:38 +08:00
|
|
|
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'autoload' . DIRECTORY_SEPARATOR . $class . '.php')) {
|
|
|
|
include __DIR__ . DIRECTORY_SEPARATOR . 'autoload' . DIRECTORY_SEPARATOR . $class . '.php';
|
2022-09-21 15:15:00 +08:00
|
|
|
} else {
|
|
|
|
$class = str_replace("\\", DIRECTORY_SEPARATOR, $class);
|
|
|
|
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'autoload' . DIRECTORY_SEPARATOR . $class . '.php'))
|
|
|
|
include __DIR__ . DIRECTORY_SEPARATOR . 'autoload' . DIRECTORY_SEPARATOR . $class . '.php';
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-04 16:07:38 +08:00
|
|
|
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'autoload' . DIRECTORY_SEPARATOR . $class . '.php')) {
|
|
|
|
include __DIR__ . DIRECTORY_SEPARATOR . 'autoload' . DIRECTORY_SEPARATOR . $class . '.php';
|
2022-09-21 15:15:00 +08:00
|
|
|
} else {
|
|
|
|
$class = str_replace("\\", DIRECTORY_SEPARATOR, $class);
|
|
|
|
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'autoload' . DIRECTORY_SEPARATOR . $class . '.php'))
|
|
|
|
include __DIR__ . DIRECTORY_SEPARATOR . 'autoload' . DIRECTORY_SEPARATOR . $class . '.php';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spl_autoload_register('_autoloader');
|
|
|
|
|
2023-09-07 10:35:00 +08:00
|
|
|
|
2023-10-04 16:07:38 +08:00
|
|
|
if (php_sapi_name() !== 'cli') {
|
2023-09-07 10:35:00 +08:00
|
|
|
echo "<pre>";
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:38:58 +08:00
|
|
|
if(!file_exists('../config.php')){
|
|
|
|
die("config.php file not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!file_exists('orm.php')){
|
|
|
|
die("orm.php file not found");
|
|
|
|
}
|
|
|
|
|
2023-10-25 11:03:25 +08:00
|
|
|
if(!file_exists('uploads/notifications.default.json')){
|
|
|
|
die("uploads/notifications.default.json file not found");
|
2023-10-24 14:38:58 +08:00
|
|
|
}
|
2023-09-07 10:35:00 +08:00
|
|
|
|
2023-09-06 12:25:13 +08:00
|
|
|
require_once '../config.php';
|
2023-09-06 11:48:52 +08:00
|
|
|
require_once 'orm.php';
|
2023-09-06 12:25:13 +08:00
|
|
|
require_once 'autoload/PEAR2/Autoload.php';
|
|
|
|
include "autoload/Hookers.php";
|
2023-09-06 11:48:52 +08:00
|
|
|
|
|
|
|
ORM::configure("mysql:host=$db_host;dbname=$db_name");
|
|
|
|
ORM::configure('username', $db_user);
|
|
|
|
ORM::configure('password', $db_password);
|
|
|
|
ORM::configure('return_result_sets', true);
|
|
|
|
ORM::configure('logging', true);
|
|
|
|
|
|
|
|
|
|
|
|
// notification message
|
2023-09-06 12:25:13 +08:00
|
|
|
if (file_exists("uploads/notifications.json")) {
|
|
|
|
$_notifmsg = json_decode(file_get_contents('uploads/notifications.json'), true);
|
2023-09-06 11:48:52 +08:00
|
|
|
}
|
2023-09-06 12:25:13 +08:00
|
|
|
$_notifmsg_default = json_decode(file_get_contents('uploads/notifications.default.json'), true);
|
2023-09-06 11:48:52 +08:00
|
|
|
|
|
|
|
//register all plugin
|
|
|
|
foreach (glob(File::pathFixer("plugin/*.php")) as $filename) {
|
|
|
|
include $filename;
|
|
|
|
}
|
|
|
|
|
2017-03-11 03:51:06 +08:00
|
|
|
$result = ORM::for_table('tbl_appconfig')->find_many();
|
2023-03-06 15:49:44 +08:00
|
|
|
foreach ($result as $value) {
|
|
|
|
$config[$value['setting']] = $value['value'];
|
2017-03-11 03:51:06 +08:00
|
|
|
}
|
2023-08-18 10:47:03 +08:00
|
|
|
|
2023-10-04 16:07:38 +08:00
|
|
|
if (!empty($radius_user) && $config['radius_enable']) {
|
|
|
|
ORM::configure("mysql:host=$radius_host;dbname=$radius_name", null, 'radius');
|
|
|
|
ORM::configure('username', $radius_user, 'radius');
|
|
|
|
ORM::configure('password', $radius_pass, 'radius');
|
|
|
|
ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'), 'radius');
|
|
|
|
ORM::configure('return_result_sets', true, 'radius');
|
|
|
|
}
|
|
|
|
|
2023-09-07 10:35:00 +08:00
|
|
|
echo "PHP Time\t" . date('Y-m-d H:i:s') . "\n";
|
|
|
|
$res = ORM::raw_execute('SELECT NOW() AS WAKTU;');
|
|
|
|
$statement = ORM::get_last_statement();
|
|
|
|
$rows = array();
|
|
|
|
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
|
|
|
|
echo "MYSQL Time\t" . $row['WAKTU'] . "\n";
|
|
|
|
}
|
|
|
|
|
2023-08-18 10:47:03 +08:00
|
|
|
$_c = $config;
|
|
|
|
|
2017-03-11 03:51:06 +08:00
|
|
|
date_default_timezone_set($config['timezone']);
|
|
|
|
|
2023-08-24 12:35:23 +08:00
|
|
|
$textExpired = Lang::getNotifText('expired');
|
2023-03-06 15:49:44 +08:00
|
|
|
|
2023-08-28 11:38:59 +08:00
|
|
|
$d = ORM::for_table('tbl_user_recharges')->where('status', 'on')->where_lte('expiration', date("Y-m-d"))->find_many();
|
2023-09-01 14:40:26 +08:00
|
|
|
echo "Found " . count($d) . " user(s)\n";
|
2022-09-21 15:15:00 +08:00
|
|
|
run_hook('cronjob'); #HOOK
|
|
|
|
|
2023-03-06 15:49:44 +08:00
|
|
|
foreach ($d as $ds) {
|
|
|
|
if ($ds['type'] == 'Hotspot') {
|
|
|
|
$date_now = strtotime(date("Y-m-d H:i:s"));
|
|
|
|
$expiration = strtotime($ds['expiration'] . ' ' . $ds['time']);
|
|
|
|
echo $ds['expiration'] . " : " . $ds['username'];
|
|
|
|
if ($date_now >= $expiration) {
|
|
|
|
echo " : EXPIRED \r\n";
|
|
|
|
$u = ORM::for_table('tbl_user_recharges')->where('id', $ds['id'])->find_one();
|
|
|
|
$c = ORM::for_table('tbl_customers')->where('id', $ds['customer_id'])->find_one();
|
|
|
|
$m = ORM::for_table('tbl_routers')->where('name', $ds['routers'])->find_one();
|
2023-09-06 11:02:31 +08:00
|
|
|
$p = ORM::for_table('tbl_plans')->where('id', $u['plan_id'])->find_one();
|
2023-03-06 15:49:44 +08:00
|
|
|
|
2023-10-04 17:11:55 +08:00
|
|
|
if ($p['is_radius']) {
|
2023-10-24 10:19:35 +08:00
|
|
|
if (empty($p['pool_expired'])) {
|
2023-10-16 11:15:45 +08:00
|
|
|
print_r(Radius::customerDeactivate($c['username']));
|
|
|
|
} else {
|
2023-11-09 15:52:41 +08:00
|
|
|
Radius::upsertCustomerAttr($c['username'], 'Framed-Pool', $p['pool_expired'], ':=');
|
2023-10-16 11:15:45 +08:00
|
|
|
print_r(Radius::disconnectCustomer($c['username']));
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-18 01:52:39 +08:00
|
|
|
$client = Mikrotik::getClient($m['ip_address'], $m['username'], $m['password']);
|
2023-10-04 16:07:38 +08:00
|
|
|
if (!empty($p['pool_expired'])) {
|
|
|
|
Mikrotik::setHotspotUserPackage($client, $c['username'], 'EXPIRED NUXBILL ' . $p['pool_expired']);
|
|
|
|
} else {
|
2023-09-06 11:02:31 +08:00
|
|
|
Mikrotik::removeHotspotUser($client, $c['username']);
|
|
|
|
}
|
2023-09-13 10:37:05 +08:00
|
|
|
Mikrotik::removeHotspotActiveUser($client, $c['username']);
|
2022-09-07 17:11:35 +08:00
|
|
|
}
|
2023-10-24 16:12:10 +08:00
|
|
|
Message::sendPackageNotification($c['phonenumber'], $c['fullname'], $u['namebp'], $textExpired, $config['user_notification_expired']);
|
2023-03-06 15:49:44 +08:00
|
|
|
//update database user dengan status off
|
|
|
|
$u->status = 'off';
|
|
|
|
$u->save();
|
2023-08-15 11:17:28 +08:00
|
|
|
|
|
|
|
// autorenewal from deposit
|
|
|
|
if ($config['enable_balance'] == 'yes' && $c['auto_renewal']) {
|
|
|
|
if ($p && $p['enabled'] && $c['balance'] >= $p['price']) {
|
|
|
|
if (Package::rechargeUser($ds['customer_id'], $p['routers'], $p['id'], 'Customer', 'Balance')) {
|
|
|
|
// if success, then get the balance
|
|
|
|
Balance::min($ds['customer_id'], $p['price']);
|
2023-08-18 10:47:03 +08:00
|
|
|
echo "plan enabled: $p[enabled] | User balance: $c[balance] | price $p[price]\n";
|
|
|
|
echo "auto renewall Success\n";
|
2023-08-15 11:17:28 +08:00
|
|
|
} else {
|
2023-08-18 10:47:03 +08:00
|
|
|
echo "plan enabled: $p[enabled] | User balance: $c[balance] | price $p[price]\n";
|
|
|
|
echo "auto renewall Failed\n";
|
2023-08-15 11:17:28 +08:00
|
|
|
Message::sendTelegram("FAILED RENEWAL #cron\n\n#u$c[username] #buy #Hotspot \n" . $p['name_plan'] .
|
|
|
|
"\nRouter: " . $router_name .
|
|
|
|
"\nPrice: " . $p['price']);
|
|
|
|
}
|
2023-09-01 14:40:26 +08:00
|
|
|
} else {
|
2023-08-18 10:47:03 +08:00
|
|
|
echo "no renewall | plan enabled: $p[enabled] | User balance: $c[balance] | price $p[price]\n";
|
2023-08-15 11:17:28 +08:00
|
|
|
}
|
2023-09-01 14:40:26 +08:00
|
|
|
} else {
|
2023-08-18 10:47:03 +08:00
|
|
|
echo "no renewall | balance $config[enable_balance] auto_renewal $c[auto_renewal]\n";
|
2023-08-15 11:17:28 +08:00
|
|
|
}
|
2023-03-06 15:49:44 +08:00
|
|
|
} else echo " : ACTIVE \r\n";
|
|
|
|
} else {
|
|
|
|
$date_now = strtotime(date("Y-m-d H:i:s"));
|
|
|
|
$expiration = strtotime($ds['expiration'] . ' ' . $ds['time']);
|
|
|
|
echo $ds['expiration'] . " : " . $ds['username'];
|
|
|
|
if ($date_now >= $expiration) {
|
|
|
|
echo " : EXPIRED \r\n";
|
|
|
|
$u = ORM::for_table('tbl_user_recharges')->where('id', $ds['id'])->find_one();
|
|
|
|
$c = ORM::for_table('tbl_customers')->where('id', $ds['customer_id'])->find_one();
|
|
|
|
$m = ORM::for_table('tbl_routers')->where('name', $ds['routers'])->find_one();
|
2023-09-06 11:02:31 +08:00
|
|
|
$p = ORM::for_table('tbl_plans')->where('id', $u['plan_id'])->find_one();
|
2023-03-06 15:49:44 +08:00
|
|
|
|
2023-10-04 17:11:55 +08:00
|
|
|
if ($p['is_radius']) {
|
2023-10-24 10:19:35 +08:00
|
|
|
if (empty($p['pool_expired'])) {
|
2023-10-16 11:15:45 +08:00
|
|
|
print_r(Radius::customerDeactivate($c['username']));
|
|
|
|
} else {
|
2023-11-09 15:52:41 +08:00
|
|
|
Radius::upsertCustomerAttr($c['username'], 'Framed-Pool', $p['pool_expired'], ':=');
|
2023-10-16 11:15:45 +08:00
|
|
|
print_r(Radius::disconnectCustomer($c['username']));
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-18 01:52:39 +08:00
|
|
|
$client = Mikrotik::getClient($m['ip_address'], $m['username'], $m['password']);
|
2023-10-04 16:07:38 +08:00
|
|
|
if (!empty($p['pool_expired'])) {
|
|
|
|
Mikrotik::setPpoeUserPlan($client, $c['username'], 'EXPIRED NUXBILL ' . $p['pool_expired']);
|
|
|
|
} else {
|
2023-09-06 11:02:31 +08:00
|
|
|
Mikrotik::removePpoeUser($client, $c['username']);
|
|
|
|
}
|
2023-09-13 10:37:05 +08:00
|
|
|
Mikrotik::removePpoeActive($client, $c['username']);
|
2022-09-07 17:11:35 +08:00
|
|
|
}
|
2023-10-24 16:12:10 +08:00
|
|
|
Message::sendPackageNotification($c['phonenumber'], $c['fullname'], $u['namebp'], $textExpired, $config['user_notification_expired']);
|
2022-04-23 02:13:17 +08:00
|
|
|
|
2023-03-06 15:49:44 +08:00
|
|
|
$u->status = 'off';
|
|
|
|
$u->save();
|
2023-08-15 11:17:28 +08:00
|
|
|
|
|
|
|
// autorenewal from deposit
|
|
|
|
if ($config['enable_balance'] == 'yes' && $c['auto_renewal']) {
|
|
|
|
if ($p && $p['enabled'] && $c['balance'] >= $p['price']) {
|
|
|
|
if (Package::rechargeUser($ds['customer_id'], $p['routers'], $p['id'], 'Customer', 'Balance')) {
|
|
|
|
// if success, then get the balance
|
|
|
|
Balance::min($ds['customer_id'], $p['price']);
|
2023-09-06 11:55:28 +08:00
|
|
|
echo "plan enabled: $p[enabled] | User balance: $c[balance] | price $p[price]\n";
|
|
|
|
echo "auto renewall Success\n";
|
2023-08-15 11:17:28 +08:00
|
|
|
} else {
|
2023-09-06 11:55:28 +08:00
|
|
|
echo "plan enabled: $p[enabled] | User balance: $c[balance] | price $p[price]\n";
|
|
|
|
echo "auto renewall Failed\n";
|
2023-08-18 15:01:59 +08:00
|
|
|
Message::sendTelegram("FAILED RENEWAL #cron\n\n#u$c[username] #buy #PPPOE \n" . $p['name_plan'] .
|
2023-08-15 11:17:28 +08:00
|
|
|
"\nRouter: " . $router_name .
|
|
|
|
"\nPrice: " . $p['price']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-06 15:49:44 +08:00
|
|
|
} else echo " : ACTIVE \r\n";
|
|
|
|
}
|
2017-03-11 03:51:06 +08:00
|
|
|
}
|