Repository navigation
Expand file tree
/
Copy pathmytemplate-engine.php
More file actions
executable file
·362 lines (283 loc) · 9.25 KB
/
Copy pathmytemplate-engine.php
File metadata and controls
executable file
·362 lines (283 loc) · 9.25 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
<?php
/*##############################################
* Mytemplate Engine version 1.3.2
* Authors:
* 1- Bruce [ founder] http://developer.ly || E-mail:mohamed.endisha@gmail.com
* 2- Saanina [develper] http://moffed.com || saanina@gmail.com
*
*###############################################
* last change reconstruct class 2017/10/04
* last change was : 10:16 pm 2008/2/17
*
* last edit by : Bruce..
* fix when print global value inside loop
* add assets js & css files
****/
class MyTemplate{
public $Tempdir = '/tpl/';
public $app_dir = '';
public $assets_dir = 'public/assets/';
public $cachedir = '/cache/';
public $cachetime = 0; //by hour
public $php_tags = 'on';
public $php_compile = 'on';
public $global_vars = 'off';
private $vars = array();
function __construct(){
$this->Tempdir = __DIR__ . '/' . $this->Tempdir . '/';
$this->cachedir = __DIR__ . '/' . $this->cachedir . '/';
}
/**
* Remove any PHP tags that do not belong, these regular expressions are derived from
* the ones that exist in zend_language_scanner.l
* access : privat ..
*/
function remove_php_tags(&$code) {
// This matches the information gathered from the internal PHP lexer
$match = array(
'#<([\?%])=?.*?\1>#s',
'#<script\s+language\s*=\s*(["\']?)php\1\s*>.*?</script\s*>#s',
'#<\?php(?:\r\n?|[ \n\t]).*?\?>#s'
);
$code = preg_replace($match, '', $code);
}
/**
* compile our codes to be real code ..
* accses : private..
*/
private function compile($content) { //was display function here
//loop
$content = preg_replace_callback('#{loop[^>]([a-zA-Z0-9\_\-\+\./]+)(.*?)\}(.*?){\/loop\}#is',array(&$this,'loop_bt'), $content);
//if and her sisters
$content = preg_replace_callback('#{(if|elseif)[^>]([a-zA-Z0-9\_\-\+\./]+)[^>]([a-zA-Z\=\!\>\%\&\|\</]{1,3})[^>](.*?)\}#i',array(&$this,'if_expr'), $content);
//matchs ..
$matchs = array(
'#{include_tpl[^>](.*?)\}#i',
'#{include_script[^>](.*?)\}#i',
'#{if[^>]([a-zA-Z0-9\_\-\+\./]+)\}#i',
'#{elseif[^>]([a-zA-Z0-9\_\-\+\./]+)\}#i',
'#{else\}#i',
'#{\/if\}#i',
($this->php_compile == 'on') ? '#{php\}(.*?){\/php\}#is' : '##',
($this->global_vars == 'on') ? '#{([a-zA-Z0-9\_\-\+\./]+)}#' : '##',
'#{assets[^>]([a-zA-Z0-9\_\-\+\./]+)}#is',
);
//replaces
$replaces = array (
'<?php $this->include_tpl(\'\\1\'); ?>',
'<?php include(\'\\1\'); ?>',
'<?php if($this->vars[\'\\1\']){ ?>',
'<?php } elseif(\\1){?>',
'<?php } else { ?>',
'<?php } ?>',
($this->php_compile == 'on') ? '<?php \\1 ?>' : '',
($this->global_vars == 'on') ? '<?php print $this->vars[\'\\1\'];?>' : '',
'<?php echo $this->assets(\'\\1\'); ?>',
);
//show time ..
$content = preg_replace($matchs,$replaces, $content);
if($this->global_vars != 'on'){
$content = preg_replace_callback('#{([a-zA-Z0-9\_\-\+\./]+)}#',array(&$this,'assign_if_global_off'), $content);
}
return $content;
}
/**
* assign variables ... i mean make copy from variable to use it in our template ..
* we have 2 type , singal or array ..
* access : public ..
**/
public function assign($name, $value){
$this->vars[$name] = $value;
}
//for array
public function assign_r($array){
foreach($array as $key=>$value){
$this->assign($key, $value);
}
}
private function assets($file){
return $this->app_dir.'/'.$this->assets_dir.$file;
}
/**
* we make this method to make your template more limited , just variables which you make it ..
* access : private ..
**/
private function assign_if_global_off($match){
if(isset($this->vars[trim($match[1])])){
return '<?php print $this->vars[\'' . $match[1] . '\'];?>';
}else{
return "{" . $match[1] . "}";
}
}
/*
*including another template in current one ,,, its prety and helpful .. ;)
* access : public ..
*/
private function include_tpl($tpl_page){
print $this->show($tpl_page);
}
/*
*this is the father of template engine ,, i dont why i love play with it , its like my little kid :)
* access : private ..
**/
private function loop_bt ($match_loop) {
//matchs ..
$matchs = array('#{odd[^>]([a-zA-Z0-9\_\-\+\./]+)\}(.*?){\/odd\}#is',
'#{even[^>]([a-zA-Z0-9\_\-\+\./]+)\}(.*?){\/even\}#is',
'#{\/if\}#i',
'#{([a-zA-Z0-9\_\-\+\./]+)}#',
'#{rand=(.*?),(.*?)}#i',
);
//replaces
$replaces = array( '<?php if(intval($value[\'\\1\'])%2){?> \\2 <?php } ?>',
'<?php if(intval($value[\'\\1\'] % 2) == 0){?> \\2 <?php } ?>',
'<?php } ?>',
'<?php print isset($value[\'\\1\'])? $value[\'\\1\'] : $this->vars[\'\\1\'];?>',
'<?php $tpl_rand_is=($tpl_rand_is==0)?1:0; print(($tpl_rand_is==1) ?"\\1":"\\2") ;?>',
);
//show time
$content_loop = preg_replace($matchs, $replaces, $match_loop[3]);
return ('<?php $this->assign(\''.$match_loop[1].'\', $GLOBALS[\''.$match_loop[1].'\']); foreach($this->vars[\''.$match_loop[1].'\'] as $key=>$value){?>'.$content_loop.'<?php }/*endloop*/?>');
}
/*
for if / elseif experssions ,,
acsess : private ..
*/
private function if_expr(&$match) {
//if it's IF ..
if (strtolower(trim($match[1])) == 'if') {
$result .= 'if (';
}elseif(strtolower(trim($match[1])) == 'elseif') {
$result .= '} elseif(';
}
///its for loop or just normal if .. [ loop begin with __
if (strpos($match[2], '__') !== false) {
$val = str_replace('__','',$match[2]);
$result .= '$this->value[\''.trim($val).'\']';
}else{
$result .= '$this->vars[\''.trim($match[2]).'\']';
}
//so , now experssions signs ..
if (!empty($match[3])) {
$expr = trim(strtolower($match[3]));
switch($expr) {
case '==':
case 'eq':
$expr_is = '==';
break;
case '!=':
case '<>':
case 'ne':
case 'neq':
$expr_is = '!=';
break;
case '<':
case 'lt':
$expr_is = '<';
break;
case '<=':
case 'le':
case 'lte':
$expr_is = '<=';
break;
case '>':
case 'gt':
$expr_is = '>';
break;
case '>=':
case 'ge':
case 'gte':
$expr_is = '>=';
break;
case '&&':
case 'and':
$expr_is = '&&';
break;
case '||':
case 'or':
$expr_is = '||';
break;
case '!':
case 'not':
$expr_is = '!';
break;
case '%':
case 'mod':
$expr_is = '%';
break;
default:
$expr_is = $expr;
}
$result .= ' ' . $expr_is . ' ';
}
//whats match4 .. its something cant change it now .. i will do something good with it later ..
$result .= $match[4];
//some .. additions
$result .= '){';
//return it
return '<?php '.$result. '?>';
}
/**
* now , showing our template ,, its prety function
* we have 2 type , by filename or just by contents ..
* name_c : just if there is contents not from file ..
* access : public ..
**/
public function show($content, $name_c='', $cache_time = false){
//at first ..
$page_is = ($name_c!='') ? $name_c : $content;
$cache_time = ($cache_time !== false )? $cache_time : $this->cachetime;
//global vars
if (empty($this->vars) || !is_array($this->vars)){
$this->vars = array();
}
if($this->global_vars == 'on'){
$this->vars = array_merge($this->vars, $GLOBALS);
}
//check step
if (empty($this->cachedir)) {
$this->cachedir = $_ENV["TEMP"];
}
if (!is_writeable( $this->cachedir ) ) {
echo "<i>ERROR :</i> could not be write in cach folder!";
}
//delete step
$npage = $page_is . '.mytpl.php';
$tpage = @filemtime("$this->cachedir/$npage");
if($tpage >= ($tpage+(3600*$cache_time))){
@unlink("$this->cachedir/$npage");
}
//including step
if(!file_exists("$this->cachedir/$npage")){
if ($name_c == ''){
if(!file_exists("$this->Tempdir$npage")){
echo "<i>ERROR :</i> <b>$page_is</b> Template Not Found!";
exit();
}
ob_start();
include("$this->Tempdir$npage");
$pageTemp= ob_get_clean();
}else {
$pageTemp = $content;
}
if($this->php_tags != 'on'){
//reomve any any php tags ,, so if you want php code use our code {php}
$this->remove_php_tags($pageTemp);
}
//compile content
$pageTemp = $this->compile($pageTemp);
//then
$filenum = @fopen("$this->cachedir/$npage", 'w');
flock($filenum, LOCK_EX);
@fwrite($filenum, $pageTemp);
fclose($filenum);
@chmod($filenum, 0666);
}
//get
ob_start();
include("$this->cachedir/$npage");
$pageTemp = ob_get_clean();
//now prepare to print
return $pageTemp;
}
} # end of class