tracking.lib.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functions used for database and table tracking
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Filters tracking entries
  10. *
  11. * @param array $data the entries to filter
  12. * @param string $filter_ts_from "from" date
  13. * @param string $filter_ts_to "to" date
  14. * @param array $filter_users users
  15. *
  16. * @return array filtered entries
  17. */
  18. function PMA_filterTracking(
  19. $data, $filter_ts_from, $filter_ts_to, $filter_users
  20. ) {
  21. $tmp_entries = array();
  22. $id = 0;
  23. foreach ($data as $entry) {
  24. $timestamp = strtotime($entry['date']);
  25. $filtered_user = in_array($entry['username'], $filter_users);
  26. if ($timestamp >= $filter_ts_from
  27. && $timestamp <= $filter_ts_to
  28. && (in_array('*', $filter_users) || $filtered_user)
  29. ) {
  30. $tmp_entries[] = array(
  31. 'id' => $id,
  32. 'timestamp' => $timestamp,
  33. 'username' => $entry['username'],
  34. 'statement' => $entry['statement']
  35. );
  36. }
  37. $id++;
  38. }
  39. return($tmp_entries);
  40. }
  41. /**
  42. * Function to get html for data definition and data manipulation statements
  43. *
  44. * @param string $url_query url query
  45. * @param int $last_version last version
  46. * @param string $db database
  47. * @param array $selected selected tables
  48. * @param string $type type of the table; table, view or both
  49. *
  50. * @return string
  51. */
  52. function PMA_getHtmlForDataDefinitionAndManipulationStatements($url_query,
  53. $last_version, $db, $selected, $type = 'both'
  54. ) {
  55. $html = '<div id="div_create_version">';
  56. $html .= '<form method="post" action="' . $url_query . '">';
  57. $html .= PMA_URL_getHiddenInputs($db);
  58. foreach ($selected as $selected_table) {
  59. $html .= '<input type="hidden" name="selected[]"'
  60. . ' value="' . htmlspecialchars($selected_table) . '" />';
  61. }
  62. $html .= '<fieldset>';
  63. $html .= '<legend>';
  64. if (count($selected) == 1) {
  65. $html .= sprintf(
  66. __('Create version %1$s of %2$s'),
  67. ($last_version + 1),
  68. htmlspecialchars($db . '.' . $selected[0])
  69. );
  70. } else {
  71. $html .= sprintf(__('Create version %1$s'), ($last_version + 1));
  72. }
  73. $html .= '</legend>';
  74. $html .= '<input type="hidden" name="version" value="' . ($last_version + 1)
  75. . '" />';
  76. $html .= '<p>' . __('Track these data definition statements:')
  77. . '</p>';
  78. if ($type == 'both' || $type == 'table') {
  79. $html .= '<input type="checkbox" name="alter_table" value="true"'
  80. . (/*overload*/mb_stripos(
  81. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  82. 'ALTER TABLE'
  83. ) !== false ? ' checked="checked"' : '')
  84. . ' /> ALTER TABLE<br/>';
  85. $html .= '<input type="checkbox" name="rename_table" value="true"'
  86. . (/*overload*/mb_stripos(
  87. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  88. 'RENAME TABLE'
  89. ) !== false ? ' checked="checked"' : '')
  90. . ' /> RENAME TABLE<br/>';
  91. $html .= '<input type="checkbox" name="create_table" value="true"'
  92. . (/*overload*/mb_stripos(
  93. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  94. 'CREATE TABLE'
  95. ) !== false ? ' checked="checked"' : '')
  96. . ' /> CREATE TABLE<br/>';
  97. $html .= '<input type="checkbox" name="drop_table" value="true"'
  98. . (/*overload*/mb_stripos(
  99. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  100. 'DROP TABLE'
  101. ) !== false ? ' checked="checked"' : '')
  102. . ' /> DROP TABLE<br/>';
  103. }
  104. if ($type == 'both') {
  105. $html .= '<br/>';
  106. }
  107. if ($type == 'both' || $type == 'view') {
  108. $html .= '<input type="checkbox" name="alter_view" value="true"'
  109. . (/*overload*/mb_stripos(
  110. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  111. 'ALTER VIEW'
  112. ) !== false ? ' checked="checked"' : '')
  113. . ' /> ALTER VIEW<br/>';
  114. $html .= '<input type="checkbox" name="create_view" value="true"'
  115. . (/*overload*/mb_stripos(
  116. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  117. 'CREATE VIEW'
  118. ) !== false ? ' checked="checked"' : '')
  119. . ' /> CREATE VIEW<br/>';
  120. $html .= '<input type="checkbox" name="drop_view" value="true"'
  121. . (/*overload*/mb_stripos(
  122. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  123. 'DROP VIEW'
  124. ) !== false ? ' checked="checked"' : '')
  125. . ' /> DROP VIEW<br/>';
  126. }
  127. $html .= '<br/>';
  128. $html .= '<input type="checkbox" name="create_index" value="true"'
  129. . (/*overload*/mb_stripos(
  130. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  131. 'CREATE INDEX'
  132. ) !== false ? ' checked="checked"' : '')
  133. . ' /> CREATE INDEX<br/>';
  134. $html .= '<input type="checkbox" name="drop_index" value="true"'
  135. . (/*overload*/mb_stripos(
  136. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  137. 'DROP INDEX'
  138. ) !== false ? ' checked="checked"' : '')
  139. . ' /> DROP INDEX<br/>';
  140. $html .= '<p>' . __('Track these data manipulation statements:') . '</p>';
  141. $html .= '<input type="checkbox" name="insert" value="true"'
  142. . (/*overload*/mb_stripos(
  143. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  144. 'INSERT'
  145. ) !== false ? ' checked="checked"' : '')
  146. . ' /> INSERT<br/>';
  147. $html .= '<input type="checkbox" name="update" value="true"'
  148. . (/*overload*/mb_stripos(
  149. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  150. 'UPDATE'
  151. ) !== false ? ' checked="checked"' : '')
  152. . ' /> UPDATE<br/>';
  153. $html .= '<input type="checkbox" name="delete" value="true"'
  154. . (/*overload*/mb_stripos(
  155. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  156. 'DELETE'
  157. ) !== false ? ' checked="checked"' : '')
  158. . ' /> DELETE<br/>';
  159. $html .= '<input type="checkbox" name="truncate" value="true"'
  160. . (/*overload*/mb_stripos(
  161. $GLOBALS['cfg']['Server']['tracking_default_statements'],
  162. 'TRUNCATE'
  163. ) !== false ? ' checked="checked"' : '')
  164. . ' /> TRUNCATE<br/>';
  165. $html .= '</fieldset>';
  166. $html .= '<fieldset class="tblFooters">';
  167. $html .= '<input type="hidden" name="submit_create_version" value="1" />';
  168. $html .= '<input type="submit" value="' . __('Create version') . '" />';
  169. $html .= '</fieldset>';
  170. $html .= '</form>';
  171. $html .= '</div>';
  172. return $html;
  173. }
  174. /**
  175. * Function to get html for activate/deactivate tracking
  176. *
  177. * @param string $action activate|deactivate
  178. * @param string $url_query url query
  179. * @param int $last_version last version
  180. *
  181. * @return string
  182. */
  183. function PMA_getHtmlForActivateDeactivateTracking(
  184. $action, $url_query, $last_version
  185. ) {
  186. $html = '<div>';
  187. $html .= '<form method="post" action="tbl_tracking.php' . $url_query . '">';
  188. $html .= '<fieldset>';
  189. $html .= '<legend>';
  190. switch($action) {
  191. case 'activate':
  192. $legend = __('Activate tracking for %s');
  193. $value = "activate_now";
  194. $button = __('Activate now');
  195. break;
  196. case 'deactivate':
  197. $legend = __('Deactivate tracking for %s');
  198. $value = "deactivate_now";
  199. $button = __('Deactivate now');
  200. break;
  201. default:
  202. $legend = '';
  203. $value = '';
  204. $button = '';
  205. }
  206. $html .= sprintf(
  207. $legend,
  208. htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
  209. );
  210. $html .= '</legend>';
  211. $html .= '<input type="hidden" name="version" value="' . $last_version . '" />';
  212. $html .= '<input type="hidden" name="toggle_activation" value="' . $value
  213. . '" />';
  214. $html .= '<input type="submit" value="' . $button . '" />';
  215. $html .= '</fieldset>';
  216. $html .= '</form>';
  217. $html .= '</div>';
  218. return $html;
  219. }
  220. /**
  221. * Function to get the list versions of the table
  222. *
  223. * @return array
  224. */
  225. function PMA_getListOfVersionsOfTable()
  226. {
  227. $cfgRelation = PMA_getRelationsParam();
  228. $sql_query = " SELECT * FROM " .
  229. PMA_Util::backquote($cfgRelation['db']) . "." .
  230. PMA_Util::backquote($cfgRelation['tracking']) .
  231. " WHERE db_name = '" . PMA_Util::sqlAddSlashes($_REQUEST['db']) . "' " .
  232. " AND table_name = '" . PMA_Util::sqlAddSlashes($_REQUEST['table']) . "' " .
  233. " ORDER BY version DESC ";
  234. return PMA_queryAsControlUser($sql_query);
  235. }
  236. /**
  237. * Function to get html for displaying last version number
  238. *
  239. * @param array $sql_result sql result
  240. * @param int $last_version last version
  241. * @param array $url_params url parameters
  242. * @param string $url_query url query
  243. * @param string $pmaThemeImage path to theme's image folder
  244. * @param string $text_dir text direction
  245. *
  246. * @return string
  247. */
  248. function PMA_getHtmlForTableVersionDetails(
  249. $sql_result, $last_version, $url_params,
  250. $url_query, $pmaThemeImage, $text_dir
  251. ) {
  252. $tracking_active = false;
  253. $html = '<form method="post" action="tbl_tracking.php" name="versionsForm"'
  254. . ' id="versionsForm" class="ajax">';
  255. $html .= PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table']);
  256. $html .= '<table id="versions" class="data">';
  257. $html .= '<thead>';
  258. $html .= '<tr>';
  259. $html .= '<th></th>';
  260. $html .= '<th>' . __('Version') . '</th>';
  261. $html .= '<th>' . __('Created') . '</th>';
  262. $html .= '<th>' . __('Updated') . '</th>';
  263. $html .= '<th>' . __('Status') . '</th>';
  264. $html .= '<th>' . __('Action') . '</th>';
  265. $html .= '<th>' . __('Show') . '</th>';
  266. $html .= '</tr>';
  267. $html .= '</thead>';
  268. $html .= '<tbody>';
  269. $style = 'odd';
  270. $GLOBALS['dbi']->dataSeek($sql_result, 0);
  271. $delete = PMA_Util::getIcon('b_drop.png', __('Delete version'));
  272. $report = PMA_Util::getIcon('b_report.png', __('Tracking report'));
  273. $structure = PMA_Util::getIcon('b_props.png', __('Structure snapshot'));
  274. while ($version = $GLOBALS['dbi']->fetchArray($sql_result)) {
  275. if ($version['version'] == $last_version) {
  276. if ($version['tracking_active'] == 1) {
  277. $tracking_active = true;
  278. } else {
  279. $tracking_active = false;
  280. }
  281. }
  282. $delete_link = 'tbl_tracking.php' . $url_query . '&amp;version='
  283. . htmlspecialchars($version['version'])
  284. . '&amp;submit_delete_version=true';
  285. $checkbox_id = 'selected_versions_' . htmlspecialchars($version['version']);
  286. $html .= '<tr class="' . $style . '">';
  287. $html .= '<td class="center">';
  288. $html .= '<input type="checkbox" name="selected_versions[]"'
  289. . ' class="checkall" id="' . $checkbox_id . '"'
  290. . ' value="' . htmlspecialchars($version['version']) . '"/>';
  291. $html .= '</td>';
  292. $html .= '<th class="floatright">';
  293. $html .= '<label for="' . $checkbox_id . '">'
  294. . htmlspecialchars($version['version']) . '</label>';
  295. $html .= '</th>';
  296. $html .= '<td>' . htmlspecialchars($version['date_created']) . '</td>';
  297. $html .= '<td>' . htmlspecialchars($version['date_updated']) . '</td>';
  298. $html .= '<td>' . PMA_getVersionStatus($version) . '</td>';
  299. $html .= '<td><a class="delete_version_anchor ajax"'
  300. . ' href="' . $delete_link . '" >' . $delete . '</a></td>';
  301. $html .= '<td><a href="tbl_tracking.php';
  302. $html .= PMA_URL_getCommon(
  303. $url_params + array(
  304. 'report' => 'true', 'version' => $version['version']
  305. )
  306. );
  307. $html .= '">' . $report . '</a>';
  308. $html .= '&nbsp;&nbsp;';
  309. $html .= '<a href="tbl_tracking.php';
  310. $html .= PMA_URL_getCommon(
  311. $url_params + array(
  312. 'snapshot' => 'true', 'version' => $version['version']
  313. )
  314. );
  315. $html .= '">' . $structure . '</a>';
  316. $html .= '</td>';
  317. $html .= '</tr>';
  318. if ($style == 'even') {
  319. $style = 'odd';
  320. } else {
  321. $style = 'even';
  322. }
  323. }
  324. $html .= '</tbody>';
  325. $html .= '</table>';
  326. $html .= PMA_Util::getWithSelected($pmaThemeImage, $text_dir, "versionsForm");
  327. $html .= PMA_Util::getButtonOrImage(
  328. 'submit_mult', 'mult_submit', 'submit_mult_delete_version',
  329. __('Delete version'), 'b_drop.png', 'delete_version'
  330. );
  331. $html .= '</form>';
  332. if ($tracking_active) {
  333. $html .= PMA_getHtmlForActivateDeactivateTracking(
  334. 'deactivate', $url_query, $last_version
  335. );
  336. } else {
  337. $html .= PMA_getHtmlForActivateDeactivateTracking(
  338. 'activate', $url_query, $last_version
  339. );
  340. }
  341. return $html;
  342. }
  343. /**
  344. * Function to get the last version number of a table
  345. *
  346. * @param array $sql_result sql result
  347. *
  348. * @return int
  349. */
  350. function PMA_getTableLastVersionNumber($sql_result)
  351. {
  352. $maxversion = $GLOBALS['dbi']->fetchArray($sql_result);
  353. $last_version = $maxversion['version'];
  354. return $last_version;
  355. }
  356. /**
  357. * Function to get sql results for selectable tables
  358. *
  359. * @return array
  360. */
  361. function PMA_getSQLResultForSelectableTables()
  362. {
  363. include_once 'libraries/relation.lib.php';
  364. $cfgRelation = PMA_getRelationsParam();
  365. $sql_query = " SELECT DISTINCT db_name, table_name FROM " .
  366. PMA_Util::backquote($cfgRelation['db']) . "." .
  367. PMA_Util::backquote($cfgRelation['tracking']) .
  368. " WHERE db_name = '" . PMA_Util::sqlAddSlashes($GLOBALS['db']) . "' " .
  369. " ORDER BY db_name, table_name";
  370. return PMA_queryAsControlUser($sql_query);
  371. }
  372. /**
  373. * Function to get html for selectable table rows
  374. *
  375. * @param array $selectable_tables_sql_result sql results for selectable rows
  376. * @param string $url_query url query
  377. *
  378. * @return string
  379. */
  380. function PMA_getHtmlForSelectableTables($selectable_tables_sql_result, $url_query)
  381. {
  382. $html = '<form method="post" action="tbl_tracking.php' . $url_query . '">';
  383. $html .= '<select name="table" class="autosubmit">';
  384. while ($entries = $GLOBALS['dbi']->fetchArray($selectable_tables_sql_result)) {
  385. if (PMA_Tracker::isTracked($entries['db_name'], $entries['table_name'])) {
  386. $status = ' (' . __('active') . ')';
  387. } else {
  388. $status = ' (' . __('not active') . ')';
  389. }
  390. if ($entries['table_name'] == $_REQUEST['table']) {
  391. $s = ' selected="selected"';
  392. } else {
  393. $s = '';
  394. }
  395. $html .= '<option value="' . htmlspecialchars($entries['table_name'])
  396. . '"' . $s . '>' . htmlspecialchars($entries['db_name']) . ' . '
  397. . htmlspecialchars($entries['table_name']) . $status . '</option>'
  398. . "\n";
  399. }
  400. $html .= '</select>';
  401. $html .= '<input type="hidden" name="show_versions_submit" value="1" />';
  402. $html .= '</form>';
  403. return $html;
  404. }
  405. /**
  406. * Function to get html for tracking report and tracking report export
  407. *
  408. * @param string $url_query url query
  409. * @param array $data data
  410. * @param array $url_params url params
  411. * @param boolean $selection_schema selection schema
  412. * @param boolean $selection_data selection data
  413. * @param boolean $selection_both selection both
  414. * @param int $filter_ts_to filter time stamp from
  415. * @param int $filter_ts_from filter time stamp tp
  416. * @param array $filter_users filter users
  417. *
  418. * @return string
  419. */
  420. function PMA_getHtmlForTrackingReport($url_query, $data, $url_params,
  421. $selection_schema, $selection_data, $selection_both, $filter_ts_to,
  422. $filter_ts_from, $filter_users
  423. ) {
  424. $html = '<h3>' . __('Tracking report')
  425. . ' [<a href="tbl_tracking.php' . $url_query . '">' . __('Close')
  426. . '</a>]</h3>';
  427. $html .= '<small>' . __('Tracking statements') . ' '
  428. . htmlspecialchars($data['tracking']) . '</small><br/>';
  429. $html .= '<br/>';
  430. list($str1, $str2, $str3, $str4, $str5) = PMA_getHtmlForElementsOfTrackingReport(
  431. $selection_schema, $selection_data, $selection_both
  432. );
  433. // Prepare delete link content here
  434. $drop_image_or_text = '';
  435. if (PMA_Util::showIcons('ActionLinksMode')) {
  436. $drop_image_or_text .= PMA_Util::getImage(
  437. 'b_drop.png', __('Delete tracking data row from report')
  438. );
  439. }
  440. if (PMA_Util::showText('ActionLinksMode')) {
  441. $drop_image_or_text .= __('Delete');
  442. }
  443. /*
  444. * First, list tracked data definition statements
  445. */
  446. if (count($data['ddlog']) == 0 && count($data['dmlog']) == 0) {
  447. $msg = PMA_Message::notice(__('No data'));
  448. $msg->display();
  449. }
  450. $html .= PMA_getHtmlForTrackingReportExportForm1(
  451. $data, $url_params, $selection_schema, $selection_data, $selection_both,
  452. $filter_ts_to, $filter_ts_from, $filter_users, $str1, $str2, $str3,
  453. $str4, $str5, $drop_image_or_text
  454. );
  455. $html .= PMA_getHtmlForTrackingReportExportForm2(
  456. $url_params, $str1, $str2, $str3, $str4, $str5
  457. );
  458. $html .= "<br/><br/><hr/><br/>\n";
  459. return $html;
  460. }
  461. /**
  462. * Generate HTML element for report form
  463. *
  464. * @param boolean $selection_schema selection schema
  465. * @param boolean $selection_data selection data
  466. * @param boolean $selection_both selection both
  467. *
  468. * @return array
  469. */
  470. function PMA_getHtmlForElementsOfTrackingReport(
  471. $selection_schema, $selection_data, $selection_both
  472. ) {
  473. $str1 = '<select name="logtype">'
  474. . '<option value="schema"'
  475. . ($selection_schema ? ' selected="selected"' : '') . '>'
  476. . __('Structure only') . '</option>'
  477. . '<option value="data"'
  478. . ($selection_data ? ' selected="selected"' : '') . '>'
  479. . __('Data only') . '</option>'
  480. . '<option value="schema_and_data"'
  481. . ($selection_both ? ' selected="selected"' : '') . '>'
  482. . __('Structure and data') . '</option>'
  483. . '</select>';
  484. $str2 = '<input type="text" name="date_from" value="'
  485. . htmlspecialchars($_REQUEST['date_from']) . '" size="19" />';
  486. $str3 = '<input type="text" name="date_to" value="'
  487. . htmlspecialchars($_REQUEST['date_to']) . '" size="19" />';
  488. $str4 = '<input type="text" name="users" value="'
  489. . htmlspecialchars($_REQUEST['users']) . '" />';
  490. $str5 = '<input type="hidden" name="list_report" value="1" />'
  491. . '<input type="submit" value="' . __('Go') . '" />';
  492. return array($str1, $str2, $str3, $str4, $str5);
  493. }
  494. /**
  495. * Generate HTML for export form
  496. *
  497. * @param array $data data
  498. * @param array $url_params url params
  499. * @param boolean $selection_schema selection schema
  500. * @param boolean $selection_data selection data
  501. * @param boolean $selection_both selection both
  502. * @param int $filter_ts_to filter time stamp from
  503. * @param int $filter_ts_from filter time stamp tp
  504. * @param array $filter_users filter users
  505. * @param string $str1 HTML for logtype select
  506. * @param string $str2 HTML for "from date"
  507. * @param string $str3 HTML for "to date"
  508. * @param string $str4 HTML for user
  509. * @param string $str5 HTML for "list report"
  510. * @param string $drop_image_or_text HTML for image or text
  511. *
  512. * @return string HTML for form
  513. */
  514. function PMA_getHtmlForTrackingReportExportForm1(
  515. $data, $url_params, $selection_schema, $selection_data, $selection_both,
  516. $filter_ts_to, $filter_ts_from, $filter_users, $str1, $str2, $str3,
  517. $str4, $str5, $drop_image_or_text
  518. ) {
  519. $ddlog_count = 0;
  520. $html = '<form method="post" action="tbl_tracking.php'
  521. . PMA_URL_getCommon(
  522. $url_params + array(
  523. 'report' => 'true', 'version' => $_REQUEST['version']
  524. )
  525. )
  526. . '">';
  527. $html .= sprintf(
  528. __('Show %1$s with dates from %2$s to %3$s by user %4$s %5$s'),
  529. $str1, $str2, $str3, $str4, $str5
  530. );
  531. if ($selection_schema || $selection_both && count($data['ddlog']) > 0) {
  532. list($temp, $ddlog_count) = PMA_getHtmlForDataDefinitionStatements(
  533. $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
  534. $drop_image_or_text
  535. );
  536. $html .= $temp;
  537. unset($temp);
  538. } //endif
  539. /*
  540. * Secondly, list tracked data manipulation statements
  541. */
  542. if (($selection_data || $selection_both) && count($data['dmlog']) > 0) {
  543. $html .= PMA_getHtmlForDataManipulationStatements(
  544. $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
  545. $ddlog_count, $drop_image_or_text
  546. );
  547. }
  548. $html .= '</form>';
  549. return $html;
  550. }
  551. /**
  552. * Generate HTML for export form
  553. *
  554. * @param array $url_params Parameters
  555. * @param string $str1 HTML for logtype select
  556. * @param string $str2 HTML for "from date"
  557. * @param string $str3 HTML for "to date"
  558. * @param string $str4 HTML for user
  559. * @param string $str5 HTML for "list report"
  560. *
  561. * @return string HTML for form
  562. */
  563. function PMA_getHtmlForTrackingReportExportForm2(
  564. $url_params, $str1, $str2, $str3, $str4, $str5
  565. ) {
  566. $html = '<form method="post" action="tbl_tracking.php'
  567. . PMA_URL_getCommon(
  568. $url_params + array(
  569. 'report' => 'true', 'version' => $_REQUEST['version']
  570. )
  571. )
  572. . '">';
  573. $html .= sprintf(
  574. __('Show %1$s with dates from %2$s to %3$s by user %4$s %5$s'),
  575. $str1, $str2, $str3, $str4, $str5
  576. );
  577. $html .= '</form>';
  578. $html .= '<form class="disableAjax" method="post" action="tbl_tracking.php'
  579. . PMA_URL_getCommon(
  580. $url_params
  581. + array('report' => 'true', 'version' => $_REQUEST['version'])
  582. )
  583. . '">';
  584. $html .= '<input type="hidden" name="logtype" value="'
  585. . htmlspecialchars($_REQUEST['logtype']) . '" />';
  586. $html .= '<input type="hidden" name="date_from" value="'
  587. . htmlspecialchars($_REQUEST['date_from']) . '" />';
  588. $html .= '<input type="hidden" name="date_to" value="'
  589. . htmlspecialchars($_REQUEST['date_to']) . '" />';
  590. $html .= '<input type="hidden" name="users" value="'
  591. . htmlspecialchars($_REQUEST['users']) . '" />';
  592. $str_export1 = '<select name="export_type">'
  593. . '<option value="sqldumpfile">' . __('SQL dump (file download)')
  594. . '</option>'
  595. . '<option value="sqldump">' . __('SQL dump') . '</option>'
  596. . '<option value="execution" onclick="alert(\''
  597. . PMA_escapeJsString(
  598. __('This option will replace your table and contained data.')
  599. )
  600. . '\')">' . __('SQL execution') . '</option>' . '</select>';
  601. $str_export2 = '<input type="hidden" name="report_export" value="1" />'
  602. . '<input type="submit" value="' . __('Go') . '" />';
  603. $html .= "<br/>" . sprintf(__('Export as %s'), $str_export1)
  604. . $str_export2 . "<br/>";
  605. $html .= '</form>';
  606. return $html;
  607. }
  608. /**
  609. * Function to get html for data manipulation statements
  610. *
  611. * @param array $data data
  612. * @param array $filter_users filter users
  613. * @param int $filter_ts_from filter time staml from
  614. * @param int $filter_ts_to filter time stamp to
  615. * @param array $url_params url parameters
  616. * @param int $ddlog_count data definition log count
  617. * @param string $drop_image_or_text drop image or text
  618. *
  619. * @return string
  620. */
  621. function PMA_getHtmlForDataManipulationStatements($data, $filter_users,
  622. $filter_ts_from, $filter_ts_to, $url_params, $ddlog_count,
  623. $drop_image_or_text
  624. ) {
  625. // no need for the secondth returned parameter
  626. list($html,) = PMA_getHtmlForDataStatements(
  627. $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
  628. $drop_image_or_text, 'dmlog', __('Data manipulation statement'),
  629. $ddlog_count, 'dml_versions'
  630. );
  631. return $html;
  632. }
  633. /**
  634. * Function to get html for one data manipulation statement
  635. *
  636. * @param array $entry entry
  637. * @param array $filter_users filter users
  638. * @param int $filter_ts_from filter time stamp from
  639. * @param int $filter_ts_to filter time stamp to
  640. * @param string $style style
  641. * @param int $line_number line number
  642. * @param array $url_params url parameters
  643. * @param int $offset line number offset
  644. * @param string $drop_image_or_text drop image or text
  645. * @param string $delete_param parameter for delete
  646. *
  647. * @return string
  648. */
  649. function PMA_getHtmlForOneStatement($entry, $filter_users,
  650. $filter_ts_from, $filter_ts_to, $style, $line_number, $url_params, $offset,
  651. $drop_image_or_text, $delete_param
  652. ) {
  653. $statement = PMA_Util::formatSql($entry['statement'], true);
  654. $timestamp = strtotime($entry['date']);
  655. $filtered_user = in_array($entry['username'], $filter_users);
  656. $html = null;
  657. if ($timestamp >= $filter_ts_from
  658. && $timestamp <= $filter_ts_to
  659. && (in_array('*', $filter_users) || $filtered_user)
  660. ) {
  661. $html = '<tr class="noclick ' . $style . '">';
  662. $html .= '<td class="right"><small>' . $line_number . '</small></td>';
  663. $html .= '<td><small>'
  664. . htmlspecialchars($entry['date']) . '</small></td>';
  665. $html .= '<td><small>'
  666. . htmlspecialchars($entry['username']) . '</small></td>';
  667. $html .= '<td>' . $statement . '</td>';
  668. $html .= '<td class="nowrap"><a class="delete_entry_anchor ajax"'
  669. . ' href="tbl_tracking.php'
  670. . PMA_URL_getCommon(
  671. $url_params + array(
  672. 'report' => 'true',
  673. 'version' => $_REQUEST['version'],
  674. $delete_param => ($line_number - $offset),
  675. )
  676. )
  677. . '">'
  678. . $drop_image_or_text
  679. . '</a></td>';
  680. $html .= '</tr>';
  681. }
  682. return $html;
  683. }
  684. /**
  685. * Function to get html for data definition statements in schema snapshot
  686. *
  687. * @param array $data data
  688. * @param array $filter_users filter users
  689. * @param int $filter_ts_from filter time stamp from
  690. * @param int $filter_ts_to filter time stamp to
  691. * @param array $url_params url parameters
  692. * @param string $drop_image_or_text drop image or text
  693. *
  694. * @return array
  695. */
  696. function PMA_getHtmlForDataDefinitionStatements($data, $filter_users,
  697. $filter_ts_from, $filter_ts_to, $url_params, $drop_image_or_text
  698. ) {
  699. list($html, $line_number) = PMA_getHtmlForDataStatements(
  700. $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
  701. $drop_image_or_text, 'ddlog', __('Data definition statement'),
  702. 1, 'ddl_versions'
  703. );
  704. return array($html, $line_number);
  705. }
  706. /**
  707. * Function to get html for data statements in schema snapshot
  708. *
  709. * @param array $data data
  710. * @param array $filter_users filter users
  711. * @param int $filter_ts_from filter time stamp from
  712. * @param int $filter_ts_to filter time stamp to
  713. * @param array $url_params url parameters
  714. * @param string $drop_image_or_text drop image or text
  715. * @param string $which_log dmlog|ddlog
  716. * @param string $header_message message for this section
  717. * @param int $line_number line number
  718. * @param string $table_id id for the table element
  719. *
  720. * @return array
  721. */
  722. function PMA_getHtmlForDataStatements($data, $filter_users,
  723. $filter_ts_from, $filter_ts_to, $url_params, $drop_image_or_text,
  724. $which_log, $header_message, $line_number, $table_id
  725. ) {
  726. $offset = $line_number;
  727. $html = '<table id="' . $table_id . '" class="data" width="100%">';
  728. $html .= '<thead>';
  729. $html .= '<tr>';
  730. $html .= '<th width="18">#</th>';
  731. $html .= '<th width="100">' . __('Date') . '</th>';
  732. $html .= '<th width="60">' . __('Username') . '</th>';
  733. $html .= '<th>' . $header_message . '</th>';
  734. $html .= '<th>' . __('Action') . '</th>';
  735. $html .= '</tr>';
  736. $html .= '</thead>';
  737. $html .= '<tbody>';
  738. $style = 'odd';
  739. foreach ($data[$which_log] as $entry) {
  740. $html .= PMA_getHtmlForOneStatement(
  741. $entry, $filter_users, $filter_ts_from, $filter_ts_to, $style,
  742. $line_number, $url_params, $offset, $drop_image_or_text,
  743. 'delete_' . $which_log
  744. );
  745. if ($style == 'even') {
  746. $style = 'odd';
  747. } else {
  748. $style = 'even';
  749. }
  750. $line_number++;
  751. }
  752. $html .= '</tbody>';
  753. $html .= '</table>';
  754. return array($html, $line_number);
  755. }
  756. /**
  757. * Function to get html for schema snapshot
  758. *
  759. * @param string $url_query url query
  760. *
  761. * @return string
  762. */
  763. function PMA_getHtmlForSchemaSnapshot($url_query)
  764. {
  765. $html = '<h3>' . __('Structure snapshot')
  766. . ' [<a href="tbl_tracking.php' . $url_query . '">' . __('Close')
  767. . '</a>]</h3>';
  768. $data = PMA_Tracker::getTrackedData(
  769. $_REQUEST['db'], $_REQUEST['table'], $_REQUEST['version']
  770. );
  771. // Get first DROP TABLE/VIEW and CREATE TABLE/VIEW statements
  772. $drop_create_statements = $data['ddlog'][0]['statement'];
  773. if (/*overload*/mb_strstr($data['ddlog'][0]['statement'], 'DROP TABLE')
  774. || /*overload*/mb_strstr($data['ddlog'][0]['statement'], 'DROP VIEW')
  775. ) {
  776. $drop_create_statements .= $data['ddlog'][1]['statement'];
  777. }
  778. // Print SQL code
  779. $html .= PMA_Util::getMessage(
  780. sprintf(
  781. __('Version %s snapshot (SQL code)'),
  782. htmlspecialchars($_REQUEST['version'])
  783. ),
  784. $drop_create_statements
  785. );
  786. // Unserialize snapshot
  787. $temp = unserialize($data['schema_snapshot']);
  788. $columns = $temp['COLUMNS'];
  789. $indexes = $temp['INDEXES'];
  790. $html .= PMA_getHtmlForColumns($columns);
  791. if (count($indexes) > 0) {
  792. $html .= PMA_getHtmlForIndexes($indexes);
  793. } // endif
  794. $html .= '<br /><hr /><br />';
  795. return $html;
  796. }
  797. /**
  798. * Function to get html for displaying columns in the schema snapshot
  799. *
  800. * @param array $columns columns
  801. *
  802. * @return string
  803. */
  804. function PMA_getHtmlForColumns($columns)
  805. {
  806. $html = '<h3>' . __('Structure') . '</h3>';
  807. $html .= '<table id="tablestructure" class="data">';
  808. $html .= '<thead>';
  809. $html .= '<tr>';
  810. $html .= '<th>' . __('#') . '</th>';
  811. $html .= '<th>' . __('Column') . '</th>';
  812. $html .= '<th>' . __('Type') . '</th>';
  813. $html .= '<th>' . __('Collation') . '</th>';
  814. $html .= '<th>' . __('Null') . '</th>';
  815. $html .= '<th>' . __('Default') . '</th>';
  816. $html .= '<th>' . __('Extra') . '</th>';
  817. $html .= '<th>' . __('Comment') . '</th>';
  818. $html .= '</tr>';
  819. $html .= '</thead>';
  820. $html .= '<tbody>';
  821. $style = 'odd';
  822. $index = 1;
  823. foreach ($columns as $field) {
  824. $html .= PMA_getHtmlForField($index++, $field, $style);
  825. if ($style == 'even') {
  826. $style = 'odd';
  827. } else {
  828. $style = 'even';
  829. }
  830. }
  831. $html .= '</tbody>';
  832. $html .= '</table>';
  833. return $html;
  834. }
  835. /**
  836. * Function to get html for field
  837. *
  838. * @param int $index index
  839. * @param array $field field
  840. * @param string $style style
  841. *
  842. * @return string
  843. */
  844. function PMA_getHtmlForField($index, $field, $style)
  845. {
  846. $html = '<tr class="noclick ' . $style . '">';
  847. $html .= '<td>' . $index . '</td>';
  848. $html .= '<td><b>' . htmlspecialchars($field['Field']);
  849. if ($field['Key'] == 'PRI') {
  850. $html .= ' ' . PMA_Util::getImage(
  851. 'b_primary.png', __('Primary')
  852. );
  853. } elseif (! empty($field['Key'])) {
  854. $html .= ' ' . PMA_Util::getImage(
  855. 'bd_primary.png', __('Index')
  856. );
  857. }
  858. $html .= '</b></td>';
  859. $html .= "\n";
  860. $html .= '<td>' . htmlspecialchars($field['Type']) . '</td>';
  861. $html .= '<td>' . htmlspecialchars($field['Collation']) . '</td>';
  862. $html .= '<td>' . (($field['Null'] == 'YES') ? __('Yes') : __('No')) . '</td>';
  863. $html .= '<td>';
  864. if (isset($field['Default'])) {
  865. $extracted_columnspec = PMA_Util::extractColumnSpec($field['Type']);
  866. if ($extracted_columnspec['type'] == 'bit') {
  867. // here, $field['Default'] contains something like b'010'
  868. $html .= PMA_Util::convertBitDefaultValue($field['Default']);
  869. } else {
  870. $html .= htmlspecialchars($field['Default']);
  871. }
  872. } else {
  873. if ($field['Null'] == 'YES') {
  874. $html .= '<i>NULL</i>';
  875. } else {
  876. $html .= '<i>' . _pgettext('None for default', 'None') . '</i>';
  877. }
  878. }
  879. $html .= '</td>';
  880. $html .= '<td>' . htmlspecialchars($field['Extra']) . '</td>';
  881. $html .= '<td>' . htmlspecialchars($field['Comment']) . '</td>';
  882. $html .= '</tr>';
  883. return $html;
  884. }
  885. /**
  886. * Function to get html for the indexes in schema snapshot
  887. *
  888. * @param array $indexes indexes
  889. *
  890. * @return string
  891. */
  892. function PMA_getHtmlForIndexes($indexes)
  893. {
  894. $html = '<h3>' . __('Indexes') . '</h3>';
  895. $html .= '<table id="tablestructure_indexes" class="data">';
  896. $html .= '<thead>';
  897. $html .= '<tr>';
  898. $html .= '<th>' . __('Keyname') . '</th>';
  899. $html .= '<th>' . __('Type') . '</th>';
  900. $html .= '<th>' . __('Unique') . '</th>';
  901. $html .= '<th>' . __('Packed') . '</th>';
  902. $html .= '<th>' . __('Column') . '</th>';
  903. $html .= '<th>' . __('Cardinality') . '</th>';
  904. $html .= '<th>' . __('Collation') . '</th>';
  905. $html .= '<th>' . __('Null') . '</th>';
  906. $html .= '<th>' . __('Comment') . '</th>';
  907. $html .= '</tr>';
  908. $html .= '<tbody>';
  909. $style = 'odd';
  910. foreach ($indexes as $index) {
  911. $html .= PMA_getHtmlForIndex($index, $style);
  912. if ($style == 'even') {
  913. $style = 'odd';
  914. } else {
  915. $style = 'even';
  916. }
  917. }
  918. $html .= '</tbody>';
  919. $html .= '</table>';
  920. return $html;
  921. }
  922. /**
  923. * Function to get html for an index in schema snapshot
  924. *
  925. * @param array $index index
  926. * @param string $style style
  927. *
  928. * @return string
  929. */
  930. function PMA_getHtmlForIndex($index, $style)
  931. {
  932. if ($index['Non_unique'] == 0) {
  933. $str_unique = __('Yes');
  934. } else {
  935. $str_unique = __('No');
  936. }
  937. if ($index['Packed'] != '') {
  938. $str_packed = __('Yes');
  939. } else {
  940. $str_packed = __('No');
  941. }
  942. $html = '<tr class="noclick ' . $style . '">';
  943. $html .= '<td><b>' . htmlspecialchars($index['Key_name']) . '</b></td>';
  944. $html .= '<td>' . htmlspecialchars($index['Index_type']) . '</td>';
  945. $html .= '<td>' . $str_unique . '</td>';
  946. $html .= '<td>' . $str_packed . '</td>';
  947. $html .= '<td>' . htmlspecialchars($index['Column_name']) . '</td>';
  948. $html .= '<td>' . htmlspecialchars($index['Cardinality']) . '</td>';
  949. $html .= '<td>' . htmlspecialchars($index['Collation']) . '</td>';
  950. $html .= '<td>' . htmlspecialchars($index['Null']) . '</td>';
  951. $html .= '<td>' . htmlspecialchars($index['Comment']) . '</td>';
  952. $html .= '</tr>';
  953. return $html;
  954. }
  955. /**
  956. * Function to handle the tracking report
  957. *
  958. * @param array &$data tracked data
  959. *
  960. * @return string HTML for the message
  961. */
  962. function PMA_deleteTrackingReportRows(&$data)
  963. {
  964. $html = '';
  965. if (isset($_REQUEST['delete_ddlog'])) {
  966. // Delete ddlog row data
  967. $html .= PMA_deleteFromTrackingReportLog(
  968. $data,
  969. 'ddlog',
  970. 'DDL',
  971. __('Tracking data definition successfully deleted')
  972. );
  973. }
  974. if (isset($_REQUEST['delete_dmlog'])) {
  975. // Delete dmlog row data
  976. $html .= PMA_deleteFromTrackingReportLog(
  977. $data,
  978. 'dmlog',
  979. 'DML',
  980. __('Tracking data manipulation successfully deleted')
  981. );
  982. }
  983. return $html;
  984. }
  985. /**
  986. * Function to delete from a tracking report log
  987. *
  988. * @param array &$data tracked data
  989. * @param string $which_log ddlog|dmlog
  990. * @param string $type DDL|DML
  991. * @param string $message success message
  992. *
  993. * @return string HTML for the message
  994. */
  995. function PMA_deleteFromTrackingReportLog(&$data, $which_log, $type, $message)
  996. {
  997. $html = '';
  998. $delete_id = $_REQUEST['delete_' . $which_log];
  999. // Only in case of valid id
  1000. if ($delete_id == (int)$delete_id) {
  1001. unset($data[$which_log][$delete_id]);
  1002. $successfullyDeleted = PMA_Tracker::changeTrackingData(
  1003. $_REQUEST['db'],
  1004. $_REQUEST['table'],
  1005. $_REQUEST['version'],
  1006. $type,
  1007. $data[$which_log]
  1008. );
  1009. if ($successfullyDeleted) {
  1010. $msg = PMA_Message::success($message);
  1011. } else {
  1012. $msg = PMA_Message::rawError(__('Query error'));
  1013. }
  1014. $html .= $msg->getDisplay();
  1015. }
  1016. return $html;
  1017. }
  1018. /**
  1019. * Function to export as sql dump
  1020. *
  1021. * @param array $entries entries
  1022. *
  1023. * @return string HTML SQL query form
  1024. */
  1025. function PMA_exportAsSQLDump($entries)
  1026. {
  1027. $html = '';
  1028. $new_query = "# "
  1029. . __(
  1030. 'You can execute the dump by creating and using a temporary database. '
  1031. . 'Please ensure that you have the privileges to do so.'
  1032. )
  1033. . "\n"
  1034. . "# " . __('Comment out these two lines if you do not need them.') . "\n"
  1035. . "\n"
  1036. . "CREATE database IF NOT EXISTS pma_temp_db; \n"
  1037. . "USE pma_temp_db; \n"
  1038. . "\n";
  1039. foreach ($entries as $entry) {
  1040. $new_query .= $entry['statement'];
  1041. }
  1042. $msg = PMA_Message::success(
  1043. __('SQL statements exported. Please copy the dump or execute it.')
  1044. );
  1045. $html .= $msg->getDisplay();
  1046. $db_temp = $GLOBALS['db'];
  1047. $table_temp = $GLOBALS['table'];
  1048. $GLOBALS['db'] = $GLOBALS['table'] = '';
  1049. include_once './libraries/sql_query_form.lib.php';
  1050. $html .= PMA_getHtmlForSqlQueryForm($new_query, 'sql');
  1051. $GLOBALS['db'] = $db_temp;
  1052. $GLOBALS['table'] = $table_temp;
  1053. return $html;
  1054. }
  1055. /**
  1056. * Function to export as sql execution
  1057. *
  1058. * @param array $entries entries
  1059. *
  1060. * @return array
  1061. */
  1062. function PMA_exportAsSQLExecution($entries)
  1063. {
  1064. $sql_result = array();
  1065. foreach ($entries as $entry) {
  1066. $sql_result = $GLOBALS['dbi']->query("/*NOTRACK*/\n" . $entry['statement']);
  1067. }
  1068. return $sql_result;
  1069. }
  1070. /**
  1071. * Function to export as entries
  1072. *
  1073. * @param array $entries entries
  1074. *
  1075. * @return void
  1076. */
  1077. function PMA_exportAsFileDownload($entries)
  1078. {
  1079. @ini_set('url_rewriter.tags', '');
  1080. $dump = "# " . sprintf(
  1081. __('Tracking report for table `%s`'), htmlspecialchars($_REQUEST['table'])
  1082. )
  1083. . "\n" . "# " . date('Y-m-d H:i:s') . "\n";
  1084. foreach ($entries as $entry) {
  1085. $dump .= $entry['statement'];
  1086. }
  1087. $filename = 'log_' . htmlspecialchars($_REQUEST['table']) . '.sql';
  1088. PMA_Response::getInstance()->disable();
  1089. PMA_downloadHeader(
  1090. $filename,
  1091. 'text/x-sql',
  1092. /*overload*/mb_strlen($dump)
  1093. );
  1094. echo $dump;
  1095. exit();
  1096. }
  1097. /**
  1098. * Function to activate tracking
  1099. *
  1100. * @return string HTML for the success message
  1101. */
  1102. function PMA_activateTracking()
  1103. {
  1104. $html = '';
  1105. $activated = PMA_Tracker::activateTracking(
  1106. $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']
  1107. );
  1108. if ($activated) {
  1109. $msg = PMA_Message::success(
  1110. sprintf(
  1111. __('Tracking for %1$s was activated at version %2$s.'),
  1112. htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table']),
  1113. htmlspecialchars($_REQUEST['version'])
  1114. )
  1115. );
  1116. $html .= $msg->getDisplay();
  1117. }
  1118. return $html;
  1119. }
  1120. /**
  1121. * Function to deactivate tracking
  1122. *
  1123. * @return string HTML of the success message
  1124. */
  1125. function PMA_deactivateTracking()
  1126. {
  1127. $html = '';
  1128. $deactivated = PMA_Tracker::deactivateTracking(
  1129. $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']
  1130. );
  1131. if ($deactivated) {
  1132. $msg = PMA_Message::success(
  1133. sprintf(
  1134. __('Tracking for %1$s was deactivated at version %2$s.'),
  1135. htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table']),
  1136. htmlspecialchars($_REQUEST['version'])
  1137. )
  1138. );
  1139. $html .= $msg->getDisplay();
  1140. }
  1141. return $html;
  1142. }
  1143. /**
  1144. * Function to get tracking set
  1145. *
  1146. * @return string
  1147. */
  1148. function PMA_getTrackingSet()
  1149. {
  1150. $tracking_set = '';
  1151. // a key is absent from the request if it has been removed from
  1152. // tracking_default_statements in the config
  1153. if (isset($_REQUEST['alter_table']) && $_REQUEST['alter_table'] == true) {
  1154. $tracking_set .= 'ALTER TABLE,';
  1155. }
  1156. if (isset($_REQUEST['rename_table']) && $_REQUEST['rename_table'] == true) {
  1157. $tracking_set .= 'RENAME TABLE,';
  1158. }
  1159. if (isset($_REQUEST['create_table']) && $_REQUEST['create_table'] == true) {
  1160. $tracking_set .= 'CREATE TABLE,';
  1161. }
  1162. if (isset($_REQUEST['drop_table']) && $_REQUEST['drop_table'] == true) {
  1163. $tracking_set .= 'DROP TABLE,';
  1164. }
  1165. if (isset($_REQUEST['alter_view']) && $_REQUEST['alter_view'] == true) {
  1166. $tracking_set .= 'ALTER VIEW,';
  1167. }
  1168. if (isset($_REQUEST['create_view']) && $_REQUEST['create_view'] == true) {
  1169. $tracking_set .= 'CREATE VIEW,';
  1170. }
  1171. if (isset($_REQUEST['drop_view']) && $_REQUEST['drop_view'] == true) {
  1172. $tracking_set .= 'DROP VIEW,';
  1173. }
  1174. if (isset($_REQUEST['create_index']) && $_REQUEST['create_index'] == true) {
  1175. $tracking_set .= 'CREATE INDEX,';
  1176. }
  1177. if (isset($_REQUEST['drop_index']) && $_REQUEST['drop_index'] == true) {
  1178. $tracking_set .= 'DROP INDEX,';
  1179. }
  1180. if (isset($_REQUEST['insert']) && $_REQUEST['insert'] == true) {
  1181. $tracking_set .= 'INSERT,';
  1182. }
  1183. if (isset($_REQUEST['update']) && $_REQUEST['update'] == true) {
  1184. $tracking_set .= 'UPDATE,';
  1185. }
  1186. if (isset($_REQUEST['delete']) && $_REQUEST['delete'] == true) {
  1187. $tracking_set .= 'DELETE,';
  1188. }
  1189. if (isset($_REQUEST['truncate']) && $_REQUEST['truncate'] == true) {
  1190. $tracking_set .= 'TRUNCATE,';
  1191. }
  1192. $tracking_set = rtrim($tracking_set, ',');
  1193. return $tracking_set;
  1194. }
  1195. /**
  1196. * Deletes a tracking version
  1197. *
  1198. * @param string $version tracking version
  1199. *
  1200. * @return string HTML of the success message
  1201. */
  1202. function PMA_deleteTrackingVersion($version)
  1203. {
  1204. $html = '';
  1205. $versionDeleted = PMA_Tracker::deleteTracking(
  1206. $GLOBALS['db'],
  1207. $GLOBALS['table'],
  1208. $version
  1209. );
  1210. if ($versionDeleted) {
  1211. $msg = PMA_Message::success(
  1212. sprintf(
  1213. __('Version %1$s of %2$s was deleted.'),
  1214. htmlspecialchars($version),
  1215. htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
  1216. )
  1217. );
  1218. $html .= $msg->getDisplay();
  1219. }
  1220. return $html;
  1221. }
  1222. /**
  1223. * Function to create the tracking version
  1224. *
  1225. * @return string HTML of the success message
  1226. */
  1227. function PMA_createTrackingVersion()
  1228. {
  1229. $html = '';
  1230. $tracking_set = PMA_getTrackingSet();
  1231. $versionCreated = PMA_Tracker::createVersion(
  1232. $GLOBALS['db'],
  1233. $GLOBALS['table'],
  1234. $_REQUEST['version'],
  1235. $tracking_set,
  1236. $GLOBALS['dbi']->getTable($GLOBALS['db'], $GLOBALS['table'])->isView()
  1237. );
  1238. if ($versionCreated) {
  1239. $msg = PMA_Message::success(
  1240. sprintf(
  1241. __('Version %1$s was created, tracking for %2$s is active.'),
  1242. htmlspecialchars($_REQUEST['version']),
  1243. htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
  1244. )
  1245. );
  1246. $html .= $msg->getDisplay();
  1247. }
  1248. return $html;
  1249. }
  1250. /**
  1251. * Create tracking version for multiple tables
  1252. *
  1253. * @param array $selected list of selected tables
  1254. *
  1255. * @return void
  1256. */
  1257. function PMA_createTrackingForMultipleTables($selected)
  1258. {
  1259. $tracking_set = PMA_getTrackingSet();
  1260. foreach ($selected as $selected_table) {
  1261. PMA_Tracker::createVersion(
  1262. $GLOBALS['db'],
  1263. $selected_table,
  1264. $_REQUEST['version'],
  1265. $tracking_set,
  1266. $GLOBALS['dbi']->getTable($GLOBALS['db'], $selected_table)->isView()
  1267. );
  1268. }
  1269. }
  1270. /**
  1271. * Function to get the entries
  1272. *
  1273. * @param array $data data
  1274. * @param int $filter_ts_from filter time stamp from
  1275. * @param int $filter_ts_to filter time stamp to
  1276. * @param array $filter_users filter users
  1277. *
  1278. * @return array
  1279. */
  1280. function PMA_getEntries($data, $filter_ts_from, $filter_ts_to, $filter_users)
  1281. {
  1282. $entries = array();
  1283. // Filtering data definition statements
  1284. if ($_REQUEST['logtype'] == 'schema'
  1285. || $_REQUEST['logtype'] == 'schema_and_data'
  1286. ) {
  1287. $entries = array_merge(
  1288. $entries,
  1289. PMA_filterTracking(
  1290. $data['ddlog'], $filter_ts_from, $filter_ts_to, $filter_users
  1291. )
  1292. );
  1293. }
  1294. // Filtering data manipulation statements
  1295. if ($_REQUEST['logtype'] == 'data'
  1296. || $_REQUEST['logtype'] == 'schema_and_data'
  1297. ) {
  1298. $entries = array_merge(
  1299. $entries,
  1300. PMA_filterTracking(
  1301. $data['dmlog'], $filter_ts_from, $filter_ts_to, $filter_users
  1302. )
  1303. );
  1304. }
  1305. // Sort it
  1306. $ids = $timestamps = $usernames = $statements = array();
  1307. foreach ($entries as $key => $row) {
  1308. $ids[$key] = $row['id'];
  1309. $timestamps[$key] = $row['timestamp'];
  1310. $usernames[$key] = $row['username'];
  1311. $statements[$key] = $row['statement'];
  1312. }
  1313. array_multisort(
  1314. $timestamps, SORT_ASC, $ids, SORT_ASC, $usernames,
  1315. SORT_ASC, $statements, SORT_ASC, $entries
  1316. );
  1317. return $entries;
  1318. }
  1319. /**
  1320. * Function to get version status
  1321. *
  1322. * @param array $version version info
  1323. *
  1324. * @return string $version_status The status message
  1325. */
  1326. function PMA_getVersionStatus($version)
  1327. {
  1328. if ($version['tracking_active'] == 1) {
  1329. return __('active');
  1330. } else {
  1331. return __('not active');
  1332. }
  1333. }
  1334. /**
  1335. * Display untracked tables
  1336. *
  1337. * @param string $db current database
  1338. * @param array $untracked_tables untracked tables
  1339. * @param string $url_query url query string
  1340. * @param string $pmaThemeImage path to theme's image folder
  1341. * @param string $text_dir text direction
  1342. *
  1343. * @return void
  1344. */
  1345. function PMA_displayUntrackedTables(
  1346. $db, $untracked_tables, $url_query, $pmaThemeImage, $text_dir
  1347. ) {
  1348. ?>
  1349. <h3><?php echo __('Untracked tables');?></h3>
  1350. <form method="post" action="db_tracking.php" name="untrackedForm"
  1351. id="untrackedForm" class="ajax">
  1352. <?php
  1353. echo PMA_URL_getHiddenInputs($db)
  1354. ?>
  1355. <table id="noversions" class="data">
  1356. <thead>
  1357. <tr>
  1358. <th></th>
  1359. <th style="width: 300px"><?php echo __('Table');?></th>
  1360. <th><?php echo __('Action');?></th>
  1361. </tr>
  1362. </thead>
  1363. <tbody>
  1364. <?php
  1365. // Print out list of untracked tables
  1366. $style = 'odd';
  1367. foreach ($untracked_tables as $key => $tablename) {
  1368. $style = PMA_displayOneUntrackedTable($db, $tablename, $url_query, $style);
  1369. }
  1370. ?>
  1371. </tbody>
  1372. </table>
  1373. <?php
  1374. echo PMA_Util::getWithSelected($pmaThemeImage, $text_dir, "untrackedForm");
  1375. echo PMA_Util::getButtonOrImage(
  1376. 'submit_mult', 'mult_submit', 'submit_mult_track',
  1377. __('Track table'), 'eye.png', 'track'
  1378. );
  1379. ?>
  1380. </form>
  1381. <?php
  1382. }
  1383. /**
  1384. * Display one untracked table
  1385. *
  1386. * @param string $db current database
  1387. * @param string $tablename the table name for which to display a line
  1388. * @param string $url_query url query string
  1389. * @param string $style odd|even
  1390. *
  1391. * @return string $style changed style (even|odd)
  1392. */
  1393. function PMA_displayOneUntrackedTable($db, $tablename, $url_query, $style)
  1394. {
  1395. $checkbox_id = "selected_tbl_"
  1396. . htmlspecialchars($tablename);
  1397. if (PMA_Tracker::getVersion($db, $tablename) == -1) {
  1398. $my_link = '<a href="tbl_tracking.php' . $url_query
  1399. . '&amp;table=' . htmlspecialchars($tablename) . '">';
  1400. $my_link .= PMA_Util::getIcon('eye.png', __('Track table'));
  1401. $my_link .= '</a>';
  1402. ?>
  1403. <tr class="<?php echo $style;?>">
  1404. <td class="center">
  1405. <input type="checkbox" name="selected_tbl[]"
  1406. class="checkall" id="<?php echo $checkbox_id;?>"
  1407. value="<?php echo htmlspecialchars($tablename);?>"/>
  1408. </td>
  1409. <th>
  1410. <label for="<?php echo $checkbox_id;?>">
  1411. <?php echo htmlspecialchars($tablename);?>
  1412. </label>
  1413. </th>
  1414. <td><?php echo $my_link;?></td>
  1415. </tr>
  1416. <?php
  1417. if ($style == 'even') {
  1418. $style = 'odd';
  1419. } else {
  1420. $style = 'even';
  1421. }
  1422. }
  1423. return $style;
  1424. }
  1425. /**
  1426. * Get untracked tables
  1427. *
  1428. * @param string $db current database
  1429. *
  1430. * @return array $untracked_tables
  1431. */
  1432. function PMA_getUntrackedTables($db)
  1433. {
  1434. $untracked_tables = array();
  1435. $sep = $GLOBALS['cfg']['NavigationTreeTableSeparator'];
  1436. // Get list of tables
  1437. $table_list = PMA_Util::getTableList($db);
  1438. // For each table try to get the tracking version
  1439. foreach ($table_list as $key => $value) {
  1440. // If $value is a table group.
  1441. if (array_key_exists(('is' . $sep . 'group'), $value)
  1442. && $value['is' . $sep . 'group']
  1443. ) {
  1444. foreach ($value as $temp_table) {
  1445. // If $temp_table is a table with the value for 'Name' is set,
  1446. // rather than a property of the table group.
  1447. if (is_array($temp_table)
  1448. && array_key_exists('Name', $temp_table)
  1449. ) {
  1450. $tracking_version = PMA_Tracker::getVersion(
  1451. $db,
  1452. $temp_table['Name']
  1453. );
  1454. if ($tracking_version == -1) {
  1455. $untracked_tables[] = $temp_table['Name'];
  1456. }
  1457. }
  1458. }
  1459. } else { // If $value is a table.
  1460. if (PMA_Tracker::getVersion($db, $value['Name']) == -1) {
  1461. $untracked_tables[] = $value['Name'];
  1462. }
  1463. }
  1464. }
  1465. return $untracked_tables;
  1466. }
  1467. /**
  1468. * Display tracked tables
  1469. *
  1470. * @param string $db current database
  1471. * @param object $all_tables_result result set of tracked tables
  1472. * @param string $url_query url query string
  1473. * @param string $pmaThemeImage path to theme's image folder
  1474. * @param string $text_dir text direction
  1475. * @param array $cfgRelation configuration storage info
  1476. *
  1477. * @return void
  1478. */
  1479. function PMA_displayTrackedTables(
  1480. $db, $all_tables_result, $url_query, $pmaThemeImage, $text_dir, $cfgRelation
  1481. ) {
  1482. ?>
  1483. <div id="tracked_tables">
  1484. <h3><?php echo __('Tracked tables');?></h3>
  1485. <form method="post" action="db_tracking.php" name="trackedForm"
  1486. id="trackedForm" class="ajax">
  1487. <?php
  1488. echo PMA_URL_getHiddenInputs($db)
  1489. ?>
  1490. <table id="versions" class="data">
  1491. <thead>
  1492. <tr>
  1493. <th></th>
  1494. <th><?php echo __('Table');?></th>
  1495. <th><?php echo __('Last version');?></th>
  1496. <th><?php echo __('Created');?></th>
  1497. <th><?php echo __('Updated');?></th>
  1498. <th><?php echo __('Status');?></th>
  1499. <th><?php echo __('Action');?></th>
  1500. <th><?php echo __('Show');?></th>
  1501. </tr>
  1502. </thead>
  1503. <tbody>
  1504. <?php
  1505. // Print out information about versions
  1506. $delete = PMA_Util::getIcon('b_drop.png', __('Delete tracking'));
  1507. $versions = PMA_Util::getIcon('b_versions.png', __('Versions'));
  1508. $report = PMA_Util::getIcon('b_report.png', __('Tracking report'));
  1509. $structure = PMA_Util::getIcon('b_props.png', __('Structure snapshot'));
  1510. $style = 'odd';
  1511. while ($one_result = $GLOBALS['dbi']->fetchArray($all_tables_result)) {
  1512. list($table_name, $version_number) = $one_result;
  1513. $table_query = ' SELECT * FROM ' .
  1514. PMA_Util::backquote($cfgRelation['db']) . '.' .
  1515. PMA_Util::backquote($cfgRelation['tracking']) .
  1516. ' WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($_REQUEST['db'])
  1517. . '\' AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table_name)
  1518. . '\' AND `version` = \'' . $version_number . '\'';
  1519. $table_result = PMA_queryAsControlUser($table_query);
  1520. $version_data = $GLOBALS['dbi']->fetchArray($table_result);
  1521. $tbl_link = 'tbl_tracking.php' . $url_query . '&amp;table='
  1522. . htmlspecialchars($version_data['table_name']);
  1523. $delete_link = 'db_tracking.php' . $url_query . '&amp;table='
  1524. . htmlspecialchars($version_data['table_name'])
  1525. . '&amp;delete_tracking=true&amp';
  1526. $checkbox_id = "selected_tbl_"
  1527. . htmlspecialchars($version_data['table_name']);
  1528. ?>
  1529. <tr class="<?php echo $style;?>">
  1530. <td class="center">
  1531. <input type="checkbox" name="selected_tbl[]"
  1532. class="checkall" id="<?php echo $checkbox_id;?>"
  1533. value="<?php echo htmlspecialchars($version_data['table_name']);?>"/>
  1534. </td>
  1535. <th>
  1536. <label for="<?php echo $checkbox_id;?>">
  1537. <?php echo htmlspecialchars($version_data['table_name']);?>
  1538. </label>
  1539. </th>
  1540. <td class="right"><?php echo $version_data['version'];?></td>
  1541. <td><?php echo $version_data['date_created'];?></td>
  1542. <td><?php echo $version_data['date_updated'];?></td>
  1543. <td>
  1544. <?php
  1545. PMA_displayStatusButton($version_data, $tbl_link);
  1546. ?>
  1547. </td>
  1548. <td>
  1549. <a class="delete_tracking_anchor ajax"
  1550. href="<?php echo $delete_link;?>" >
  1551. <?php echo $delete; ?></a>
  1552. <?php
  1553. echo '</td>'
  1554. . '<td>'
  1555. . '<a href="' . $tbl_link . '">' . $versions . '</a>'
  1556. . '&nbsp;&nbsp;'
  1557. . '<a href="' . $tbl_link . '&amp;report=true&amp;version='
  1558. . $version_data['version'] . '">' . $report . '</a>'
  1559. . '&nbsp;&nbsp;'
  1560. . '<a href="' . $tbl_link . '&amp;snapshot=true&amp;version='
  1561. . $version_data['version'] . '">' . $structure . '</a>'
  1562. . '</td>'
  1563. . '</tr>';
  1564. if ($style == 'even') {
  1565. $style = 'odd';
  1566. } else {
  1567. $style = 'even';
  1568. }
  1569. }
  1570. ?>
  1571. </tbody>
  1572. </table>
  1573. <?php
  1574. echo PMA_Util::getWithSelected($pmaThemeImage, $text_dir, "trackedForm");
  1575. echo PMA_Util::getButtonOrImage(
  1576. 'submit_mult', 'mult_submit', 'submit_mult_delete_tracking',
  1577. __('Delete tracking'), 'b_drop.png', 'delete_tracking'
  1578. );
  1579. ?>
  1580. </form>
  1581. </div>
  1582. <?php
  1583. }
  1584. /**
  1585. * Display tracking status button
  1586. *
  1587. * @param array $version_data data about tracking versions
  1588. * @param string $tbl_link link for tbl_tracking.php
  1589. *
  1590. * @return void
  1591. */
  1592. function PMA_displayStatusButton($version_data, $tbl_link)
  1593. {
  1594. $state = PMA_getVersionStatus($version_data);
  1595. $options = array(
  1596. 0 => array(
  1597. 'label' => __('not active'),
  1598. 'value' => 'deactivate_now',
  1599. 'selected' => ($state != 'active')
  1600. ),
  1601. 1 => array(
  1602. 'label' => __('active'),
  1603. 'value' => 'activate_now',
  1604. 'selected' => ($state == 'active')
  1605. )
  1606. );
  1607. echo PMA_Util::toggleButton(
  1608. $tbl_link . '&amp;version=' . $version_data['version'],
  1609. 'toggle_activation',
  1610. $options,
  1611. null
  1612. );
  1613. }