DT(destoon) 標簽解析原理
<?php
defined('IN_DESTOON') or exit('Access Denied');
function tag($parameter, $expires = 0) {
// $DT 網站配置
// $CFG 網站配置
// $MODULE 模塊配置
// $CATEGORY 當前模塊分類信息
// $db 數據庫操作對象
// $DCAT ★★★
// $DT_TIME 當前時間戳
global $DT, $CFG, $MODULE, $CATEGORY, $db, $DCAT, $DT_TIME;
// $CATBAK 當前模塊的分類信息,作為備份使用
$CATBAK = $CATEGORY ? $CATEGORY : array();
if($expires > 0) {
// TAG(標簽)自定義緩存$expires秒;
$tag_expires = $expires;
} else if($expires == -2) {
// SQL查詢緩存,數據庫緩存
$tag_expires = $CFG['db_expires'];
} else if($expires == -1) {
// TAG(標簽)不緩存;
$tag_expires = 0;
} else {
// TAG(標簽)默認配置緩存;
$tag_expires = $CFG['tag_expires'];
}
// 定義TAG(標簽)緩存的變量
$tag_cache = false;
// 定義SQL查詢緩存,數據庫緩存的變量
$db_cache = ($expires == -2 || defined('TOHTML')) ? 'CACHE' : '';
// 定義了tag緩存 && 不是SQL查詢緩存 && 參數字符串不含有&page=字符串(即列表分頁不緩存數據)
if($tag_expires && $db_cache != 'CACHE' && strpos($parameter, '&page=') === false) {
$tag_cache = true;
// tag緩存的文件
$TCF = DT_CACHE.'/tag/'.md5($parameter).'.htm';
// 存在tag緩存文件 并且 緩存文件的生成時間在 TAG(標簽)緩存時間的有效時間內,讀取緩存文件
if(is_file($TCF) && ($DT_TIME - filemtime($TCF) < $tag_expires)) {
echo substr(file_get($TCF), 17);
return;
}
}
// ★★★
$parameter = str_replace(array('&', '%'), array('', '##'), $parameter);
// 參數1轉為變量
parse_str($parameter, $par);
// 空數組,直接返回
if(!is_array($par)) return '';
$par = dstripslashes($par);
extract($par);
######################## 參數 #######################
// 表前綴 無用的代碼,呵呵
isset($prefix) or $prefix = $db->pre;
// 模塊id
isset($moduleid) or $moduleid = 1;
// 不存在模塊,返回
if(!isset($MODULE[$moduleid])) return '';
// fields指查詢的字段,默認為*,可以傳遞例如 fields=title,addtime
isset($fields) or $fields = '*';
// 分類id
isset($catid) or $catid = 0;
// 是否包含分類兒子
isset($child) or $child = 1;
// 地區id
isset($areaid) or $areaid = 0;
// 是否包含地區分類兒子
isset($areachild) or $areachild = 0;
// 定義緩存的目錄
isset($dir) or $dir = 'tag';
// tag默認使用list模板
isset($template) or $template = 'list';
// 定義sql條件變量
isset($condition) or $condition = '1';
// 定義group條件變量
isset($group) or $group = '';
// 定義頁碼變量
isset($page) or $page = 1;
// 定義查詢記錄條數變量
isset($pagesize) or $pagesize = 10;
// ★★★
isset($update) or $update = 0;
// 定義排序變量
isset($order) or $order = '';
// 定義是否顯示分頁變量
isset($showpage) or $showpage = 0;
// 定義日期類型變量
isset($datetype) or $datetype = 0;
// 定義鏈接窗口變量
isset($target) or $target = '';
//
isset($class) or $class = '';
// 定義簡介長度變量
isset($length) or $length = 0;
// 定義簡介變量
isset($introduce) or $introduce = 0;
// 定義調試變量
isset($debug) or $debug = 0;
// 定義列變量(模板中表格使用)
isset($cols) && $cols or $cols = 1;
// ★★★
isset($DCAT) or $DCAT = array();
if(isset($DCAT[$moduleid])) {
$CATEGORY = $DCAT[$moduleid];
} else {
$CATEGORY = $DCAT[$moduleid] = cache_read('category-'.$moduleid.'.php');
}
if($catid && $moduleid > 4) {
if(is_numeric($catid)) {
$condition .= ($child && $CATEGORY[$catid]['child']) ? " AND catid IN (".$CATEGORY[$catid]['arrchildid'].")" : " AND catid=$catid";
} else {
if($child) {
$_catids = explode(',', $catid);
$catids = '';
foreach($_catids as $_c) {
$catids .= ','.($CATEGORY[$_c]['child'] ? $CATEGORY[$_c]['arrchildid'] : $_c);
}
$catids = substr($catids, 1);
$condition .= " AND catid IN ($catids)";
} else {
$condition .= " AND catid IN ($catid)";
}
}
}
if($areaid) {
$AREA = cache_read('area.php');
if(is_numeric($areaid)) {
$condition .= ($areachild && $AREA[$areaid]['child']) ? " AND areaid IN (".$AREA[$areaid]['arrchildid'].")" : " AND areaid=$areaid";
} else {
if($areachild) {
$_areaids = explode(',', $areaid);
$areaids = '';
foreach($_areaids as $_a) {
$areaids .= ','.($AREA[$_a]['child'] ? $AREA[$_a]['arrchildid'] : $_a);
}
$areaids = substr($areaids, 1);
$condition .= " AND areaid IN ($areaids)";
} else {
$condition .= " AND areaid IN ($areaid)";
}
}
}
// 模塊的鏈接地址
$path = $MODULE[$moduleid]['linkurl'];
// 讀取表名
$table = isset($table) ? 1 : get_table($moduleid);
// 起始記錄
$offset = ($page-1)*$pagesize;
// 估摸著是在模板中使用,表格使用的
$percent = dround(100/$cols).'%';
// 定義記錄數量
$num = 0;
// sql 分組條件
$group = $group ? " GROUP BY $group" : '';
// sql 排序條件
$order = $order ? " ORDER BY $order" : '';
// stripslashes
$condition = stripslashes($condition);
// ★★★
$condition = str_replace('##', '%', $condition);
// ★★★
if($showpage) {
$num = $db->count($table, $condition.$group, $tag_expires ? $tag_expires : $CFG['db_expires']);
if($catid) {
if($page < 3 && $update) update_item($catid, $num);
$pages = listpages($moduleid, $catid, $num, $page, $pagesize, $CATEGORY);
} else {
$pages = pages($num, $page, $pagesize);
}
}
// $template為null,使用SQL查詢緩存,數據庫緩存
if($template == 'null') $db_cache = 'CACHE';
// sql
$query = "SELECT $fields FROM {$table} WHERe {$condition}{$group}{$order} LIMIT $offset,$pagesize";
// 調試
if($debug) echo $parameter.'<br/>'.$query.'<br/>';
//
$tags = array();
// rs
$result = $db->query($query, $db_cache, $tag_expires);
// rs 2 array
while($r = $db->fetch_array($result)) {
if(isset($r['title'])) {
$r['title'] = str_replace('"', '"', trim($r['title']));
$r['alt'] = $r['title'];
if($length) $r['title'] = dsubstr($r['title'], $length);
if(isset($r['style']) && $r['style']) $r['title'] = set_style($r['title'], $r['style']);
}
if(isset($r['introduce']) && $introduce) $r['introduce'] = dsubstr($r['introduce'], $introduce);
if(isset($r['linkurl']) && $r['linkurl'] && strpos($r['linkurl'], '://') === false) $r['linkurl'] = $path.$r['linkurl'];
$tags[] = $r;
}
// 不tag緩存,返回array
if($template == 'null') {
$CATEGORY = $CATBAK;
return $tags;
}
// 生成tag緩存
if($tag_cache) {
ob_start();
include template($template, $dir);
$contents = ob_get_contents();
ob_clean();
file_put($TCF, '<!--'.($DT_TIME + $tag_expires).'-->'.$contents);
$CATEGORY = $CATBAK;
echo $contents;
// 不tag緩存,返回array
} else {
include template($template, $dir);
$CATEGORY = $CATBAK;
}
}
?>
<?php
defined('IN_DESTOON') or exit('Access Denied');
function tag($parameter, $expires = 0) {
// $DT 網站配置
// $CFG 網站配置
// $MODULE 模塊配置
// $CATEGORY 當前模塊分類信息
// $db 數據庫操作對象
// $DCAT ★★★
// $DT_TIME 當前時間戳
global $DT, $CFG, $MODULE, $CATEGORY, $db, $DCAT, $DT_TIME;
// $CATBAK 當前模塊的分類信息,作為備份使用
$CATBAK = $CATEGORY ? $CATEGORY : array();
if($expires > 0) {
// TAG(標簽)自定義緩存$expires秒;
$tag_expires = $expires;
} else if($expires == -2) {
// SQL查詢緩存,數據庫緩存
$tag_expires = $CFG['db_expires'];
} else if($expires == -1) {
// TAG(標簽)不緩存;
$tag_expires = 0;
} else {
// TAG(標簽)默認配置緩存;
$tag_expires = $CFG['tag_expires'];
}
// 定義TAG(標簽)緩存的變量
$tag_cache = false;
// 定義SQL查詢緩存,數據庫緩存的變量
$db_cache = ($expires == -2 || defined('TOHTML')) ? 'CACHE' : '';
// 定義了tag緩存 && 不是SQL查詢緩存 && 參數字符串不含有&page=字符串(即列表分頁不緩存數據)
if($tag_expires && $db_cache != 'CACHE' && strpos($parameter, '&page=') === false) {
$tag_cache = true;
// tag緩存的文件
$TCF = DT_CACHE.'/tag/'.md5($parameter).'.htm';
// 存在tag緩存文件 并且 緩存文件的生成時間在 TAG(標簽)緩存時間的有效時間內,讀取緩存文件
if(is_file($TCF) && ($DT_TIME - filemtime($TCF) < $tag_expires)) {
echo substr(file_get($TCF), 17);
return;
}
}
// ★★★
$parameter = str_replace(array('&', '%'), array('', '##'), $parameter);
// 參數1轉為變量
parse_str($parameter, $par);
// 空數組,直接返回
if(!is_array($par)) return '';
$par = dstripslashes($par);
extract($par);
######################## 參數 #######################
// 表前綴 無用的代碼,呵呵
isset($prefix) or $prefix = $db->pre;
// 模塊id
isset($moduleid) or $moduleid = 1;
// 不存在模塊,返回
if(!isset($MODULE[$moduleid])) return '';
// fields指查詢的字段,默認為*,可以傳遞例如 fields=title,addtime
isset($fields) or $fields = '*';
// 分類id
isset($catid) or $catid = 0;
// 是否包含分類兒子
isset($child) or $child = 1;
// 地區id
isset($areaid) or $areaid = 0;
// 是否包含地區分類兒子
isset($areachild) or $areachild = 0;
// 定義緩存的目錄
isset($dir) or $dir = 'tag';
// tag默認使用list模板
isset($template) or $template = 'list';
// 定義sql條件變量
isset($condition) or $condition = '1';
// 定義group條件變量
isset($group) or $group = '';
// 定義頁碼變量
isset($page) or $page = 1;
// 定義查詢記錄條數變量
isset($pagesize) or $pagesize = 10;
// ★★★
isset($update) or $update = 0;
// 定義排序變量
isset($order) or $order = '';
// 定義是否顯示分頁變量
isset($showpage) or $showpage = 0;
// 定義日期類型變量
isset($datetype) or $datetype = 0;
// 定義鏈接窗口變量
isset($target) or $target = '';
//
isset($class) or $class = '';
// 定義簡介長度變量
isset($length) or $length = 0;
// 定義簡介變量
isset($introduce) or $introduce = 0;
// 定義調試變量
isset($debug) or $debug = 0;
// 定義列變量(模板中表格使用)
isset($cols) && $cols or $cols = 1;
// ★★★
isset($DCAT) or $DCAT = array();
if(isset($DCAT[$moduleid])) {
$CATEGORY = $DCAT[$moduleid];
} else {
$CATEGORY = $DCAT[$moduleid] = cache_read('category-'.$moduleid.'.php');
}
if($catid && $moduleid > 4) {
if(is_numeric($catid)) {
$condition .= ($child && $CATEGORY[$catid]['child']) ? " AND catid IN (".$CATEGORY[$catid]['arrchildid'].")" : " AND catid=$catid";
} else {
if($child) {
$_catids = explode(',', $catid);
$catids = '';
foreach($_catids as $_c) {
$catids .= ','.($CATEGORY[$_c]['child'] ? $CATEGORY[$_c]['arrchildid'] : $_c);
}
$catids = substr($catids, 1);
$condition .= " AND catid IN ($catids)";
} else {
$condition .= " AND catid IN ($catid)";
}
}
}
if($areaid) {
$AREA = cache_read('area.php');
if(is_numeric($areaid)) {
$condition .= ($areachild && $AREA[$areaid]['child']) ? " AND areaid IN (".$AREA[$areaid]['arrchildid'].")" : " AND areaid=$areaid";
} else {
if($areachild) {
$_areaids = explode(',', $areaid);
$areaids = '';
foreach($_areaids as $_a) {
$areaids .= ','.($AREA[$_a]['child'] ? $AREA[$_a]['arrchildid'] : $_a);
}
$areaids = substr($areaids, 1);
$condition .= " AND areaid IN ($areaids)";
} else {
$condition .= " AND areaid IN ($areaid)";
}
}
}
// 模塊的鏈接地址
$path = $MODULE[$moduleid]['linkurl'];
// 讀取表名
$table = isset($table) ? 1 : get_table($moduleid);
// 起始記錄
$offset = ($page-1)*$pagesize;
// 估摸著是在模板中使用,表格使用的
$percent = dround(100/$cols).'%';
// 定義記錄數量
$num = 0;
// sql 分組條件
$group = $group ? " GROUP BY $group" : '';
// sql 排序條件
$order = $order ? " ORDER BY $order" : '';
// stripslashes
$condition = stripslashes($condition);
// ★★★
$condition = str_replace('##', '%', $condition);
// ★★★
if($showpage) {
$num = $db->count($table, $condition.$group, $tag_expires ? $tag_expires : $CFG['db_expires']);
if($catid) {
if($page < 3 && $update) update_item($catid, $num);
$pages = listpages($moduleid, $catid, $num, $page, $pagesize, $CATEGORY);
} else {
$pages = pages($num, $page, $pagesize);
}
}
// $template為null,使用SQL查詢緩存,數據庫緩存
if($template == 'null') $db_cache = 'CACHE';
// sql
$query = "SELECT $fields FROM {$table} WHERe {$condition}{$group}{$order} LIMIT $offset,$pagesize";
// 調試
if($debug) echo $parameter.'<br/>'.$query.'<br/>';
//
$tags = array();
// rs
$result = $db->query($query, $db_cache, $tag_expires);
// rs 2 array
while($r = $db->fetch_array($result)) {
if(isset($r['title'])) {
$r['title'] = str_replace('"', '"', trim($r['title']));
$r['alt'] = $r['title'];
if($length) $r['title'] = dsubstr($r['title'], $length);
if(isset($r['style']) && $r['style']) $r['title'] = set_style($r['title'], $r['style']);
}
if(isset($r['introduce']) && $introduce) $r['introduce'] = dsubstr($r['introduce'], $introduce);
if(isset($r['linkurl']) && $r['linkurl'] && strpos($r['linkurl'], '://') === false) $r['linkurl'] = $path.$r['linkurl'];
$tags[] = $r;
}
// 不tag緩存,返回array
if($template == 'null') {
$CATEGORY = $CATBAK;
return $tags;
}
// 生成tag緩存
if($tag_cache) {
ob_start();
include template($template, $dir);
$contents = ob_get_contents();
ob_clean();
file_put($TCF, '<!--'.($DT_TIME + $tag_expires).'-->'.$contents);
$CATEGORY = $CATBAK;
echo $contents;
// 不tag緩存,返回array
} else {
include template($template, $dir);
$CATEGORY = $CATBAK;
}
}
?>
樂發網超市批發網提供超市貨源信息,超市采購進貨渠道。超市進貨網提供成都食品批發,日用百貨批發信息、微信淘寶網店超市采購信息和超市加盟信息.打造國內超市采購商與批發市場供應廠商搭建網上批發市場平臺,是全國批發市場行業中電子商務權威性網站。
本文來源: DT(destoon) 標簽解析原理