PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/mambots/system/pc_includes/template.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 166 lines | 88 code | 27 blank | 51 comment | 19 complexity | 40bbd82e95fd0d86f809040d970ab6c9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. (defined('_VALID_MOS') or defined('_JEXEC')) or die('Direct Access to this location is not allowed.');
  3. // Include our custom cmslib
  4. if(!defined('AZ_CACHE_PATH'))
  5. define('AZ_CACHE_PATH',(dirname(dirname(dirname(dirname(__FILE__))))). '/components/libraries/cmslib/cache');
  6. /**
  7. * Original code by Brian Lozier
  8. * http://www.massassi.com/php/articles/template_engines/
  9. * Modified by Azrul (www.azrul.com) to run on Joomla
  10. */
  11. class AzrulJXTemplate {
  12. var $vars; /// Holds all the template variables
  13. /**
  14. * Constructor
  15. *
  16. * @param $file string the file name you want to load
  17. */
  18. function AzrulJXTemplate($file = null) {
  19. $this->file = $file;
  20. @ini_set('short_open_tag', 'On');
  21. }
  22. /**
  23. * Set a template variable.
  24. */
  25. function set($name, $value) {
  26. $this->vars[$name] = is_object($value) ? $value->fetch() : $value;
  27. }
  28. /**
  29. * Open, parse, and return the template file.
  30. *
  31. * @param $file string the template file name
  32. */
  33. function fetch($file = null) {
  34. if(!$file) $file = $this->file;
  35. if($this->vars)
  36. extract($this->vars); // Extract the vars to local namespace
  37. ob_start(); // Start output buffering
  38. include($file); // Include the file
  39. $contents = ob_get_contents(); // Get the contents of the buffer
  40. ob_end_clean(); // End buffering and discard
  41. return $contents; // Return the contents
  42. }
  43. function object_to_array($obj) {
  44. $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  45. $arr = array();
  46. foreach ($_arr as $key => $val) {
  47. $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val) : $val;
  48. $arr[$key] = $val;
  49. }
  50. return $arr;
  51. }
  52. }
  53. class AzrulJXCachedTemplate extends AzrulJXTemplate {
  54. var $cache_id;
  55. var $expire;
  56. var $cached;
  57. var $file;
  58. /**
  59. * Constructor.
  60. *
  61. * @param $cache_id string unique cache identifier
  62. * @param $expire int number of seconds the cache will live
  63. */
  64. function AzrulJXCachedTemplate($cache_id = "", $cache_timeout = 10000) {
  65. $this->AzrulJXTemplate();
  66. $this->cache_id = AZ_CACHE_PATH . "/cache__". md5($cache_id);
  67. $this->cached = false;
  68. $this->expire = $cache_timeout;
  69. }
  70. /**
  71. * Test to see whether the currently loaded cache_id has a valid
  72. * corrosponding cache file.
  73. */
  74. function is_cached() {
  75. //return false;
  76. if($this->cached) return true;
  77. // Passed a cache_id?
  78. if(!$this->cache_id) return false;
  79. // Cache file exists?
  80. if(!file_exists($this->cache_id)) return false;
  81. // Can get the time of the file?
  82. if(!($mtime = filemtime($this->cache_id))) return false;
  83. // Cache expired?
  84. // Implemented as 'never-expires' cache, so, the data need to change
  85. // for the cache to be modified
  86. if(($mtime + $this->expire) < time()) {
  87. @unlink($this->cache_id);
  88. return false;
  89. }
  90. else {
  91. /**
  92. * Cache the results of this is_cached() call. Why? So
  93. * we don't have to double the overhead for each template.
  94. * If we didn't cache, it would be hitting the file system
  95. * twice as much (file_exists() & filemtime() [twice each]).
  96. */
  97. $this->cached = true;
  98. return true;
  99. }
  100. }
  101. /**
  102. * This function returns a cached copy of a template (if it exists),
  103. * otherwise, it parses it as normal and caches the content.
  104. *
  105. * @param $file string the template file
  106. */
  107. function fetch_cache($file, $processFunc = null) {
  108. $contents = "";
  109. if($this->is_cached()) {
  110. $fp = @fopen($this->cache_id, 'r');
  111. if($fp){
  112. $filesize = filesize($this->cache_id);
  113. if($filesize > 0){
  114. $contents = fread($fp, $filesize);
  115. }
  116. fclose($fp);
  117. } else {
  118. $contents = $this->fetch($file);
  119. }
  120. }
  121. else {
  122. $contents = $this->fetch($file);
  123. // Check if caller wants to process contents with another function
  124. if($processFunc)
  125. $contents = $processFunc($contents);
  126. if(!empty($contents)){
  127. // Write the cache, only if there is some data
  128. if($fp = @fopen($this->cache_id, 'w')) {
  129. fwrite($fp, $contents);
  130. fclose($fp);
  131. }
  132. else {
  133. //die('Unable to write cache.');
  134. }
  135. }
  136. }
  137. return $contents;
  138. }
  139. }