New version 0.4.0 of the reports plugin
[racktables-contribs] / reports / reports / reportExtensionLib.php
1 <?php
2 // Custom Racktables Report v.0.3.3
3 // Libary file
4
5 // 2016-02-04 - Mogilowski Sebastian <sebastian@mogilowski.net>
6
7 error_reporting(E_ERROR | E_PARSE);
8
9 /**
10 * Get the location of an RackTables object
11 *
12 * @param array $aObject
13 * @return string
14 */
15 function getLocation($aObject) {
16 $sRowName = 'unknown';
17 $sRackName = 'unknown';
18
19 # Location parsing for other realms than objects
20 if ($aObject['realm'] == 'rack') {
21 $sLocation = $aObject["location_name"] . ': '. $aObject["row_name"];
22 return $sLocation;
23 }
24
25 if ($aObject['realm'] == 'row') {
26 $sLocation = $aObject["location_name"];
27 return $sLocation;
28 }
29
30 if ($aObject['realm'] == 'location') {
31 if ($aObject["parent_name"] == null)
32 return '';
33 $sLocation = $aObject["parent_name"];
34 return $sLocation;
35 }
36
37 # Try to read the mount informations of the object
38 if ( function_exists('getMountInfo') ) {
39 $mountInfo = getMountInfo (array($aObject['id']));
40
41 if ( isset( $mountInfo[$aObject['id']][0]["rack_name"] ) )
42 $sRackName = $mountInfo[$aObject['id']][0]["rack_name"];
43
44 $sRowName = 'unknown';
45 if ( isset( $mountInfo[$aObject['id']][0]["row_name"] ) )
46 $sRowName = $mountInfo[$aObject['id']][0]["row_name"];
47 }
48 else {
49 if ( isset( $aObject["Row_name"] ) )
50 $sRowName = $aObject["Row_name"];
51
52 if ( isset( $aObject["Rack_name"] ) )
53 $sRackName = $aObject["Rack_name"];
54 }
55
56 # No mount information available - check for a container
57 if ( ( $sRowName == 'unknown' ) && ( $sRackName == 'unknown' ) && ( isset( $aObject['container_id'] ) ) ) {
58 $sContainerName = '<a href="'. makeHref ( array( 'page' => 'object', 'object_id' => $aObject['container_id']) ) .'">'.$aObject['container_name'].'</a>';
59
60 $attributes = getAttrValues ($aObject['id']);
61 if ( isset($attributes['28']['a_value']) && $attributes['28']['a_value'] != '' )
62 $sLocation = $sContainerName.': Slot '.$attributes['28']['a_value'];
63 else
64 $sLocation = $sContainerName;
65
66 # Get mount info of the container
67 $sContainerRowName = 'unknown';
68 $sContainerRackName = 'unknown';
69
70 if ( function_exists('getMountInfo') ) {
71
72 $containerMountInfo = getMountInfo (array($aObject['container_id']));
73
74 if ( isset( $containerMountInfo[$aObject['container_id']][0]["rack_name"] ) )
75 $sContainerRackName = $containerMountInfo[$aObject['container_id']][0]["rack_name"];
76
77 if ( isset( $containerMountInfo[$aObject['container_id']][0]["row_name"] ) )
78 $sContainerRowName = $containerMountInfo[$aObject['container_id']][0]["row_name"];
79
80 $sLocation .= '<br/>' . $sContainerRowName.': '.$sContainerRackName;
81 }
82 }
83 else {
84 $sLocation = $sRowName.': '.$sRackName;
85 }
86
87 return $sLocation;
88
89 }
90
91 /**
92 * Create hyperlinks in text
93 *
94 * @param string $sText
95 * @return string
96 */
97 function makeLinksInText($sText)
98 {
99 # prepend http:// to www.xyz.com strings
100 $sText = preg_replace("/([^\/](www\.))(([^(\s|,)<]{4,68})[^(\s|,)<]*)/", ' http://$2$3', $sText);
101
102 # add html hyperlink to http:// and https:// strings
103 $sText = preg_replace("/(http:\/\/|https:\/\/)(([^(\s|,)<]{4,68})[^(\s|,)<]*)/", '<a href="$1$2" target="_blank">$2$4</a>', $sText);
104
105 # try to add mailto: links in text containing an '@' with a domain name
106 $sText = preg_replace("/([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/", '<a href="mailto:$1" target="_blank">$1</a>', $sText);
107
108 return $sText;
109 }
110
111
112 # Need for backward compatibility - Define function from Racktables version 0.20.x
113 if ( !function_exists('ip6_format') )
114 {
115
116 function ip6_format ($ip_bin) {
117 // maybe this is IPv6-to-IPv4 address?
118 if (substr ($ip_bin, 0, 12) == "\0\0\0\0\0\0\0\0\0\0\xff\xff")
119 return '::ffff:' . implode ('.', unpack ('C*', substr ($ip_bin, 12, 4)));
120
121 $result = array();
122 $hole_index = NULL;
123 $max_hole_index = NULL;
124 $hole_length = 0;
125 $max_hole_length = 0;
126
127 for ($i = 0; $i < 8; $i++) {
128
129 $unpacked = unpack ('n', substr ($ip_bin, $i * 2, 2));
130 $value = array_shift ($unpacked);
131 $result[] = dechex ($value & 0xffff);
132
133 if ($value != 0) {
134
135 unset ($hole_index);
136 $hole_length = 0;
137
138 }
139 else {
140
141 if (! isset ($hole_index))
142 $hole_index = $i;
143 if (++$hole_length >= $max_hole_length) {
144 $max_hole_index = $hole_index;
145 $max_hole_length = $hole_length;
146 }
147
148 }
149
150 }
151
152 if (isset ($max_hole_index)) {
153 array_splice ($result, $max_hole_index, $max_hole_length, array (''));
154
155 if ($max_hole_index == 0 && $max_hole_length == 8)
156 return '::';
157 elseif ($max_hole_index == 0)
158 return ':' . implode (':', $result);
159 elseif ($max_hole_index + $max_hole_length == 8)
160 return implode (':', $result) . ':';
161 }
162
163 return implode (':', $result);
164 }
165
166 }
167
168 /**
169 * Get the containers of an RackTables object
170 *
171 * @param integer $object_id
172 * @return array
173 */
174 function getObjectContainerList ($object_id) {
175 $ret = array();
176
177 $result = usePreparedSelectBlade
178 (
179 'SELECT el.parent_entity_id AS container_id, ro.name as container_name '.
180 'FROM EntityLink AS el '.
181 'INNER JOIN RackObject AS ro ON ro.id = el.parent_entity_id '.
182 'WHERE el.child_entity_type = "object" AND el.parent_entity_type = "object" AND el.child_entity_id = ?',
183 array ($object_id)
184 );
185
186 while ($row = $result->fetch (PDO::FETCH_ASSOC))
187 $ret[$row['container_id']] = array ('container_name' => $row['container_name']);
188
189 return $ret;
190 }
191
192 /**
193 * Get list of child objects of an RackTables object
194 *
195 * @param integer $object_id
196 * @return array
197 */
198 function getObjectChildObjectList ($object_id) {
199 $ret = array();
200
201 $result = usePreparedSelectBlade
202 (
203 'SELECT el.child_entity_id AS object_id, ro.name as object_name '.
204 'FROM EntityLink AS el '.
205 'INNER JOIN RackObject AS ro ON ro.id = el.child_entity_id '.
206 'WHERE el.child_entity_type = "object" AND el.parent_entity_type = "object" AND el.parent_entity_id = ?',
207 array ($object_id)
208 );
209
210 while ($row = $result->fetch (PDO::FETCH_ASSOC))
211 $ret[$row['object_id']] = array ('object_name' => $row['object_name']);
212
213 return $ret;
214 }
215
216 ?>