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

https://bitbucket.org/eternaware/joomus · PHP · 196 lines · 93 code · 23 blank · 80 comment · 22 complexity · 0fe0e3a2e37a689d4de7eb9cb1b5a20c MD5 · raw file

  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. // If an id is not given, generate it from the request
  48. if ($id == false)
  49. {
  50. $id = $this->_makeId();
  51. }
  52. // If the etag matches the page id ... set a no change header and exit : utilize browser cache
  53. if (!headers_sent() && isset($_SERVER['HTTP_IF_NONE_MATCH']))
  54. {
  55. $etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
  56. if ($etag == $id)
  57. {
  58. $browserCache = isset($this->options['browsercache']) ? $this->options['browsercache'] : false;
  59. if ($browserCache)
  60. {
  61. $this->_noChange();
  62. }
  63. }
  64. }
  65. // We got a cache hit... set the etag header and echo the page data
  66. $data = $this->cache->get($id, $group);
  67. $this->_locktest = new stdClass;
  68. $this->_locktest->locked = null;
  69. $this->_locktest->locklooped = null;
  70. if ($data === false)
  71. {
  72. $this->_locktest = $this->cache->lock($id, $group);
  73. if ($this->_locktest->locked == true && $this->_locktest->locklooped == true)
  74. {
  75. $data = $this->cache->get($id, $group);
  76. }
  77. }
  78. if ($data !== false)
  79. {
  80. $data = unserialize(trim($data));
  81. if ($wrkarounds === true)
  82. {
  83. $data = JCache::getWorkarounds($data);
  84. }
  85. $this->_setEtag($id);
  86. if ($this->_locktest->locked == true)
  87. {
  88. $this->cache->unlock($id, $group);
  89. }
  90. return $data;
  91. }
  92. // Set id and group placeholders
  93. $this->_id = $id;
  94. $this->_group = $group;
  95. return false;
  96. }
  97. /**
  98. * Stop the cache buffer and store the cached data
  99. *
  100. * @param boolean $wrkarounds True to use wrkarounds
  101. *
  102. * @return boolean True if cache stored
  103. *
  104. * @since 11.1
  105. */
  106. public function store($wrkarounds = true)
  107. {
  108. // Get page data from JResponse body
  109. $data = JResponse::getBody();
  110. // Get id and group and reset the placeholders
  111. $id = $this->_id;
  112. $group = $this->_group;
  113. $this->_id = null;
  114. $this->_group = null;
  115. // Only attempt to store if page data exists
  116. if ($data)
  117. {
  118. $data = $wrkarounds == false ? $data : JCache::setWorkarounds($data);
  119. if ($this->_locktest->locked == false)
  120. {
  121. $this->_locktest = $this->cache->lock($id, $group);
  122. }
  123. $sucess = $this->cache->store(serialize($data), $id, $group);
  124. if ($this->_locktest->locked == true)
  125. {
  126. $this->cache->unlock($id, $group);
  127. }
  128. return $sucess;
  129. }
  130. return false;
  131. }
  132. /**
  133. * Generate a page cache id
  134. *
  135. * @return string MD5 Hash : page cache id
  136. *
  137. * @since 11.1
  138. * @todo Discuss whether this should be coupled to a data hash or a request
  139. * hash ... perhaps hashed with a serialized request
  140. */
  141. protected function _makeId()
  142. {
  143. return JCache::makeId();
  144. }
  145. /**
  146. * There is no change in page data so send an
  147. * unmodified header and die gracefully
  148. *
  149. * @return void
  150. *
  151. * @since 11.1
  152. */
  153. protected function _noChange()
  154. {
  155. $app = JFactory::getApplication();
  156. // Send not modified header and exit gracefully
  157. header('HTTP/1.x 304 Not Modified', true);
  158. $app->close();
  159. }
  160. /**
  161. * Set the ETag header in the response
  162. *
  163. * @param string $etag The entity tag (etag) to set
  164. *
  165. * @return void
  166. *
  167. * @since 11.1
  168. */
  169. protected function _setEtag($etag)
  170. {
  171. JResponse::setHeader('ETag', $etag, true);
  172. }
  173. }