update htmlprocess

This commit is contained in:
Amberstone 2024-10-19 10:33:10 +09:00
parent 64207f833d
commit 5c07ddae07
Signed by: amber
GPG key ID: 094B0E55F98D8BF1

View file

@ -1,11 +1,137 @@
<?php <?php
class html_process class html_process
{ {
protected $latecss = [];
protected $css = []; protected $css = [];
protected $js = []; protected $js = [];
function merge_stylesheet($stylesheet, $order) private function updateLoginTable()
{ {
global $g5, $member;
$ip = $_SERVER['REMOTE_ADDR'];
$now = G5_TIME_YMDHIS;
$location = $g5['lo_location'];
$url = $g5['lo_url'];
$result = sql_fetch("SELECT COUNT(*) as cnt FROM {$g5['login_table']} WHERE lo_ip = '$ip'");
$exists = $result['cnt'];
$query = $exists
? "UPDATE {$g5['login_table']} SET mb_id = '{$member['mb_id']}', lo_datetime = '$now', lo_location = '$location', lo_url = '$url' WHERE lo_ip = '$ip'"
: "INSERT INTO {$g5['login_table']} (lo_ip, mb_id, lo_datetime, lo_location, lo_url) VALUES ('$ip', '{$member['mb_id']}', '$now', '$location', '$url')";
sql_query($query, FALSE);
}
private function cleanOldLoginRecords($minutes)
{
global $g5;
$oldDate = date("Y-m-d H:i:s", G5_SERVER_TIME - (60 * $minutes));
sql_query("DELETE FROM {$g5['login_table']} WHERE lo_datetime < '$oldDate'");
}
private function getBufferContents()
{
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
private function genStylesheet()
{
return $this->genLinks($this->css);
}
private function genLateStylesheet()
{
return $this->genLinks($this->latecss);
}
private function genLinks($links)
{
if (empty($links))
return '';
usort($links, function ($a, $b) {
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
});
$stylesheet = '';
foreach ($links as $link) {
if (trim($link[1])) {
$link[1] = preg_replace('#\.css([\'\"]?>)$#i', '.css?ver=' . G5_CSS_VER . '$1', $link[1]);
$stylesheet .= PHP_EOL . $link[1];
}
}
return $stylesheet;
}
private function genJavascript()
{
if (empty($this->js))
return '';
usort($this->js, function ($a, $b) {
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
});
$javascript = '';
foreach ($this->js as $js) {
if (trim($js[1])) {
$js[1] = preg_replace('#\.js([\'\"]?>)$#i', '.js?ver=' . G5_JS_VER . '$1', $js[1]);
$javascript .= PHP_EOL . $js[1];
}
}
return $javascript;
}
private function injectStyles($buffer, $stylesheet, $latestylesheet)
{
$buffer = preg_replace('#(</title>[^<]*<link[^>]+>)#', "$1$stylesheet", $buffer);
if ($latestylesheet) {
$buffer = preg_replace('#(</head>[^<]*<body[^>]*>)#', "$latestylesheet\n$1", $buffer);
}
return $buffer;
}
private function injectJavascript($buffer, $javascript)
{
if ($javascript) {
$buffer = preg_replace('#(</head>[^<]*<body[^>]*>)#', "$javascript\n$1", $buffer);
}
return $buffer;
}
public function addLateStyleSheet($stylesheet, $order)
{
if (trim($stylesheet)) {
$stylesheet = trim($stylesheet);
$links = $this->latecss;
$is_merge = true;
foreach ($links as $link) {
if ($link[1] == $stylesheet) {
$is_merge = false;
break;
}
}
if ($is_merge)
$this->latecss[] = [$order, $stylesheet];
}
}
public function addStyleSheet($stylesheet, $order = 0)
{
if (trim($stylesheet)) {
$stylesheet = trim($stylesheet);
$links = $this->css; $links = $this->css;
$is_merge = true; $is_merge = true;
@ -19,9 +145,12 @@ class html_process
if ($is_merge) if ($is_merge)
$this->css[] = [$order, $stylesheet]; $this->css[] = [$order, $stylesheet];
} }
}
function merge_javascript($javascript, $order) public function addJavascript($javascript, $order = 0)
{ {
if (trim($javascript)) {
$javascript = trim($javascript);
$scripts = $this->js; $scripts = $this->js;
$is_merge = true; $is_merge = true;
@ -35,83 +164,44 @@ class html_process
if ($is_merge) if ($is_merge)
$this->js[] = [$order, $javascript]; $this->js[] = [$order, $javascript];
} }
}
function run() public function merge_stylesheet($stylesheet, $order = 0)
{
$this->addStyleSheet($stylesheet, $order);
}
public function merge_javascript($javascript, $order = 0)
{
$this->addJavascript($javascript, $order);
}
public function optimizeTable()
{
global $g5;
/*
// todo
$row = sql_fetch(" SHOW TABLE STATUS FROM `{$mysql_db}` LIKE '{$g5['login_table']}' ");
// 부담(overhead)이 있다면 테이블 최적화
if ($row['Data_free'] > 0) {
sql_query(" OPTIMIZE TABLE {$g5['login_table']} ");
}*/
}
public function run()
{ {
global $config, $g5, $member; global $config, $g5, $member;
$tmp_sql = "SELECT count(*) as cnt FROM {$g5['login_table']} where lo_ip = '{$_SERVER['REMOTE_ADDR']}' "; $this->updateLoginTable();
$tmp_row = sql_fetch($tmp_sql); $this->cleanOldLoginRecords($config['cf_login_minutes']);
if ($tmp_row['cnt']) { $buffer = $this->getBufferContents();
$tmp_sql = "UPDATE {$g5['login_table']} SET mb_id = '{$member['mb_id']}', lo_datetime = '" . G5_TIME_YMDHIS . "', lo_location = '{$g5['lo_location']}', lo_url = '{$g5['lo_url']}' where lo_ip = '{$_SERVER['REMOTE_ADDR']}' "; $stylesheet = $this->genStylesheet();
sql_query($tmp_sql, FALSE); $latestylesheet = $this->genLateStylesheet();
} else { $javascript = $this->genJavascript();
$tmp_sql = "INSERT INTO {$g5['login_table']} ( lo_ip, mb_id, lo_datetime, lo_location, lo_url ) values ( '{$_SERVER['REMOTE_ADDR']}', '{$member['mb_id']}', '" . G5_TIME_YMDHIS . "', '{$g5['lo_location']}', '{$g5['lo_url']}' ) ";
sql_query($tmp_sql, FALSE);
sql_query("DELETE FROM {$g5['login_table']} WHERE lo_datetime < '" . date("Y-m-d H:i:s", G5_SERVER_TIME - (60 * $config['cf_login_minutes'])) . "' "); $buffer = $this->injectStyles($buffer, $stylesheet, $latestylesheet);
$buffer = $this->injectJavascript($buffer, $javascript);
// 부담(overhead)이 있다면 테이블 최적화
//$row = sql_fetch(" SHOW TABLE STATUS FROM `$mysql_db` LIKE '$g5['login_table']' ");
//if ($row['Data_free'] > 0) sql_query(" OPTIMIZE TABLE $g5['login_table'] ");
}
$buffer = ob_get_contents();
ob_end_clean();
$stylesheet = '';
$links = $this->css;
if (!empty($links)) {
foreach ($links as $key => $row) {
$order[$key] = $row[0];
$index[$key] = $key;
$style[$key] = $row[1];
}
array_multisort($order, SORT_ASC, $index, SORT_ASC, $links);
foreach ($links as $link) {
if (!trim($link[1]))
continue;
$link[1] = preg_replace('#\.css([\'\"]?>)$#i', '.css?ver=' . G5_CSS_VER . '$1', $link[1]);
$stylesheet .= PHP_EOL . $link[1];
}
}
$javascript = '';
$scripts = $this->js;
$php_eol = '';
unset($order);
unset($index);
if (!empty($scripts)) {
foreach ($scripts as $key => $row) {
$order[$key] = $row[0];
$index[$key] = $key;
$script[$key] = $row[1];
}
array_multisort($order, SORT_ASC, $index, SORT_ASC, $scripts);
foreach ($scripts as $js) {
if (!trim($js[1]))
continue;
$js[1] = preg_replace('#\.js([\'\"]?>)$#i', '.js?ver=' . G5_JS_VER . '$1', $js[1]);
$javascript .= $php_eol . $js[1];
$php_eol = PHP_EOL;
}
}
$buffer = preg_replace('#(</title>[^<]*<link[^>]+>)#', "$1$stylesheet", $buffer);
$nl = '';
if ($javascript)
$nl = "\n";
$buffer = preg_replace('#(</head>[^<]*<body[^>]*>)#', "$javascript{$nl}$1", $buffer);
if (class_exists("EventHandler")) { if (class_exists("EventHandler")) {
EventHandler::triggerEvent("amber.renderhtml_before_print", $buffer); EventHandler::triggerEvent("amber.renderhtml_before_print", $buffer);