Merge pull request 'Upload files to "system/autoload"' (#2) from master into v1.0.1

Reviewed-on: #2
This commit is contained in:
nestict 2025-05-24 11:07:11 +02:00
commit 61f8acd6a5
3 changed files with 575 additions and 0 deletions

202
system/autoload/User.php Normal file
View File

@ -0,0 +1,202 @@
<?php
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
**/
class User
{
public static function getID()
{
global $db_password;
if (isset($_SESSION['uid']) && !empty($_SESSION['uid'])) {
return $_SESSION['uid'];
} else if (isset($_COOKIE['uid'])) {
// id.time.sha1
$tmp = explode('.', $_COOKIE['uid']);
if (sha1($tmp[0] . '.' . $tmp[1] . '.' . $db_password) == $tmp[2]) {
if (time() - $tmp[1] < 86400 * 30) {
$_SESSION['uid'] = $tmp[0];
return $tmp[0];
}
}
}
return 0;
}
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
if (!empty($rem)) {
$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);
}
}
}
}
public static function setAttribute($name, $value, $id = 0)
{
if (!$id) {
$id = User::getID();
if (!$id) {
return '';
}
}
$f = ORM::for_table('tbl_customers_fields')->where('field_name', $name)->where('customer_id', $id)->find_one();
if (!$f) {
$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;
}
} else {
$f->field_value = $value;
$f->save();
return $f['id'];
}
return 0;
}
public static function getAttribute($name, $id = 0)
{
if (!$id) {
$id = User::getID();
if (!$id) {
return [];
}
}
$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();
if (!$id) {
return [];
}
}
$attrs = [];
$f = ORM::for_table('tbl_customers_fields')->where_like('field_name', "%$endWith")->where('customer_id', $id)->find_many();
if ($f) {
foreach ($f as $k) {
$attrs[$k['field_name']] = $k['field_value'];
}
return $attrs;
}
return [];
}
public static function setCookie($uid)
{
global $db_password;
if (isset($uid)) {
$time = time();
setcookie('uid', $uid . '.' . $time . '.' . sha1($uid . '.' . $time . '.' . $db_password), time() + 86400 * 30);
}
}
public static function removeCookie()
{
if (isset($_COOKIE['uid'])) {
setcookie('uid', '', time() - 86400);
}
}
public static function _info($id = 0)
{
global $config;
if ($config['maintenance_mode'] == true) {
if ($config['maintenance_mode_logout'] == true) {
r2(U . 'logout', 'd', '');
} else {
displayMaintenanceMessage();
}
}
if (!$id) {
$id = User::getID();
}
$d = ORM::for_table('tbl_customers')->find_one($id);
if ($d['status'] == 'Banned') {
_alert(Lang::T('This account status') . ' : ' . Lang::T($d['status']), 'danger', "logout");
}
if (empty($d['username'])) {
r2(U . 'logout', 'd', '');
}
return $d;
}
public static function _billing($id = 0)
{
if (!$id) {
$id = User::getID();
}
$d = ORM::for_table('tbl_user_recharges')
->select('tbl_user_recharges.id', 'id')
->selects([
'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'],
'admin_id', 'prepaid'
])
->where('customer_id', $id)
->left_outer_join('tbl_plans', array('tbl_plans.id', '=', 'tbl_user_recharges.plan_id'))
->find_many();
return $d;
}
}

View File

@ -0,0 +1,323 @@
<?php
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
**/
/**
* Validator class
*/
class Validator
{
/**
* String text finder
*
* @access private
* @param string $string
* @param array $hits
* @return void
*/
public static function textHit($string, $exclude = "")
{
if (empty($exclude)) return false;
if (is_array($exclude)) {
foreach ($exclude as $text) {
if (strstr($string, $text)) return true;
}
} else {
if (strstr($string, $exclude)) return true;
}
return false;
}
/**
* Number compare
*
* @access private
* @param int $integer
* @param int $max
* @param int $min
* @return bool
*/
private static function numberBetween($integer, $max = null, $min = 0)
{
if (is_numeric($min) && $integer <= $min) return false;
if (is_numeric($max) && $integer >= $max) return false;
return true;
}
/**
* Email addres check
*
* @access public
* @param string $string
* @param array $exclude
* @return bool
*/
public static function Email($string, $exclude = "")
{
if (self::textHit($string, $exclude)) return false;
return (bool)preg_match("/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i", $string);
}
/**
* URL check
*
* @access public
* @param strin $string
* @return bool
*/
public static function Url($string, $exclude = "")
{
if (self::textHit($string, $exclude)) return false;
return (bool)preg_match("/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i", $string);
}
/**
* IP
*
* @access public
* @param string $string
* @return void
*/
public static function Ip($string)
{
return (bool)preg_match("/^(1?\d{1,2}|2([0-4]\d|5[0-5]))(\.(1?\d{1,2}|2([0-4]\d|5[0-5]))){3}$/", $string);
}
/**
* Check if it is an number
*
* @access public
* @param int $integer
* @param int $max
* @param int $min
* @return bool
*/
public static function Number($integer, $max = null, $min = 0)
{
if (preg_match("/^\-?\+?[0-9e1-9]+$/", $integer)) {
if (!self::numberBetween($integer, $max, $min)) return false;
return true;
}
return false;
}
/**
* Check if it is an unsigned number
*
* @access public
* @param int $integer
* @return bool
*/
public static function UnsignedNumber($integer)
{
return (bool)preg_match("/^\+?[0-9]+$/", $integer);
}
/**
* Float
*
* @access public
* @param string $string
* @return bool
*/
public static function Float($string)
{
return (bool)($string == strval(floatval($string))) ? true : false;
}
/**
* Alpha check
*
* @access public
* @param string $string
* @return void
*/
public static function Alpha($string)
{
return (bool)preg_match("/^[a-zA-Z]+$/", $string);
}
/**
* Alpha numeric check
*
* @access public
* @param string $string
* @return void
*/
public static function AlphaNumeric($string)
{
return (bool)preg_match("/^[0-9a-zA-Z]+$/", $string);
}
/**
* Specific chars check
*
* @access public
* @param string $string
* @param array $allowed
* @return void
*/
public static function Chars($string, $allowed = array("a-z"))
{
return (bool)preg_match("/^[" . implode("", $allowed) . "]+$/", $string);
}
/**
* Check length of an string
*
* @access public
* @param string $stirng
* @param int $max
* @param int $min
* @return bool
*/
public static function Length($string, $max = null, $min = 0)
{
$length = strlen($string);
if (!self::numberBetween($length, $max, $min)) return false;
return true;
}
/**
* Hex color check
*
* @access public
* @param string $string
* @return void
*/
public static function HexColor($string)
{
return (bool)preg_match("/^(#)?([0-9a-f]{1,2}){3}$/i", $string);
}
/**
* Data validation
*
* Does'nt matter how you provide the date
* dd/mm/yyyy
* dd-mm-yyyy
* yyyy/mm/dd
* yyyy-mm-dd
*
* @access public
* @param string $string
* @return bool
*/
public static function Date($string)
{
$date = date('Y', strtotime($string));
return ($date == "1970" || $date == '') ? false : true;
}
/**
* Older than check
*
* @access public
* @param string $string
* @param int $age
* @return bool
*/
public static function OlderThan($string, $age)
{
$date = date('Y', strtotime($string));
if ($date == "1970" || $date == '') return false;
return (date('Y') - $date) > $age ? true : false;
}
/**
* XML valid
*
* @access public
* @param string $string
* @return bool
*/
public static function Xml($string)
{
$Xml = @simplexml_load_string($string);
return ($Xml === false) ? false : true;
}
/**
* Is filesize between
*
* @access public
* @param string $file
* @param int $max
* @param int $min
* @return bool
*/
public static function FilesizeBetween($file, $max = null, $min = 0)
{
$filesize = filesize($file);
return self::numberBetween($filesize, $max, $min);
}
/**
* Is image width between
*
* @access public
* @param string $image
* @param int $max_width
* @param int $min_width
* @param int $max_height
* @param int $min_height
* @return void
*/
public static function ImageSizeBetween($image, $max_width = "", $min_width = 0, $max_height = "", $min_height = 0)
{
$size = getimagesize($image);
if (!self::numberBetween($size[0], $max_width, $min_width)) return false;
if (!self::numberBetween($size[1], $max_height, $min_height)) return false;
return true;
}
/**
* Phone numbers
*
* @access public
* @param string $phone
* @return bool
*/
public static function Phone($phone)
{
$formats = array(
'###-###-####',
'####-###-###',
'(###) ###-###',
'####-####-####',
'##-###-####-####',
'####-####',
'###-###-###',
'#####-###-###',
'##########',
'####-##-##-##'
);
$format = trim(preg_replace("/[0-9]/", "#", $phone));
return (bool)in_array($format, $formats);
}
public static function countRouterPlan($plans, $router){
$n = 0;
foreach ($plans as $plan){
if($plan['routers'] == $router){
$n++;
}
}
return $n;
}
public static function isRouterHasPlan($plans, $router){
foreach ($plans as $plan){
if($plan['routers'] == $router){
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,50 @@
<?php
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
**/
/**
* Validator class
*/
class Widget
{
public static function rows($rows, $result){
$result .= '<div class="row">';
foreach($rows as $row){
}
$result .= '</div>';
}
public static function columns($cols, $result){
$c = count($cols);
switch($c){
case 1:
$result .= '<div class="col-md-12">';
break;
case 2:
$result .= '<div class="col-md-6">';
break;
case 3:
$result .= '<div class="col-md-4">';
break;
case 4:
$result .= '<div class="col-md-4">';
break;
case 5:
$result .= '<div class="col-md-4">';
break;
default:
$result .= '<div class="col-md-1">';
break;
}
foreach($cols as $col){
}
$result .= '</div>';
}
}