You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
6.6 KiB
PHTML
179 lines
6.6 KiB
PHTML
14 years ago
|
<?php
|
||
|
/**
|
||
|
* Smarty Internal Plugin Resource File
|
||
13 years ago
|
*
|
||
10 years ago
|
* @package Smarty
|
||
14 years ago
|
* @subpackage TemplateResources
|
||
10 years ago
|
* @author Uwe Tews
|
||
|
* @author Rodney Rehm
|
||
14 years ago
|
*/
|
||
|
|
||
13 years ago
|
/**
|
||
14 years ago
|
* Smarty Internal Plugin Resource File
|
||
13 years ago
|
* Implements the file system as resource for Smarty templates
|
||
|
*
|
||
10 years ago
|
* @package Smarty
|
||
13 years ago
|
* @subpackage TemplateResources
|
||
14 years ago
|
*/
|
||
11 years ago
|
class Smarty_Internal_Resource_File extends Smarty_Resource
|
||
|
{
|
||
9 years ago
|
/**
|
||
|
* build template filepath by traversing the template_dir array
|
||
|
*
|
||
|
* @param Smarty_Template_Source $source source object
|
||
|
* @param Smarty_Internal_Template $_template template object
|
||
|
*
|
||
|
* @return string fully qualified filepath
|
||
|
* @throws SmartyException
|
||
|
*/
|
||
|
protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
|
||
|
{
|
||
|
$file = $source->name;
|
||
|
// absolute file ?
|
||
7 years ago
|
if ($file[ 0 ] === '/' || $file[ 1 ] === ':') {
|
||
9 years ago
|
$file = $source->smarty->_realpath($file, true);
|
||
|
return is_file($file) ? $file : false;
|
||
|
}
|
||
|
// go relative to a given template?
|
||
7 years ago
|
if ($file[ 0 ] === '.' && $_template && $_template->_isSubTpl() &&
|
||
9 years ago
|
preg_match('#^[.]{1,2}[\\\/]#', $file)
|
||
|
) {
|
||
7 years ago
|
if ($_template->parent->source->type !== 'file' && $_template->parent->source->type !== 'extends' &&
|
||
7 years ago
|
!isset($_template->parent->_cache[ 'allow_relative_path' ])
|
||
9 years ago
|
) {
|
||
|
throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
|
||
|
}
|
||
|
// normalize path
|
||
7 years ago
|
$path = $source->smarty->_realpath(dirname($_template->parent->source->filepath) . DIRECTORY_SEPARATOR . $file);
|
||
9 years ago
|
// files relative to a template only get one shot
|
||
|
return is_file($path) ? $path : false;
|
||
|
}
|
||
7 years ago
|
// normalize DIRECTORY_SEPARATOR
|
||
|
if (strpos($file, DIRECTORY_SEPARATOR === '/' ? '\\' : '/') !== false) {
|
||
|
$file = str_replace(DIRECTORY_SEPARATOR === '/' ? '\\' : '/', DIRECTORY_SEPARATOR, $file);
|
||
9 years ago
|
}
|
||
|
|
||
|
$_directories = $source->smarty->getTemplateDir(null, $source->isConfig);
|
||
|
// template_dir index?
|
||
7 years ago
|
if ($file[ 0 ] === '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) {
|
||
7 years ago
|
$file = $fileMatch[ 2 ];
|
||
|
$_indices = explode(',', $fileMatch[ 1 ]);
|
||
9 years ago
|
$_index_dirs = array();
|
||
|
foreach ($_indices as $index) {
|
||
|
$index = trim($index);
|
||
|
// try string indexes
|
||
7 years ago
|
if (isset($_directories[ $index ])) {
|
||
|
$_index_dirs[] = $_directories[ $index ];
|
||
9 years ago
|
} elseif (is_numeric($index)) {
|
||
|
// try numeric index
|
||
|
$index = (int) $index;
|
||
7 years ago
|
if (isset($_directories[ $index ])) {
|
||
|
$_index_dirs[] = $_directories[ $index ];
|
||
9 years ago
|
} else {
|
||
|
// try at location index
|
||
|
$keys = array_keys($_directories);
|
||
7 years ago
|
if (isset($_directories[ $keys[ $index ] ])) {
|
||
|
$_index_dirs[] = $_directories[ $keys[ $index ] ];
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if (empty($_index_dirs)) {
|
||
|
// index not found
|
||
|
return false;
|
||
|
} else {
|
||
|
$_directories = $_index_dirs;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// relative file name?
|
||
|
foreach ($_directories as $_directory) {
|
||
|
$path = $_directory . $file;
|
||
|
if (is_file($path)) {
|
||
7 years ago
|
return (strpos($path, '.' . DIRECTORY_SEPARATOR) !== false) ? $source->smarty->_realpath($path) : $path;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
if (!isset($_index_dirs)) {
|
||
|
// Could be relative to cwd
|
||
|
$path = $source->smarty->_realpath($file, true);
|
||
|
if (is_file($path)) {
|
||
|
return $path;
|
||
|
}
|
||
|
}
|
||
|
// Use include path ?
|
||
|
if ($source->smarty->use_include_path) {
|
||
|
return $source->smarty->ext->_getIncludePath->getIncludePath($_directories, $file, $source->smarty);
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
/**
|
||
13 years ago
|
* populate Source Object with meta data from Resource
|
||
|
*
|
||
|
* @param Smarty_Template_Source $source source object
|
||
|
* @param Smarty_Internal_Template $_template template object
|
||
7 years ago
|
*
|
||
|
* @throws \SmartyException
|
||
14 years ago
|
*/
|
||
10 years ago
|
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
|
||
14 years ago
|
{
|
||
13 years ago
|
$source->filepath = $this->buildFilepath($source, $_template);
|
||
14 years ago
|
|
||
13 years ago
|
if ($source->filepath !== false) {
|
||
9 years ago
|
if (isset($source->smarty->security_policy) && is_object($source->smarty->security_policy)) {
|
||
|
$source->smarty->security_policy->isTrustedResourceDir($source->filepath, $source->isConfig);
|
||
13 years ago
|
}
|
||
9 years ago
|
$source->exists = true;
|
||
7 years ago
|
$source->uid = sha1($source->filepath . ($source->isConfig ? $source->smarty->_joined_config_dir :
|
||
|
$source->smarty->_joined_template_dir));
|
||
|
$source->timestamp = filemtime($source->filepath);
|
||
9 years ago
|
} else {
|
||
7 years ago
|
$source->timestamp = $source->exists = false;
|
||
13 years ago
|
}
|
||
|
}
|
||
14 years ago
|
|
||
|
/**
|
||
13 years ago
|
* populate Source Object with timestamp and exists from Resource
|
||
|
*
|
||
|
* @param Smarty_Template_Source $source source object
|
||
14 years ago
|
*/
|
||
13 years ago
|
public function populateTimestamp(Smarty_Template_Source $source)
|
||
14 years ago
|
{
|
||
9 years ago
|
if (!$source->exists) {
|
||
|
$source->timestamp = $source->exists = is_file($source->filepath);
|
||
|
}
|
||
|
if ($source->exists) {
|
||
|
$source->timestamp = filemtime($source->filepath);
|
||
|
}
|
||
13 years ago
|
}
|
||
14 years ago
|
|
||
|
/**
|
||
13 years ago
|
* Load template's source from file into current template object
|
||
|
*
|
||
11 years ago
|
* @param Smarty_Template_Source $source source object
|
||
10 years ago
|
*
|
||
11 years ago
|
* @return string template source
|
||
|
* @throws SmartyException if source cannot be loaded
|
||
14 years ago
|
*/
|
||
13 years ago
|
public function getContent(Smarty_Template_Source $source)
|
||
|
{
|
||
9 years ago
|
if ($source->exists) {
|
||
13 years ago
|
return file_get_contents($source->filepath);
|
||
|
}
|
||
9 years ago
|
throw new SmartyException('Unable to read ' . ($source->isConfig ? 'config' : 'template') .
|
||
|
" {$source->type} '{$source->name}'");
|
||
13 years ago
|
}
|
||
14 years ago
|
|
||
|
/**
|
||
13 years ago
|
* Determine basename for compiled filename
|
||
|
*
|
||
11 years ago
|
* @param Smarty_Template_Source $source source object
|
||
10 years ago
|
*
|
||
11 years ago
|
* @return string resource's basename
|
||
14 years ago
|
*/
|
||
13 years ago
|
public function getBasename(Smarty_Template_Source $source)
|
||
14 years ago
|
{
|
||
9 years ago
|
return basename($source->filepath);
|
||
13 years ago
|
}
|
||
|
}
|