PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/common/xajax/xajax_core/plugin_layer/xajaxScriptPlugin.inc.php

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