330 lines
9.8 KiB
PHP
Raw Normal View History

2017-03-11 02:51:06 +07:00
<?php
2023-06-15 15:26:38 +07:00
2017-03-11 02:51:06 +07:00
/**
2023-10-12 15:55:42 +07:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
2023-06-15 15:26:38 +07:00
**/
2017-03-11 02:51:06 +07:00
2023-10-12 15:55:42 +07:00
2023-06-15 15:26:38 +07:00
class User
{
2024-03-13 14:32:10 +07:00
public static function getID()
{
2024-07-29 09:06:27 +07:00
global $db_pass;
2024-03-13 14:32:10 +07:00
if (isset($_SESSION['uid']) && !empty($_SESSION['uid'])) {
2024-02-12 09:45:44 +07:00
return $_SESSION['uid'];
2024-03-13 14:32:10 +07:00
} else if (isset($_COOKIE['uid'])) {
2024-02-12 09:45:44 +07:00
// id.time.sha1
2024-03-13 14:32:10 +07:00
$tmp = explode('.', $_COOKIE['uid']);
2024-07-29 09:06:27 +07:00
if (sha1($tmp[0] . '.' . $tmp[1] . '.' . $db_pass) == $tmp[2]) {
2024-03-13 14:32:10 +07:00
if (time() - $tmp[1] < 86400 * 30) {
2024-02-12 09:45:44 +07:00
$_SESSION['uid'] = $tmp[0];
return $tmp[0];
}
}
}
return 0;
}
2024-11-05 12:07:54 +07:00
public static function getTawkToHash($email)
{
global $config;
2025-02-07 15:18:50 +07:00
if (!empty($config['tawkto_api_key']) && !empty($email)) {
2024-11-05 12:07:54 +07:00
return hash_hmac('sha256', $email, $config['tawkto_api_key']);
}
return '';
}
2024-03-15 10:38:05 +07:00
public static function getBills($id = 0)
{
if (!$id) {
$id = User::getID();
if (!$id) {
return [];
}
}
$addcost = 0;
$bills = [];
$attrs = User::getAttributes('Bill', $id);
foreach ($attrs as $k => $v) {
// if has : then its an installment
if (strpos($v, ":") === false) {
// Not installment
$bills[$k] = $v;
$addcost += $v;
} else {
// installment
list($cost, $rem) = explode(":", $v);
// :0 installment is done
2024-03-15 10:48:17 +07:00
if (!empty($rem)) {
2024-03-15 10:38:05 +07:00
$bills[$k] = $cost;
$addcost += $cost;
}
}
}
return [$bills, $addcost];
}
public static function getBillNames($id = 0)
{
if (!$id) {
$id = User::getID();
if (!$id) {
return [];
}
}
$bills = [];
$attrs = User::getAttributes('Bill', $id);
foreach ($attrs as $k => $v) {
$bills[] = str_replace(' Bill', '', $k);
}
return $bills;
}
2024-03-15 10:38:05 +07:00
public static function billsPaid($bills, $id = 0)
{
if (!$id) {
$id = User::getID();
if (!$id) {
return [];
}
}
foreach ($bills as $k => $v) {
// if has : then its an installment
$v = User::getAttribute($k, $id);
if (strpos($v, ":") === false) {
// Not installment, no need decrement
} else {
// installment
list($cost, $rem) = explode(":", $v);
// :0 installment is done
if ($rem != 0) {
User::setAttribute($k, "$cost:" . ($rem - 1), $id);
2024-03-15 10:38:05 +07:00
}
}
}
}
2024-03-14 11:38:32 +07:00
public static function setAttribute($name, $value, $id = 0)
2024-03-13 14:32:10 +07:00
{
if (!$id) {
$id = User::getID();
2024-03-14 11:38:32 +07:00
if (!$id) {
return '';
}
}
$f = ORM::for_table('tbl_customers_fields')->where('field_name', $name)->where('customer_id', $id)->find_one();
2024-03-15 10:38:05 +07:00
if (!$f) {
2024-03-14 11:38:32 +07:00
$f = ORM::for_table('tbl_customers_fields')->create();
$f->customer_id = $id;
$f->field_name = $name;
$f->field_value = $value;
$f->save();
$result = $f->id();
if ($result) {
return $result;
}
2024-03-15 10:38:05 +07:00
} else {
2024-03-14 11:38:32 +07:00
$f->field_value = $value;
$f->save();
return $f['id'];
2024-03-13 14:32:10 +07:00
}
2024-03-14 11:38:32 +07:00
return 0;
}
public static function getAttribute($name, $id = 0, $default = '')
2024-03-14 11:38:32 +07:00
{
2024-03-13 14:32:10 +07:00
if (!$id) {
2024-03-14 11:38:32 +07:00
$id = User::getID();
if (!$id) {
return [];
}
2024-03-13 14:32:10 +07:00
}
$f = ORM::for_table('tbl_customers_fields')->where('field_name', $name)->where('customer_id', $id)->find_one();
if ($f) {
return $f['field_value'];
}
return $default;
2024-03-13 14:32:10 +07:00
}
public static function getAttributes($endWith, $id = 0)
{
if (!$id) {
$id = User::getID();
2024-03-14 11:38:32 +07:00
if (!$id) {
return [];
}
2024-03-13 14:32:10 +07:00
}
$attrs = [];
2024-03-14 11:38:32 +07:00
$f = ORM::for_table('tbl_customers_fields')->where_like('field_name', "%$endWith")->where('customer_id', $id)->find_many();
2024-03-13 14:32:10 +07:00
if ($f) {
foreach ($f as $k) {
$attrs[$k['field_name']] = $k['field_value'];
}
return $attrs;
}
return [];
}
2024-11-04 13:57:28 +07:00
public static function generateToken($uid, $validDays = 30)
2024-11-04 13:54:45 +07:00
{
global $db_pass;
2025-02-07 15:18:50 +07:00
if ($validDays >= 30) {
2024-11-04 13:57:28 +07:00
$time = time();
2025-02-07 15:18:50 +07:00
} else {
// for customer, deafult expired is 30 days
2025-02-07 15:18:50 +07:00
$time = strtotime('+ ' . (30 - $validDays) . ' days');
2024-11-04 13:57:28 +07:00
}
2024-11-04 13:54:45 +07:00
return [
'time' => $time,
'token' => $uid . '.' . $time . '.' . sha1($uid . '.' . $time . '.' . $db_pass)
];
}
2024-03-13 14:32:10 +07:00
public static function setCookie($uid)
{
2024-07-29 09:06:27 +07:00
global $db_pass;
2024-03-13 14:32:10 +07:00
if (isset($uid)) {
2024-11-05 10:42:41 +07:00
$token = self::generateToken($uid);
2025-02-07 15:18:50 +07:00
setcookie('uid', $token['token'], time() + 86400 * 30, "/");
2024-11-04 13:54:45 +07:00
return $token;
} else {
return false;
2024-02-12 09:45:44 +07:00
}
}
2024-03-13 14:32:10 +07:00
public static function removeCookie()
{
if (isset($_COOKIE['uid'])) {
2025-02-07 15:18:50 +07:00
setcookie('uid', '', time() - 86400, "/");
2024-02-12 09:45:44 +07:00
}
}
2024-03-12 15:09:00 +07:00
public static function _info($id = 0)
2023-06-15 15:26:38 +07:00
{
global $config;
if ($config['maintenance_mode'] == true) {
if ($config['maintenance_mode_logout'] == true) {
2025-01-31 16:22:58 +07:00
r2(getUrl('logout'), 'd', '');
} else {
displayMaintenanceMessage();
}
}
2024-03-13 14:32:10 +07:00
if (!$id) {
2024-03-12 15:09:00 +07:00
$id = User::getID();
}
2017-03-11 02:51:06 +07:00
$d = ORM::for_table('tbl_customers')->find_one($id);
2024-05-17 19:05:16 +07:00
if ($d['status'] == 'Banned') {
_alert(Lang::T('This account status') . ' : ' . Lang::T($d['status']), 'danger', "logout");
}
return $d;
}
public static function _infoByName($username)
{
global $config;
if ($config['maintenance_mode'] == true) {
if ($config['maintenance_mode_logout'] == true) {
2025-01-31 16:22:58 +07:00
r2(getUrl('logout'), 'd', '');
} else {
displayMaintenanceMessage();
}
}
$d = ORM::for_table('tbl_customers')->where("username", $username)->find_one();
if ($d['status'] == 'Banned') {
_alert(Lang::T('This account status') . ' : ' . Lang::T($d['status']), 'danger', "logout");
}
2017-03-11 02:51:06 +07:00
return $d;
}
2023-06-15 15:26:38 +07:00
2024-11-04 13:54:45 +07:00
public static function isUserVoucher($kode)
{
$regex = '/^GC\d+C.{10}$/';
return preg_match($regex, $kode);
}
2024-03-13 14:32:10 +07:00
public static function _billing($id = 0)
2023-06-15 15:26:38 +07:00
{
2024-03-13 14:32:10 +07:00
if (!$id) {
$id = User::getID();
}
$d = ORM::for_table('tbl_user_recharges')
->select('tbl_user_recharges.id', 'id')
->selects([
2024-11-04 13:54:45 +07:00
'customer_id',
'username',
'plan_id',
'namebp',
'recharged_on',
'recharged_time',
'expiration',
'time',
'status',
'method',
'plan_type',
['tbl_user_recharges.routers', 'routers'],
['tbl_user_recharges.type', 'type'],
2024-11-04 13:54:45 +07:00
'admin_id',
'prepaid'
])
->left_outer_join('tbl_plans', ['tbl_plans.id', '=', 'tbl_user_recharges.plan_id'])
->left_outer_join('tbl_bandwidth', ['tbl_bandwidth.id', '=', 'tbl_plans.id_bw'])
->select('tbl_bandwidth.name_bw', 'name_bw')
->select('tbl_plans.price', 'price')
2024-03-13 14:32:10 +07:00
->where('customer_id', $id)
->find_many();
2017-03-11 02:51:06 +07:00
return $d;
}
2025-02-07 15:18:50 +07:00
public static function setFormCustomField($uid = 0)
{
global $UPLOAD_PATH;
$fieldPath = $UPLOAD_PATH . DIRECTORY_SEPARATOR . "customer_field.json";
2025-02-07 15:18:50 +07:00
if (!file_exists($fieldPath)) {
return '';
}
$fields = json_decode(file_get_contents($fieldPath), true);
2025-02-07 15:18:50 +07:00
foreach ($fields as $field) {
if (!empty(_post($field['name']))) {
self::setAttribute($field['name'], _post($field['name']), $uid);
}
}
}
2025-02-07 15:18:50 +07:00
public static function getFormCustomField($ui, $register = false, $uid = 0)
{
global $UPLOAD_PATH;
$fieldPath = $UPLOAD_PATH . DIRECTORY_SEPARATOR . "customer_field.json";
2025-02-07 15:18:50 +07:00
if (!file_exists($fieldPath)) {
return '';
}
$fields = json_decode(file_get_contents($fieldPath), true);
$attrs = [];
2025-02-07 15:18:50 +07:00
if (!$register) {
$attrs = self::getAttributes('', $uid);
$ui->assign('attrs', $attrs);
}
$html = '';
$ui->assign('register', $register);
2025-02-07 15:18:50 +07:00
foreach ($fields as $field) {
if ($register) {
if ($field['register']) {
$ui->assign('field', $field);
$html .= $ui->fetch('customer/custom_field.tpl');
}
2025-02-07 15:18:50 +07:00
} else {
$ui->assign('field', $field);
$html .= $ui->fetch('customer/custom_field.tpl');
}
}
return $html;
}
public static function find($id)
{
return ORM::for_table('tbl_customers')->find_one($id);
}
2023-06-15 15:26:38 +07:00
}