sibtracker/system/classes/templates.class.php
2019-05-18 13:46:03 +08:00

225 lines
7.4 KiB
PHP

<?php
class dle_template {
var $dir = '.';
var $template = null;
var $copy_template = null;
var $data = array ();
var $block_data = array ();
var $result = array ('info' => '', 'vote' => '', 'speedbar' => '', 'content' => '' );
var $allow_php_include = true;
function set($name, $var) {
if( is_array( $var ) && count( $var ) ) {
foreach ( $var as $key => $key_var ) {
$this->set( $key, $key_var );
}
} else
$this->data[$name] = $var;
}
function set_block($name, $var) {
if( is_array( $var ) && count( $var ) ) {
foreach ( $var as $key => $key_var ) {
$this->set_block( $key, $key_var );
}
} else
$this->block_data[$name] = $var;
}
function load_template($tpl_name) {
if( $this->CacheTemplate[ $tpl_name ] ){
$this->copy_template = $this->CacheTemplate[ $tpl_name ];
return true;
}
if( $tpl_name == '' || ! file_exists( $this->dir . DIRECTORY_SEPARATOR . $tpl_name ) ) {
die( "Íåâîçìîæíî çàãðóçèòü øàáëîí: " . $tpl_name );
return false;
}
$this->template = file_get_contents( $this->dir . DIRECTORY_SEPARATOR . $tpl_name );
if (strpos ( $this->template, "[aviable=" ) !== false) {$this->template = preg_replace ( "#\\[aviable=(.+?)\\](.*?)\\[/aviable\\]#ies", "\$this->check_module('\\1', '\\2')", $this->template );}
if (strpos ( $this->template, "[not-aviable=" ) !== false) {$this->template = preg_replace ( "#\\[not-aviable=(.+?)\\](.*?)\\[/not-aviable\\]#ies", "\$this->check_module('\\1', '\\2', false)", $this->template );}
if (strpos ( $this->template, "[not-group=" ) !== false) {$this->template = preg_replace ( "#\\[not-group=(.+?)\\](.*?)\\[/not-group\\]#ies", "\$this->check_group('\\1', '\\2', false)", $this->template );}
if (strpos ( $this->template, "[group=" ) !== false) {$this->template = preg_replace ( "#\\[group=(.+?)\\](.*?)\\[/group\\]#ies", "\$this->check_group('\\1', '\\2')", $this->template );}
if (strpos ( $this->template, "{include file=" ) !== false ) {$this->template = preg_replace( "#\\{include file=['\"](.+?)['\"]\\}#ies", "\$this->load_file('\\1', 'tpl')", $this->template );}
$this->copy_template = $this->template;
$this->CacheTemplate[ $tpl_name ] = $this->template;
return true;
}
function load_file( $name, $include_file = "tpl" ) {
global $db, $is_logged, $member_id, $cat_info, $config, $user_group, $category_id, $_TIME, $lang, $smartphone_detected, $dle_module;
$name = str_replace( '..', '', $name );
$url = @parse_url ($name);
$type = explode( ".", $url['path'] );
$type = strtolower( end( $type ) );
if ($type == "tpl") {return $this->sub_load_template( $name );}
if ($include_file == "php") {
if ( !$this->allow_php_include ) return;
if ($type != "php") return "Äëÿ ïîäêëþ÷åíèÿ äîïóñêàþòñÿ òîëüêî ôàéëû ñ ðàñøèðåíèåì .tpl èëè .php";
if ($url['path']{0} == "/" )$file_path = dirname (ROOT_DIR.$url['path']);
else $file_path = dirname (ROOT_DIR."/".$url['path']);
$file_name = pathinfo($url['path']);
$file_name = $file_name['basename'];
if ( stristr ( php_uname( "s" ) , "windows" ) === false )
$chmod_value = @decoct(@fileperms($file_path)) % 1000;
if ($chmod_value == 777 ) return "Ôàéë {$url['path']} íàõîäèòñÿ â ïàïêå, êîòîðàÿ äîñòóïíà äëÿ çàïèñè (CHMOD 777).  öåëÿõ áåçîïàñíîñòè ïîäêëþ÷åíèå ôàéëîâ èç òàêèõ ïàïîê íåâîçìîæíî. Èçìåíèòå ïðàâà íà ïàïêó, ÷òîáû íà íåå íåáûëî ïðàâ íà çàïèñü.";
if ( !file_exists($file_path."/".$file_name) ) return "Ôàéë {$url['path']} íå íàéäåí, åãî çàãðóçêà íåâîçìîæíà.";
if ( $url['query'] ) {parse_str( $url['query'] );}
ob_start();
$tpl = new dle_template( );
$tpl->dir = TEMPLATE_DIR;
include $file_path."/".$file_name;
return ob_get_clean();
}
return '{include file="'.$name.'"}';
}
function sub_load_template( $tpl_name ) {
$tpl_name = totranslit( $tpl_name );
if( $tpl_name == '' || ! file_exists( $this->dir . DIRECTORY_SEPARATOR . $tpl_name ) ) {
return "Îòñóòñòâóåò ôàéë øàáëîíà: " . $tpl_name ;
return false;
}
$template = file_get_contents( $this->dir . DIRECTORY_SEPARATOR . $tpl_name );
if (strpos ( $template, "[aviable=" ) !== false) {$template = preg_replace ( "#\\[aviable=(.+?)\\](.*?)\\[/aviable\\]#ies", "\$this->check_module('\\1', '\\2')", $template );}
if (strpos ( $template, "[not-aviable=" ) !== false) {$template = preg_replace ( "#\\[not-aviable=(.+?)\\](.*?)\\[/not-aviable\\]#ies", "\$this->check_module('\\1', '\\2', false)", $template );}
if (strpos ( $template, "[not-group=" ) !== false) {$template = preg_replace ( "#\\[not-group=(.+?)\\](.*?)\\[/not-group\\]#ies", "\$this->check_group('\\1', '\\2', false)", $template );}
if (strpos ( $this->template, "{if " ) !== false) {$this->template = preg_replace ( "#{if (.+?)}(.*?){/if}#ies", "$this->check_else('1', '2', false)", $this->template );}
if (strpos ( $template, "[group=" ) !== false) {$template = preg_replace ( "#\\[group=(.+?)\\](.*?)\\[/group\\]#ies", "\$this->check_group('\\1', '\\2')", $template );}
return $template;
}
function check_module($aviable, $block, $action = true) {
global $dle_module;
$aviable = explode( '|', $aviable );
$block = str_replace( '\"', '"', $block );
if( $action ) {
if( ! (in_array( $dle_module, $aviable )) and ($aviable[0] != "global") ) return "";
else return $block;
} else {
if( (in_array( $dle_module, $aviable )) ) return "";
else return $block;
}
}
function check_group($groups, $block, $action = true) {
global $member_id;
$groups = explode( ',', $groups );
if( $action ) {
if( ! in_array( $member_id['user_group'], $groups ) ) return "";
} else {
if( in_array( $member_id['user_group'], $groups ) ) return "";
}
$block = str_replace( '\"', '"', $block );
return $block;
}
function _clear() {
$this->data = array ();
$this->block_data = array ();
$this->copy_template = $this->template;
}
function clear() {
$this->data = array ();
$this->block_data = array ();
$this->copy_template = null;
$this->template = null;
}
function global_clear() {
$this->data = array ();
$this->block_data = array ();
$this->result = array ();
$this->copy_template = null;
$this->template = null;
}
function compile($tpl) {
if( count( $this->block_data ) ) {
foreach ( $this->block_data as $key_find => $key_replace ) {
$find_preg[] = $key_find;
$replace_preg[] = $key_replace;
}
$this->copy_template = preg_replace( $find_preg, $replace_preg, $this->copy_template );
}
foreach ( $this->data as $key_find => $key_replace ) {
$find[] = $key_find;
$replace[] = $key_replace;
}
$this->copy_template = str_replace( $find, $replace, $this->copy_template );
if( strpos( $this->copy_template, "{include file=" ) !== false ) {
$this->copy_template = preg_replace( "#\\{include file=['\"](.+?)['\"]\\}#ies", "\$this->load_file('\\1', 'php')", $this->copy_template );
}
if( isset( $this->result[$tpl] ) ) $this->result[$tpl] .= $this->copy_template;
else $this->result[$tpl] = $this->copy_template;
$this->_clear();
}
function check_else($condition, $block){
global $GLOBALS;
extract($GLOBALS, EXTR_SKIP, "");
if(is_array($matches=explode("{else}",$block))) {
$block=$matches[0];
$else=$matches[1];
}
if(eval(("return $condition;"))) return str_replace( '"', '"', $block );
return str_replace( '"', '"', $else );
}
}
?>