Footer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * Used to render the footer of PMA's pages
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin;
  7. use Traversable;
  8. use function basename;
  9. use function file_exists;
  10. use function htmlspecialchars;
  11. use function in_array;
  12. use function is_array;
  13. use function is_object;
  14. use function json_encode;
  15. use function json_last_error;
  16. use function sprintf;
  17. use function strlen;
  18. /**
  19. * Class used to output the footer
  20. */
  21. class Footer
  22. {
  23. /**
  24. * Scripts instance
  25. *
  26. * @access private
  27. * @var Scripts
  28. */
  29. private $scripts;
  30. /**
  31. * Whether we are servicing an ajax request.
  32. *
  33. * @access private
  34. * @var bool
  35. */
  36. private $isAjax;
  37. /**
  38. * Whether to only close the BODY and HTML tags
  39. * or also include scripts, errors and links
  40. *
  41. * @access private
  42. * @var bool
  43. */
  44. private $isMinimal;
  45. /**
  46. * Whether to display anything
  47. *
  48. * @access private
  49. * @var bool
  50. */
  51. private $isEnabled;
  52. /** @var Relation */
  53. private $relation;
  54. /** @var Template */
  55. private $template;
  56. /**
  57. * Creates a new class instance
  58. */
  59. public function __construct()
  60. {
  61. global $dbi;
  62. $this->template = new Template();
  63. $this->isEnabled = true;
  64. $this->scripts = new Scripts();
  65. $this->isMinimal = false;
  66. $this->relation = new Relation($dbi);
  67. }
  68. /**
  69. * Returns the message for demo server to error messages
  70. */
  71. private function getDemoMessage(): string
  72. {
  73. $message = '<a href="/">' . __('phpMyAdmin Demo Server') . '</a>: ';
  74. if (@file_exists(ROOT_PATH . 'revision-info.php')) {
  75. $revision = '';
  76. $fullrevision = '';
  77. $repobase = '';
  78. $repobranchbase = '';
  79. $branch = '';
  80. include ROOT_PATH . 'revision-info.php';
  81. $message .= sprintf(
  82. __('Currently running Git revision %1$s from the %2$s branch.'),
  83. '<a target="_blank" rel="noopener noreferrer" href="'
  84. . htmlspecialchars($repobase . $fullrevision) . '">'
  85. . htmlspecialchars($revision) . '</a>',
  86. '<a target="_blank" rel="noopener noreferrer" href="'
  87. . htmlspecialchars($repobranchbase . $branch) . '">'
  88. . htmlspecialchars($branch) . '</a>'
  89. );
  90. } else {
  91. $message .= __('Git information missing!');
  92. }
  93. return Message::notice($message)->getDisplay();
  94. }
  95. /**
  96. * Remove recursions and iterator objects from an object
  97. *
  98. * @param object|array $object Object to clean
  99. * @param array $stack Stack used to keep track of recursion,
  100. * need not be passed for the first time
  101. *
  102. * @return object Reference passed object
  103. */
  104. private static function removeRecursion(&$object, array $stack = [])
  105. {
  106. if ((is_object($object) || is_array($object)) && $object) {
  107. if ($object instanceof Traversable) {
  108. $object = '***ITERATOR***';
  109. } elseif (! in_array($object, $stack, true)) {
  110. $stack[] = $object;
  111. foreach ($object as &$subobject) {
  112. self::removeRecursion($subobject, $stack);
  113. }
  114. } else {
  115. $object = '***RECURSION***';
  116. }
  117. }
  118. return $object;
  119. }
  120. /**
  121. * Renders the debug messages
  122. */
  123. public function getDebugMessage(): string
  124. {
  125. $retval = '\'null\'';
  126. if ($GLOBALS['cfg']['DBG']['sql']
  127. && empty($_REQUEST['no_debug'])
  128. && ! empty($_SESSION['debug'])
  129. ) {
  130. // Remove recursions and iterators from $_SESSION['debug']
  131. self::removeRecursion($_SESSION['debug']);
  132. $retval = json_encode($_SESSION['debug']);
  133. $_SESSION['debug'] = [];
  134. return json_last_error() ? '\'false\'' : $retval;
  135. }
  136. $_SESSION['debug'] = [];
  137. return $retval;
  138. }
  139. /**
  140. * Returns the url of the current page
  141. */
  142. public function getSelfUrl(): string
  143. {
  144. global $route, $db, $table, $server;
  145. $params = [];
  146. if (isset($route)) {
  147. $params['route'] = $route;
  148. }
  149. if (isset($db) && strlen($db) > 0) {
  150. $params['db'] = $db;
  151. }
  152. if (isset($table) && strlen($table) > 0) {
  153. $params['table'] = $table;
  154. }
  155. $params['server'] = $server;
  156. // needed for server privileges tabs
  157. if (isset($_GET['viewing_mode'])
  158. && in_array($_GET['viewing_mode'], ['server', 'db', 'table'])
  159. ) {
  160. $params['viewing_mode'] = $_GET['viewing_mode'];
  161. }
  162. /*
  163. * @todo coming from /server/privileges, here $db is not set,
  164. * add the following condition below when that is fixed
  165. * && $_GET['checkprivsdb'] == $db
  166. */
  167. if (isset($_GET['checkprivsdb'])
  168. ) {
  169. $params['checkprivsdb'] = $_GET['checkprivsdb'];
  170. }
  171. /*
  172. * @todo coming from /server/privileges, here $table is not set,
  173. * add the following condition below when that is fixed
  174. * && $_REQUEST['checkprivstable'] == $table
  175. */
  176. if (isset($_GET['checkprivstable'])
  177. ) {
  178. $params['checkprivstable'] = $_GET['checkprivstable'];
  179. }
  180. if (isset($_REQUEST['single_table'])
  181. && in_array($_REQUEST['single_table'], [true, false])
  182. ) {
  183. $params['single_table'] = $_REQUEST['single_table'];
  184. }
  185. return basename(Core::getenv('SCRIPT_NAME')) . Url::getCommonRaw($params);
  186. }
  187. /**
  188. * Renders the link to open a new page
  189. *
  190. * @param string $url The url of the page
  191. */
  192. private function getSelfLink(string $url): string
  193. {
  194. $retval = '';
  195. $retval .= '<div id="selflink" class="print_ignore">';
  196. $retval .= '<a href="' . htmlspecialchars($url) . '"'
  197. . ' title="' . __('Open new phpMyAdmin window') . '" target="_blank" rel="noopener noreferrer">';
  198. if (Util::showIcons('TabsMode')) {
  199. $retval .= Html\Generator::getImage(
  200. 'window-new',
  201. __('Open new phpMyAdmin window')
  202. );
  203. } else {
  204. $retval .= __('Open new phpMyAdmin window');
  205. }
  206. $retval .= '</a>';
  207. $retval .= '</div>';
  208. return $retval;
  209. }
  210. /**
  211. * Renders the link to open a new page
  212. */
  213. public function getErrorMessages(): string
  214. {
  215. $retval = '';
  216. if ($GLOBALS['error_handler']->hasDisplayErrors()) {
  217. $retval .= $GLOBALS['error_handler']->getDispErrors();
  218. }
  219. /**
  220. * Report php errors
  221. */
  222. $GLOBALS['error_handler']->reportErrors();
  223. return $retval;
  224. }
  225. /**
  226. * Saves query in history
  227. */
  228. private function setHistory(): void
  229. {
  230. global $dbi;
  231. if (Core::isValid($_REQUEST['no_history'])
  232. || ! empty($GLOBALS['error_message'])
  233. || empty($GLOBALS['sql_query'])
  234. || ! isset($dbi)
  235. || ! $dbi->isConnected()
  236. ) {
  237. return;
  238. }
  239. $this->relation->setHistory(
  240. Core::ifSetOr($GLOBALS['db'], ''),
  241. Core::ifSetOr($GLOBALS['table'], ''),
  242. $GLOBALS['cfg']['Server']['user'],
  243. $GLOBALS['sql_query']
  244. );
  245. }
  246. /**
  247. * Disables the rendering of the footer
  248. */
  249. public function disable(): void
  250. {
  251. $this->isEnabled = false;
  252. }
  253. /**
  254. * Set the ajax flag to indicate whether
  255. * we are servicing an ajax request
  256. *
  257. * @param bool $isAjax Whether we are servicing an ajax request
  258. */
  259. public function setAjax(bool $isAjax): void
  260. {
  261. $this->isAjax = $isAjax;
  262. }
  263. /**
  264. * Turn on minimal display mode
  265. */
  266. public function setMinimal(): void
  267. {
  268. $this->isMinimal = true;
  269. }
  270. /**
  271. * Returns the Scripts object
  272. *
  273. * @return Scripts object
  274. */
  275. public function getScripts(): Scripts
  276. {
  277. return $this->scripts;
  278. }
  279. /**
  280. * Renders the footer
  281. */
  282. public function getDisplay(): string
  283. {
  284. $this->setHistory();
  285. if ($this->isEnabled) {
  286. if (! $this->isAjax && ! $this->isMinimal) {
  287. if (Core::getenv('SCRIPT_NAME')
  288. && empty($_POST)
  289. && ! $this->isAjax
  290. ) {
  291. $url = $this->getSelfUrl();
  292. $header = Response::getInstance()->getHeader();
  293. $scripts = $header->getScripts()->getFiles();
  294. $menuHash = $header->getMenu()->getHash();
  295. // prime the client-side cache
  296. $this->scripts->addCode(
  297. sprintf(
  298. 'if (! (history && history.pushState)) '
  299. . 'MicroHistory.primer = {'
  300. . ' url: "%s",'
  301. . ' scripts: %s,'
  302. . ' menuHash: "%s"'
  303. . '};',
  304. Sanitize::escapeJsString($url),
  305. json_encode($scripts),
  306. Sanitize::escapeJsString($menuHash)
  307. )
  308. );
  309. }
  310. if (Core::getenv('SCRIPT_NAME')
  311. && ! $this->isAjax
  312. ) {
  313. $url = $this->getSelfUrl();
  314. $selfLink = $this->getSelfLink($url);
  315. }
  316. $this->scripts->addCode(
  317. 'var debugSQLInfo = ' . $this->getDebugMessage() . ';'
  318. );
  319. $errorMessages = $this->getErrorMessages();
  320. $scripts = $this->scripts->getDisplay();
  321. if ($GLOBALS['cfg']['DBG']['demo']) {
  322. $demoMessage = $this->getDemoMessage();
  323. }
  324. $footer = Config::renderFooter();
  325. }
  326. return $this->template->render('footer', [
  327. 'is_ajax' => $this->isAjax,
  328. 'is_minimal' => $this->isMinimal,
  329. 'self_link' => $selfLink ?? '',
  330. 'error_messages' => $errorMessages ?? '',
  331. 'scripts' => $scripts ?? '',
  332. 'is_demo' => $GLOBALS['cfg']['DBG']['demo'],
  333. 'demo_message' => $demoMessage ?? '',
  334. 'footer' => $footer ?? '',
  335. ]);
  336. }
  337. return '';
  338. }
  339. }