PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/admintools/response.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 294 lines | 129 code | 36 blank | 129 comment | 23 complexity | bbdd763e40ce47ff338e9e9c86d764c5 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: response.php 77 2010-10-21 06:55:09Z nikosdion $
  4. * @package Joomla.Framework
  5. * @subpackage Environment
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. *
  14. * Modified by Nicholas K. Dionysopoulos to handle GZip headers without conflicts
  15. */
  16. /**
  17. * Create the response global object
  18. */
  19. defined('JPATH_BASE') or die();
  20. if(!class_exists('JResponse'))
  21. {
  22. $GLOBALS['_JRESPONSE'] = new stdClass();
  23. $GLOBALS['_JRESPONSE']->cachable = false;
  24. $GLOBALS['_JRESPONSE']->headers = array();
  25. $GLOBALS['_JRESPONSE']->body = array();
  26. /**
  27. * JResponse Class
  28. *
  29. * This class serves to provide the Joomla Framework with a common interface to access
  30. * response variables. This includes header and body.
  31. *
  32. * @static
  33. * @package Joomla.Framework
  34. * @subpackage Environment
  35. * @since 1.5
  36. */
  37. class JResponse
  38. {
  39. /**
  40. * Set/get cachable state for the response
  41. *
  42. * If $allow is set, sets the cachable state of the response. Always returns current state
  43. *
  44. * @static
  45. * @param boolean $allow
  46. * @return boolean True of browser caching should be allowed
  47. * @since 1.5
  48. */
  49. function allowCache($allow = null)
  50. {
  51. if (!is_null($allow)) {
  52. $GLOBALS['_JRESPONSE']->cachable = (bool) $allow;
  53. }
  54. return $GLOBALS['_JRESPONSE']->cachable;
  55. }
  56. /**
  57. * Set a header
  58. *
  59. * If $replace is true, replaces any headers already defined with that
  60. * $name.
  61. *
  62. * @access public
  63. * @param string $name
  64. * @param string $value
  65. * @param boolean $replace
  66. */
  67. function setHeader($name, $value, $replace = false)
  68. {
  69. $name = (string) $name;
  70. $value = (string) $value;
  71. if ($replace)
  72. {
  73. foreach ($GLOBALS['_JRESPONSE']->headers as $key => $header) {
  74. if ($name == $header['name']) {
  75. unset($GLOBALS['_JRESPONSE']->headers[$key]);
  76. }
  77. }
  78. }
  79. $GLOBALS['_JRESPONSE']->headers[] = array(
  80. 'name' => $name,
  81. 'value' => $value
  82. );
  83. }
  84. /**
  85. * Return array of headers;
  86. *
  87. * @access public
  88. * @return array
  89. */
  90. function getHeaders() {
  91. return $GLOBALS['_JRESPONSE']->headers;
  92. }
  93. /**
  94. * Clear headers
  95. *
  96. * @access public
  97. */
  98. function clearHeaders() {
  99. $GLOBALS['_JRESPONSE']->headers = array();
  100. }
  101. /**
  102. * Send all headers
  103. *
  104. * @access public
  105. * @return void
  106. */
  107. function sendHeaders()
  108. {
  109. if (!headers_sent())
  110. {
  111. $headers_sent = array();
  112. foreach ($GLOBALS['_JRESPONSE']->headers as $header)
  113. {
  114. $key = strtolower($header['name']);
  115. if(in_array($key, $headers_sent)) continue;
  116. $headers_sent[] = $key;
  117. if ('status' == $key)
  118. {
  119. // 'status' headers indicate an HTTP status, and need to be handled slightly differently
  120. header(ucfirst(strtolower($header['name'])) . ': ' . $header['value'], null, (int) $header['value']);
  121. } else {
  122. header($header['name'] . ': ' . $header['value']);
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * Set body content
  129. *
  130. * If body content already defined, this will replace it.
  131. *
  132. * @access public
  133. * @param string $content
  134. */
  135. function setBody($content) {
  136. $GLOBALS['_JRESPONSE']->body = array((string) $content);
  137. }
  138. /**
  139. * Prepend content to the body content
  140. *
  141. * @access public
  142. * @param string $content
  143. */
  144. function prependBody($content) {
  145. array_unshift($GLOBALS['_JRESPONSE']->body, (string) $content);
  146. }
  147. /**
  148. * Append content to the body content
  149. *
  150. * @access public
  151. * @param string $content
  152. */
  153. function appendBody($content) {
  154. array_push($GLOBALS['_JRESPONSE']->body, (string) $content);
  155. }
  156. /**
  157. * Return the body content
  158. *
  159. * @access public
  160. * @param boolean $toArray Whether or not to return the body content as an
  161. * array of strings or as a single string; defaults to false
  162. * @return string|array
  163. */
  164. function getBody($toArray = false)
  165. {
  166. if ($toArray) {
  167. return $GLOBALS['_JRESPONSE']->body;
  168. }
  169. ob_start();
  170. foreach ($GLOBALS['_JRESPONSE']->body as $content) {
  171. echo $content;
  172. }
  173. return ob_get_clean();
  174. }
  175. /**
  176. * Sends all headers prior to returning the string
  177. *
  178. * @access public
  179. * @param boolean $compress If true, compress the data
  180. * @return string
  181. */
  182. function toString($compress = false)
  183. {
  184. $data = JResponse::getBody();
  185. // Don't compress something if the server is going todo it anyway. Waste of time.
  186. if($compress && !ini_get('zlib.output_compression') && ini_get('output_handler')!='ob_gzhandler') {
  187. $data = JResponse::_compress($data);
  188. }
  189. if (JResponse::allowCache() === false)
  190. {
  191. JResponse::setHeader( 'Expires', 'Mon, 1 Jan 2001 00:00:00 GMT', true ); // Expires in the past
  192. JResponse::setHeader( 'Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT', true ); // Always modified
  193. JResponse::setHeader( 'Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', false );
  194. JResponse::setHeader( 'Pragma', 'no-cache' ); // HTTP 1.0
  195. }
  196. JResponse::sendHeaders();
  197. return $data;
  198. }
  199. /**
  200. * Compress the data
  201. *
  202. * Checks the accept encoding of the browser and compresses the data before
  203. * sending it to the client.
  204. *
  205. * @access public
  206. * @param string data
  207. * @return string compressed data
  208. */
  209. function _compress( $data )
  210. {
  211. $encoding = JResponse::_clientEncoding();
  212. if (!$encoding)
  213. return $data;
  214. if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) {
  215. return $data;
  216. }
  217. if (headers_sent())
  218. return $data;
  219. if (connection_status() !== 0)
  220. return $data;
  221. $level = 4; //ideal level
  222. /*
  223. $size = strlen($data);
  224. $crc = crc32($data);
  225. $gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
  226. $gzdata .= gzcompress($data, $level);
  227. $gzdata = substr($gzdata, 0, strlen($gzdata) - 4);
  228. $gzdata .= pack("V",$crc) . pack("V", $size);
  229. */
  230. $gzdata = gzencode($data, $level);
  231. JResponse::setHeader('Content-Encoding', $encoding);
  232. JResponse::setHeader('X-Content-Encoded-By', 'Joomla! 1.5');
  233. return $gzdata;
  234. }
  235. /**
  236. * check, whether client supports compressed data
  237. *
  238. * @access private
  239. * @return boolean
  240. */
  241. function _clientEncoding()
  242. {
  243. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  244. return false;
  245. }
  246. $encoding = false;
  247. if (false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
  248. $encoding = 'gzip';
  249. }
  250. if (false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip')) {
  251. $encoding = 'x-gzip';
  252. }
  253. return $encoding;
  254. }
  255. }
  256. }