Merge remote-tracking branch 'origin/QA_4_0' into QA_4_0
[phpmyadmin.git] / libraries / Response.class.php
blob1bc183293c99a666f3d446f40a7f37a118f0c9e2
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Manages the rendering of pages in PMA
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 require_once 'libraries/OutputBuffering.class.php';
13 require_once 'libraries/Header.class.php';
14 require_once 'libraries/Footer.class.php';
16 /**
17 * Singleton class used to manage the rendering of pages in PMA
19 * @package PhpMyAdmin
21 class PMA_Response
23 /**
24 * PMA_Response instance
26 * @access private
27 * @static
28 * @var object
30 private static $_instance;
31 /**
32 * PMA_Header instance
34 * @access private
35 * @var object
37 private $_header;
38 /**
39 * HTML data to be used in the response
41 * @access private
42 * @var string
44 private $_HTML;
45 /**
46 * An array of JSON key-value pairs
47 * to be sent back for ajax requests
49 * @access private
50 * @var array
52 private $_JSON;
53 /**
54 * PMA_Footer instance
56 * @access private
57 * @var object
59 private $_footer;
60 /**
61 * Whether we are servicing an ajax request.
62 * We can't simply use $GLOBALS['is_ajax_request']
63 * here since it may have not been initialised yet.
65 * @access private
66 * @var bool
68 private $_isAjax;
69 /**
70 * Whether we are servicing an ajax request for a page
71 * that was fired using the generic page handler in JS.
73 * @access private
74 * @var bool
76 private $_isAjaxPage;
77 /**
78 * Whether there were any errors druing the processing of the request
79 * Only used for ajax responses
81 * @access private
82 * @var bool
84 private $_isSuccess;
85 /**
86 * Workaround for PHP bug
88 * @access private
89 * @var bool
91 private $_CWD;
93 /**
94 * Creates a new class instance
96 * @return new PMA_Response object
98 private function __construct()
100 if (! defined('TESTSUITE')) {
101 $buffer = PMA_OutputBuffering::getInstance();
102 $buffer->start();
104 $this->_header = new PMA_Header();
105 $this->_HTML = '';
106 $this->_JSON = array();
107 $this->_footer = new PMA_Footer();
109 $this->_isSuccess = true;
110 $this->_isAjax = false;
111 $this->_isAjaxPage = false;
112 if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) {
113 $this->_isAjax = true;
115 if (isset($_REQUEST['ajax_page_request'])
116 && $_REQUEST['ajax_page_request'] == true
118 $this->_isAjaxPage = true;
120 $this->_header->setAjax($this->_isAjax);
121 $this->_footer->setAjax($this->_isAjax);
122 $this->_CWD = getcwd();
126 * Returns the singleton PMA_Response object
128 * @return PMA_Response object
130 public static function getInstance()
132 if (empty(self::$_instance)) {
133 self::$_instance = new PMA_Response();
135 return self::$_instance;
139 * Set the status of an ajax response,
140 * whether it is a success or an error
142 * @param bool $state Whether the request was successfully processed
144 * @return void
146 public function isSuccess($state)
148 $this->_isSuccess = ($state == true);
152 * Returns true or false depending on whether
153 * we are servicing an ajax request
155 * @return bool
157 public function isAjax()
159 return $this->_isAjax;
163 * Returns the path to the current working directory
164 * Necessary to work around a PHP bug where the CWD is
165 * reset after the initial script exits
167 * @return string
169 public function getCWD()
171 return $this->_CWD;
175 * Disables the rendering of the header
176 * and the footer in responses
178 * @return void
180 public function disable()
182 $this->_header->disable();
183 $this->_footer->disable();
187 * Returns a PMA_Header object
189 * @return object
191 public function getHeader()
193 return $this->_header;
197 * Returns a PMA_Footer object
199 * @return object
201 public function getFooter()
203 return $this->_footer;
207 * Add HTML code to the response
209 * @param string $content A string to be appended to
210 * the current output buffer
212 * @return void
214 public function addHTML($content)
216 if ($content instanceof PMA_Message) {
217 $this->_HTML .= $content->getDisplay();
218 } else {
219 $this->_HTML .= $content;
224 * Add JSON code to the response
226 * @param mixed $json Either a key (string) or an
227 * array or key-value pairs
228 * @param mixed $value Null, if passing an array in $json otherwise
229 * it's a string value to the key
231 * @return void
233 public function addJSON($json, $value = null)
235 if (is_array($json)) {
236 foreach ($json as $key => $value) {
237 $this->addJSON($key, $value);
239 } else {
240 if ($value instanceof PMA_Message) {
241 $this->_JSON[$json] = $value->getDisplay();
242 } else {
243 $this->_JSON[$json] = $value;
250 * Renders the HTML response text
252 * @return string
254 private function _getDisplay()
256 // The header may contain nothing at all,
257 // if it's content was already rendered
258 // and, in this case, the header will be
259 // in the content part of the request
260 $retval = $this->_header->getDisplay();
261 $retval .= $this->_HTML;
262 $retval .= $this->_footer->getDisplay();
263 return $retval;
267 * Sends an HTML response to the browser
269 * @return void
271 private function _htmlResponse()
273 echo $this->_getDisplay();
277 * Sends a JSON response to the browser
279 * @return void
281 private function _ajaxResponse()
283 if (! isset($this->_JSON['message'])) {
284 $this->_JSON['message'] = $this->_getDisplay();
285 } else if ($this->_JSON['message'] instanceof PMA_Message) {
286 $this->_JSON['message'] = $this->_JSON['message']->getDisplay();
289 if ($this->_isSuccess) {
290 $this->_JSON['success'] = true;
291 } else {
292 $this->_JSON['success'] = false;
293 $this->_JSON['error'] = $this->_JSON['message'];
294 unset($this->_JSON['message']);
297 if ($this->_isAjaxPage && $this->_isSuccess) {
298 $this->addJSON('_title', $this->getHeader()->getTitleTag());
300 $menuHash = $this->getHeader()->getMenu()->getHash();
301 $this->addJSON('_menuHash', $menuHash);
302 $hashes = array();
303 if (isset($_REQUEST['menuHashes'])) {
304 $hashes = explode('-', $_REQUEST['menuHashes']);
306 if (! in_array($menuHash, $hashes)) {
307 $this->addJSON('_menu', $this->getHeader()->getMenu()->getDisplay());
310 $this->addJSON('_scripts', $this->getHeader()->getScripts()->getFiles());
311 $this->addJSON('_selflink', $this->getFooter()->getSelfUrl('unencoded'));
312 $this->addJSON('_displayMessage', $this->getHeader()->getMessage());
313 $errors = $this->_footer->getErrorMessages();
314 if (strlen($errors)) {
315 $this->addJSON('_errors', $errors);
317 if (empty($GLOBALS['error_message'])) {
318 // set current db, table and sql query in the querywindow
319 $query = '';
320 if (isset($GLOBALS['sql_query'])
321 && strlen($GLOBALS['sql_query']) < $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']
323 $query = PMA_escapeJsString($GLOBALS['sql_query']);
325 $this->addJSON(
326 '_reloadQuerywindow',
327 array(
328 'db' => PMA_ifSetOr($GLOBALS['db'], ''),
329 'table' => PMA_ifSetOr($GLOBALS['table'], ''),
330 'sql_query' => $query
333 if (! empty($GLOBALS['focus_querywindow'])) {
334 $this->addJSON('_focusQuerywindow', $query);
336 if (! empty($GLOBALS['reload'])) {
337 $this->addJSON('_reloadNavigation', 1);
339 $this->addJSON('_params', $this->getHeader()->getJsParams());
343 // Set the Content-Type header to JSON so that jQuery parses the
344 // response correctly.
345 if (! defined('TESTSUITE')) {
346 header('Cache-Control: no-cache');
347 header('Content-Type: application/json');
350 echo json_encode($this->_JSON);
354 * Sends an HTML response to the browser
356 * @static
357 * @return void
359 public static function response()
361 $response = PMA_Response::getInstance();
362 chdir($response->getCWD());
363 $buffer = PMA_OutputBuffering::getInstance();
364 if (empty($response->_HTML)) {
365 $response->_HTML = $buffer->getContents();
367 if ($response->isAjax()) {
368 $response->_ajaxResponse();
369 } else {
370 $response->_htmlResponse();
372 $buffer->flush();
373 exit;