/includes/template_class.php

https://github.com/sseshachala/Open-Web-Analytics · PHP · 201 lines · 80 code · 25 blank · 96 comment · 13 complexity · ac3b9d7f13ceee6d85950dfcce9996f8 MD5 · raw file

  1. <?php
  2. //
  3. // Open Web Analytics - An Open Source Web Analytics Framework
  4. //
  5. // Copyright 2006 Peter Adams. All rights reserved.
  6. //
  7. // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // $Id$
  16. //
  17. /**
  18. * Template
  19. *
  20. * @author Peter Adams <peter@openwebanalytics.com>
  21. * @copyright Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
  23. * @category owa
  24. * @package owa
  25. * @version $Revision$
  26. * @since owa 1.0.0
  27. */
  28. class Template {
  29. /**
  30. * Template files directory
  31. *
  32. * @var string
  33. */
  34. var $template_dir;
  35. /**
  36. * Template Variables
  37. *
  38. * @var array
  39. */
  40. var $vars = array();
  41. /**
  42. * Template file
  43. *
  44. * @var string
  45. */
  46. var $file;
  47. /**
  48. * Constructor
  49. *
  50. * @access public
  51. */
  52. function Template() {
  53. return;
  54. }
  55. /**
  56. * Set the template file
  57. *
  58. * @param string $file
  59. */
  60. function set_template($file = null) {
  61. $this->file = $this->template_dir.$file;
  62. return;
  63. }
  64. /**
  65. * Set a template variable
  66. *
  67. * @param string $name
  68. * @param unknown_value $value
  69. * @access public
  70. */
  71. function set($name, $value) {
  72. if (is_object($value)) {
  73. $class = 'Template';
  74. if ($value instanceof $this) {
  75. $value = $value->fetch();
  76. }
  77. }
  78. $this->vars[$name] = $value;
  79. return;
  80. }
  81. /**
  82. * Open, parse, and return the template file.
  83. *
  84. * @param string $file
  85. * @return string $contents
  86. * @access public
  87. */
  88. function fetch($file = null) {
  89. if(!$file):
  90. $file = $this->file;
  91. else:
  92. $file = $this->template_dir.$file;
  93. endif;
  94. extract($this->vars); // Extract the vars to local namespace
  95. ob_start(); // Start output buffering
  96. include($file); // Include the file
  97. $contents = ob_get_contents(); // Get the contents of the buffer
  98. ob_end_clean(); // End buffering and discard
  99. return $contents; // Return the contents
  100. }
  101. }
  102. /**
  103. * An extension to Template that provides automatic caching of
  104. * template contents.
  105. */
  106. class CachedTemplate extends Template {
  107. var $cache_id;
  108. var $expire;
  109. var $cached;
  110. /**
  111. * Constructor.
  112. *
  113. * @param $cache_id string unique cache identifier
  114. * @param $expire int number of seconds the cache will live
  115. */
  116. function CachedTemplate($cache_id = null, $expire = 900) {
  117. $this->Template();
  118. $this->cache_id = $cache_id ? 'cache/' . md5($cache_id) : $cache_id;
  119. $this->expire = $expire;
  120. }
  121. /**
  122. * Test to see whether the currently loaded cache_id has a valid
  123. * corrosponding cache file.
  124. */
  125. function is_cached() {
  126. if($this->cached) return true;
  127. // Passed a cache_id?
  128. if(!$this->cache_id) return false;
  129. // Cache file exists?
  130. if(!file_exists($this->cache_id)) return false;
  131. // Can get the time of the file?
  132. if(!($mtime = filemtime($this->cache_id))) return false;
  133. // Cache expired?
  134. if(($mtime + $this->expire) < time()) {
  135. @unlink($this->cache_id);
  136. return false;
  137. }
  138. else {
  139. /**
  140. * Cache the results of this is_cached() call. Why? So
  141. * we don't have to double the overhead for each template.
  142. * If we didn't cache, it would be hitting the file system
  143. * twice as much (file_exists() & filemtime() [twice each]).
  144. */
  145. $this->cached = true;
  146. return true;
  147. }
  148. }
  149. /**
  150. * This function returns a cached copy of a template (if it exists),
  151. * otherwise, it parses it as normal and caches the content.
  152. *
  153. * @param $file string the template file
  154. */
  155. function fetch_cache($file) {
  156. if($this->is_cached()) {
  157. $fp = @fopen($this->cache_id, 'r');
  158. $contents = fread($fp, filesize($this->cache_id));
  159. fclose($fp);
  160. return $contents;
  161. }
  162. else {
  163. $contents = $this->fetch($file);
  164. // Write the cache
  165. if($fp = @fopen($this->cache_id, 'w')) {
  166. fwrite($fp, $contents);
  167. fclose($fp);
  168. }
  169. else {
  170. die('Unable to write cache.');
  171. }
  172. return $contents;
  173. }
  174. }
  175. }
  176. ?>