r5011 bugfix: fetchPortList: wrong field name 'cable_id' renamed to 'cableid'
[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;
261$image['COPY']['path'] = 'pix/tango-edit-copy-32x32.png';
262$image['COPY']['width'] = 32;
263$image['COPY']['height'] = 32;
264$image['html']['path'] = 'pix/tango-text-html.png';
265$image['html']['width'] = 16;
266$image['html']['height'] = 16;
267
646272be
AA
268$page_by_realm = array();
269$page_by_realm['object'] = 'depot';
270$page_by_realm['rack'] = 'rackspace';
271$page_by_realm['ipv4net'] = 'ipv4space';
272$page_by_realm['ipv6net'] = 'ipv6space';
273$page_by_realm['ipv4vs'] = 'ipv4vslist';
274$page_by_realm['ipv4rspool'] = 'ipv4rsplist';
275$page_by_realm['file'] = 'files';
276$page_by_realm['user'] = 'userlist';
277
ca8aa8e9
DO
278function printSelect ($optionList, $select_attrs = array(), $selected_id = NULL)
279{
280 echo getSelect ($optionList, $select_attrs, $selected_id);
281}
282
283// Input array keys are OPTION VALUEs and input array values are OPTION text.
c4ad9ac0 284function getSelect ($optionList, $select_attrs = array(), $selected_id = NULL, $treat_single_special = TRUE)
ca8aa8e9
DO
285{
286 $ret = '';
287 if (!array_key_exists ('name', $select_attrs))
288 return '';
289 // handle two corner cases in a specific way
290 if (count ($optionList) == 0)
291 return '(none)';
c4ad9ac0 292 if (count ($optionList) == 1 && $treat_single_special)
ca8aa8e9
DO
293 {
294 foreach ($optionList as $key => $value) { break; }
295 return "<input type=hidden name=${select_attrs['name']} id=${select_attrs['name']} value=${key}>" . $value;
296 }
297 if (!array_key_exists ('id', $select_attrs))
298 $select_attrs['id'] = $select_attrs['name'];
299 $ret .= '<select';
300 foreach ($select_attrs as $attr_name => $attr_value)
301 $ret .= " ${attr_name}=${attr_value}";
302 $ret .= '>';
303 foreach ($optionList as $dict_key => $dict_value)
304 $ret .= "<option value='${dict_key}'" . ($dict_key == $selected_id ? ' selected' : '') . ">${dict_value}</option>";
305 $ret .= '</select>';
306 return $ret;
307}
308
309function printNiftySelect ($groupList, $select_attrs = array(), $selected_id = NULL, $autocomplete = false)
310{
311 echo getNiftySelect ($groupList, $select_attrs, $selected_id);
312}
313
314// Input is a cooked list of OPTGROUPs, each with own sub-list of OPTIONs in the same
315// format as printSelect() expects.
7232ef01
AD
316// If tree is true, hierarchical drop-boxes are used, otherwise optgroups are used.
317function getNiftySelect ($groupList, $select_attrs, $selected_id = NULL, $tree = false)
ca8aa8e9
DO
318{
319 // special treatment for ungrouped data
320 if (count ($groupList) == 1 and isset ($groupList['other']))
321 return getSelect ($groupList['other'], $select_attrs, $selected_id);
322 if (!array_key_exists ('name', $select_attrs))
323 return '';
324 if (!array_key_exists ('id', $select_attrs))
325 $select_attrs['id'] = $select_attrs['name'];
7232ef01 326 if ($tree)
ca8aa8e9 327 {
2f06f6be
DO
328 # it is safe to call many times for the same file
329 addJS ('js/jquery.optionTree.js');
7232ef01
AD
330 $ret = "<input type=hidden name=${select_attrs['name']}>\n";
331 $ret .= "<script type='text/javascript'>\n";
332 $ret .= "\$(function() {\n";
333 $ret .= " var option_tree = {\n";
334 foreach ($groupList as $groupname => $groupdata)
335 {
336 $ret .= " '${groupname}': {";
337 foreach ($groupdata as $dict_key => $dict_value)
338 $ret .= "\"${dict_value}\":'${dict_key}', ";
339 $ret .= "},\n";
340 }
341 $ret .= " };\n";
30dce055 342 $ret .= " var options = {empty_value: '', choose: 'select...'};\n";
7232ef01
AD
343 $ret .= " \$('input[name=${select_attrs['name']}]').optionTree(option_tree, options);\n";
344 $ret .= "});\n";
345 $ret .= "</script>\n";
346 }
347 else
348 {
349 $ret = '<select';
350 foreach ($select_attrs as $attr_name => $attr_value)
351 $ret .= " ${attr_name}=${attr_value}";
352 $ret .= ">\n";
353 foreach ($groupList as $groupname => $groupdata)
354 {
355 $ret .= "<optgroup label='${groupname}'>\n";
356 foreach ($groupdata as $dict_key => $dict_value)
357 $ret .= "<option value='${dict_key}'" . ($dict_key == $selected_id ? ' selected' : '') . ">${dict_value}</option>\n";
358 $ret .= "</optgroup>\n";
359 }
360 $ret .= "</select>\n";
ca8aa8e9 361 }
ca8aa8e9
DO
362 return $ret;
363}
364
16d86b28
DO
365function getOptionTree ($tree_name, $tree_options, $tree_config = array())
366{
77b5137a
DO
367 function serializeJSArray ($options)
368 {
369 $tmp = array();
370 foreach ($options as $key => $value)
371 $tmp[] = "'${key}': \"${value}\"";
372 return '{' . implode (', ', $tmp) . "}\n";
373 }
374 function serializeJSTree ($tree_options)
16d86b28
DO
375 {
376 $self = __FUNCTION__;
377 $tmp = array();
77b5137a
DO
378 # Leaves on the PHP tree are stored "value => label" way,
379 # non-leaves are stored "label => array" way, and the JS
380 # tree is always built "label => value" or "label => array"
381 # way, hence a structure transform is required.
382 foreach ($tree_options as $key => $value)
383 $tmp[] = is_array ($value) ?
1b655fac
AA
384 '"' . str_replace ('"', '\"', $key) . '": ' . $self ($value) :
385 '"' . str_replace ('"', '\"', $value) . '": "' . str_replace ('"', '\"', $key) . '"';
16d86b28
DO
386 return '{' . implode (', ', $tmp) . "}\n";
387 }
388
389 $default_config = array
390 (
391 'choose' => 'select...',
392 'empty_value' => '',
393 );
394 foreach ($tree_config as $cfgoption_name => $cfgoption_value)
395 $default_config[$cfgoption_name] = $cfgoption_value;
396 # it is safe to call many times for the same file
397 addJS ('js/jquery.optionTree.js');
398 $ret = "<input type=hidden name=${tree_name}>\n";
399 $ret .= "<script type='text/javascript'>\n";
400 $ret .= "\$(function() {\n";
77b5137a 401 $ret .= " var option_tree = " . serializeJSTree ($tree_options) . ";\n";
16d86b28
DO
402 $ret .= " var options = " . serializeJSArray ($default_config) . ";\n";
403 $ret .= " \$('input[name=${tree_name}]').optionTree(option_tree, options);\n";
404 $ret .= "});\n";
405 $ret .= "</script>\n";
406 return $ret;
407}
408
ca8aa8e9
DO
409function printImageHREF ($tag, $title = '', $do_input = FALSE, $tabindex = 0)
410{
411 echo getImageHREF ($tag, $title, $do_input, $tabindex);
412}
413
414// this would be better called mkIMG(), make "IMG" HTML element
415function getImageHREF ($tag, $title = '', $do_input = FALSE, $tabindex = 0)
416{
417 global $image;
418 if (!isset ($image[$tag]))
419 $tag = 'error';
420 $img = $image[$tag];
19f22ad8 421 $img['path'] = '?module=chrome&uri=' . $img['path'];
ca8aa8e9
DO
422 if ($do_input == TRUE)
423 return
424 "<input type=image name=submit class=icon " .
425 "src='${img['path']}' " .
426 "border=0 " .
427 ($tabindex ? "tabindex=${tabindex}" : '') .
428 (!strlen ($title) ? '' : " title='${title}'") . // JT: Add title to input hrefs too
429 ">";
430 else
431 return
432 "<img " .
433 "src='${img['path']}' " .
434 "width=${img['width']} " .
435 "height=${img['height']} " .
436 "border=0 " .
437 (!strlen ($title) ? '' : "title='${title}'") .
438 ">";
439}
440
967e3845
DO
441function dos2unix ($text)
442{
443 return str_replace ("\r\n", "\n", $text);
444}
445
446function escapeString ($value, $do_db_escape = FALSE)
447{
448 $ret = htmlspecialchars ($value, ENT_QUOTES, 'UTF-8');
449 if ($do_db_escape)
450 {
451 global $dbxlink;
452 $ret = substr ($dbxlink->quote ($ret), 1, -1);
453 }
454 return $ret;
455}
456
457function transformRequestData()
458{
459 global $sic;
460 // Magic quotes feature is deprecated, but just in case the local system
461 // still has it activated, reverse its effect.
dbd20c7e
AA
462 $do_magic_quotes = (function_exists ('get_magic_quotes_gpc') and get_magic_quotes_gpc());
463 $seen_keys = array();
464
967e3845
DO
465 // Escape any globals before we ever try to use them, but keep a copy of originals.
466 $sic = array();
4675424f
AA
467 // walk through merged GET and POST instead of REQUEST array because it
468 // can contain cookies with data which could not be decoded from UTF-8
4675424f 469 foreach (array_merge($_GET, $_POST) as $key => $value)
967e3845 470 {
5a7dc9e7
AA
471 if (is_array ($value))
472 $_REQUEST[$key] = $value;
473 else
474 {
475 $value = dos2unix ($value);
476 if ($do_magic_quotes)
477 $value = stripslashes ($value);
478 $_REQUEST[$key] = escapeString ($value);
479 }
480 $sic[$key] = $value;
dbd20c7e 481 $seen_keys[$key] = 1;
967e3845
DO
482 }
483
dbd20c7e
AA
484 // delete cookie information from the $_REQUEST array
485 foreach (array_keys ($_REQUEST) as $key)
486 if (! isset ($seen_keys[$key]))
487 unset ($_REQUEST[$key]);
488
967e3845
DO
489 if (isset ($_SERVER['PHP_AUTH_USER']))
490 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
491 if (isset ($_SERVER['REMOTE_USER']))
492 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
493}
494
ca6698d8
AA
495// JS scripts should be included through this function.
496// They automatically appear in the <head> of your page.
497// $data is a JS filename, or JS code w/o tags around, if $inline = TRUE
498// Scripts are included in the order of adding within the same group, and groups are sorted alphabetically.
499function addJS ($data, $inline = FALSE, $group = 'default')
500{
501 static $javascript = array();
502 static $seen_filenames = array();
503
504 if (! isset ($data))
505 {
506 ksort ($javascript);
507 return $javascript;
508 }
509 // Add jquery.js and racktables.js the first time a Javascript file is added.
510 if (empty($javascript))
511 {
512 $javascript = array
513 (
514 'a_core' => array
515 (
516 array('type' => 'file', 'script' => 'js/jquery-1.4.4.min.js'),
517 array('type' => 'file', 'script' => 'js/racktables.js'),
518 ),
519 );
520
521 // initialize core js filelist
522 foreach ($javascript as $group_name => $group_array)
523 foreach ($group_array as $item)
524 if ($item['type'] == 'file')
525 $seen_filenames[$item['script']] = 1;
526 }
527
528 if ($inline)
529 $javascript[$group][] = array
530 (
531 'type' => 'inline',
532 'script' => $data,
533 );
534 elseif (! isset ($seen_filenames[$data]))
535 {
536 $javascript[$group][] = array
537 (
538 'type' => 'file',
539 'script' => $data,
540 );
541 $seen_filenames[$data] = 1;
542 }
543}
544
545// CSS styles should be included through this function.
546// They automatically appear in the <head> of your page.
547// $data is a CSS filename, or CSS code w/o tags around, if $inline = TRUE
548// Styles are included in the order of adding.
549function addCSS ($data, $inline = FALSE)
550{
551 static $styles = array();
552 static $seen_filenames = array();
553
554 if (! isset ($data))
555 return $styles;
556 if ($inline)
557 $styles[] = array
558 (
559 'type' => 'inline',
560 'style' => $data,
561 );
562 elseif (! isset ($seen_filenames[$data]))
563 {
564 $styles[] = array
565 (
566 'type' => 'file',
567 'style' => $data,
568 );
569 $seen_filenames[$data] = 1;
570 }
571}
572
573// print part of HTML HEAD block
574function printPageHeaders ()
575{
576 global $pageheaders;
577 ksort ($pageheaders);
578 foreach ($pageheaders as $s)
579 echo $s . "\n";
580
581 // add CSS styles
582 foreach (addCSS (NULL) as $item)
583 if ($item['type'] == 'inline')
584 echo '<style type="text/css">' . "\n" . trim ($item['style'], "\r\n") . "\n</style>\n";
585 elseif ($item['type'] == 'file')
586 echo "<link rel=stylesheet type='text/css' href='?module=chrome&uri=${item['style']}' />\n";
587
588 // add JS scripts
589 foreach (addJS (NULL) as $group_name => $js_list)
590 foreach ($js_list as $item)
591 if ($item['type'] == 'inline')
592 echo '<script type="text/javascript">' . "\n" . trim ($item['script'], "\r\n") . "\n</script>\n";
593 elseif ($item['type'] == 'file')
594 echo "<script type='text/javascript' src='?module=chrome&uri=${item['script']}'></script>\n";
595}
596
d3535346
DO
597function validTagName ($s, $allow_autotag = FALSE)
598{
599 if (1 == preg_match (TAGNAME_REGEXP, $s))
600 return TRUE;
601 if ($allow_autotag and 1 == preg_match (AUTOTAGNAME_REGEXP, $s))
602 return TRUE;
603 return FALSE;
604}
605
221327e3
DO
606function serializeTags ($chain, $baseurl = '')
607{
608 $tmp = array();
609 foreach ($chain as $taginfo)
30bb83bd 610 {
d3ee17b7
AA
611 if ($baseurl == '')
612 $tmp[] = $taginfo['tag'];
30bb83bd 613 else
d3ee17b7 614 {
30bb83bd 615 $title = '';
d3ee17b7
AA
616 if (isset ($taginfo['user']) and isset ($taginfo['time']))
617 $title = 'title="' . htmlspecialchars ($taginfo['user'] . ', ' . formatAge ($taginfo['time']), ENT_QUOTES) . '"';
618 $tmp[] = "<a $title href='${baseurl}cft[]=${taginfo['id']}'>" . $taginfo['tag'] . "</a>";
619 }
30bb83bd 620 }
221327e3
DO
621 return implode (', ', $tmp);
622}
623
2030a7f2
AA
624function startPortlet ($title = '')
625{
626 echo "<div class=portlet><h2>${title}</h2>";
627}
628
629function finishPortlet ()
630{
631 echo "</div>\n";
632}
633
a1d72d2e
AA
634function getPageName ($page_code)
635{
636 global $page;
637 $title = isset ($page[$page_code]['title']) ? $page[$page_code]['title'] : dynamic_title_decoder ($page_code);
638 if (is_array ($title))
639 $title = $title['name'];
640 return $title;
641}
642
646272be
AA
643function printTagTRs ($cell, $baseurl = '')
644{
645 if (getConfigVar ('SHOW_EXPLICIT_TAGS') == 'yes' and count ($cell['etags']))
646 {
647 echo "<tr><th width='50%' class=tagchain>Explicit tags:</th><td class=tagchain>";
648 echo serializeTags ($cell['etags'], $baseurl) . "</td></tr>\n";
649 }
650 if (getConfigVar ('SHOW_IMPLICIT_TAGS') == 'yes' and count ($cell['itags']))
651 {
652 echo "<tr><th width='50%' class=tagchain>Implicit tags:</th><td class=tagchain>";
653 echo serializeTags ($cell['itags'], $baseurl) . "</td></tr>\n";
654 }
655 if (getConfigVar ('SHOW_AUTOMATIC_TAGS') == 'yes' and count ($cell['atags']))
656 {
657 echo "<tr><th width='50%' class=tagchain>Automatic tags:</th><td class=tagchain>";
658 echo serializeTags ($cell['atags']) . "</td></tr>\n";
659 }
660}
661
662// renders 'summary' portlet, which persist on default tab of every realm page.
663// $values is a tricky array.
664// 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 665// 'tags' key has a special meaning: instead of value, the result of printTagTRs call is appended to output
646272be
AA
666// if the value is a single-element array, its value rendered as-is instead of <tr> tag and all its contents.
667// 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
668function renderEntitySummary ($cell, $title, $values = array())
669{
670 global $page_by_realm;
671 startPortlet ($title);
672 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
673 foreach ($values as $name => $value)
674 {
675 if (is_array ($value) and count ($value) == 1)
676 {
677 $value = array_shift ($value);
678 echo $value;
679 continue;
680 }
681 if (is_array ($value))
682 {
683 $name = array_shift ($value);
684 $value = array_shift ($value);
685 }
686 elseif (! is_array ($value))
687 $name .= ':';
688 $class = 'tdright';
689 $m = array();
690 if (preg_match('/^\{(.*?)\}(.*)/', $name, $m))
691 {
692 $class .= ' ' . $m[1];
693 $name = $m[2];
694 }
695 if ($name == 'tags:')
696 {
697 $baseurl = '';
698 if (isset ($page_by_realm[$cell['realm']]))
699 $baseurl = makeHref(array('page'=>$page_by_realm[$cell['realm']], 'tab'=>'default'))."&";
700 printTagTRs ($cell, $baseurl);
701 }
702 else
703 echo "<tr><th width='50%' class='$class'>$name</th><td class=tdleft>$value</td></tr>";
704 }
705 echo "</table>\n";
706 finishPortlet();
707}
708
93a49e5b
AA
709function getOpLink ($params, $title, $img_name = '', $comment = '', $class = '')
710{
711 if (isset ($params))
712 $ret = '<a href="' . makeHrefProcess ($params) . '"';
713 else
714 {
715 $ret = '<a href="#" onclick="return false;"';
716 $class .= ' noclick';
717 }
718 if (! empty ($comment))
719 $ret .= ' title="' . htmlspecialchars ($comment, ENT_QUOTES) . '"';
720 $class = trim ($class);
721 if (! empty ($class))
722 $ret .= ' class="' . htmlspecialchars ($class, ENT_QUOTES) . '"';
723 if (! empty ($comment))
724 $ret .= 'title="' . htmlspecialchars($comment, ENT_QUOTES) . '"';
725 $ret .= '>';
726 if (! empty ($img_name))
727 {
728 $ret .= getImageHREF ($img_name, $comment);
729 if (! empty ($title))
730 $ret .= ' ';
731 }
732 $ret .= $title . '</a>';
733 return $ret;
734}
735
ca8aa8e9 736?>