db_tracking.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Tracking configuration for database
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Run common work
  10. */
  11. require_once 'libraries/common.inc.php';
  12. //Get some js files needed for Ajax requests
  13. $response = PMA_Response::getInstance();
  14. $header = $response->getHeader();
  15. $scripts = $header->getScripts();
  16. $scripts->addFile('db_structure.js');
  17. /**
  18. * If we are not in an Ajax request, then do the common work and show the links etc.
  19. */
  20. require 'libraries/db_common.inc.php';
  21. $url_query .= '&amp;goto=tbl_tracking.php&amp;back=db_tracking.php';
  22. // Get the database structure
  23. $sub_part = '_structure';
  24. require 'libraries/db_info.inc.php';
  25. // Work to do?
  26. // (here, do not use $_REQUEST['db] as it can be crafted)
  27. if (isset($_REQUEST['delete_tracking']) && isset($_REQUEST['table'])) {
  28. PMA_Tracker::deleteTracking($GLOBALS['db'], $_REQUEST['table']);
  29. /**
  30. * If in an Ajax request, generate the success message and use
  31. * {@link PMA_Response()} to send the output
  32. */
  33. if ($GLOBALS['is_ajax_request'] == true) {
  34. $response = PMA_Response::getInstance();
  35. $response->addJSON('message', PMA_Message::success());
  36. exit;
  37. }
  38. }
  39. // Get tracked data about the database
  40. $data = PMA_Tracker::getTrackedData($_REQUEST['db'], '', '1');
  41. // No tables present and no log exist
  42. if ($num_tables == 0 && count($data['ddlog']) == 0) {
  43. echo '<p>' . __('No tables found in database.') . '</p>' . "\n";
  44. if (empty($db_is_system_schema)) {
  45. include 'libraries/display_create_table.lib.php';
  46. }
  47. exit;
  48. }
  49. // ---------------------------------------------------------------------------
  50. // Prepare statement to get HEAD version
  51. $all_tables_query = ' SELECT table_name, MAX(version) as version FROM ' .
  52. PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . '.' .
  53. PMA_Util::backquote($GLOBALS['cfg']['Server']['tracking']) .
  54. ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($_REQUEST['db']) . '\' ' .
  55. ' GROUP BY table_name' .
  56. ' ORDER BY table_name ASC';
  57. $all_tables_result = PMA_queryAsControlUser($all_tables_query);
  58. // If a HEAD version exists
  59. if ($GLOBALS['dbi']->numRows($all_tables_result) > 0) {
  60. ?>
  61. <div id="tracked_tables">
  62. <h3><?php echo __('Tracked tables');?></h3>
  63. <table id="versions" class="data">
  64. <thead>
  65. <tr>
  66. <th><?php echo __('Database');?></th>
  67. <th><?php echo __('Table');?></th>
  68. <th><?php echo __('Last version');?></th>
  69. <th><?php echo __('Created');?></th>
  70. <th><?php echo __('Updated');?></th>
  71. <th><?php echo __('Status');?></th>
  72. <th><?php echo __('Action');?></th>
  73. <th><?php echo __('Show');?></th>
  74. </tr>
  75. </thead>
  76. <tbody>
  77. <?php
  78. // Print out information about versions
  79. $drop_image_or_text = '';
  80. if (PMA_Util::showIcons('ActionLinksMode')) {
  81. $drop_image_or_text .= PMA_Util::getImage(
  82. 'b_drop.png',
  83. __('Delete tracking data for this table')
  84. );
  85. }
  86. if (PMA_Util::showText('ActionLinksMode')) {
  87. $drop_image_or_text .= __('Drop');
  88. }
  89. $style = 'odd';
  90. while ($one_result = $GLOBALS['dbi']->fetchArray($all_tables_result)) {
  91. list($table_name, $version_number) = $one_result;
  92. $table_query = ' SELECT * FROM ' .
  93. PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . '.' .
  94. PMA_Util::backquote($GLOBALS['cfg']['Server']['tracking']) .
  95. ' WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($_REQUEST['db'])
  96. . '\' AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table_name)
  97. . '\' AND `version` = \'' . $version_number . '\'';
  98. $table_result = PMA_queryAsControlUser($table_query);
  99. $version_data = $GLOBALS['dbi']->fetchArray($table_result);
  100. if ($version_data['tracking_active'] == 1) {
  101. $version_status = __('active');
  102. } else {
  103. $version_status = __('not active');
  104. }
  105. $tmp_link = 'tbl_tracking.php?' . $url_query . '&amp;table='
  106. . htmlspecialchars($version_data['table_name']);
  107. $delete_link = 'db_tracking.php?' . $url_query . '&amp;table='
  108. . htmlspecialchars($version_data['table_name'])
  109. . '&amp;delete_tracking=true&amp';
  110. ?>
  111. <tr class="noclick <?php echo $style;?>">
  112. <td><?php echo htmlspecialchars($version_data['db_name']);?></td>
  113. <td><?php echo htmlspecialchars($version_data['table_name']);?></td>
  114. <td><?php echo $version_data['version'];?></td>
  115. <td><?php echo $version_data['date_created'];?></td>
  116. <td><?php echo $version_data['date_updated'];?></td>
  117. <td><?php echo $version_status;?></td>
  118. <td>
  119. <a class="drop_tracking_anchor ajax" href="<?php echo $delete_link;?>" >
  120. <?php echo $drop_image_or_text; ?></a>
  121. <?php
  122. echo '</td>'
  123. . '<td>'
  124. . '<a href="' . $tmp_link . '">' . __('Versions') . '</a>'
  125. . '&nbsp;|&nbsp;'
  126. . '<a href="' . $tmp_link . '&amp;report=true&amp;version='
  127. . $version_data['version'] . '">' . __('Tracking report') . '</a>'
  128. . '&nbsp;|&nbsp;'
  129. . '<a href="' . $tmp_link . '&amp;snapshot=true&amp;version='
  130. . $version_data['version'] . '">' . __('Structure snapshot')
  131. . '</a>'
  132. . '</td>'
  133. . '</tr>';
  134. if ($style == 'even') {
  135. $style = 'odd';
  136. } else {
  137. $style = 'even';
  138. }
  139. }
  140. unset($tmp_link);
  141. ?>
  142. </tbody>
  143. </table>
  144. </div>
  145. <?php
  146. }
  147. $sep = $GLOBALS['cfg']['NavigationTreeTableSeparator'];
  148. // Get list of tables
  149. $table_list = PMA_Util::getTableList($GLOBALS['db']);
  150. // For each table try to get the tracking version
  151. foreach ($table_list as $key => $value) {
  152. // If $value is a table group.
  153. if (array_key_exists(('is' . $sep . 'group'), $value)
  154. && $value['is' . $sep . 'group']
  155. ) {
  156. foreach ($value as $temp_table) {
  157. // If $temp_table is a table with the value for 'Name' is set,
  158. // rather than a propery of the table group.
  159. if (is_array($temp_table)
  160. && array_key_exists('Name', $temp_table)
  161. ) {
  162. $tracking_version = PMA_Tracker::getVersion(
  163. $GLOBALS['db'],
  164. $temp_table['Name']
  165. );
  166. if ($tracking_version == -1) {
  167. $my_tables[] = $temp_table['Name'];
  168. }
  169. }
  170. }
  171. } else { // If $value is a table.
  172. if (PMA_Tracker::getVersion($GLOBALS['db'], $value['Name']) == -1) {
  173. $my_tables[] = $value['Name'];
  174. }
  175. }
  176. }
  177. // If untracked tables exist
  178. if (isset($my_tables)) {
  179. ?>
  180. <h3><?php echo __('Untracked tables');?></h3>
  181. <table id="noversions" class="data">
  182. <thead>
  183. <tr>
  184. <th style="width: 300px"><?php echo __('Table');?></th>
  185. <th></th>
  186. </tr>
  187. </thead>
  188. <tbody>
  189. <?php
  190. // Print out list of untracked tables
  191. $style = 'odd';
  192. foreach ($my_tables as $key => $tablename) {
  193. if (PMA_Tracker::getVersion($GLOBALS['db'], $tablename) == -1) {
  194. $my_link = '<a href="tbl_tracking.php?' . $url_query
  195. . '&amp;table=' . htmlspecialchars($tablename) . '">';
  196. $my_link .= PMA_Util::getIcon('eye.png', __('Track table'));
  197. $my_link .= '</a>';
  198. ?>
  199. <tr class="noclick <?php echo $style;?>">
  200. <td><?php echo htmlspecialchars($tablename);?></td>
  201. <td><?php echo $my_link;?></td>
  202. </tr>
  203. <?php
  204. if ($style == 'even') {
  205. $style = 'odd';
  206. } else {
  207. $style = 'even';
  208. }
  209. }
  210. }
  211. ?>
  212. </tbody>
  213. </table>
  214. <?php
  215. }
  216. // If available print out database log
  217. if (count($data['ddlog']) > 0) {
  218. $log = '';
  219. foreach ($data['ddlog'] as $entry) {
  220. $log .= '# ' . $entry['date'] . ' ' . $entry['username'] . "\n"
  221. . $entry['statement'] . "\n";
  222. }
  223. echo PMA_Util::getMessage(__('Database Log'), $log);
  224. }
  225. ?>