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 druing the processing of the request
79 * Only used for ajax responses
86 * Workaround for PHP bug
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();
104 $this->_header
= new PMA_Header();
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
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
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
169 public function getCWD()
175 * Disables the rendering of the header
176 * and the footer in responses
180 public function disable()
182 $this->_header
->disable();
183 $this->_footer
->disable();
187 * Returns a PMA_Header object
191 public function getHeader()
193 return $this->_header
;
197 * Returns a PMA_Footer 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
214 public function addHTML($content)
216 if ($content instanceof PMA_Message
) {
217 $this->_HTML
.= $content->getDisplay();
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
233 public function addJSON($json, $value = null)
235 if (is_array($json)) {
236 foreach ($json as $key => $value) {
237 $this->addJSON($key, $value);
240 if ($value instanceof PMA_Message
) {
241 $this->_JSON
[$json] = $value->getDisplay();
243 $this->_JSON
[$json] = $value;
250 * Renders the HTML response text
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();
267 * Sends an HTML response to the browser
271 private function _htmlResponse()
273 echo $this->_getDisplay();
277 * Sends a JSON response to the browser
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;
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);
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
320 if (isset($GLOBALS['sql_query'])
321 && strlen($GLOBALS['sql_query']) < $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']
323 $query = PMA_escapeJsString($GLOBALS['sql_query']);
326 '_reloadQuerywindow',
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
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();
370 $response->_htmlResponse();