r4861 bugfix: generic exception formatting was throwing PHP warnings
[racktables-incomplete-works] / wwwroot / inc / interface-lib.php
CommitLineData
ca8aa8e9
DO
1<?php
2
d3535346
DO
3define ('TAGNAME_REGEXP', '/^[\p{L}0-9]([. _~-]?[\p{L}0-9])*$/u');
4define ('AUTOTAGNAME_REGEXP', '/^\$[\p{L}0-9]([. _~-]?[\p{L}0-9])*$/u');
5
ca8aa8e9
DO
6// Let's have it here, so extensions can add their own images.
7$image = array();
ca8aa8e9
DO
8$image['rackspace']['path'] = 'pix/racks.png';
9$image['rackspace']['width'] = 218;
10$image['rackspace']['height'] = 200;
11$image['objects']['path'] = 'pix/server.png';
12$image['objects']['width'] = 218;
13$image['objects']['height'] = 200;
14$image['depot']['path'] = 'pix/server.png';
15$image['depot']['width'] = 218;
16$image['depot']['height'] = 200;
17$image['files']['path'] = 'pix/files.png';
18$image['files']['width'] = 218;
19$image['files']['height'] = 200;
20$image['ipv4space']['path'] = 'pix/addressspace.png';
21$image['ipv4space']['width'] = 218;
22$image['ipv4space']['height'] = 200;
23$image['ipv6space']['path'] = 'pix/addressspacev6.png';
24$image['ipv6space']['width'] = 218;
25$image['ipv6space']['height'] = 200;
26$image['ipv4slb']['path'] = 'pix/slb.png';
27$image['ipv4slb']['width'] = 218;
28$image['ipv4slb']['height'] = 200;
29$image['config']['path'] = 'pix/configuration.png';
30$image['config']['width'] = 218;
31$image['config']['height'] = 200;
32$image['reports']['path'] = 'pix/report.png';
33$image['reports']['width'] = 218;
34$image['reports']['height'] = 200;
35$image['8021q']['path'] = 'pix/8021q.png';
36$image['8021q']['width'] = 218;
37$image['8021q']['height'] = 200;
38$image['objectlog']['path'] = 'pix/crystal-mimetypes-shellscript-218x200.png';
39$image['objectlog']['width'] = 218;
40$image['objectlog']['height'] = 200;
41$image['virtual']['path'] = 'pix/virtualresources.png';
42$image['virtual']['width'] = 218;
43$image['virtual']['height'] = 200;
44$image['download']['path'] = 'pix/download.png';
45$image['download']['width'] = 16;
46$image['download']['height'] = 16;
47$image['DOWNLOAD']['path'] = 'pix/download-big.png';
48$image['DOWNLOAD']['width'] = 32;
49$image['DOWNLOAD']['height'] = 32;
50$image['plug']['path'] = 'pix/tango-network-wired.png';
51$image['plug']['width'] = 16;
52$image['plug']['height'] = 16;
53$image['cut']['path'] = 'pix/tango-edit-cut-16x16.png';
54$image['cut']['width'] = 16;
55$image['cut']['height'] = 16;
56$image['Cut']['path'] = 'pix/tango-edit-cut-22x22.png';
57$image['Cut']['width'] = 22;
58$image['Cut']['height'] = 22;
75a01117
DO
59$image['Cut gray']['path'] = 'pix/tango-edit-cut-22x22-gray.png';
60$image['Cut gray']['width'] = 22;
61$image['Cut gray']['height'] = 22;
ca8aa8e9
DO
62$image['CUT']['path'] = 'pix/tango-edit-cut-32x32.png';
63$image['CUT']['width'] = 32;
64$image['CUT']['height'] = 32;
65$image['add']['path'] = 'pix/tango-list-add.png';
66$image['add']['width'] = 16;
67$image['add']['height'] = 16;
68$image['ADD']['path'] = 'pix/tango-list-add-big.png';
69$image['ADD']['width'] = 32;
70$image['ADD']['height'] = 32;
71$image['delete']['path'] = 'pix/tango-list-remove.png';
72$image['delete']['width'] = 16;
73$image['delete']['height'] = 16;
74$image['DELETE']['path'] = 'pix/tango-list-remove-32x32.png';
75$image['DELETE']['width'] = 32;
76$image['DELETE']['height'] = 32;
77$image['destroy']['path'] = 'pix/tango-user-trash-16x16.png';
78$image['destroy']['width'] = 16;
79$image['destroy']['height'] = 16;
80$image['nodestroy']['path'] = 'pix/tango-user-trash-16x16-gray.png';
81$image['nodestroy']['width'] = 16;
82$image['nodestroy']['height'] = 16;
83$image['NODESTROY']['path'] = 'pix/tango-user-trash-32x32-gray.png';
84$image['NODESTROY']['width'] = 32;
85$image['NODESTROY']['height'] = 32;
86$image['DESTROY']['path'] = 'pix/tango-user-trash-32x32.png';
87$image['DESTROY']['width'] = 32;
88$image['DESTROY']['height'] = 32;
89$image['nodelete']['path'] = 'pix/tango-list-remove-shadow.png';
90$image['nodelete']['width'] = 16;
91$image['nodelete']['height'] = 16;
92$image['inservice']['path'] = 'pix/tango-emblem-system.png';
93$image['inservice']['width'] = 16;
94$image['inservice']['height'] = 16;
95$image['notinservice']['path'] = 'pix/tango-dialog-error.png';
96$image['notinservice']['width'] = 16;
97$image['notinservice']['height'] = 16;
98$image['find']['path'] = 'pix/tango-system-search.png';
99$image['find']['width'] = 16;
100$image['find']['height'] = 16;
101$image['next']['path'] = 'pix/tango-go-next.png';
102$image['next']['width'] = 32;
103$image['next']['height'] = 32;
104$image['prev']['path'] = 'pix/tango-go-previous.png';
105$image['prev']['width'] = 32;
106$image['prev']['height'] = 32;
107$image['COMMIT']['path'] = 'pix/tango-go-prev-next-32x32.png';
108$image['COMMIT']['width'] = 32;
109$image['COMMIT']['height'] = 32;
110$image['COMMIT gray']['path'] = 'pix/tango-go-prev-next-gray-32x32.png';
111$image['COMMIT gray']['width'] = 32;
112$image['COMMIT gray']['height'] = 32;
113$image['RECALC']['path'] = 'pix/tango-view-refresh-32x32.png';
114$image['RECALC']['width'] = 32;
115$image['RECALC']['height'] = 32;
116$image['clear']['path'] = 'pix/tango-edit-clear.png';
117$image['clear']['width'] = 16;
118$image['clear']['height'] = 16;
119$image['CLEAR']['path'] = 'pix/tango-edit-clear-big.png';
120$image['CLEAR']['width'] = 32;
121$image['CLEAR']['height'] = 32;
122$image['CLEAR gray']['path'] = 'pix/tango-edit-clear-gray-32x32.png';
123$image['CLEAR gray']['width'] = 32;
124$image['CLEAR gray']['height'] = 32;
125$image['save']['path'] = 'pix/tango-document-save-16x16.png';
126$image['save']['width'] = 16;
127$image['save']['height'] = 16;
128$image['SAVE']['path'] = 'pix/tango-document-save-32x32.png';
129$image['SAVE']['width'] = 32;
130$image['SAVE']['height'] = 32;
131$image['NOSAVE']['path'] = 'pix/tango-document-save-32x32-gray.png';
132$image['NOSAVE']['width'] = 32;
133$image['NOSAVE']['height'] = 32;
134$image['create']['path'] = 'pix/tango-document-new.png';
135$image['create']['width'] = 16;
136$image['create']['height'] = 16;
137$image['CREATE']['path'] = 'pix/tango-document-new-big.png';
138$image['CREATE']['width'] = 32;
139$image['CREATE']['height'] = 32;
140$image['DENIED']['path'] = 'pix/tango-dialog-error-big.png';
141$image['DENIED']['width'] = 32;
142$image['DENIED']['height'] = 32;
143$image['node-collapsed']['path'] = 'pix/node-collapsed.png';
144$image['node-collapsed']['width'] = 16;
145$image['node-collapsed']['height'] = 16;
146$image['node-expanded']['path'] = 'pix/node-expanded.png';
147$image['node-expanded']['width'] = 16;
148$image['node-expanded']['height'] = 16;
149$image['node-expanded-static']['path'] = 'pix/node-expanded-static.png';
150$image['node-expanded-static']['width'] = 16;
151$image['node-expanded-static']['height'] = 16;
152$image['dragons']['path'] = 'pix/mitsudragon.png';
153$image['dragons']['width'] = 195;
154$image['dragons']['height'] = 33;
155$image['LB']['path'] = 'pix/loadbalancer.png';
156$image['LB']['width'] = 32;
157$image['LB']['height'] = 32;
158$image['RS pool']['path'] = 'pix/serverpool.png';
159$image['RS pool']['width'] = 48;
160$image['RS pool']['height'] = 16;
161$image['VS']['path'] = 'pix/servicesign.png';
162$image['VS']['width'] = 39;
163$image['VS']['height'] = 62;
164$image['router']['path'] = 'pix/router.png';
165$image['router']['width'] = 32;
166$image['router']['height'] = 32;
167$image['object']['path'] = 'pix/bracket-16x16.png';
168$image['object']['width'] = 16;
169$image['object']['height'] = 16;
170$image['OBJECT']['path'] = 'pix/bracket-32x32.png';
171$image['OBJECT']['width'] = 32;
172$image['OBJECT']['height'] = 32;
173$image['attach']['path'] = 'pix/tango-mail-attachment-16x16.png';
174$image['attach']['width'] = 16;
175$image['attach']['height'] = 16;
176$image['Attach']['path'] = 'pix/tango-mail-attachment-22x22.png';
177$image['Attach']['width'] = 22;
178$image['Attach']['height'] = 22;
179$image['ATTACH']['path'] = 'pix/tango-mail-attachment-32x32.png';
180$image['ATTACH']['width'] = 32;
181$image['ATTACH']['height'] = 32;
182$image['favorite']['path'] = 'pix/tango-emblem-favorite.png';
183$image['favorite']['width'] = 16;
184$image['favorite']['height'] = 16;
185$image['computer']['path'] = 'pix/tango-computer.png';
186$image['computer']['width'] = 16;
187$image['computer']['height'] = 16;
188$image['empty file']['path'] = 'pix/crystal-file-empty-32x32.png';
189$image['empty file']['width'] = 32;
190$image['empty file']['height'] = 32;
191$image['text file']['path'] = 'pix/crystal-file-text-32x32.png';
192$image['text file']['width'] = 32;
193$image['text file']['height'] = 32;
194$image['image file']['path'] = 'pix/crystal-file-image-32x32.png';
195$image['image file']['width'] = 32;
196$image['image file']['height'] = 32;
197$image['text']['path'] = 'pix/tango-text-x-generic-16x16.png';
198$image['text']['width'] = 16;
199$image['text']['height'] = 16;
200$image['NET']['path'] = 'pix/crystal-network_local-32x32.png';
201$image['NET']['width'] = 32;
202$image['NET']['height'] = 32;
203$image['net']['path'] = 'pix/crystal-network_local-16x16.png';
204$image['net']['width'] = 16;
205$image['net']['height'] = 16;
206$image['USER']['path'] = 'pix/crystal-edit-user-32x32.png';
207$image['USER']['width'] = 32;
208$image['USER']['height'] = 32;
209$image['setfilter']['path'] = 'pix/pgadmin3-viewfiltereddata.png';
210$image['setfilter']['width'] = 32;
211$image['setfilter']['height'] = 32;
212$image['setfilter gray']['path'] = 'pix/pgadmin3-viewfiltereddata-grayscale.png';
213$image['setfilter gray']['width'] = 32;
214$image['setfilter gray']['height'] = 32;
215$image['resetfilter']['path'] = 'pix/pgadmin3-viewdata.png';
216$image['resetfilter']['width'] = 32;
217$image['resetfilter']['height'] = 32;
218$image['resetfilter gray']['path'] = 'pix/pgadmin3-viewdata-grayscale.png';
219$image['resetfilter gray']['width'] = 32;
220$image['resetfilter gray']['height'] = 32;
221$image['knight']['path'] = 'pix/smiley_knight.png';
222$image['knight']['width'] = 72;
223$image['knight']['height'] = 33;
224$image['Zoom']['path'] = 'pix/tango-system-search-22x22.png';
225$image['Zoom']['width'] = 22;
226$image['Zoom']['height'] = 22;
227$image['Zooming']['path'] = 'pix/tango-view-fullscreen-22x22.png';
228$image['Zooming']['width'] = 22;
229$image['Zooming']['height'] = 22;
230$image['UNLOCK']['path'] = 'pix/crystal-actions-unlock-32x32.png';
231$image['UNLOCK']['width'] = 32;
232$image['UNLOCK']['height'] = 32;
233$image['CLOCK']['path'] = 'pix/tango-appointment-32x32.png';
234$image['CLOCK']['width'] = 32;
235$image['CLOCK']['height'] = 32;
236$image['DQUEUE done']['path'] = 'pix/crystal-ok-32x32.png';
237$image['DQUEUE done']['width'] = 32;
238$image['DQUEUE done']['height'] = 32;
239$image['DQUEUE sync_aging']['path'] = 'pix/tango-appointment-32x32.png';
240$image['DQUEUE sync_aging']['width'] = 32;
241$image['DQUEUE sync_aging']['height'] = 32;
242$image['DQUEUE resync_aging']['path'] = 'pix/tango-appointment-32x32.png';
243$image['DQUEUE resync_aging']['width'] = 32;
244$image['DQUEUE resync_aging']['height'] = 32;
245$image['DQUEUE sync_ready']['path'] = 'pix/tango-emblem-system-32x32.png';
246$image['DQUEUE sync_ready']['width'] = 32;
247$image['DQUEUE sync_ready']['height'] = 32;
248$image['DQUEUE resync_ready']['path'] = 'pix/tango-emblem-important-32x32.png';
249$image['DQUEUE resync_ready']['width'] = 32;
250$image['DQUEUE resync_ready']['height'] = 32;
251$image['DQUEUE failed']['path'] = 'pix/tango-emblem-unreadable-32x32.png';
252$image['DQUEUE failed']['width'] = 32;
253$image['DQUEUE failed']['height'] = 32;
254$image['DQUEUE disabled']['path'] = 'pix/tango-emblem-readonly-32x32.png';
255$image['DQUEUE disabled']['width'] = 32;
256$image['DQUEUE disabled']['height'] = 32;
257$image['COPY']['path'] = 'pix/tango-edit-copy-32x32.png';
258$image['COPY']['width'] = 32;
259$image['COPY']['height'] = 32;
260$image['html']['path'] = 'pix/tango-text-html.png';
261$image['html']['width'] = 16;
262$image['html']['height'] = 16;
263
646272be
AA
264$page_by_realm = array();
265$page_by_realm['object'] = 'depot';
266$page_by_realm['rack'] = 'rackspace';
267$page_by_realm['ipv4net'] = 'ipv4space';
268$page_by_realm['ipv6net'] = 'ipv6space';
269$page_by_realm['ipv4vs'] = 'ipv4vslist';
270$page_by_realm['ipv4rspool'] = 'ipv4rsplist';
271$page_by_realm['file'] = 'files';
272$page_by_realm['user'] = 'userlist';
273
ca8aa8e9
DO
274function printSelect ($optionList, $select_attrs = array(), $selected_id = NULL)
275{
276 echo getSelect ($optionList, $select_attrs, $selected_id);
277}
278
279// Input array keys are OPTION VALUEs and input array values are OPTION text.
c4ad9ac0 280function getSelect ($optionList, $select_attrs = array(), $selected_id = NULL, $treat_single_special = TRUE)
ca8aa8e9
DO
281{
282 $ret = '';
283 if (!array_key_exists ('name', $select_attrs))
284 return '';
285 // handle two corner cases in a specific way
286 if (count ($optionList) == 0)
287 return '(none)';
c4ad9ac0 288 if (count ($optionList) == 1 && $treat_single_special)
ca8aa8e9
DO
289 {
290 foreach ($optionList as $key => $value) { break; }
291 return "<input type=hidden name=${select_attrs['name']} id=${select_attrs['name']} value=${key}>" . $value;
292 }
293 if (!array_key_exists ('id', $select_attrs))
294 $select_attrs['id'] = $select_attrs['name'];
295 $ret .= '<select';
296 foreach ($select_attrs as $attr_name => $attr_value)
297 $ret .= " ${attr_name}=${attr_value}";
298 $ret .= '>';
299 foreach ($optionList as $dict_key => $dict_value)
300 $ret .= "<option value='${dict_key}'" . ($dict_key == $selected_id ? ' selected' : '') . ">${dict_value}</option>";
301 $ret .= '</select>';
302 return $ret;
303}
304
305function printNiftySelect ($groupList, $select_attrs = array(), $selected_id = NULL, $autocomplete = false)
306{
307 echo getNiftySelect ($groupList, $select_attrs, $selected_id);
308}
309
310// Input is a cooked list of OPTGROUPs, each with own sub-list of OPTIONs in the same
311// format as printSelect() expects.
7232ef01
AD
312// If tree is true, hierarchical drop-boxes are used, otherwise optgroups are used.
313function getNiftySelect ($groupList, $select_attrs, $selected_id = NULL, $tree = false)
ca8aa8e9
DO
314{
315 // special treatment for ungrouped data
316 if (count ($groupList) == 1 and isset ($groupList['other']))
317 return getSelect ($groupList['other'], $select_attrs, $selected_id);
318 if (!array_key_exists ('name', $select_attrs))
319 return '';
320 if (!array_key_exists ('id', $select_attrs))
321 $select_attrs['id'] = $select_attrs['name'];
7232ef01 322 if ($tree)
ca8aa8e9 323 {
2f06f6be
DO
324 # it is safe to call many times for the same file
325 addJS ('js/jquery.optionTree.js');
7232ef01
AD
326 $ret = "<input type=hidden name=${select_attrs['name']}>\n";
327 $ret .= "<script type='text/javascript'>\n";
328 $ret .= "\$(function() {\n";
329 $ret .= " var option_tree = {\n";
330 foreach ($groupList as $groupname => $groupdata)
331 {
332 $ret .= " '${groupname}': {";
333 foreach ($groupdata as $dict_key => $dict_value)
334 $ret .= "\"${dict_value}\":'${dict_key}', ";
335 $ret .= "},\n";
336 }
337 $ret .= " };\n";
30dce055 338 $ret .= " var options = {empty_value: '', choose: 'select...'};\n";
7232ef01
AD
339 $ret .= " \$('input[name=${select_attrs['name']}]').optionTree(option_tree, options);\n";
340 $ret .= "});\n";
341 $ret .= "</script>\n";
342 }
343 else
344 {
345 $ret = '<select';
346 foreach ($select_attrs as $attr_name => $attr_value)
347 $ret .= " ${attr_name}=${attr_value}";
348 $ret .= ">\n";
349 foreach ($groupList as $groupname => $groupdata)
350 {
351 $ret .= "<optgroup label='${groupname}'>\n";
352 foreach ($groupdata as $dict_key => $dict_value)
353 $ret .= "<option value='${dict_key}'" . ($dict_key == $selected_id ? ' selected' : '') . ">${dict_value}</option>\n";
354 $ret .= "</optgroup>\n";
355 }
356 $ret .= "</select>\n";
ca8aa8e9 357 }
ca8aa8e9
DO
358 return $ret;
359}
360
16d86b28
DO
361function getOptionTree ($tree_name, $tree_options, $tree_config = array())
362{
77b5137a
DO
363 function serializeJSArray ($options)
364 {
365 $tmp = array();
366 foreach ($options as $key => $value)
367 $tmp[] = "'${key}': \"${value}\"";
368 return '{' . implode (', ', $tmp) . "}\n";
369 }
370 function serializeJSTree ($tree_options)
16d86b28
DO
371 {
372 $self = __FUNCTION__;
373 $tmp = array();
77b5137a
DO
374 # Leaves on the PHP tree are stored "value => label" way,
375 # non-leaves are stored "label => array" way, and the JS
376 # tree is always built "label => value" or "label => array"
377 # way, hence a structure transform is required.
378 foreach ($tree_options as $key => $value)
379 $tmp[] = is_array ($value) ?
1b655fac
AA
380 '"' . str_replace ('"', '\"', $key) . '": ' . $self ($value) :
381 '"' . str_replace ('"', '\"', $value) . '": "' . str_replace ('"', '\"', $key) . '"';
16d86b28
DO
382 return '{' . implode (', ', $tmp) . "}\n";
383 }
384
385 $default_config = array
386 (
387 'choose' => 'select...',
388 'empty_value' => '',
389 );
390 foreach ($tree_config as $cfgoption_name => $cfgoption_value)
391 $default_config[$cfgoption_name] = $cfgoption_value;
392 # it is safe to call many times for the same file
393 addJS ('js/jquery.optionTree.js');
394 $ret = "<input type=hidden name=${tree_name}>\n";
395 $ret .= "<script type='text/javascript'>\n";
396 $ret .= "\$(function() {\n";
77b5137a 397 $ret .= " var option_tree = " . serializeJSTree ($tree_options) . ";\n";
16d86b28
DO
398 $ret .= " var options = " . serializeJSArray ($default_config) . ";\n";
399 $ret .= " \$('input[name=${tree_name}]').optionTree(option_tree, options);\n";
400 $ret .= "});\n";
401 $ret .= "</script>\n";
402 return $ret;
403}
404
ca8aa8e9
DO
405function printImageHREF ($tag, $title = '', $do_input = FALSE, $tabindex = 0)
406{
407 echo getImageHREF ($tag, $title, $do_input, $tabindex);
408}
409
410// this would be better called mkIMG(), make "IMG" HTML element
411function getImageHREF ($tag, $title = '', $do_input = FALSE, $tabindex = 0)
412{
413 global $image;
414 if (!isset ($image[$tag]))
415 $tag = 'error';
416 $img = $image[$tag];
19f22ad8 417 $img['path'] = '?module=chrome&uri=' . $img['path'];
ca8aa8e9
DO
418 if ($do_input == TRUE)
419 return
420 "<input type=image name=submit class=icon " .
421 "src='${img['path']}' " .
422 "border=0 " .
423 ($tabindex ? "tabindex=${tabindex}" : '') .
424 (!strlen ($title) ? '' : " title='${title}'") . // JT: Add title to input hrefs too
425 ">";
426 else
427 return
428 "<img " .
429 "src='${img['path']}' " .
430 "width=${img['width']} " .
431 "height=${img['height']} " .
432 "border=0 " .
433 (!strlen ($title) ? '' : "title='${title}'") .
434 ">";
435}
436
967e3845
DO
437function dos2unix ($text)
438{
439 return str_replace ("\r\n", "\n", $text);
440}
441
442function escapeString ($value, $do_db_escape = FALSE)
443{
444 $ret = htmlspecialchars ($value, ENT_QUOTES, 'UTF-8');
445 if ($do_db_escape)
446 {
447 global $dbxlink;
448 $ret = substr ($dbxlink->quote ($ret), 1, -1);
449 }
450 return $ret;
451}
452
453function transformRequestData()
454{
455 global $sic;
456 // Magic quotes feature is deprecated, but just in case the local system
457 // still has it activated, reverse its effect.
dbd20c7e
AA
458 $do_magic_quotes = (function_exists ('get_magic_quotes_gpc') and get_magic_quotes_gpc());
459 $seen_keys = array();
460
967e3845
DO
461 // Escape any globals before we ever try to use them, but keep a copy of originals.
462 $sic = array();
4675424f
AA
463 // walk through merged GET and POST instead of REQUEST array because it
464 // can contain cookies with data which could not be decoded from UTF-8
4675424f 465 foreach (array_merge($_GET, $_POST) as $key => $value)
967e3845 466 {
5a7dc9e7
AA
467 if (is_array ($value))
468 $_REQUEST[$key] = $value;
469 else
470 {
471 $value = dos2unix ($value);
472 if ($do_magic_quotes)
473 $value = stripslashes ($value);
474 $_REQUEST[$key] = escapeString ($value);
475 }
476 $sic[$key] = $value;
dbd20c7e 477 $seen_keys[$key] = 1;
967e3845
DO
478 }
479
dbd20c7e
AA
480 // delete cookie information from the $_REQUEST array
481 foreach (array_keys ($_REQUEST) as $key)
482 if (! isset ($seen_keys[$key]))
483 unset ($_REQUEST[$key]);
484
967e3845
DO
485 if (isset ($_SERVER['PHP_AUTH_USER']))
486 $_SERVER['PHP_AUTH_USER'] = escapeString ($_SERVER['PHP_AUTH_USER']);
487 if (isset ($_SERVER['REMOTE_USER']))
488 $_SERVER['REMOTE_USER'] = escapeString ($_SERVER['REMOTE_USER']);
489}
490
ca6698d8
AA
491// JS scripts should be included through this function.
492// They automatically appear in the <head> of your page.
493// $data is a JS filename, or JS code w/o tags around, if $inline = TRUE
494// Scripts are included in the order of adding within the same group, and groups are sorted alphabetically.
495function addJS ($data, $inline = FALSE, $group = 'default')
496{
497 static $javascript = array();
498 static $seen_filenames = array();
499
500 if (! isset ($data))
501 {
502 ksort ($javascript);
503 return $javascript;
504 }
505 // Add jquery.js and racktables.js the first time a Javascript file is added.
506 if (empty($javascript))
507 {
508 $javascript = array
509 (
510 'a_core' => array
511 (
512 array('type' => 'file', 'script' => 'js/jquery-1.4.4.min.js'),
513 array('type' => 'file', 'script' => 'js/racktables.js'),
514 ),
515 );
516
517 // initialize core js filelist
518 foreach ($javascript as $group_name => $group_array)
519 foreach ($group_array as $item)
520 if ($item['type'] == 'file')
521 $seen_filenames[$item['script']] = 1;
522 }
523
524 if ($inline)
525 $javascript[$group][] = array
526 (
527 'type' => 'inline',
528 'script' => $data,
529 );
530 elseif (! isset ($seen_filenames[$data]))
531 {
532 $javascript[$group][] = array
533 (
534 'type' => 'file',
535 'script' => $data,
536 );
537 $seen_filenames[$data] = 1;
538 }
539}
540
541// CSS styles should be included through this function.
542// They automatically appear in the <head> of your page.
543// $data is a CSS filename, or CSS code w/o tags around, if $inline = TRUE
544// Styles are included in the order of adding.
545function addCSS ($data, $inline = FALSE)
546{
547 static $styles = array();
548 static $seen_filenames = array();
549
550 if (! isset ($data))
551 return $styles;
552 if ($inline)
553 $styles[] = array
554 (
555 'type' => 'inline',
556 'style' => $data,
557 );
558 elseif (! isset ($seen_filenames[$data]))
559 {
560 $styles[] = array
561 (
562 'type' => 'file',
563 'style' => $data,
564 );
565 $seen_filenames[$data] = 1;
566 }
567}
568
569// print part of HTML HEAD block
570function printPageHeaders ()
571{
572 global $pageheaders;
573 ksort ($pageheaders);
574 foreach ($pageheaders as $s)
575 echo $s . "\n";
576
577 // add CSS styles
578 foreach (addCSS (NULL) as $item)
579 if ($item['type'] == 'inline')
580 echo '<style type="text/css">' . "\n" . trim ($item['style'], "\r\n") . "\n</style>\n";
581 elseif ($item['type'] == 'file')
582 echo "<link rel=stylesheet type='text/css' href='?module=chrome&uri=${item['style']}' />\n";
583
584 // add JS scripts
585 foreach (addJS (NULL) as $group_name => $js_list)
586 foreach ($js_list as $item)
587 if ($item['type'] == 'inline')
588 echo '<script type="text/javascript">' . "\n" . trim ($item['script'], "\r\n") . "\n</script>\n";
589 elseif ($item['type'] == 'file')
590 echo "<script type='text/javascript' src='?module=chrome&uri=${item['script']}'></script>\n";
591}
592
d3535346
DO
593function validTagName ($s, $allow_autotag = FALSE)
594{
595 if (1 == preg_match (TAGNAME_REGEXP, $s))
596 return TRUE;
597 if ($allow_autotag and 1 == preg_match (AUTOTAGNAME_REGEXP, $s))
598 return TRUE;
599 return FALSE;
600}
601
221327e3
DO
602function serializeTags ($chain, $baseurl = '')
603{
604 $tmp = array();
605 foreach ($chain as $taginfo)
30bb83bd 606 {
d3ee17b7
AA
607 if ($baseurl == '')
608 $tmp[] = $taginfo['tag'];
30bb83bd 609 else
d3ee17b7 610 {
30bb83bd 611 $title = '';
d3ee17b7
AA
612 if (isset ($taginfo['user']) and isset ($taginfo['time']))
613 $title = 'title="' . htmlspecialchars ($taginfo['user'] . ', ' . formatAge ($taginfo['time']), ENT_QUOTES) . '"';
614 $tmp[] = "<a $title href='${baseurl}cft[]=${taginfo['id']}'>" . $taginfo['tag'] . "</a>";
615 }
30bb83bd 616 }
221327e3
DO
617 return implode (', ', $tmp);
618}
619
2030a7f2
AA
620function startPortlet ($title = '')
621{
622 echo "<div class=portlet><h2>${title}</h2>";
623}
624
625function finishPortlet ()
626{
627 echo "</div>\n";
628}
629
a1d72d2e
AA
630function getPageName ($page_code)
631{
632 global $page;
633 $title = isset ($page[$page_code]['title']) ? $page[$page_code]['title'] : dynamic_title_decoder ($page_code);
634 if (is_array ($title))
635 $title = $title['name'];
636 return $title;
637}
638
646272be
AA
639function printTagTRs ($cell, $baseurl = '')
640{
641 if (getConfigVar ('SHOW_EXPLICIT_TAGS') == 'yes' and count ($cell['etags']))
642 {
643 echo "<tr><th width='50%' class=tagchain>Explicit tags:</th><td class=tagchain>";
644 echo serializeTags ($cell['etags'], $baseurl) . "</td></tr>\n";
645 }
646 if (getConfigVar ('SHOW_IMPLICIT_TAGS') == 'yes' and count ($cell['itags']))
647 {
648 echo "<tr><th width='50%' class=tagchain>Implicit tags:</th><td class=tagchain>";
649 echo serializeTags ($cell['itags'], $baseurl) . "</td></tr>\n";
650 }
651 if (getConfigVar ('SHOW_AUTOMATIC_TAGS') == 'yes' and count ($cell['atags']))
652 {
653 echo "<tr><th width='50%' class=tagchain>Automatic tags:</th><td class=tagchain>";
654 echo serializeTags ($cell['atags']) . "</td></tr>\n";
655 }
656}
657
658// renders 'summary' portlet, which persist on default tab of every realm page.
659// $values is a tricky array.
660// 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 661// 'tags' key has a special meaning: instead of value, the result of printTagTRs call is appended to output
646272be
AA
662// if the value is a single-element array, its value rendered as-is instead of <tr> tag and all its contents.
663// 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
664function renderEntitySummary ($cell, $title, $values = array())
665{
666 global $page_by_realm;
667 startPortlet ($title);
668 echo "<table border=0 cellspacing=0 cellpadding=3 width='100%'>\n";
669 foreach ($values as $name => $value)
670 {
671 if (is_array ($value) and count ($value) == 1)
672 {
673 $value = array_shift ($value);
674 echo $value;
675 continue;
676 }
677 if (is_array ($value))
678 {
679 $name = array_shift ($value);
680 $value = array_shift ($value);
681 }
682 elseif (! is_array ($value))
683 $name .= ':';
684 $class = 'tdright';
685 $m = array();
686 if (preg_match('/^\{(.*?)\}(.*)/', $name, $m))
687 {
688 $class .= ' ' . $m[1];
689 $name = $m[2];
690 }
691 if ($name == 'tags:')
692 {
693 $baseurl = '';
694 if (isset ($page_by_realm[$cell['realm']]))
695 $baseurl = makeHref(array('page'=>$page_by_realm[$cell['realm']], 'tab'=>'default'))."&";
696 printTagTRs ($cell, $baseurl);
697 }
698 else
699 echo "<tr><th width='50%' class='$class'>$name</th><td class=tdleft>$value</td></tr>";
700 }
701 echo "</table>\n";
702 finishPortlet();
703}
704
ca8aa8e9 705?>