template.core.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. <?php
  2. /*
  3. * Edition: ET080708
  4. * Desc: Core Engine 3 (Memcache/Compile/Replace)
  5. * File: template.core.php
  6. * Author: David Meng
  7. * Site: http://www.systn.com
  8. * Email: mdchinese@gmail.com
  9. *
  10. */
  11. error_reporting(0);
  12. define("ET3!",TRUE);
  13. class ETCore{
  14. var $ThisFile = ''; //当前文件
  15. var $IncFile = ''; //引入文件
  16. var $ThisValue = array(); //当前数值
  17. var $FileList = array(); //载入文件列表
  18. var $IncList = array(); //引入文件列表
  19. var $ImgDir = array('images'); //图片地址目录
  20. var $HtmDir = 'cache_htm/'; //静态存放的目录
  21. var $HtmID = ''; //静态文件ID
  22. var $HtmTime = '180'; //秒为单位,默认三分钟
  23. var $AutoImage = 1; //自动解析图片目录开关默认值
  24. var $Hacker = "<?php if(!defined('ET3!')){die('You are Hacker!<br>Power by Ease Template!');}";
  25. var $Compile = array();
  26. var $Analysis = array();
  27. var $Emc = array();
  28. /**
  29. * 声明模板用法
  30. */
  31. function ETCoreStart(
  32. $set = array(
  33. 'ID' =>'1', //缓存ID
  34. 'TplType' =>'htm', //模板格式
  35. 'CacheDir' =>'cache', //缓存目录
  36. 'TemplateDir'=>'template' , //模板存放目录
  37. 'AutoImage' =>'on' , //自动解析图片目录开关 on表示开放 off表示关闭
  38. 'LangDir' =>'language' , //语言文件存放的目录
  39. 'Language' =>'default' , //语言的默认文件
  40. 'Copyright' =>'off' , //版权保护
  41. 'MemCache' =>'' , //Memcache服务器地址例如:127.0.0.1:11211
  42. )
  43. ){
  44. $this->TplID = (defined('TemplateID')?TemplateID:( ((int)@$set['ID']<=1)?1:(int)$set['ID']) ).'_';
  45. $this->CacheDir = (defined('NewCache')?NewCache:( (trim($set['CacheDir']) != '')?$set['CacheDir']:'cache') ).'/';
  46. $this->TemplateDir = (defined('NewTemplate')?NewTemplate:( (trim($set['TemplateDir']) != '')?$set['TemplateDir']:'template') ).'/';
  47. $this->Ext = (@$set['TplType'] != '')?$set['TplType']:'htm';
  48. $this->AutoImage = (@$set['AutoImage']=='off')?0:1;
  49. $this->Copyright = (@$set['Copyright']=='off')?0:1;
  50. $this->Server = (is_array($GLOBALS['_SERVER']))?$GLOBALS['_SERVER']:$_SERVER;
  51. $this->version = (trim($_GET['EaseTemplateVer']))?die('Ease Templae E3!'):'';
  52. //载入语言文件
  53. $this->LangDir = (defined('LangDir')?LangDir:( ((@$set['LangDir']!='language' && @$set['LangDir'])?$set['LangDir']:'language') )).'/';
  54. if(is_dir($this->LangDir)){
  55. $this->Language = (defined('Language')?Language:( (($set['Language']!='default' && $set['Language'])?$set['Language']:'default') ));
  56. if(@is_file($this->LangDir.$this->Language.'.php')){
  57. $lang = array();
  58. @include_once $this->LangDir.$this->Language.'.php';
  59. $this->LangData = $lang;
  60. }
  61. }else{
  62. $this->Language = 'default';
  63. }
  64. //缓存目录检测以及运行模式
  65. if(@ereg(':',$set['MemCache'])){
  66. $this->RunType = 'MemCache';
  67. $memset = explode(":",$set['MemCache']);
  68. $this->Emc = memcache_connect($memset[0], $memset[1]) OR die("Could not connect!");
  69. }else{
  70. $this->RunType = (@substr(@sprintf('%o', @fileperms($this->CacheDir)), -3)==777 && is_dir($this->CacheDir))?'Cache':'Replace';
  71. }
  72. $CompileBasic = array(
  73. '/(\{\s*|<!--\s*)inc_php:([a-zA-Z0-9_\[\]\.\,\/\?\=\#\:\;\-\|\^]{5,200})(\s*\}|\s*-->)/eis',
  74. '/<!--\s*DEL\s*-->/is',
  75. '/<!--\s*IF(\[|\()(.+?)(\]|\))\s*-->/is',
  76. '/<!--\s*ELSEIF(\[|\()(.+?)(\]|\))\s*-->/is',
  77. '/<!--\s*ELSE\s*-->/is',
  78. '/<!--\s*END\s*-->/is',
  79. '/<!--\s*([a-zA-Z0-9_\$\[\]\'\"]{2,60})\s*(AS|as)\s*(.+?)\s*-->/',
  80. '/<!--\s*while\:\s*(.+?)\s*-->/is',
  81. '/(\{\s*|<!--\s*)lang\:(.+?)(\s*\}|\s*-->)/eis',
  82. '/(\{\s*|<!--\s*)row\:(.+?)(\s*\}|\s*-->)/eis',
  83. '/(\{\s*|<!--\s*)color\:\s*([\#0-9A-Za-z]+\,[\#0-9A-Za-z]+)(\s*\}|\s*-->)/eis',
  84. '/(\{\s*|<!--\s*)dir\:([^\{\}]{1,100})(\s*\}|\s*-->)/eis',
  85. '/(\{\s*|<!--\s*)run\:(\}|\s*-->)\s*(.+?)\s*(\{|<!--\s*)\/run(\s*\}|\s*-->)/is',
  86. '/(\{\s*|<!--\s*)run\:(.+?)(\s*\}|\s*-->)/is',
  87. '/\{([a-zA-Z0-9_\'\"\[\]\$]{1,100})\}/',
  88. );
  89. $this->Compile = (is_array($this->Compile))?array_merge($this->Compile,$CompileBasic):$CompileBasic;
  90. $AnalysisBasic = array(
  91. '$this->inc_php("\\2")',
  92. '";if($ET_Del==true){echo"',
  93. '";if(\\2){echo"',
  94. '";}elseif(\\2){echo"',
  95. '";}else{echo"',
  96. '";}echo"',
  97. '";\$_i=0;foreach((array)\\1 AS \\3){\$_i++;echo"',
  98. '";\$_i=0;while(\\1){\$_i++;echo"',
  99. '$this->lang("\\2")',
  100. '$this->Row("\\2")',
  101. '$this->Color("\\2")',
  102. '$this->Dirs("\\2")',
  103. '";\\3;echo"',
  104. '";\\2;echo"',
  105. '";echo \$\\1;echo"',
  106. );
  107. $this->Analysis = (is_array($this->Analysis))?array_merge($this->Analysis,$AnalysisBasic):$AnalysisBasic;
  108. }
  109. /**
  110. * 设置数值
  111. * set_var(变量名或是数组,设置数值[数组不设置此值]);
  112. */
  113. function set_var(
  114. $name,
  115. $value = ''
  116. ){
  117. if (is_array($name)){
  118. $this->ThisValue = @array_merge($this->ThisValue,$name);
  119. }else{
  120. $this->ThisValue[$name] = $value;
  121. }
  122. }
  123. /**
  124. * 设置模板文件
  125. * set_file(文件名,设置目录);
  126. */
  127. function set_file(
  128. $FileName,
  129. $NewDir = ''
  130. ){
  131. //当前模板名
  132. $this->ThisFile = $FileName.'.'.$this->Ext;
  133. //目录地址检测
  134. $this->FileDir[$this->ThisFile] = (trim($NewDir) != '')?$NewDir.'/':$this->TemplateDir;
  135. $this->IncFile[$FileName] = $this->FileDir[$this->ThisFile].$this->ThisFile;
  136. if(!is_file($this->IncFile[$FileName]) && $this->Copyright==1){
  137. die('Sorry, The file <b>'.$this->IncFile[$FileName].'</b> does not exist.');
  138. }
  139. //bug 系统
  140. $this->IncList[] = $this->ThisFile;
  141. }
  142. //解析替换程序
  143. function ParseCode(
  144. $FileList = '',
  145. $CacheFile = ''
  146. ){
  147. //模板数据
  148. $ShowTPL = '';
  149. //解析续载
  150. if (@is_array($FileList) && $FileList!='include_page'){
  151. foreach ($FileList AS $K=>$V) {
  152. $ShowTPL .= $this->reader($V.$K);
  153. }
  154. }else{
  155. //如果指定文件地址则载入
  156. $SourceFile = ($FileList!='')?$FileList:$this->FileDir[$this->ThisFile].$this->ThisFile;
  157. if(!is_file($SourceFile) && $this->Copyright==1){
  158. die('Sorry, The file <b>'.$SourceFile.'</b> does not exist.');
  159. }
  160. $ShowTPL = $this->reader($SourceFile);
  161. }
  162. //引用模板处理
  163. $ShowTPL = $this->inc_preg($ShowTPL);
  164. //检测run方法
  165. $run = 0;
  166. if (eregi("run:",$ShowTPL)){
  167. $run = 1;
  168. //Fix =
  169. $ShowTPL = preg_replace('/(\{|<!--\s*)run:(\}|\s*-->)\s*=/','{run:}echo ',$ShowTPL);
  170. $ShowTPL = preg_replace('/(\{|<!--\s*)run:\s*=/','{run:echo ',$ShowTPL);
  171. //Fix Run 1
  172. $ShowTPL = preg_replace('/(\{|<!--\s*)run:(\}|\s*-->)\s*(.+?)\s*(\{|<!--\s*)\/run(\}|\s*-->)/is', '(T_T)\\3;(T_T!)',$ShowTPL);
  173. }
  174. //Fix XML
  175. if (eregi("<?xml",$ShowTPL)){
  176. $ShowTPL = @preg_replace('/<\?(xml.+?)\?>/is', '<ET>\\1</ET>', $ShowTPL);
  177. }
  178. //修复代码中\n换行错误
  179. $ShowTPL = str_replace('\\','\\\\',$ShowTPL);
  180. //修复双引号问题
  181. $ShowTPL = str_replace('"','\"',$ShowTPL);
  182. //编译运算
  183. $ShowTPL = @preg_replace($this->Compile, $this->Analysis, $ShowTPL);
  184. //分析图片地址
  185. $ShowTPL = $this->ImgCheck($ShowTPL);
  186. //Fix 模板中金钱符号
  187. $ShowTPL = str_replace('$','\$',$ShowTPL);
  188. //修复php运行错误
  189. $ShowTPL = @preg_replace("/\";(.+?)echo\"/e", '$this->FixPHP(\'\\1\')', $ShowTPL);
  190. //Fix Run 2
  191. if ($run==1){
  192. $ShowTPL = preg_replace("/\(T_T\)(.+?)\(T_T!\)/ise", '$this->FixPHP(\'\\1\')', $ShowTPL);
  193. }
  194. //还原xml
  195. $ShowTPL = (strrpos($ShowTPL,'<ET>'))?@preg_replace('/ET>(.+?)<\/ET/is', '?\\1?', $ShowTPL):$ShowTPL;
  196. //修复"问题
  197. $ShowTPL = str_replace('echo ""','echo "\"',$ShowTPL);
  198. //从数组中将变量导入到当前的符号表
  199. @extract($this->Value());
  200. ob_start();
  201. ob_implicit_flush(0);
  202. @eval('echo "'.$ShowTPL.'";');
  203. $contents = ob_get_contents();
  204. ob_end_clean();
  205. //Cache htm
  206. if($this->HtmID){
  207. $this->writer($this->HtmDir.$this->HtmID,$this->Hacker."?>".$contents);
  208. }
  209. //编译模板
  210. if ($this->RunType=='Cache'){
  211. $this->CompilePHP($ShowTPL,$CacheFile);
  212. }
  213. //错误检查
  214. if(strlen($contents)<=0){
  215. //echo $ShowTPL;
  216. die('<br>Sorry, Error or complicated syntax error exists in '.$SourceFile.' file.');
  217. }
  218. return $contents;
  219. }
  220. /**
  221. * 多语言
  222. */
  223. function lang(
  224. $str = ''
  225. ){
  226. if (is_dir($this->LangDir)){
  227. //采用MD5效验
  228. $id = md5($str);
  229. //不存在数据则写入
  230. if($this->LangData[$id]=='' && $this->Language=='default'){
  231. //语言包文件
  232. if (@is_file($this->LangDir.$this->Language.'.php')){
  233. unset($lang);
  234. @include($this->LangDir.$this->Language.'.php');
  235. }
  236. //如果检测到有数据则输出
  237. if ($lang[$id]){
  238. $out = str_replace('\\','\\\\',$lang[$id]);
  239. return str_replace('"','\"',$out);
  240. }
  241. //修复'多\问题
  242. $str = str_replace("\\'","'",$str);
  243. //语言文件过大时采取建立新文件
  244. if(strlen($docs)>400){
  245. $this->writer($this->LangDir.$this->Language.'.'.$id.'.php','<? $etl = "'.$str.'";?>');
  246. $docs= substr($str,0,40); //简要说明
  247. $docs = str_replace('\"','"',$docs);
  248. $docs = str_replace('\\\\','\\',$docs);
  249. $str = 'o(O_O)o.ET Lang.o(*_*)o'; //语言新文件
  250. }else{
  251. $docs = str_replace('\"','"',$str);
  252. $docs = str_replace('\\\\','\\',$docs);
  253. }
  254. //文件安全处理
  255. $data = (!is_file($this->LangDir.'default.php'))?"<?\n/**\n/* SYSTN ET Language For ".$this->Language."\n*/\n\n\n":'';
  256. if (trim($str)){
  257. //写入数据
  258. $data .= "/**".date("Y.m.d",time())."\n";
  259. $data.= $docs."\n";
  260. $data.= "*/\n";
  261. $data.= '$lang["'.$id.'"] = "'.$str.'";'."\n\n";
  262. $this->writer($this->LangDir.'default.php',$data,'a+');
  263. }
  264. }
  265. //单独语言文件包
  266. if($this->LangData[$id]=='o(O_O)o.ET Lang.o(*_*)o'){
  267. unset($etl);
  268. include($this->LangDir.$this->Language.".".$id.".php");
  269. $this->LangData[$id] = $etl;
  270. }
  271. $out = ($this->LangData[$id])?$this->LangData[$id]:$str;
  272. //输出部分要做处理
  273. if(($this->RunType=='Replace' || $this->RunType!='Replace') && $data==''){
  274. $out = str_replace('\\','\\\\',$out);
  275. $out = str_replace('"','\"',$out);
  276. }
  277. return $out;
  278. }else{
  279. return $str;
  280. }
  281. }
  282. /**
  283. * inc引用函数
  284. */
  285. function inc_preg(
  286. $content
  287. ){
  288. return preg_replace('/<\!--\s*\#include\s*file\s*=(\"|\')([a-zA-Z0-9_\.\|]{1,100})(\"|\')\s*-->/eis', '$this->inc("\\2")', preg_replace('/(\{\s*|<!--\s*)inc\:([^\{\} ]{1,100})(\s*\}|\s*-->)/eis', '$this->inc("\\2")', $content));
  289. }
  290. /**
  291. * 引用函数运算
  292. */
  293. function inc(
  294. $Files = ''
  295. ){
  296. if($Files){
  297. if (!strrpos($Files,$this->Ext)){
  298. $Files = $Files.".".$this->Ext;
  299. }
  300. $FileLs = $this->TemplateDir.$Files;
  301. $contents =$this->ParseCode($FileLs,$Files);
  302. if($this->RunType=='Cache'){
  303. //引用模板
  304. $this->IncList[] = $Files;
  305. $cache_file = $this->CacheDir.$this->TplID.$Files.".".$this->Language.".php";
  306. return "<!-- ET_inc_cache[".$Files."] -->
  307. <!-- IF(@is_file('".$cache_file."')) -->{inc_php:".$cache_file."}
  308. <!-- IF(\$EaseTemplate3_Cache) -->{run:@eval('echo \"'.\$EaseTemplate3_Cache.'\";')}<!-- END -->
  309. <!-- END -->";
  310. }elseif($this->RunType=='MemCache'){
  311. //cache date
  312. memcache_set($this->Emc,$Files.'_date', time()) OR die("Failed to save data at the server.");
  313. memcache_set($this->Emc,$Files, $contents) OR die("Failed to save data at the server");
  314. return "<!-- ET_inc_cache[".$Files."] -->".$contents;
  315. }else{
  316. //引用模板
  317. $this->IncList[] = $Files;
  318. return $contents;
  319. }
  320. }
  321. }
  322. /**
  323. * 编译解析处理
  324. */
  325. function CompilePHP(
  326. $content='',
  327. $cachename = ''
  328. ){
  329. if ($content){
  330. //如果没有安全文件则自动创建
  331. if($this->RunType=='Cache' && !is_file($this->CacheDir.'index.htm')){
  332. $Ease_name = 'Ease Template!';
  333. $Ease_base = "<title>$Ease_name</title><a href='http://www.systn.com'>$Ease_name</a>";
  334. $this->writer($this->CacheDir.'index.htm',$Ease_base);
  335. $this->writer($this->CacheDir.'index.html',$Ease_base);
  336. $this->writer($this->CacheDir.'default.htm',$Ease_base);
  337. }
  338. //编译记录
  339. $content = str_replace("\\","\\\\",$content);
  340. $content = str_replace("'","\'",$content);
  341. $content = str_replace('echo"";',"",$content); //替换多余数据
  342. $wfile = ($cachename)?$cachename:$this->ThisFile;
  343. $this->writer($this->FileName($wfile,$this->TplID) ,$this->Hacker.'$EaseTemplate3_Cache = \''.$content.'\';');
  344. }
  345. }
  346. //修复PHP执行时产生的错误
  347. function FixPHP(
  348. $content=''
  349. ){
  350. $content = str_replace('\\\\','\\',$content);
  351. return '";'.str_replace('\\"','"',str_replace('\$','$',$content)).'echo"';
  352. }
  353. /**
  354. * 检测缓存是否要更新
  355. * filename 缓存文件名
  356. * settime 指定事件则提供更新,只用于memcache
  357. */
  358. function FileUpdate($filname,$settime=0){
  359. //检测设置模板文件
  360. if (is_array($this->IncFile)){
  361. unset($k,$v);
  362. $update = 0;
  363. $settime = ($settime>0)?$settime:@filemtime($filname);
  364. foreach ($this->IncFile AS $k=>$v) {
  365. if (@filemtime($v)>$settime){$update = 1;}
  366. }
  367. //更新缓存
  368. if($update==1){
  369. return false;
  370. }else {
  371. return $filname;
  372. }
  373. }else{
  374. return $filname;
  375. }
  376. }
  377. /**
  378. * 输出运算
  379. * Filename 连载编译输出文件名
  380. */
  381. function output(
  382. $Filename = ''
  383. ){
  384. switch($this->RunType){
  385. //Mem编译模式
  386. case'MemCache':
  387. if ($Filename=='include_page'){
  388. //直接输出文件
  389. return $this->reader($this->FileDir[$this->ThisFile].$this->ThisFile);
  390. }else{
  391. $FileNames = ($Filename)?$Filename:$this->ThisFile;
  392. $CacheFile = $this->FileName($FileNames,$this->TplID);
  393. //检测记录时间
  394. $updateT = memcache_get($this->Emc,$CacheFile.'_date');
  395. $update = $this->FileUpdate($CacheFile,$updateT);
  396. $CacheData = memcache_get($this->Emc,$CacheFile);
  397. if(trim($CacheData) && $update){
  398. //获得列表文件
  399. unset($ks,$vs);
  400. preg_match_all('/<\!-- ET\_inc\_cache\[(.+?)\] -->/',$CacheData, $IncFile);
  401. if (is_array($IncFile[1])){
  402. foreach ($IncFile[1] AS $ks=>$vs) {
  403. $this->IncList[] = $vs;
  404. $listDate = memcache_get($this->Emc,$vs.'_date');
  405. echo @filemtime($this->TemplateDir.$vs).' - '.$listDate.'<br>';
  406. //更新inc缓存
  407. if (@filemtime($this->TemplateDir.$vs)>$listDate){
  408. $update = 1;
  409. $this->inc($vs);
  410. }
  411. }
  412. //更新数据
  413. if ($update == 1){
  414. $CacheData = $this->ParseCode($this->FileList,$Filename);
  415. //cache date
  416. @memcache_set($this->Emc,$CacheFile.'_date', time()) OR die("Failed to save data at the server.");
  417. @memcache_set($this->Emc,$CacheFile, $CacheData) OR die("Failed to save data at the server.");
  418. }
  419. }
  420. //Close
  421. memcache_close($this->Emc);
  422. return $CacheData;
  423. }else{
  424. if ($Filename){
  425. $CacheData = $this->ParseCode($this->FileList,$Filename);
  426. //cache date
  427. @memcache_set($this->Emc,$CacheFile.'_date', time()) OR die("Failed to save data at the server.");
  428. @memcache_set($this->Emc,$CacheFile, $CacheData) OR die("Failed to save data at the server.");
  429. //Close
  430. memcache_close($this->Emc);
  431. return $CacheData;
  432. }else{
  433. $CacheData = $this->ParseCode();
  434. //cache date
  435. @memcache_set($this->Emc,$CacheFile.'_date', time()) OR die("Failed to save data at the server.");
  436. @memcache_set($this->Emc,$CacheFile, $CacheData) OR die("Failed to save data at the server2");
  437. //Close
  438. memcache_close($this->Emc);
  439. return $CacheData;
  440. }
  441. }
  442. }
  443. break;
  444. //编译模式
  445. case'Cache':
  446. if ($Filename=='include_page'){
  447. //直接输出文件
  448. return $this->reader($this->FileDir[$this->ThisFile].$this->ThisFile);
  449. }else{
  450. $FileNames = ($Filename)?$Filename:$this->ThisFile;
  451. $CacheFile = $this->FileName($FileNames,$this->TplID);
  452. $CacheFile = $this->FileUpdate($CacheFile);
  453. if (@is_file($CacheFile)){
  454. @extract($this->Value());
  455. ob_start();
  456. ob_implicit_flush(0);
  457. include $CacheFile;
  458. //获得列表文件
  459. if($EaseTemplate3_Cache!=''){
  460. unset($ks,$vs);
  461. preg_match_all('/<\!-- ET\_inc\_cache\[(.+?)\] -->/',$EaseTemplate3_Cache, $IncFile);
  462. if (is_array($IncFile[1])){
  463. foreach ($IncFile[1] AS $ks=>$vs) {
  464. $this->IncList[] = $vs;
  465. //更新inc缓存
  466. if (@filemtime($this->TemplateDir.$vs)>@filemtime($this->CacheDir.$this->TplID.$vs.'.'.$this->Language.'.php')){
  467. $this->inc($vs);
  468. }
  469. }
  470. }
  471. @eval('echo "'.$EaseTemplate3_Cache.'";');
  472. $contents = ob_get_contents();
  473. ob_end_clean();
  474. return $contents;
  475. }
  476. }else{
  477. if ($Filename){
  478. return $this->ParseCode($this->FileList,$Filename);
  479. }else{
  480. return $this->ParseCode();
  481. }
  482. }
  483. }
  484. break;
  485. //替换引擎
  486. default:
  487. if($Filename){
  488. if ($Filename=='include_page'){
  489. //直接输出文件
  490. return $this->reader($this->FileDir[$this->ThisFile].$this->ThisFile);
  491. }else {
  492. return $this->ParseCode($this->FileList);
  493. }
  494. }else{
  495. return $this->ParseCode();
  496. }
  497. }
  498. }
  499. /**
  500. * 连载函数
  501. */
  502. function n(){
  503. //连载模板
  504. $this->FileList[$this->ThisFile] = $this->FileDir[$this->ThisFile];
  505. }
  506. /**
  507. * 输出模板内容
  508. * Filename 连载编译输出文件名
  509. */
  510. function r(
  511. $Filename = ''
  512. ){
  513. return $this->output($Filename);
  514. }
  515. /**
  516. * 打印模板内容
  517. * Filename 连载编译输出文件名
  518. */
  519. function p(
  520. $Filename = ''
  521. ){
  522. echo $this->output($Filename);
  523. }
  524. /**
  525. * 分析图片地址
  526. */
  527. function ImgCheck(
  528. $content
  529. ){
  530. //Check Image Dir
  531. if($this->AutoImage==1){
  532. $NewFileDir = $this->FileDir[$this->ThisFile];
  533. //FIX img
  534. if(is_array($this->ImgDir)){
  535. foreach($this->ImgDir AS $rep){
  536. $rep = trim($rep);
  537. //检测是否执行替换
  538. if(strrpos($content,$rep."/")){
  539. if(substr($rep,-1)=='/'){
  540. $rep = substr($rep,0,strlen($rep)-1);
  541. }
  542. $content = str_replace($rep.'/',$NewFileDir.$rep.'/',$content);
  543. }
  544. }
  545. }
  546. //FIX Dir
  547. $NewFileDirs = $NewFileDir.$NewFileDir;
  548. if(strrpos($content,$NewFileDirs)){
  549. $content = str_replace($NewFileDirs,$NewFileDir,$content);
  550. }
  551. }
  552. return $content;
  553. }
  554. /**
  555. * 获得所有设置与公共变量
  556. */
  557. function Value(){
  558. return (is_array($this->ThisValue))?array_merge($this->ThisValue,$GLOBALS):$GLOBALS;
  559. }
  560. /**
  561. * 清除设置
  562. */
  563. function clear(){
  564. $this->RunType = 'Replace';
  565. }
  566. /**
  567. * 静态文件写入
  568. */
  569. function htm_w(
  570. $w_dir = '',
  571. $w_filename = '',
  572. $w_content = ''
  573. ){
  574. $dvs = '';
  575. if($w_dir && $w_filename && $w_content){
  576. //目录检测数量
  577. $w_dir_ex = explode('/',$w_dir);
  578. $w_new_dir = ''; //处理后的写入目录
  579. unset($dvs,$fdk,$fdv,$w_dir_len);
  580. foreach((array)$w_dir_ex AS $dvs){
  581. if(trim($dvs) && $dvs!='..'){
  582. $w_dir_len .= '../';
  583. $w_new_dir .= $dvs.'/';
  584. if (!@is_dir($w_new_dir)) @mkdir($w_new_dir, 0777);
  585. }
  586. }
  587. //获得需要更改的目录数
  588. foreach((array)$this->FileDir AS $fdk=>$fdv){
  589. $w_content = str_replace($fdv,$w_dir_len.str_replace('../','',$fdv),$w_content);
  590. }
  591. $this->writer($w_dir.$w_filename,$w_content);
  592. }
  593. }
  594. /**
  595. * 改变静态刷新时间
  596. */
  597. function htm_time($times=0){
  598. if((int)$times>0){
  599. $this->HtmTime = (int)$times;
  600. }
  601. }
  602. /**
  603. * 静态文件存放的绝对目录
  604. */
  605. function htm_dir($Name = ''){
  606. if(trim($Name)){
  607. $this->HtmDir = trim($Name).'/';
  608. }
  609. }
  610. /**
  611. * 产生静态文件输出
  612. */
  613. function HtmCheck(
  614. $Name = ''
  615. ){
  616. $this->HtmID = md5(trim($Name)? trim($Name).'.php' : $this->Server['REQUEST_URI'].'.php' );
  617. //检测时间
  618. if(is_file($this->HtmDir.$this->HtmID) && (time() - @filemtime($this->HtmDir.$this->HtmID)<=$this->HtmTime)){
  619. ob_start();
  620. ob_implicit_flush(0);
  621. include $this->HtmDir.$this->HtmID;
  622. $HtmContent = ob_get_contents();
  623. ob_end_clean();
  624. return $HtmContent;
  625. }
  626. }
  627. /**
  628. * 打印静态内容
  629. */
  630. function htm_p(
  631. $Name = ''
  632. ){
  633. $output = $this->HtmCheck($Name);
  634. if ($output){
  635. die($this->HtmCheck($Name));
  636. }
  637. }
  638. /**
  639. * 输出静态内容
  640. */
  641. function htm_r(
  642. $Name = ''
  643. ){
  644. return $this->HtmCheck($Name);
  645. }
  646. /**
  647. * 解析文件
  648. */
  649. function FileName(
  650. $name,
  651. $id = '1'
  652. ){
  653. $extdir = explode("/",$name);
  654. $dircnt = @count($extdir) - 1;
  655. $extdir[$dircnt] = $id.$extdir[$dircnt];
  656. return $this->CacheDir.implode("_",$extdir).".".$this->Language.'.php';
  657. }
  658. /**
  659. * 检测引入文件
  660. */
  661. function inc_php(
  662. $url = ''
  663. ){
  664. $parse = parse_url($url);
  665. unset($vals,$code_array);
  666. foreach((array)explode('&',$parse['query']) AS $vals){
  667. $code_array .= preg_replace('/(.+)=(.+)/',"\$_GET['\\1']= \$\\1 ='\\2';",$vals);
  668. }
  669. return '";'.$code_array.' @include(\''.$parse['path'].'\');echo"';
  670. }
  671. /**
  672. * 换行函数
  673. * Row(换行数,换行颜色);
  674. * Row("5,#ffffff:#e1e1e1");
  675. */
  676. function Row(
  677. $Num = ''
  678. ){
  679. $Num = trim($Num);
  680. if($Num != ''){
  681. $Nums = explode(",",$Num);
  682. $Numr = ((int)$Nums[0]>0)?(int)$Nums[0]:2;
  683. $input = (trim($Nums[1]) == '')?'</tr><tr>':$Nums[1];
  684. if(trim($Nums[1]) != ''){
  685. $Co = explode(":",$Nums[1]);
  686. $OutStr = "if(\$_i%$Numr===0){\$row_count++;echo(\$row_count%2===0)?'</tr><tr bgcolor=\"$Co[0]\">':'</tr><tr bgcolor=\"$Co[1]\">';}";
  687. }else{
  688. $OutStr = "if(\$_i%$Numr===0){echo '$input';}";
  689. }
  690. return '";'.$OutStr.'echo "';
  691. }
  692. }
  693. /**
  694. * 间隔变色
  695. * Color(两组颜色代码);
  696. * Color('#FFFFFF,#DCDCDC');
  697. */
  698. function Color(
  699. $color = ''
  700. ){
  701. if($color != ''){
  702. $OutStr = preg_replace("/(.+),(.+)/","_i%2===0)?'\\1':'\\2';",$color);
  703. if(strrpos($OutStr,"%2")){
  704. return '";echo(\$'.$OutStr.'echo "';
  705. }
  706. }
  707. }
  708. /**
  709. * 映射图片地址
  710. */
  711. function Dirs(
  712. $adds = ''
  713. ){
  714. $adds_ary = explode(",",$adds);
  715. if(is_array($adds_ary)){
  716. $this->ImgDir = (is_array($this->ImgDir))?@array_merge($adds_ary, $this->ImgDir):$adds_ary;
  717. }
  718. }
  719. /**
  720. * 读取函数
  721. * reader(文件名);
  722. */
  723. function reader(
  724. $filename
  725. ){
  726. $get_fun = @get_defined_functions();
  727. return (in_array('file_get_contents',$get_fun['internal']))?@file_get_contents($filename):@implode("", @file($filename));
  728. }
  729. /**
  730. * 写入函数
  731. * writer(文件名,写入数据, 写入数据方式);
  732. */
  733. function writer(
  734. $filename,
  735. $data = '',
  736. $mode='w'
  737. ){
  738. if(trim($filename)){
  739. $file = @fopen($filename, $mode);
  740. $filedata = @fwrite($file, $data);
  741. @fclose($file);
  742. }
  743. if(!is_file($filename)){
  744. die('Sorry,'.$filename.' file write in failed!');
  745. }
  746. }
  747. /**
  748. * 引入模板系统
  749. * 察看当前使用的模板以及调试信息
  750. */
  751. function inc_list(){
  752. if(is_array($this->IncList)){
  753. $EXTS = explode("/",$this->Server['REQUEST_URI']);
  754. $Last = count($EXTS) -1;
  755. //处理清除工作 START
  756. if(strrpos($EXTS[$Last],'Ease_Templatepage=Clear') && trim($EXTS[$Last]) != ''){
  757. $dir_name = $this->CacheDir;
  758. if(file_exists($dir_name)){
  759. $handle=@opendir($dir_name);
  760. while($tmp_file=@readdir($handle)){
  761. if(@file_exists($dir_name.$tmp_file)){
  762. @unlink($dir_name.$tmp_file);
  763. }
  764. }
  765. @closedir($handle);
  766. }
  767. $GoURL = urldecode(preg_replace("/.+?REFERER=(.+?)!!!/","\\1",$EXTS[$Last]));
  768. die('<script language="javascript" type="text/javascript">location="'.urldecode($GoURL).'";</script>');
  769. }
  770. //处理清除工作 END
  771. $list_file = array();
  772. $file_nums = count($this->IncList);
  773. $AllSize = 0;
  774. foreach($this->IncList AS $Ks=>$Vs){
  775. $FSize[$Ks] = @filesize($this->TemplateDir.$Vs);
  776. $AllSize += $FSize[$Ks];
  777. }
  778. foreach($this->IncList AS $K=>$V){
  779. $File_Size = @round($FSize[$K] / 1024 * 100) / 100 . 'KB';
  780. $Fwidth = @floor(100*$FSize[$K]/$AllSize);
  781. $list_file[] = "<tr><td colspan=\"2\" bgcolor=\"#F7F7F7\" title='".$Fwidth."%'><a href='".$this->TemplateDir.$V."' target='_blank'><font color='#6F7D84' style='font-size:14px;'>".$this->TemplateDir.$V."</font></a>
  782. <font color='#B4B4B4' style='font-size:10px;'>".$File_Size."</font>
  783. <table border='1' width='".$Fwidth."%' style='border-collapse: collapse' bordercolor='#89A3ED' bgcolor='#4886B3'><tr><td></td></tr></table></td></tr>";
  784. }
  785. //连接地址
  786. $BackURL = preg_replace("/.+\//","\\1",$this->Server['REQUEST_URI']);
  787. $NowPAGE = 'http://'.$this->Server['HTTP_HOST'].$this->Server['SCRIPT_NAME'];
  788. $clear_link = $NowPAGE."?Ease_Templatepage=Clear&REFERER=".urlencode($BackURL)."!!!";
  789. $sf13 = ' style="font-size:13px;color:#666666"';
  790. echo '<br><table border="1" width="100%" cellpadding="3" style="border-collapse: collapse" bordercolor="#DCDCDC">
  791. <tr bgcolor="#B5BDC1"><td><font color=#000000 style="font-size:16px;"><b>Include Templates (Num:'.count($this-> IncList).')</b></font></td>
  792. <td align="right">';
  793. if($this->RunType=='Cache'){
  794. echo '[<a onclick="alert(\'Cache file is successfully deleted\');location=\''.$clear_link.'\';return false;" href="'.$clear_link.'"><font'.$sf13.'>Clear Cache</font></a>]';
  795. }
  796. echo '</td></tr><tr><td colspan="2" bgcolor="#F7F7F7"><table border="0" width="100%" cellpadding="0" style="border-collapse: collapse">
  797. <tr><td'.$sf13.'>Cache File ID: <b>'.substr($this->TplID,0,-1).'</b></td>
  798. <td'.$sf13.'>Index: <b>'.((count($this->FileList)==0)?'False':'True').'</b></td>
  799. <td'.$sf13.'>Format: <b>'.$this->Ext.'</b></td>
  800. <td'.$sf13.'>Cache: <b>'.($this->RunType=='MemCache'?'Memcache Engine':($this->RunType == 'Replace'?'Replace Engine':$this->CacheDir)).'</b></td>
  801. <td'.$sf13.'>Template: <b>'.$this->TemplateDir.'</b></td></tr>
  802. </table></td></tr>'.implode("",$list_file)."</table>";
  803. }
  804. }
  805. }
  806. ?>