PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_virtuemart/classes/template.class.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 254 lines | 115 code | 24 blank | 115 comment | 20 complexity | 581d0d86f7d1ae0d9a7d4544b3accf4a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: template.class.php 1391 2008-04-28 19:47:14Z soeren_nb $
  6. * @package VirtueMart
  7. * @subpackage core
  8. * @copyright Copyright (c) 2003 Brian E. Lozier (brian@massassi.net)
  9. * @copyright Copyright (C) 2007 soeren - All rights reserved.
  10. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  11. * VirtueMart is free software. This version may have been modified pursuant
  12. * to the GNU General Public License, and as distributed it includes or
  13. * is derivative of works licensed under the GNU General Public License or
  14. * other free or open source software licenses.
  15. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  16. *
  17. * http://virtuemart.net
  18. *
  19. * set_vars() method contributed by Ricardo Garcia (Thanks!)
  20. *
  21. * Permission is hereby granted, free of charge, to any person obtaining a copy
  22. * of this software and associated documentation files (the "Software"), to
  23. * deal in the Software without restriction, including without limitation the
  24. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  25. * sell copies of the Software, and to permit persons to whom the Software is
  26. * furnished to do so, subject to the following conditions:
  27. *
  28. * The above copyright notice and this permission notice shall be included in
  29. * all copies or substantial portions of the Software.
  30. */
  31. require_once( CLASSPATH . 'parameters.class.php');
  32. class vmTemplate {
  33. var $vars; /// Holds all the template variables
  34. var $path; /// Path to the templates
  35. /**
  36. * Stores the theme configuration
  37. *
  38. * @var $config
  39. */
  40. var $config;
  41. var $cache_filename;
  42. var $expire;
  43. var $cached = false;
  44. /**
  45. * Constructor.
  46. *
  47. * @param string $path path to template files
  48. * @param string $cache_id unique cache identifier
  49. * @param int $expire number of seconds the cache will live
  50. *
  51. * @return void
  52. */
  53. function vmTemplate($path='', $expire = 0 ) {
  54. global $mosConfig_live_site, $mosConfig_cachepath, $mosConfig_cachetime;
  55. $this->path = empty($path) ? VM_THEMEPATH.'templates/' : $path;
  56. $this->default_path = $mosConfig_live_site.'/components/'.VM_COMPONENT_NAME.'/themes/default/templates/';
  57. $globalsArray = vmGetGlobalsArray();
  58. foreach( $globalsArray as $global ) {
  59. global $$global;
  60. $this->vars[$global] = $GLOBALS[$global];
  61. }
  62. $this->cache_filename = $mosConfig_cachepath.'/' . $GLOBALS['cache_id'];
  63. $this->expire = $expire == 0 ? $mosConfig_cachetime : $expire;
  64. // the theme configuration needs to be available to the templates! (so you can use $this->get_cfg('showManufacturerLink') for example )
  65. if( empty( $GLOBALS['vmThemeConfig'] ) || !empty( $_REQUEST['vmThemeConfig'])) {
  66. $GLOBALS['vmThemeConfig'] =& new vmParameters( @file_get_contents(VM_THEMEPATH.'theme.config.php'), VM_THEMEPATH.'theme.xml', 'theme');
  67. }
  68. $this->config =& $GLOBALS['vmThemeConfig'];
  69. }
  70. /**
  71. * Returns a unique Cache ID
  72. * @static
  73. * @return string
  74. */
  75. function getCacheId() {
  76. global $modulename, $pagename, $product_id, $category_id, $manufacturer_id, $auth, $limitstart, $limit;
  77. return 'vm_' . @md5( 'vm_' . @md5( $modulename. $pagename. $product_id. $category_id .$manufacturer_id. $auth["shopper_group_id"]. $limitstart. $limit. @$_REQUEST['orderby']. @$_REQUEST['DescOrderBy'] ). @$_REQUEST['orderby']. @$_REQUEST['DescOrderBy'] );
  78. }
  79. /**
  80. * @static
  81. *
  82. * @return vmTheme
  83. */
  84. function getInstance() {
  85. return new $GLOBALS['VM_THEMECLASS']();
  86. }
  87. /**
  88. * Test to see whether the currently loaded cache_id has a valid
  89. * corresponding cache file.
  90. * @param string the name of the template file (relative path to the theme dir)
  91. * @return array
  92. */
  93. function get_cached( $templateFile ) {
  94. global $mosConfig_cachepath;
  95. // Passed a cache_id?
  96. if(!$GLOBALS['cache_id']) {
  97. $GLOBALS['cache_id'] = $this->getCacheId();
  98. }
  99. $returnArr['cache_file_id'] = md5($templateFile . $GLOBALS['cache_id']);
  100. $returnArr['cache_file_name'] = $mosConfig_cachepath.'/'.$returnArr['cache_file_id'];
  101. // Cache file exists?
  102. if(!@file_exists($returnArr['cache_file_name'])) {
  103. $returnArr['isCached'] = false;
  104. return $returnArr;
  105. }
  106. if( @filesize($returnArr['cache_file_name']) == 0) {
  107. $returnArr['isCached'] = false;
  108. return $returnArr;
  109. }
  110. // Can get the time of the file?
  111. if(!($mtime = filemtime($returnArr['cache_file_name']))) {
  112. $returnArr['isCached'] = false;
  113. return $returnArr;
  114. }
  115. // Cache expired?
  116. if(($mtime + $this->expire) < time()) {
  117. @unlink($returnArr['cache_file_name']);
  118. $returnArr['isCached'] = false;
  119. return $returnArr;
  120. }
  121. $returnArr['isCached'] = true;
  122. return $returnArr;
  123. }
  124. /**
  125. * Set the path to the template files.
  126. *
  127. * @param string $path path to template files
  128. *
  129. * @return void
  130. */
  131. function set_path($path) {
  132. $this->path = $path;
  133. }
  134. /**
  135. * Set a template variable.
  136. *
  137. * @param string $name name of the variable to set
  138. * @param mixed $value the value of the variable
  139. *
  140. * @return void
  141. */
  142. function set($name, $value) {
  143. $this->vars[$name] = $value;
  144. }
  145. /**
  146. * Set a bunch of variables at once using an associative array.
  147. *
  148. * @param array $vars array of vars to set
  149. * @param bool $clear whether to completely overwrite the existing vars
  150. *
  151. * @return void
  152. */
  153. function set_vars($vars, $clear = false) {
  154. if($clear) {
  155. $this->vars = $vars;
  156. }
  157. else {
  158. if(is_array($vars)) {
  159. $this->vars = array_merge($this->vars, $vars);
  160. }
  161. }
  162. }
  163. /**
  164. * Returns the value of a configuration parameter of this theme
  165. *
  166. * @param string $var
  167. * @param mixed $default
  168. * @return mixed
  169. */
  170. function get_cfg( $var, $default='' ) {
  171. return $this->config->get( $var, $default );
  172. }
  173. /**
  174. * Sets the configuration parameter of this theme
  175. *
  176. * @param string $var
  177. * @param mixed $value
  178. */
  179. function set_cfg( $var, $value ) {
  180. if( is_a( $this->config, 'vmParameters' )) {
  181. $this->config->set( $var, $value );
  182. }
  183. }
  184. /**
  185. * Open, parse, and return the template file.
  186. *
  187. * @param string string the template file name
  188. *
  189. * @return string
  190. */
  191. function fetch($file) {
  192. extract($this->vars); // Extract the vars to local namespace
  193. ob_start(); // Start output buffering
  194. if( file_exists( $this->path . $file ) ) {
  195. include($this->path . $file); // Include the file
  196. } elseif( file_exists( $this->default_path . $file ) ) {
  197. include( $this->default_path . $file );
  198. }
  199. $contents = ob_get_contents(); // Get the contents of the buffer
  200. ob_end_clean(); // End buffering and discard
  201. return $contents; // Return the contents
  202. }
  203. /**
  204. * This function returns a cached copy of a template (if it exists),
  205. * otherwise, it parses it as normal and caches the content.
  206. *
  207. * @param $file string the template file
  208. *
  209. * @return string
  210. */
  211. function fetch_cache($file) {
  212. global $mosConfig_caching;
  213. $cacheFileArr = $this->get_cached( $file );
  214. if( $cacheFileArr['isCached'] !== false ) {
  215. $contents = file_get_contents($cacheFileArr['cache_file_name']);
  216. return $contents;
  217. }
  218. else {
  219. $contents = $this->fetch($file);
  220. if( $mosConfig_caching ) {
  221. // Write the cache
  222. if( !file_put_contents($cacheFileArr['cache_file_name'], $contents ) ) {
  223. $vmLogger->crit('Failed to write to Cache!');
  224. }
  225. }
  226. return $contents;
  227. }
  228. }
  229. }
  230. ?>