Merge branch 'master' of ssh://git.onelab.eu/git/plewww
[plewww.git] / planetlab / includes / plc_functions.php
1 <?php
2
3 // $Id$
4
5 # note: this needs to be consistent with the value in Monitor/monitor/wrapper/plc.py
6 global $PENDING_CONSORTIUM_ID;
7 $PENDING_CONSORTIUM_ID = 0;
8 # this one does not matter that much, just picking one
9 global $APPROVED_CONSORTIUM_ID;
10 $APPROVED_CONSORTIUM_ID = 999999;
11
12 // utility
13 function my_is_int ($x) {
14     return (is_numeric($x) ? intval($x) == $x : false);
15 }
16
17 //////////////////////////////////////////////////////////// roles & other checks on global $plc
18 function plc_is_admin () {
19   global $plc;
20   return in_array( 10, $plc->person['role_ids']);
21 }
22 function plc_is_pi () {
23   global $plc;
24   return in_array( 20, $plc->person['role_ids']);
25 }
26 function plc_is_user () {
27   global $plc;
28   return in_array( 30, $plc->person['role_ids']);
29 }
30 function plc_is_tech () {
31   global $plc;
32   return in_array( 40, $plc->person['role_ids']);
33 }
34 function plc_in_site ($site_id) {
35   global $plc;
36   return in_array( $site_id, $plc->person['site_ids']);
37 }
38
39 function plc_my_site_ids () {
40   global $plc;
41   return $plc->person['site_ids'];
42 }
43 function plc_my_sites () {
44   global $plc;
45   global $api;
46   return $api->GetSites($plc->person['site_ids'], array('site_id', 'abbreviated_name'));
47 }
48
49 function plc_my_site_id () {
50   global $plc;
51   return $plc->person['site_ids'][0];
52 }
53
54 function plc_my_person () {
55   return $plc->person;
56 }
57 function plc_my_person_id () {
58   global $plc;
59   return $plc->person['person_id'];
60 }
61
62 //////////////////////////////////////////////////////////// links    
63 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
64
65 // naming scheme is
66 // l_objects()                  -> the url to the page that list objects
67 // l_object($object_id)         -> the url to hte page thas details object with given id
68 // l_object_t($object_id,text)  -> an <a> tag that shows text and links to the above
69 // l_object_add ()              -> the url to that object-afding page
70
71 function l_actions ()                   { return "/db/common/actions.php"; }
72 // some complex node actions are kept separate, e.g. the ones related to getbootmedium
73 function l_actions_download ()          { return "/db/nodes/node_downloads.php"; }
74 function l_register_node ()             { return "/registerwizard/index.php"; }
75 function l_pcu_add ()                   { return "/registerwizard/index.php/register/stage1_addpcu"; }
76 function l_pcu ($pcu_id)                { return "/db/sites/pcu.php?id=$pcu_id"; }
77
78 function l_nodes ()                     { return "/db/nodes/index.php"; }
79 function l_nodes_peer ($peer_id)        { return "/db/nodes/index.php?peerscope=$peer_id"; }
80 function l_node ($node_id)              { return "/db/nodes/node.php?id=$node_id"; }
81 function l_node_interfaces ($node_id)   { return "/db/nodes/node.php?id=$node_id%show_details=0&show_interfaces=1"; }
82 function l_node_tags ($node_id)         { return "/db/nodes/node.php?id=$node_id%show_details=0&show_tags=1"; }
83 function l_node_t ($node_id,$text)      { return href (l_node($node_id),$text); }
84 function l_node_obj($node)              { return href(l_node($node['node_id']),$node['hostname']); }
85 function l_node_add ()                  { return "/db/nodes/node_add.php"; }
86 function l_nodes_site ($site_id)        { return "/db/nodes/index.php?site_id=$site_id"; }
87 function l_nodes_my_site ()             { return l_nodes_site(plc_my_site_id()) . "&active_line_tab=My site nodes"; }
88 function l_nodes_all_my_site ()         { return l_nodes_person(plc_my_person_id()) . "&active_line_tab=All My site nodes"; }
89 function l_nodes_person ($person_id)    { return "/db/nodes/index.php?person_id=$person_id"; }
90 function l_nodes_slice ($slice_id)      { return "/db/nodes/index.php?slice_id=$slice_id"; }
91
92 function l_interface ($interface_id)    { return "/db/nodes/interface.php?id=$interface_id"; }
93 function l_interface_tags($interface_id){ return "/db/nodes/interface.php?id=$interface_id&show_details=0&show_tags=1"; }
94 function l_interface_t ($interface_id,$text) { 
95                                           return href (l_interface($interface_id),$text); }
96 function l_interface_add($node_id)      { return "/db/nodes/interface.php?node_id=$node_id"; }
97
98 function l_sites ()                     { return "/db/sites/index.php"; }
99 function l_sites_peer ($peer_id)        { return "/db/sites/index.php?peerscope=$peer_id"; }
100 function l_site ($site_id)              { return "/db/sites/index.php?id=$site_id"; }
101 function l_site_t ($site_id,$text)      { return href (l_site($site_id),$text); }
102 function l_site_obj($site)              { return href (l_site($site['site_id']),$site['name']); }
103
104 function l_slices ()                    { return "/db/slices/index.php"; }
105 function l_slices_peer ($peer_id)       { return "/db/slices/index.php?peerscope=$peer_id"; }
106 function l_slice ($slice_id)            { return "/db/slices/index.php?id=$slice_id"; }
107 function l_slice_nodes ($slice_id)      { return "/db/slices/index.php?id=$slice_id&show_details=0&show_nodes=1&show_nodes_current=1&show_nodes_add=1"; }
108 function l_slice_t ($slice_id,$text)    { return href (l_slice($slice_id),$text); }
109 function l_slice_add ()                 { return "/db/slices/slice_add.php"; }
110 function l_slices_site($site_id)        { return "/db/slices/index.php?site_id=$site_id"; }
111 function l_slices_my_site()             { return l_slices_site(plc_my_site_id()) . "&active_line_tab=My site slices"; }
112 function l_slices_person($person_id)    { return "/db/slices/index.php?person_id=$person_id"; }
113 function l_slices_local()               { return "/db/slices/index.php?peerscope=local"; }
114 // from an object
115 function l_slice_obj ($slice)           { return l_slice_t ($slice['slice_id'],$slice['name']); }
116
117 function l_sliver ($node_id,$slice_id)  { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; }
118 function l_sliver_t ($node_id,$slice_id,$text) { 
119                                           return href (l_sliver($node_id,$slice_id),$text) ; }
120
121 function l_persons ()                   { return "/db/persons/index.php&active_line_tab=All Accounts"; }
122 function l_persons_peer ($peer_id)      { return "/db/persons/index.php?peerscope=$peer_id&active_line_tab=Local Accounts"; }
123 function l_person ($person_id)          { return "/db/persons/index.php?id=$person_id"; }
124 function l_person_roles ($person_id)    { return "/db/persons/index.php?id=$person_id&show_details=0&show_roles=1"; }
125 function l_person_t ($person_id,$text)  { return href (l_person($person_id),$text); }
126 function l_persons_site ($site_id)      { return "/db/persons/index.php?site_id=$site_id"; }
127 function l_persons_slice ($slice_id)    { return "/db/persons/index.php?slice_id=$slice_id"; }
128 function l_person_obj ($person)         { return l_person_t($person['person_id'],$person['email']); }
129
130 function l_tags ()                      { return "/db/tags/index.php"; }
131 function l_tag ($tag_type_id)           { return "/db/tags/index.php?id=$tag_type_id"; }
132 function l_tag_obj ($tag)               { return href(l_tag($tag['tag_type_id']),$tag['tagname']); }
133 function l_tag_roles ($tag_type_id)     { return "/db/tags/index.php?id=$tag_type_id&show_details=0&show_roles=1"; }
134
135 function l_nodegroups ()                { return "/db/tags/nodegroups.php"; }
136 function l_nodegroup ($nodegroup_id)    { return "/db/tags/nodegroup.php?id=$nodegroup_id"; }
137 function l_nodegroup_t ($nodegroup_id,$text) { 
138                                           return href(l_nodegroup($nodegroup_id),$text); }
139 function l_nodegroup_obj ($nodegroup) { 
140                                           return href(l_nodegroup($nodegroup['nodegroup_id']),$nodegroup['groupname']); }
141
142 function l_events ()                    { return "/db/events/index.php"; }
143 function l_event ($type,$param,$id)     { return "/db/events/index.php?type=$type&$param=$id"; }
144
145 function l_peers()                      { return "/db/peers/index.php"; }
146 function l_peer($peer_id)               { return "/db/peers/index.php?id=$peer_id"; }
147 function l_peer_t($peer_id,$text)       { return href(l_peer($peer_id),$text); }
148
149 function l_comon($id_name,$id_value)    { return "/db/nodes/comon.php?$id_name=$id_value"; }
150 function l_sirius()                     { return "/db/sirius/index.php"; }
151 function l_about()                      { return "/db/common/about.php"; }
152 function l_doc_plcapi()                 { return "/db/doc/PLCAPI.php"; }
153 function l_doc_nmapi()                  { return "/db/doc/NMAPI.php"; }
154 function l_admin()                      { return "/db/common/adminsearch.php"; }
155
156 function l_login()                      { return "/db/common/login.php"; }
157 function l_logout()                     { return "/planetlab/common/logout.php"; }
158 function l_sulogout()                   { return "/planetlab/common/sulogout.php"; }
159 function l_reset_password()             { return "/db/persons/reset_password.php"; }
160 function l_person_register()            { return "/db/persons/register.php"; }
161 function l_site_register()              { return "/db/sites/register.php"; }
162 function l_sites_pending()              { return "/db/sites/join_request.php"; }
163 function l_site_review_pending($site_id){ return "/db/sites/join_request.php?review=t&site_id=$site_id"; }
164
165
166 //////////////////////////////////////////////////////////// nav tabs
167 function tab_nodes ()           { return array ('label'=>'All nodes','url'=>l_nodes(), 
168                                                 'bubble'=>'Display nodes from all peers'); }
169 function tab_nodes_local ()     { return array ('label'=>'Local nodes', 'url'=>l_nodes_peer('local'), 
170                                                 'bubble'=>'Display all nodes local to this peer'); }
171 function tab_nodes_site($site_id){ return array ('label'=>'Site nodes', 'url'=>l_nodes_site($site_id), 
172                                                  'bubble'=>'Display nodes on that site'); }
173 function tab_nodes_slice($slice_id){ return array ('label'=>'Slice nodes', 'url'=>l_nodes_slice($slice_id), 
174                                                  'bubble'=>'Display nodes for that slice'); }
175 function tab_nodes_mysite ()    { return array ('label'=>'My site nodes', 'url'=>l_nodes_my_site(), 
176                                                 'bubble'=>'Display nodes on my site'); }
177 function tab_nodes_all_mysite (){ return array ('label'=>'All My site nodes', 'url'=>l_nodes_all_my_site(),
178                                                 'bubble'=>'Display nodes on all my sites'); }
179 function tab_node($node)        { return array ('label'=>'Node '.$node['hostname'], 'url'=>l_node($node['node_id']),
180                                                 'bubble'=>'Details for ' . $node['hostname']); }
181 //////////
182 function tab_site($site)        { return array ('label'=>'Site '.$site['login_base'], 'url'=>l_site($site['site_id']),
183                                                 'bubble'=>'Details for ' . $site['name']); }
184 function tab_mysite()           { return array ('label'=>'My site', 'url'=>l_site(plc_my_site_id()),
185                                                 'bubble'=>'Details for site ' . plc_my_site_id()); }
186 function tab_sites ()           { return array ('label'=>'All sites' , 'url'=>l_sites(), 'bubble'=> 'Display all sites'); }
187 function tab_sites_local ()     { return array ('label'=>'Local sites' , 'url'=>l_sites_peer('local'), 
188                                                 'bubble'=> 'Display all siteslocal to this peer'); }
189 //////////
190 function tab_slices()           { return array ('label'=>'All slices', 'url'=>l_slices(),
191                                                 'bubble' => 'Display all slices'); }
192 function tab_slices_person()    { return array ('label'=>'My slices', 'url'=>l_slices_person(plc_my_person_id()),
193                                                 'bubble' => 'Display my all slices'); }
194 function tab_slices_mysite ()   { return array ('label'=>'My site slices', 'url'=>l_slices_my_site(), 
195                                                 'bubble'=>'Display all slices on my site'); }
196 function tab_slices_local ()    { return array ('label'=>'Local slices', 'url'=>l_slices_local(), 
197                                                 'bubble'=>'Display all slices local to this peer'); }
198 function tab_slice($slice)      { return array ('label'=>'Slice '.$slice['name'], 'url'=>l_slice($slice['slice_id']),
199                                                 'bubble' => 'Details for ' . $slice['name']); }
200 //////////
201 function tab_persons()          { return array ('label'=>'All Accounts', 'url'=>l_persons(),
202                                                 'bubble'=>'Display users from all peers'); }
203 function tab_persons_local()    { return array ('label'=>'Local Accounts', 'url'=>l_persons_peer('local'),
204                                                 'bubble'=>'Display all users local to this peer'); }
205 function tab_persons_mysite()   { return array ('label'=>'My site accounts' , 'url'=>l_persons_site(plc_my_site_id()),
206                                                 'bubble'=>'Display accounts on site ' . plc_my_site_id()); }
207 function tab_person($person)    { return array ('label'=>'Account '.$person['email'], 'url'=>l_person($person['person_id']),
208                                                 'bubble'=>'Details for ' . $person['email']); }
209 //////////
210 function tab_tags()             { return array ('label'=>'Tag Types', 'url'=>l_tags(),
211                                                 'bubble' => 'Display and create tag types'); }
212 function tab_nodegroups()       { return array ('label'=>'Nodegroups', 'url'=>l_nodegroups(),
213                                                 'bubble' => 'Display and create nodegroups'); }
214
215 // only partial tab
216 function tablook_event()        { return array('image'=>'/planetlab/icons/event.png','height'=>18);}
217 function tablook_comon()        { return array('image'=>'/planetlab/icons/comon.png','height'=>18);}
218
219
220
221 //////////////////////////////////////////////////////////// validation functions
222 function topdomain ($hostname) {
223   $exploded=array_reverse(explode(".",$hostname));
224   return $exploded[0];
225 }
226
227 //// with php-5.3 on f12, ereg is marked deprecated, using PCRE instead
228 //// looks unused
229 // function is_valid_email_addr ($email) {
230 //  if (preg_match("/^.+@.+\\..+$/", $email) ) {
231 //    return true;
232 //  } else {
233 //    return false;
234 //  }
235 //}
236 //
237 //// looks unused
238 //function is_valid_url ($url) {
239 //  if (preg_match("/^(http|https):\/\/.+\..+$/", strtolower($url) ) ) {
240 //    return true;
241 //  } else {
242 //    return false;
243 //  }
244 //}
245
246 function is_valid_ip ($ip) {
247   if (preg_match("/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/", $ip ) ) {
248       // it's at least in the right format, now check to see if
249       // each part is equal to less than 255
250       $parts= explode( '.', $ip );
251       $count= count($parts);
252
253       for( $i= 0; $i < $count; $i++ ) {
254         if( intval($parts[$i]) > 255 )
255           return false;
256       }
257
258       return true;
259   } else {
260     return false;
261   }
262 }
263
264 function is_valid_network_addr($network_addr,$mask) {
265   $lNetwork= ip2long($network_addr);
266   $lMask= ip2long($mask);
267
268   // are they the correct format?
269   if( $lNetwork == -1 || $lMask == -1 )
270     return false;
271
272   // is network address valid for the mask?
273   if( ($lNetwork & $lMask) != $lNetwork )
274     return false;
275
276   return true;
277 }
278
279
280 // returns whether or not a network address is in the reserved space
281 // in the case of a invalid network address, false will be returned.
282 function is_reserved_network_addr($network_addr) {
283   $lNetwork= ip2long($network_addr);
284
285   if( $lNetwork == -1 )
286     return false;
287
288   // does the network address fall in a reserved block?
289   $reserved_ips = array (
290                          array('10.0.0.0','10.255.255.255'),
291                          array('172.16.0.0','172.31.0.0'),
292                          array('192.168.0.0','192.168.255.0')
293                          );
294   foreach ($reserved_ips as $r) {
295     $min = ip2long($r[0]);
296     $max = ip2long($r[1]);
297       
298     if (($lNetwork >= $min) && ($lNetwork <= $max))
299       return true;
300   }
301
302   return false;
303 }
304
305 ////////////////////////////////////////////////////////////  roles
306 function plc_role_global_hash ($api) {
307   $hash=array();
308   $roles=$api->GetRoles();
309   foreach ($roles as $role) {
310     $hash[$role['role_id']]=$role['name'];
311   }
312   return $hash;
313 }
314
315 // because GetRoles does not correctly support filters, it's really painful to do this
316 function sort_roles ($r1, $r2) { return $r2['role_id'] - $r1['role_id']; }
317 function roles_except ($roles, $exception_ids) {
318   $result=array();
319   if ($roles) foreach ($roles as $role) {
320       if ( ! in_array ($role['role_id'],$exception_ids) ) {
321         $result[]=$role;
322       }
323     }
324   usort($result,sort_roles);
325   return $result;
326 }
327
328 //////////////////////////////////////////////////////////// nodegroups
329 // hash by 'tagname=value'
330 function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
331   $filter=NULL;
332   // xxx somehow this does not work; I've checked that the feature is working from plcsh
333   // but I suspect the php marshalling or something; no time to fix, get all nodegroups for now
334   // if ($tagnames) $filter=array("tagname"=>$tagnames);
335   $nodegroups=$api->GetNodeGroups($filter);
336   $hash=array();
337   if ($nodegroups) foreach ($nodegroups as $nodegroup) {
338       $key=$nodegroup['tagname']."=".$nodegroup['value'];
339       $hash[$key]=$nodegroup;
340     }
341   return $hash;
342 }
343   
344 //////////////////////////////////////////////////////////// titles
345 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
346 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
347 function t_person ($person) { return " belonging to " . $person['email'] . " (" . $person['person_id'] . ")"; }
348
349 //////////////////////////////////////////////////////////// html fragments
350 function plc_vertical_table ($messages, $class="") {
351   if ( empty( $messages) ) return "";
352   $formatted = "";
353   $formatted .= "<table";
354   if ($class) $formatted .= " class='" . $class . "'";
355   $formatted .= ">";
356   foreach ($messages as $message) {
357     $formatted .= "<tr><td>" . $message . "</td></tr>";
358   }
359   $formatted .= "</table>";
360   return $formatted;
361 }
362 function plc_itemize ($messages, $class="") {
363   if ( empty( $messages) ) return "";
364   $formatted = "";
365   $formatted .= "<ul";
366   if ($class) $formatted .= " class='" . $class . "'";
367   $formatted .= ">";
368   foreach ($messages as $message) {
369     $formatted .= "<li>" . $message . "</li>";
370   }
371   $formatted .= "</ul>";
372   return $formatted;
373 }
374
375 ////////// just return a truncated text
376 function truncate ($text,$numb,$etc = "...") {
377   if (strlen($text) <= $numb)   return $text;
378   return substr($text, 0, $numb).$etc;
379 }
380 // ditto but in case the text is too lare, returns a <span> with its 'title' set to the full value
381 function truncate_and_popup ($text,$numb,$etc = "...") {
382   if (strlen($text) <= $numb)   return $text;
383   $display=substr($text, 0, $numb).$etc;
384   return sprintf("<span title='%s'>%s</span>",$text,$display);
385 }
386   
387 // generates <(atom) class=(class)> (text) </(atom)>
388 function html_atom ($atom,$text,$class="") {
389   $html="<$atom";
390   if ($class) $html .= " class='$class'";
391   $html .= ">$text</$atom>";
392   return $html;
393 }
394 function html_div ($text,$class="") { return html_atom ('div',$text,$class); }
395 function html_span ($text,$class="") { return html_atom ('span',$text,$class); }
396
397 // should use the same channel as the php errors..
398 function plc_error_html ($text)         { return  html_div ($text,'plc-error'); }
399 function plc_error ($text)              { print plc_error_html ("Error " . $text); }
400
401 function errors_init() { return array();}
402 function errors_record ($adm, $errors) {
403   if ($adm->error()) {
404     $tmp=$adm->error();
405     $errors []= $tmp;
406   }
407   return $errors;
408 }
409
410 function errors_display ($errors) {
411   if ($errors) {
412     print( "<div class='plc-error'>" );
413     print( "<p>The following errors occured:</p>" );
414     print("<ul>");
415     foreach( $errors as $error ) 
416       print( "<li>$error</li>\n" );
417     print( "</ul></div>\n" );
418   }
419 }
420
421 function plc_warning_html ($text)       { return html_span($text,'plc-warning'); }
422 function plc_warning ($text)            { print plc_warning_html("Warning " . $text); }
423
424 function bold_html ($text)              { return html_span($text,'bold'); }
425
426 // shows a php variable verbatim with a heading message
427 function plc_debug ($message,$object) {
428   print "<br />" . $message . "<pre>";
429   print_r ($object);
430   print "</pre>";
431 }
432
433 $plc_prof_start=0.;
434 $plc_prof_time=0.;
435 $plc_prof_counter=0;
436 function plc_debug_prof_start () {
437   global $plc_prof_counter, $plc_prof_start, $plc_prof_time;
438   $plc_prof_counter=0;
439   plc_debug(strftime("[0] %T (start)") ,"heating up");
440   $plc_prof_time=microtime(true);
441   $plc_prof_start=$plc_prof_time;
442 }
443 function plc_debug_prof ($message,$object) {
444   global $plc_prof_counter, $plc_prof_start, $plc_prof_time;
445   $plc_prof_counter+=1;
446   $now=microtime(true);
447   $timelabel=strftime("%T");
448   $prof_message=sprintf("[%d] %s (%2.3f s -- %2.3f s) ",$plc_prof_counter,$timelabel,
449                         ($now-$plc_prof_time),($now-$plc_prof_start));
450   plc_debug($prof_message.$message,$object);
451   $plc_prof_time=$now;
452 }
453 function plc_debug_prof_end () {
454   plc_debug_prof ("end","cooling down");
455 }
456
457 if (! function_exists ("drupal_set_error")) {
458   function drupal_set_error ($text) {
459     drupal_set_message ("<span class=error>$text</span>");
460   }
461  }
462
463 //////////////////////////////////////////////////////////// sort out for obsolete / trash
464 // builds a table from an array of strings, with the given class
465 // attempt to normalize the delete buttons and confirmations
466 function plc_delete_icon($width=15) {
467   return "<img width='$width' src='/planetlab/icons/delete.png'>";
468 }
469
470 function plc_add_icon($width=15) {
471   return "<img width='$width' src='/planetlab/icons/add.png'>";
472 }
473
474 function plc_bubble($text,$bubble) {
475   return "<span title='$bubble'>$text</span>";
476 }
477 function plc_delete_icon_bubble ($bubble,$width=15) {
478   return plc_bubble(plc_delete_icon($width),$bubble);
479 }
480
481 function plc_event_button($type,$param,$id) {
482   return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
483 }
484
485 function plc_comon_button ($id_name, $id_value,$target="") {
486   $result='<a ';
487   if (!empty($target)) {
488     $result.='target="' . $target . '" ';
489   }
490   $result.='href="' . l_comon($id_name,$id_value) . '">';
491   $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
492   return $result;
493 }
494
495 ////////////////////
496 function plc_redirect ($url) {
497   header ("Location: " . $url);
498   exit ();
499 }
500
501 //////////////////// the options for an interface - suitable for plekit/form
502 //>>> GetNetworkMethods()
503 //[u'static', u'dhcp', u'proxy', u'tap', u'ipmi', u'unknown']
504 function interface_method_selectors ($api, $method, $primary) {
505   if ($primary) {
506     $builtin_methods=array("static"=>"Static",
507                            "dhcp"=>"DHCP");
508   } else {
509     $builtin_methods=array("static"=>"Static",
510                            "dhcp"=>"DHCP", 
511                            "proxy"=>"Proxy",  
512                            "tap"=>"TUN/TAP",
513                            "ipmi"=>"IPMI");
514   }
515   $selectors=array();
516   foreach ($builtin_methods as $value=>$display) {
517     $selector=array('display'=>$display, 'value'=>$value);
518     if ($value == $method) $selector['selected']=true;
519     $selectors []= $selector;
520   }
521   return $selectors;
522 }
523
524 // displays bandwidth with kbps Mbps Gbps as needed
525 function pretty_bandwidth ($bw) {
526   if ($bw < 1000)               return $bw;
527   if ($bw < 1000000)            return strval($bw/1000) . " kbps";
528   if ($bw < 1000000000)         return strval($bw/1000000) . " Mbps";
529   else                          return strval($bw/1000000000) . " Gbps";
530 }
531
532 //////////////////// 
533 function instantiation_label ($slice) {
534   $instantiation_labels = array ('not-instantiated'=>'NOT',
535                                  'plc-instantiated'=>'PLC',
536                                  'delegated' => 'DEL',
537                                  'nm-controller' => 'NM');
538   $result=$instantiation_labels[$slice['instantiation']];
539   if (!$result) $result = $slice['instantiation'];
540   if (!$result) $result = '??';
541   return $result;
542 }
543   
544 //////////////////// toggle areas
545 // get_arg ('show_persons',false) returns $_GET['show_persons'] if set and false otherwise
546 function get_arg ($name,$default,$method='get') {
547   if ($method == 'get') $var=$_GET; else $var=$_POST;
548   if (isset ($var[$name])) return $var[$name];
549   else return $default;
550 }
551
552 //////////////////// number of ...
553 function count_english ($objs,$name) {
554   $count=count($objs);
555   if ($count == 0) return 'No ' . $name;
556   else if ($count == 1) return 'One ' . $name;
557   else return $count . ' ' . $name . 's';
558 }
559 function count_english_warning ($objs, $name) {
560   $x=count_english ($objs,$name);
561   if (count ($objs) == 0) $x=plc_warning_html($x . ' !!');
562   return $x;
563 }
564
565 //////////////////// outlining reservable nodes
566 function reservable_mark () { return "-R-";}
567 function reservable_legend () { return "reservable nodes are marked with " . reservable_mark (); }
568
569
570 ?>