Footer.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * Used to render the footer of PMA's pages
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin;
  7. use PhpMyAdmin\ConfigStorage\Relation;
  8. use Traversable;
  9. use function basename;
  10. use function in_array;
  11. use function is_array;
  12. use function is_object;
  13. use function is_scalar;
  14. use function json_encode;
  15. use function json_last_error;
  16. use function strlen;
  17. /**
  18. * Class used to output the footer
  19. */
  20. class Footer
  21. {
  22. /**
  23. * Scripts instance
  24. *
  25. * @var Scripts
  26. */
  27. private $scripts;
  28. /**
  29. * Whether we are servicing an ajax request.
  30. *
  31. * @var bool
  32. */
  33. private $isAjax = false;
  34. /**
  35. * Whether to only close the BODY and HTML tags
  36. * or also include scripts, errors and links
  37. *
  38. * @var bool
  39. */
  40. private $isMinimal;
  41. /**
  42. * Whether to display anything
  43. *
  44. * @var bool
  45. */
  46. private $isEnabled;
  47. /** @var Relation */
  48. private $relation;
  49. /** @var Template */
  50. private $template;
  51. /**
  52. * Creates a new class instance
  53. */
  54. public function __construct()
  55. {
  56. global $dbi;
  57. $this->template = new Template();
  58. $this->isEnabled = true;
  59. $this->scripts = new Scripts();
  60. $this->isMinimal = false;
  61. $this->relation = new Relation($dbi);
  62. }
  63. /**
  64. * Remove recursions and iterator objects from an object
  65. *
  66. * @param mixed $object Object to clean
  67. * @param array $stack Stack used to keep track of recursion, need not be passed for the first time
  68. *
  69. * @return mixed Reference passed object
  70. */
  71. private static function removeRecursion(&$object, array $stack = [])
  72. {
  73. if ((is_object($object) || is_array($object)) && $object) {
  74. if ($object instanceof Traversable) {
  75. $object = '***ITERATOR***';
  76. } elseif (! in_array($object, $stack, true)) {
  77. $stack[] = $object;
  78. // @phpstan-ignore-next-line
  79. foreach ($object as &$subObject) {
  80. self::removeRecursion($subObject, $stack);
  81. }
  82. } else {
  83. $object = '***RECURSION***';
  84. }
  85. }
  86. return $object;
  87. }
  88. /**
  89. * Renders the debug messages
  90. */
  91. public function getDebugMessage(): string
  92. {
  93. $retval = '\'null\'';
  94. if ($GLOBALS['cfg']['DBG']['sql'] && empty($_REQUEST['no_debug']) && ! empty($_SESSION['debug'])) {
  95. // Remove recursions and iterators from $_SESSION['debug']
  96. self::removeRecursion($_SESSION['debug']);
  97. $retval = (string) json_encode($_SESSION['debug']);
  98. $_SESSION['debug'] = [];
  99. return json_last_error() ? '\'false\'' : $retval;
  100. }
  101. $_SESSION['debug'] = [];
  102. return $retval;
  103. }
  104. /**
  105. * Returns the url of the current page
  106. */
  107. public function getSelfUrl(): string
  108. {
  109. global $route, $db, $table, $server;
  110. $params = [];
  111. if (isset($route)) {
  112. $params['route'] = $route;
  113. }
  114. if (isset($db) && strlen($db) > 0) {
  115. $params['db'] = $db;
  116. }
  117. if (isset($table) && strlen($table) > 0) {
  118. $params['table'] = $table;
  119. }
  120. $params['server'] = $server;
  121. // needed for server privileges tabs
  122. if (isset($_GET['viewing_mode']) && in_array($_GET['viewing_mode'], ['server', 'db', 'table'])) {
  123. $params['viewing_mode'] = $_GET['viewing_mode'];
  124. }
  125. /**
  126. * @todo coming from /server/privileges, here $db is not set,
  127. * add the following condition below when that is fixed
  128. * && $_GET['checkprivsdb'] == $db
  129. */
  130. if (isset($_GET['checkprivsdb'])) {
  131. $params['checkprivsdb'] = $_GET['checkprivsdb'];
  132. }
  133. /**
  134. * @todo coming from /server/privileges, here $table is not set,
  135. * add the following condition below when that is fixed
  136. * && $_REQUEST['checkprivstable'] == $table
  137. */
  138. if (isset($_GET['checkprivstable'])) {
  139. $params['checkprivstable'] = $_GET['checkprivstable'];
  140. }
  141. if (isset($_REQUEST['single_table']) && in_array($_REQUEST['single_table'], [true, false])) {
  142. $params['single_table'] = $_REQUEST['single_table'];
  143. }
  144. return basename(Core::getenv('SCRIPT_NAME')) . Url::getCommonRaw($params);
  145. }
  146. /**
  147. * Renders the link to open a new page
  148. */
  149. public function getErrorMessages(): string
  150. {
  151. $retval = '';
  152. if ($GLOBALS['errorHandler']->hasDisplayErrors()) {
  153. $retval .= $GLOBALS['errorHandler']->getDispErrors();
  154. }
  155. /**
  156. * Report php errors
  157. */
  158. $GLOBALS['errorHandler']->reportErrors();
  159. return $retval;
  160. }
  161. /**
  162. * Saves query in history
  163. */
  164. private function setHistory(): void
  165. {
  166. global $dbi;
  167. if (
  168. (
  169. isset($_REQUEST['no_history'])
  170. && is_scalar($_REQUEST['no_history'])
  171. && strlen((string) $_REQUEST['no_history']) > 0
  172. )
  173. || ! empty($GLOBALS['error_message'])
  174. || empty($GLOBALS['sql_query'])
  175. || ! isset($dbi)
  176. || ! $dbi->isConnected()
  177. ) {
  178. return;
  179. }
  180. $this->relation->setHistory(
  181. isset($GLOBALS['db']) && is_scalar($GLOBALS['db']) ? (string) $GLOBALS['db'] : '',
  182. isset($GLOBALS['table']) && is_scalar($GLOBALS['table']) ? (string) $GLOBALS['table'] : '',
  183. $GLOBALS['cfg']['Server']['user'],
  184. $GLOBALS['sql_query']
  185. );
  186. }
  187. /**
  188. * Disables the rendering of the footer
  189. */
  190. public function disable(): void
  191. {
  192. $this->isEnabled = false;
  193. }
  194. /**
  195. * Set the ajax flag to indicate whether
  196. * we are servicing an ajax request
  197. *
  198. * @param bool $isAjax Whether we are servicing an ajax request
  199. */
  200. public function setAjax(bool $isAjax): void
  201. {
  202. $this->isAjax = $isAjax;
  203. }
  204. /**
  205. * Turn on minimal display mode
  206. */
  207. public function setMinimal(): void
  208. {
  209. $this->isMinimal = true;
  210. }
  211. /**
  212. * Returns the Scripts object
  213. *
  214. * @return Scripts object
  215. */
  216. public function getScripts(): Scripts
  217. {
  218. return $this->scripts;
  219. }
  220. /**
  221. * Renders the footer
  222. */
  223. public function getDisplay(): string
  224. {
  225. $this->setHistory();
  226. if ($this->isEnabled) {
  227. if (! $this->isAjax && ! $this->isMinimal) {
  228. if (Core::getenv('SCRIPT_NAME')) {
  229. $url = $this->getSelfUrl();
  230. }
  231. $this->scripts->addCode('var debugSQLInfo = ' . $this->getDebugMessage() . ';');
  232. $errorMessages = $this->getErrorMessages();
  233. $scripts = $this->scripts->getDisplay();
  234. if ($GLOBALS['cfg']['DBG']['demo']) {
  235. $git = new Git(true, ROOT_PATH);
  236. $gitRevisionInfo = $git->getGitRevisionInfo();
  237. }
  238. $footer = Config::renderFooter();
  239. }
  240. return $this->template->render('footer', [
  241. 'is_ajax' => $this->isAjax,
  242. 'is_minimal' => $this->isMinimal,
  243. 'self_url' => $url ?? null,
  244. 'error_messages' => $errorMessages ?? '',
  245. 'scripts' => $scripts ?? '',
  246. 'is_demo' => $GLOBALS['cfg']['DBG']['demo'],
  247. 'git_revision_info' => $gitRevisionInfo ?? [],
  248. 'footer' => $footer ?? '',
  249. ]);
  250. }
  251. return '';
  252. }
  253. }