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