PageRenderTime 40ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/classes/Controllers/Table/TrackingController.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 224 lines | 182 code | 37 blank | 5 comment | 37 complexity | 062cabf7648bca180a1fa951ea18f9d1 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Table;
  4. use PhpMyAdmin\DbTableExists;
  5. use PhpMyAdmin\Message;
  6. use PhpMyAdmin\ResponseRenderer;
  7. use PhpMyAdmin\Template;
  8. use PhpMyAdmin\Tracker;
  9. use PhpMyAdmin\Tracking;
  10. use PhpMyAdmin\Url;
  11. use PhpMyAdmin\Util;
  12. use function __;
  13. use function array_map;
  14. use function define;
  15. use function explode;
  16. use function htmlspecialchars;
  17. use function sprintf;
  18. use function strtotime;
  19. final class TrackingController extends AbstractController
  20. {
  21. /** @var Tracking */
  22. private $tracking;
  23. public function __construct(
  24. ResponseRenderer $response,
  25. Template $template,
  26. string $db,
  27. string $table,
  28. Tracking $tracking
  29. ) {
  30. parent::__construct($response, $template, $db, $table);
  31. $this->tracking = $tracking;
  32. }
  33. public function __invoke(): void
  34. {
  35. global $text_dir, $urlParams, $msg, $errorUrl;
  36. global $data, $entries, $filter_ts_from, $filter_ts_to, $filter_users, $selection_schema;
  37. global $selection_data, $selection_both, $sql_result, $db, $table, $cfg;
  38. $this->addScriptFiles(['vendor/jquery/jquery.tablesorter.js', 'table/tracking.js']);
  39. define('TABLE_MAY_BE_ABSENT', true);
  40. Util::checkParameters(['db', 'table']);
  41. $urlParams = ['db' => $db, 'table' => $table];
  42. $errorUrl = Util::getScriptNameForOption($cfg['DefaultTabTable'], 'table');
  43. $errorUrl .= Url::getCommon($urlParams, '&');
  44. DbTableExists::check();
  45. $activeMessage = '';
  46. if (
  47. Tracker::isActive()
  48. && Tracker::isTracked($GLOBALS['db'], $GLOBALS['table'])
  49. && ! (isset($_POST['toggle_activation'])
  50. && $_POST['toggle_activation'] === 'deactivate_now')
  51. && ! (isset($_POST['report_export'])
  52. && $_POST['export_type'] === 'sqldumpfile')
  53. ) {
  54. $msg = Message::notice(
  55. sprintf(
  56. __('Tracking of %s is activated.'),
  57. htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
  58. )
  59. );
  60. $activeMessage = $msg->getDisplay();
  61. }
  62. $urlParams['goto'] = Url::getFromRoute('/table/tracking');
  63. $urlParams['back'] = Url::getFromRoute('/table/tracking');
  64. $data = [];
  65. $entries = [];
  66. $filter_ts_from = null;
  67. $filter_ts_to = null;
  68. $filter_users = [];
  69. $selection_schema = false;
  70. $selection_data = false;
  71. $selection_both = false;
  72. // Init vars for tracking report
  73. if (isset($_POST['report']) || isset($_POST['report_export'])) {
  74. $data = Tracker::getTrackedData($GLOBALS['db'], $GLOBALS['table'], $_POST['version']);
  75. if (! isset($_POST['logtype'])) {
  76. $_POST['logtype'] = 'schema_and_data';
  77. }
  78. if ($_POST['logtype'] === 'schema') {
  79. $selection_schema = true;
  80. } elseif ($_POST['logtype'] === 'data') {
  81. $selection_data = true;
  82. } else {
  83. $selection_both = true;
  84. }
  85. if (! isset($_POST['date_from'])) {
  86. $_POST['date_from'] = $data['date_from'];
  87. }
  88. if (! isset($_POST['date_to'])) {
  89. $_POST['date_to'] = $data['date_to'];
  90. }
  91. if (! isset($_POST['users'])) {
  92. $_POST['users'] = '*';
  93. }
  94. $filter_ts_from = strtotime($_POST['date_from']);
  95. $filter_ts_to = strtotime($_POST['date_to']);
  96. $filter_users = array_map('trim', explode(',', $_POST['users']));
  97. }
  98. // Prepare export
  99. if (isset($_POST['report_export'])) {
  100. $entries = $this->tracking->getEntries($data, (int) $filter_ts_from, (int) $filter_ts_to, $filter_users);
  101. }
  102. // Export as file download
  103. if (isset($_POST['report_export']) && $_POST['export_type'] === 'sqldumpfile') {
  104. $this->tracking->exportAsFileDownload($entries);
  105. }
  106. $actionMessage = '';
  107. if (isset($_POST['submit_mult'])) {
  108. if (! empty($_POST['selected_versions'])) {
  109. if ($_POST['submit_mult'] === 'delete_version') {
  110. foreach ($_POST['selected_versions'] as $version) {
  111. $this->tracking->deleteTrackingVersion($db, $table, $version);
  112. }
  113. $actionMessage = Message::success(
  114. __('Tracking versions deleted successfully.')
  115. )->getDisplay();
  116. }
  117. } else {
  118. $actionMessage = Message::notice(
  119. __('No versions selected.')
  120. )->getDisplay();
  121. }
  122. }
  123. $deleteVersion = '';
  124. if (isset($_POST['submit_delete_version'])) {
  125. $deleteVersion = $this->tracking->deleteTrackingVersion($db, $table, $_POST['version']);
  126. }
  127. $createVersion = '';
  128. if (isset($_POST['submit_create_version'])) {
  129. $createVersion = $this->tracking->createTrackingVersion($db, $table);
  130. }
  131. $deactivateTracking = '';
  132. if (isset($_POST['toggle_activation']) && $_POST['toggle_activation'] === 'deactivate_now') {
  133. $deactivateTracking = $this->tracking->changeTracking($db, $table, 'deactivate');
  134. }
  135. $activateTracking = '';
  136. if (isset($_POST['toggle_activation']) && $_POST['toggle_activation'] === 'activate_now') {
  137. $activateTracking = $this->tracking->changeTracking($db, $table, 'activate');
  138. }
  139. // Export as SQL execution
  140. $message = '';
  141. if (isset($_POST['report_export']) && $_POST['export_type'] === 'execution') {
  142. $sql_result = $this->tracking->exportAsSqlExecution($entries);
  143. $msg = Message::success(__('SQL statements executed.'));
  144. $message = $msg->getDisplay();
  145. }
  146. $sqlDump = '';
  147. if (isset($_POST['report_export']) && $_POST['export_type'] === 'sqldump') {
  148. $sqlDump = $this->tracking->exportAsSqlDump($db, $table, $entries);
  149. }
  150. $schemaSnapshot = '';
  151. if (isset($_POST['snapshot'])) {
  152. $schemaSnapshot = $this->tracking->getHtmlForSchemaSnapshot($urlParams);
  153. }
  154. $trackingReportRows = '';
  155. if (isset($_POST['report']) && (isset($_POST['delete_ddlog']) || isset($_POST['delete_dmlog']))) {
  156. $trackingReportRows = $this->tracking->deleteTrackingReportRows($db, $table, $data);
  157. }
  158. $trackingReport = '';
  159. if (isset($_POST['report']) || isset($_POST['report_export'])) {
  160. $trackingReport = $this->tracking->getHtmlForTrackingReport(
  161. $data,
  162. $urlParams,
  163. $selection_schema,
  164. $selection_data,
  165. $selection_both,
  166. (int) $filter_ts_to,
  167. (int) $filter_ts_from,
  168. $filter_users
  169. );
  170. }
  171. $main = $this->tracking->getHtmlForMainPage($db, $table, $urlParams, $text_dir);
  172. $this->render('table/tracking/index', [
  173. 'active_message' => $activeMessage,
  174. 'action_message' => $actionMessage,
  175. 'delete_version' => $deleteVersion,
  176. 'create_version' => $createVersion,
  177. 'deactivate_tracking' => $deactivateTracking,
  178. 'activate_tracking' => $activateTracking,
  179. 'message' => $message,
  180. 'sql_dump' => $sqlDump,
  181. 'schema_snapshot' => $schemaSnapshot,
  182. 'tracking_report_rows' => $trackingReportRows,
  183. 'tracking_report' => $trackingReport,
  184. 'main' => $main,
  185. ]);
  186. }
  187. }