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 (is_array($content)) {
217 foreach ($content as $msg) {
218 $this->addHTML($msg);
220 } elseif ($content instanceof PMA_Message
) {
221 $this->_HTML
.= $content->getDisplay();
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
237 public function addJSON($json, $value = null)
239 if (is_array($json)) {
240 foreach ($json as $key => $value) {
241 $this->addJSON($key, $value);
244 if ($value instanceof PMA_Message
) {
245 $this->_JSON
[$json] = $value->getDisplay();
247 $this->_JSON
[$json] = $value;
254 * Renders the HTML response text
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();
271 * Sends an HTML response to the browser
275 private function _htmlResponse()
277 echo $this->_getDisplay();
281 * Sends a JSON response to the browser
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;
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);
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
324 if (isset($GLOBALS['sql_query'])
325 && strlen($GLOBALS['sql_query']) < $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']
327 $query = PMA_escapeJsString($GLOBALS['sql_query']);
330 '_reloadQuerywindow',
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
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();
374 $response->_htmlResponse();