TrackingController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Database;
  4. use PhpMyAdmin\CheckUserPrivileges;
  5. use PhpMyAdmin\DatabaseInterface;
  6. use PhpMyAdmin\Html\Generator;
  7. use PhpMyAdmin\Message;
  8. use PhpMyAdmin\Response;
  9. use PhpMyAdmin\Template;
  10. use PhpMyAdmin\Tracker;
  11. use PhpMyAdmin\Tracking;
  12. use PhpMyAdmin\Url;
  13. use PhpMyAdmin\Util;
  14. use function count;
  15. use function htmlspecialchars;
  16. use function sprintf;
  17. /**
  18. * Tracking configuration for database.
  19. */
  20. class TrackingController extends AbstractController
  21. {
  22. /** @var Tracking */
  23. private $tracking;
  24. /** @var DatabaseInterface */
  25. private $dbi;
  26. /**
  27. * @param Response $response
  28. * @param string $db Database name.
  29. * @param DatabaseInterface $dbi
  30. */
  31. public function __construct($response, Template $template, $db, Tracking $tracking, $dbi)
  32. {
  33. parent::__construct($response, $template, $db);
  34. $this->tracking = $tracking;
  35. $this->dbi = $dbi;
  36. }
  37. public function index(): void
  38. {
  39. global $db, $text_dir, $url_params, $tables, $num_tables, $PMA_Theme;
  40. global $total_num_tables, $sub_part, $pos, $data, $cfg;
  41. global $tooltip_truename, $tooltip_aliasname, $err_url;
  42. $this->addScriptFiles(['vendor/jquery/jquery.tablesorter.js', 'database/tracking.js']);
  43. Util::checkParameters(['db']);
  44. $err_url = Util::getScriptNameForOption($cfg['DefaultTabDatabase'], 'database');
  45. $err_url .= Url::getCommon(['db' => $db], '&');
  46. if (! $this->hasDatabase()) {
  47. return;
  48. }
  49. $url_params['goto'] = Url::getFromRoute('/table/tracking');
  50. $url_params['back'] = Url::getFromRoute('/database/tracking');
  51. // Get the database structure
  52. $sub_part = '_structure';
  53. [
  54. $tables,
  55. $num_tables,
  56. $total_num_tables,
  57. $sub_part,,
  58. $isSystemSchema,
  59. $tooltip_truename,
  60. $tooltip_aliasname,
  61. $pos,
  62. ] = Util::getDbInfo($db, $sub_part ?? '');
  63. if (isset($_POST['delete_tracking'], $_POST['table'])) {
  64. Tracker::deleteTracking($db, $_POST['table']);
  65. echo Message::success(
  66. __('Tracking data deleted successfully.')
  67. )->getDisplay();
  68. } elseif (isset($_POST['submit_create_version'])) {
  69. $this->tracking->createTrackingForMultipleTables($_POST['selected']);
  70. echo Message::success(
  71. sprintf(
  72. __(
  73. 'Version %1$s was created for selected tables,'
  74. . ' tracking is active for them.'
  75. ),
  76. htmlspecialchars($_POST['version'])
  77. )
  78. )->getDisplay();
  79. } elseif (isset($_POST['submit_mult'])) {
  80. if (! empty($_POST['selected_tbl'])) {
  81. if ($_POST['submit_mult'] === 'delete_tracking') {
  82. foreach ($_POST['selected_tbl'] as $table) {
  83. Tracker::deleteTracking($db, $table);
  84. }
  85. echo Message::success(
  86. __('Tracking data deleted successfully.')
  87. )->getDisplay();
  88. } elseif ($_POST['submit_mult'] === 'track') {
  89. echo $this->template->render('create_tracking_version', [
  90. 'route' => '/database/tracking',
  91. 'url_params' => $url_params,
  92. 'last_version' => 0,
  93. 'db' => $db,
  94. 'selected' => $_POST['selected_tbl'],
  95. 'type' => 'both',
  96. 'default_statements' => $cfg['Server']['tracking_default_statements'],
  97. ]);
  98. return;
  99. }
  100. } else {
  101. echo Message::notice(
  102. __('No tables selected.')
  103. )->getDisplay();
  104. }
  105. }
  106. // Get tracked data about the database
  107. $data = Tracker::getTrackedData($db, '', '1');
  108. // No tables present and no log exist
  109. if ($num_tables == 0 && count($data['ddlog']) === 0) {
  110. echo '<p>' , __('No tables found in database.') , '</p>' , "\n";
  111. if (empty($isSystemSchema)) {
  112. $checkUserPrivileges = new CheckUserPrivileges($this->dbi);
  113. $checkUserPrivileges->getPrivileges();
  114. echo $this->template->render('database/create_table', ['db' => $db]);
  115. }
  116. return;
  117. }
  118. echo $this->tracking->getHtmlForDbTrackingTables(
  119. $db,
  120. $url_params,
  121. $PMA_Theme->getImgPath(),
  122. $text_dir
  123. );
  124. // If available print out database log
  125. if (count($data['ddlog']) <= 0) {
  126. return;
  127. }
  128. $log = '';
  129. foreach ($data['ddlog'] as $entry) {
  130. $log .= '# ' . $entry['date'] . ' ' . $entry['username'] . "\n"
  131. . $entry['statement'] . "\n";
  132. }
  133. echo Generator::getMessage(__('Database Log'), $log);
  134. }
  135. }