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