/www/application/libraries/mabilis/Mabilis.class.php

https://github.com/kelios/imshop · PHP · 139 lines · 94 code · 27 blank · 18 comment · 20 complexity · a07eb891401d29610c7074144db4978e MD5 · raw file

  1. <?php
  2. /***************************************************
  3. * Image CMS Template Engine (Mabilis TPL)
  4. *
  5. * Simple template engine for Image CMS based on regular expressions search and replace.
  6. *
  7. * author: dev@imagecms.net
  8. * version: 0.3 PHP5
  9. ***************************************************/
  10. class Mabilis {
  11. private $compiler = NULL;
  12. private $config = NULL;
  13. public function __construct(&$config = array())
  14. {
  15. $this->load_config($config);
  16. }
  17. /**
  18. * Display or fetch template file
  19. */
  20. public function view($file, $data = array(), $return = FALSE)
  21. {
  22. // Delete double .tpl.tpl
  23. $file = preg_replace('/.tpl.tpl/', '.tpl', $file);
  24. if (preg_match('/file:/', $file, $_Matches))
  25. {
  26. $file_dir = preg_replace('/\/\//','/', $file);
  27. $file_dir = preg_replace('/file\:/','', $file_dir);
  28. }else{
  29. $file_dir = $this->config->tpl_path . $file;
  30. }
  31. if (preg_match('/application\/modules/', $file_dir, $mm))
  32. {
  33. $newFile = explode('application/modules', $file_dir);
  34. $new_file_dir = $this->config->tpl_path. 'modules' . $newFile[1];
  35. if (file_exists($new_file_dir))
  36. {
  37. $file_dir = $new_file_dir;
  38. }
  39. }
  40. $compiled_file = $this->config->compile_path . md5($file_dir) . $this->config->compiled_ext;
  41. if ( ! file_exists( $compiled_file ) OR $this->config->force_compile == TRUE )
  42. {
  43. // Compile file
  44. $this->load_compiler();
  45. $this->compiler->compile($file_dir);
  46. }
  47. extract($data);
  48. ob_start();
  49. if (file_exists($compiled_file))
  50. {
  51. include ($compiled_file);
  52. }else{
  53. print '<p class="error">Error: '.$compiled_file. ' does not exists!</p>';
  54. }
  55. // Time to live expried
  56. if ($mabilis_ttl <= time())
  57. {
  58. @unlink( $compiled_file );
  59. }
  60. if ($this->config->use_filemtime == TRUE AND $mabilis_last_modified != @filemtime($file_dir))
  61. {
  62. @unlink( $compiled_file );
  63. }
  64. if ($return == TRUE)
  65. {
  66. $buffer = ob_get_contents();
  67. ob_end_clean();
  68. return $buffer;
  69. }
  70. ob_end_flush();
  71. }
  72. public function load_config($config = array())
  73. {
  74. if (extension_loaded('zlib') AND $config['compress_output'] == TRUE)
  75. {
  76. if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
  77. {
  78. ob_start('ob_gzhandler');
  79. }
  80. }
  81. if ($this->config == NULL)
  82. {
  83. include 'Config.class.php';
  84. $this->config = new Mabilis_Config($config);
  85. }
  86. return TRUE;
  87. }
  88. public function set_config_value($param, $value)
  89. {
  90. $this->config->$param = $value;
  91. }
  92. public function get_config_value($param)
  93. {
  94. if (isset($this->config->$param))
  95. {
  96. return $this->config->$param;
  97. }
  98. }
  99. /**
  100. * Load compiler class if not loaded yet
  101. */
  102. public function load_compiler()
  103. {
  104. if ($this->compiler == NULL)
  105. {
  106. include 'Mabilis.compiler.php';
  107. $this->compiler = new Mabilis_Compiler($this->config);
  108. }
  109. return TRUE;
  110. }
  111. }
  112. /* End of Mabilis.class.php */