cut 'Quidway' word
[racktables-incomplete-works] / wwwroot / inc / interface-lib.php
CommitLineData
ca8aa8e9
DO
1<?php
2
cddbb9fd
DO
3# This file is a part of RackTables, a datacenter and server room management
4# framework. See accompanying file "COPYING" for the full copyright and
5# licensing information.
6
d3535346
DO
7define ('TAGNAME_REGEXP', '/^[\p{L}0-9]([. _~-]?[\p{L}0-9])*$/u');
8define ('AUTOTAGNAME_REGEXP', '/^\$[\p{L}0-9]([. _~-]?[\p{L}0-9])*$/u');
9
ca8aa8e9
DO
10// Let's have it here, so extensions can add their own images.
11$image = array();
ca8aa8e9
DO
12$image['rackspace']['path'] = 'pix/racks.png';
13$image['rackspace']['width'] = 218;
14$image['rackspace']['height'] = 200;
15$image['objects']['path'] = 'pix/server.png';
16$image['objects']['width'] = 218;
17$image['objects']['height'] = 200;
18$image['depot']['path'] = 'pix/server.png';
19$image['depot']['width'] = 218;
20$image['depot']['height'] = 200;
21$image['files']['path'] = 'pix/files.png';
22$image['files']['width'] = 218;
23$image['files']['height'] = 200;
24$image['ipv4space']['path'] = 'pix/addressspace.png';
25$image['ipv4space']['width'] = 218;
26$image['ipv4space']['height'] = 200;
27$image['ipv6space']['path'] = 'pix/addressspacev6.png';
28$image['ipv6space']['width'] = 218;
29$image['ipv6space']['height'] = 200;
30$image['ipv4slb']['path'] = 'pix/slb.png';
31$image['ipv4slb']['width'] = 218;
32$image['ipv4slb']['height'] = 200;
33$image['config']['path'] = 'pix/configuration.png';
34$image['config']['width'] = 218;
35$image['config']['height'] = 200;
36$image['reports']['path'] = 'pix/report.png';
37$image['reports']['width'] = 218;
38$image['reports']['height'] = 200;
39$image['8021q']['path'] = 'pix/8021q.png';
40$image['8021q']['width'] = 218;
41$image['8021q']['height'] = 200;
42$image['objectlog']['path'] = 'pix/crystal-mimetypes-shellscript-218x200.png';
43$image['objectlog']['width'] = 218;
44$image['objectlog']['height'] = 200;
45$image['virtual']['path'] = 'pix/virtualresources.png';
46$image['virtual']['width'] = 218;
47$image['virtual']['height'] = 200;
48$image['download']['path'] = 'pix/download.png';
49$image['download']['width'] = 16;
50$image['download']['height'] = 16;
51$image['DOWNLOAD']['path'] = 'pix/download-big.png';
52$image['DOWNLOAD']['width'] = 32;
53$image['DOWNLOAD']['height'] = 32;
54$image['plug']['path'] = 'pix/tango-network-wired.png';
55$image['plug']['width'] = 16;
56$image['plug']['height'] = 16;
57$image['cut']['path'] = 'pix/tango-edit-cut-16x16.png';
58$image['cut']['width'] = 16;
59$image['cut']['height'] = 16;
60$image['Cut']['path'] = 'pix/tango-edit-cut-22x22.png';
61$image['Cut']['width'] = 22;
62$image['Cut']['height'] = 22;
75a01117
DO
63$image['Cut gray']['path'] = 'pix/tango-edit-cut-22x22-gray.png';
64$image['Cut gray']['width'] = 22;
65$image['Cut gray']['height'] = 22;
ca8aa8e9
DO
66$image['CUT']['path'] = 'pix/tango-edit-cut-32x32.png';
67$image['CUT']['width'] = 32;
68$image['CUT']['height'] = 32;
69$image['add']['path'] = 'pix/tango-list-add.png';
70$image['add']['width'] = 16;
71$image['add']['height'] = 16;
72$image['ADD']['path'] = 'pix/tango-list-add-big.png';
73$image['ADD']['width'] = 32;
74$image['ADD']['height'] = 32;
75$image['delete']['path'] = 'pix/tango-list-remove.png';
76$image['delete']['width'] = 16;
77$image['delete']['height'] = 16;
78$image['DELETE']['path'] = 'pix/tango-list-remove-32x32.png';
79$image['DELETE']['width'] = 32;
80$image['DELETE']['height'] = 32;
81$image['destroy']['path'] = 'pix/tango-user-trash-16x16.png';
82$image['destroy']['width'] = 16;
83$image['destroy']['height'] = 16;
84$image['nodestroy']['path'] = 'pix/tango-user-trash-16x16-gray.png';
85$image['nodestroy']['width'] = 16;
86$image['nodestroy']['height'] = 16;
87$image['NODESTROY']['path'] = 'pix/tango-user-trash-32x32-gray.png';
88$image['NODESTROY']['width'] = 32;
89$image['NODESTROY']['height'] = 32;
90$image['DESTROY']['path'] = 'pix/tango-user-trash-32x32.png';
91$image['DESTROY']['width'] = 32;
92$image['DESTROY']['height'] = 32;
93$image['nodelete']['path'] = 'pix/tango-list-remove-shadow.png';
94$image['nodelete']['width'] = 16;
95$image['nodelete']['height'] = 16;
96$image['inservice']['path'] = 'pix/tango-emblem-system.png';
97$image['inservice']['width'] = 16;
98$image['inservice']['height'] = 16;
99$image['notinservice']['path'] = 'pix/tango-dialog-error.png';
100$image['notinservice']['width'] = 16;
101$image['notinservice']['height'] = 16;
102$image['find']['path'] = 'pix/tango-system-search.png';
103$image['find']['width'] = 16;
104$image['find']['height'] = 16;
105$image['next']['path'] = 'pix/tango-go-next.png';
106$image['next']['width'] = 32;
107$image['next']['height'] = 32;
108$image['prev']['path'] = 'pix/tango-go-previous.png';
109$image['prev']['width'] = 32;
110$image['prev']['height'] = 32;
111$image['COMMIT']['path'] = 'pix/tango-go-prev-next-32x32.png';
112$image['COMMIT']['width'] = 32;
113$image['COMMIT']['height'] = 32;
114$image['COMMIT gray']['path'] = 'pix/tango-go-prev-next-gray-32x32.png';
115$image['COMMIT gray']['width'] = 32;
116$image['COMMIT gray']['height'] = 32;
117$image['RECALC']['path'] = 'pix/tango-view-refresh-32x32.png';
118$image['RECALC']['width'] = 32;
119$image['RECALC']['height'] = 32;
120$image['clear']['path'] = 'pix/tango-edit-clear.png';
121$image['clear']['width'] = 16;
122$image['clear']['height'] = 16;
123$image['CLEAR']['path'] = 'pix/tango-edit-clear-big.png';
124$image['CLEAR']['width'] = 32;
125$image['CLEAR']['height'] = 32;
126$image['CLEAR gray']['path'] = 'pix/tango-edit-clear-gray-32x32.png';
127$image['CLEAR gray']['width'] = 32;
128$image['CLEAR gray']['height'] = 32;
129$image['save']['path'] = 'pix/tango-document-save-16x16.png';
130$image['save']['width'] = 16;
131$image['save']['height'] = 16;
132$image['SAVE']['path'] = 'pix/tango-document-save-32x32.png';
133$image['SAVE']['width'] = 32;
134$image['SAVE']['height'] = 32;
135$image['NOSAVE']['path'] = 'pix/tango-document-save-32x32-gray.png';
136$image['NOSAVE']['width'] = 32;
137$image['NOSAVE']['height'] = 32;
138$image['create']['path'] = 'pix/tango-document-new.png';
139$image['create']['width'] = 16;
140$image['create']['height'] = 16;
141$image['CREATE']['path'] = 'pix/tango-document-new-big.png';
142$image['CREATE']['width'] = 32;
143$image['CREATE']['height'] = 32;
144$image['DENIED']['path'] = 'pix/tango-dialog-error-big.png';
145$image['DENIED']['width'] = 32;
146$image['DENIED']['height'] = 32;
147$image['node-collapsed']['path'] = 'pix/node-collapsed.png';
148$image['node-collapsed']['width'] = 16;
149$image['node-collapsed']['height'] = 16;
150$image['node-expanded']['path'] = 'pix/node-expanded.png';
151$image['node-expanded']['width'] = 16;
152$image['node-expanded']['height'] = 16;
153$image['node-expanded-static']['path'] = 'pix/node-expanded-static.png';
154$image['node-expanded-static']['width'] = 16;
155$image['node-expanded-static']['height'] = 16;
156$image['dragons']['path'] = 'pix/mitsudragon.png';
157$image['dragons']['width'] = 195;
158$image['dragons']['height'] = 33;
159$image['LB']['path'] = 'pix/loadbalancer.png';
160$image['LB']['width'] = 32;
161$image['LB']['height'] = 32;
162$image['RS pool']['path'] = 'pix/serverpool.png';
163$image['RS pool']['width'] = 48;
164$image['RS pool']['height'] = 16;
165$image['VS']['path'] = 'pix/servicesign.png';
166$image['VS']['width'] = 39;
167$image['VS']['height'] = 62;
168$image['router']['path'] = 'pix/router.png';
169$image['router']['width'] = 32;
170$image['router']['height'] = 32;
171$image['object']['path'] = 'pix/bracket-16x16.png';
172$image['object']['width'] = 16;
173$image['object']['height'] = 16;
174$image['OBJECT']['path'] = 'pix/bracket-32x32.png';
175$image['OBJECT']['width'] = 32;
176$image['OBJECT']['height'] = 32;
177$image['attach']['path'] = 'pix/tango-mail-attachment-16x16.png';
178$image['attach']['width'] = 16;
179$image['attach']['height'] = 16;
180$image['Attach']['path'] = 'pix/tango-mail-attachment-22x22.png';
181$image['Attach']['width'] = 22;
182$image['Attach']['height'] = 22;
183$image['ATTACH']['path'] = 'pix/tango-mail-attachment-32x32.png';
184$image['ATTACH']['width'] = 32;
185$image['ATTACH']['height'] = 32;
186$image['favorite']['path'] = 'pix/tango-emblem-favorite.png';
187$image['favorite']['width'] = 16;
188$image['favorite']['height'] = 16;
189$image['computer']['path'] = 'pix/tango-computer.png';
190$image['computer']['width'] = 16;
191$image['computer']['height'] = 16;
192$image['empty file']['path'] = 'pix/crystal-file-empty-32x32.png';
193$image['empty file']['width'] = 32;
194$image['empty file']['height'] = 32;
195$image['text file']['path'] = 'pix/crystal-file-text-32x32.png';
196$image['text file']['width'] = 32;
197$image['text file']['height'] = 32;
198$image['image file']['path'] = 'pix/crystal-file-image-32x32.png';
199$image['image file']['width'] = 32;
200$image['image file']['height'] = 32;
201$image['text']['path'] = 'pix/tango-text-x-generic-16x16.png';
202$image['text']['width'] = 16;
203$image['text']['height'] = 16;
204$image['NET']['path'] = 'pix/crystal-network_local-32x32.png';
205$image['NET']['width'] = 32;
206$image['NET']['height'] = 32;
207$image['net']['path'] = 'pix/crystal-network_local-16x16.png';
208$image['net']['width'] = 16;
209$image['net']['height'] = 16;
210$image['USER']['path'] = 'pix/crystal-edit-user-32x32.png';
211$image['USER']['width'] = 32;
212$image['USER']['height'] = 32;
213$image['setfilter']['path'] = 'pix/pgadmin3-viewfiltereddata.png';
214$image['setfilter']['width'] = 32;
215$image['setfilter']['height'] = 32;
216$image['setfilter gray']['path'] = 'pix/pgadmin3-viewfiltereddata-grayscale.png';
217$image['setfilter gray']['width'] = 32;
218$image['setfilter gray']['height'] = 32;
219$image['resetfilter']['path'] = 'pix/pgadmin3-viewdata.png';
220$image['resetfilter']['width'] = 32;
221$image['resetfilter']['height'] = 32;
222$image['resetfilter gray']['path'] = 'pix/pgadmin3-viewdata-grayscale.png';
223$image['resetfilter gray']['width'] = 32;
224$image['resetfilter gray']['height'] = 32;
225$image['knight']['path'] = 'pix/smiley_knight.png';
226$image['knight']['width'] = 72;
227$image['knight']['height'] = 33;
228$image['Zoom']['path'] = 'pix/tango-system-search-22x22.png';
229$image['Zoom']['width'] = 22;
230$image['Zoom']['height'] = 22;
231$image['Zooming']['path'] = 'pix/tango-view-fullscreen-22x22.png';
232$image['Zooming']['width'] = 22;
233$image['Zooming']['height'] = 22;
234$image['UNLOCK']['path'] = 'pix/crystal-actions-unlock-32x32.png';
235$image['UNLOCK']['width'] = 32;
236$image['UNLOCK']['height'] = 32;
237$image['CLOCK']['path'] = 'pix/tango-appointment-32x32.png';
238$image['CLOCK']['width'] = 32;
239$image['CLOCK']['height'] = 32;
240$image['DQUEUE done']['path'] = 'pix/crystal-ok-32x32.png';
241$image['DQUEUE done']['width'] = 32;
242$image['DQUEUE done']['height'] = 32;
243$image['DQUEUE sync_aging']['path'] = 'pix/tango-appointment-32x32.png';
244$image['DQUEUE sync_aging']['width'] = 32;
245$image['DQUEUE sync_aging']['height'] = 32;
246$image['DQUEUE resync_aging']['path'] = 'pix/tango-appointment-32x32.png';
247$image['DQUEUE resync_aging']['width'] = 32;
248$image['DQUEUE resync_aging']['height'] = 32;
249$image['DQUEUE sync_ready']['path'] = 'pix/tango-emblem-system-32x32.png';
250$image['DQUEUE sync_ready']['width'] = 32;
251$image['DQUEUE sync_ready']['height'] = 32;
252$image['DQUEUE resync_ready']['path'] = 'pix/tango-emblem-important-32x32.png';
253$image['DQUEUE resync_ready']['width'] = 32;
254$image['DQUEUE resync_ready']['height'] = 32;
255$image['DQUEUE failed']['path'] = 'pix/tango-emblem-unreadable-32x32.png';
256$image['DQUEUE failed']['width'] = 32;
257$image['DQUEUE failed']['height'] = 32;
258$image['DQUEUE disabled']['path'] = 'pix/tango-emblem-readonly-32x32.png';
259$image['DQUEUE disabled']['width'] = 32;
260$image['DQUEUE disabled']['height'] = 32;
d60c7760
AA
261$image['copy']['path'] = 'pix/tango-edit-copy-16x16.png';
262$image['copy']['width'] = 16;
263$image['copy']['height'] = 16;
ca8aa8e9
DO
264$image['COPY']['path'] = 'pix/tango-edit-copy-32x32.png';
265$image['COPY']['width'] = 32;
266$image['COPY']['height'] = 32;
267$image['html']['path'] = 'pix/tango-text-html.png';
268$image['html']['width'] = 16;
269$image['html']['height'] = 16;
2147167d
AA
270$image['pencil']['path'] = 'pix/pencil-icon.png';
271$image['pencil']['width'] = 12;
272$image['pencil']['height'] = 12;
ca8aa8e9 273
646272be
AA
274$page_by_realm = array();
275$page_by_realm['object'] = 'depot';
276$page_by_realm['rack'] = 'rackspace';
277$page_by_realm['ipv4net'] = 'ipv4space';
278$page_by_realm['ipv6net'] = 'ipv6space';
e2f674be
AA
279$page_by_realm['ipv4vs'] = 'ipv4slb';
280$page_by_realm['ipv4rspool'] = 'ipv4slb';
646272be
AA
281$page_by_realm['file'] = 'files';
282$page_by_realm['user'] = 'userlist';
283
ca8aa8e9
DO
284function printSelect ($optionList, $select_attrs = array(), $selected_id = NULL)
285{
286 echo getSelect ($optionList, $select_attrs, $selected_id);
287}
288
289// Input array keys are OPTION VALUEs and input array values are OPTION text.
c4ad9ac0 290function getSelect ($optionList, $select_attrs = array(), $selected_id = NULL, $treat_single_special = TRUE)
ca8aa8e9
DO
291{
292 $ret = '';
293 if (!array_key_exists ('name', $select_attrs))
294 return '';
295 // handle two corner cases in a specific way
296 if (count ($optionList) == 0)
297 return '(none)';
c4ad9ac0 298 if (count ($optionList) == 1 && $treat_single_special)
ca8aa8e9
DO
299 {
300 foreach ($optionList as $key => $value) { break; }
301 return "<input type=hidden name=${select_attrs['name']} id=${select_attrs['name']} value=${key}>" . $value;
302 }
303 if (!array_key_exists ('id', $select_attrs))
304 $select_attrs['id'] = $select_attrs['name'];
305 $ret .= '<select';
306 foreach ($select_attrs as $attr_name => $attr_value)
307 $ret .= " ${attr_name}=${attr_value}";
308 $ret .= '>';
309 foreach ($optionList as $dict_key => $dict_value)
310 $ret .= "<option value='${dict_key}'" . ($dict_key == $selected_id ? ' selected' : '') . ">${dict_value}</option>";
311 $ret .= '</select>';
312 return $ret;
313}
314
315function printNiftySelect ($groupList, $select_attrs = array(), $selected_id = NULL, $autocomplete = false)
316{
317 echo getNiftySelect ($groupList, $select_attrs, $selected_id);
318}
319
320// Input is a cooked list of OPTGROUPs, each with own sub-list of OPTIONs in the same
321// format as printSelect() expects.
7232ef01
AD
322// If tree is true, hierarchical drop-boxes are used, otherwise optgroups are used.
323function getNiftySelect ($groupList, $select_attrs, $selected_id = NULL, $tree = false)
ca8aa8e9
DO
324{
325 // special treatment for ungrouped data
326 if (count ($groupList) == 1 and isset ($groupList['other']))
327 return getSelect ($groupList['other'], $select_attrs, $selected_id);
328 if (!array_key_exists ('name', $select_attrs))
329 return '';
330 if (!array_key_exists ('id', $select_attrs))
331 $select_attrs['id'] = $select_attrs['name'];
7232ef01 332 if ($tree)
ca8aa8e9 333 {
2f06f6be
DO
334 # it is safe to call many times for the same file
335 addJS ('js/jquery.optionTree.js');
7232ef01
AD
336 $ret = "<input type=hidden name=${select_attrs['name']}>\n";
337 $ret .= "<script type='text/javascript'>\n";
338 $ret .= "\$(function() {\n";
339 $ret .= " var option_tree = {\n";
340 foreach ($groupList as $groupname => $groupdata)
341 {
342 $ret .= " '${groupname}': {";
343 foreach ($groupdata as $dict_key => $dict_value)
344 $ret .= "\"${dict_value}\":'${dict_key}', ";
345 $ret .= "},\n";
346 }
347 $ret .= " };\n";
30dce055 348 $ret .= " var options = {empty_value: '', choose: 'select...'};\n";
7232ef01
AD
349 $ret .= " \$('input[name=${select_attrs['name']}]').optionTree(option_tree, options);\n";
350 $ret .= "});\n";
351 $ret .= "</script>\n";
352 }
353 else
354 {
355 $ret = '<select';
356 foreach ($select_attrs as $attr_name => $attr_value)
357 $ret .= " ${attr_name}=${attr_value}";
358 $ret .= ">\n";
359 foreach ($groupList as $groupname => $groupdata)
360 {
361 $ret .= "<optgroup label='${groupname}'>\n";
362 foreach ($groupdata as $dict_key => $dict_value)
363 $ret .= "<option value='${dict_key}'" . ($dict_key == $selected_id ? ' selected' : '') . ">${dict_value}</option>\n";
364 $ret .= "</optgroup>\n";
365 }
366 $ret .= "</select>\n";
ca8aa8e9 367 }
ca8aa8e9
DO
368 return $ret;
369}
370
16d86b28
DO
371function getOptionTree ($tree_name, $tree_options, $tree_config = array())
372{
77b5137a
DO
373 function serializeJSArray ($options)
374 {
375 $tmp = array();
376 foreach ($options as $key => $value)
377 $tmp[] = "'${key}': \"${value}\"";
378 return '{' . implode (', ', $tmp) . "}\n";
379 }
380 function serializeJSTree ($tree_options)
16d86b28
DO
381 {
382 $self = __FUNCTION__;
383 $tmp = array();
77b5137a
DO
384 # Leaves on the PHP tree are stored "value => label" way,
385 # non-leaves are stored "label => array" way, and the JS
386 # tree is always built "label => value" or "label => array"
387 # way, hence a structure transform is required.
388 foreach ($tree_options as $key => $value)
389 $tmp[] = is_array ($value) ?
1b655fac
AA
390 '"' . str_replace ('"', '\"', $key) . '": ' . $self ($value) :
391 '"' . str_replace ('"', '\"', $value) . '": "' . str_replace ('"', '\"', $key) . '"';
16d86b28
DO
392 return '{' . implode (', ', $tmp) . "}\n";
393 }
394
395 $default_config = array
396 (
397 'choose' => 'select...',
398 'empty_value' => '',
399 );
400 foreach ($tree_config as $cfgoption_name => $cfgoption_value)
401 $default_config[$cfgoption_name] = $cfgoption_value;
402 # it is safe to call many times for the same file
403 addJS ('js/jquery.optionTree.js');
404 $ret = "<input type=hidden name=${tree_name}>\n";
405 $ret .= "<script type='text/javascript'>\n";
406 $ret .= "\$(function() {\n";
77b5137a 407 $ret .= " var option_tree = " . serializeJSTree ($tree_options) . ";\n";
16d86b28
DO
408 $ret .= " var options = " . serializeJSArray ($default_config) . ";\n";
409 $ret .= " \$('input[name=${tree_name}]').optionTree(option_tree, options);\n";
410 $ret .= "});\n";
411 $ret .= "</script>\n";
412 return $ret;
413}
414
ca8aa8e9
DO
415function printImageHREF ($tag, $title = '', $do_input = FALSE, $tabindex = 0)
416{
417 echo getImageHREF ($tag, $title, $do_input, $tabindex);
418}
419
420// this would be better called mkIMG(), make "IMG" HTML element
421function getImageHREF ($tag, $title = '', $do_input = FALSE, $tabindex = 0)
422{
423 global $image;
424 if (!isset ($image[$tag]))
425 $tag = 'error';
426 $img = $image[$tag];
19f22ad8 427 $img['path'] = '?module=chrome&uri=' . $img['path'];
ca8aa8e9
DO
428 if ($do_input == TRUE)
429 return
430 "<input type=image name=submit class=icon " .
431 "src='${img['path']}' " .
432 "border=0 " .
433 ($tabindex ? "tabindex=${tabindex}" : '') .
434 (!strlen ($title) ? '' : " title='${title}'") . // JT: Add title to input hrefs too
435 ">";
436 else
437 return
438 "<img " .
439 "src='${img['path']}' " .
440 "width=${img['width']} " .
441 "height=${img['height']} " .
442 "border=0 " .
443 (!strlen ($title) ? '' : "title='${title}'") .
444 ">";
445}
446
967e3845
DO
447function dos2unix ($text)
448{
449 return str_replace ("\r\n", "\n", $text);
450}
451
452function escapeString ($value, $do_db_escape = FALSE)
453{
454 $ret = htmlspecialchars ($value, ENT_QUOTES, 'UTF-8');
455 if ($do_db_escape)
456 {
457 global $dbxlink;
458 $ret = substr ($dbxlink->quote ($ret), 1, -1);
459 }
460 return $ret;
461}
462
463function transformRequestData()
464{
465 global $sic;
466 // Magic quotes feature is deprecated, but just in case the local system
467 // still has it activated, reverse its effect.
dbd20c7e
AA
468 $do_magic_quotes = (function_exists ('get_magic_quotes_gpc') and get_magic_quotes_gpc());
469 $seen_keys = array();
470
967e3845
DO
471 // Escape any globals before we ever try to use them, but keep a copy of originals.
472 $sic = array();
4675424f
AA
473 // walk through merged GET and POST instead of REQUEST array because it
474 // can contain cookies with data which could not be decoded from UTF-8
2b6b0038 475 foreach (($_POST + $_GET) as $key => $value)
967e3845 476 {
5a7dc9e7
AA
477 if (is_array ($value))
478 $_REQUEST[$key] = $value;
479 else
480 {
481 $value = dos2unix ($value);
482 if ($do_magic_quotes)
483 $value = stripslashes ($value);
484 $_REQUEST[$key] = escapeString ($value);
485 }
486 $sic[$key] = $value;
dbd20c7e 487 $seen_keys[$key] = 1;
967e3845
DO
488 }
489
dbd20c7e
AA
490 // delete cookie information from the $_REQUEST array
491 foreach (array_keys ($_REQUEST) as $key)
492 if (! isset ($seen_keys[$key]))
493 unset ($_REQUEST[$key]);
494
967e3845
DO
495 if (isset ($_SERVER['PHP_AUTH_USER']))
496 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
497 if (isset ($_SERVER['REMOTE_USER']))
498 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
499}
500
ca6698d8
AA
501// JS scripts should be included through this function.
502// They automatically appear in the <head> of your page.
503// $data is a JS filename, or JS code w/o tags around, if $inline = TRUE
504// Scripts are included in the order of adding within the same group, and groups are sorted alphabetically.
505function addJS ($data, $inline = FALSE, $group = 'default')
506{
507 static $javascript = array();
508 static $seen_filenames = array();
dec748f6 509
ca6698d8
AA
510 if (! isset ($data))
511 {
512 ksort ($javascript);
513 return $javascript;
514 }
515 // Add jquery.js and racktables.js the first time a Javascript file is added.
516 if (empty($javascript))
517 {
518 $javascript = array
519 (
520 'a_core' => array
521 (
522 array('type' => 'file', 'script' => 'js/jquery-1.4.4.min.js'),
523 array('type' => 'file', 'script' => 'js/racktables.js'),
524 ),
525 );
526
527 // initialize core js filelist
528 foreach ($javascript as $group_name => $group_array)
529 foreach ($group_array as $item)
530 if ($item['type'] == 'file')
531 $seen_filenames[$item['script']] = 1;
532 }
533
534 if ($inline)
535 $javascript[$group][] = array
536 (
537 'type' => 'inline',
538 'script' => $data,
539 );
540 elseif (! isset ($seen_filenames[$data]))
541 {
542 $javascript[$group][] = array
543 (
544 'type' => 'file',
545 'script' => $data,
546 );
547 $seen_filenames[$data] = 1;
548 }
549}
550
551// CSS styles should be included through this function.
552// They automatically appear in the <head> of your page.
553// $data is a CSS filename, or CSS code w/o tags around, if $inline = TRUE
554// Styles are included in the order of adding.
555function addCSS ($data, $inline = FALSE)
556{
557 static $styles = array();
558 static $seen_filenames = array();
dec748f6 559
ca6698d8
AA
560 if (! isset ($data))
561 return $styles;
562 if ($inline)
563 $styles[] = array
564 (
565 'type' => 'inline',
566 'style' => $data,
567 );
568 elseif (! isset ($seen_filenames[$data]))
569 {
570 $styles[] = array
571 (
572 'type' => 'file',
573 'style' => $data,
574 );
575 $seen_filenames[$data] = 1;
576 }
577}
578
4318ced5
AA
579function getRenderedIPNetCapacity ($range)
580{
581 switch (strlen ($range['ip_bin']))
582 {
583 case 4: return getRenderedIPv4NetCapacity ($range);
584 case 16: return getRenderedIPv6NetCapacity ($range);
585 default: throw new InvalidArgException ('range["ip_bin"]', $range['ip_bin'], "Invalid binary IP");
586 }
587}
588
589function getRenderedIPv4NetCapacity ($range)
590{
591 $class = 'net-usage';
592 if (isset ($range['addrc']))
593 {
594 // full mode
f59e175c 595 // $a is "aquamarine zone", $b is "gray zone"
4318ced5 596 $total = ip4_range_size ($range);
91fcd2c0
AA
597
598 // compute $a_total: own range size, without subranges
6a95327a 599 if ($range['kidc'] == 0)
91fcd2c0
AA
600 $a_total = $total;
601 else
602 {
603 $a_total = 0;
604 foreach ($range['spare_ranges'] as $mask => $spare_list)
fc338a7b 605 $a_total = bcadd ($a_total, bcmul (count ($spare_list), ip4_mask_size ($mask)), 0);
91fcd2c0 606 }
f59e175c 607 $a_used = $range['own_addrc'];
fc338a7b 608 $b_total = bcsub ($total, $a_total, 0);
f59e175c 609 $b_used = $range['addrc'] - $a_used;
4318ced5
AA
610
611 // generate link to progress bar image
612 $width = 100;
91fcd2c0 613 if ($total != 0)
4318ced5 614 {
fc338a7b
AA
615 $px_a = round (bcdiv ($a_total, $total, 4) * $width);
616 $px1 = round (bcdiv ($a_used, $total, 4) * $width);
4318ced5 617 $px2 = $px_a - $px1;
fc338a7b 618 $px3 = round (bcdiv ($b_used, $total, 4) * $width);
4318ced5
AA
619 if ($px3 + $px1 + $px2 > $width)
620 $px3 = $width - $px1 - $px2;
621 }
622 else
623 $px1 = $px2 = $px3 = 0;
624
4318ced5
AA
625 $title_items = array();
626 $title2_items = array();
91fcd2c0 627 if ($a_total != 0)
4318ced5
AA
628 {
629 $title_items[] = "$a_used / $a_total";
fc338a7b 630 $title2_items[] = sprintf ("%d%% used", bcdiv ($a_used, $a_total, 4) * 100);
4318ced5 631 }
91fcd2c0 632 if ($b_total != 0)
4318ced5
AA
633 {
634 $title_items[] = ($b_used ? "$b_used / " : "") . $b_total;
fc338a7b 635 $title2_items[] = sprintf ("%d%% sub-allocated", bcdiv ($b_total, $total, 4) * 100);
4318ced5
AA
636 }
637 $title = implode (', ', $title_items);
638 $title2 = implode (', ', $title2_items);
639 $text = "<img width='$width' height=10 border=0 title='$title2' src='?module=progressbar4&px1=$px1&px2=$px2&px3=$px3'>" .
640 " <small class='title'>$title</small>";
641 }
642 else
643 {
644 // fast mode
645 $class .= ' pending';
646 addJS ('js/net-usage.js');
647
648 $free_text = '';
649 if (isset ($range['kidc']) and $range['kidc'] > 0)
650 {
651 $free_masks = array_keys ($range['spare_ranges']);
652 sort ($free_masks, SORT_NUMERIC);
653 if ($mask = array_shift ($free_masks))
654 {
655 $cnt = count ($range['spare_ranges'][$mask]);
656 $free_text = ', ' . ($cnt > 1 ? "<small>${cnt}x</small>" : "") . "/$mask free";
657 }
658 }
659 $text = ip4_range_size ($range) . $free_text;
660 }
661
662 $div_id = $range['ip'] . '/' . $range['mask'];
663
664 return "<div class=\"$class\" id=\"$div_id\">" . $text . "</div>";
665}
666
667function getRenderedIPv6NetCapacity ($range)
668{
669 $div_id = $range['ip'] . '/' . $range['mask'];
670 $class = 'net-usage';
671 if (isset ($range['addrc']))
672 $used = $range['addrc'];
673 else
674 {
675 $used = NULL;
676 $class .= ' pending';
677 addJS ('js/net-usage.js');
678 }
679
680 static $prefixes = array
681 (
682 0 => '',
683 3 => 'k',
684 6 => 'M',
685 9 => 'G',
686 12 => 'T',
687 15 => 'P',
688 18 => 'E',
689 21 => 'Z',
690 24 => 'Y',
691 );
692
693 if ($range['mask'] <= 64)
694 {
695 $what = 'net';
696 $preposition = 'in';
697 $range['mask'] += 64;
698 }
699 else
700 {
701 $what = 'IP';
702 $preposition = 'of';
703 }
704 $what .= (0 == $range['mask'] % 64 ? '' : 's');
705 $addrc = isset ($used) ? "$used $preposition " : '';
706
707 $dec_order = intval ((128 - $range['mask']) / 10) * 3;
708 $mult = isset ($prefixes[$dec_order]) ? $prefixes[$dec_order] : '??';
dec748f6 709
4318ced5
AA
710 $cnt = 1 << ((128 - $range['mask']) % 10);
711 if ($cnt == 1 && $mult == '')
712 $cnt = '1';
713
714 return "<div class=\"$class\" id=\"$div_id\">" . "{$addrc}${cnt}${mult} ${what}" . "</div>";
715}
716
ca6698d8
AA
717// print part of HTML HEAD block
718function printPageHeaders ()
719{
720 global $pageheaders;
721 ksort ($pageheaders);
722 foreach ($pageheaders as $s)
723 echo $s . "\n";
724
725 // add CSS styles
726 foreach (addCSS (NULL) as $item)
727 if ($item['type'] == 'inline')
728 echo '<style type="text/css">' . "\n" . trim ($item['style'], "\r\n") . "\n</style>\n";
729 elseif ($item['type'] == 'file')
730 echo "<link rel=stylesheet type='text/css' href='?module=chrome&uri=${item['style']}' />\n";
731
732 // add JS scripts
733 foreach (addJS (NULL) as $group_name => $js_list)
734 foreach ($js_list as $item)
735 if ($item['type'] == 'inline')
736 echo '<script type="text/javascript">' . "\n" . trim ($item['script'], "\r\n") . "\n</script>\n";
737 elseif ($item['type'] == 'file')
738 echo "<script type='text/javascript' src='?module=chrome&uri=${item['script']}'></script>\n";
739}
740
d3535346
DO
741function validTagName ($s, $allow_autotag = FALSE)
742{
743 if (1 == preg_match (TAGNAME_REGEXP, $s))
744 return TRUE;
745 if ($allow_autotag and 1 == preg_match (AUTOTAGNAME_REGEXP, $s))
746 return TRUE;
747 return FALSE;
748}
749
0dc380e9
AA
750function cmpTags ($a, $b)
751{
752 global $taglist;
753 if (isset ($a['id']) && isset ($b['id']))
754 {
c09cdace
AA
755 $a_root = array_first ($taglist[$a['id']]['trace']);
756 $b_root = array_first ($taglist[$b['id']]['trace']);
757 if ($a_root < $b_root)
758 return -1;
759 elseif ($a_root > $b_root)
760 return 1;
0dc380e9
AA
761 }
762 elseif (isset ($a['id']))
763 return -1;
764 elseif (isset ($b['id']))
765 return 1;
c09cdace
AA
766
767 return strcmp ($a['tag'], $b['tag']);
0dc380e9
AA
768}
769
221327e3
DO
770function serializeTags ($chain, $baseurl = '')
771{
772 $tmp = array();
0dc380e9 773 usort ($chain, 'cmpTags');
221327e3 774 foreach ($chain as $taginfo)
30bb83bd 775 {
d3ee17b7
AA
776 if ($baseurl == '')
777 $tmp[] = $taginfo['tag'];
30bb83bd 778 else
d3ee17b7 779 {
30bb83bd 780 $title = '';
d3ee17b7
AA
781 if (isset ($taginfo['user']) and isset ($taginfo['time']))
782 $title = 'title="' . htmlspecialchars ($taginfo['user'] . ', ' . formatAge ($taginfo['time']), ENT_QUOTES) . '"';
783 $tmp[] = "<a $title href='${baseurl}cft[]=${taginfo['id']}'>" . $taginfo['tag'] . "</a>";
784 }
30bb83bd 785 }
221327e3
DO
786 return implode (', ', $tmp);
787}
788
2030a7f2
AA
789function startPortlet ($title = '')
790{
791 echo "<div class=portlet><h2>${title}</h2>";
792}
793
794function finishPortlet ()
795{
796 echo "</div>\n";
797}
798
a1d72d2e
AA
799function getPageName ($page_code)
800{
801 global $page;
2b8c5aad 802 $title = isset ($page[$page_code]['title']) ? $page[$page_code]['title'] : callHook ('dynamic_title_decoder' ,$page_code);
a1d72d2e
AA
803 if (is_array ($title))
804 $title = $title['name'];
805 return $title;
806}
807
646272be
AA
808function printTagTRs ($cell, $baseurl = '')
809{
810 if (getConfigVar ('SHOW_EXPLICIT_TAGS') == 'yes' and count ($cell['etags']))
811 {
812 echo "<tr><th width='50%' class=tagchain>Explicit tags:</th><td class=tagchain>";
813 echo serializeTags ($cell['etags'], $baseurl) . "</td></tr>\n";
814 }
815 if (getConfigVar ('SHOW_IMPLICIT_TAGS') == 'yes' and count ($cell['itags']))
816 {
817 echo "<tr><th width='50%' class=tagchain>Implicit tags:</th><td class=tagchain>";
818 echo serializeTags ($cell['itags'], $baseurl) . "</td></tr>\n";
819 }
820 if (getConfigVar ('SHOW_AUTOMATIC_TAGS') == 'yes' and count ($cell['atags']))
821 {
822 echo "<tr><th width='50%' class=tagchain>Automatic tags:</th><td class=tagchain>";
823 echo serializeTags ($cell['atags']) . "</td></tr>\n";
824 }
825}
826
ec143409
AA
827// stub function to override it by chain-connected hooks
828function modifyEntitySummary ($cell, $summary)
829{
830 return $summary;
831}
832
646272be
AA
833// renders 'summary' portlet, which persist on default tab of every realm page.
834// $values is a tricky array.
835// if its value is a string, it is treated as right td inner html, and the key is treated as left th text, colon appends there automatically.
defd92d8 836// 'tags' key has a special meaning: instead of value, the result of printTagTRs call is appended to output
646272be
AA
837// if the value is a single-element array, its value rendered as-is instead of <tr> tag and all its contents.
838// if the value is an array, its first 2 items are treated as left and right contents of row, no colon is appended. Used to enable non-unique titles
839function renderEntitySummary ($cell, $title, $values = array())
840{
841 global $page_by_realm;
54f3cd0a 842 // allow plugins to override summary table
ec143409 843 $values = callHook ('modifyEntitySummary', $cell, $values);
54f3cd0a 844
646272be
AA
845 startPortlet ($title);
846 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
847 foreach ($values as $name => $value)
848 {
849 if (is_array ($value) and count ($value) == 1)
850 {
851 $value = array_shift ($value);
852 echo $value;
853 continue;
854 }
855 if (is_array ($value))
856 {
857 $name = array_shift ($value);
858 $value = array_shift ($value);
859 }
860 elseif (! is_array ($value))
861 $name .= ':';
862 $class = 'tdright';
863 $m = array();
864 if (preg_match('/^\{(.*?)\}(.*)/', $name, $m))
865 {
866 $class .= ' ' . $m[1];
867 $name = $m[2];
868 }
dec748f6 869 if ($name == 'tags:')
646272be
AA
870 {
871 $baseurl = '';
872 if (isset ($page_by_realm[$cell['realm']]))
873 $baseurl = makeHref(array('page'=>$page_by_realm[$cell['realm']], 'tab'=>'default'))."&";
874 printTagTRs ($cell, $baseurl);
875 }
876 else
877 echo "<tr><th width='50%' class='$class'>$name</th><td class=tdleft>$value</td></tr>";
878 }
879 echo "</table>\n";
880 finishPortlet();
881}
882
93a49e5b
AA
883function getOpLink ($params, $title, $img_name = '', $comment = '', $class = '')
884{
885 if (isset ($params))
886 $ret = '<a href="' . makeHrefProcess ($params) . '"';
887 else
888 {
889 $ret = '<a href="#" onclick="return false;"';
890 $class .= ' noclick';
891 }
892 if (! empty ($comment))
893 $ret .= ' title="' . htmlspecialchars ($comment, ENT_QUOTES) . '"';
894 $class = trim ($class);
895 if (! empty ($class))
896 $ret .= ' class="' . htmlspecialchars ($class, ENT_QUOTES) . '"';
897 if (! empty ($comment))
898 $ret .= 'title="' . htmlspecialchars($comment, ENT_QUOTES) . '"';
899 $ret .= '>';
900 if (! empty ($img_name))
901 {
902 $ret .= getImageHREF ($img_name, $comment);
903 if (! empty ($title))
904 $ret .= ' ';
905 }
906 $ret .= $title . '</a>';
907 return $ret;
908}
909
4318ced5
AA
910function renderProgressBar ($percentage = 0, $theme = '', $inline = FALSE)
911{
912 echo getProgressBar ($percentage, $theme, $inline);
913}
914
915function getProgressBar ($percentage = 0, $theme = '', $inline = FALSE)
916{
917 $done = ((int) ($percentage * 100));
918 if (! $inline)
919 $src = "?module=progressbar&done=$done" . (empty ($theme) ? '' : "&theme=${theme}");
920 else
921 {
922 $bk_request = $_REQUEST;
923 $_REQUEST['theme'] = $theme;
924 $src = 'data:image/png;base64,' . chunk_split (base64_encode (getOutputOf ('renderProgressBarImage', $done)));
925 $_REQUEST = $bk_request;
926 header ('Content-type: text/html');
927 }
928 $ret = "<img width=100 height=10 border=0 title='${done}%' src='$src'>";
929 return $ret;
930}
931
0dc380e9
AA
932function renderNetVLAN ($cell)
933{
934 if (! empty ($cell['8021q']))
935 {
936 $seen = array();
937 foreach ($cell['8021q'] as $vlan_info)
938 $seen[$vlan_info['vlan_id']] = $vlan_info['domain_id'] . '-' . $vlan_info['vlan_id'];
939 echo '<div class="vlan"><strong><small>VLAN' . (count ($seen) > 1 ? 'S' : '') . '</small> ';
940 $links = array();
941 foreach ($seen as $vlan_id => $vlan_ck)
942 $links[] = '<a href="' . makeHref (array ('page' => 'vlan', 'vlan_ck' => $vlan_ck)) . '">' . $vlan_id . '</a>';
943 echo implode (', ', $links);
944 echo '</strong></div>';
945 }
946}
947
2b8c5aad
AA
948function includeJQueryUI ($do_css = TRUE)
949{
950 addJS ('js/jquery-ui-1.8.21.min.js');
951 if ($do_css)
952 addCSS ('css/jquery-ui-1.8.22.redmond.css');
953}
954
7419401e
AA
955function getRenderedIPPortPair ($ip, $port = NULL)
956{
957 return "<a href=\"" .
958 makeHref (array ('page' => 'ipaddress', 'tab'=>'default', 'ip' => $ip)) .
959 "\">" . $ip . "</a>" .
960 (isset ($port) ? ":" . $port : "");
961}
962
ca8aa8e9 963?>