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