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