PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Quản lý công ty du lịch khách sạn PHP/sgncnew/libraries/joomla/environment/response.php

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