PageRenderTime 36ms CodeModel.GetById 13ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Plug-in/xajax/xajax_core/plugin_layer/xajaxScriptPlugin.inc.php

http://prosporous.googlecode.com/
PHP | 265 lines | 122 code | 45 blank | 98 comment | 32 complexity | f1abf0c7f4ede0af93e0d5e78d59515b MD5 | raw file
  1<?php
  2/*
  3	File: xajaxScriptPlugin.inc.php
  4
  5	Contains the xajaxScriptPlugin class declaration.
  6
  7	Title: xajaxScriptPlugin class
  8
  9	Please see <copyright.inc.php> for a detailed description, copyright
 10	and license information.
 11*/
 12
 13/*
 14	@package xajax
 15	@version $Id: xajaxScriptPlugin.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
 16	@copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
 17	@license http://www.xajaxproject.org/bsd_license.txt BSD License
 18*/
 19
 20/*
 21	Class: xajaxScriptPlugin
 22	
 23	Contains the code that can produce script and style data during deferred script
 24	generation.  This allows the xajax generated javascript and style sheet information
 25	to be loaded via an external file reference instead of inlined into the page
 26	source.
 27*/
 28class xajaxScriptPlugin extends xajaxRequestPlugin
 29{
 30	/*
 31		String: sRequest
 32	*/
 33	var $sRequest;
 34	
 35	/*
 36		String: sHash
 37	*/
 38	var $sHash;
 39	
 40	/*
 41		String: sRequestURI
 42	*/
 43	var $sRequestURI;
 44	
 45	/*
 46		Boolean: bDeferScriptGeneration
 47	*/
 48	var $bDeferScriptGeneration;
 49	
 50	/*
 51		Boolean: bValidateHash
 52	*/
 53	var $bValidateHash;
 54	
 55	/*
 56		Boolean: bWorking
 57	*/
 58	var $bWorking;
 59	
 60	/*
 61		Function: xajaxScriptPlugin
 62		
 63		Construct and initialize the xajax script plugin object.  During
 64		initialization, this plugin will look for hash codes in the
 65		GET data (parameters passed on the request URI) and store them
 66		for later use.
 67	*/
 68	function xajaxScriptPlugin()
 69	{
 70		$this->sRequestURI = '';
 71		$this->bDeferScriptGeneration = false;
 72		$this->bValidateHash = true;
 73		
 74		$this->bWorking = false;
 75
 76		$this->sRequest = '';
 77		$this->sHash = null;
 78		
 79		if (isset($_GET['xjxGenerateJavascript'])) {
 80			$this->sRequest = 'script';
 81			$this->sHash = $_GET['xjxGenerateJavascript'];
 82		}
 83		
 84		if (isset($_GET['xjxGenerateStyle'])) {
 85			$this->sRequest = 'style';
 86			$this->sHash = $_GET['xjxGenerateStyle'];
 87		}
 88	}
 89
 90	/*
 91		Function: configure
 92		
 93		Sets/stores configuration options used by this plugin.  See also:
 94		<xajax::configure>.  This plugin will watch for and store the current
 95		setting for the following configuration options:
 96		
 97		- <requestURI> (string): The requestURI of the current script file.
 98		- <deferScriptGeneration> (boolean): A flag that indicates whether
 99			script deferral is in effect or not.
100		- <deferScriptValidateHash> (boolean): A flag that indicates whether
101			or not the script hash should be validated.
102	*/
103	function configure($sName, $mValue)
104	{
105		if ('requestURI' == $sName) {
106			$this->sRequestURI = $mValue;
107		} else if ('deferScriptGeneration' == $sName) {
108			if (true === $mValue || false === $mValue)
109				$this->bDeferScriptGeneration = $mValue;
110		} else if ('deferScriptValidateHash' == $sName) {
111			if (true === $mValue || false === $mValue)
112				$this->bValidateHash = $mValue;
113		}
114	}
115	
116	/*
117		Function: generateClientScript
118		
119		Called by the <xajaxPluginManager> when the text of the client script
120		(or style) declarations are needed.
121		
122		This function will only output script or style information if the 
123		request URI contained an appropriate hash code and script deferral 
124		is in effect.
125	*/
126	function generateClientScript()
127	{
128		if ($this->bWorking)
129			return;
130		
131		if (true === $this->bDeferScriptGeneration)
132		{
133			$this->bWorking = true;
134			
135			$sQueryBase = '?';
136			if (0 < strpos($this->sRequestURI, '?'))
137				$sQueryBase = '&';
138			
139			$aScripts = $this->_getSections('script');
140			if (0 < count($aScripts))
141			{
142//				echo "<!--" . print_r($aScripts, true) . "-->";
143			
144				$sHash = md5(implode($aScripts));
145				$sQuery = $sQueryBase . "xjxGenerateJavascript=" . $sHash;
146				
147				echo "\n<script type='text/javascript' src='" . $this->sRequestURI . $sQuery . "' charset='UTF-8'></script>\n";
148			}
149			
150			$aStyles = $this->_getSections('style');
151			if (0 < count($aStyles))
152			{
153//				echo "<!--" . print_r($aStyles, true) . "-->";
154			
155				$sHash = md5(implode($aStyles));
156				$sQuery = $sQueryBase . "xjxGenerateStyle=" . $sHash;
157				
158				echo "\n<link href='" . $this->sRequestURI . $sQuery . "' rel='Stylesheet' />\n";
159			}
160			
161			$this->bWorking = false;
162		}
163	}
164	
165	/*
166		Function: canProcessRequest
167		
168		Called by the <xajaxPluginManager> to determine if this plugin can
169		process the current request.  This will return true when the
170		requestURI contains an appropriate hash code.
171	*/
172	function canProcessRequest()
173	{
174		return ('' != $this->sRequest);
175	}
176	
177	function &_getSections($sType)
178	{
179		$objPluginManager =& xajaxPluginManager::getInstance();
180		
181		$objPluginManager->configure('deferScriptGeneration', 'deferred');
182		
183		$aSections = array();
184		
185		// buffer output
186		
187		ob_start();
188		$objPluginManager->generateClientScript();
189		$sScript = ob_get_clean();
190		
191		// parse out blocks
192		
193		$aParts = explode('</' . $sType . '>', $sScript);
194		foreach ($aParts as $sPart)
195		{
196			$aValues = explode('<' . $sType, $sPart, 2);
197			if (2 == count($aValues))
198			{
199				list($sJunk, $sPart) = $aValues;
200				
201				$aValues = explode('>', $sPart, 2);
202				if (2 == count($aValues))
203				{
204					list($sJunk, $sPart) = $aValues;
205			
206					if (0 < strlen($sPart))
207						$aSections[] = $sPart;
208				}
209			}
210		}
211
212		$objPluginManager->configure('deferScriptGeneration', $this->bDeferScriptGeneration);
213		
214		return $aSections;
215	}
216	
217	/*
218		Function: processRequest
219		
220		Called by the <xajaxPluginManager> when the current request should be 
221		processed.  This plugin will generate the javascript or style sheet information
222		that would normally be output by the other xajax plugin objects, when script 
223		deferral is in effect.  If script deferral is disabled, this function returns 
224		without performing any functions.
225	*/
226	function processRequest()
227	{
228		if ($this->canProcessRequest())
229		{
230			$aSections =& $this->_getSections($this->sRequest);
231			
232//			echo "<!--" . print_r($aSections, true) . "-->";
233			
234			// validate the hash
235			$sHash = md5(implode($aSections));
236			if (false == $this->bValidateHash || $sHash == $this->sHash)
237			{
238				$sType = 'text/javascript';
239				if ('style' == $this->sRequest)
240					$sType = 'text/css';
241					
242				$objResponse =& new xajaxCustomResponse($sType);
243				
244				foreach ($aSections as $sSection)
245					$objResponse->append($sSection . "\n");
246				
247				$objResponseManager =& xajaxResponseManager::getInstance();
248				$objResponseManager->append($objResponse);
249				
250				header ('Expires: ' . gmdate('D, d M Y H:i:s', time() + (60*60*24)) . ' GMT');
251
252				return true;
253			}
254			
255			return 'Invalid script or style request.';
256			trigger_error('Hash mismatch: ' . $this->sRequest . ': ' . $sHash . ' <==> ' . $this->sHash, E_USER_ERROR);
257		}
258	}
259}
260
261/*
262	Register the plugin with the xajax plugin manager.
263*/
264$objPluginManager =& xajaxPluginManager::getInstance();
265$objPluginManager->registerPlugin(new xajaxScriptPlugin(), 9999);