html.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. if(!DEFINED('EGP'))
  3. exit(header('Refresh: 0; URL=http://'.$_SERVER['SERVER_NAME'].'/404'));
  4. class html
  5. {
  6. var $dir = TPL;
  7. var $template = null;
  8. var $data = array();
  9. var $unitblock = array();
  10. public $arr;
  11. public $select_template;
  12. public function set($name, $var, $unset = false)
  13. {
  14. $this->data['['.$name.']'] = $var;
  15. if($unset)
  16. unset($this->arr[$name]);
  17. return NULL;
  18. }
  19. public function unit($name, $var = false, $mirror = false)
  20. {
  21. $block = str_replace($name, "'\\|".$name."\\|(.*?)\\|_".$name."\\|'si", $name);
  22. $var = $var ? '\\1' : '';
  23. $this->unitblock[$block] = $var;
  24. if($mirror)
  25. {
  26. $block = str_replace($name, "'\\|!".$name."\\|(.*?)\\|_!".$name."\\|'si", $name);
  27. $var = !$var ? '\\1' : '';
  28. $this->unitblock[$block] = $var;
  29. }
  30. return NULL;
  31. }
  32. public function nav($name, $link = false)
  33. {
  34. $this->get('nav');
  35. if($link)
  36. {
  37. $this->set('link', $link);
  38. $this->unit('link', 1, 1);
  39. }else
  40. $this->unit('link', 0, 1);
  41. $this->set('name', $name);
  42. $this->pack('nav');
  43. return NULL;
  44. }
  45. public function get($name, $path = '')
  46. {
  47. global $device, $cfg;
  48. $path_root = $device == '!mobile' ? '' : 'megp/';
  49. $path = $path_root.$path;
  50. if($path != '')
  51. $name = str_replace('//', '/', $path.'/'.$name);
  52. if(!file_exists($this->dir.'/'.$name.'.html'))
  53. {
  54. $route = explode('/', $name);
  55. $namefile = end($route);
  56. $dir = $this->dir.str_replace($namefile, '', $name);
  57. die('Error: html file <u>'.$namefile.'.html</u> not found in: <u>'.$dir.'</u>');
  58. }
  59. $this->template = file_get_contents($this->dir.'/'.$name.'.html');
  60. $this->select_template = $this->template;
  61. return NULL;
  62. }
  63. private function delete()
  64. {
  65. unset($this->data);
  66. unset($this->unitblock);
  67. $this->select_template = $this->template;
  68. return NULL;
  69. }
  70. public function pack($compile)
  71. {
  72. if(isset($this->unitblock))
  73. {
  74. $find_preg = array();
  75. $replace_preg = array();
  76. foreach($this->unitblock as $key_find => $key_replace)
  77. {
  78. $find_preg[] = $key_find;
  79. $replace_preg[] = $key_replace;
  80. }
  81. $this->select_template = preg_replace($find_preg, $replace_preg, $this->select_template);
  82. }
  83. $find = array();
  84. $replace = array();
  85. if(isset($this->data))
  86. {
  87. foreach($this->data as $key_find => $key_replace)
  88. {
  89. $find[] = $key_find;
  90. $replace[] = $key_replace;
  91. }
  92. }
  93. $this->select_template = str_replace($find, $replace, $this->select_template);
  94. if(isset($this->arr[$compile]))
  95. $this->arr[$compile] .= $this->select_template;
  96. else
  97. $this->arr[$compile] = $this->select_template;
  98. $this->delete();
  99. return NULL;
  100. }
  101. public function upd($name, $old = array(), $new = array())
  102. {
  103. $this->arr[$name] = str_replace($old, $new, $this->arr[$name]);
  104. return NULL;
  105. }
  106. public function unitall($name, $arr = array(), $var = false, $mirror = false)
  107. {
  108. $block = str_replace($name, "'\\|".$name."\\|(.*?)\\|_".$name."\\|'si", $name);
  109. $var = $var ? '\\1' : '';
  110. $this->unitblock[$block] = $var;
  111. if($mirror)
  112. {
  113. $block = str_replace($name, "'\\|!".$name."\\|(.*?)\\|_!".$name."\\|'si", $name);
  114. $var = !$var ? '\\1' : '';
  115. $this->unitblock[$block] = $var;
  116. }
  117. if(count($this->unitblock))
  118. {
  119. foreach($this->unitblock as $key_find => $key_replace)
  120. {
  121. $find_preg[] = $key_find;
  122. $replace_preg[] = $key_replace;
  123. }
  124. $this->arr[$arr] = preg_replace($find_preg, $replace_preg, $this->arr[$arr]);
  125. }
  126. return NULL;
  127. }
  128. }
  129. $html = new html;
  130. ?>