2017-03-11 02:51:06 +07:00
|
|
|
<?php
|
2023-03-06 14:49:44 +07:00
|
|
|
|
2024-02-19 14:24:34 +07:00
|
|
|
include "../init.php";
|
2024-09-11 14:19:13 +01:00
|
|
|
$lockFile = "$CACHE_PATH/router_monitor.lock";
|
|
|
|
|
|
|
|
if (!is_dir($CACHE_PATH)) {
|
|
|
|
echo "Directory '$CACHE_PATH' does not exist. Exiting...\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lock = fopen($lockFile, 'c');
|
|
|
|
|
|
|
|
if ($lock === false) {
|
|
|
|
echo "Failed to open lock file. Exiting...\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!flock($lock, LOCK_EX | LOCK_NB)) {
|
|
|
|
echo "Script is already running. Exiting...\n";
|
|
|
|
fclose($lock);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-19 11:21:44 +07:00
|
|
|
$isCli = true;
|
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
|
|
$isCli = false;
|
2023-09-07 09:35:00 +07:00
|
|
|
echo "<pre>";
|
|
|
|
}
|
|
|
|
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();
|
2025-02-09 16:06:59 +01:00
|
|
|
$rows = [];
|
2023-09-07 09:35:00 +07:00
|
|
|
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
|
|
|
|
echo "MYSQL Time\t" . $row['WAKTU'] . "\n";
|
|
|
|
}
|
|
|
|
|
2024-04-19 11:21:44 +07:00
|
|
|
$_c = $config;
|
2023-08-15 10:17:28 +07:00
|
|
|
|
2024-04-08 21:21:54 +07:00
|
|
|
|
2024-04-19 11:21:44 +07:00
|
|
|
$textExpired = Lang::getNotifText('expired');
|
2024-04-08 21:21:54 +07:00
|
|
|
|
2024-04-19 11:21:44 +07:00
|
|
|
$d = ORM::for_table('tbl_user_recharges')->where('status', 'on')->where_lte('expiration', date("Y-m-d"))->find_many();
|
|
|
|
echo "Found " . count($d) . " user(s)\n";
|
2024-06-21 16:05:05 +07:00
|
|
|
run_hook('cronjob'); #HOOK
|
2024-04-08 21:21:54 +07:00
|
|
|
|
2024-04-19 11:21:44 +07:00
|
|
|
foreach ($d as $ds) {
|
2025-02-09 16:06:59 +01:00
|
|
|
try {
|
|
|
|
$date_now = strtotime(date("Y-m-d H:i:s"));
|
|
|
|
$expiration = strtotime($ds['expiration'] . ' ' . $ds['time']);
|
|
|
|
echo $ds['expiration'] . " : " . ($isCli ? $ds['username'] : Lang::maskText($ds['username']));
|
|
|
|
|
|
|
|
if ($date_now >= $expiration) {
|
|
|
|
echo " : EXPIRED \r\n";
|
|
|
|
|
|
|
|
// Fetch user recharge details
|
|
|
|
$u = ORM::for_table('tbl_user_recharges')->where('id', $ds['id'])->find_one();
|
|
|
|
if (!$u) {
|
|
|
|
throw new Exception("User recharge record not found for ID: " . $ds['id']);
|
2024-06-20 14:16:09 +07:00
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
|
|
|
|
// Fetch customer details
|
|
|
|
$c = ORM::for_table('tbl_customers')->where('id', $ds['customer_id'])->find_one();
|
|
|
|
if (!$c) {
|
|
|
|
$c = $u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch plan details
|
|
|
|
$p = ORM::for_table('tbl_plans')->where('id', $u['plan_id'])->find_one();
|
|
|
|
if (!$p) {
|
|
|
|
throw new Exception("Plan not found for ID: " . $u['plan_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dvc = Package::getDevice($p);
|
|
|
|
if ($_app_stage != 'demo') {
|
|
|
|
if (file_exists($dvc)) {
|
|
|
|
require_once $dvc;
|
|
|
|
try {
|
|
|
|
(new $p['device'])->remove_customer($c, $p);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
_log($e->getMessage());
|
|
|
|
sendTelegram($e->getMessage());
|
|
|
|
echo "Error: " . $e->getMessage() . "\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Exception("Cron error: Devices " . $p['device'] . "not found, cannot disconnect ".$c['username']."\n");
|
2023-10-16 10:15:45 +07:00
|
|
|
}
|
2024-04-19 11:21:44 +07:00
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
|
|
|
|
// Send notification and update user status
|
|
|
|
try {
|
|
|
|
echo Message::sendPackageNotification($c, $u['namebp'], $p['price'], $textExpired, $config['user_notification_expired']) . "\n";
|
|
|
|
$u->status = 'off';
|
|
|
|
$u->save();
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
_log($e->getMessage());
|
|
|
|
sendTelegram($e->getMessage());
|
|
|
|
echo "Error: " . $e->getMessage() . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auto-renewal from deposit
|
|
|
|
if ($config['enable_balance'] == 'yes' && $c['auto_renewal']) {
|
|
|
|
[$bills, $add_cost] = User::getBills($ds['customer_id']);
|
|
|
|
if ($add_cost != 0) {
|
|
|
|
$p['price'] += $add_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($p && $c['balance'] >= $p['price']) {
|
|
|
|
if (Package::rechargeUser($ds['customer_id'], $ds['routers'], $p['id'], 'Customer', 'Balance')) {
|
|
|
|
Balance::min($ds['customer_id'], $p['price']);
|
|
|
|
echo "plan enabled: " . (string) $p['enabled'] . " | User balance: " . (string) $c['balance'] . " | price " . (string) $p['price'] . "\n";
|
|
|
|
echo "auto renewal Success\n";
|
|
|
|
} else {
|
|
|
|
echo "plan enabled: " . $p['enabled'] . " | User balance: " . $c['balance'] . " | price " . $p['price'] . "\n";
|
|
|
|
echo "auto renewal Failed\n";
|
|
|
|
Message::sendTelegram("FAILED RENEWAL #cron\n\n#u." . $c['username'] . " #buy #Hotspot \n" . $p['name_plan'] .
|
|
|
|
"\nRouter: " . $p['routers'] .
|
|
|
|
"\nPrice: " . $p['price']);
|
|
|
|
}
|
2023-10-04 15:07:38 +07:00
|
|
|
} else {
|
2025-02-09 16:06:59 +01:00
|
|
|
echo "no renewal | plan enabled: " . (string) $p['enabled'] . " | User balance: " . (string) $c['balance'] . " | price " . (string) $p['price'] . "\n";
|
2023-09-06 10:02:31 +07:00
|
|
|
}
|
2024-04-19 11:21:44 +07:00
|
|
|
} else {
|
2025-02-09 16:06:59 +01:00
|
|
|
echo "no renewal | balance" . $config['enable_balance'] . " auto_renewal " . $c['auto_renewal'] . "\n";
|
2023-08-15 10:17:28 +07:00
|
|
|
}
|
2024-06-04 17:14:04 +07:00
|
|
|
} else {
|
2025-02-09 16:06:59 +01:00
|
|
|
echo " : ACTIVE \r\n";
|
2024-06-04 17:14:04 +07:00
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
// Catch any unexpected errors
|
|
|
|
_log($e->getMessage());
|
|
|
|
sendTelegram($e->getMessage());
|
|
|
|
echo "Unexpected Error: " . $e->getMessage() . "\n";
|
2023-03-06 14:49:44 +07:00
|
|
|
}
|
2024-05-07 04:55:20 +07:00
|
|
|
}
|
2024-08-27 00:26:52 +01:00
|
|
|
|
2025-02-09 16:06:59 +01:00
|
|
|
//Cek interim-update radiusrest
|
|
|
|
if ($config['frrest_interim_update'] != 0) {
|
2024-12-29 19:15:19 +07:00
|
|
|
|
|
|
|
$r_a = ORM::for_table('rad_acct')
|
2025-02-09 16:06:59 +01:00
|
|
|
->whereRaw("BINARY acctstatustype = 'Start' OR acctstatustype = 'Interim-Update'")
|
|
|
|
->where_lte('dateAdded', date("Y-m-d H:i:s"))->find_many();
|
|
|
|
|
|
|
|
foreach ($r_a as $ra) {
|
|
|
|
$interval = $_c['frrest_interim_update'] * 60;
|
|
|
|
$timeUpdate = strtotime($ra['dateAdded']) + $interval;
|
|
|
|
$timeNow = strtotime(date("Y-m-d H:i:s"));
|
|
|
|
if ($timeNow >= $timeUpdate) {
|
|
|
|
$ra->acctstatustype = 'Stop';
|
|
|
|
$ra->save();
|
|
|
|
}
|
|
|
|
}
|
2024-12-29 19:15:19 +07:00
|
|
|
}
|
2024-08-28 07:23:58 +01:00
|
|
|
|
2024-08-27 08:17:28 +01:00
|
|
|
if ($config['router_check']) {
|
2024-09-11 14:19:13 +01:00
|
|
|
echo "Checking router status...\n";
|
2024-08-28 07:23:58 +01:00
|
|
|
$routers = ORM::for_table('tbl_routers')->where('enabled', '1')->find_many();
|
2024-08-27 08:17:28 +01:00
|
|
|
if (!$routers) {
|
2024-08-28 07:23:58 +01:00
|
|
|
echo "No active routers found in the database.\n";
|
|
|
|
flock($lock, LOCK_UN);
|
|
|
|
fclose($lock);
|
|
|
|
unlink($lockFile);
|
2024-08-27 08:17:28 +01:00
|
|
|
exit;
|
|
|
|
}
|
2024-08-27 00:26:52 +01:00
|
|
|
|
2024-08-28 07:23:58 +01:00
|
|
|
$offlineRouters = [];
|
|
|
|
$errors = [];
|
|
|
|
|
2024-08-27 08:17:28 +01:00
|
|
|
foreach ($routers as $router) {
|
2024-08-28 13:58:40 +07:00
|
|
|
// check if custom port
|
2025-02-09 16:06:59 +01:00
|
|
|
if (strpos($router->ip_address, ':') === false) {
|
2024-08-28 13:58:40 +07:00
|
|
|
$ip = $router->ip_address;
|
|
|
|
$port = 8728;
|
|
|
|
} else {
|
|
|
|
[$ip, $port] = explode(':', $router->ip_address);
|
|
|
|
}
|
2024-08-27 08:17:28 +01:00
|
|
|
$isOnline = false;
|
2024-08-27 00:26:52 +01:00
|
|
|
|
2024-08-27 08:17:28 +01:00
|
|
|
try {
|
|
|
|
$timeout = 5;
|
|
|
|
if (is_callable('fsockopen') && false === stripos(ini_get('disable_functions'), 'fsockopen')) {
|
|
|
|
$fsock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
|
|
|
|
if ($fsock) {
|
|
|
|
fclose($fsock);
|
|
|
|
$isOnline = true;
|
|
|
|
} else {
|
|
|
|
throw new Exception("Unable to connect to $ip on port $port using fsockopen: $errstr ($errno)");
|
|
|
|
}
|
|
|
|
} elseif (is_callable('stream_socket_client') && false === stripos(ini_get('disable_functions'), 'stream_socket_client')) {
|
|
|
|
$connection = @stream_socket_client("$ip:$port", $errno, $errstr, $timeout);
|
|
|
|
if ($connection) {
|
|
|
|
fclose($connection);
|
|
|
|
$isOnline = true;
|
|
|
|
} else {
|
|
|
|
throw new Exception("Unable to connect to $ip on port $port using stream_socket_client: $errstr ($errno)");
|
|
|
|
}
|
2024-08-27 00:26:52 +01:00
|
|
|
} else {
|
2024-08-27 08:17:28 +01:00
|
|
|
throw new Exception("Neither fsockopen nor stream_socket_client are enabled on the server.");
|
2024-08-27 00:26:52 +01:00
|
|
|
}
|
2024-08-27 08:17:28 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
_log($e->getMessage());
|
2024-08-28 07:23:58 +01:00
|
|
|
$errors[] = "Error with router $ip: " . $e->getMessage();
|
2024-08-27 08:17:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($isOnline) {
|
|
|
|
$router->last_seen = date('Y-m-d H:i:s');
|
|
|
|
$router->status = 'Online';
|
2024-08-27 00:26:52 +01:00
|
|
|
} else {
|
2024-08-27 08:17:28 +01:00
|
|
|
$router->status = 'Offline';
|
2024-08-28 07:23:58 +01:00
|
|
|
$offlineRouters[] = $router;
|
2024-08-27 00:26:52 +01:00
|
|
|
}
|
2024-08-27 08:17:28 +01:00
|
|
|
|
|
|
|
$router->save();
|
2024-08-27 00:26:52 +01:00
|
|
|
}
|
|
|
|
|
2024-08-28 07:23:58 +01:00
|
|
|
if (!empty($offlineRouters)) {
|
|
|
|
$message = "Dear Administrator,\n";
|
|
|
|
$message .= "The following routers are offline:\n";
|
|
|
|
foreach ($offlineRouters as $router) {
|
|
|
|
$message .= "Name: {$router->name}, IP: {$router->ip_address}, Last Seen: {$router->last_seen}\n";
|
|
|
|
}
|
|
|
|
$message .= "\nPlease check the router's status and take appropriate action.\n\nBest regards,\nRouter Monitoring System";
|
|
|
|
|
|
|
|
$adminEmail = $config['mail_from'];
|
|
|
|
$subject = "Router Offline Alert";
|
|
|
|
Message::SendEmail($adminEmail, $subject, $message);
|
|
|
|
sendTelegram($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($errors)) {
|
|
|
|
$message = "The following errors occurred during router monitoring:\n";
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
$message .= "$error\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$adminEmail = $config['mail_from'];
|
|
|
|
$subject = "Router Monitoring Error Alert";
|
|
|
|
Message::SendEmail($adminEmail, $subject, $message);
|
|
|
|
sendTelegram($message);
|
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
echo "Router monitoring finished checking.\n";
|
2024-08-27 00:26:52 +01:00
|
|
|
}
|
2024-09-11 14:19:13 +01:00
|
|
|
|
|
|
|
flock($lock, LOCK_UN);
|
|
|
|
fclose($lock);
|
|
|
|
unlink($lockFile);
|
|
|
|
|
|
|
|
$timestampFile = "$UPLOAD_PATH/cron_last_run.txt";
|
|
|
|
file_put_contents($timestampFile, time());
|
|
|
|
|
2024-12-29 19:15:19 +07:00
|
|
|
run_hook('cronjob_end'); #HOOK
|
2025-02-09 16:06:59 +01:00
|
|
|
echo "Cron job finished and completed successfully.\n";
|