| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class pTemplate { |
|---|
| 22 |
var $tpl_name; |
|---|
| 23 |
var $tpl_data; |
|---|
| 24 |
var $tpl_options; |
|---|
| 25 |
|
|---|
| 26 |
var $dtpl_name; |
|---|
| 27 |
var $dtpl_data; |
|---|
| 28 |
var $dtpl_options; |
|---|
| 29 |
var $dtpl_values; |
|---|
| 30 |
|
|---|
| 31 |
var $namespace; |
|---|
| 32 |
|
|---|
| 33 |
var $root_dir; |
|---|
| 34 |
|
|---|
| 35 |
var $tpl_start_tag; |
|---|
| 36 |
var $tpl_end_tag; |
|---|
| 37 |
var $tpl_start_tag_name; |
|---|
| 38 |
var $tpl_end_tag_name; |
|---|
| 39 |
var $tpl_name_rexpr; |
|---|
| 40 |
|
|---|
| 41 |
var $tpl_start_rexpr; |
|---|
| 42 |
var $tpl_end_rexpr; |
|---|
| 43 |
|
|---|
| 44 |
var $last_parsed; |
|---|
| 45 |
|
|---|
| 46 |
var $stack; |
|---|
| 47 |
var $sp; |
|---|
| 48 |
|
|---|
| 49 |
function pTemplate($r_dir = '') { |
|---|
| 50 |
$this->tpl_name = array(); |
|---|
| 51 |
$this->tpl_data = array(); |
|---|
| 52 |
$this->tpl_options = array(); |
|---|
| 53 |
|
|---|
| 54 |
$this->dtpl_name = array(); |
|---|
| 55 |
$this->dtpl_data = array(); |
|---|
| 56 |
$this->dtpl_options = array(); |
|---|
| 57 |
$this->dtpl_values = array(); |
|---|
| 58 |
|
|---|
| 59 |
$this->namespace = array(); |
|---|
| 60 |
|
|---|
| 61 |
if ($r_dir) { |
|---|
| 62 |
$this->set_root($r_dir); |
|---|
| 63 |
} else { |
|---|
| 64 |
$this->set_root(); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
$this->tpl_start_tag = '<!-- '; |
|---|
| 68 |
$this->tpl_end_tag = ' -->'; |
|---|
| 69 |
$this->tpl_start_tag_name = 'BDP: '; |
|---|
| 70 |
$this->tpl_end_tag_name = 'EDP: '; |
|---|
| 71 |
$this->tpl_name_rexpr = '([a-z0-9][a-z0-9\_]*)'; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
$this->tpl_include = 'INCLUDE "([^\"]+)"'; |
|---|
| 75 |
|
|---|
| 76 |
$this->tpl_start_rexpr = '/'; |
|---|
| 77 |
$this->tpl_start_rexpr .= $this->tpl_start_tag; |
|---|
| 78 |
$this->tpl_start_rexpr .= $this->tpl_start_tag_name; |
|---|
| 79 |
$this->tpl_start_rexpr .= $this->tpl_name_rexpr; |
|---|
| 80 |
$this->tpl_start_rexpr .= $this->tpl_end_tag . '/' ; |
|---|
| 81 |
|
|---|
| 82 |
$this->tpl_end_rexpr = '/'; |
|---|
| 83 |
$this->tpl_end_rexpr .= $this->tpl_start_tag; |
|---|
| 84 |
$this->tpl_end_rexpr .= $this->tpl_end_tag_name; |
|---|
| 85 |
$this->tpl_end_rexpr .= $this->tpl_name_rexpr; |
|---|
| 86 |
$this->tpl_end_rexpr .= $this->tpl_end_tag . '/'; |
|---|
| 87 |
|
|---|
| 88 |
$this->last_parsed = ''; |
|---|
| 89 |
|
|---|
| 90 |
$this->stack = array(); |
|---|
| 91 |
$this->sp = 0; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
function set_root($set_dir = '.') { |
|---|
| 95 |
$this->root_dir = $set_dir; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
function assign($nsp_name, $nsp_data = '') { |
|---|
| 99 |
if (gettype($nsp_name) == "array") { |
|---|
| 100 |
foreach ($nsp_name as $key => $value) { |
|---|
| 101 |
$this->namespace[$key] = $value; |
|---|
| 102 |
} |
|---|
| 103 |
} else { |
|---|
| 104 |
$this->namespace[$nsp_name] = $nsp_data; |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
function unsign($nsp_name) { |
|---|
| 109 |
if (gettype($nsp_name) == "array") { |
|---|
| 110 |
foreach ($nsp_name as $key => $value) { |
|---|
| 111 |
unset($this->namespace[$key]); |
|---|
| 112 |
} |
|---|
| 113 |
} else { |
|---|
| 114 |
unset($this->namespace[$nsp_name]); |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
function define($t_name, $t_value = '') { |
|---|
| 119 |
if (gettype($t_name) == "array") { |
|---|
| 120 |
foreach ($t_name as $key => $value) { |
|---|
| 121 |
$this->tpl_name[$key] = $value; |
|---|
| 122 |
$this->tpl_data[$key] = ''; |
|---|
| 123 |
$this->tpl_options[$key] = ''; |
|---|
| 124 |
} |
|---|
| 125 |
} else { |
|---|
| 126 |
$this->tpl_name[$t_name] = $t_value; |
|---|
| 127 |
$this->tpl_data[$t_name] = ''; |
|---|
| 128 |
$this->tpl_options[$t_name] = ''; |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
function define_dynamic($t_name, $t_value = '') { |
|---|
| 133 |
if (gettype($t_name) == "array") { |
|---|
| 134 |
foreach ($t_name as $key => $value) { |
|---|
| 135 |
$this->dtpl_name[$key] = $value; |
|---|
| 136 |
$this->dtpl_data[$key] = ''; |
|---|
| 137 |
$this->dtpl_options[$key] = ''; |
|---|
| 138 |
} |
|---|
| 139 |
} else { |
|---|
| 140 |
$this->dtpl_name[$t_name] = $t_value; |
|---|
| 141 |
$this->dtpl_data[$t_name] = ''; |
|---|
| 142 |
$this->dtpl_options[$t_name] = ''; |
|---|
| 143 |
} |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
function define_no_file($t_name, $t_value = '') { |
|---|
| 147 |
if (gettype($t_name) == "array") { |
|---|
| 148 |
foreach ($t_name as $key => $value) { |
|---|
| 149 |
$this->tpl_name[$key] = '_no_file_'; |
|---|
| 150 |
$this->tpl_data[$key] = $value; |
|---|
| 151 |
$this->tpl_options[$key] = ''; |
|---|
| 152 |
} |
|---|
| 153 |
} else { |
|---|
| 154 |
$this->tpl_name[$t_name] = '_no_file_'; |
|---|
| 155 |
$this->tpl_data[$t_name] = $t_value; |
|---|
| 156 |
$this->tpl_options[$t_name] = ''; |
|---|
| 157 |
} |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
function define_no_file_dynamic($t_name, $t_value = '') { |
|---|
| 161 |
if (gettype($t_name) == "array") { |
|---|
| 162 |
foreach ($t_name as $key => $value) { |
|---|
| 163 |
$this->dtpl_name[$key] = '_no_file_'; |
|---|
| 164 |
|
|---|
| 165 |
$this->dtpl_data[$key] = $value; |
|---|
| 166 |
|
|---|
| 167 |
$this->dtpl_data[strtoupper($key)] = $value; |
|---|
| 168 |
|
|---|
| 169 |
$this->dtpl_options[$key] = ''; |
|---|
| 170 |
} |
|---|
| 171 |
} else { |
|---|
| 172 |
$this->dtpl_name[$t_name] = '_no_file_'; |
|---|
| 173 |
|
|---|
| 174 |
$this->dtpl_data[$t_name] = $t_value; |
|---|
| 175 |
|
|---|
| 176 |
$this->dtpl_data[strtoupper($t_name)] = @$t_value; |
|---|
| 177 |
|
|---|
| 178 |
$this->dtpl_options[$t_name] = ''; |
|---|
| 179 |
} |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
function find_next($data, $spos) { |
|---|
| 183 |
do { |
|---|
| 184 |
$tag_spos = strpos($data, $this->tpl_start_tag, $spos + 1); |
|---|
| 185 |
|
|---|
| 186 |
if (gettype($tag_spos) == 'boolean') { |
|---|
| 187 |
return false; |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
$tag_epos = strpos($data, $this->tpl_end_tag, $tag_spos + 1); |
|---|
| 191 |
|
|---|
| 192 |
if (gettype($tag_epos) == 'boolean') { |
|---|
| 193 |
return false; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
$length = $tag_epos + strlen($this->tpl_end_tag) - $tag_spos; |
|---|
| 197 |
|
|---|
| 198 |
$tag = substr($data, $tag_spos, $length); |
|---|
| 199 |
|
|---|
| 200 |
if ($tag) { |
|---|
| 201 |
if (preg_match($this->tpl_start_rexpr, $tag, $matches)) { |
|---|
| 202 |
return array($matches[1], 'b', $tag_spos, $tag_epos + strlen($this->tpl_end_tag) - 1); |
|---|
| 203 |
} else if (preg_match($this->tpl_end_rexpr, $tag, $matches)) { |
|---|
| 204 |
return array($matches[1], 'e', $tag_spos, $tag_epos + strlen($this->tpl_end_tag) - 1); |
|---|
| 205 |
} else { |
|---|
| 206 |
$spos = $tag_epos; |
|---|
| 207 |
} |
|---|
| 208 |
} else { |
|---|
| 209 |
return false; |
|---|
| 210 |
} |
|---|
| 211 |
} while (true); |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
function find_next_curl($data, $spos) { |
|---|
| 215 |
$curl_b = strpos($data, '{', $spos + 1); |
|---|
| 216 |
|
|---|
| 217 |
$curl_e = strpos($data, '}', $spos + 1); |
|---|
| 218 |
|
|---|
| 219 |
if ($curl_b) { |
|---|
| 220 |
if ($curl_e) { |
|---|
| 221 |
if ($curl_b < $curl_e) { |
|---|
| 222 |
return array('{', $curl_b); |
|---|
| 223 |
} else { |
|---|
| 224 |
return array('}', $curl_e); |
|---|
| 225 |
} |
|---|
| 226 |
} else { |
|---|
| 227 |
return array('{', $curl_b); |
|---|
| 228 |
} |
|---|
| 229 |
} else { |
|---|
| 230 |
if ($curl_e) { |
|---|
| 231 |
return array('}', $curl_e); |
|---|
| 232 |
} else { |
|---|
| 233 |
return false; |
|---|
| 234 |
} |
|---|
| 235 |
} |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
function devide_dynamic($data) { |
|---|
| 239 |
$start_from = -1; |
|---|
| 240 |
|
|---|
| 241 |
$tag = $this->find_next($data, $start_from); |
|---|
| 242 |
|
|---|
| 243 |
while ($tag) { |
|---|
| 244 |
if ($tag[1] == 'b') { |
|---|
| 245 |
$this->stack[$this->sp++] = $tag; |
|---|
| 246 |
|
|---|
| 247 |
$start_from = $tag[3]; |
|---|
| 248 |
} else { |
|---|
| 249 |
$tpl_name = $tag[0]; |
|---|
| 250 |
|
|---|
| 251 |
$tpl_eb_pos = $tag[2]; |
|---|
| 252 |
$tpl_ee_pos = $tag[3]; |
|---|
| 253 |
|
|---|
| 254 |
$tag = $this->stack [--$this->sp]; |
|---|
| 255 |
|
|---|
| 256 |
$tpl_bb_pos = $tag[2]; |
|---|
| 257 |
$tpl_be_pos = $tag[3]; |
|---|
| 258 |
|
|---|
| 259 |
$this->dtpl_data[strtoupper($tpl_name)] = substr($data, $tpl_be_pos + 1, $tpl_eb_pos - $tpl_be_pos - 1); |
|---|
| 260 |
|
|---|
| 261 |
$this->dtpl_data[$tpl_name] = substr($data, $tpl_be_pos + 1, $tpl_eb_pos - $tpl_be_pos - 1); |
|---|
| 262 |
|
|---|
| 263 |
$data = substr_replace($data, "{" . strtoupper($tpl_name) . "}", $tpl_bb_pos, $tpl_ee_pos - $tpl_bb_pos + 1); |
|---|
| 264 |
|
|---|
| 265 |
$start_from = $tpl_bb_pos + strlen("{" . $tpl_name . "}") - 1; |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
$tag = $this->find_next($data, $start_from); |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
return $data; |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
function substitute_dynamic($data) { |
|---|
| 275 |
$this->sp = 0; |
|---|
| 276 |
|
|---|
| 277 |
$start_from = -1; |
|---|
| 278 |
|
|---|
| 279 |
$curl_b = substr($data, '{', $start_from); |
|---|
| 280 |
|
|---|
| 281 |
if ($curl_b) { |
|---|
| 282 |
$this->stack[$this->sp++] = array('{', $curl_b); |
|---|
| 283 |
|
|---|
| 284 |
$curl = $this->find_next_curl($data, $start_from); |
|---|
| 285 |
|
|---|
| 286 |
while ($curl) { |
|---|
| 287 |
if ($curl[0] == '{') { |
|---|
| 288 |
$this->stack[$this->sp++] = $curl; |
|---|
| 289 |
|
|---|
| 290 |
$start_from = $curl[1]; |
|---|
| 291 |
} else { |
|---|
| 292 |
$curl_e = $curl[1]; |
|---|
| 293 |
|
|---|
| 294 |
if ($this->sp > 0) { |
|---|
| 295 |
$curl = $this->stack [--$this->sp]; |
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
$curl_b = $curl[1]; |
|---|
| 299 |
|
|---|
| 300 |
if ($curl_b < $curl_e + 1) { |
|---|
| 301 |
$var_name = substr($data, $curl_b + 1, $curl_e - $curl_b - 1); |
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
if (preg_match('/[A-Z0-9][A-Z0-9\_]*/', $var_name)) { |
|---|
| 305 |
if (isset($this->namespace[$var_name])) { |
|---|
| 306 |
$data = substr_replace($data, $this->namespace[$var_name], $curl_b, $curl_e - $curl_b + 1); |
|---|
| 307 |
|
|---|
| 308 |
$start_from = $curl_b - 1; |
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
} else if (isset($this->dtpl_data[$var_name])) { |
|---|
| 312 |
$data = substr_replace($data, $this->dtpl_data[$var_name], $curl_b, $curl_e - $curl_b + 1); |
|---|
| 313 |
$start_from = $curl_b - 1; |
|---|
| 314 |
|
|---|
| 315 |
} else { |
|---|
| 316 |
$start_from = $curl_b; |
|---|
| 317 |
|
|---|
| 318 |
} |
|---|
| 319 |
} else { |
|---|
| 320 |
$start_from = $curl_b; |
|---|
| 321 |
|
|---|
| 322 |
} |
|---|
| 323 |
} else { |
|---|
| 324 |
$start_from = $curl_e; |
|---|
| 325 |
|
|---|
| 326 |
} |
|---|
| 327 |
} else { |
|---|
| 328 |
$start_from = $curl_e; |
|---|
| 329 |
} |
|---|
| 330 |
} |
|---|
| 331 |
|
|---|
| 332 |
$curl = $this->find_next_curl($data, $start_from); |
|---|
| 333 |
} |
|---|
| 334 |
|
|---|
| 335 |
return $data; |
|---|
| 336 |
} else { |
|---|
| 337 |
return $data; |
|---|
| 338 |
|
|---|
| 339 |
} |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
function is_safe($fname) { |
|---|
| 343 |
if (file_exists(($this->root_dir) . '/' . $fname)) { |
|---|
| 344 |
return true; |
|---|
| 345 |
} |
|---|
| 346 |
|
|---|
| 347 |
return false; |
|---|
| 348 |
} |
|---|
| 349 |
|
|---|
| 350 |
function get_file($fname) { |
|---|
| 351 |
if (is_array($fname)) { |
|---|
| 352 |
$fname = (!empty($this->__includeRelativePath) ? $this->__includeRelativePath . '/' : '') . $fname[1]; |
|---|
| 353 |
} |
|---|
| 354 |
if ($this->is_safe($fname)) { |
|---|
| 355 |
if (!($fp = fopen($fn = ($this->root_dir) . '/' . $fname, 'r'))) { |
|---|
| 356 |
return ""; |
|---|
| 357 |
} |
|---|
| 358 |
|
|---|
| 359 |
$res = fread($fp, filesize(($this->root_dir) . '/' . $fname)); |
|---|
| 360 |
|
|---|
| 361 |
if (!$res) { |
|---|
| 362 |
return ""; |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
fclose($fp); |
|---|
| 366 |
|
|---|
| 367 |
$this->__includeRelativePath = dirname($fn); |
|---|
| 368 |
$res = preg_replace_callback('~' . $this->tpl_start_tag . $this->tpl_include . $this->tpl_end_tag . '~m', array($this, 'get_file'), $res); |
|---|
| 369 |
|
|---|
| 370 |
return $res; |
|---|
| 371 |
} |
|---|
| 372 |
|
|---|
| 373 |
return ""; |
|---|
| 374 |
} |
|---|
| 375 |
|
|---|
| 376 |
function find_origin($tname) { |
|---|
| 377 |
if (!@$this->dtpl_name[$tname]) { |
|---|
| 378 |
return false; |
|---|
| 379 |
} while (!preg_match('/\.[Tt][Pp][Ll]/', $this->dtpl_name[$tname]) && !preg_match('/_no_file_/', $this->dtpl_name[$tname]) |
|---|
| 380 |
) { |
|---|
| 381 |
$tname = $this->dtpl_name[$tname]; |
|---|
| 382 |
} |
|---|
| 383 |
|
|---|
| 384 |
return $tname; |
|---|
| 385 |
} |
|---|
| 386 |
|
|---|
| 387 |
function parse_dynamic($pname, $tname, $ADD_FLAG) { |
|---|
| 388 |
$CHILD = false; |
|---|
| 389 |
$parent = ''; |
|---|
| 390 |
$swap = ''; |
|---|
| 391 |
|
|---|
| 392 |
if (!preg_match('/\.[Tt][Pp][Ll]/', @$this->dtpl_name[$tname]) && !preg_match('/_no_file_/', @$this->dtpl_name[$tname])) { |
|---|
| 393 |
$CHILD = true; |
|---|
| 394 |
|
|---|
| 395 |
$parent = $this->find_origin($tname); |
|---|
| 396 |
|
|---|
| 397 |
if (!$parent) { |
|---|
| 398 |
return false; |
|---|
| 399 |
} |
|---|
| 400 |
} |
|---|
| 401 |
|
|---|
| 402 |
if ($CHILD) { |
|---|
| 403 |
$swap = $parent; |
|---|
| 404 |
$parent = $tname; |
|---|
| 405 |
$tname = $swap; |
|---|
| 406 |
} |
|---|
| 407 |
|
|---|
| 408 |
if (!@$this->dtpl_data[$tname]) { |
|---|
| 409 |
@$this->dtpl_data[$tname] = $this->get_file(@$this->dtpl_name[$tname]); |
|---|
| 410 |
} |
|---|
| 411 |
|
|---|
| 412 |
if (!preg_match('/d\_/', @$this->dtpl_options[$tname])) { |
|---|
| 413 |
@$this->dtpl_options[$tname] .= 'd_'; |
|---|
| 414 |
|
|---|
| 415 |
$tpl_origin = @$this->dtpl_data[$tname]; |
|---|
| 416 |
|
|---|
| 417 |
@$this->dtpl_data[$tname] = $this->devide_dynamic($tpl_origin); |
|---|
| 418 |
} |
|---|
| 419 |
|
|---|
| 420 |
if ($CHILD) { |
|---|
| 421 |
$swap = $parent; |
|---|
| 422 |
$parent = $tname; |
|---|
| 423 |
$tname = $swap; |
|---|
| 424 |
} |
|---|
| 425 |
|
|---|
| 426 |
if ($ADD_FLAG) { |
|---|
| 427 |
$safe = @$this->namespace[$pname]; |
|---|
| 428 |
|
|---|
| 429 |
$this->namespace[$pname] = $safe . ($this->substitute_dynamic($this->dtpl_data[$tname], $ADD_FLAG)); |
|---|
| 430 |
} else { |
|---|
| 431 |
$this->namespace[$pname] = $this->substitute_dynamic($this->dtpl_data[$tname], $ADD_FLAG); |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
return true; |
|---|
| 435 |
} |
|---|
| 436 |
|
|---|
| 437 |
function parse($pname, $tname) { |
|---|
| 438 |
if (!preg_match('/[A-Z0-9][A-Z0-9\_]*/', $pname)) { |
|---|
| 439 |
return false; |
|---|
| 440 |
} |
|---|
| 441 |
|
|---|
| 442 |
if (!preg_match('/[A-Za-z0-9][A-Za-z0-9\_]*/', $tname)) { |
|---|
| 443 |
return false; |
|---|
| 444 |
} |
|---|
| 445 |
|
|---|
| 446 |
$ADD_FLAG = false; |
|---|
| 447 |
|
|---|
| 448 |
if (preg_match('/^\./', $tname)) { |
|---|
| 449 |
$tname = substr($tname, 1); |
|---|
| 450 |
|
|---|
| 451 |
$ADD_FLAG = true; |
|---|
| 452 |
} |
|---|
| 453 |
|
|---|
| 454 |
if (@$this->tpl_name[$tname] == '_no_file_' || preg_match('/\.[Tt][Pp][Ll]/', @$this->tpl_name[$tname])) { |
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
if (@$this->tpl_data[$tname] == '') { |
|---|
| 458 |
$this->tpl_data[$tname] = $this-> |
|---|