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