fixed change of framework

This commit is contained in:
2026-09-10 11:43:07 +07:00
parent a2978ae927
commit bd93cca382
18 changed files with 244 additions and 246 deletions
+133 -75
View File
@@ -5,7 +5,7 @@
Module : /Data/Cache.php
Created By : Siau Pheng ([email protected]).
Created Date : March 16th, 2008.
Last Modified : August 21st, 2026.
Last Modified : September 3rd, 2026.
(c) 3FONIA Software; WWW.3FONIA.COM.
@@ -17,6 +17,7 @@ namespace Webapp\Data;
use \flight\Engine;
use \flight\Session;
use \flight\database\SimplePdo;
use \FilesystemIterator;
use \MatthiasMullie\Minify;
use \SplFileInfo;
@@ -33,16 +34,20 @@ use \SplFileInfo;
*/
final class Cache {
private $__cachedir = "";
private $__cssdir = "";
private $__localedir = "";
private $__jsdir = "";
private $__jsapp = "";
private $__rootdir = "";
private $__ini = [];
private string $cachedir;
private string $cssdir;
private string $localedir;
private string $jsdir;
private string $jsapp;
private string $rootdir;
private array $ini;
private array $web_app;
private array $web_user;
private bool $is_login;
private bool $IS_DEV = false;
protected Engine $app;
protected Session $session;
protected array $__asset = [];
protected SimplePdo $db;
protected array $asset = [];
/**
* @param Engine $app
@@ -50,42 +55,50 @@ final class Cache {
public function __construct(Engine $app) {
$this->app = $app;
$this->session = $app->session();
$rootdir = (defined('PROJECT_ROOT')) ? PROJECT_ROOT : $this->app->get('app.project_root');
$this->__cachedir = $rootdir . '/public/chunks/';
$this->__localedir = $rootdir . '/app/Scripts/locale/';
$this->__jsapp = $rootdir . '/app/Scripts/';
$this->__cssdir = __DIR__ . '/../../assets/css/';
$this->__jsdir = __DIR__ . '/../../assets/js/';
$this->__rootdir = $rootdir . '/';
if (!is_dir($this->__cachedir)) mkdir($this->__cachedir, 0755, true);
if (!file_exists($this->__cachedir . "cache.ini")) {
$this->__ini = [
'login' => [ 'stamp' => 0, 'css' => "", 'js' => "", 'app' => "", ],
'index' => [ 'stamp' => 0, 'css' => "", 'js' => "", 'app' => "", ],
'display' => [ 'stamp' => 0, 'css' => "", 'js' => "", 'app' => "", ],
$this->db = $app->db();
$this->web_app = $app->session()->get('WEB_APP') ?? [];
$this->web_user = $app->session()->get('WEB_USER') ?? [];
$this->is_login = (count($this->web_user) > 0);
$rootdir = (defined('PROJECT_ROOT')) ? PROJECT_ROOT : $app->get('app.project_root');
$this->cachedir = $rootdir . '/temp/cache/';
$this->localedir = $rootdir . '/app/Scripts/locale/';
$this->jsapp = $rootdir . '/app/Scripts/';
$this->cssdir = realpath(__DIR__ . '/../../assets/css') . '/';
$this->jsdir = realpath(__DIR__ . '/../../assets/js') . '/';
$this->rootdir = $rootdir . '/';
if (!is_dir($this->cachedir)) mkdir($this->cachedir, 0755, true);
if (!file_exists($this->cachedir . "cache.ini")) {
$this->ini = [
'login' => [ 'css' => "", 'js' => "", 'app' => "", ],
'index' => [ 'css' => "", 'js' => "", 'app' => "", ],
'display' => [ 'css' => "", 'js' => "", 'app' => "", ],
];
$this->save_ini();
} else $this->__ini = parse_ini_file($this->__cachedir . "cache.ini", true);
} else $this->ini = parse_ini_file($this->cachedir . "cache.ini", true);
}
public function set_dev(bool $is_dev = false): void {
$this->IS_DEV = $is_dev;
}
public function set_asset(array $asset = []): void {
$this->__asset = $asset;
$this->asset = $asset;
}
private function gen_file(string $mode = "index", string $type = "css"): array {
$itime = 0;
$afile = [];
if ($type == "css") {
$_afile = array_unique($this->__asset['css'][$mode] ?? []);
if ($type === "css") {
$_afile = array_unique($this->asset['css'][$mode] ?? []);
foreach ($_afile as $__key => $val) {
if (!file_exists($this->__cssdir . $val)) continue;
$_file = new SplFileInfo($this->__cssdir . $val);
if (!file_exists($this->cssdir . $val)) continue;
$_file = new SplFileInfo($this->cssdir . $val);
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
if (is_dir($this->__rootdir . 'app/Styles/')) {
$iterator = new FilesystemIterator($this->__rootdir . 'app/Styles/', FilesystemIterator::SKIP_DOTS);
if (is_dir($this->rootdir . 'app/Styles/')) {
$iterator = new FilesystemIterator($this->rootdir . 'app/Styles/', FilesystemIterator::SKIP_DOTS);
foreach ($iterator as $_file) {
if ($_file->isFile()) {
$afile[] = $_file->getRealPath();
@@ -93,18 +106,18 @@ final class Cache {
}
}
}
} else if ($type == "js") {
$_afile = array_unique($this->__asset['js'][$mode] ?? []);
} else if ($type === "js") {
$_afile = array_unique($this->asset['js'][$mode] ?? []);
foreach ($_afile as $__key => $val) {
if (!file_exists($this->__jsdir . $val)) continue;
$_file = new SplFileInfo($this->__jsdir . $val);
if (!file_exists($this->jsdir . $val)) continue;
$_file = new SplFileInfo($this->jsdir . $val);
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
// add locale file
$ljs = ($this->session->get('__WEB_USER')['LOCALE'] ?? $this->session->get('__WEB_APP')['DEF_LOCALE']).'.js';
if (is_dir($this->__localedir)) {
$iterator = new \FilesystemIterator($this->__localedir, \FilesystemIterator::SKIP_DOTS);
$ljs = ($this->web_user['LOCALE'] ?? $this->web_app['DEF_LOCALE']).'.js';
if (is_dir($this->localedir)) {
$iterator = new \FilesystemIterator($this->localedir, \FilesystemIterator::SKIP_DOTS);
foreach ($iterator as $_file) {
if ($_file->isFile() && str_ends_with($_file->getFilename(), $ljs)) {
$afile[] = $_file->getRealPath();
@@ -112,25 +125,31 @@ final class Cache {
}
}
}
} else if ($type == "app") {
if ($mode === 'index') {
if (is_dir($this->__jsapp)) {
$dirIterator = new \RecursiveDirectoryIterator($this->__jsapp, \RecursiveDirectoryIterator::SKIP_DOTS);
} else if ($type === "app") {
if (is_dir($this->jsapp)) {
if (file_exists($this->jsapp . $mode . '.js')) {
$_file = new SplFileInfo($this->jsapp . $mode . '.js');
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
if ($mode === 'index') {
$_ajs = ['system/profile', 'system/chpasswd'];
$amenu = array_values($this->web_user['Merge_Menu'] ?? []);
$_ops = $this->db->fetchColumn("SELECT Link FROM app_menu WHERE Kode IN (?) AND Link<>'' ORDER BY Kode", [$amenu]);
if ($_ops) $_ajs += $_ops;
$dirIterator = new \RecursiveDirectoryIterator($this->jsapp, \RecursiveDirectoryIterator::SKIP_DOTS);
$dirFilter = new \RecursiveCallbackFilterIterator($dirIterator, fn($curr, $key, $iter) => !($curr->isDir() && in_array($curr->getFilename(), ['locale'])));
$iterator = new \RecursiveIteratorIterator($dirFilter);
foreach ($iterator as $_file) {
if ($_file->isFile() && !in_array($_file->getFilename(), ['login.js','display.js'])) {
if ($_file->isFile() &&
!in_array($_file->getFilename(), ['login.js','index.js','display.js']) &&
array_any($_ajs, fn($js) => str_ends_with(str_replace(['_','\\'],['','/'],$_file->getRealPath()), $js.'.js'))) {
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
}
}
} else {
if (file_exists($this->__jsapp . $mode . '.js')) {
$_file = new SplFileInfo($this->__jsapp . $mode . '.js');
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
}
}
return [ $afile, $itime ];
@@ -139,8 +158,8 @@ final class Cache {
private function create_cache(string $type = "css", array $afile = [], string $cachefile = ""): void {
$contents = ''; $smini = null;
$bpublic = ($this->app->request()->ip === '127.0.0.1') ? false : true;
if ($bpublic == true) {
if ($type=="css") {
if ($bpublic === true) {
if ($type === "css") {
$smini = new Minify\CSS;
} else {
$smini = new Minify\JS;
@@ -148,7 +167,7 @@ final class Cache {
}
foreach ($afile as $nfile) {
if (file_exists($nfile)) {
if ($bpublic == true) {
if ($bpublic === true) {
$smini->add($nfile);
} else {
$contents .= "\n". file_get_contents($nfile);
@@ -156,61 +175,100 @@ final class Cache {
}
}
if ($bpublic) $contents = $smini->minify();
file_put_contents($this->__cachedir . $cachefile, $contents);
file_put_contents($this->cachedir . $cachefile, $contents);
}
private function save_ini(): void {
$content = "";
foreach ($this->__ini as $key => $elem) {
$content .= ($content==""?"":"\n"). "[".$key."]\n";
unset ($this->ini['index']['app']);
foreach ($this->ini as $key => $elem) {
$content .= ($content === ""?"" : "\n"). "[".$key."]\n";
foreach ($elem as $key2 => $elem2) {
if ($elem2=="") $content .= $key2." = \n";
if ($key)
if ($elem2 === "") $content .= $key2." = \n";
else $content .= $key2." = \"".$elem2."\"\n";
}
}
file_put_contents($this->__cachedir . "cache.ini", $content);
file_put_contents($this->cachedir . "cache.ini", $content);
}
public function Cache(string $mode = "index"): array {
if ($mode === 'index') {
$this->ini['index']['app'] = (string) $this->db->fetchField("SELECT JS FROM app_operator_profile WHERE UserID=?", [$this->web_user['UserID']]) ?? '';
$this->web_user['Merge_Menu'] = array_filter(array_unique(explode(",", $this->web_user['Menu_Grup'] .",". $this->web_user['Menu'])));
}
$is_modif = false;
foreach (['css','js','app'] as $type) {
[ $afile, $lastmod ] = $this->gen_file($mode, $type);
$cachefile = md5($mode . $type . $lastmod) . ($type=='css'?'.css':'.js');
if (!file_exists($this->__cachedir . $cachefile) || ((time()-(int)$this->__ini[$mode]['stamp']) > (86400*14))) {
$is_index_app = ($mode === 'index' && $type === 'app');
if (empty($this->ini[$mode][$type]) || !file_exists($this->cachedir . $this->ini[$mode][$type]) || ($this->IS_DEV)) {
[ $afile, $lastmod ] = $this->gen_file($mode, $type);
$cachefile = md5($mode . '-' . $type . '-' . $lastmod) . ($type === 'css' ? '.css' : '.js');
if ($is_index_app) $cachefile = md5('index-app-' . (string) $this->web_user['UserID'] . $lastmod) . '.js';
$this->create_cache($type, $afile, $cachefile);
$this->__ini[$mode][$type] = $cachefile;
$this->__ini[$mode]['stamp'] = time();
$this->ini[$mode][$type] = $cachefile;
if ($is_index_app) $this->db->runQuery("INSERT INTO app_operator_profile (UserID,JS) VALUES (?, ?) ON DUPLICATE KEY UPDATE JS=VALUES(JS)", [$this->web_user['UserID'], $cachefile]);
$is_modif = true;
}
}
if ($is_modif === true) $this->save_ini();
$acache = [ 'CSS' => [], 'JS' => [], 'JSX' => [] ];
foreach ($this->__ini[$mode] as $_type => $_file) {
if ($_type == 'stamp') continue;
if (file_exists($this->__cachedir . $_file)) {
foreach ($this->ini[$mode] as $_type => $_file) {
if (file_exists($this->cachedir . $_file)) {
if ($_type === 'css') {
$acache['CSS'][] = 'chunks/' . $_file;
} else $acache['JS'][] = 'chunks/' . $_file;
$acache['CSS'][] = 'cache/' . $_file;
} else $acache['JS'][] = 'cache/' . $_file;
}
}
if ($is_modif === true) $this->save_ini();
$aoption = $this->session->get('__WEB_APP');
$auser = $this->session->get('__WEB_USER');
$nonce = $this->app->get('csp_nonce');
$islog = user_logged($this->session);
$_secret = $aoption['APP_CODE'] .";". $aoption['APP_SID'] .";". $this->app->request()->getVar('HTTP_HOST') .";". "http://".$this->app->request()->getVar('HTTP_HOST') .";". isMobile() .";". ($aoption['AUTH_LEVEL']?$aoption['AUTH_LEVEL']:0);
$_suser = ($islog) ? $auser['Username'] .";". $auser['UserID'] .";". $auser['Grup_MenuID'] .";". $auser['Level'] : "nobody;999;999;9";
$_secret = $this->web_app['APP_CODE'] .";". $this->web_app['APP_SID'] .";". $this->app->request()->getVar('HTTP_HOST') .";". "http://".$this->app->request()->getVar('HTTP_HOST') .";". isMobile() .";". ($this->web_app['AUTH_LEVEL']?$this->web_app['AUTH_LEVEL']:0);
$_suser = ($this->is_login) ? $this->web_user['Username'] .";". $this->web_user['UserID'] .";". $this->web_user['Grup_MenuID'] .";". $this->web_user['Level'] : "nobody;999;999;9";
$acache['JSX'][] = '<script nonce="'.$nonce.'" type="text/javascript">';
$acache['JSX'][] = 'FoniaJS.setConfig("'.base64_encode($_secret).'", "'.base64_encode($_suser).'");';
$acache['JSX'][] = '$(document).ready(function(){';
if ($islog) $acache['JSX'][] = 'FoniaJS.loadMenu();';
if ($this->is_login) $acache['JSX'][] = 'FoniaJS.loadMenu();';
$acache['JSX'][] = '});';
$acache['JSX'][] = '</script>';
return $acache;
}
public function Make_Cache(array $amode = ['login', 'index', 'display']): array {
$is_modif = false;
foreach ($amode as $mode) {
foreach (['css','js','app'] as $type) {
if ($mode === 'index' && $type === 'app') continue;
if (empty($this->ini[$mode][$type]) || !file_exists($this->cachedir . $this->ini[$mode][$type])) {
[ $afile, $lastmod ] = $this->gen_file($mode, $type);
$cachefile = md5($mode . '-' . $type . '-' . $lastmod) . ($type === 'css' ? '.css' : '.js');
$this->create_cache($type, $afile, $cachefile);
$this->ini[$mode][$type] = $cachefile;
$is_modif = true;
}
}
}
if ($is_modif === true) $this->save_ini();
if (in_array('index', $amode)) {
$agrup = $this->db->fetchAll('SELECT Grup_MenuID,DECODE(Menu,?) Menu FROM app_menu_grup', [$this->web_app['PARENT_CODE']]);
$auser = $this->db->fetchAll("SELECT A.*,IFNULL(B.JS,'') JS FROM (SELECT UserID,Grup_MenuID,DECODE(Menu,?) Menu FROM app_operator) A LEFT JOIN app_operator_profile B ON (A.UserID=B.UserID)", [$this->web_app['PARENT_CODE']]);
foreach ($auser as $user) {
if (empty($user['JS']) || !file_exists($this->cachedir . $user['JS'])) {
$this->web_user['Merge_Menu'] = array_filter(array_unique(explode(",", $user['Menu'] .",". ($agrup[$user['Grup_MenuID']]['Menu'] ?? ''))));
[ $afile, $lastmod ] = $this->gen_file('index', 'app');
$cachefile = md5('index-app-' . (string) $user['UserID'] . $lastmod) . '.js';
$this->create_cache('app', $afile, $cachefile);
$this->db->runQuery("INSERT INTO app_operator_profile (UserID,JS) VALUES (?, ?) ON DUPLICATE KEY UPDATE JS=VALUES(JS)", [$user['UserID'], $cachefile]);
}
}
}
return [ 'status' => 1, 'user_count' => count($auser) ];
}
}
?>