ecs_shop_config -> 商店配置(template, stylename)
load_config() -> lib_common.php
includes/init.php 文件做了 smarty 文件初始化:
require(ROOT_PATH . 'includes/cls_template.php');
$smarty = new cls_template;
assign_template() 函数封装在 lib_main.php
strrchr($haystack, $needle);
stat();
strncmp();
"(.*?)"
http://deerchao.net/tutorials/regex/regex.htm
/**
* 处理模板文件
*
* @access public
* @param string $filename
* @param sting $cache_id
*
* @return sring
*/
function fetch($filename, $cache_id = '')
{
if (!$this->_seterror)
{
error_reporting(E_ALL ^ E_NOTICE);
}
$this->_seterror++;
if (strncmp($filename,'str:', 4) == 0)
{
$out = $this->_eval($this->fetch_str(substr($filename, 4)));
}
else
{
if ($this->_checkfile)
{
if (!file_exists($filename))
{
$filename = $this->template_dir . '/' . $filename;
}
}
else
{
$filename = $this->template_dir . '/' . $filename;
}
if ($this->direct_output)
{
$this->_current_file = $filename;
$out = $this->_eval($this->fetch_str(file_get_contents($filename)));
}
else
{
if ($cache_id && $this->caching)
{
$out = $this->template_out;
}
else
{
if (!in_array($filename, $this->template))
{
$this->template[] = $filename;
}
$out = $this->make_compiled($filename);
if ($cache_id)
{
$cachename = basename($filename, strrchr($filename, '.')) . '_' . $cache_id;
$data = serialize(array('template' => $this->template, 'expires' => $this->_nowtime + $this->cache_lifetime, 'maketime' => $this->_nowtime));
$out = str_replace("\r", '', $out);
while (strpos($out, "\n\n") !== false)
{
$out = str_replace("\n\n", "\n", $out);
}
$hash_dir = $this->cache_dir . '/' . substr(md5($cachename), 0, 1);
if (!is_dir($hash_dir))
{
mkdir($hash_dir);
}
if (file_put_contents($hash_dir . '/' . $cachename . '.php', '<?php exit;?>' . $data . $out, LOCK_EX) === false)
{
trigger_error('can\'t write:' . $hash_dir . '/' . $cachename . '.php');
}
$this->template = array();
}
}
}
}
$this->_seterror--;
if (!$this->_seterror)
{
error_reporting($this->_errorlevel);
}
return $out; // 返回html数据
}
smarty_prefilter_preCompile() -> cls_template.php (class cls_template)
说明:主要是进行针对引入库文件的部分进行预编译,
<!-- #BeginLibraryItem "...." -->
<!--\s#EndLibraryItem\s-->
编译成为 {include file="..."}.
/**
* 处理{}标签
*
* @access public
* @param string $tag
*
* @return sring
*/
function select($tag)
{
$tag = stripslashes(trim($tag));
if (empty($tag))
{
return '{}';
}
elseif ($tag{0} == '*' && substr($tag, -1) == '*') // 注释部分
{
return '';
}
elseif ($tag{0} == '$') // 变量
{
// if(strpos($tag,"'") || strpos($tag,"]"))
// {
// return '';
// }
return '<?php echo ' . $this->get_val(substr($tag, 1)) . '; ?>';
}
elseif ($tag{0} == '/') // 结束 tag
{
switch (substr($tag, 1))
{
case 'if':
return '<?php endif; ?>';
break;
case 'foreach':
if ($this->_foreachmark == 'foreachelse')
{
$output = '<?php endif; unset($_from); ?>';
}
else
{
array_pop($this->_patchstack);
$output = '<?php endforeach; endif; unset($_from); ?>';
}
$output .= "<?php \$this->pop_vars();; ?>";
return $output;
break;
case 'literal':
return '';
break;
default:
return '{'. $tag .'}';
break;
}
}
else
{
$tag_arr = explode(' ', $tag);
$tag_sel = array_shift($tag_arr);
switch ($tag_sel)
{
case 'if':
return $this->_compile_if_tag(substr($tag, 3));
break;
case 'else':
return '<?php else: ?>';
break;
case 'elseif':
return $this->_compile_if_tag(substr($tag, 7), true);
break;
case 'foreachelse':
$this->_foreachmark = 'foreachelse';
return '<?php endforeach; else: ?>';
break;
case 'foreach':
$this->_foreachmark = 'foreach';
if(!isset($this->_patchstack))
{
$this->_patchstack = array();
}
return $this->_compile_foreach_start(substr($tag, 8));
break;
case 'assign':
$t = $this->get_para(substr($tag, 7),0);
if ($t['value']{0} == '$')
{
/* 如果传进来的值是变量,就不用用引号 */
$tmp = '$this->assign(\'' . $t['var'] . '\',' . $t['value'] . ');';
}
else
{
$tmp = '$this->assign(\'' . $t['var'] . '\',\'' . addcslashes($t['value'], "'") . '\');';
}
// $tmp = $this->assign($t['var'], $t['value']);
return '<?php ' . $tmp . ' ?>';
break;
case 'include':
$t = $this->get_para(substr($tag, 8), 0);
return '<?php echo $this->fetch(' . "'$t[file]'" . '); ?>';
break;
case 'insert_scripts':
$t = $this->get_para(substr($tag, 15), 0);
return '<?php echo $this->smarty_insert_scripts(' . $this->make_array($t) . '); ?>';
break;
case 'create_pages':
$t = $this->get_para(substr($tag, 13), 0);
return '<?php echo $this->smarty_create_pages(' . $this->make_array($t) . '); ?>';
break;
case 'insert' :
$t = $this->get_para(substr($tag, 7), false);
//var_dump($t);exit;
if (!function_exists('version_compare') || version_compare(phpversion(), '5.3.0', '<')) {
$out = "<?php \n" . '$k = ' . preg_replace("/(\'\\$[^,]+)/e" , "stripslashes(trim('\\1','\''));", var_export($t, true)) . ";\n";
} else {
include(ROOT_PATH . 'includes' . DIRECTORY_SEPARATOR . 'patch' . DIRECTORY_SEPARATOR . 'includes_cls_template_select.php');
}
$out .= 'echo $this->_echash . $k[\'name\'] . \'|\' . serialize($k) . $this->_echash;' . "\n?>";
//var_dump($out);
return $out;
break;
case 'literal':
return '';
break;
case 'cycle' :
$t = $this->get_para(substr($tag, 6), 0);
return '<?php echo $this->cycle(' . $this->make_array($t) . '); ?>';
break;
case 'html_options':
$t = $this->get_para(substr($tag, 13), 0);
return '<?php echo $this->html_options(' . $this->make_array($t) . '); ?>';
break;
case 'html_select_date':
$t = $this->get_para(substr($tag, 17), 0);
return '<?php echo $this->html_select_date(' . $this->make_array($t) . '); ?>';
break;
case 'html_radios':
$t = $this->get_para(substr($tag, 12), 0);
return '<?php echo $this->html_radios(' . $this->make_array($t) . '); ?>';
break;
case 'html_select_time':
$t = $this->get_para(substr($tag, 12), 0);
return '<?php echo $this->html_select_time(' . $this->make_array($t) . '); ?>';
break;
case 'var_dump':
$t = $this->get_para(substr($tag, 12), 0);
echo '<?php var_dump($t);?>';
exit;
default:
return '{' . $tag . '}';
break;
}
}
}
/**
* 显示页面函数
*
* @access public
* @param string $filename
* @param sting $cache_id
*
* @return void
*/
function display($filename, $cache_id = '')
{
$this->_seterror++;
error_reporting(E_ALL ^ E_NOTICE);
$this->_checkfile = false;
$out = $this->fetch($filename, $cache_id);
if (strpos($out, $this->_echash) !== false)
{
$k = explode($this->_echash, $out);
foreach ($k AS $key => $val)
{
if (($key % 2) == 1)
{
$k[$key] = $this->insert_mod($val);
}
}
$out = implode('', $k);
}
error_reporting($this->_errorlevel);
$this->_seterror--;
echo $out;
}
function insert_mod($name) // 处理动态内容
{
echo $name . "###--<br/>";
list($fun, $para) = explode('|', $name);
$para = unserialize($para);
echo $fun = 'insert_' . $fun;
return $fun($para);
}
// 以下这些函数,都是在 lib_insert.php
insert_member_info($para);
insert_cart_info($para);
/**
* 调用指定的广告位的广告
*
* @access public
* @param integer $id 广告位ID
* @param integer $num 广告数量
* @return string
*/
function insert_ads($arr)
{
static $static_res = NULL;
$arr['num'] = intval($arr['num']);
$arr['id'] = intval($arr['id']);
$time = gmtime();
if (!empty($arr['num']) && $arr['num'] != 1)
{
$sql = 'SELECT a.ad_id, a.position_id, a.media_type, a.ad_link, a.ad_code, a.ad_name, p.ad_width, ' .
'p.ad_height, p.position_style, RAND() AS rnd ' .
'FROM ' . $GLOBALS['ecs']->table('ad') . ' AS a '.
'LEFT JOIN ' . $GLOBALS['ecs']->table('ad_position') . ' AS p ON a.position_id = p.position_id ' .
"WHERE enabled = 1 AND start_time <= '" . $time . "' AND end_time >= '" . $time . "' ".
"AND a.position_id = '" . $arr['id'] . "' " .
'ORDER BY rnd LIMIT ' . $arr['num'];
$res = $GLOBALS['db']->GetAll($sql);
}
else
{
if ($static_res[$arr['id']] === NULL)
{
$sql = 'SELECT a.ad_id, a.position_id, a.media_type, a.ad_link, a.ad_code, a.ad_name, p.ad_width, '.
'p.ad_height, p.position_style, RAND() AS rnd ' .
'FROM ' . $GLOBALS['ecs']->table('ad') . ' AS a '.
'LEFT JOIN ' . $GLOBALS['ecs']->table('ad_position') . ' AS p ON a.position_id = p.position_id ' .
"WHERE enabled = 1 AND a.position_id = '" . $arr['id'] .
"' AND start_time <= '" . $time . "' AND end_time >= '" . $time . "' " .
'ORDER BY rnd LIMIT 1';
$static_res[$arr['id']] = $GLOBALS['db']->GetAll($sql);
}
$res = $static_res[$arr['id']];
}
$ads = array();
$position_style = '';
foreach ($res AS $row)
{
if ($row['position_id'] != $arr['id'])
{
continue;
}
$position_style = $row['position_style'];
switch ($row['media_type'])
{
case 0: // 图片广告
$src = (strpos($row['ad_code'], 'http://') === false && strpos($row['ad_code'], 'https://') === false) ?
DATA_DIR . "/afficheimg/$row[ad_code]" : $row['ad_code'];
$ads[] = "<a href='affiche.php?ad_id=$row[ad_id]&uri=" .urlencode($row["ad_link"]). "'
target='_blank'><img src='$src' width='" .$row['ad_width']. "' height='$row[ad_height]'
border='0' /></a>";
break;
case 1: // Flash
$src = (strpos($row['ad_code'], 'http://') === false && strpos($row['ad_code'], 'https://') === false) ?
DATA_DIR . "/afficheimg/$row[ad_code]" : $row['ad_code'];
$ads[] = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" " .
"codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" " .
"width='$row[ad_width]' height='$row[ad_height]'>
<param name='movie' value='$src'>
<param name='quality' value='high'>
<embed src='$src' quality='high'
pluginspage='http://www.macromedia.com/go/getflashplayer'
type='application/x-shockwave-flash' width='$row[ad_width]'
height='$row[ad_height]'></embed>
</object>";
break;
case 2: // CODE
$ads[] = $row['ad_code'];
break;
case 3: // TEXT
$ads[] = "<a href='affiche.php?ad_id=$row[ad_id]&uri=" .urlencode($row["ad_link"]). "'
target='_blank'>" .htmlspecialchars($row['ad_code']). '</a>';
break;
}
}
echo "<br/>$$$$$$" . $position_style = 'str:' . $position_style;
$need_cache = $GLOBALS['smarty']->caching;
$GLOBALS['smarty']->caching = false;
$GLOBALS['smarty']->assign('ads', $ads);
$val = $GLOBALS['smarty']->fetch($position_style);
$GLOBALS['smarty']->caching = $need_cache;
echo "<pre>";
echo $val;
echo "</pre>";
return $val;
}