"Fossies" - the Fresh Open Source Software Archive

Member "phpMyAdmin-5.1.0-english/libraries/classes/Controllers/Table/TrackingController.php" (24 Feb 2021, 8098 Bytes) of package /linux/www/phpMyAdmin-5.1.0-english.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "TrackingController.php" see the Fossies "Dox" file reference documentation.

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