-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.php
230 lines (176 loc) · 6.64 KB
/
draw.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
$host='';
$user='root';
$pass='root';
$db = @mysql_connect('localhost',$user,$pass);
//$out = getStructure($db, $dbname);
$dbname = @$_REQUEST['dbname'];
$out = array();
if(trim($dbname) != "")
{
$out = getForeginKey($db, $dbname);
$struct = getStructure($db, $dbname);
}
$databases = getDatabaseNames($db);
drawInterface($dbname, $out, $struct, $databases);
function drawInterface( $dbname, $out, $struct, $databases)
{
echo "<html><head><title>sGrep</title></head><body>";
echo "<style>a:link, a:hover, a:active, a:visited{color:black;text-decoration: none;}</style>";
echo "<div class='bar'>";
echo "<span><b>Databases</b>: </span>";
foreach($databases as $database)
{
echo " <span><a href='?dbname={$database}' style='color:blue;'>{$database}</a></span> | ";
}
echo "</div>";
echo "<h1>Graf</h1>";
echo "<div class='draw' >";
draw($out);
echo "</div>";
echo "<div class='structure' >";
echo "<h1>Short structure</h1>";
echo "<table class='short_structure' >";
foreach($struct as $tablename => $column)
{
echo "<tr id='table_structure_short_".$tablename."' colspan='2' style='background-color:red;'><td><b>".$tablename."</b><span style='color:grey'>(".count($column).")</span></td></tr>";
foreach($column as $columnname => $columnvalues)
{
echo "<tr colspan='2'><td><a href='#table_structure_column_".$tablename."_".$columnname."'>".$columnname."</a></td></tr>";
}
}
echo "</table>";
echo "<h1>Long structure</h1>";
echo "<table class='long_structure' >";
foreach($struct as $tablename => $column)
{
echo "<tr colspan='2' style='background-color:red;'><td><b>".$tablename."</b></td></tr>";
foreach($column as $columnname => $columnvalues)
{
echo "<tr id='table_structure_column_".$tablename."_".$columnname."' colspan='2'><td style='background-color:green;'><b><i>".$columnname."</i></b></td></tr>";
foreach($columnvalues as $columnvaluename => $columnvalue)
{
echo "<tr>";
echo "<td>".$columnvaluename."</td>";
echo "<td>".$columnvalue."</td>";
echo "</tr>";
}
}
}
echo "</table>";
echo "</div>";
echo "</body></html>";
}
function draw($out)
{
foreach($out as &$item)
{
$item['color'] = randomColor (128, 255);
}
echo "<table><tr style='font-size:200%;text-align:center;'><td style='width:30%;'>Table</td><td style='width:30%;'>Keys</td><td style='width:30%;'>Parents</td></tr>";
foreach($out as $table => &$item)
{
echo "<tr style='border:solid 1px grey;'>";
echo "<td><a href='#table_structure_short_".$table."'><div id='table_".$table."' style='float:left;margin:10px;padding:10px;background-color:".$item['color']."' >".$table."<span style='color:grey;font-size:small;'>(".$item['num'].")</span></div></a></td>";
echo "<td>";
if(isset($item['ref']))foreach($item['ref'] as $columname => &$data)
{
echo "<a href='#table_".$data['table']."'><div style='float:left;margin:10px;padding:10px;background-color:".$out[$data['table']]['color']."' >".$data['table'].":".$data['column']."<span style='color:grey;font-size:small;'>(".$out[$data['table']]['num'].")</span></div>";
}
echo "</td>";
echo "<td>";
if(isset($item['parents']))foreach($item['parents'] as &$parent)
{
echo "<a href='#table_".$parent['table']."'><div style='float:left;margin:10px;padding:10px;background-color:".$out[$parent['table']]['color']."' >".$parent['table'].":".$parent['column']."<span style='color:grey;font-size:small;'>(".$out[$parent['table']]['num'].")</span></div>";
}
echo "</td>";
echo "</tr>";
}
echo "</table><div style='clear:both;'></div>";
/*echo "<pre>";
print_r($out);
echo "</pre>";*/
}
function randomColor ($minVal = 0, $maxVal = 255)
{
// Make sure the parameters will result in valid colours
$minVal = $minVal < 0 || $minVal > 255 ? 0 : $minVal;
$maxVal = $maxVal < 0 || $maxVal > 255 ? 255 : $maxVal;
// Generate 3 values
$r = mt_rand($minVal, $maxVal);
$g = mt_rand($minVal, $maxVal);
$b = mt_rand($minVal, $maxVal);
// Return a hex colour ID string
return sprintf('#%02X%02X%02X', $r, $g, $b);
}
function getStructure($db, $dbname)
{
$sql = mysql_query("
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '{$dbname}'
ORDER BY TABLE_SCHEMA ASC, TABLE_NAME ASC, ORDINAL_POSITION ASC
");
$dbstructure = array();
if (!$sql) {
echo "Error: e004: Table not exist!\n";die;
}
$data = array();
while ($row = mysql_fetch_assoc($sql)) {
$data[] = $row;
$databasename = $row['TABLE_SCHEMA'];
$tablename = $row['TABLE_NAME'];
$columnname = $row['COLUMN_NAME'];
$dbstructure[$tablename][$columnname] = $row;
}
return $dbstructure;
}
function getForeginKey($db, $dbname)
{
$dbstructure = array();
mysql_select_db($dbname, $db);
$sql = mysql_query("SHOW TABLES FROM {$dbname}", $db);
if (!$sql) {
echo 'Error: e009: '.mysql_error($db);die;
}
$data = array();
$i = 1;
while ($row = mysql_fetch_assoc($sql)) {
$dbstructure[$row['Tables_in_'.$dbname]] = array( 'num' => $i++);
}
mysql_select_db('information_schema', $db);
$sql = mysql_query("
SELECT *
FROM KEY_COLUMN_USAGE
WHERE CONSTRAINT_SCHEMA = '{$dbname}'
");
if (!$sql) {
echo "Error: e006: Table not exist!\n";die;
}
$data = array();
while ($row = mysql_fetch_assoc($sql)) {
$data[] = $row;
$databasename = $row['TABLE_SCHEMA'];
$tablename = $row['TABLE_NAME'];
$columnname = $row['COLUMN_NAME'];
$reftablename = $row['REFERENCED_TABLE_NAME'];
$refcolumnname = $row['REFERENCED_COLUMN_NAME'];
if($reftablename != "" && $refcolumnname != ""){
$dbstructure[$tablename]['ref'][$columnname] = array("table" => $reftablename, "column" => $refcolumnname);
$dbstructure[$reftablename]['parents'][] = array("table" => $tablename, "column" => $refcolumnname);
}
}
return $dbstructure;
}
function getDatabaseNames($db)
{
$sql = mysql_query("SHOW DATABASES", $db);
if (!$sql) {
echo 'Error: e010: '.mysql_error($db);die;
}
$data = array();
while ($row = mysql_fetch_assoc($sql)) {
$data[] = $row['Database'];
}
return $data;
}