source: trunk/Modules/NetworkTopology/topologie-img.php@ 931

Last change on this file since 931 was 874, checked in by chronos, 5 years ago
  • Modified: Do not use parenthesis around returned value.
File size: 9.7 KB
Line 
1<?php
2 // Skript pro generování grafu stromové struktury sítě do PNG obrázku
3include('../global.php');
4
5if (array_key_exists('debug', $_GET)) $debug = $_GET['debug'];
6else $debug = 0;
7$TopHostName = 'nix-router';
8// $debug = 0;
9
10// === Zpětné vyvážení stromu do hloubky =======================================
11function balance($id, $level, &$vlast, &$vleft, &$vpred, &$vfirst, &$vnext, &$tbound, &$width, $limit)
12{
13 global $debug, $bbound;
14
15 if (!array_key_exists($id, $vfirst)) $vfirst[$id] = 0;
16 if ($i = $vfirst[$id])
17 {
18 //if ($debug==2) echo $id.':'.@$i.','.@$vpred[$i].'-'.@$vleft[@$vpred[$i]]."\n";
19 if (($vlast[$id] > 0) && ($vleft[$id] > $vleft[$vlast[$id]]))
20 {
21 $diff=$vleft[$id]-$vleft[$vlast[$id]];
22 $i=$vfirst[$id];
23 if ($vleft[$id] >= $tbound[$level])
24 {
25 $tbound[$level] = $vleft[$id] + 2;
26 if ($vleft[$id] > $width) $width = $vleft[$id];
27 }
28 } else {
29 $diff=0;
30 if ($vpred[$i]&&($vleft[$i]<=$vleft[$vpred[$i]]))
31 {
32 $diff=$vleft[$i]-$vleft[$vpred[$i]]+2;
33 } else $i = 0;
34 }
35 while ($i>0)
36 {
37 $vleft[$i]+=$diff;
38 $limit = balance($i,$level+1, $vlast,$vleft,$vpred, $vfirst,$vnext,$tbound, $width, $limit) + 2;
39 if (!array_key_exists($i, $vnext)) $vnext[$i] = 0;
40 $i = $vnext[$i];
41 }
42 }
43}
44
45// === Generování rovinné stromové struktury ===================================
46function gentree($mode) // depth-first algorithm
47{
48 global $debug, $TopHostName, $Database;
49
50 // --- Inicializace ----------------------------------------------------------
51 $tbound = array(); // Hranice pozic jednotlivých úrovní
52 $tranger = array(); // Hranicni prvek
53 $position = array(); // Pozice aktuálního prvku na dané úrovni
54 $vfirst = array(); // První potomek prvku
55 $vlast = array(); // Poslední potomek prvku
56 $vnext = array(); // Následující sourozenec
57 $vleft = array(); // Pozice prvku zleva
58 $vtop = array(); // Pozice prvku shora
59 $vpred = array(); // Vedlejsi prvek na řádku
60
61 $index = 0; // Index aktuálního prvku
62 $curr = 0; // Aktuální prvek
63 $level = 0; // Aktuální úroveň hloubky ve stromu
64 $width = 0; // Šířka stromu
65 $height = 0; // Hloubka stromu
66
67 $parent[$level] = 0; // Rodič dané úrovně
68 $position[$level] = 0; // Aktuální pozice prvku na dané úrovni
69 $count[$level] = 0; // Počet prvků na dané úrovni
70
71 $maxindex = 0;
72 $tbound[$level] = 0;
73 $tranger[$level] = 0;
74
75 // --- Hlavní cyklus ---------------------------------------------------------
76 do
77 {
78 // --- Proveď databázový dotaz -----------------------------------------------
79 $query = 'SELECT * FROM hosts WHERE used=1 AND ';
80 if ($level == 0)
81 {
82 $query .= 'name = "'.$TopHostName.'" ORDER BY id';
83 } else
84 {
85 $query .= ' parent = '.$parent[$level].' ORDER BY id';
86 }
87 if ($mode) $query.=' DESC';
88 $query .= ' LIMIT '.$position[$level].',1';
89 //echo($query.'<br>');
90 $DbResult = $Database->query($query);
91 $item = $DbResult->fetch_array();
92 //print_r($item);
93 if ($item)
94 {
95 // --- Zpracování položky z DB -----------------------------------------------
96 if ($position[$level] > 0)
97 {
98 $vnext[$curr] = $item['id']; // Neprvní položka, nastav předchozí
99 }
100 $curr = $item['id'];
101 if ($curr > $maxindex) $maxindex = $curr;
102 if ($position[$level] == 0) $vfirst[$parent[$level]]=$curr; // První položka, nastav první
103 $vlast[$parent[$level]] = $curr;
104 $vtop[$curr] = $level;
105 if (!array_key_exists($level, $tbound)) $tbound[$level] = 0;
106 $vleft[$curr] = $tbound[$level];
107 if (!array_key_exists($level, $tranger)) $tranger[$level] = 0;
108 $vpred[$curr] = $tranger[$level];
109 $tranger[$level] = $curr;
110 if (($debug == 3) && ($level == 8)) echo $curr.',';
111 $position[$level]++;
112 $count[$level]++;
113 // --- Zjisti existenci potomků ----------------------------------------------
114 $DbResult = $Database->query('SELECT COUNT(*) FROM hosts WHERE used=1 AND parent = '.$curr);
115 $childcnt = $DbResult->fetch_array();
116 if ($childcnt[0] > 0)
117 {
118 // Uzelový vrchol
119 if (array_key_exists($level + 1, $tbound))
120 if ($tbound[$level + 1] > $vleft[$curr]) $vleft[$curr] = $tbound[$level + 1];
121 }
122 $tbound[$level] = $vleft[$curr] + 2;
123 if ($vleft[$curr] > $width) $width = $vleft[$curr];
124 if ($childcnt[0] > 0)
125 {
126 $level++;
127 if ($level > $height) $height = $level;
128 $parent[$level] = $curr;
129 $position[$level] = 0;
130 $count[$level] = 0;
131 } else $index++; // Listový vrchol
132 } else
133 {
134 // --- Zarovnávání prvků kvůli vzhledu
135 if (!array_key_exists($vfirst[$parent[$level]], $vleft)) $vleft[$vfirst[$parent[$level]]] = 0;
136 if (!array_key_exists($parent[$level], $vleft)) $vleft[$parent[$level]] = 0;
137 if ($vleft[$vfirst[$parent[$level]]] > $vleft[$parent[$level]])
138 {
139 $vleft[$parent[$level]] = $vleft[$vfirst[$parent[$level]]];
140 if ($vleft[$parent[$level]]+2>$tbound[$level-1]) $tbound[$level-1] = $vleft[$parent[$level]]+2;
141 }
142 balance($parent[$level],$level, $vlast,$vleft,$vpred,$vfirst,$vnext,$tbound, $width, 0);
143 if ($position[$level]==1)
144 {
145 $vleft[$vfirst[$parent[$level]]] = $vleft[$parent[$level]];
146 }
147 $level--;
148 if (!array_key_exists($level, $parent)) $parent[$level] = 0;
149 if (!array_key_exists($parent[$level], $vlast)) $vlast[$parent[$level]] = 0;
150 $curr = $vlast[$parent[$level]];
151
152 if (!array_key_exists($level, $tbound)) $tbound[$level] = 0;
153 if (!array_key_exists($level + 1, $tbound)) $tbound[$level + 1] = 0;
154 if ($tbound[$level] > $tbound[$level + 1]) $tbound[$level + 1] = $tbound[$level];
155 }
156 } while ($level >= 0);
157 $data = compact('tbound', 'count', 'tbound', 'vfirst', 'vlast', 'vtop', 'vleft', 'height', 'width', 'index', 'maxindex');
158 return $data;
159};
160
161// === Vytvoř stromy a spoj je =================================================
162extract(gentree(0));
163// exit();
164$data = gentree(1);
165$datawidth = $data['width'];
166for ($i = 0; $i <= $maxindex; $i++)
167{
168 if (!array_key_exists($i, $vleft)) $vleft[$i] = 0;
169 if (!array_key_exists($i, $data['vleft'])) $data['vleft'][$i] = 0;
170 $vleft[$i] = .2 + ($vleft[$i] + ($datawidth - $data['vleft'][$i])) / 2;
171}
172
173$spacex = 32;
174$spacey = 68;
175$halfx = $spacex / 2;
176$halfy = $spacey / 2;
177
178// Preload icon images
179$IconList = array();
180$DbResult = $Database->query('SELECT * FROM HostType');
181while ($HostType = $DbResult->fetch_assoc())
182 $IconList[$HostType['Id']] = imagecreatefrompng('images/'.$HostType['IconName'].'.png');
183
184// === Generování obrázku ======================================================
185$im = imagecreate(($datawidth + $width + 3.2) * $halfx, ($height + 1) * $spacey);
186$background_color = imagecolorallocate($im, 255, 255, 255);
187$black = imagecolorallocate($im, 0, 0, 0);
188$red = imagecolorallocate($im, 255, 0, 0);
189$green = imagecolorallocate($im, 0, 128, 0);
190$blue = imagecolorallocate($im, 100, 100, 255);
191$gray = imagecolorallocate($im, 160, 160, 160);
192
193function xpos($id)
194{
195 global $vleft, $spacex;
196 return $vleft[$id] * $spacex;
197}
198
199$DbResult = $Database->query('SELECT * FROM hosts JOIN HostType ON HostType.Id = hosts.type WHERE hosts.used=1');
200while ($item = $DbResult->fetch_array())
201{
202 $id = $item['id'];
203 if (!array_key_exists($id, $vtop)) $vtop[$id] = 0;
204 if (($vtop[$id] > 0) || ($item['name'] == $TopHostName))
205 {
206 if ($vtop[$id] > 0) imageline($im, xpos($id) + $halfx, $vtop[$id] * $spacey, xpos($id) + $halfx, $vtop[$id] * $spacey + 8, $black);
207 if (!array_key_exists($id, $vfirst)) $vfirst[$id] = 0;
208 if ($vfirst[$id] > 0)
209 {
210 imageline($im, xpos($vfirst[$id]) + $halfx, $vtop[$id] * $spacey + $spacey, xpos($vlast[$id]) + $halfx, $vtop[$id] * $spacey + $spacey, $black);
211 imageline($im, xpos($id) + $halfx, ($vtop[$id] + 1) * $spacey - 10, xpos($id) + $halfx, ($vtop[$id] + 1) * $spacey, $black);
212 }
213
214 $image = $IconList[$item['type']];
215 if ($item['IP'] == '')
216 {
217 $color = $gray;
218 } else
219 if ($item['ShowOnline'] == 1)
220 {
221 if ($item['online'] == 1) $color = $green; else $color = $black;
222 } else
223 {
224 if ($item['online'] == 1) $color = $green; else $color = $red;
225 }
226// $text='IP: '.$ip[0];
227// imagestring($im,2,xpos($id)+($spacex-strlen($text)*imagefontwidth(2))/2,$vtop[$id]*$spacey+24+imagefontheight(2),$text,$black);
228 imagecopy($im, $image, xpos($id) + $halfx - 15, $vtop[$id] * $spacey + 12, 0, 0, 30, 30);
229// imagerectangle($im,xpos($id)+$halfx-6,$vtop[$id]*$spacey+16,xpos($id)+$halfx+6,$vtop[$id]*$spacey+28,$color);
230 if ($debug)
231 {
232 imagestring($im, 2, xpos($id) + ($spacex - strlen($item['id']) * imagefontwidth(2)) / 2, $vtop[$id] * $spacey + 31 + imagefontheight(2), $item['id'], $color);
233 } else
234 imagestring($im, 2, xpos($id) + ($spacex - strlen($item['name']) * imagefontwidth(2)) / 2, $vtop[$id] * $spacey + 31 + imagefontheight(2), strtoupper($item['name']), $color);
235 }
236}
237
238// === Sestavení výsledného souboru ============================================
239if (!($debug > 1))
240{
241 header('Content-type: image/png');
242 header('Cache-Control: no-cache');
243 imagepng($im);
244 imagedestroy($im);
245}
Note: See TracBrowser for help on using the repository browser.