adding user class
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
/*******************************************************************
|
||||
|
||||
Module : /App/Auth.php
|
||||
Created By : Siau Pheng ([email protected]).
|
||||
Created Date : March 16th, 2008.
|
||||
Last Modified : September 10th, 2026.
|
||||
|
||||
(c) 3FONIA Software; WWW.3FONIA.COM.
|
||||
|
||||
*******************************************************************/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Webapp\App;
|
||||
|
||||
use \flight\Engine;
|
||||
use \flight\Session;
|
||||
use \flight\database\SimplePdo;
|
||||
|
||||
/**
|
||||
* Class Auth
|
||||
*
|
||||
* Kontrol dan Manajemen User Session.
|
||||
*
|
||||
* @license MIT, https://www.3fonia.com
|
||||
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
||||
*
|
||||
* @package Webapp\App
|
||||
*/
|
||||
|
||||
final class Auth {
|
||||
private int $id_reg = 0;
|
||||
protected Engine $app;
|
||||
protected SimplePdo $db;
|
||||
protected Session $session;
|
||||
|
||||
/**
|
||||
* @param Engine $app
|
||||
*/
|
||||
public function __construct(Engine $app){
|
||||
$this->app = $app;
|
||||
$this->db = $app->db();
|
||||
$this->session = $app->session();
|
||||
$this->id_reg = (int) $this->session->get('WEB_APP', ['REG_ID'=>0])['REG_ID'];
|
||||
}
|
||||
|
||||
public function set_id_reg(int|string $id = 0): void {
|
||||
if ((int) $id > 0) $this->id_reg = (int) $id;
|
||||
}
|
||||
|
||||
public function user_check(string $user, string $pass): bool {
|
||||
$i_hasil = (int) $this->db->fetchField('SELECT COUNT(*) FROM app_operator WHERE Username=? AND Password=MD5(?) AND Aktif=1', [$user, $pass]);
|
||||
return ($i_hasil > 0 ? true : false);
|
||||
}
|
||||
|
||||
public function user_data(string $user): array {
|
||||
$adata = [ 'Grup_MenuID' => 0 ];
|
||||
$xdata = $this->db->fetchRow('SELECT *,DECODE(Menu,?) Menu2 FROM app_operator WHERE Username=?', [$this->id_reg, $user]);
|
||||
if ($xdata) {
|
||||
$adata = $xdata->getData();
|
||||
$adata['Menu'] = $adata['Menu2'];
|
||||
unset($adata['Menu2'], $adata['Password']);
|
||||
}
|
||||
|
||||
if (empty($this->session->get('APP_TOKEN'))) {
|
||||
$_token = $this->genToken(60);
|
||||
$this->session->set('APP_TOKEN', $_token);
|
||||
$this->db->insert('app_token', [ 'Token' => $_token, 'UserID' => $adata['UserID'], 'IP' => $this->app->request()->ip ]);
|
||||
}
|
||||
if ((int) $adata['Grup_MenuID'] > 0) $adata['Menu_Grup'] = $this->user_allmenu((int) $adata['Grup_MenuID']);
|
||||
$adata['UNIX'] = time();
|
||||
$adata['LOCALE'] = $this->session->get('WEB_APP', ['DEF_LOCALE'=>'id'])['DEF_LOCALE'];
|
||||
$adata['MENU_MODUL'] = [];
|
||||
return $adata;
|
||||
}
|
||||
|
||||
public function gen_module_menu(): void {
|
||||
$amodul = []; $aallow = []; $tallow = '';
|
||||
if ($this->session->get('WEB_APP', ['APP_MODULE'=>0])['APP_MODULE']) {
|
||||
$tallow = $this->db->fetchField('SELECT Modul_Akses FROM app_menu_grup WHERE Grup_MenuID=?', [$this->session->get('WEB_USER', ['Grup_MenuID'=>0])['Grup_MenuID']]);
|
||||
$aallow = explode(",", $tallow);
|
||||
|
||||
if (count($aallow) > 0) {
|
||||
$stat = $this->db->runQuery('SELECT * FROM (SELECT APP_MODULE,APP_PATH FROM app_config) A LEFT JOIN (SELECT Modul,Urut,Judul,Icon FROM app_menu WHERE Level=0) B ON (A.APP_MODULE=B.Modul) ORDER BY B.Urut');
|
||||
while ($row = $stat->fetch()) {
|
||||
if (!in_array($row['Modul'], $aallow) || $row['Modul']==$this->session->get('WEB_APP')['APP_MODULE']) continue;
|
||||
$amodul[] = $row;
|
||||
}
|
||||
}
|
||||
}
|
||||
$auser = $this->session->get('WEB_USER', []);
|
||||
$auser['MENU_MODUL'] = $amodul;
|
||||
$this->session->set('WEB_USER', $auser);
|
||||
}
|
||||
|
||||
public function user_allmenu(int $id_grup = 0): string {
|
||||
$allmenu = $this->db->fetchField('SELECT DECODE(Menu,?) MenuX FROM app_menu_grup WHERE Grup_MenuID=?', [$this->id_reg, $id_grup]);
|
||||
return $allmenu;
|
||||
}
|
||||
|
||||
public function user_check_level(string $user, int $level = 9): bool {
|
||||
$i_hasil = (int)$this->db->fetchField('SELECT COUNT(*) FROM app_operator WHERE Username=? AND Level<=? AND Aktif=1', [$user, $level]);
|
||||
return ($i_hasil > 0 ? true : false);
|
||||
}
|
||||
|
||||
public function user_log(): void {
|
||||
$_user_id = $this->session->get('WEB_USER')['UserID'] ?? 0;
|
||||
if ($_user_id === 0) return;
|
||||
$a_hasil = $this->db->fetchRow('SELECT * FROM app_logs WHERE UserID=? ORDER BY Last_Date DESC LIMIT 1', [$_user_id])->getData();
|
||||
if (count($a_hasil) > 0) {
|
||||
$this->db->update('app_operator', [ 'Last_IP' => $a_hasil['Last_IP'], 'Last_Date' => $a_hasil['Last_Date'] ], 'UserID=?', [$_user_id]);
|
||||
}
|
||||
$this->db->runQuery('INSERT IGNORE INTO app_logs SELECT UserID,Token,IP,DateStamp FROM app_token WHERE Token=?', [$this->session->get('APP_TOKEN')]);
|
||||
}
|
||||
|
||||
public function logout(): void {
|
||||
$this->db->delete('app_token', 'Token=?', [$this->session->get('APP_TOKEN')]);
|
||||
}
|
||||
|
||||
public function token_check(string $token): bool {
|
||||
$hasil = (bool) $this->db->fetchField('SELECT 1 FROM app_token WHERE Token=?', [$token ?? $this->session->get('APP_TOKEN')]);
|
||||
if ($hasil === true) $this->db->runQuery('UPDATE app_token SET DateStamp=NOW() WHERE Token=?', [$token ?? $this->session->get('APP_TOKEN')]);
|
||||
return $hasil;
|
||||
}
|
||||
|
||||
public function token_update(): void {
|
||||
$this->db->runQuery('UPDATE app_token SET DateStamp=NOW() WHERE Token=?', [$this->session->get('APP_TOKEN')]);
|
||||
}
|
||||
|
||||
public function redirect(string $appUrl): string {
|
||||
$source = parse_url($appUrl);
|
||||
$desti = "";
|
||||
$hasil = (bool)$this->db->fetchField('SELECT 1 FROM app_host WHERE Nama_Host=?', [$source['host']]);
|
||||
if ($hasil) $desti = $source['scheme'].$source['host'].$source['path'].'?'.'auth='.$this->session->get('APP_TOKEN');
|
||||
return $desti;
|
||||
}
|
||||
|
||||
private function genToken(int $length = 16): string {
|
||||
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
|
||||
}
|
||||
|
||||
public function getUserByToken(string $token): string {
|
||||
$hasil = $this->db->fetchField('SELECT Username FROM app_operator WHERE UserID IN (SELECT UserID FROM app_token WHERE Token=?) LIMIT 1', [$token ?? $this->session->get('APP_TOKEN')]);
|
||||
return $hasil;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*******************************************************************
|
||||
|
||||
Module : /App/Register.php
|
||||
Created By : Siau Pheng ([email protected]).
|
||||
Created Date : November 23th, 2011.
|
||||
Last Modified : September 10th, 2026.
|
||||
|
||||
(c) 3FONIA Software; WWW.3FONIA.COM.
|
||||
|
||||
*******************************************************************/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Webapp\App;
|
||||
|
||||
use \flight\Engine;
|
||||
use \flight\Session;
|
||||
use \flight\database\SimplePdo;
|
||||
|
||||
/**
|
||||
* Class Register
|
||||
*
|
||||
* Registrasi aplikasi.
|
||||
*
|
||||
* @license MIT, https://www.3fonia.com
|
||||
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
||||
*
|
||||
* @package Webapp\App
|
||||
*/
|
||||
|
||||
final class Register {
|
||||
public function __construct(){
|
||||
}
|
||||
|
||||
public static function reg_id(Session $session): string {
|
||||
$id = (string) $session->get('WEB_APP', ['REG_ID'=>''])['REG_ID'];
|
||||
return (substr($id,0,3).".".substr($id,3,3).".".substr($id,6));
|
||||
}
|
||||
|
||||
public static function reg_code(Session $session): string {
|
||||
$id = (string) $session->get('WEB_APP', ['REG_CODE'=>''])['REG_CODE'];
|
||||
return (substr($id,0,6)."-".substr($id,6,5)."-".substr($id,11,5)."-".substr($id,16,5)."-".substr($id,21,5)."-".substr($id,26,6));
|
||||
}
|
||||
|
||||
public static function reg_check(Session $session): bool {
|
||||
$webapp = $session->get('WEB_APP');
|
||||
return (bool) (isset($webapp['REG_STATUS']) && $webapp['REG_STATUS']) ?? false;
|
||||
}
|
||||
|
||||
public static function reg_status(SimplePdo $db, array $apps): array {
|
||||
$kode_db = (bool) $db->fetchField('SELECT UPPER(CONVERT(MD5(CONCAT(REG_YEAR,CONVERT(MD5(CONCAT(REG_ID,REG_NAME)) using latin1),REG_ID,REG_NAME)) using latin1))=REG_CODE FROM app_config WHERE REG_ID=?', [$apps['APP_CODE'] ?? '']);
|
||||
return [ 'REG_STATUS' => $kode_db === true ];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/*******************************************************************
|
||||
|
||||
Module : /App/Update.php
|
||||
Created By : Siau Pheng ([email protected]).
|
||||
Created Date : June 14th, 2013.
|
||||
Last Modified : August 22nd, 2026.
|
||||
|
||||
(c) 3FONIA Software; WWW.3FONIA.COM.
|
||||
|
||||
*******************************************************************/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Webapp\App;
|
||||
|
||||
use \flight\Engine;
|
||||
|
||||
/**
|
||||
* Class Update
|
||||
*
|
||||
* Update Otomatis Aplikasi berbasis GIT.
|
||||
*
|
||||
* @license MIT, https://www.3fonia.com
|
||||
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
||||
*
|
||||
* @package Webapp\App
|
||||
*/
|
||||
|
||||
final class Update {
|
||||
private Array $__conf = [];
|
||||
private Bool $is_git = false;
|
||||
protected Engine $app;
|
||||
|
||||
/**
|
||||
* @param Engine $app
|
||||
*/
|
||||
public function __construct(Engine $app){
|
||||
$this->app = $app;
|
||||
$this->__conf = $app->session()->get('WEB_APP');
|
||||
$this->current();
|
||||
}
|
||||
|
||||
public function current(): void {
|
||||
if (defined('PROJECT_ROOT')) chdir(PROJECT_ROOT);
|
||||
try {
|
||||
$output = shell_exec("git log -1 --pretty=format:\"%H||%s||%ci\" --abbrev-commit 2>&1");
|
||||
if ($output) {
|
||||
$_ainfo = explode("||", $output);
|
||||
$this->__conf['GIT_SHA1'] = $_ainfo[0] ?? '';
|
||||
$this->__conf['GIT_DATE'] = $_ainfo[2] ?? '';
|
||||
$this->is_git = true;
|
||||
}
|
||||
$output = shell_exec("git config --get remote.origin.url 2>&1");
|
||||
if ($output) {
|
||||
$output = str_replace(["http://","https://",".git"], "", $output);
|
||||
$_ainfo = explode("/", trim($output));
|
||||
$this->__conf['GIT_OWNER'] = $_ainfo[1] ?? '';
|
||||
$this->__conf['GIT_REPO'] = $_ainfo[2] ?? '';
|
||||
}
|
||||
$output = shell_exec("git branch --show-current 2>&1");
|
||||
if ($output) {
|
||||
$this->__conf['GIT_BRANCH'] = $output;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
public function check(): array {
|
||||
$_ainfo['cur_version'] = $this->__conf['APP_BUILD'];
|
||||
$_ainfo['cur_sha'] = substr($this->__conf['GIT_SHA1'], 0, 10);
|
||||
$_ainfo['cur_sha1'] = $this->__conf['GIT_SHA1'];
|
||||
$_ainfo['cur_date'] = $this->__conf['GIT_DATE'];
|
||||
$_ainfo['svr_status'] = 0;
|
||||
$_ainfo['svr_message'] = "Server Offline";
|
||||
$_ainfo['new_sha'] = 'check git config';
|
||||
$_ainfo['new_sha1'] = $this->__conf['GIT_SHA1'];
|
||||
$_ainfo['new_date'] = '';
|
||||
$_ainfo['new_message'] = 'check git config';
|
||||
|
||||
if (!$this->is_git || empty($this->__conf['GIT_OWNER']) || empty($this->__conf['GIT_REPO'])) return $_ainfo;
|
||||
|
||||
try {
|
||||
$_url = "https://git.trifonia.cloud/api/v1/repos/".$this->__conf['GIT_OWNER']."/".$this->__conf['GIT_REPO']."/commits?limit=1";
|
||||
$_conten = file_get_contents($_url);
|
||||
$ajson = json_decode($_conten, true);
|
||||
|
||||
if (count($ajson) > 0) {
|
||||
$_ainfo['new_sha'] = substr($ajson[0]['sha'], 0, 10);
|
||||
$_ainfo['new_sha1'] = $ajson[0]['sha'];
|
||||
$_ainfo['new_date'] = date("Y-m-d H:i:s O", strtotime($ajson[0]['created']));
|
||||
$_ainfo['new_message'] = $ajson[0]['commit']['message'];
|
||||
$_ainfo['svr_status'] = 1;
|
||||
$_ainfo['svr_message'] = "Server Online";
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
return $_ainfo;
|
||||
}
|
||||
|
||||
public function go(): array {
|
||||
$_branch = $this->__conf['GIT_BRANCH'] ?? "main";
|
||||
if (defined('PROJECT_ROOT')) {
|
||||
chdir(PROJECT_ROOT);
|
||||
$output = shell_exec("git reset --hard HEAD && git pull origin {$_branch} 2>&1");
|
||||
return ['success'=>true,'message'=>$output];
|
||||
} else return ['success'=>false,'message'=>"Tanpa PROJECT_ROOT !"];
|
||||
}
|
||||
|
||||
public function mysql() {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/*******************************************************************
|
||||
|
||||
Module : /App/User.php
|
||||
Created By : Siau Pheng ([email protected]).
|
||||
Created Date : September 10th, 2026.
|
||||
Last Modified : September 10th, 2026.
|
||||
|
||||
(c) 3FONIA Software; WWW.3FONIA.COM.
|
||||
|
||||
*******************************************************************/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Webapp\App;
|
||||
|
||||
use \flight\Engine;
|
||||
|
||||
/**
|
||||
* Class User
|
||||
*
|
||||
* User & Role Class.
|
||||
*
|
||||
* @license MIT, https://www.3fonia.com
|
||||
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
||||
*
|
||||
* @package Webapp\App
|
||||
*/
|
||||
|
||||
final class User {
|
||||
private array $web_user;
|
||||
protected Engine $app;
|
||||
|
||||
/**
|
||||
* @param Engine $app
|
||||
*/
|
||||
public function __construct(Engine $app) {
|
||||
$this->app = $app;
|
||||
$this->web_user = (array) $app->session()->get('WEB_USER');
|
||||
}
|
||||
|
||||
public function logged(): bool {
|
||||
return !empty($this->web_user);
|
||||
}
|
||||
|
||||
public function is_level(int $level = 9): bool {
|
||||
if (($this->web_user['Level'] ?? 9) <= $level) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public function not_level(int $level = 9): bool {
|
||||
if (($this->web_user['Level'] ?? 9) !== $level) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public function id(int $digit = 0): string|int {
|
||||
$id_user = (int) $this->web_user['UserID'] ?? 0;
|
||||
if ($digit > 0) return isi_nol($id_user, $digit);
|
||||
return $id_user;
|
||||
}
|
||||
|
||||
public function username(): string {
|
||||
return (string) $this->web_user['Username'] ?? '';
|
||||
}
|
||||
|
||||
public function level(): int {
|
||||
return (int) $this->web_user['Level'] ?? 9;
|
||||
}
|
||||
|
||||
public function unix(): string {
|
||||
return (string) $this->web_user['UNIX'] ?? '0';
|
||||
}
|
||||
|
||||
public function nama(): string {
|
||||
return (string) $this->web_user['Nama'] ?? '';
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user