PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/cache/controller/page.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 199 lines | 94 code | 24 blank | 81 comment | 22 complexity | 09648f790ad4bf9ad6eb28cf58803820 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Cache
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Joomla! Cache page type object
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Cache
  15. * @since 11.1
  16. */
  17. class JCacheControllerPage extends JCacheController
  18. {
  19. /**
  20. * @var integer ID property for the cache page object.
  21. * @since 11.1
  22. */
  23. protected $_id;
  24. /**
  25. * @var string Cache group
  26. * @since 11.1
  27. */
  28. protected $_group;
  29. /**
  30. * @var object Cache lock test
  31. * @since 11.1
  32. */
  33. protected $_locktest = null;
  34. /**
  35. * Get the cached page data
  36. *
  37. * @param string $id The cache data id
  38. * @param string $group The cache data group
  39. * @param boolean $wrkarounds True to use wrkarounds
  40. *
  41. * @return boolean True if the cache is hit (false else)
  42. *
  43. * @since 11.1
  44. */
  45. public function get($id = false, $group = 'page', $wrkarounds = true)
  46. {
  47. // Initialise variables.
  48. $data = false;
  49. // If an id is not given, generate it from the request
  50. if ($id == false)
  51. {
  52. $id = $this->_makeId();
  53. }
  54. // If the etag matches the page id ... set a no change header and exit : utilize browser cache
  55. if (!headers_sent() && isset($_SERVER['HTTP_IF_NONE_MATCH']))
  56. {
  57. $etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
  58. if ($etag == $id)
  59. {
  60. $browserCache = isset($this->options['browsercache']) ? $this->options['browsercache'] : false;
  61. if ($browserCache)
  62. {
  63. $this->_noChange();
  64. }
  65. }
  66. }
  67. // We got a cache hit... set the etag header and echo the page data
  68. $data = $this->cache->get($id, $group);
  69. $this->_locktest = new stdClass;
  70. $this->_locktest->locked = null;
  71. $this->_locktest->locklooped = null;
  72. if ($data === false)
  73. {
  74. $this->_locktest = $this->cache->lock($id, $group);
  75. if ($this->_locktest->locked == true && $this->_locktest->locklooped == true)
  76. {
  77. $data = $this->cache->get($id, $group);
  78. }
  79. }
  80. if ($data !== false)
  81. {
  82. $data = unserialize(trim($data));
  83. if ($wrkarounds === true)
  84. {
  85. $data = JCache::getWorkarounds($data);
  86. }
  87. $this->_setEtag($id);
  88. if ($this->_locktest->locked == true)
  89. {
  90. $this->cache->unlock($id, $group);
  91. }
  92. return $data;
  93. }
  94. // Set id and group placeholders
  95. $this->_id = $id;
  96. $this->_group = $group;
  97. return false;
  98. }
  99. /**
  100. * Stop the cache buffer and store the cached data
  101. *
  102. * @param boolean $wrkarounds True to use wrkarounds
  103. *
  104. * @return boolean True if cache stored
  105. *
  106. * @since 11.1
  107. */
  108. public function store($wrkarounds = true)
  109. {
  110. // Get page data from JResponse body
  111. $data = JResponse::getBody();
  112. // Get id and group and reset the placeholders
  113. $id = $this->_id;
  114. $group = $this->_group;
  115. $this->_id = null;
  116. $this->_group = null;
  117. // Only attempt to store if page data exists
  118. if ($data)
  119. {
  120. $data = $wrkarounds == false ? $data : JCache::setWorkarounds($data);
  121. if ($this->_locktest->locked == false)
  122. {
  123. $this->_locktest = $this->cache->lock($id, $group);
  124. }
  125. $sucess = $this->cache->store(serialize($data), $id, $group);
  126. if ($this->_locktest->locked == true)
  127. {
  128. $this->cache->unlock($id, $group);
  129. }
  130. return $sucess;
  131. }
  132. return false;
  133. }
  134. /**
  135. * Generate a page cache id
  136. *
  137. * @return string MD5 Hash : page cache id
  138. *
  139. * @since 11.1
  140. * @todo Discuss whether this should be coupled to a data hash or a request
  141. * hash ... perhaps hashed with a serialized request
  142. */
  143. protected function _makeId()
  144. {
  145. return JCache::makeId();
  146. }
  147. /**
  148. * There is no change in page data so send an
  149. * unmodified header and die gracefully
  150. *
  151. * @return void
  152. *
  153. * @since 11.1
  154. */
  155. protected function _noChange()
  156. {
  157. $app = JFactory::getApplication();
  158. // Send not modified header and exit gracefully
  159. header('HTTP/1.x 304 Not Modified', true);
  160. $app->close();
  161. }
  162. /**
  163. * Set the ETag header in the response
  164. *
  165. * @param string $etag The entity tag (etag) to set
  166. *
  167. * @return void
  168. *
  169. * @since 11.1
  170. */
  171. protected function _setEtag($etag)
  172. {
  173. JResponse::setHeader('ETag', $etag, true);
  174. }
  175. }