-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathease_template.php
More file actions
1607 lines (1466 loc) · 53.3 KB
/
ease_template.php
File metadata and controls
1607 lines (1466 loc) · 53.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* (C)2005-2021 FoundPHP Framework.
* name: Ease Template
* weburl: http://www.FoundPHP.com
* mail: master@FoundPHP.com
* author: 孟大川
* version: 1.21.430
* start: 2013-02-19
* update: 2021-04-30
* support: >=PHP5.4,PHP7,PHP8
* payment: Free 免费
* This is not a freeware, use is subject to license terms.
* 此软件为授权使用软件,请参考软件协议。
* http://www.foundphp.com/?m=agreement
Relation:
ease_template.php, ease_template_mime.php, language/et_en.php
*/
define("ET3!",TRUE);
error_reporting(E_ALL & ~E_NOTICE);
$GLOBALS['_ET'] = '';
class FoundPHP_template{
public $ThisFile = ''; //当前文件
public $IncFile = array(); //引入文件
public $ThisValue = array(); //当前数值
public $FileList = array(); //载入文件列表
public $IncList = array(); //引入文件列表
public $ImgDir = array('images','assets'); //图片地址目录
public $HtmDir = 'cache_htm/'; //静态存放的目录
public $HtmID = ''; //静态文件ID
public $HtmTime = '180'; //秒为单位,默认三分钟
public $AutoImage = 1; //自动解析图片目录开关默认值
public $Rewrite = 1; //地址自动静态化
public $Hacker = "<?php defined('ET3!') OR die('You are Hacker!<br>Power by Ease Template!');";
public $Compile = array();
public $Analysis = array();
public $Emc = array();
public $Ext = 'htm';
public $Language = 'zh'; //默认中文
public $Language_name= 'def';
public $curl_header = array();
public $curl_cookie = array();
public $curl_file = array();
public $curl_pem = array(); //证书文件
public $curl_age = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 FoundPHP Browser/2.1';
public $curl_request = 'GET';
public $get_dir_ext = array(); //格式:array('php','htm');
public $get_dir_num = 0; //目录深度:0不限制,2表示目录2级深度
public $file_base = 0;
private $lang = array(
'not_exist1' => '抱歉,',
'not_exist2' => '文件名错误或是文件不存在。',
'not_exist3' => '<br>抱歉,模板语法错误不能执行。',
'unconnect' => '抱歉,MemCache 不能连接!',
'copyright' => '版权模式只有Cache模式下可以开启',
'version' => '抱歉,请使用php 5.4或以上版本运行ET引擎。',
'mc_save' => '无法存储到memchache服务器。',
'not_write' => ' 写入失败!',
'clear_cache' => '清除缓存',
'inc_tpl' => '模板调试平台',
'open_file' => '打开模板文件: ',
'open_file_num' => '个',
'inc_file' => '载入PHP文件数: ',
'inc_file_num' => '个',
'cache_id' => '缓存 ID:',
'index' => '索引模式:',
'format' => '模板格式:',
'cache' => '缓存目录:',
'template' => '模板目录:',
'memory' => '占用内存:',
'run_time' => '运行时间:',
'second' => '秒',
'support' => '技术支持:',
'cache_del' => '缓存文件删除成功。\n页面即将刷新。',
'routing' => '采用路由模式后,您的配置文件必须设置WebURL网站真实地址。<br>例如: $tpl = new template( array( "WebURL" => "http://www.FoundPHP.com/" ) );',
'memcache' => '请修改 php.ini 中增加 memcache 模块',
'error' => 'Ease Template 错误: ',
'error_file' => '错误文件: ',
'error_line' => '错误行号: ',
'error_info' => '错误信息: ',
'error_code' => '错误代码: ',
'curl_link' => '抱歉,请输入正确URL地址',
'modify_time' => '修改: ',
'size' => ' 大小: ',
'curl_init' => '您的服务器不支持curl',
'curl_text' => 'curl上传文件时,不支持字符数据',
'curl_file' => 'curl上传文件时,post 数据只能用一维数组',
'curl_pem' => 'curl 证书文件路径错误或文件不存在',
);
/*
* 声明模板用法
*/
function __construct(
$set = array(
'ID' =>1, //缓存ID
'TplType' =>'htm', //模板格式
'CacheDir' =>'cache', //缓存目录
'TemplateDir'=>'template', //模板存放目录
'AutoImage' =>1, //自动解析图片目录开关 on表示开放 off表示关闭
'LangDir' =>'language', //语言文件存放的目录
'Language' =>'default', //语言的默认文件
'Copyright' =>0, //版权保护
'MemCache' =>'', //Memcache服务器地址例如:127.0.0.1:11211
'Compress' =>0, //压缩代码
'WebURL' =>'', //如果采用路由模式请设定真实网站地址
'Rewrite' =>0, //是否开启地址重写的静态方式
'StartTime' =>1, //启用执行时间计算
)
){
$this->TplID = (defined('TemplateID')?TemplateID:( (@(int)$set['ID']<=1)?1:(int)$set['ID']) ).'_';
$this->CacheDir = (defined('NewCache')?NewCache:( (trim($set['CacheDir']) != '')?$set['CacheDir']:'cache') ).'/';
$this->TemplateDir = (defined('NewTemplate')?NewTemplate:( (trim($set['TemplateDir']) != '')?$set['TemplateDir']:'template') ).'/';
$this->Ext = (@$set['TplType']!='')?$set['TplType']:'htm';
$this->AutoImage = (@$set['AutoImage']==1)?1:0;
$this->Compress = (@$set['Compress']==1)?1:0;
$this->Rewrite = (@$set['Rewrite']==1)?1:0;
$this->Language = (@$set['Language']!='')?$set['Language']:'zh';
$this->version = (@$_GET['m']=='EaseTemplateVer')?die('Ease Templae!'):'';
if (@$set['Copyright']==1){
$this->Copyright= 1;
if (!is_writable($set['CacheDir'])){
$error = $this->lang['copyright'];
function_exists('foundphp_error')?foundphp_error($error):die($error);
}
}else{
$this->Copyright= 0;
}
//载入语言包
if (@$set['Language']!=''){
$etlang = dirname(__FILE__).'/language/et_'.$set['Language'].'.php';
if (is_file($etlang)){
include_once($etlang);
$this->lang = $GLOBALS['FoundPHP_ET_Lang'];
}
}
if(@$set['WebURL']!= ''){
$this->WebURL = $set['WebURL'];
if(substr($this->WebURL,-1)!='/'){
$this->WebURL .= '/';
}
}
if(@$_SERVER["PATH_INFO"]!='' && trim($_SERVER["PATH_INFO"])!='' && $this->WebURL==''){
$path_info = explode('/',$_SERVER["PATH_INFO"]);
if(count($path_info)>0){
$error = $this->lang['routing'];
function_exists('foundphp_error')?foundphp_error($error):die($error);
}
}
$this->LangDir = 'language/';
//来自FoundPHP框架自动处理
if (@constant('FoundPHP.com')==true){
$this->Language = $set['Language'];
$this->Language_name = (empty($GLOBALS['m'])?'':'_'.$GLOBALS['m']).(!empty($GLOBALS['a'])?'_'.$GLOBALS['a']:'');
}else{
if ($set['Language']!='' && $set['Language']!='zh'){
$this->Language = $set['Language'];
}
}
if ($set['LangDir']!='' && $set['LangDir']!='language'){
$this->LangDir = $set['LangDir'].'/';
}
if(is_dir($this->LangDir)){
if(@is_file($this->LangDir.$this->Language.'.php')){
$lang = array();
include_once $this->LangDir.$this->Language.'.php';
$this->LangData = $fp_lang;
}
}
//缓存目录检测以及运行模式
if(strchr(@$set['MemCache'],':')){
$this->RunType = 'MemCache';
if(!function_exists('memcache_connect')){
$error = $this->lang['memcache'];
function_exists('foundphp_error')?foundphp_error($error):die($error);
}
$memset = explode(":",$set['MemCache']);
$this->Emc = @memcache_connect($memset['0'], $memset['1']) OR (function_exists('foundphp_error')?foundphp_error($error):die($error));
}else{
$this->RunType = (@substr(@sprintf('%o', @fileperms($this->CacheDir)), -3)==777 && is_dir($this->CacheDir))?'Cache':'Replace';
}
//得到当前执行时间
$this->start_time = $this->get_time();
}
/*
* 设置数值
* set_var(变量名或是数组,设置数值[数组不设置此值]);
*/
function set_var(
$name,
$value = ''
){
if (is_array($name)){
$this->ThisValue = @array_merge($this->ThisValue,$name);
}else{
$this->ThisValue[$name] = $value;
}
}
/*
* 设置模板文件
* set_file(文件名,设置目录);
*/
function set_file(
$FileName,
$NewDir = '',
$Path=0
){
//当前模板名
$this->ThisFile = $FileName.'.'.$this->Ext;
//目录地址检测
if(trim($NewDir) != ''){
//指定非设定的模板目录
if($Path==1){
$this->FileDir[$this->ThisFile] = $NewDir.'/';
}else{
$this->FileDir[$this->ThisFile] = strstr($NewDir.'/',$this->TemplateDir)?$NewDir.'/':$this->TemplateDir.$NewDir.'/';
}
}else {
$this->FileDir[$this->ThisFile] = $this->TemplateDir;
}
$this->IncFile[$FileName] = $this->FileDir[$this->ThisFile].$this->ThisFile;
if(!is_file($this->IncFile[$FileName]) && $this->Copyright==0){
$error = $this->lang['not_exist1'].$this->IncFile[$FileName].$this->lang['not_exist2'];
function_exists('foundphp_error')?foundphp_error($error):die($error);
}
//bug 系统
$this->IncList[] = $this->ThisFile;
}
//bug 系统
$this->IncList[] = $this->ThisFile;
}
//修复php7.2缺少引号错误
function fix_php7($str=''){
$str = str_replace(array("['",'["',"']",'"]'),array("[",'[',"]",']'),$str);
$strs = explode('[',$str);
$i = 0;
$result = '';
foreach($strs AS $k=>$v){
$first = substr($v,0,1);
if ($first!='$'){
$result .= ($i==0?'':"['").str_replace(array("\']",']',"']']"),array("]","']","']]"),$v);
}else{
$result .= ($i==0?'':"[").$v;
}
$i++;
$result = str_replace(array("[''","'']"),array("['","']"),$result);
}
return $result;
}
//解析替换程序
function ParseCode(
$FileList = '',
$CacheFile = ''
){
//模板数据
$ShowTPL = '';
//解析续载
if (@is_array($FileList) && $FileList!='include_page'){
foreach ($FileList AS $K=>$V){
$read_file= FoundPHP_template::reader($V.$K);
$ShowTPL .= FoundPHP_template::ImgCheck($read_file, $V.$K);
}
}else{
//如果指定文件地址则载入
$SourceFile = ($FileList!='')?$FileList:$this->FileDir[$this->ThisFile].$this->ThisFile;
if(!is_file($SourceFile) && $this->Copyright==0){
$error = $this->lang['not_exist1'].$SourceFile.$this->lang['not_exist2'];
function_exists('foundphp_error')?foundphp_error($error):die($error);
}
$ShowTPL = FoundPHP_template::ImgCheck($this->reader($SourceFile) ,$SourceFile);
}
//inc引用模板函数
if(strchr($ShowTPL,'inc:') || strchr($ShowTPL,'#include')){
$ShowTPL = preg_replace_callback(
array('/(\{\s*|<!--\s*)inc\s*:\s*([^\{\} ]{1,100})(\s*\}|\s*-->)/is','/<\!--\s*\#include\s*file\s*=\s*(\"|\')\s*([a-zA-Z0-9_\.\|\/]{1,100})\s*(\"|\')\s*-->/is'),function ($m=''){
$Files = $m['2'];
if($Files){
if (!strrpos($Files,$this->Ext)){
$Files = $Files.".".$this->Ext;
}
$filels = $this->TemplateDir.$Files;
//新风格模板不存在则采用默认模板
if(!is_file($filels)){
$this->TemplateDir = 'plugin/view/default/'.($GLOBALS['m']?$GLOBALS['m'].'/':'');
$filels = $this->TemplateDir.$Files;
}
$contents = $this->ParseCode($filels, $Files);
if($this->RunType=='Cache' || $this->RunType=='MemCache'){
$this->FileDir[$Files] = $this->TemplateDir;
//引用模板
$this->IncList[] = $Files;
$cache_file = FoundPHP_template::FileName($Files);
$this->IncHtm[$filels] = $cache_file;
return '[DNA_F];$this->FileDir["'.$Files.'"] = "'.$this->TemplateDir.'"; if(is_file("'.$cache_file.'")){include("'.$cache_file.'");} $_ET.= [DNA_F]';
}else{
//引用模板
$this->FileDir[$Files] = $this->TemplateDir;
$this->IncList[] = $Files;
return $contents;
}
}
},$ShowTPL);
}
//修复代码中\\错误,双引号问题
$ShowTPL = str_replace( array('\\',"'"), array('\\\\',"\'"), $ShowTPL);
//多语言
$ShowTPL = preg_replace( array('/<lang><\/lang>/is','/<lang>\s*<\/lang>/is'), '',$ShowTPL );
$ShowTPL = preg_replace_callback('/<lang>(.+?)<\/lang>/is', function ($m=''){if(trim($m['1'])){return $this->lang(trim($m['1']));}},$ShowTPL);
//保护 XML
$ShowTPL = preg_replace_callback(
array('/(<\?xml.+?\?>)/is'),function ($m=''){return '<ET_XJ>'.base64_encode($m['1']).'</ET_XJ>';},$ShowTPL);
//编译运算
//引用php
$ShowTPL = preg_replace_callback('/(\{\s*|<!--\s*)inc_php:([a-zA-Z0-9_\[\]\.\,\/\?\=\$\#\&\:\;\->\|\^]{2,100})(\s*\}|\s*-->)/is',function ($m=''){
$parse = parse_url($m['2']);
unset($vals,$code_array);
foreach((array)explode('&',$parse['query']) AS $vals){
$code_array .= preg_replace_callback('/(.+)=(.+)/',function($m=''){return ";\$_GET['".$m['1']."']=\$".$m['1']."=\"".$m['2']."\"";},$vals);
}
return '\''.$code_array.';ob_start();ob_implicit_flush(0);@include\''.$parse['path'].'\'; $c=ob_get_contents();ob_end_clean();$_ET.=$c;$_ET.=\'';
},$ShowTPL);
//内部执行
$ShowTPL = preg_replace_callback(array('/(\{\s*)(run\:)(.+?)(\s*\})/is'),function ($m=''){
$m['3'] = preg_replace_callback(array('/(\$[a-zA-Z0-9\_]+\[[\$\-\>a-zA-Z0-9\_\'"\[\]]+\]+)/'),function ($m=''){
return FoundPHP_template::fix_php7($m['1']);
},$m['3']);
$m['3'] = preg_replace_callback(array('/(\$[a-zA-Z0-9\_]+\[[\$\-\>a-zA-Z0-9\_\'"\[\]]+\]+\[[\$\-\>a-zA-Z0-9\_\'"\[\]]+\])/'),function ($m=''){
return FoundPHP_template::fix_php7($m['1']);
},$m['3']);
$m['3'] = str_replace("\'","'",$m['3']);
return "(T_T)".$m['3'].";(T_T!)";},$ShowTPL);
//表格排序
$ShowTPL = preg_replace_callback(array('/(\{\s*|<!--\s*)order\:(\}|\s*-->)\s*(.+?)\s*(\{|<!--\s*)\/run(\s*\}|\s*-->)/is','/(\{\s*|<!--\s*)(order\:)(.+?)(\s*\}|\s*-->)/is'),function ($m=''){if (trim($m['3'])){return "<a href=\"';\$_ET.=\$GLOBALS['page_url'];\$_ET.='&o=".$m['3']."&g=';\$_ET.=(\$GLOBALS['o']=='".$m['3']."' && \$GLOBALS['g']=='a'?'d':'a');\$_ET.='\"><span class=\"fa fa-caret-';\$_ET.=(\$GLOBALS['o']=='".$m['3']."' && \$GLOBALS['g']=='a'?'up':'down');\$_ET.='\"></span></a>";}},$ShowTPL);
//判断
$ShowTPL = preg_replace_callback('/<!--\s*DEL\s*-->/is',function ($m=''){return "';if(@\$ET_Del!=''){\$_ET.='";},$ShowTPL);
$ShowTPL = preg_replace_callback('/<!--\s*IF\s*[\(|\[](.+?)[\)|\]]\s*-->/is',function ($m=''){return "';if(".FoundPHP_template::FixOther($m['1'])."){\$_ET.='";},$ShowTPL);
$ShowTPL = preg_replace_callback('/<!--\s*ELSEIF\s*[\(|\[](.+?)[\)|\]]\s*-->/is',function ($m=''){return "';}elseif(".FoundPHP_template::FixOther($m['1'])."){\$_ET.='";},$ShowTPL);
$ShowTPL = preg_replace_callback('/<!--\s*ELSE\s*-->/is',function ($m=''){return "';}else{\$_ET.='";},$ShowTPL);
$ShowTPL = preg_replace_callback('/<!--\s*END\s*-->/is',function ($m=''){return "';}\$_ET.='";},$ShowTPL);
//循环
$ShowTPL = preg_replace_callback('/<!--\s*([a-zA-Z0-9_\[\]\$\'\\\"]{1,100}|[a-zA-Z0-9_\[\]]{1,100}\->[a-zA-Z0-9_[\]\$\'\\\"\->\(\)\;]{1,100})\s*( [ASas]{2} )\s*(.+?)\s*-->/',function ($m=''){return "';\$_i=0;foreach((array)".FoundPHP_template::FixOther($m['1'])." AS ".$m['3']."){\$_i++;\$_ET.='";},$ShowTPL);
$ShowTPL = preg_replace_callback('/<!--\s*while\:\s*(.+?)\s*-->/is',function ($m=''){return "';\$_i=0;while(".FoundPHP_template::FixOther($m['1'])."){\$_i++;\$_ET.='";},$ShowTPL);
$ShowTPL = preg_replace_callback('/<!--\s*([a-zA-Z0-9_\[\]\'\$\\\"=]{4,30})\s*;\s*(.+)\s*;\s*([a-zA-Z0-9_\[\]\$\'\\\"\-\+]{4})\s*-->/',function ($m=''){return "';for(".$m['1'].";".FoundPHP_template::FixOther($m['2']).";".$m['3']."){\$_ET.='";},$ShowTPL);
//变量
$ShowTPL = preg_replace_callback('/\{([a-zA-Z0-9_\[\]\$\'\\\"]{1,40}|[a-zA-Z0-9_\[\]]{1,40}\->[a-zA-Z0-9_[\]\$\'\\\"\->\(\)\;]{1,40})\}/',function ($m=''){
$first = substr($m['1'],0,1);
if(preg_match("/^[a-zA-Z\s]+$/",$first ) || $first=='_'){
$m['1'] = FoundPHP_template::fix_php7(str_replace("\'","'",$m['1']));
return "';\$_ET.=\$".$m['1'].";\$_ET.='";
}else{
return '{'.$m['1'].'}';
}
},$ShowTPL);
//换行
$ShowTPL = preg_replace_callback('/(\{\s*|<!--\s*)row\:(.+?)(\s*\}|\s*-->)/is',function ($m=''){return FoundPHP_template::Row($m['2']);},$ShowTPL);
//颜色
$ShowTPL = preg_replace_callback('/(\{\s*|<!--\s*)color\:\s*([\#0-9A-Za-z]+\,[\#0-9A-Za-z]+)(\s*\}|\s*-->)/is',function ($m=''){return FoundPHP_template::Color($m['2']);},$ShowTPL);
//还原js错误
$ShowTPL = preg_replace_callback( '/(<script.*<\/script>)/is',function ($m=''){return FoundPHP_template::FixJS($m['1']);},$ShowTPL);
//修复php运行错误
$ShowTPL = preg_replace_callback(array("/\(T_T\)(.+?)\(T_T!\)/is"),function ($m=''){return FoundPHP_template::FixPHP($m['1']);},$ShowTPL);
$ShowTPL = preg_replace_callback(array("/';\s*([if|echo |$].+?)\s*echo\s*'/"),function ($m=''){return FoundPHP_template::FixPHP($m['1']);},$ShowTPL);
//还原xml
$ShowTPL = preg_replace_callback( '/<ET_XJ>(.+?)<\/ET_XJ>/i',function ($m=''){return base64_decode($m['1']);},$ShowTPL );
//地址静态化
$ShowTPL = preg_replace_callback( array('/<(link)>(.+?)<\/link>/is','/<(js)>(.+?)<\/js>/is'),function ($m=''){return FoundPHP_template::links($m['2'],$m['1']);},$ShowTPL );
//照片懒加载
$ShowTPL = preg_replace_callback( '/<lazy\s*(.+?)>/is', function ($m=''){
$newsrc = str_ireplace('src=','data-original=',$m['1']);
$newsrc = str_ireplace('def=','src=',$newsrc);
return "<img ".trim($newsrc).">";
},$ShowTPL );
//从数组中将变量导入到当前的符号表
$sys_val = FoundPHP_template::Value();
unset($sys_val['GLOBALS']);
@extract($sys_val);
//修复"问题
//修复语言中的变量
$ShowTPL = str_replace('\\\\$','$', $ShowTPL);
$ShowTPL = str_replace('[DNA_F]',"'", $ShowTPL);
$ShowTPL = str_replace(';;$',";$", $ShowTPL);
//部分编辑器的回车导致错行
$ShowTPL = str_replace("\r","\r\n",$ShowTPL);
$ShowTPL = str_replace("\r\n\n","\r\n",$ShowTPL);
switch($this->RunType){
//编译模板
case'Cache':
$ShowTPL = str_replace(array(';;$_ET',';;;$_ET'),';$_ET',$ShowTPL);
FoundPHP_template::CompilePHP($ShowTPL,$CacheFile);
break;
//MemCache 编译
case'':
$error = $this->lang['mc_save'];
function_exists('foundphp_error')?foundphp_error($error):die($error);
memcache_set($this->Emc,$this->ThisFile.'_date', time()) OR (function_exists('foundphp_error')?foundphp_error($error):die($error));
memcache_set($this->Emc,$this->ThisFile, $ShowTPL) OR $error($this->lang['mc_save']);
break;
}
ob_start();
ob_implicit_flush(0);
if ($this->RunType=='Cache'){
@eval('$_ET.=\''.$ShowTPL.'\';');
echo $_ET;
}else{
@eval('echo '."'".str_replace('$_ET.=',"echo ",$ShowTPL)."';");
}
$content = ob_get_contents();
ob_end_clean();
//错误检测
if(strchr($content,'<b>Parse error</b>') && strchr($content,' in ') && strchr($content,"eval()'d code") && strchr($content,"on line")){
$content = FoundPHP_template::Error($content,$ShowTPL,$SourceFile);
}
//错误检查
if(strlen($content)<=0){
$error = $this->lang['not_exist3'].$SourceFile;
function_exists('foundphp_error')?foundphp_error($error):die($error);
}
return $content;
}
/*
* 编译解析处理
*/
function CompilePHP(
$content ='',
$cachename = ''
){
if ($content){
//如果没有安全文件则自动创建
if($this->RunType=='Cache' && !is_file($this->CacheDir.'index.htm')){
$Ease_name = 'Ease Template!';
$Ease_base = "<title>$Ease_name</title><a href='http://www.FoundPHP.com'>$Ease_name</a>";
$this->writer($this->CacheDir.'index.html',$Ease_base);
$this->writer($this->CacheDir.'default.htm',$Ease_base);
}
$wfile = ($cachename)?$cachename:$this->ThisFile;
$serialize_data = serialize(@$this->IncHtm);
if(strlen($serialize_data)>2){
$serial_data= '$EaseTemplate3_inc = unserialize(\''.$serialize_data.'\');';
}
//代码压缩
if($this->Compress==1){
$content = FoundPHP_template::compress_html($content);
}
$this->writer($this->FileName($wfile) ,$this->Hacker.@$serial_data.' $_ET.=\''.$content.'\';');
}
}
/*
* 输出运算
* Filename 连载编译输出文件名
*/
function output(
$Filename = ''
){
switch($this->RunType){
//Mem编译模式
case'MemCache':
if ($Filename=='include_page'){
//直接输出文件
$contents = FoundPHP_template::reader($this->FileDir[$this->ThisFile].$this->ThisFile);
}else{
$FileNames = ($Filename)?$Filename:$this->ThisFile;
//检测模版更新
$updateT = memcache_get($this->Emc,$FileNames.'_date');
$update = filemtime($this->FileDir[$this->ThisFile].$this->ThisFile);
//更新缓存
if($update>$updateT){
//创建缓存
$contents = $Filename?$this->ParseCode($this->FileList,$Filename):$this->ParseCode();
//关闭memcache
memcache_close($this->Emc);
}else{
//获取缓存
$CacheData = memcache_get($this->Emc,$FileNames);
//关闭memcache
memcache_close($this->Emc);
@extract(FoundPHP_template::Value());
ob_start();
ob_implicit_flush(0);
eval('echo \''.$CacheData.'\';');
$content = ob_get_contents();
ob_end_clean();
//错误检测
if(strchr($content,'<b>Parse error</b>') && strchr($content,' in ') && strchr($content,"eval()'d code") && strchr($content,"on line")){
$file_text = FoundPHP_template::reader($CacheFile);
$content = FoundPHP_template::Error($content,$file_text,$CacheFile);
}
$contents = $content;
unset($content); //释放内存
}
}
break;
//编译模式
case'Cache':
$update = 0;
$EaseTemplate3_inc = array();
if ($Filename=='include_page'){
//直接输出文件
$contents = FoundPHP_template::reader($this->FileDir[$this->ThisFile].$this->ThisFile);
}else{
$FileNames = ($Filename)?$Filename:$this->ThisFile;
$CacheFile = FoundPHP_template::FileName($FileNames);
$CacheFile = FoundPHP_template::FileUpdate($CacheFile);
if (@is_file($CacheFile)){
@extract(FoundPHP_template::Value());
$_ET = '';
include $CacheFile;
//获得引用列表
if(is_array($EaseTemplate3_inc)){
foreach ($EaseTemplate3_inc AS $K=>$V){
if(@filemtime($K)>@filemtime($V)) $update = 1;
}
}
//更新缓存
if(@$update ==1){
$contents = $Filename?$this->ParseCode($this->FileList,$Filename):$this->ParseCode();
}
//获得列表文件
if($_ET!='' && $update!=1){
$content = $_ET;
//错误检测
if(strchr($content,'<b>Parse error</b>') && strchr($content,' in ') && strchr($content,"eval()'d code") && strchr($content,"on line")){
$file_text = FoundPHP_template::reader($CacheFile);
$content = FoundPHP_template::Error($content,$file_text,$CacheFile);
}
$contents = $content;
unset($content); //释放内存
}
}else{
$contents = $Filename?$this->ParseCode($this->FileList,$Filename):$this->ParseCode();
}
}
break;
//替换引擎
default:
if($Filename){
if ($Filename=='include_page'){
//直接输出文件
$contents = FoundPHP_template::reader($this->FileDir[$this->ThisFile].$this->ThisFile);
}else {
$contents = $this->ParseCode($this->FileList);
}
}else{
$contents = $this->ParseCode();
}
}
//代码压缩
if($this->Compress==1){
$contents = FoundPHP_template::compress_html($contents);
}
//写入静态文件
if($this->HtmID){
FoundPHP_template::writer($this->HtmDir.$this->HtmID,$contents);
}
return $contents;
}
/*
* 多语言
*/
function lang(
$str = ''
){
global $db_obj;
//针对linux修复
if (getcwd()=='/'){
$this->LangDir = $_SERVER['DOCUMENT_ROOT'].'/'.$this->LangDir;
}
if (@is_dir($this->LangDir)){
$str = trim($str);
//采用MD5效验
$id = md5($str);
$lang_file = $this->LangDir.$this->Language.$this->Language_name;
//不存在数据则写入
if(@$this->LangData[$id]==''){
//语言包文件
if (@is_file($lang_file.'.php')){
include$lang_file.'.php';
}
//如果检测到有数据则输出
if (@$fp_lang[$id]){
return $fp_lang[$id];
}else{
//文件安全处理
$data = (!is_file($lang_file.'.php'))?"<?php\n/*\n/* SYSTN ET Language For ".$this->Language."\n*/\n":'';
//记录语言包
if (@!$fp_lang[$id]){
//过滤语言中的变量
$str = preg_replace_callback("/([$][a-zA-Z0-9\_\$]{2,100})/is",function($m=''){return $m['1'];},$str);
$str = str_replace('\\"','"', $str);
$w_str = str_replace(array('\\','\\\\$'), array('\\\\','\\\\\$'), $str);
if(strlen($w_str)>200){
FoundPHP_template::writer($lang_file.'.'.$id.'.php','<?php $EaseLang = <<< EOT'."\n".$w_str."\nEOT;\n?>");
$w_str = 'Ease Template Language Loading...'; //语言新文件
}
//写入数据
if (!@$fp_lang[$id]){
$data.= '$fp_lang[\''.$id.'\'] = <<< EOT'."\n".$w_str."\n".'EOT;'."\n";
FoundPHP_template::writer($lang_file.'.php',$data,'a+');
}
}
}
}
//单独语言文件包
if(@$this->LangData[$id]=='Ease Template Language Loading...'){
unset($EaseLang);
if(is_file($lang_file.'.'.$id.'.php')){
include_once $lang_file.'.'.$id.'.php';
$this->LangData[$id] = $EaseLang;
}
}
return (@$this->LangData[$id])?$this->LangData[$id]:$str;
}else{
return $str;
}
}
//修复PHP执行时产生的错误
function FixPHP(
$content=''
){
$search = array(
'\\\\',
'\$',
"\'",
"echo "
);
$replace = array(
'\\',
'$',
"'",
"\$_ET.="
);
$content = str_ireplace($search, $replace, $content);
$content = str_ireplace('in_array', '@in_array', $content);
$content = @preg_replace_callback("/(print_r\((.+?)\))/is", function ($m=''){$text = 'ob_start();ob_implicit_flush(0);'.$m['0'].';$result = ob_get_contents();ob_end_clean();$_ET.=$result;';return $text;},$content);
return '\';'.$content.'$_ET.=\'';
}
//修复JS执行时产生的错误
function FixJS(
$content=''
){
$content = preg_replace_callback('/\(T_T\)(.+?)\(T_T\!\)/is',function ($m=''){
$text = str_replace('echo ',';$_ET.=',$m['1']);
$text = str_replace("\'","'",$text);
return "';".$text.';$_ET.=\'';
},$content);
return $content;
}
//其他用途修复
function FixOther(
$content=''
){
$search = array(
"\'",
'in_array',
'@count(',
'count('
);
$replace = array(
"'",
'@in_array',
'count(',
'@count('
);
$content = FoundPHP_template::fix_php7(str_replace($search, $replace, $content));
return $content;
}
/*
* 检测缓存是否要更新
* filename 缓存文件名
*/
function FileUpdate($filname,$settime=0){
//检测设置模板文件
if (is_array($this->IncFile)){
unset($k,$v);
$update = 0;
$settime = ($settime>0)?$settime:@filemtime($filname);
foreach ($this->IncFile AS $k=>$v){
if (@filemtime($v)>$settime){$update = 1;}
}
//更新缓存
if($update==1){
return false;
}else {
return $filname;
}
}else{
return $filname;
}
}
//函数名: compress_js
//参数: $string
//返回值: 压缩后的$string
function compress_js($str){
$str = preg_replace_callback(array('/(\/\/.*\r\n)/',"/\s(?=\s)/"),function ($m=''){return "";},$str);
$search = array('var','\"',"\r\n","\t"," = ","; "," ","if (",") ",", ","} else {","{ ","} ");
$replace = array('var ','"','','','=',";"," ","if(",")",",","}else{","{","}");
$str= str_replace($search, $replace, $str);
return $str;
}
//函数名: compress_html
//参数: $string
//返回值: 压缩后的$string
function compress_html($str){
$str = preg_replace_callback("/(<script.+?<\/script>)/is",array($this,'replace_compress'),$str);
$str = preg_replace_callback(array('~>\s+\n~','~>\s+\r~'),function ($m=''){return ">";},$str);
$str = preg_replace_callback(array('~>\s+<~'),function ($m=''){return "><";},$str);
$str = preg_replace_callback(array("/\s*\r\n/"),function ($m=''){return "\r\n";},$str);
$search = array(
"\r\n", //清除换行符
"\n", //清除换行符
"\t", //清除制表符
"O_N_O", ////清除换行符
"\r\n\r\n",
);
$replace = array(
' ',
' ',
'',
"\r\n",
"\r\n",
);
$str = str_replace($search, $replace, $str);
return $str;
}
/*
* 连载函数
*/
function n(){
//连载模板
$this->FileList[$this->ThisFile] = $this->FileDir[$this->ThisFile];
}
/*
* 输出模板内容
* Filename 连载编译输出文件名
*/
function r(
$Filename = ''
){
$output = FoundPHP_template::output($Filename);
return $output;
}
/*
* 打印模板内容
* Filename 连载编译输出文件名
*/
function p(
$Filename = ''
){
echo FoundPHP_template::output($Filename);
}
/*
* 分析图片地址
* content 分析内容
* fileadds 文件名
*/
function ImgCheck(
$content,
$fileadds=''
){
if(@$this->AutoImage==1){
$file_dir = dirname($fileadds);
if(@$_SERVER["PATH_INFO"]!='' || @$this->WebURL){
$file_dir = $this->WebURL.$file_dir;
}
//增加替换照片目录
preg_match_all('@<!--\s*dir\s*\:\s*([a-zA-Z0-9_\-\.,]+)\s*-->@i', $content, $matches);
if (!empty($matches['1'])){
$adds_ary = explode(',',$matches['1']['0']);
}
//合并目录数组
if (!empty($adds_ary)){
if(is_array($adds_ary)){
$this->ImgDir = (!in_array($adds_ary,$this->ImgDir))?@array_merge($adds_ary, $this->ImgDir):$adds_ary;
}
}
if(is_array($this->ImgDir)){
foreach($this->ImgDir AS $rep){
$rep = trim($rep);
//检测是否执行替换
if(strrpos($content,$rep."/")){
if(substr($rep,-1)=='/'){
$rep = substr($rep,0,strlen($rep)-1);
}
$content = str_replace($rep.'/',$file_dir.'/'.$rep.'/',$content);
}
}
}
}
return $content;
}
/*
* 映射图片地址
*/
function Dirs(
$adds = ''
){
$adds_ary = explode(",",$adds);
if(is_array($adds_ary)){
$this->ImgDir = (is_array($this->ImgDir))?@array_merge($adds_ary, $this->ImgDir):$adds_ary;
}
}
/*
* 编译错误提示
*/
function Error($content='',$check_data='',$error_file=''){
if($content){
$error_line = preg_replace_callback("/.+on line <b>([0-9]{1,5})<\/b>.+/is",function ($m=''){return (int)$m['1'];},$content);
$error_info = trim(strip_tags( preg_replace_callback("/.+:(.+) in .+/is",function ($m=''){return $m['1'];},$content) ));
$content_ext = explode("\n",$check_data);
$content = '<font color="red"><h2>'.$this->lang['error'].'</h2></font><dir>';
if($error_file){
$content.= '<li><b>'.$this->lang['error_file'].'</b>'.$error_file;
}
$content .= '</li><li><b>'.$this->lang['error_line'].'</b>'.$error_line;
$content .= '</li><li><b>'.$this->lang['error_info'].'</b>'.$error_info;
$content .= '</li><li><b>'.$this->lang['error_code'].'</b><font color="red">'.trim(htmlspecialchars($content_ext[($error_line-1)]));
$content .= '</font></li></dir>';
return $content;
}
}
/*
* 获得所有设置与公共变量
*/
function Value(){
return (is_array($this->ThisValue))?@array_merge($this->ThisValue,$GLOBALS):$GLOBALS;
}
/*
* 清除设置
*/
function clear(){
$this->RunType = 'Replace';
}
/*
* 静态文件写入
*/
function htm_w(
$w_dir = '',
$w_filename = '',
$w_content = ''
){
$dvs = '';
if($w_dir && $w_filename && $w_content){
//目录检测数量
$w_dir_ex = explode('/',$w_dir);
$w_new_dir = ''; //处理后的写入目录
unset($dvs,$fdk,$fdv,$w_dir_len);
foreach((array)$w_dir_ex AS $dvs){
if(trim($dvs) && $dvs!='..'){
$w_dir_len .= '../';
$w_new_dir .= $dvs.'/';
if (!@is_dir($w_new_dir)) @mkdir($w_new_dir, 0777);
}
}
//获得需要更改的目录数
foreach((array)$this->FileDir AS $fdk=>$fdv){
$w_content = str_replace($fdv,$w_dir_len.str_replace('../','',$fdv),$w_content);
}
if(!is_dir($w_dir)){
FoundPHP_template::mk_dir($w_dir);
}
FoundPHP_template::writer($w_dir.$w_filename,$w_content);
}
}
/*
* 改变静态刷新时间
*/
function htm_time($times=0){
if((int)$times>0){
$this->HtmTime = (int)$times;
}
}
/*
* 静态文件存放的绝对目录
*/
function htm_dir($Name = ''){
if(trim($Name)){
$this->HtmDir = trim($Name).'/';
}
}
/*
* 产生静态文件输出
*/
function HtmCheck(
$Name = ''
){
//缓存文件夹
$this->cache_dir = md5(dirname($_SERVER['REQUEST_URI'])).'/'.basename(str_replace(".","_",$_SERVER['SCRIPT_NAME'])).'/';
//建立缓存文件夹
if(!is_dir($this->HtmDir.$this->cache_dir)){
FoundPHP_template::mk_dir($this->HtmDir.$this->cache_dir);
}
$this->cache_action = md5($_SERVER['QUERY_STRING']);
$this->HtmID = (trim($Name) ? trim($Name) : $this->cache_dir.$this->cache_action). '.php';
$file_adds = $this->HtmDir.$this->HtmID;
//检测时间
if(is_file($file_adds) && (time() - @filemtime($file_adds)<=$this->HtmTime)){
return $this->reader($file_adds);
}
}
/*