Footer.php 11 KB

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