2 /* vim: set expandtab sw=4 ts=4 sts=4: */
5 * @author Alexander Rutkowski
11 require_once './libraries/common.inc.php';
12 require_once './libraries/Table.class.php';
14 define('TABLE_MAY_BE_ABSENT', true);
15 require './libraries/tbl_common.php';
16 $url_query .= '&goto=tbl_tracking.php&back=tbl_tracking.php';
17 $url_params['goto'] = 'tbl_tracking.php';;
18 $url_params['back'] = 'tbl_tracking.php';
20 // Get relation settings
21 require_once './libraries/relation.lib.php';
23 // Init vars for tracking report
24 if (isset($_REQUEST['report']) ||
isset($_REQUEST['report_export'])) {
25 $data = PMA_Tracker
::getTrackedData($_REQUEST['db'], $_REQUEST['table'], $_REQUEST['version']);
27 $selection_schema = false;
28 $selection_data = false;
29 $selection_both = false;
31 if (! isset($_REQUEST['logtype'])) {
32 $_REQUEST['logtype'] = 'schema_and_data';
34 if ($_REQUEST['logtype'] == 'schema') {
35 $selection_schema = true;
36 } elseif($_REQUEST['logtype'] == 'data') {
37 $selection_data = true;
39 $selection_both = true;
41 if (! isset($_REQUEST['date_from'])) {
42 $_REQUEST['date_from'] = $data['date_from'];
44 if (! isset($_REQUEST['date_to'])) {
45 $_REQUEST['date_to'] = $data['date_to'];
47 if (! isset($_REQUEST['users'])) {
48 $_REQUEST['users'] = '*';
50 $filter_ts_from = strtotime($_REQUEST['date_from']);
51 $filter_ts_to = strtotime($_REQUEST['date_to']);
52 $filter_users = array_map('trim', explode(',', $_REQUEST['users']));
56 if (isset($_REQUEST['report_export'])) {
59 * Filters tracking entries
61 * @param array the entries to filter
62 * @param string "from" date
63 * @param string "to" date
66 * @return array filtered entries
69 function PMA_filter_tracking($data, $filter_ts_from, $filter_ts_to, $filter_users) {
70 $tmp_entries = array();
72 foreach( $data as $entry ) {
73 $timestamp = strtotime($entry['date']);
75 if ($timestamp >= $filter_ts_from && $timestamp <= $filter_ts_to &&
76 ( in_array('*', $filter_users) ||
in_array($entry['username'], $filter_users) ) ) {
77 $tmp_entries[] = array( 'id' => $id,
78 'timestamp' => $timestamp,
79 'username' => $entry['username'],
80 'statement' => $entry['statement']
89 // Filtering data definition statements
90 if ($_REQUEST['logtype'] == 'schema' ||
$_REQUEST['logtype'] == 'schema_and_data') {
91 $entries = array_merge($entries, PMA_filter_tracking($data['ddlog'], $filter_ts_from, $filter_ts_to, $filter_users));
94 // Filtering data manipulation statements
95 if ($_REQUEST['logtype'] == 'data' ||
$_REQUEST['logtype'] == 'schema_and_data') {
96 $entries = array_merge($entries, PMA_filter_tracking($data['dmlog'], $filter_ts_from, $filter_ts_to, $filter_users));
100 foreach ($entries as $key => $row) {
101 $ids[$key] = $row['id'];
102 $timestamps[$key] = $row['timestamp'];
103 $usernames[$key] = $row['username'];
104 $statements[$key] = $row['statement'];
107 array_multisort($timestamps, SORT_ASC
, $ids, SORT_ASC
, $usernames, SORT_ASC
, $statements, SORT_ASC
, $entries);
111 // Export as file download
112 if (isset($_REQUEST['report_export']) && $_REQUEST['export_type'] == 'sqldumpfile') {
113 @ini_set
('url_rewriter.tags','');
115 $dump = "# " . sprintf($strTrackingReportForTable, htmlspecialchars($_REQUEST['table'])) . "\n" .
116 "# " . date('Y-m-d H:i:s') . "\n";
117 foreach($entries as $entry) {
118 $dump .= $entry['statement'];
120 $filename = 'log_' . htmlspecialchars($_REQUEST['table']) . '.sql';
121 header('Content-Type: text/x-sql');
122 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
123 header('Content-Disposition: attachment; filename="' . $filename . '"');
124 if (PMA_USR_BROWSER_AGENT
== 'IE') {
125 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
126 header('Pragma: public');
128 header('Pragma: no-cache');
129 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
138 * Gets tables informations
142 * Displays top menu links
144 require_once './libraries/tbl_links.inc.php';
151 // Create tracking version
152 if (isset($_REQUEST['submit_create_version'])) {
155 if ($_REQUEST['alter_table'] == true) {
156 $tracking_set .= 'ALTER TABLE,';
158 if ($_REQUEST['rename_table'] == true) {
159 $tracking_set .= 'RENAME TABLE,';
161 if ($_REQUEST['create_table'] == true) {
162 $tracking_set .= 'CREATE TABLE,';
164 if ($_REQUEST['drop_table'] == true) {
165 $tracking_set .= 'DROP TABLE,';
167 if ($_REQUEST['create_index'] == true) {
168 $tracking_set .= 'CREATE INDEX,';
170 if ($_REQUEST['drop_index'] == true) {
171 $tracking_set .= 'DROP INDEX,';
173 if ($_REQUEST['insert'] == true) {
174 $tracking_set .= 'INSERT,';
176 if ($_REQUEST['update'] == true) {
177 $tracking_set .= 'UPDATE,';
179 if ($_REQUEST['delete'] == true) {
180 $tracking_set .= 'DELETE,';
182 if ($_REQUEST['truncate'] == true) {
183 $tracking_set .= 'TRUNCATE,';
185 $tracking_set = rtrim($tracking_set, ',');
187 if (PMA_Tracker
::createVersion($GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version'], $tracking_set )) {
188 $msg = PMA_Message
::success(sprintf($strTrackingVersionCreated, $_REQUEST['version'], $GLOBALS['db'], $GLOBALS['table']));
193 // Deactivate tracking
194 if (isset($_REQUEST['submit_deactivate_now'])) {
195 if (PMA_Tracker
::deactivateTracking($GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version'])) {
196 $msg = PMA_Message
::success(sprintf($strTrackingVersionDeactivated, $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']));
202 if (isset($_REQUEST['submit_activate_now'])) {
203 if (PMA_Tracker
::activateTracking($GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version'])) {
204 $msg = PMA_Message
::success(sprintf($strTrackingVersionActivated, $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']));
209 // Export as SQL execution
210 if (isset($_REQUEST['report_export']) && $_REQUEST['export_type'] == 'execution') {
211 foreach($entries as $entry) {
212 $sql_result = PMA_DBI_query( "/*NOTRACK*/\n" . $entry['statement'] );
214 $msg = PMA_Message
::success($strTrackingSQLExecuted);
218 // Export as SQL dump
219 if (isset($_REQUEST['report_export']) && $_REQUEST['export_type'] == 'sqldump')
221 $new_query = "# " . $strTrackingYouCanExecute . "\n" .
222 "# " . $strTrackingCommentOut . "\n" .
224 "CREATE database IF NOT EXISTS pma_temp_db; \n" .
225 "USE pma_temp_db; \n" .
228 foreach($entries as $entry) {
229 $new_query .= $entry['statement'];
231 $msg = PMA_Message
::success($strTrackingSQLExported);
235 $table_temp = $table;
238 $GLOBALS['js_include'][] = 'functions.js';
239 require_once './libraries/sql_query_form.lib.php';
241 PMA_sqlQueryForm($new_query, 'sql');
244 $table = $table_temp;
250 if (isset($_REQUEST['snapshot'])) {
252 <h3
><?php
echo $strTrackingStructureSnapshot;?
> [<a href
="tbl_tracking.php?<?php echo $url_query;?>"><?php
echo $strTrackingReportClose;?
></a
>]</h3
>
254 $data = PMA_Tracker
::getTrackedData($_REQUEST['db'], $_REQUEST['table'], $_REQUEST['version']);
256 // Get first DROP TABLE and CREATE TABLE statements
257 $drop_create_statements = $data['ddlog'][0]['statement'];
259 if (strstr($data['ddlog'][0]['statement'], 'DROP TABLE')) {
260 $drop_create_statements .= $data['ddlog'][1]['statement'];
263 PMA_showMessage(sprintf($strTrackingVersionSnapshotSQL, $_REQUEST['version']), $drop_create_statements);
265 // Unserialize snapshot
266 $temp = unserialize($data['schema_snapshot']);
267 $columns = $temp['COLUMNS'];
268 $indexes = $temp['INDEXES'];
270 <h3
><?php
echo $strStructure;?
></h3
>
271 <table id
="tablestructure" class="data">
274 <th
><?php
echo $strField; ?
></th
>
275 <th
><?php
echo $strType; ?
></th
>
276 <th
><?php
echo $strCollation; ?
></th
>
277 <th
><?php
echo $strNull; ?
></th
>
278 <th
><?php
echo $strDefault; ?
></th
>
279 <th
><?php
echo $strExtra; ?
></th
>
280 <th
><?php
echo $strComment; ?
></th
>
286 foreach($columns as $field_index => $field) {
288 <tr
class="<?php echo $style; ?>">
290 if ($field['Key'] == 'PRI') {
291 echo '<td><b><u>' . $field['Field'] . '</u></b></td>' . "\n";
293 echo '<td><b>' . $field['Field'] . '</b></td>' . "\n";
296 <td
><?php
echo $field['Type'];?
></td
>
297 <td
><?php
echo $field['Collation'];?
></td
>
298 <td
><?php
echo $field['Null'];?
></td
>
299 <td
><?php
echo $field['Default'];?
></td
>
300 <td
><?php
echo $field['Extra'];?
></td
>
301 <td
><?php
echo $field['Comment'];?
></td
>
304 if ($style == 'even') {
315 if (count($indexes) > 0) {
317 <h3
><?php
echo $strIndexes;?
></h3
>
318 <table id
="tablestructure_indexes" class="data">
321 <th
><?php
echo $strKeyname;?
></th
>
322 <th
><?php
echo $strType;?
></th
>
323 <th
><?php
echo $strUnique;?
></th
>
324 <th
><?php
echo $strPacked;?
></th
>
325 <th
><?php
echo $strField;?
></th
>
326 <th
><?php
echo $strCardinality;?
></th
>
327 <th
><?php
echo $strCollation;?
></th
>
328 <th
><?php
echo $strNull;?
></th
>
329 <th
><?php
echo $strComment;?
></th
>
334 foreach ($indexes as $indexes_index => $index) {
335 if ($index['Non_unique'] == 0) {
336 $str_unique = $strYes;
338 $str_unique = $strNo;
340 if ($index['Packed'] != '') {
341 $str_packed = $strYes;
343 $str_packed = $strNo;
346 <tr
class="<?php echo $style; ?>">
347 <td
><b
><?php
echo $index['Key_name'];?
></b
></td
>
348 <td
><?php
echo $index['Index_type'];?
></td
>
349 <td
><?php
echo $str_unique;?
></td
>
350 <td
><?php
echo $str_packed;?
></td
>
351 <td
><?php
echo $index['Column_name'];?
></td
>
352 <td
><?php
echo $index['Cardinality'];?
></td
>
353 <td
><?php
echo $index['Collation'];?
></td
>
354 <td
><?php
echo $index['Null'];?
></td
>
355 <td
><?php
echo $index['Comment'];?
></td
>
358 if ($style == 'even') {
373 // end of snapshot report
378 if (isset($_REQUEST['report']) ||
isset($_REQUEST['report_export'])) {
380 <h3
><?php
echo $strTrackingReport;?
> [<a href
="tbl_tracking.php?<?php echo $url_query;?>"><?php
echo $strTrackingReportClose;?
></a
>]</h3
>
382 <small
><?php
echo $strTrackingStatements . ' ' . $data['tracking']; ?
></small
><br
/>
385 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>&report=true&version=<?php echo $_REQUEST['version'];?>">
388 $str1 = '<select name="logtype">' .
389 '<option value="schema"' . ($selection_schema ?
' selected="selected"' : '') . '>' . $strStrucOnly . '</option>' .
390 '<option value="data"' . ($selection_data ?
' selected="selected"' : ''). '>' . $strDataOnly . '</option>' .
391 '<option value="schema_and_data"' . ($selection_both ?
' selected="selected"' : '') . '>' . $strStrucData . '</option>' .
393 $str2 = '<input type="text" name="date_from" value="' . $_REQUEST['date_from'] . '" size="19" />';
394 $str3 = '<input type="text" name="date_to" value="' . $_REQUEST['date_to'] . '" size="19" />';
395 $str4 = '<input type="text" name="users" value="' . $_REQUEST['users'] . '" />';
396 $str5 = '<input type="submit" name="list_report" value="' . $strGo . '" />';
398 printf($strTrackingShowLogDateUsers, $str1, $str2, $str3, $str4, $str5);
402 * First, list tracked data definition statements
405 if ($selection_schema ||
$selection_both ) {
407 <table id
="ddl_versions" class="data" width
="100%">
410 <th width
="18">#</th>
411 <th width
="100"><?php
echo $strTrackingDate;?
></th
>
412 <th width
="60"><?php
echo $strTrackingUsername;?
></th
>
413 <th
><?php
echo $strTrackingDataDefinitionStatement;?
></th
>
419 foreach ($data['ddlog'] as $entry) {
420 $parsed_sql = PMA_SQP_parse($entry['statement']);
421 $statement = PMA_formatSql($parsed_sql);
422 $timestamp = strtotime($entry['date']);
424 if ($timestamp >= $filter_ts_from && $timestamp <= $filter_ts_to &&
425 ( in_array('*', $filter_users) ||
in_array($entry['username'], $filter_users) ) ) {
427 <tr
class="<?php echo $style; ?>">
428 <td
><small
><?php
echo $i;?
></small
></td
>
429 <td
><small
><?php
echo $entry['date'];?
></small
></td
>
430 <td
><small
><?php
echo $entry['username']; ?
></small
></td
>
431 <td
><?php
echo $statement; ?
></td
>
434 if ($style == 'even') {
450 * Secondly, list tracked data manipulation statements
453 if (($selection_data ||
$selection_both) && count($data['dmlog']) > 0) {
455 <table id
="dml_versions" class="data" width
="100%">
458 <th width
="18">#</th>
459 <th width
="100"><?php
echo $strTrackingDate;?
></th
>
460 <th width
="60"><?php
echo $strTrackingUsername;?
></th
>
461 <th
><?php
echo $strTrackingDataManipulationStatement;?
></th
>
467 foreach ($data['dmlog'] as $entry) {
468 $parsed_sql = PMA_SQP_parse($entry['statement']);
469 $statement = PMA_formatSql($parsed_sql);
470 $timestamp = strtotime($entry['date']);
472 if ($timestamp >= $filter_ts_from && $timestamp <= $filter_ts_to &&
473 ( in_array('*', $filter_users) ||
in_array($entry['username'], $filter_users) ) ) {
475 <tr
class="<?php echo $style; ?>">
476 <td
><small
><?php
echo $i; ?
></small
></td
>
477 <td
><small
><?php
echo $entry['date']; ?
></small
></td
>
478 <td
><small
><?php
echo $entry['username']; ?
></small
></td
>
479 <td
><?php
echo $statement; ?
></td
>
482 if ($style == 'even') {
497 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>&report=true&version=<?php echo $_REQUEST['version'];?>">
499 printf($strTrackingShowLogDateUsers, $str1, $str2, $str3, $str4, $str5);
501 $str_export1 = '<select name="export_type">' .
502 '<option value="sqldumpfile">' . $strTrackingSQLDumpFile . '</option>' .
503 '<option value="sqldump">' . $strTrackingSQLDump . '</option>' .
504 '<option value="execution" onclick="alert(\'' . $strTrackingSQLExecutionAlert .'\')">' . $strTrackingSQLExecution . '</option>' .
507 $str_export2 = '<input type="submit" name="report_export" value="' . $strGo .'" />';
510 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>&report=true&version=<?php echo $_REQUEST['version'];?>">
511 <input type
="hidden" name
="logtype" value
="<?php echo $_REQUEST['logtype'];?>" />
512 <input type
="hidden" name
="date_from" value
="<?php echo $_REQUEST['date_from'];?>" />
513 <input type
="hidden" name
="date_to" value
="<?php echo $_REQUEST['date_to'];?>" />
514 <input type
="hidden" name
="users" value
="<?php echo $_REQUEST['users'];?>" />
516 echo "<br/>" . sprintf($strTrackingExportAs, $str_export1) . $str_export2 . "<br/>";
520 echo "<br/><br/><hr/><br/>\n";
525 * List selectable tables
528 $sql_query = " SELECT DISTINCT db_name, table_name FROM " .
529 PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) . "." .
530 PMA_backquote($GLOBALS['cfg']['Server']['tracking']) .
531 " WHERE " . PMA_backquote('db_name') . " = '" . PMA_sqlAddslashes($GLOBALS['db']) . "' " .
532 " ORDER BY ". PMA_backquote('db_name') . ", " . PMA_backquote('table_name');
534 $sql_result = PMA_query_as_controluser($sql_query);
536 if (PMA_DBI_num_rows($sql_result) > 0) {
538 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query;?>">
539 <select name
="table">
541 while ($entries = PMA_DBI_fetch_array($sql_result)) {
542 if (PMA_Tracker
::isTracked($entries['db_name'], $entries['table_name'])) {
543 $status = ' (' . $strTrackingStatusActive . ')';
545 $status = ' (' . $strTrackingStatusNotActive . ')';
547 if ($entries['table_name'] == $_REQUEST['table']) {
548 $s = ' selected="selected"';
552 echo '<option value="' . htmlspecialchars($entries['table_name']) . '"' . $s . '>' . htmlspecialchars($entries['db_name']) . ' . ' . htmlspecialchars($entries['table_name']) . $status . '</option>' . "\n";
556 <input type
="submit" name
="show_versions_submit" value
="<?php echo $strTrackingShowVersions;?>" />
565 * List versions of current table
568 $sql_query = " SELECT * FROM " .
569 PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) . "." .
570 PMA_backquote($GLOBALS['cfg']['Server']['tracking']) .
571 " WHERE " . PMA_backquote('db_name') . " = '" . PMA_sqlAddslashes($_REQUEST['db']) . "' ".
572 " AND " . PMA_backquote('table_name') . " = '" . PMA_sqlAddslashes($_REQUEST['table']) ."' ".
573 " ORDER BY ". PMA_backquote('version') . " DESC ";
575 $sql_result = PMA_query_as_controluser($sql_query);
578 $maxversion = PMA_DBI_fetch_array($sql_result);
579 $last_version = $maxversion['version'];
581 if ($last_version > 0) {
583 <table id
="versions" class="data">
586 <th
><?php
echo $strDatabase;?
></th
>
587 <th
><?php
echo $strTable;?
></th
>
588 <th
><?php
echo $strTrackingThVersion;?
></th
>
589 <th
><?php
echo $strTrackingThCreated;?
></th
>
590 <th
><?php
echo $strTrackingThUpdated;?
></th
>
591 <th
><?php
echo $strStatus;?
></th
>
592 <th
><?php
echo $strShow;?
></th
>
598 PMA_DBI_data_seek($sql_result, 0);
599 while($version = PMA_DBI_fetch_array($sql_result)) {
600 if ($version['tracking_active'] == 1) {
601 $version_status = $strTrackingStatusActive;
603 $version_status = $strTrackingStatusNotActive;
605 if (($version['version'] == $last_version) && ($version_status == $strTrackingStatusNotActive)) {
606 $tracking_active = false;
608 if (($version['version'] == $last_version) && ($version_status == $strTrackingStatusActive)) {
609 $tracking_active = true;
612 <tr
class="<?php echo $style;?>">
613 <td
><?php
echo htmlspecialchars($version['db_name']);?
></td
>
614 <td
><?php
echo htmlspecialchars($version['table_name']);?
></td
>
615 <td
><?php
echo $version['version'];?
></td
>
616 <td
><?php
echo $version['date_created'];?
></td
>
617 <td
><?php
echo $version['date_updated'];?
></td
>
618 <td
><?php
echo $version_status;?
></td
>
619 <td
> <a href
="tbl_tracking.php?<?php echo $url_query;?>&report=true&version=<?php echo $version['version'];?>"><?php
echo $strTrackingReport;?
></a
> |
<a href
="tbl_tracking.php?<?php echo $url_query;?>&snapshot=true&version=<?php echo $version['version'];?>"><?php
echo $strTrackingStructureSnapshot;?
></a
></td
>
622 if ($style == 'even') {
631 <?php
if ($tracking_active == true) {?
>
632 <div id
="div_deactivate_tracking">
633 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>">
635 <legend
><?php
printf($strTrackingDeactivateTrackingFor, $GLOBALS['db'], $GLOBALS['table']); ?
></legend
>
636 <input type
="hidden" name
="version" value
="<?php echo $last_version; ?>" />
637 <input type
="submit" name
="submit_deactivate_now" value
="<?php echo $strTrackingDeactivateNow; ?>" />
644 <?php
if ($tracking_active == false) {?
>
645 <div id
="div_activate_tracking">
646 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>">
648 <legend
><?php
printf($strTrackingActivateTrackingFor, $GLOBALS['db'], $GLOBALS['table']); ?
></legend
>
649 <input type
="hidden" name
="version" value
="<?php echo $last_version; ?>" />
650 <input type
="submit" name
="submit_activate_now" value
="<?php echo $strTrackingActivateNow; ?>" />
659 <div id
="div_create_version">
660 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>">
661 <?php
echo PMA_generate_common_hidden_inputs($GLOBALS['db'], $GLOBALS['table']); ?
>
663 <legend
><?php
printf($strTrackingCreateVersionOf, ($last_version +
1), $GLOBALS['db'], $GLOBALS['table']); ?
></legend
>
665 <input type
="hidden" name
="version" value
="<?php echo ($last_version + 1); ?>" />
667 <p
><?php
echo $strTrackingTrackDDStatements;?
></p
>
668 <input type
="checkbox" name
="alter_table" value
="true" checked
="checked" /> ALTER TABLE
<br
/>
669 <input type
="checkbox" name
="rename_table" value
="true" checked
="checked" /> RENAME TABLE
<br
/>
670 <input type
="checkbox" name
="create_table" value
="true" checked
="checked" /> CREATE TABLE
<br
/>
671 <input type
="checkbox" name
="drop_table" value
="true" checked
="checked" /> DROP TABLE
<br
/>
673 <input type
="checkbox" name
="create_index" value
="true" checked
="checked" /> CREATE INDEX
<br
/>
674 <input type
="checkbox" name
="drop_index" value
="true" checked
="checked" /> DROP INDEX
<br
/>
675 <p
><?php
echo $strTrackingTrackDMStatements;?
></p
>
676 <input type
="checkbox" name
="insert" value
="true" checked
="checked" /> INSERT
<br
/>
677 <input type
="checkbox" name
="update" value
="true" checked
="checked" /> UPDATE
<br
/>
678 <input type
="checkbox" name
="delete" value
="true" checked
="checked" /> DELETE
<br
/>
679 <input type
="checkbox" name
="truncate" value
="true" checked
="checked" /> TRUNCATE
<br
/>
682 <fieldset
class="tblFooters">
683 <input type
="submit" name
="submit_create_version" value
="<?php echo $strTrackingCreateVersion; ?>" />
688 <br
class="clearfloat"/>
692 * Displays the footer
694 require_once './libraries/footer.inc.php';