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