Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / libraries / Response.class.php
blobdbea700dfafdb6a894c94fd5d597f4cebb686d70
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 (is_array($content)) {
217 foreach ($content as $msg) {
218 $this->addHTML($msg);
220 } elseif ($content instanceof PMA_Message) {
221 $this->_HTML .= $content->getDisplay();
222 } else {
223 $this->_HTML .= $content;
228 * Add JSON code to the response
230 * @param mixed $json Either a key (string) or an
231 * array or key-value pairs
232 * @param mixed $value Null, if passing an array in $json otherwise
233 * it's a string value to the key
235 * @return void
237 public function addJSON($json, $value = null)
239 if (is_array($json)) {
240 foreach ($json as $key => $value) {
241 $this->addJSON($key, $value);
243 } else {
244 if ($value instanceof PMA_Message) {
245 $this->_JSON[$json] = $value->getDisplay();
246 } else {
247 $this->_JSON[$json] = $value;
254 * Renders the HTML response text
256 * @return string
258 private function _getDisplay()
260 // The header may contain nothing at all,
261 // if it's content was already rendered
262 // and, in this case, the header will be
263 // in the content part of the request
264 $retval = $this->_header->getDisplay();
265 $retval .= $this->_HTML;
266 $retval .= $this->_footer->getDisplay();
267 return $retval;
271 * Sends an HTML response to the browser
273 * @return void
275 private function _htmlResponse()
277 echo $this->_getDisplay();
281 * Sends a JSON response to the browser
283 * @return void
285 private function _ajaxResponse()
287 if (! isset($this->_JSON['message'])) {
288 $this->_JSON['message'] = $this->_getDisplay();
289 } else if ($this->_JSON['message'] instanceof PMA_Message) {
290 $this->_JSON['message'] = $this->_JSON['message']->getDisplay();
293 if ($this->_isSuccess) {
294 $this->_JSON['success'] = true;
295 } else {
296 $this->_JSON['success'] = false;
297 $this->_JSON['error'] = $this->_JSON['message'];
298 unset($this->_JSON['message']);
301 if ($this->_isAjaxPage && $this->_isSuccess) {
302 $this->addJSON('_title', $this->getHeader()->getTitleTag());
304 $menuHash = $this->getHeader()->getMenu()->getHash();
305 $this->addJSON('_menuHash', $menuHash);
306 $hashes = array();
307 if (isset($_REQUEST['menuHashes'])) {
308 $hashes = explode('-', $_REQUEST['menuHashes']);
310 if (! in_array($menuHash, $hashes)) {
311 $this->addJSON('_menu', $this->getHeader()->getMenu()->getDisplay());
314 $this->addJSON('_scripts', $this->getHeader()->getScripts()->getFiles());
315 $this->addJSON('_selflink', $this->getFooter()->getSelfUrl('unencoded'));
316 $this->addJSON('_displayMessage', $this->getHeader()->getMessage());
317 $errors = $this->_footer->getErrorMessages();
318 if (strlen($errors)) {
319 $this->addJSON('_errors', $errors);
321 if (empty($GLOBALS['error_message'])) {
322 // set current db, table and sql query in the querywindow
323 $query = '';
324 if (isset($GLOBALS['sql_query'])
325 && strlen($GLOBALS['sql_query']) < $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']
327 $query = PMA_escapeJsString($GLOBALS['sql_query']);
329 $this->addJSON(
330 '_reloadQuerywindow',
331 array(
332 'db' => PMA_ifSetOr($GLOBALS['db'], ''),
333 'table' => PMA_ifSetOr($GLOBALS['table'], ''),
334 'sql_query' => $query
337 if (! empty($GLOBALS['focus_querywindow'])) {
338 $this->addJSON('_focusQuerywindow', $query);
340 if (! empty($GLOBALS['reload'])) {
341 $this->addJSON('_reloadNavigation', 1);
343 $this->addJSON('_params', $this->getHeader()->getJsParams());
347 // Set the Content-Type header to JSON so that jQuery parses the
348 // response correctly.
349 if (! defined('TESTSUITE')) {
350 header('Cache-Control: no-cache');
351 header('Content-Type: application/json');
354 echo json_encode($this->_JSON);
358 * Sends an HTML response to the browser
360 * @static
361 * @return void
363 public static function response()
365 $response = PMA_Response::getInstance();
366 chdir($response->getCWD());
367 $buffer = PMA_OutputBuffering::getInstance();
368 if (empty($response->_HTML)) {
369 $response->_HTML = $buffer->getContents();
371 if ($response->isAjax()) {
372 $response->_ajaxResponse();
373 } else {
374 $response->_htmlResponse();
376 $buffer->flush();
377 exit;