2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Manages the rendering of pages in PMA
8 if (! defined('PHPMYADMIN')) {
12 require_once 'libraries/OutputBuffering.class.php';
13 require_once 'libraries/Header.class.php';
14 require_once 'libraries/Footer.class.php';
17 * Singleton class used to manage the rendering of pages in PMA
24 * PMA_Response instance
30 private static $_instance;
39 * HTML data to be used in the response
46 * An array of JSON key-value pairs
47 * to be sent back for ajax requests
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.
70 * Whether we are servicing an ajax request for a page
71 * that was fired using the generic page handler in JS.
78 * Whether there were any errors during the processing of the request
79 * Only used for ajax responses
86 * Workaround for PHP bug
94 * Creates a new class instance
96 private function __construct()
98 if (! defined('TESTSUITE')) {
99 $buffer = PMA_OutputBuffering
::getInstance();
101 register_shutdown_function('PMA_Response::response');
103 $this->_header
= new PMA_Header();
105 $this->_JSON
= array();
106 $this->_footer
= new PMA_Footer();
108 $this->_isSuccess
= true;
109 $this->_isAjax
= false;
110 $this->_isAjaxPage
= false;
111 if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) {
112 $this->_isAjax
= true;
114 if (isset($_REQUEST['ajax_page_request'])
115 && $_REQUEST['ajax_page_request'] == true
117 $this->_isAjaxPage
= true;
119 $this->_header
->setAjax($this->_isAjax
);
120 $this->_footer
->setAjax($this->_isAjax
);
121 $this->_CWD
= getcwd();
125 * Returns the singleton PMA_Response object
127 * @return PMA_Response object
129 public static function getInstance()
131 if (empty(self
::$_instance)) {
132 self
::$_instance = new PMA_Response();
134 return self
::$_instance;
138 * Set the status of an ajax response,
139 * whether it is a success or an error
141 * @param bool $state Whether the request was successfully processed
145 public function isSuccess($state)
147 $this->_isSuccess
= ($state == true);
151 * Returns true or false depending on whether
152 * we are servicing an ajax request
156 public function isAjax()
158 return $this->_isAjax
;
162 * Returns the path to the current working directory
163 * Necessary to work around a PHP bug where the CWD is
164 * reset after the initial script exits
168 public function getCWD()
174 * Disables the rendering of the header
175 * and the footer in responses
179 public function disable()
181 $this->_header
->disable();
182 $this->_footer
->disable();
186 * Returns a PMA_Header object
190 public function getHeader()
192 return $this->_header
;
196 * Returns a PMA_Footer object
200 public function getFooter()
202 return $this->_footer
;
206 * Add HTML code to the response
208 * @param string $content A string to be appended to
209 * the current output buffer
213 public function addHTML($content)
215 if (is_array($content)) {
216 foreach ($content as $msg) {
217 $this->addHTML($msg);
219 } elseif ($content instanceof PMA_Message
) {
220 $this->_HTML
.= $content->getDisplay();
222 $this->_HTML
.= $content;
227 * Add JSON code to the response
229 * @param mixed $json Either a key (string) or an
230 * array or key-value pairs
231 * @param mixed $value Null, if passing an array in $json otherwise
232 * it's a string value to the key
236 public function addJSON($json, $value = null)
238 if (is_array($json)) {
239 foreach ($json as $key => $value) {
240 $this->addJSON($key, $value);
243 if ($value instanceof PMA_Message
) {
244 $this->_JSON
[$json] = $value->getDisplay();
246 $this->_JSON
[$json] = $value;
253 * Renders the HTML response text
257 private function _getDisplay()
259 // The header may contain nothing at all,
260 // if its content was already rendered
261 // and, in this case, the header will be
262 // in the content part of the request
263 $retval = $this->_header
->getDisplay();
264 $retval .= $this->_HTML
;
265 $retval .= $this->_footer
->getDisplay();
270 * Sends an HTML response to the browser
274 private function _htmlResponse()
276 echo $this->_getDisplay();
280 * Sends a JSON response to the browser
284 private function _ajaxResponse()
286 if (! isset($this->_JSON
['message'])) {
287 $this->_JSON
['message'] = $this->_getDisplay();
288 } else if ($this->_JSON
['message'] instanceof PMA_Message
) {
289 $this->_JSON
['message'] = $this->_JSON
['message']->getDisplay();
292 if ($this->_isSuccess
) {
293 $this->_JSON
['success'] = true;
295 $this->_JSON
['success'] = false;
296 $this->_JSON
['error'] = $this->_JSON
['message'];
297 unset($this->_JSON
['message']);
300 if ($this->_isSuccess
) {
301 // Note: the old judge sentence is:
302 // $this->_isAjaxPage && $this->_isSuccess
303 // Removal the first, because console need log all queries, if caused any
304 // bug, contact Edward Cheng
305 $this->addJSON('_title', $this->getHeader()->getTitleTag());
307 if (isset($GLOBALS['dbi'])) {
308 $menuHash = $this->getHeader()->getMenu()->getHash();
309 $this->addJSON('_menuHash', $menuHash);
311 if (isset($_REQUEST['menuHashes'])) {
312 $hashes = explode('-', $_REQUEST['menuHashes']);
314 if (! in_array($menuHash, $hashes)) {
315 $this->addJSON('_menu', $this->getHeader()->getMenu()->getDisplay());
319 $this->addJSON('_scripts', $this->getHeader()->getScripts()->getFiles());
320 $this->addJSON('_selflink', $this->getFooter()->getSelfUrl('unencoded'));
321 $this->addJSON('_displayMessage', $this->getHeader()->getMessage());
323 $debug = $this->_footer
->getDebugMessage();
324 if (/*overload*/mb_strlen($debug)) {
325 $this->addJSON('_debug', $debug);
328 $errors = $this->_footer
->getErrorMessages();
329 if (/*overload*/mb_strlen($errors)) {
330 $this->addJSON('_errors', $errors);
332 $promptPhpErrors = $GLOBALS['error_handler']->hasErrorsForPrompt();
333 $this->addJSON('_promptPhpErrors', $promptPhpErrors);
335 if (empty($GLOBALS['error_message'])) {
336 // set current db, table and sql query in the querywindow
337 // (this is for the bottom console)
339 $maxChars = $GLOBALS['cfg']['MaxCharactersInDisplayedSQL'];
340 if (isset($GLOBALS['sql_query'])
341 && /*overload*/mb_strlen($GLOBALS['sql_query']) < $maxChars
343 $query = $GLOBALS['sql_query'];
346 '_reloadQuerywindow',
348 'db' => PMA_ifSetOr($GLOBALS['db'], ''),
349 'table' => PMA_ifSetOr($GLOBALS['table'], ''),
350 'sql_query' => $query
353 if (! empty($GLOBALS['focus_querywindow'])) {
354 $this->addJSON('_focusQuerywindow', $query);
356 if (! empty($GLOBALS['reload'])) {
357 $this->addJSON('_reloadNavigation', 1);
359 $this->addJSON('_params', $this->getHeader()->getJsParams());
363 // Set the Content-Type header to JSON so that jQuery parses the
364 // response correctly.
365 if (! defined('TESTSUITE')) {
366 header('Cache-Control: no-cache');
367 header('Content-Type: application/json');
370 echo json_encode($this->_JSON
);
374 * Sends an HTML response to the browser
379 public static function response()
381 $response = PMA_Response
::getInstance();
382 chdir($response->getCWD());
383 $buffer = PMA_OutputBuffering
::getInstance();
384 if (empty($response->_HTML
)) {
385 $response->_HTML
= $buffer->getContents();
387 if ($response->isAjax()) {
388 $response->_ajaxResponse();
390 $response->_htmlResponse();