start(); $this->_header = new PMA_Header(); $this->_HTML = ''; $this->_JSON = array(); $this->_footer = new PMA_Footer(); $this->_isSuccess = true; $this->_isAjax = false; if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) { $this->_isAjax = true; } $this->_header->setAjax($this->_isAjax); $this->_footer->setAjax($this->_isAjax); $this->_CWD = getcwd(); } /** * Returns the singleton PMA_Response object * * @return PMA_Response object */ public static function getInstance() { if (empty(self::$_instance)) { self::$_instance = new PMA_Response(); } return self::$_instance; } /** * Set the status of an ajax response, * whether it is a success or an error * * @param bool $state Whether the request was successfully processed * * @return void */ public function isSuccess($state) { $this->_isSuccess = ($state == true); } /** * Returns true or false depending on whether * we are servicing an ajax request * * @return bool */ public function isAjax() { return $this->_isAjax; } /** * Returns the path to the current working directory * Necessary to work around a PHP bug where the CWD is * reset after the initial script exits * * @return string */ public function getCWD() { return $this->_CWD; } /** * Disables the rendering of the header * and the footer in responses * * @return void */ public function disable() { $this->_header->disable(); $this->_footer->disable(); } /** * Returns a PMA_Header object * * @return object */ public function getHeader() { return $this->_header; } /** * Returns a PMA_Footer object * * @return object */ public function getFooter() { return $this->_footer; } /** * Add HTML code to the response * * @param string $content A string to be appended to * the current output buffer * * @return void */ public function addHTML($content) { if ($content instanceof PMA_Message) { $this->_HTML .= $content->getDisplay(); } else { $this->_HTML .= $content; } } /** * Add JSON code to the response * * @param mixed $json Either a key (string) or an * array or key-value pairs * @param mixed $value Null, if passing an array in $json otherwise * it's a string value to the key * * @return void */ public function addJSON($json, $value = null) { if (is_array($json)) { foreach ($json as $key => $value) { $this->addJSON($key, $value); } } else { if ($value instanceof PMA_Message) { $this->_JSON[$json] = $value->getDisplay(); } else { $this->_JSON[$json] = $value; } } } /** * Renders the HTML response text * * @return string */ private function _getDisplay() { // The header may contain nothing at all, // if it's content was already rendered // and, in this case, the header will be // in the content part of the request $retval = $this->_header->getDisplay(); $retval .= $this->_HTML; $retval .= $this->_footer->getDisplay(); return $retval; } /** * Sends an HTML response to the browser * * @return void */ private function _htmlResponse() { echo $this->_getDisplay(); } /** * Sends a JSON response to the browser * * @return void */ private function _ajaxResponse() { if (! isset($this->_JSON['message'])) { $this->_JSON['message'] = $this->_getDisplay(); } else if ($this->_JSON['message'] instanceof PMA_Message) { $this->_JSON['message'] = $this->_JSON['message']->getDisplay(); } if ($this->_isSuccess) { $this->_JSON['success'] = true; } else { $this->_JSON['success'] = false; $this->_JSON['error'] = $this->_JSON['message']; unset($this->_JSON['message']); } // Set the Content-Type header to JSON so that jQuery parses the // response correctly. if (! defined('TESTSUITE')) { header('Cache-Control: no-cache'); header('Content-Type: application/json'); } echo json_encode($this->_JSON); } /** * Sends an HTML response to the browser * * @static * @return void */ public static function response() { $response = PMA_Response::getInstance(); chdir($response->getCWD()); $buffer = PMA_OutputBuffering::getInstance(); if (empty($response->_HTML)) { $response->_HTML = $buffer->getContents(); } if ($response->isAjax()) { $response->_ajaxResponse(); } else { $response->_htmlResponse(); } $buffer->flush(); exit; } } ?>