upgrade first session to flightphp

This commit is contained in:
2026-08-19 17:22:07 +07:00
parent 36395c2b55
commit 27ebdca49b
31 changed files with 1383 additions and 7659 deletions
+101 -98
View File
@@ -3,133 +3,122 @@
/*******************************************************************
Module : /Data/Cache.php
Desc. : Class cache aplikasi
Desc. : Class cache css dan js aplikasi
Created By : Siau Pheng ([email protected]).
Created Date : March 16th, 2008.
Last Modified : July 25th, 2026.
Last Modified : August 19th, 2026.
(c) 2008 - 2026, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
//OK
declare(strict_types=1);
namespace siaupheng\webapp\Data;
use flight\Engine;
use FilesystemIterator;
use MatthiasMullie\Minify;
use SplFileInfo;
final class Cache {
private $__cachedays = 0;
private $__cachedir = "";
private $__cssdir = "";
private $__localedir = "";
private $__jsdir = "";
private $__jsapp = "";
private $__realpath = "";
private $__rootdir = "";
private $__ini = [];
protected $__db = null;
protected Engine $app;
protected array $__asset = [];
public function __construct($db = null){
if (is_resource($db)) { $this->__db = $db; }
else $this->__db = new \siaupheng\webapp\DB\MySQL();
$rootdir = $_SESSION['__WEB_APP']['REAL_PATH'];
$this->__cachedir = $rootdir . 'chunks/';
public function __construct(?Engine $app = null, array $asset = []) {
$this->app = (empty($app) === true) ? Flight::app() : $app;
$this->__asset = $asset;
$rootdir = (defined('PROJECT_ROOT')) ? PROJECT_ROOT : $this->app->get('flight.root_dir');
$this->__cachedir = $rootdir . '/public/chunks/';
$this->__localedir = $rootdir . '/app/locale/';
$this->__jsapp = $rootdir . '/app/scripts/';
$this->__cssdir = __DIR__ . '/../../assets/css/';
$this->__localedir = $rootdir . 'assets/locale/';
$this->__jsdir = __DIR__ . '/../../assets/js/';
$this->__jsapp = $rootdir . 'apps/js/';
$this->__realpath = $rootdir;
$this->__rootdir = $rootdir . '/';
if (!is_dir($this->__cachedir)) mkdir($this->__cachedir, 0755, true);
if (!file_exists($this->__cachedir . "cache.ini")) {
$this->__ini = [
'css' => ['login_stamp'=>"0",'login_file'=>"",'index_stamp'=>"0",'index_file'=>"",'display_stamp'=>"0",'display_file'=>""],
'js' => ['login_stamp'=>"0",'login_file'=>"",'index_stamp'=>"0",'index_file'=>"",'display_stamp'=>"0",'display_file'=>""]
'login' => [ 'stamp' => 0, 'css' => "", 'js' => "", 'app' => "", ],
'index' => [ 'stamp' => 0, 'css' => "", 'js' => "", 'app' => "", ],
'display' => [ 'stamp' => 0, 'css' => "", 'js' => "", 'app' => "", ],
];
$this->save_ini();
}
$this->__ini = parse_ini_file($this->__cachedir . "cache.ini", true);
} else $this->__ini = parse_ini_file($this->__cachedir . "cache.ini", true);
}
private function gen_file($type="css", $mode="index") {
private function gen_file($mode = "index", $type = "css") {
$itime = 0;
$afile = [];
if ($type == "css") {
$_afile = array_unique($GLOBALS['A_CSS']);
$_afile = array_unique($this->__asset['css'][$mode] ?? []);
foreach ($_afile as $__key => $val) {
$afile[] = $this->__cssdir .$val;
if (!file_exists($this->__cssdir . $val)) continue;
$_file = new SplFileInfo($this->__cssdir . $val);
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
if (file_exists($this->__realpath . 'assets/css/')) {
if ($handle = opendir($this->__realpath . 'assets/css/')) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..' || is_dir($this->__realpath . 'assets/css/'.$file)) continue;
$afile[] = $this->__realpath . 'assets/css/' . $file;
if (is_dir($this->__rootdir . 'app/css/')) {
$iterator = new FilesystemIterator($this->__rootdir . 'app/css/', FilesystemIterator::SKIP_DOTS);
foreach ($iterator as $_file) {
if ($_file->isFile()) {
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
closedir($handle);
}
}
} else if ($type == "js") {
$_afile = array_unique($GLOBALS['A_JS']);
$_afile = array_unique($this->__asset['js'][$mode] ?? []);
foreach ($_afile as $__key => $val) {
$afile[] = $this->__jsdir .$val;
}
if (file_exists($this->__realpath . 'assets/js/')) {
if ($handle = opendir($this->__realpath . 'assets/js/')) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..' || is_dir($this->__realpath . 'assets/js/'.$file)) continue;
$afile[] = $this->__realpath . 'assets/js/' . $file;
}
closedir($handle);
}
if (!file_exists($this->__jsdir . $val)) continue;
$_file = new SplFileInfo($this->__jsdir . $val);
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
// add locale file
$ljs = (isset($_SESSION['__WEB_USER']['LOCALE'])?$_SESSION['__WEB_USER']['LOCALE']:$_SESSION['__WEB_APP']['DEF_LOCALE']).".js";
$ljs = ($this->app->session()->get('__WEB_USER')['LOCALE'] ?? $this->app->session()->get('__WEB_APP')['DEF_LOCALE']).'.js';
if (is_dir($this->__localedir)) {
if ($handle = opendir($this->__localedir)) {
while (false !== ($file = readdir($handle))) {
$file = $this->__localedir . $file;
if (substr($file, -5) == $ljs) $afile[] = $this->__localedir .basename(str_replace("\\", "/", $file));
$iterator = new \FilesystemIterator($this->__localedir, \FilesystemIterator::SKIP_DOTS);
foreach ($iterator as $_file) {
if ($_file->isFile() && str_ends_with($_file->getFilename(), $ljs)) {
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
closedir($handle);
}
}
$afile[] = $this->__jsapp . $mode . ".js";
} else if ($type == "app") {
$afile[] = $this->__jsapp ."sys/profile.js";
$afile[] = $this->__jsapp ."sys/chpasswd.js";
$amenu = explode(",", $_SESSION['__WEB_USER']['Menu_Grup'] .",". $_SESSION['__WEB_USER']['Menu']);
$tmenu = implode("','", array_unique($amenu));
$this->__db->Query("SELECT * FROM app_menu WHERE Link<>'' AND Kode IN ('".$tmenu."') ORDER BY Level, Urut");
while ($this->__db->Next()) {
$afile[] = $this->__jsapp . str_replace("php", "js", $this->__db->Row("Link"));
}
}
return $afile;
}
private function check_file($afile) {
$lastmod = 0;
foreach ($afile as $nfile) {
if (file_exists($nfile)) {
$lastmod += filemtime($nfile);
}
}
if ($this->__cachedays > 0) {
if ($handle = opendir($this->__cachedir)) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..' || is_dir($this->__cachedir.$file)) continue;
if ((time()-filemtime($this->__cachedir.$file)) > (86400*$this->__cachedays)) {
unlink($this->__cachedir . $file);
if ($mode === 'index') {
if (is_dir($this->__jsapp)) {
$dirIterator = new \RecursiveDirectoryIterator($this->__jsapp, \RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new \RecursiveIteratorIterator($dirIterator);
foreach ($iterator as $_file) {
if ($_file->isFile() && !in_array($_file->getFilename(), ['login.js','display.js'])) {
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
}
}
closedir($handle);
} else {
if (file_exists($this->__jsapp . $mode . '.js')) {
$_file = new SplFileInfo($this->__jsapp . $mode . '.js');
$afile[] = $_file->getRealPath();
$itime += $_file->getMTime();
}
}
}
return $lastmod;
return [ $afile, $itime ];
}
private function create_cache($type="css", $mode="index", $afile=[], $lastmod=0) {
private function create_cache($type = "css", $afile = [], $cachefile = "") {
$contents = ''; $smini = null;
$bpublic = ($_SERVER['SERVER_ADDR'] == "127.0.0.1") ? false : true;
$bpublic = ($this->app->request()->ip === '127.0.0.1') ? false : true;
if ($bpublic == true) {
if ($type=="css") {
$smini = new Minify\CSS;
@@ -147,15 +136,10 @@ final class Cache {
}
}
if ($bpublic) $contents = $smini->minify();
$cachefile = md5($mode . $lastmod) . ($type=="app"?".app":"") . ($type=="css" ? ".css" : ".js");
file_put_contents($this->__cachedir . $cachefile, $contents);
if ($type == "app") return;
$this->__ini[$type][$mode.'_stamp'] = $lastmod;
$this->__ini[$type][$mode.'_file'] = $cachefile;
$this->save_ini();
}
private function save_ini(){
private function save_ini() {
$content = "";
foreach ($this->__ini as $key => $elem) {
$content .= ($content==""?"":"\n"). "[".$key."]\n";
@@ -167,27 +151,46 @@ final class Cache {
file_put_contents($this->__cachedir . "cache.ini", $content);
}
public function Main($type="css", $mode="") {
if ($mode == "") $mode = "login";
if (($_SESSION['__WEB_USER']['UserID'] ?? 0) > 0) $mode = "index";
$afile = $this->gen_file($type, $mode);
$lastmod = $this->check_file($afile);
if ($lastmod <> intval($this->__ini[$type][$mode.'_stamp']) || !file_exists($this->__cachedir . $this->__ini[$type][$mode.'_file'])) {
$this->create_cache($type, $mode, $afile, $lastmod);
public function Cache($mode = "index") {
$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))) {
$this->create_cache($type, $afile, $cachefile);
$this->__ini[$mode][$type] = $cachefile;
$this->__ini[$mode]['stamp'] = time();
$is_modif = true;
}
}
return $_SESSION['__WEB_APP']['WEB_PATH'] ."chunks/". $this->__ini[$type][$mode.'_file'];
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)) {
if ($_type === 'css') {
$acache['CSS'][] = 'chunks/' . $_file;
} else $acache['JS'][] = 'chunks/' . $_file;
}
}
$session = $this->app->session();
$aoption = $session->get('__WEB_APP');
$auser = $session->get('__WEB_USER');
$nonce = $this->app->get('csp_nonce');
$_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 = (user_logged()) ? $auser['Username'] .";". $auser['UserID'] .";". $auser['Grup_MenuID'] .";". $auser['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 (user_logged()) $acache['JSX'][] = 'FoniaJS.loadMenu();';
$acache['JSX'][] = '});';
$acache['JSX'][] = '</script>';
return $acache;
}
public function Apps() {
$id_user = (isset($_SESSION['__WEB_USER']) && is_array($_SESSION['__WEB_USER'])) ? $_SESSION['__WEB_USER']['UserID'] : 0;
$afile = $this->gen_file("app", $id_user);
$lastmod = $this->check_file($afile);
$appfile = md5($id_user . $lastmod) . ".app.js";
if (!file_exists($this->__cachedir . $appfile)) {
$this->create_cache("app", $id_user, $afile, $lastmod);
}
return $_SESSION['__WEB_APP']['WEB_PATH'] ."chunks/". $appfile;
}
}
?>