Clean up some dirty code in page.view.php

This commit is contained in:
Kijin Sung 2022-03-14 02:11:57 +09:00
parent f373e38d6d
commit 063d38487e

View file

@ -21,19 +21,15 @@ class pageView extends page
{ {
switch($this->module_info->page_type) switch($this->module_info->page_type)
{ {
case 'WIDGET' : case 'WIDGET':
{ $this->cache_file = sprintf("%sfiles/cache/page/%d.%s.%s.cache.php", \RX_BASEDIR, $this->module_info->module_srl, Context::getLangType(), Context::getSslStatus());
$this->cache_file = sprintf("%sfiles/cache/page/%d.%s.%s.cache.php", RX_BASEDIR, $this->module_info->module_srl, Context::getLangType(), Context::getSslStatus()); $this->interval = (int)($this->module_info->page_caching_interval ?? 0);
$this->interval = (int)($this->module_info->page_caching_interval ?? 0); break;
break; case 'OUTSIDE':
} $this->cache_file = sprintf("%sfiles/cache/opage/%d.%s.cache.php", \RX_BASEDIR, $this->module_info->module_srl, Context::getSslStatus());
case 'OUTSIDE' : $this->interval = (int)($this->module_info->page_caching_interval ?? 0);
{ $this->path = $this->module_info->path ?? '';
$this->cache_file = sprintf("%sfiles/cache/opage/%d.%s.cache.php", RX_BASEDIR, $this->module_info->module_srl, Context::getSslStatus()); break;
$this->interval = (int)($this->module_info->page_caching_interval ?? 0);
$this->path = $this->module_info->path;
break;
}
} }
} }
@ -113,14 +109,23 @@ class pageView extends page
function _getOutsideContent() function _getOutsideContent()
{ {
// check if it is http or internal file // Stop if the path is not set.
if($this->path) if (!$this->path)
{ {
if(preg_match("/^([a-z]+):\/\//i",$this->path)) $content = $this->getHtmlPage($this->path, $this->interval, $this->cache_file); return;
else $content = $this->executeFile($this->path, $this->interval, $this->cache_file); }
// External URL
if (preg_match('!^[a-z]+://!i', $this->path))
{
return $this->getHtmlPage($this->path, $this->interval, $this->cache_file);
}
// Internal PHP document
else
{
return $this->executeFile($this->path, $this->interval, $this->cache_file);
} }
return $content;
} }
/** /**
@ -138,8 +143,10 @@ class pageView extends page
FileHandler::getRemoteFile($path, $cache_file); FileHandler::getRemoteFile($path, $cache_file);
$content = FileHandler::readFile($cache_file); $content = FileHandler::readFile($cache_file);
} }
// Create opage controller // Create opage controller
$oPageController = getController('page'); $oPageController = getController('page');
// change url of image, css, javascript and so on if the page is from external server // change url of image, css, javascript and so on if the page is from external server
$content = $oPageController->replaceSrc($content, $path); $content = $oPageController->replaceSrc($content, $path);
@ -148,12 +155,15 @@ class pageView extends page
$buff->content = $content; $buff->content = $content;
$buff = Context::convertEncoding($buff); $buff = Context::convertEncoding($buff);
$content = $buff->content; $content = $buff->content;
// Extract a title // Extract a title
$title = $oPageController->getTitle($content); $title = $oPageController->getTitle($content);
if($title) Context::setBrowserTitle($title); if($title) Context::setBrowserTitle($title);
// Extract header script // Extract header script
$head_script = $oPageController->getHeadScript($content); $head_script = $oPageController->getHeadScript($content);
if($head_script) Context::addHtmlHeader($head_script); if($head_script) Context::addHtmlHeader($head_script);
// Extract content from the body // Extract content from the body
$body_script = $oPageController->getBodyScript($content); $body_script = $oPageController->getBodyScript($content);
if(!$body_script) $body_script = $content; if(!$body_script) $body_script = $content;
@ -175,7 +185,6 @@ class pageView extends page
$filepath = preg_replace('/'.$filename."$/i","",$cache_file); $filepath = preg_replace('/'.$filename."$/i","",$cache_file);
$cache_file = FileHandler::getRealPath($cache_file); $cache_file = FileHandler::getRealPath($cache_file);
$level = ob_get_level();
// Verify cache // Verify cache
if($caching_interval <1 || !file_exists($cache_file) || filemtime($cache_file) + $caching_interval*60 <= $_SERVER['REQUEST_TIME'] || filemtime($cache_file)<filemtime($target_file)) if($caching_interval <1 || !file_exists($cache_file) || filemtime($cache_file) + $caching_interval*60 <= $_SERVER['REQUEST_TIME'] || filemtime($cache_file)<filemtime($target_file))
{ {
@ -185,14 +194,15 @@ class pageView extends page
ob_start(); ob_start();
include(FileHandler::getRealPath($target_file)); include(FileHandler::getRealPath($target_file));
$content = ob_get_clean(); $content = ob_get_clean();
// Replace relative path to the absolute path // Replace relative path to the absolute path
$this->path = str_replace('\\', '/', realpath(dirname($target_file))) . '/'; $this->path = str_replace('\\', '/', realpath(dirname($target_file))) . '/';
$content = preg_replace_callback('/(target=|src=|href=|url\()("|\')?([^"\'\)]+)("|\'\))?/is',array($this,'_replacePath'),$content); $content = preg_replace_callback('/(target=|src=|href=|url\()("|\')?([^"\'\)]+)("|\'\))?/is',array($this,'_replacePath'),$content);
$content = preg_replace_callback('/(<!--%import\()(\")([^"]+)(\")/is',array($this,'_replacePath'),$content); $content = preg_replace_callback('/(<!--%import\()(\")([^"]+)(\")/is',array($this,'_replacePath'),$content);
FileHandler::writeFile($cache_file, $content); FileHandler::writeFile($cache_file, $content);
// Include and then Return the result
if(!file_exists($cache_file)) return; if(!file_exists($cache_file)) return;
// Attempt to compile // Attempt to compile
$oTemplate = &TemplateHandler::getInstance(); $oTemplate = &TemplateHandler::getInstance();
$script = $oTemplate->compileDirect($filepath, $filename); $script = $oTemplate->compileDirect($filepath, $filename);
@ -232,14 +242,15 @@ class pageView extends page
function _replacePath($matches) function _replacePath($matches)
{ {
$val = trim($matches[3]); $val = trim($matches[3]);
// Pass if the path is external or starts with /, #, { characters // Pass if the path is external or starts with /, #, { characters
// /=absolute path, #=hash in a page, {=Template syntax // /=absolute path, #=hash in a page, {=Template syntax
if(strpos($val, '.') === FALSE || preg_match('@^((?:http|https|ftp|telnet|mms)://|(?:mailto|javascript):|[/#{])@i',$val)) if(strpos($val, '.') === FALSE || preg_match('@^((?:http|https|ftp|telnet|mms)://|(?:mailto|javascript):|[/#{])@i',$val))
{ {
return $matches[0]; return $matches[0];
// In case of .. , get a path
} }
else if(strncasecmp('..', $val, 2) === 0) // In case of .. , get a path
elseif(strncasecmp('..', $val, 2) === 0)
{ {
$p = Context::pathToUrl($this->path); $p = Context::pathToUrl($this->path);
return sprintf("%s%s%s%s",$matches[1],$matches[2],$p.$val,$matches[4]); return sprintf("%s%s%s%s",$matches[1],$matches[2],$p.$val,$matches[4]);