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