Page MenuHomeIn-Portal Phabricator

template_cache.php
No OneTemporary

File Metadata

Created
Fri, Sep 26, 12:36 AM

template_cache.php

<?php
/**
* @version $Id: template_cache.php 15640 2012-12-03 15:24:27Z alex $
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class TemplatesCache extends kHelper {
/**
* Base path for searching templates
*
* @var string
*/
var $BasePath;
/**
* Force templates cache to search templates on front-end:
* true - search for theme name in template name
* false - don't search anywhere
* name - theme name to prepend for each template name
*
* @var mixed
*/
var $forceThemeName = false;
/**
* Compile templates to database
*
* @var bool
*/
var $_compileToDatabase = false;
/**
* Template locations of each module
*
* @var Array
*/
var $_modulePaths = Array ();
public function __construct()
{
parent::__construct();
$this->BasePath = FULL_PATH . THEMES_PATH;
$this->_compileToDatabase = defined('SAFE_MODE') && SAFE_MODE;
if ( $this->Application->isAdmin ) {
// prepare module template paths for quick access
$module_paths = Array ();
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
$module_paths[mb_strtolower($module_name)] = rtrim($module_info['Path'], '/');
}
$this->_modulePaths = $module_paths;
}
}
/**
* Based on template name gets it's location on disk and owner module
*
* @param string $filename
* @return Array 0 - path on disk, 1 - template name
*/
function GetTemplatePaths($filename)
{
if ( $this->Application->isAdmin && array_key_exists($filename, $this->Application->ReplacementTemplates) ) {
// process admin template replacement
$filename = $this->Application->ReplacementTemplates[$filename];
}
// allows to use non-replaced version of replaced template
$filename = preg_replace('/^original:(.*)/', '\\1', $filename);
if ( preg_match('#^[\/]{0,1}([^\/]*)\/(.*)#', $filename, $regs) ) {
$first_dir = $regs[1];
$module_filename = $regs[2];
}
else {
$first_dir = '';
$module_filename = $filename;
}
if ( is_string($this->forceThemeName) ) {
// when defined, then all templates are read from given theme name
$first_dir = 'theme:' . $this->forceThemeName . ($first_dir ? '/' . $first_dir : '');
}
if ( $this->Application->isAdmin && array_key_exists($first_dir, $this->_modulePaths) ) {
// template belongs to one of the modules
$path = FULL_PATH . '/' . $this->_modulePaths[$first_dir] . '/admin_templates';
}
elseif ( $this->forceThemeName && preg_match('/^theme:(.*)/', $first_dir, $regs) ) {
// ability to use Front-End templates in admin (only during mass compilation)
$path = FULL_PATH . '/themes/' . $regs[1];
}
else {
// template from "core" module
$path = $this->BasePath;
$module_filename = $first_dir . '/' . $module_filename;
}
return Array ($path, $module_filename);
}
/**
* Returns template filename by given template name
*
* @param string $filename
* @return string
*/
function GetRealFilename($filename)
{
list ($path, $module_filename) = $this->GetTemplatePaths($filename);
return $path . '/' . trim($module_filename, '/');
}
/**
* Checks, that given template exists on disk
*
* @param string $filename
* @return bool
*/
function TemplateExists($filename)
{
if ( (strpos($filename, '../') !== false) || (trim($filename) !== $filename) ) {
// when relative paths or special chars are found template names from url, then it's hacking attempt
return false;
}
$real_file = $this->GetRealFilename(strtolower($filename));
if ( substr($real_file, -4) != '.tpl' ) {
// add ".tpl" file extension, when not specified in template name
$real_file .= '.tpl';
}
return file_exists($real_file);
}
/**
* Returns information about template compilation status
*
* @param string $template
* @return Array
*/
function GetPreParsed($template)
{
$real_name = $this->GetRealFilename($template);
$fname = str_replace(FULL_PATH, WRITEABLE . '/cache', $real_name . '.php');
$tname = $real_name . '.tpl';
if ( !file_exists($tname) ) {
// given template doesn't exist
return false;
}
if ( $this->_compileToDatabase ) {
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'SystemCache
WHERE VarName = "' . $fname . '"';
$cached = $this->Conn->GetRow($sql);
if ( $cached !== false && $cached['Cached'] > filemtime($tname) ) {
return Array ('active' => 1, 'fname' => $fname, 'tname' => $tname, 'mode' => 'db', 'content' => $cached['Data']);
}
}
else {
if ( file_exists($fname) && file_exists($tname) && filemtime($fname) > filemtime($tname) ) {
return Array ('active' => 1, 'fname' => $fname, 'tname' => $tname, 'mode' => 'file');
}
if ( !file_exists($fname) ) {
// make sure to create directory if pre-parsed file does not exist
$this->CheckDir(dirname($fname), WRITEABLE . '/cache');
}
}
// when compiled template is expired or doesn't exist
return Array ('active' => 0, 'fname' => $fname, 'tname' => $tname, 'mode' => 'file');
}
/**
* Saves compiled template version to database or disk
*
* @param string $filename
* @param string $compiled_template
* @return void
* @throws ParserException
*/
function saveTemplate($filename, &$compiled_template)
{
if ( $this->_compileToDatabase ) {
$fields_hash = Array (
'VarName' => $filename,
'Data' => &$compiled_template,
'Cached' => adodb_mktime(),
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'SystemCache', 'REPLACE');
return ;
}
$fp = fopen($filename, 'w');
if ( !fwrite($fp, $compiled_template) ) {
throw new ParserException('Saving compiled template failed');
}
fclose($fp);
}
/**
* Runs template and returns result (template already should be compiled by now)
*
* @param NParser $_parser
* @param Array $pre_parsed
* @return string
*/
function &runTemplate(&$_parser, &$pre_parsed)
{
ob_start();
if ( $this->_compileToDatabase ) {
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'SystemCache
WHERE VarName = "' . $pre_parsed['fname'] . '"';
$cached = $this->Conn->GetRow($sql);
if ( ($cached !== false) && ($cached['Cached'] > filemtime($pre_parsed['tname'])) ) {
eval('?' . '>' . $cached['Data']);
}
}
else {
if ( $pre_parsed['mode'] == 'file' ) {
include($pre_parsed['fname']);
}
else {
eval('?' . '>' . $pre_parsed['content']);
}
}
$output = ob_get_clean();
return $output;
}
/**
* Recursive mkdir
*
* @param string $dir
* @param string $base_path base path to directory where folders should be created in
* @return bool
* @access protected
*/
protected function CheckDir($dir, $base_path = '')
{
if ( file_exists($dir) ) {
return true;
}
// remove $base_path from beginning because it is already created during install
$dir = preg_replace('/^' . preg_quote($base_path . '/', '/') . '/', '', $dir, 1);
$segments = explode('/', $dir);
$cur_path = $base_path;
foreach ($segments as $segment) {
// do not add leading / for windows paths (c:\...)
$cur_path .= preg_match('/^[a-zA-Z]{1}:/', $segment) ? $segment : '/' . $segment;
if ( !file_exists($cur_path) ) {
if ( !mkdir($cur_path) ) {
return false;
}
}
}
return false;
}
}

Event Timeline