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