1 <?php
2 /**
3 * @package Joomla.Legacy
4 * @subpackage Response
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 JLog::add('JResponse is deprecated.', JLog::WARNING, 'deprecated');
13
14 /**
15 * JResponse Class.
16 *
17 * This class serves to provide the Joomla Platform with a common interface to access
18 * response variables. This includes header and body.
19 *
20 * @since 11.1
21 * @deprecated 1.5 Use JApplicationWeb instead
22 */
23 class JResponse
24 {
25 /**
26 * Response body
27 *
28 * @var array
29 * @since 1.6
30 * @deprecated 3.2
31 */
32 protected static $body = array();
33
34 /**
35 * Flag if the response is cachable
36 *
37 * @var boolean
38 * @since 1.6
39 * @deprecated 3.2
40 */
41 protected static $cachable = false;
42
43 /**
44 * Response headers
45 *
46 * @var array
47 * @since 1.6
48 * @deprecated 3.2
49 */
50 protected static $headers = array();
51
52 /**
53 * Set/get cachable state for the response.
54 *
55 * If $allow is set, sets the cachable state of the response. Always returns current state.
56 *
57 * @param boolean $allow True to allow browser caching.
58 *
59 * @return boolean True if browser caching should be allowed
60 *
61 * @since 1.5
62 * @deprecated 3.2 Use JApplicationWeb::allowCache() instead
63 */
64 public static function allowCache($allow = null)
65 {
66 return JFactory::getApplication()->allowCache($allow);
67 }
68
69 /**
70 * Set a header.
71 *
72 * If $replace is true, replaces any headers already defined with that $name.
73 *
74 * @param string $name The name of the header to set.
75 * @param string $value The value of the header to set.
76 * @param boolean $replace True to replace any existing headers by name.
77 *
78 * @return void
79 *
80 * @since 1.5
81 * @deprecated 3.2 Use JApplicationWeb::setHeader() instead
82 */
83 public static function setHeader($name, $value, $replace = false)
84 {
85 JFactory::getApplication()->setHeader($name, $value, $replace);
86 }
87
88 /**
89 * Return array of headers.
90 *
91 * @return array
92 *
93 * @since 1.5
94 * @deprecated 3.2 Use JApplicationWeb::getHeaders() instead
95 */
96 public static function getHeaders()
97 {
98 return JFactory::getApplication()->getHeaders();
99 }
100
101 /**
102 * Clear headers.
103 *
104 * @return void
105 *
106 * @since 1.5
107 * @deprecated 3.2 Use JApplicationWeb::clearHeaders() instead
108 */
109 public static function clearHeaders()
110 {
111 JFactory::getApplication()->clearHeaders();
112 }
113
114 /**
115 * Send all headers.
116 *
117 * @return void
118 *
119 * @since 1.5
120 * @deprecated 3.2 Use JApplicationWeb::sendHeaders() instead
121 */
122 public static function sendHeaders()
123 {
124 JFactory::getApplication()->sendHeaders();
125 }
126
127 /**
128 * Set body content.
129 *
130 * If body content already defined, this will replace it.
131 *
132 * @param string $content The content to set to the response body.
133 *
134 * @return void
135 *
136 * @since 1.5
137 * @deprecated 3.2 Use JApplicationWeb::setBody() instead
138 */
139 public static function setBody($content)
140 {
141 JFactory::getApplication()->setBody($content);
142 }
143
144 /**
145 * Prepend content to the body content
146 *
147 * @param string $content The content to prepend to the response body.
148 *
149 * @return void
150 *
151 * @since 1.5
152 * @deprecated 3.2 Use JApplicationWeb::prependBody() instead
153 */
154 public static function prependBody($content)
155 {
156 JFactory::getApplication()->prependBody($content);
157 }
158
159 /**
160 * Append content to the body content
161 *
162 * @param string $content The content to append to the response body.
163 *
164 * @return void
165 *
166 * @since 1.5
167 * @deprecated 3.2 Use JApplicationWeb::appendBody() instead
168 */
169 public static function appendBody($content)
170 {
171 JFactory::getApplication()->appendBody($content);
172 }
173
174 /**
175 * Return the body content
176 *
177 * @param boolean $toArray Whether or not to return the body content as an array of strings or as a single string; defaults to false.
178 *
179 * @return string array
180 *
181 * @since 1.5
182 * @deprecated 3.2 Use JApplicationWeb::getBody() instead
183 */
184 public static function getBody($toArray = false)
185 {
186 return JFactory::getApplication()->getBody($toArray);
187 }
188
189 /**
190 * Sends all headers prior to returning the string
191 *
192 * @param boolean $compress If true, compress the data
193 *
194 * @return string
195 *
196 * @since 1.5
197 * @deprecated 3.2 Use JApplicationCms::toString() instead
198 */
199 public static function toString($compress = false)
200 {
201 return JFactory::getApplication()->toString($compress);
202 }
203
204 /**
205 * Compress the data
206 *
207 * Checks the accept encoding of the browser and compresses the data before
208 * sending it to the client.
209 *
210 * @param string $data Content to compress for output.
211 *
212 * @return string compressed data
213 *
214 * @note Replaces _compress method from 1.5
215 * @since 1.7
216 * @deprecated 3.2 Use JApplicationWeb::compress() instead
217 */
218 protected static function compress($data)
219 {
220 $encoding = self::clientEncoding();
221
222 if (!$encoding)
223 {
224 return $data;
225 }
226
227 if (!extension_loaded('zlib') || ini_get('zlib.output_compression'))
228 {
229 return $data;
230 }
231
232 if (headers_sent())
233 {
234 return $data;
235 }
236
237 if (connection_status() !== 0)
238 {
239 return $data;
240 }
241
242 // Ideal level
243 $level = 4;
244
245 /*
246 $size = strlen($data);
247 $crc = crc32($data);
248 $gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
249 $gzdata .= gzcompress($data, $level);
250 $gzdata = substr($gzdata, 0, strlen($gzdata) - 4);
251 $gzdata .= pack("V",$crc) . pack("V", $size);
252 */
253
254 $gzdata = gzencode($data, $level);
255
256 self::setHeader('Content-Encoding', $encoding);
257
258 // Header will be removed at 4.0
259 if (JFactory::getConfig()->get('MetaVersion', 0) && defined('JVERSION'))
260 {
261 self::setHeader('X-Content-Encoded-By', 'Joomla! ' . JVERSION);
262 }
263
264 return $gzdata;
265 }
266
267 /**
268 * Check, whether client supports compressed data
269 *
270 * @return boolean
271 *
272 * @since 1.7
273 * @note Replaces _clientEncoding method from 1.5
274 * @deprecated 3.2 Use JApplicationWebClient instead
275 */
276 protected static function clientEncoding()
277 {
278 if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']))
279 {
280 return false;
281 }
282
283 $encoding = false;
284
285 if (false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
286 {
287 $encoding = 'gzip';
288 }
289
290 if (false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip'))
291 {
292 $encoding = 'x-gzip';
293 }
294
295 return $encoding;
296 }
297 }
298