update
[phpmyadmin/crack.git] / db_stats.php3
blob9d6d508b029efcf697249a07e7e5e70c47cb1a38
1 <?php
2 /* $Id$ */
5 /**
6 * Gets the variables sent to this script and send headers
7 */
8 $js_to_run = 'functions.js';
9 require('./libraries/grab_globals.lib.php3');
10 require('./header.inc.php3');
13 /**
14 * Ensures the current user is super-user
16 if (!@PMA_mysql_query('USE mysql', $userlink)) {
17 echo '<p><b>' . $strError . '</b></p>' . "\n";
18 echo '<p>&nbsp;&nbsp;&nbsp;&nbsp;' . $strNoRights . '</p>' . "\n";
19 include('./footer.inc.php3');
20 exit();
21 } // end if
24 /**
25 * Drop databases if required
27 if ((!empty($submit_mult) && isset($selected_db))
28 || isset($mult_btn)) {
29 $err_url = 'db_stats.php3'
30 . '?lang=' . $lang
31 . '&amp;convcharset=' . $convcharset
32 . '&amp;server=' . $server;
33 $action = 'db_stats.php3';
34 $show_query = '1';
35 include('./mult_submits.inc.php3');
39 /**
40 * Sorts the databases array according to the user's choice
42 * @param array a record associated to a database
43 * @param array a record associated to a database
45 * @return integer a value representing whether $a should be before $b in the
46 * sorted array or not
48 * @global mixed the array to sort
49 * @global mixed 'key' if the table has to be sorted by key, the column
50 * number to use to sort the array else
52 * @access private
54 function PMA_dbCmp($a, $b)
56 global $dbs_array;
57 global $col;
59 $is_asc = ($GLOBALS['sort_order'] == 'asc');
61 // Sort by key (the db names) if required
62 if (!is_int($col) && $col == 'key') {
63 return (($is_asc) ? strcasecmp($a, $b) : -strcasecmp($a, $b));
65 // Sort by key (the db names) in ascending order if the columns' values are
66 // the same
67 else if ($dbs_array[$a][$col] == $dbs_array[$b][$col]) {
68 return strcasecmp($a, $b);
70 // Other cases
71 else {
72 $tmp = (($dbs_array[$a][$col] < $dbs_array[$b][$col]) ? -1 : 1);
73 return (($is_asc) ? $tmp : -$tmp);
75 } // end of the 'PMA_dbCmp()' function
78 /**
79 * Get the list and number of available databases.
80 * Skipped if no server selected: in this case no database should be displayed
81 * before the user choose among available ones at the welcome screen.
83 if ($server > 0) {
84 // Get the valid databases list
85 $num_dbs = count($dblist);
86 $dbs = @mysql_list_dbs() or PMA_mysqlDie('', 'mysql_list_dbs()', '', 'main.php3?lang' . $lang . '&amp;server=' . $server);
87 if ($dbs) {
88 while ($a_db = PMA_mysql_fetch_object($dbs)) {
89 if (!$num_dbs) {
90 $dblist[] = $a_db->Database;
91 } else {
92 $true_dblist[$a_db->Database] = '';
94 } // end while
95 mysql_free_result($dbs);
96 } // end if
97 if ($num_dbs && empty($true_dblist)) {
98 $dblist = array();
99 } else if ($num_dbs) {
100 for ($i = 0; $i < $num_dbs; $i++) {
101 if (isset($true_dblist[$dblist[$i]])) {
102 $dblist_valid[] = $dblist[$i];
105 if (isset($dblist_valid)) {
106 $dblist = $dblist_valid;
107 unset($dblist_valid);
108 } else {
109 $dblist = array();
111 unset($true_dblist);
113 // Get the valid databases count
114 $num_dbs = count($dblist);
115 } else {
116 $num_dbs = 0;
122 * Displays the page
125 <h1 align="center">
126 <?php echo ucfirst($strDatabasesStats); ?>
127 </h1>
128 <table align="center" border="<?php echo $cfg['Border']; ?>" cellpadding="5">
129 <tr>
130 <th align="<?php echo $cell_align_left; ?>"><big><?php echo $strHost . ' :'; ?></big></th>
131 <th align="<?php echo $cell_align_left; ?>"><big><?php echo $cfg['Server']['host']; ?></big></th>
132 </tr>
133 <tr>
134 <th align="<?php echo $cell_align_left; ?>"><big><?php echo $strGenTime . ' :'; ?></big></th>
135 <th align="<?php echo $cell_align_left; ?>"><big><?php echo PMA_localisedDate(); ?></big></th>
136 </tr>
137 </table>
138 <br /><br />
141 <?php
143 * At least one db -> do the work
145 if ($num_dbs > 0) {
146 // Defines the urls used to sort the table
147 $common_url = 'db_stats.php3?lang=' . $lang . '&amp;convcharset=' . $convcharset . '&amp;server=' . $server;
148 if (empty($sort_by)) {
149 $sort_by = 'db_name';
150 $sort_order = 'asc';
152 else if (empty($sort_order)) {
153 $sort_order = (($sort_by == 'db_name') ? 'asc' : 'desc');
155 $img_tag = '&nbsp;' . "\n"
156 . ' '
157 . '<img src="./images/' . $sort_order . '_order.gif" border="0" width="7" height="7"'
158 . ' alt="' . (($sort_order == 'asc') ? $strAscending : $strDescending) . '"'
159 . ' title="' . (($sort_order == 'asc') ? $strAscending : $strDescending) . '" />';
160 // Default order is ascending for db name, descending for sizes
161 for ($i = 0; $i < 5; $i++) {
162 $url_sort[$i]['order'] = (($i == 0) ? 'asc' : 'desc');
163 $url_sort[$i]['img_tag'] = '';
165 if ($sort_by == 'db_name') {
166 $url_sort[0]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
167 $url_sort[0]['img_tag'] = $img_tag;
168 $col = 'key'; // used in 'PMA_dbCmp()'
169 } else if ($sort_by == 'tbl_cnt') {
170 $url_sort[1]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
171 $url_sort[1]['img_tag'] = $img_tag;
172 $col = 0;
173 } else if ($sort_by == 'data_sz') {
174 $url_sort[2]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
175 $url_sort[2]['img_tag'] = $img_tag;
176 $col = 1;
177 } else if ($sort_by == 'idx_sz') {
178 $url_sort[3]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
179 $url_sort[3]['img_tag'] = $img_tag;
180 $col = 2;
181 } else {
182 $url_sort[4]['order'] = (($sort_order == 'asc') ? 'desc' : 'asc');
183 $url_sort[4]['img_tag'] = $img_tag;
184 $col = 3;
187 <form action="db_stats.php3" name="dbStatsForm">
188 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
189 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
190 <input type="hidden" name="server" value="<?php echo $server; ?>" />
192 <table align="center" border="<?php echo $cfg['Border']; ?>">
193 <tr>
194 <th>&nbsp;</th>
195 <th>
196 &nbsp;
197 <a href="<?php echo $common_url . '&amp;sort_by=db_name&amp;sort_order=' . $url_sort[0]['order']; ?>">
198 <?php echo ucfirst($strDatabase) . $url_sort[0]['img_tag']; ?></a>&nbsp;
199 </th>
200 <th>
201 &nbsp;
202 <a href="<?php echo $common_url . '&amp;sort_by=tbl_cnt&amp;sort_order=' . $url_sort[1]['order']; ?>">
203 <?php echo ucfirst(trim(sprintf($strTables, ''))) . $url_sort[1]['img_tag']; ?></a>&nbsp;
204 </th>
205 <th>
206 &nbsp;
207 <a href="<?php echo $common_url . '&amp;sort_by=data_sz&amp;sort_order=' . $url_sort[2]['order']; ?>">
208 <?php echo ucfirst($strData) . $url_sort[2]['img_tag']; ?></a>&nbsp;
209 </th>
210 <th>
211 &nbsp;
212 <a href="<?php echo $common_url . '&amp;sort_by=idx_sz&amp;sort_order=' . $url_sort[3]['order']; ?>">
213 <?php echo ucfirst($strIndexes) . $url_sort[3]['img_tag']; ?></a>&nbsp;
214 </th>
215 <th>
216 &nbsp;
217 <a href="<?php echo $common_url . '&amp;sort_by=tot_sz&amp;sort_order=' . $url_sort[4]['order']; ?>">
218 <?php echo ucfirst($strTotal) . $url_sort[4]['img_tag']; ?></a>&nbsp;
219 </th>
220 </tr>
221 <?php
222 unset($url_sort);
223 echo "\n";
225 $total_array[0] = 0; // number of tables
226 $total_array[1] = 0; // total data size
227 $total_array[2] = 0; // total index size
228 $total_array[3] = 0; // big total size
230 // Gets the tables stats per database
231 for ($i = 0; $i < $num_dbs; $i++) {
232 $db = $dblist[$i];
233 $tables = @PMA_mysql_list_tables($db);
235 // Number of tables
236 if ($tables) {
237 $dbs_array[$db][0] = mysql_numrows($tables);
238 mysql_free_result($tables);
239 } else {
240 $dbs_array[$db][0] = 0;
242 $total_array[0] += $dbs_array[$db][0];
244 // Size of data and indexes
245 $dbs_array[$db][1] = 0; // data size column
246 $dbs_array[$db][2] = 0; // index size column
247 $dbs_array[$db][3] = 0; // full size column
249 if (PMA_MYSQL_INT_VERSION >= 32303) {
250 $local_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
251 $result = @PMA_mysql_query($local_query);
252 // needs the "@" below otherwise, warnings in case of special DB names
253 if ($result && @mysql_num_rows($result)) {
254 while ($row = PMA_mysql_fetch_array($result)) {
255 $dbs_array[$db][1] += $row['Data_length'];
256 $dbs_array[$db][2] += $row['Index_length'];
258 $dbs_array[$db][3] = $dbs_array[$db][1] + $dbs_array[$db][2];
259 $total_array[1] += $dbs_array[$db][1];
260 $total_array[2] += $dbs_array[$db][2];
261 $total_array[3] += $dbs_array[$db][3];
262 mysql_free_result($result);
263 } // end if
264 } // end if MySQL 3.23.03+
266 } // end for
267 mysql_close();
269 // Sorts the dbs arrays
270 uksort($dbs_array, 'PMA_dbCmp');
271 reset($dbs_array);
273 // Check/unchek all databases url
274 $checkall_url = 'db_stats.php3'
275 . '?lang=' . $lang
276 . '&amp;convcharset=' . $convcharset
277 . '&amp;server=' . $server
278 . (empty($sort_by) ? '' : '&amp;sort_by=' . $sort_by)
279 . (empty($sort_order) ? '' : '&amp;sort_order=' . $sort_order);
280 $do_check = (empty($checkall))
281 ? ''
282 : ' checked="checked"';
284 // Displays the tables stats per database
285 $i = 0;
286 while (list($db_name, $db_prop) = each($dbs_array)) {
287 $bgcolor = ($i % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
289 list($data_size, $data_unit) = PMA_formatByteDown($dbs_array[$db_name][1], 3, 1);
290 list($idx_size, $idx_unit) = PMA_formatByteDown($dbs_array[$db_name][2], 3, 1);
291 list($tot_size, $tot_unit) = PMA_formatByteDown($dbs_array[$db_name][3], 3, 1);
293 echo ' <tr>' . "\n";
294 echo ' <td align="center" bgcolor="'. $bgcolor . '">' . "\n";
295 echo ' &nbsp;<input type="checkbox" name="selected_db[]" value="' . urlencode($db_name) . '"' . $do_check . ' />&nbsp;' . "\n";
296 echo ' </td>' . "\n";
297 echo ' <td bgcolor="'. $bgcolor . '">&nbsp;<a href="index.php3?lang=' . $lang . '&amp;convcharset=' . $convcharset . '&amp;server=' . $server . '&amp;db=' . urlencode($db_name) . '" target="_parent">' . $db_name . '</a>&nbsp;</td>' . "\n";
298 echo ' <td align="right" bgcolor="'. $bgcolor . '">&nbsp;' . $dbs_array[$db_name][0] . '&nbsp;</td>' . "\n";
299 echo ' <td align="right" bgcolor="'. $bgcolor . '">&nbsp;' . $data_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $data_unit . '&nbsp;</td>' . "\n";
300 echo ' <td align="right" bgcolor="'. $bgcolor . '">&nbsp;' . $idx_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $idx_unit . '&nbsp;</td>' . "\n";
301 echo ' <td align="right" bgcolor="'. $bgcolor . '">&nbsp;<b>' . $tot_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $tot_unit . '</b>&nbsp;</td>' . "\n";
302 echo ' </tr>' . "\n";
304 $i++;
305 } // end while
306 unset($dbs_array);
308 // Displays the server stats
309 list($data_size, $data_unit) = PMA_formatByteDown($total_array[1], 3, 1);
310 list($idx_size, $idx_unit) = PMA_formatByteDown($total_array[2], 3, 1);
311 list($tot_size, $tot_unit) = PMA_formatByteDown($total_array[3], 3, 1);
313 echo ' <tr>' . "\n";
314 echo ' <th>&nbsp;</th>' . "\n";
315 echo ' <th>&nbsp;' . $strSum . ':&nbsp;' . $num_dbs . '</th>' . "\n";
316 echo ' <th align="right">&nbsp;' . $total_array[0] . '&nbsp;</th>' . "\n";
317 echo ' <th align="right">&nbsp;' . $data_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $data_unit . '&nbsp;</th>' . "\n";
318 echo ' <th align="right">&nbsp;' . $idx_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $idx_unit . '&nbsp;</th>' . "\n";
319 echo ' <th align="right">&nbsp;<b>' . $tot_size . '<bdo dir="' . $text_dir . '"> </bdo>' . $tot_unit . '</b>&nbsp;</th>' . "\n";
320 echo ' </tr>' . "\n\n";
322 echo ' <tr>' . "\n";
323 echo ' <td colspan="6">' . "\n";
324 echo ' <img src="./images/arrow_' . $text_dir . '.gif" border="0" width="38" height="22" alt="' . $strWithChecked . '" />' . "\n";
325 echo ' <a href="' . $checkall_url . '&amp;checkall=1" onclick="setCheckboxes(\'dbStatsForm\', true); return false;">' . "\n";
326 echo ' ' . $strCheckAll . '</a>' . "\n";
327 echo ' &nbsp;/&nbsp;' . "\n";
328 echo ' <a href="' . $checkall_url . '" onclick="setCheckboxes(\'dbStatsForm\', false); return false;">' . "\n";
329 echo ' ' . $strUncheckAll . '</a>' . "\n";
330 echo ' &nbsp;&nbsp;&nbsp;' . "\n";
331 echo ' <i>' . $strWithChecked . '</i>&nbsp;&nbsp;<input type="submit" name="submit_mult" value="' . $strDrop . '" />' . "\n";
332 echo ' </td>' . "\n";
333 echo ' </tr>' . "\n";
335 echo ' </table>' . "\n\n";
337 echo '</form>' . "\n";
339 unset($total_array);
340 } // end if ($num_dbs > 0)
344 * No database case
346 else {
348 <p align="center"><big>&nbsp;&nbsp;<?php echo $strNoDatabases; ?></big></p>
349 <?php
350 } // end if ($num_dbs == 0)
351 echo "\n";
355 * Displays the footer
357 require('./footer.inc.php3');