2022-09-10 17:08:10 +08:00
< ? php
/**
2023-10-12 16:55:42 +08:00
* PHP Mikrotik Billing ( https :// github . com / hotspotbilling / phpnuxbill / )
* by https :// t . me / ibnux
2022-09-10 17:08:10 +08:00
**/
2023-10-12 16:55:42 +08:00
2023-08-09 11:49:29 +08:00
class Lang
{
2024-02-13 14:54:01 +08:00
public static function T ( $key )
2023-08-09 11:49:29 +08:00
{
2024-02-13 14:54:01 +08:00
global $_L , $lan_file , $config ;
2024-02-19 10:52:36 +08:00
$_L = $_SESSION [ 'Lang' ];
2024-02-21 12:54:22 +08:00
$key = preg_replace ( '/\s+/' , ' ' , $key );
2024-02-13 14:54:01 +08:00
if ( ! empty ( $_L [ $key ])) {
return $_L [ $key ];
}
$val = $key ;
2024-02-19 10:52:36 +08:00
$key = Lang :: sanitize ( $key );
2024-02-13 18:41:55 +08:00
if ( isset ( $_L [ $key ])) {
return $_L [ $key ];
2024-02-19 15:24:34 +08:00
} else if ( isset ( $_L [ $key ])) {
2024-02-13 14:54:01 +08:00
return $_L [ $key ];
} else {
$iso = Lang :: getIsoLang ()[ $config [ 'language' ]];
2024-02-19 15:24:34 +08:00
if ( empty ( $iso )) {
2024-02-13 18:41:55 +08:00
return $val ;
}
2024-02-19 15:24:34 +08:00
if ( ! empty ( $iso ) && ! empty ( $val )) {
2024-02-13 14:54:01 +08:00
$temp = Lang :: translate ( $val , $iso );
2024-02-19 15:24:34 +08:00
if ( ! empty ( $temp )) {
2024-02-13 14:54:01 +08:00
$val = $temp ;
}
}
$_L [ $key ] = $val ;
$_SESSION [ 'Lang' ][ $key ] = $val ;
2024-02-13 18:41:55 +08:00
file_put_contents ( $lan_file , json_encode ( $_SESSION [ 'Lang' ], JSON_PRETTY_PRINT ));
2024-02-13 14:54:01 +08:00
return $val ;
}
}
2024-02-19 15:24:34 +08:00
public static function sanitize ( $str )
{
2024-02-19 10:52:36 +08:00
return preg_replace ( " /[^A-Za-z0-9]/ " , '_' , $str );;
}
2024-02-19 15:24:34 +08:00
public static function getIsoLang ()
{
2024-02-13 14:54:01 +08:00
global $isolang ;
2024-02-19 15:24:34 +08:00
if ( empty ( $isolang ) || count ( $isolang ) == 0 ) {
$isolang = json_decode ( file_get_contents ( File :: pathFixer ( " system/lan/country.json " )), true );
2024-02-13 14:54:01 +08:00
}
return $isolang ;
2022-09-10 17:08:10 +08:00
}
2023-03-06 15:51:05 +08:00
2023-08-09 11:49:29 +08:00
public static function htmlspecialchars ( $var )
{
2023-03-06 15:51:05 +08:00
return htmlspecialchars ( $var );
}
2023-08-09 11:49:29 +08:00
public static function moneyFormat ( $var )
{
global $config ;
2023-10-04 18:07:13 +08:00
return $config [ 'currency_code' ] . ' ' . number_format ( $var , 0 , $config [ 'dec_point' ], $config [ 'thousands_sep' ]);
2023-08-09 11:49:29 +08:00
}
2023-08-09 15:54:38 +08:00
public static function phoneFormat ( $phone )
{
global $config ;
2023-10-04 18:07:13 +08:00
if ( Validator :: UnsignedNumber ( $phone ) && ! empty ( $config [ 'country_code_phone' ])) {
2023-08-09 15:54:38 +08:00
return preg_replace ( '/^0/' , $config [ 'country_code_phone' ], $phone );
2023-10-04 18:07:13 +08:00
} else {
2023-08-09 15:54:38 +08:00
return $phone ;
}
}
2023-08-21 18:09:44 +08:00
2023-10-04 18:07:13 +08:00
public static function dateFormat ( $date )
{
2023-08-21 18:09:44 +08:00
global $config ;
return date ( $config [ 'date_format' ], strtotime ( $date ));
}
2023-10-04 18:07:13 +08:00
public static function dateTimeFormat ( $date )
{
2023-08-21 18:09:44 +08:00
global $config ;
2023-10-04 18:07:13 +08:00
if ( strtotime ( $date ) < strtotime ( " 2000-01-01 00:00:00 " )) {
2023-09-01 10:16:40 +08:00
return " " ;
2023-10-04 18:07:13 +08:00
} else {
return date ( $config [ 'date_format' ] . ' H:i' , strtotime ( $date ));
2023-09-01 10:16:40 +08:00
}
2023-08-21 18:09:44 +08:00
}
2023-10-04 18:07:13 +08:00
public static function dateAndTimeFormat ( $date , $time )
{
2023-08-24 16:12:31 +08:00
global $config ;
2023-10-04 18:07:13 +08:00
return date ( $config [ 'date_format' ] . ' H:i' , strtotime ( " $date $time " ));
2023-08-24 16:12:31 +08:00
}
2024-02-19 15:24:34 +08:00
public static function timeElapsed ( $datetime , $full = false )
{
$now = new DateTime ;
$ago = new DateTime ( $datetime );
$diff = $now -> diff ( $ago );
$diff -> w = floor ( $diff -> d / 7 );
$diff -> d -= $diff -> w * 7 ;
$string = array (
'y' => Lang :: T ( 'year' ),
'm' => Lang :: T ( 'month' ),
'w' => Lang :: T ( 'week' ),
'd' => Lang :: T ( 'day' ),
'h' => Lang :: T ( 'hour' ),
'i' => Lang :: T ( 'minute' ),
's' => Lang :: T ( 'second' ),
);
foreach ( $string as $k => & $v ) {
if ( $diff -> $k ) {
$v = $diff -> $k . ' ' . $v . ( $diff -> $k > 1 ? 's' : '' );
} else {
unset ( $string [ $k ]);
2024-02-07 14:32:33 +08:00
}
2024-02-19 15:24:34 +08:00
}
if ( ! $full )
$string = array_slice ( $string , 0 , 1 );
return $string ? implode ( ', ' , $string ) . ' ago' : 'just now' ;
2024-02-07 14:32:33 +08:00
}
2023-10-04 18:07:13 +08:00
public static function nl2br ( $text )
{
2023-08-21 18:09:44 +08:00
return nl2br ( $text );
}
2023-10-04 18:07:13 +08:00
public static function arrayCount ( $arr )
{
if ( is_array ( $arr )) {
return count ( $arr );
} else if ( is_object ( $arr )) {
2023-09-26 14:50:02 +08:00
return count ( $arr );
2023-10-04 18:07:13 +08:00
} else {
2023-09-26 14:50:02 +08:00
return 0 ;
}
2023-08-21 18:09:44 +08:00
}
2023-08-24 12:35:23 +08:00
2023-10-04 18:07:13 +08:00
public static function getNotifText ( $key )
{
2023-08-24 12:35:23 +08:00
global $_notifmsg , $_notifmsg_default ;
2023-10-04 18:07:13 +08:00
if ( isset ( $_notifmsg [ $key ])) {
2023-08-24 12:35:23 +08:00
return $_notifmsg [ $key ];
2023-10-04 18:07:13 +08:00
} else {
2023-08-24 12:35:23 +08:00
return $_notifmsg_default [ $key ];
}
}
2023-10-12 17:15:50 +08:00
public static function ucWords ( $text )
{
return ucwords ( str_replace ( '_' , ' ' , $text ));
}
2023-10-18 18:23:47 +08:00
2024-02-19 15:24:34 +08:00
public static function randomUpLowCase ( $text )
{
2023-10-18 18:23:47 +08:00
$jml = strlen ( $text );
$result = '' ;
2024-02-19 15:24:34 +08:00
for ( $i = 0 ; $i < $jml ; $i ++ ) {
if ( rand ( 0 , 99 ) % 2 ) {
$result .= strtolower ( substr ( $text , $i , 1 ));
} else {
$result .= substr ( $text , $i , 1 );
2023-10-18 18:23:47 +08:00
}
}
return $result ;
}
2024-01-16 11:32:59 +08:00
/**
* $pad_type
* 0 Left
* 1 right
* 2 center
* */
2024-02-19 15:24:34 +08:00
public static function pad ( $text , $pad_string = ' ' , $pad_type = 0 )
{
2024-01-16 11:32:59 +08:00
global $config ;
$cols = 37 ;
2024-02-19 15:24:34 +08:00
if ( $config [ 'printer_cols' ]) {
2024-01-16 11:32:59 +08:00
$cols = $config [ 'printer_cols' ];
}
2024-01-16 12:41:12 +08:00
$text = trim ( $text );
$texts = explode ( " \n " , $text );
2024-02-19 15:24:34 +08:00
if ( count ( $texts ) > 1 ) {
2024-01-16 12:41:12 +08:00
$text = '' ;
2024-02-19 15:24:34 +08:00
foreach ( $texts as $t ) {
$text .= self :: pad ( trim ( $t ), $pad_string , $pad_type ) . " \n " ;
2024-01-16 12:41:12 +08:00
}
return $text ;
2024-02-19 15:24:34 +08:00
} else {
2024-01-16 12:41:12 +08:00
return str_pad ( trim ( $text ), $cols , $pad_string , $pad_type );
}
2024-01-16 11:32:59 +08:00
}
2024-02-19 15:24:34 +08:00
public static function pads ( $textLeft , $textRight , $pad_string = ' ' )
{
2024-01-16 11:32:59 +08:00
global $config ;
$cols = 37 ;
2024-02-19 15:24:34 +08:00
if ( $config [ 'printer_cols' ]) {
2024-01-16 11:32:59 +08:00
$cols = $config [ 'printer_cols' ];
}
2024-02-19 15:24:34 +08:00
return $textLeft . str_pad ( $textRight , $cols - strlen ( $textLeft ), $pad_string , 0 );
2024-01-16 11:32:59 +08:00
}
2024-02-13 14:54:01 +08:00
2024-02-19 15:24:34 +08:00
public static function translate ( $txt , $to = 'id' )
{
2024-02-13 14:54:01 +08:00
$ch = curl_init ();
2024-02-19 15:24:34 +08:00
curl_setopt ( $ch , CURLOPT_URL , " https://translate.google.com/m?hl=en&sl=en&tl= $to &ie=UTF-8&prev=_m&q= " . urlencode ( $txt ));
2024-02-13 14:54:01 +08:00
curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 );
curl_setopt ( $ch , CURLOPT_USERAGENT , " Mozilla/5.0 (iPhone; CPU OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/28.1 Mobile/15E148 Safari/605.1.15 " );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT , 2 );
2024-02-15 17:13:55 +08:00
curl_setopt ( $ch , CURLOPT_TIMEOUT , 5 );
2024-02-19 15:24:34 +08:00
curl_setopt ( $ch , CURLOPT_HEADER , 0 );
$hasil = curl_exec ( $ch );
2024-02-13 14:54:01 +08:00
curl_close ( $ch );
$temp = explode ( '<div class="result-container">' , $hasil );
2024-02-19 15:24:34 +08:00
if ( count ( $temp ) > 0 ) {
2024-02-13 14:54:01 +08:00
$temp = explode ( " </div " , $temp [ 1 ]);
2024-02-19 15:24:34 +08:00
if ( ! empty ( $temp [ 0 ])) {
2024-02-13 14:54:01 +08:00
return $temp [ 0 ];
}
}
return $txt ;
}
2024-02-19 15:24:34 +08:00
public static function maskText ( $text ){
$len = strlen ( $text );
if ( $len < 3 ){
return " *** " ;
} else if ( $len < 5 ){
return substr ( $text , 0 , 1 ) . " *** " . substr ( $text , - 1 , 1 );
} else if ( $len < 8 ){
return substr ( $text , 0 , 2 ) . " *** " . substr ( $text , - 2 , 2 );
} else {
return substr ( $text , 0 , 4 ) . " ****** " . substr ( $text , - 3 , 3 );
}
}
2022-09-10 17:08:10 +08:00
}