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