PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/core/lhtpl/tpl.php

http://hppg.googlecode.com/
PHP | 335 lines | 226 code | 66 blank | 43 comment | 26 complexity | b404a6a53f3a4eb3151cd489bd584466 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Main part of code from :
  4. * http://www.massassi.com/php/articles/template_engines/
  5. *
  6. * Modified by remdex
  7. * */
  8. class erLhcoreClassTemplate {
  9. private $vars = array(); /// Holds all the template variables
  10. private static $instance = null;
  11. private $cacheWriter = null;
  12. private $cacheTemplates = array() ;
  13. private $cacheEnabled = true;
  14. private $templatecompile = true;
  15. var $file = null;
  16. public static function getInstance($file = null)
  17. {
  18. if ( is_null( self::$instance ) )
  19. {
  20. self::$instance = new erLhcoreClassTemplate($file);
  21. } else {
  22. self::$instance->setFile($file);
  23. self::$instance->vars = array();
  24. }
  25. return self::$instance;
  26. }
  27. /**
  28. * Constructor
  29. *
  30. * @param $file string the file name you want to load
  31. */
  32. function erLhcoreClassTemplate($file = null) {
  33. $cfg = erConfigClassLhConfig::getInstance();
  34. $this->cacheEnabled = $cfg->getSetting( 'site', 'templatecache' );
  35. $this->templatecompile = $cfg->getSetting( 'site', 'templatecompile' );
  36. if (!is_null($file))
  37. $this->file = $file;
  38. $cacheObj = CSCacheAPC::getMem();
  39. if (($this->cacheTemplates = $cacheObj->restore('templateCacheArray_version_'.$cacheObj->getCacheVersion('site_version'))) === false)
  40. {
  41. $sys = erLhcoreClassSystem::instance()->SiteDir;
  42. $this->cacheWriter = new ezcCacheStorageFileArray($sys . 'cache/cacheconfig/');
  43. if (($this->cacheTemplates = $this->cacheWriter->restore('templateCache')) == false)
  44. {
  45. $this->cacheWriter->store('templateCache',array());
  46. $this->cacheTemplates = array();
  47. }
  48. $cacheObj->store('templateCacheArray_version_'.$cacheObj->getCacheVersion('site_version'),array());
  49. }
  50. }
  51. /**
  52. * Set a template variable.
  53. */
  54. function set($name, $value) {
  55. $this->vars[$name] = $value;
  56. }
  57. /**
  58. * Set a template variables from array
  59. * */
  60. function setArray($array){
  61. $this->vars = array_merge($this->vars,$array);
  62. }
  63. /**
  64. * Set's template file
  65. * */
  66. function setFile($file)
  67. {
  68. $cfg = erConfigClassLhConfig::getInstance();
  69. $this->file = $file;
  70. }
  71. public static function strip_html($data)
  72. {
  73. $dataLines = explode("\n",$data);
  74. $return = "";
  75. foreach ($dataLines as $line)
  76. {
  77. if (($lineOutput = trim($line)) != ''){
  78. $return.= $lineOutput;
  79. if (preg_match('/(\/\/|<!--)/',$lineOutput)) // In case comment is at the end somewhere, /gallery/publicupload/
  80. $return.= "\n";
  81. }
  82. }
  83. // Spaces have to be adjusted using CSS
  84. $return=str_replace("> <","><",$return);
  85. // Need space some templates
  86. $return=str_replace('<?php','<?php ',$return);
  87. return $return;
  88. }
  89. /**
  90. * Open, parse, and return the template file.
  91. *
  92. * @param $file string the template file name
  93. */
  94. function fetch($fileTemplate = null) {
  95. $instance = erLhcoreClassSystem::instance();
  96. if(!$fileTemplate) { $fileTemplate = $this->file; }
  97. if ($this->cacheEnabled == true && key_exists(md5($fileTemplate.$instance->WWWDirLang),$this->cacheTemplates))
  98. {
  99. try {
  100. return $this->fetchExecute($this->cacheTemplates[md5($fileTemplate.$instance->WWWDirLang)]);
  101. } catch (Exception $e) {
  102. // Do nothing
  103. }
  104. }
  105. $cfg = erConfigClassLhConfig::getInstance();
  106. $file = erLhcoreClassDesign::designtpl($fileTemplate);
  107. if ($this->templatecompile == true)
  108. {
  109. $contentFile = php_strip_whitespace($file);
  110. //Compile templates inclusions first level.
  111. $Matches = array();
  112. preg_match_all('/<\?php(.*?)include_once\(erLhcoreClassDesign::designtpl\(\'([a-zA-Z0-9-\.-\/\_]+)\'\)\)(.*?)\?\>/i',$contentFile,$Matches);
  113. foreach ($Matches[2] as $key => $Match)
  114. {
  115. $contentFile = str_replace($Matches[0][$key],php_strip_whitespace(erLhcoreClassDesign::designtpl($Match)),$contentFile);
  116. }
  117. //Compile templates inclusions first level.
  118. $Matches = array();
  119. preg_match_all('/<\?php(.*?)include\(erLhcoreClassDesign::designtpl\(\'([a-zA-Z0-9-\.-\/\_]+)\'\)\)(.*?)\?\>/i',$contentFile,$Matches);
  120. foreach ($Matches[2] as $key => $Match)
  121. {
  122. $contentFile = str_replace($Matches[0][$key],php_strip_whitespace(erLhcoreClassDesign::designtpl($Match)),$contentFile);
  123. }
  124. //Compile image css paths. Etc..
  125. $Matches = array();
  126. preg_match_all('/<\?=erLhcoreClassDesign::design\(\'([a-zA-Z0-9-\.-\/\_]+)\'\)(.*?)\?\>/i',$contentFile,$Matches);
  127. foreach ($Matches[1] as $key => $Match)
  128. {
  129. $contentFile = str_replace($Matches[0][$key],erLhcoreClassDesign::design($Match),$contentFile);
  130. }
  131. //Compile translations, pure translations
  132. $Matches = array();
  133. preg_match_all('/<\?=erTranslationClassLhTranslation::getInstance\(\)->getTranslation\(\'(.*?)\',\'(.*?)\'\)(.*?)\?\>/i',$contentFile,$Matches);
  134. foreach ($Matches[1] as $key => $TranslateContent)
  135. {
  136. $contentFile = str_replace($Matches[0][$key],erTranslationClassLhTranslation::getInstance()->getTranslation($TranslateContent,$Matches[2][$key]),$contentFile);
  137. }
  138. //Translations used in logical conditions
  139. $Matches = array();
  140. preg_match_all('/erTranslationClassLhTranslation::getInstance\(\)->getTranslation\(\'(.*?)\',\'(.*?)\'\)/i',$contentFile,$Matches);
  141. foreach ($Matches[1] as $key => $TranslateContent)
  142. {
  143. $contentFile = str_replace($Matches[0][$key],'\''.erTranslationClassLhTranslation::getInstance()->getTranslation($TranslateContent,$Matches[2][$key]).'\'',$contentFile);
  144. }
  145. // Compile url addresses
  146. $Matches = array();
  147. preg_match_all('/<\?=erLhcoreClassDesign::baseurl\((.*?)\)(.*?)\?\>/i',$contentFile,$Matches);
  148. foreach ($Matches[1] as $key => $UrlAddress)
  149. {
  150. $contentFile = str_replace($Matches[0][$key],erLhcoreClassDesign::baseurl(trim($UrlAddress,'\'')),$contentFile);
  151. }
  152. // Compile url direct addresses
  153. $Matches = array();
  154. preg_match_all('/<\?=erLhcoreClassDesign::baseurldirect\((.*?)\)(.*?)\?\>/i',$contentFile,$Matches);
  155. foreach ($Matches[1] as $key => $UrlAddress)
  156. {
  157. $contentFile = str_replace($Matches[0][$key],erLhcoreClassDesign::baseurldirect(trim($UrlAddress,'\'')),$contentFile);
  158. }
  159. // Compile css url addresses
  160. $Matches = array();
  161. preg_match_all('/<\?=erLhcoreClassDesign::designCSS\((.*?)\)(.*?)\?\>/i',$contentFile,$Matches);
  162. foreach ($Matches[1] as $key => $UrlAddress)
  163. {
  164. $contentFile = str_replace($Matches[0][$key],erLhcoreClassDesign::designCSS(trim($UrlAddress,'\'')),$contentFile);
  165. }
  166. // Compile css url addresses
  167. $Matches = array();
  168. preg_match_all('/<\?=erLhcoreClassDesign::designJS\((.*?)\)(.*?)\?\>/i',$contentFile,$Matches);
  169. foreach ($Matches[1] as $key => $UrlAddress)
  170. {
  171. $contentFile = str_replace($Matches[0][$key],erLhcoreClassDesign::designJS(trim($UrlAddress,'\'')),$contentFile);
  172. }
  173. // Compile url addresses in logical operations
  174. $Matches = array();
  175. preg_match_all('/erLhcoreClassDesign::baseurl\((.*?)\)/i',$contentFile,$Matches);
  176. foreach ($Matches[1] as $key => $UrlAddress)
  177. {
  178. $contentFile = str_replace($Matches[0][$key],'\''.erLhcoreClassDesign::baseurl(trim($UrlAddress,'\'')).'\'',$contentFile);
  179. }
  180. // Compile config settings, direct output
  181. $Matches = array();
  182. preg_match_all('/<\?=erConfigClassLhConfig::getInstance\(\)->getSetting\((\s?)\'([a-zA-Z0-9-\.-\/\_]+)\'(\s?),(\s?)\'([a-zA-Z0-9-\.-\/\_]+)\'(\s?)\)(.*?)\?\>/i',$contentFile,$Matches);
  183. foreach ($Matches[1] as $key => $UrlAddress)
  184. {
  185. $valueConfig = erConfigClassLhConfig::getInstance()->getSetting($Matches[2][$key],$Matches[5][$key]);
  186. $valueReplace = '';
  187. if (is_bool($valueConfig)){
  188. $valueReplace = $valueConfig == false ? 'false' : 'true';
  189. } elseif (is_integer($valueConfig)) {
  190. $valueReplace = $valueConfig;
  191. } elseif (is_array($valueConfig)) {
  192. $valueReplace = var_export($valueConfig,true);
  193. } else {
  194. $valueReplace = $valueConfig;
  195. }
  196. $contentFile = str_replace($Matches[0][$key],$valueReplace,$contentFile);
  197. }
  198. // Compile config settings
  199. $Matches = array();
  200. preg_match_all('/erConfigClassLhConfig::getInstance\(\)->getSetting\((\s?)\'([a-zA-Z0-9-\.-\/\_]+)\'(\s?),(\s?)\'([a-zA-Z0-9-\.-\/\_]+)\'(\s?)\)/i',$contentFile,$Matches);
  201. foreach ($Matches[1] as $key => $UrlAddress)
  202. {
  203. $valueConfig = erConfigClassLhConfig::getInstance()->getSetting($Matches[2][$key],$Matches[5][$key]);
  204. $valueReplace = '';
  205. if (is_bool($valueConfig)){
  206. $valueReplace = $valueConfig == false ? 'false' : 'true';
  207. } elseif (is_integer($valueConfig)) {
  208. $valueReplace = $valueConfig;
  209. } elseif (is_array($valueConfig)) {
  210. $valueReplace = var_export($valueConfig,true);
  211. } else {
  212. $valueReplace = '\''.$valueConfig.'\'';
  213. }
  214. $contentFile = str_replace($Matches[0][$key],$valueReplace,$contentFile);
  215. }
  216. // Compile override config settings, used in title, description override
  217. $Matches = array();
  218. preg_match_all('/<\?=erConfigClassLhConfig::getInstance\(\)->getOverrideValue\((\s?)\'([a-zA-Z0-9-\.-\/\_]+)\'(\s?),(\s?)\'([a-zA-Z0-9-\.-\/\_]+)\'(\s?)\)(.*?)\?\>/i',$contentFile,$Matches);
  219. foreach ($Matches[1] as $key => $UrlAddress)
  220. {
  221. $valueConfig = erConfigClassLhConfig::getInstance()->getOverrideValue($Matches[2][$key],$Matches[5][$key]);
  222. $valueReplace = '';
  223. if (is_bool($valueConfig)){
  224. $valueReplace = $valueConfig == false ? 'false' : 'true';
  225. } elseif (is_integer($valueConfig)) {
  226. $valueReplace = $valueConfig;
  227. } elseif (is_array($valueConfig)) {
  228. $valueReplace = var_export($valueConfig,true);
  229. } else {
  230. $valueReplace = $valueConfig;
  231. }
  232. $contentFile = str_replace($Matches[0][$key],$valueReplace,$contentFile);
  233. }
  234. // Compile content language
  235. $contentFile = str_replace('<?=erLhcoreClassSystem::instance()->ContentLanguage?>',erLhcoreClassSystem::instance()->ContentLanguage,$contentFile);
  236. // Compile siteaccess
  237. $contentFile = str_replace('erLhcoreClassSystem::instance()->SiteAccess','\''.erLhcoreClassSystem::instance()->SiteAccess.'\'',$contentFile);
  238. $sys = erLhcoreClassSystem::instance()->SiteDir;
  239. $file = $sys . 'cache/compiledtemplates/'.md5($file.$instance->WWWDirLang).'.php';
  240. file_put_contents($file,erLhcoreClassTemplate::strip_html($contentFile));
  241. $this->cacheTemplates[md5($fileTemplate.$instance->WWWDirLang)] = $file;
  242. $this->storeCache();
  243. }
  244. return $this->fetchExecute($file);
  245. }
  246. function storeCache()
  247. {
  248. if (is_null($this->cacheWriter)){
  249. $sys = erLhcoreClassSystem::instance()->SiteDir;
  250. $this->cacheWriter = new ezcCacheStorageFileArray($sys . 'cache/cacheconfig/');
  251. }
  252. $this->cacheWriter->store('templateCache',$this->cacheTemplates);
  253. $cacheObj = CSCacheAPC::getMem();
  254. $cacheObj->store('templateCacheArray_version_'.$cacheObj->getCacheVersion('site_version'),$this->cacheTemplates);
  255. }
  256. function fetchExecute($file)
  257. {
  258. @extract($this->vars,EXTR_REFS); // Extract the vars to local namespace
  259. ob_start(); // Start output buffering
  260. $result = include($file); // Include the file
  261. if ($result === false) { // Make sure file was included succesfuly
  262. throw new Exception("File inclusion failed"); // Throw exception if failed, so tpl compiler will recompile template
  263. }
  264. $contents = ob_get_contents(); // Get the contents of the buffer
  265. ob_end_clean(); // End buffering and discard
  266. return $contents;
  267. }
  268. }
  269. ?>