Footer.php 11 KB

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