mitrobill/system/cron.php

144 lines
6.1 KiB
PHP
Raw Normal View History

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-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();
2024-04-19 11:21:44 +07:00
$rows = array();
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-19 11:21:44 +07:00
$textExpired = Lang::getNotifText('expired');
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-19 11:21:44 +07:00
foreach ($d as $ds) {
$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";
$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();
$p = ORM::for_table('tbl_plans')->where('id', $u['plan_id'])->find_one();
2024-06-05 17:19:24 +07:00
$dvc = Package::getDevice($p);
if ($_app_stage != 'demo') {
2024-06-20 14:16:09 +07:00
if (file_exists($dvc)) {
require_once $dvc;
(new $p['device'])->remove_customer($c, $p);
} else {
echo "Cron error Devices $p[device] not found, cannot disconnect $c[username]";
Message::sendTelegram("Cron error Devices $p[device] not found, cannot disconnect $c[username]");
}
}
echo Message::sendPackageNotification($c, $u['namebp'], $p['price'], $textExpired, $config['user_notification_expired']) . "\n";
//update database user dengan status off
$u->status = 'off';
$u->save();
// autorenewal from deposit
if ($config['enable_balance'] == 'yes' && $c['auto_renewal']) {
list($bills, $add_cost) = User::getBills($ds['customer_id']);
if ($add_cost > 0) {
if (!empty($add_cost)) {
$p['price'] += $add_cost;
2023-10-16 10:15:45 +07:00
}
2024-04-19 11:21:44 +07:00
}
2024-06-21 16:05:05 +07:00
if ($p && $c['balance'] >= $p['price']) {
if (Package::rechargeUser($ds['customer_id'], $ds['routers'], $p['id'], 'Customer', 'Balance')) {
// if success, then get the balance
Balance::min($ds['customer_id'], $p['price']);
echo "plan enabled: $p[enabled] | User balance: $c[balance] | price $p[price]\n";
echo "auto renewall Success\n";
2023-10-04 15:07:38 +07:00
} else {
echo "plan enabled: $p[enabled] | User balance: $c[balance] | price $p[price]\n";
echo "auto renewall Failed\n";
Message::sendTelegram("FAILED RENEWAL #cron\n\n#u$c[username] #buy #Hotspot \n" . $p['name_plan'] .
"\nRouter: " . $p['routers'] .
"\nPrice: " . $p['price']);
}
2024-04-19 11:21:44 +07:00
} else {
echo "no renewall | plan enabled: $p[enabled] | User balance: $c[balance] | price $p[price]\n";
2023-08-15 10:17:28 +07:00
}
} else {
echo "no renewall | balance $config[enable_balance] auto_renewal $c[auto_renewal]\n";
}
2024-06-05 17:19:24 +07:00
} else {
echo " : ACTIVE \r\n";
2023-03-06 14:49:44 +07:00
}
}
if ($config['router_check']) {
$routers = ORM::for_table('tbl_routers')->find_many();
if (!$routers) {
echo "No routers found in the database.\n";
exit;
}
foreach ($routers as $router) {
[$ip, $port] = explode(':', $router->ip_address);
$isOnline = false;
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)");
}
} else {
throw new Exception("Neither fsockopen nor stream_socket_client are enabled on the server.");
}
} catch (Exception $e) {
_log($e->getMessage());
$adminEmail = $config['mail_from'];
$subject = "Router Monitoring Error Alert";
$message = "An error occurred during the monitoring of router $ip: " . (string) $e->getMessage();
Message::SendEmail($adminEmail, $subject, $message);
sendTelegram($message);
}
if ($isOnline) {
$router->last_seen = date('Y-m-d H:i:s');
$router->status = 'Online';
} else {
$router->status = 'Offline';
$adminEmail = $config['mail_from'];
$subject = "Router Offline Alert";
$message = "Dear Administrator,\nThe router with Name: {$router->name} and IP: {$router->ip_address} appears to be offline.\nThe Router was last seen online on: {$router->last_seen}\nPlease check the router's status and take appropriate action.\n\nBest regards,\nRouter Monitoring System";
Message::SendEmail($adminEmail, $subject, $message);
sendTelegram($message);
}
$router->save();
}
if ($isCli) {
echo "Cronjob finished\n";
} else {
echo "</pre>";
}
}