source: trunk/topologie.php@ 183

Last change on this file since 183 was 148, checked in by george, 16 years ago
  • Upraveno: Přepracován systém generování zobrazení výstupu. Pro nový systím přepsáno mnoho stránek.
File size: 5.8 KB
Line 
1<?php
2
3include('global.php');
4
5class NetworkHostTopology
6{
7 var $Tree;
8 var $TopHostId;
9 var $HostWidth = 64;
10 var $HostHeight = 64;
11 var $Image;
12 var $LineColor;
13 var $BackgroundColor;
14 var $TextColor;
15 var $ImageDevice;
16 var $ImageComputer;
17 var $RelPos;
18 var $Levels = array();
19
20 function NetworkHostTopology($TopHostId)
21 {
22 $this->Tree = array();
23 $this->TopHostId = $TopHostId;
24 }
25
26 function LoadTree()
27 {
28 global $Database;
29
30 $Parent = 38;
31 $Hosts = array();
32 $DbResult = $Database->select('hosts', 'id, name, ip, parent, online', 'used=1');
33 while($DbRow = $DbResult->fetch_array())
34 {
35 if(!array_key_exists($DbRow['id'], $Hosts)) $Hosts[$DbRow['id']] = array('subitems' => array());
36 $Hosts[$DbRow['id']] = array('id' => $DbRow['id'], 'name' => $DbRow['name'], 'parent' => $DbRow['parent'], 'online' => $DbRow['online'], 'subitems' => $Hosts[$DbRow['id']]['subitems']);
37 if(!array_key_exists($DbRow['parent'], $Hosts)) $Hosts[$DbRow['parent']] = array('subitems' => array());
38 $Hosts[$DbRow['parent']]['subitems'][] = &$Hosts[$DbRow['id']];
39 $Hosts[$DbRow['id']]['parent_node'] = &$Hosts[$DbRow['parent']];
40 }
41
42 $this->Tree = $Hosts[$this->TopHostId];
43 }
44
45 function CalculateDimension(&$Host)
46 {
47 $Result = array('min' => $Host['displacement'], 'max' => $Host['displacement']);
48 foreach($Host['subitems'] as $Index => $SubHost)
49 {
50 $SubitemResult = $this->CalculateDimension($Host['subitems'][$Index]);
51 $Result['min'] = min($SubitemResult['min'], $Result['min']);
52 $Result['max'] = max($SubitemResult['max'], $Result['max']);
53 }
54 return($Result);
55 }
56
57 function CalculateDisplacement(&$Host, $Level = 0)
58 {
59 if(!array_key_exists('displacement', $Host)) $Host['displacement'] = 0;
60 $Host['level'] = $Level;
61 foreach($Host['subitems'] as $Index => $SubHost)
62 {
63 $Host['subitems'][$Index]['rel_displacement'] = (-(count($Host['subitems']) - 1) * 0.5 + $Index) * $this->HostWidth;
64 $Host['subitems'][$Index]['displacement'] = $Host['displacement'] + $Host['subitems'][$Index]['rel_displacement'];
65 $this->CalculateDisplacement($Host['subitems'][$Index], $Level + 1);
66 }
67 }
68
69 function MoveNode(&$Host, $Displacement)
70 {
71 $Host['displacement'] = $Host['displacement'] + $Displacement;
72 foreach($Host['subitems'] as $Index => $SubHost)
73 {
74 $this->MoveNode($Host['subitems'][$Index], $Displacement);
75 }
76 }
77
78 function CheckColision()
79 {
80 foreach($this->Levels as $Index => $Level)
81 {
82 for($I = 0; $I < count($Level) - 1; $I++)
83 if($Level[$I]['displacement'] >= $Level[$I + 1]['displacement'])
84 {
85 // Search for common parent
86 $LeftHost = $Level[$I];
87 $RightHost = $Level[$I + 1];
88 while(($LeftHost['level'] > 0) and ($LeftHost['parent'] != $RightHost['parent']))
89 {
90 $LeftHost = $LeftHost['parent_node'];
91 $RightHost = $RightHost['parent_node'];
92 }
93 $Host = $RightHost['parent_node']['subitems'][0];
94 $II = 0;
95 while($RightHost['parent_node']['subitems'][$II]['id'] != $RightHost['id']) $II++;
96 while($II < count($RightHost['parent_node']['subitems']))
97 {
98 $this->MoveNode($RightHost['parent_node']['subitems'][$II], $Level[$I]['displacement'] - $Level[$I + 1]['displacement']);
99 $II++;
100 }
101 }
102 }
103 }
104
105 function BuildLevels(&$Host)
106 {
107 $this->Levels[$Host['level']][] = &$Host;
108 foreach($Host['subitems'] as $Index => $SubHost)
109 {
110 $this->BuildLevels($Host['subitems'][$Index]);
111 }
112 }
113
114 function DrawNode(&$Host)
115 {
116 $ParentHostPos = array('x' => -$this->RelPos['min'] + $Host['displacement'], 'y' => $Host['level'] * $this->HostHeight);
117 foreach($Host['subitems'] as $Index => $SubHost)
118 {
119 $HostPos = array('x' => -$this->RelPos['min'] + $SubHost['displacement'], 'y' => $SubHost['level'] * $this->HostHeight);
120 imageline($this->Image, $ParentHostPos['x'], $ParentHostPos['y'], $ParentHostPos['x'], $ParentHostPos['y'] + $this->HostHeight * 0.5, $this->LineColor);
121 imageline($this->Image, $ParentHostPos['x'], $ParentHostPos['y'] + $this->HostHeight * 0.5, $HostPos['x'], $ParentHostPos['y'] + $this->HostHeight * 0.5, $this->LineColor);
122 imageline($this->Image, $HostPos['x'], $ParentHostPos['y'] + $this->HostHeight * 0.5, $HostPos['x'], $HostPos['y'], $this->LineColor);
123 $this->DrawNode(&$Host['subitems'][$Index]);
124 imagecopy($this->Image, $this->ImageComputer, $HostPos['x'] - imagesx($this->ImageComputer) * 0.5, $HostPos['y'] - imagesy($this->ImageComputer) * 0.5, 0, 0, 30, 30);
125 imagestring($this->Image, 2, $HostPos['x'] - (strlen($SubHost['name']) * imagefontwidth(2)) / 2, $HostPos['y'] + imagesy($this->ImageComputer) * 0.5, $SubHost['name'], $this->TextColor);
126 }
127 }
128
129 function ProduceImage()
130 {
131 $this->CalculateDisplacement($this->Tree);
132 $this->BuildLevels($this->Tree);
133 $this->CheckColision();
134 $this->RelPos = $this->CalculateDimension($this->Tree);
135 $this->ImageComputer = imagecreatefrompng('is/images/comp.png');
136 $this->ImageDevice = imagecreatefrompng('is/images/device.png');
137
138 header("Content-type: image/png");
139 header("Cache-Control: no-cache");
140 $this->Image = imagecreate($this->RelPos['max'] - $this->RelPos['min'], count($this->Levels) * $this->HostHeight);
141 $this->BackgroundColor = imagecolorallocate($this->Image, 255, 255, 255); // Black
142 $this->LineColor = imagecolorallocate($this->Image, 0, 200, 0); // Black
143 $this->TextColor = imagecolorallocate($this->Image, 0, 0, 0); // Black
144 $this->DrawNode($this->Tree);
145 imageline($this->Image, 0, 0, 0, 0, $this->BackgroundColor);
146 imagepng($this->Image);
147 imagedestroy($this->Image);
148 }
149}
150
151$Topology = new NetworkHostTopology(38);
152$Topology->LoadTree();
153//print_r($Topology->Tree);
154$Topology->ProduceImage();
155
156?>
Note: See TracBrowser for help on using the repository browser.