diff --git a/AvocadoAmber/classes/htmlprocess/htmlprocess.class.php b/AvocadoAmber/classes/htmlprocess/htmlprocess.class.php index 4c25edb..34d6861 100644 --- a/AvocadoAmber/classes/htmlprocess/htmlprocess.class.php +++ b/AvocadoAmber/classes/htmlprocess/htmlprocess.class.php @@ -1,117 +1,207 @@ css; - $is_merge = true; + global $g5, $member; - foreach ($links as $link) { - if ($link[1] == $stylesheet) { - $is_merge = false; - break; - } - } + $ip = $_SERVER['REMOTE_ADDR']; + $now = G5_TIME_YMDHIS; + $location = $g5['lo_location']; + $url = $g5['lo_url']; - if ($is_merge) - $this->css[] = [$order, $stylesheet]; + $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); } - function merge_javascript($javascript, $order) + private function cleanOldLoginRecords($minutes) { - $scripts = $this->js; - $is_merge = true; - - foreach ($scripts as $script) { - if ($script[1] == $javascript) { - $is_merge = false; - break; - } - } - - if ($is_merge) - $this->js[] = [$order, $javascript]; + 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'"); } - function run() + private function getBufferContents() { - global $config, $g5, $member; - - $tmp_sql = "SELECT count(*) as cnt FROM {$g5['login_table']} where lo_ip = '{$_SERVER['REMOTE_ADDR']}' "; - $tmp_row = sql_fetch($tmp_sql); - - if ($tmp_row['cnt']) { - $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']}' "; - sql_query($tmp_sql, FALSE); - } else { - $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'])) . "' "); - - // 부담(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(); + 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 = ''; - $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; - + 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 = ''; - $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; - + 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]; - $php_eol = PHP_EOL; + $javascript .= PHP_EOL . $js[1]; } } + return $javascript; + } + private function injectStyles($buffer, $stylesheet, $latestylesheet) + { $buffer = preg_replace('#([^<]*]+>)#', "$1$stylesheet", $buffer); - $nl = ''; - if ($javascript) - $nl = "\n"; - $buffer = preg_replace('#([^<]*]*>)#', "$javascript{$nl}$1", $buffer); + if ($latestylesheet) { + $buffer = preg_replace('#([^<]*]*>)#', "$latestylesheet\n$1", $buffer); + } + return $buffer; + } + + private function injectJavascript($buffer, $javascript) + { + if ($javascript) { + $buffer = preg_replace('#([^<]*]*>)#', "$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; + $is_merge = true; + + foreach ($links as $link) { + if ($link[1] == $stylesheet) { + $is_merge = false; + break; + } + } + + if ($is_merge) + $this->css[] = [$order, $stylesheet]; + } + } + + public function addJavascript($javascript, $order = 0) + { + if (trim($javascript)) { + $javascript = trim($javascript); + $scripts = $this->js; + $is_merge = true; + + foreach ($scripts as $script) { + if ($script[1] == $javascript) { + $is_merge = false; + break; + } + } + + if ($is_merge) + $this->js[] = [$order, $javascript]; + } + } + + 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; + + $this->updateLoginTable(); + $this->cleanOldLoginRecords($config['cf_login_minutes']); + + $buffer = $this->getBufferContents(); + $stylesheet = $this->genStylesheet(); + $latestylesheet = $this->genLateStylesheet(); + $javascript = $this->genJavascript(); + + $buffer = $this->injectStyles($buffer, $stylesheet, $latestylesheet); + $buffer = $this->injectJavascript($buffer, $javascript); if (class_exists("EventHandler")) { EventHandler::triggerEvent("amber.renderhtml_before_print", $buffer);