phpnuxbill/system/autoload/User.php

201 lines
5.7 KiB
PHP
Raw Normal View History

2017-03-11 03:51:06 +08:00
<?php
2023-06-15 16:26:38 +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-06-15 16:26:38 +08:00
**/
2017-03-11 03:51:06 +08:00
2023-10-12 16:55:42 +08:00
2023-06-15 16:26:38 +08:00
class User
{
2024-03-13 15:32:10 +08:00
public static function getID()
{
2024-02-12 10:45:44 +08:00
global $db_password;
2024-03-13 15:32:10 +08:00
if (isset($_SESSION['uid']) && !empty($_SESSION['uid'])) {
2024-02-12 10:45:44 +08:00
return $_SESSION['uid'];
2024-03-13 15:32:10 +08:00
} else if (isset($_COOKIE['uid'])) {
2024-02-12 10:45:44 +08:00
// id.time.sha1
2024-03-13 15:32:10 +08:00
$tmp = explode('.', $_COOKIE['uid']);
if (sha1($tmp[0] . '.' . $tmp[1] . '.' . $db_password) == $tmp[2]) {
if (time() - $tmp[1] < 86400 * 30) {
2024-02-12 10:45:44 +08:00
$_SESSION['uid'] = $tmp[0];
return $tmp[0];
}
}
}
return 0;
}
2024-03-15 11:38:05 +08: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 11:48:17 +08:00
if (!empty($rem)) {
2024-03-15 11:38:05 +08:00
$bills[$k] = $cost;
$addcost += $cost;
}
}
}
return [$bills, $addcost];
}
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-14 12:38:32 +08:00
public static function setAttribute($name, $value, $id = 0)
2024-03-13 15:32:10 +08:00
{
if (!$id) {
$id = User::getID();
2024-03-14 12:38:32 +08:00
if (!$id) {
return '';
}
}
$f = ORM::for_table('tbl_customers_fields')->where('field_name', $name)->where('customer_id', $id)->find_one();
2024-03-15 11:38:05 +08:00
if (!$f) {
2024-03-14 12:38:32 +08: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 11:38:05 +08:00
} else {
2024-03-14 12:38:32 +08:00
$f->field_value = $value;
$f->save();
return $f['id'];
2024-03-13 15:32:10 +08:00
}
2024-03-14 12:38:32 +08:00
return 0;
}
public static function getAttribute($name, $id = 0)
{
2024-03-13 15:32:10 +08:00
if (!$id) {
2024-03-14 12:38:32 +08:00
$id = User::getID();
if (!$id) {
return [];
}
2024-03-13 15:32:10 +08: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 '';
}
public static function getAttributes($endWith, $id = 0)
{
if (!$id) {
$id = User::getID();
2024-03-14 12:38:32 +08:00
if (!$id) {
return [];
}
2024-03-13 15:32:10 +08:00
}
$attrs = [];
2024-03-14 12:38:32 +08:00
$f = ORM::for_table('tbl_customers_fields')->where_like('field_name', "%$endWith")->where('customer_id', $id)->find_many();
2024-03-13 15:32:10 +08:00
if ($f) {
foreach ($f as $k) {
$attrs[$k['field_name']] = $k['field_value'];
}
return $attrs;
}
return [];
}
public static function setCookie($uid)
{
2024-02-12 10:45:44 +08:00
global $db_password;
2024-03-13 15:32:10 +08:00
if (isset($uid)) {
2024-02-12 10:45:44 +08:00
$time = time();
2024-03-13 15:32:10 +08:00
setcookie('uid', $uid . '.' . $time . '.' . sha1($uid . '.' . $time . '.' . $db_password), time() + 86400 * 30);
2024-02-12 10:45:44 +08:00
}
}
2024-03-13 15:32:10 +08:00
public static function removeCookie()
{
if (isset($_COOKIE['uid'])) {
setcookie('uid', '', time() - 86400);
2024-02-12 10:45:44 +08:00
}
}
2024-03-12 16:09:00 +08:00
public static function _info($id = 0)
2023-06-15 16:26:38 +08:00
{
2024-03-13 15:32:10 +08:00
if (!$id) {
2024-03-12 16:09:00 +08:00
$id = User::getID();
}
2017-03-11 03:51:06 +08:00
$d = ORM::for_table('tbl_customers')->find_one($id);
2024-03-13 15:32:10 +08:00
if (empty($d['username'])) {
r2(U . 'logout', 'd', '');
}
2017-03-11 03:51:06 +08:00
return $d;
}
2023-06-15 16:26:38 +08:00
2024-03-13 15:32:10 +08:00
public static function _billing($id = 0)
2023-06-15 16:26:38 +08:00
{
2024-03-13 15:32:10 +08:00
if (!$id) {
$id = User::getID();
}
$d = ORM::for_table('tbl_user_recharges')
->select('tbl_user_recharges.id', 'id')
->select('customer_id')
->select('username')
->select('plan_id')
->select('namebp')
->select('recharged_on')
->select('recharged_time')
->select('expiration')
->select('time')
->select('status')
->select('method')
->select('plan_type')
2024-03-13 15:32:10 +08:00
->select('tbl_user_recharges.routers', 'routers')
->select('tbl_user_recharges.type', 'type')
->select('admin_id')
->select('prepaid')
->where('customer_id', $id)
->join('tbl_plans', array('tbl_plans.id', '=', 'tbl_user_recharges.plan_id'))
->find_many();
2017-03-11 03:51:06 +08:00
return $d;
}
2023-06-15 16:26:38 +08:00
}