1. Check existence of mb_string, mysql and xml extensions before installation.
[openemr.git] / phpmyadmin / libraries / Response.class.php
blob255fb1e8e26c26f130cdaf3ba4f09cca75325ed7
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 PMA_Response
30 private static $_instance;
31 /**
32 * PMA_Header instance
34 * @access private
35 * @var PMA_Header
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 PMA_Footer
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 during 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 string|bool
91 private $_CWD;
93 /**
94 * Creates a new class instance
96 private function __construct()
98 if (! defined('TESTSUITE')) {
99 $buffer = PMA_OutputBuffering::getInstance();
100 $buffer->start();
101 register_shutdown_function('PMA_Response::response');
103 $this->_header = new PMA_Header();
104 $this->_HTML = '';
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
143 * @return void
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
154 * @return bool
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
166 * @return string
168 public function getCWD()
170 return $this->_CWD;
174 * Disables the rendering of the header
175 * and the footer in responses
177 * @return void
179 public function disable()
181 $this->_header->disable();
182 $this->_footer->disable();
186 * Returns a PMA_Header object
188 * @return PMA_Header
190 public function getHeader()
192 return $this->_header;
196 * Returns a PMA_Footer object
198 * @return PMA_Footer
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
211 * @return void
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();
221 } else {
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
234 * @return void
236 public function addJSON($json, $value = null)
238 if (is_array($json)) {
239 foreach ($json as $key => $value) {
240 $this->addJSON($key, $value);
242 } else {
243 if ($value instanceof PMA_Message) {
244 $this->_JSON[$json] = $value->getDisplay();
245 } else {
246 $this->_JSON[$json] = $value;
253 * Renders the HTML response text
255 * @return string
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();
266 return $retval;
270 * Sends an HTML response to the browser
272 * @return void
274 private function _htmlResponse()
276 echo $this->_getDisplay();
280 * Sends a JSON response to the browser
282 * @return void
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;
294 } else {
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);
310 $hashes = array();
311 if (isset($_REQUEST['menuHashes'])) {
312 $hashes = explode('-', $_REQUEST['menuHashes']);
314 if (! in_array($menuHash, $hashes)) {
315 $this->addJSON(
316 '_menu',
317 $this->getHeader()
318 ->getMenu()
319 ->getDisplay()
324 $this->addJSON('_scripts', $this->getHeader()->getScripts()->getFiles());
325 $this->addJSON('_selflink', $this->getFooter()->getSelfUrl('unencoded'));
326 $this->addJSON('_displayMessage', $this->getHeader()->getMessage());
328 $debug = $this->_footer->getDebugMessage();
329 if (empty($_REQUEST['no_debug'])
330 && /*overload*/mb_strlen($debug)
332 $this->addJSON('_debug', $debug);
335 $errors = $this->_footer->getErrorMessages();
336 if (/*overload*/mb_strlen($errors)) {
337 $this->addJSON('_errors', $errors);
339 $promptPhpErrors = $GLOBALS['error_handler']->hasErrorsForPrompt();
340 $this->addJSON('_promptPhpErrors', $promptPhpErrors);
342 if (empty($GLOBALS['error_message'])) {
343 // set current db, table and sql query in the querywindow
344 // (this is for the bottom console)
345 $query = '';
346 $maxChars = $GLOBALS['cfg']['MaxCharactersInDisplayedSQL'];
347 if (isset($GLOBALS['sql_query'])
348 && /*overload*/mb_strlen($GLOBALS['sql_query']) < $maxChars
350 $query = $GLOBALS['sql_query'];
352 $this->addJSON(
353 '_reloadQuerywindow',
354 array(
355 'db' => PMA_ifSetOr($GLOBALS['db'], ''),
356 'table' => PMA_ifSetOr($GLOBALS['table'], ''),
357 'sql_query' => $query
360 if (! empty($GLOBALS['focus_querywindow'])) {
361 $this->addJSON('_focusQuerywindow', $query);
363 if (! empty($GLOBALS['reload'])) {
364 $this->addJSON('_reloadNavigation', 1);
366 $this->addJSON('_params', $this->getHeader()->getJsParams());
370 // Set the Content-Type header to JSON so that jQuery parses the
371 // response correctly.
372 PMA_headerJSON();
374 echo json_encode($this->_JSON);
378 * Sends an HTML response to the browser
380 * @static
381 * @return void
383 public static function response()
385 $response = PMA_Response::getInstance();
386 chdir($response->getCWD());
387 $buffer = PMA_OutputBuffering::getInstance();
388 if (empty($response->_HTML)) {
389 $response->_HTML = $buffer->getContents();
391 if ($response->isAjax()) {
392 $response->_ajaxResponse();
393 } else {
394 $response->_htmlResponse();
396 $buffer->flush();
397 exit;