PageRenderTime 173ms CodeModel.GetById 50ms app.highlight 66ms RepoModel.GetById 35ms app.codeStats 0ms

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

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