source: trunk/Modules/NetworkTopology/topologie2.php

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