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 //////////////////////////////////////////////////////////// roles & other checks on global $plc
9 function plc_is_admin () {
10   global $plc;
11   return in_array( 10, $plc->person['role_ids']);
12 }
13 function plc_is_pi () {
14   global $plc;
15   return in_array( 20, $plc->person['role_ids']);
16 }
17 function plc_is_tech () {
18   global $plc;
19   return in_array( 40, $plc->person['role_ids']);
20 }
21 function plc_in_site ($site_id) {
22   global $plc;
23   return in_array( $site_id, $plc->person['site_ids']);
24 }
25
26 function plc_my_site_id () {
27   global $plc;
28   return $plc->person['site_ids'][0];
29 }
30
31 function plc_my_person_id () {
32   global $plc;
33   return $plc->person['person_id'];
34 }
35
36 //////////////////////////////////////////////////////////// links    
37 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
38
39 // naming scheme is
40 // l_objects()                  -> the url to the page that list objects
41 // l_object($object_id)         -> the url to hte page thas details object with given id
42 // l_object_t($object_id,text)  -> an <a> tag that shows text and links to the above
43 // l_object_add ()              -> the url to that object-afding page
44
45 function l_actions ()                   { return "/db/actions.php"; }
46
47 function l_nodes ()                     { return "/db/nodes/index.php"; }
48 function l_nodes_local ()               { return "/db/nodes/index.php?peerscope=local"; }
49 function l_node ($node_id)              { return "/db/nodes/node.php?id=$node_id"; }
50 function l_node_t ($node_id,$text)      { return href (l_node($node_id),$text); }
51 function l_node_add ()                  { return "/db/nodes/node_add.php"; }
52 function l_nodes_site ($site_id)        { return "/db/nodes/index.php?site_id=$site_id"; }
53
54 function l_interface ($interface_id)    { return "/db/nodes/interfaces.php?id=$interface_id"; }
55 function l_interface_t ($interface_id,$text) { 
56                                           return href (l_interface($interface_id),$text); }
57 function l_interface_add($node_id)      { return "/db/nodes/interfaces.php?node_id=$node_id"; }
58
59 function l_sites ()                     { return "/db/sites/index.php"; }
60 function l_sites_local ()               { return "/db/sites/index.php?peerscope=local"; }
61 function l_site ($site_id)              { return "/db/sites/index.php?id=$site_id"; }
62 function l_site_t ($site_id,$text)      { return href (l_site($site_id),$text); }
63 function l_site_update($site_id)        { return "/xxx-undefined"; }
64
65 function l_slices ()                    { return "/db/slices/index.php"; }
66 function l_slice ($slice_id)            { return "/db/slices/index.php?id=$slice_id"; }
67 function l_slice_t ($slice_id,$text)    { return href (l_slice($slice_id),$text); }
68 function l_slice_add ()                 { return "/db/slices/add_slice.php"; }
69 function l_slices_site($site_id)        { return "/db/slices/index.php?site_id=$site_id"; }
70 // from an object
71 function l_slice_text ($slice)          { return l_slice_t ($slice['slice_id'],$slice['name']); }
72
73 function l_sliver ($node_id,$slice_id)  { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; }
74 function l_sliver_t ($node_id,$slice_id,$text) { 
75                                           return href (l_sliver($node_id,$slice_id),$text) ; }
76
77 function l_persons ()                   { return "/db/persons/index.php"; }
78 function l_person ($person_id)          { return "/db/persons/index.php?id=$person_id"; }
79 function l_person_t ($person_id,$text)  { return href (l_person($person_id),$text); }
80 function l_persons_site ($site_id)      { return "/db/persons/index.php?site_id=$site_id"; }
81
82 function l_tags ()                      { return "/db/tags/index.php"; }
83 function l_tag ($tag_type_id)           { return "/db/tags/index.php"; }
84 function l_tag_add()                    { return "/db/tags/tag_form.php"; }
85 function l_tag_update($id)              { return "/db/tags/tag_form.php&action=update-tag-type&id=$id"; }
86
87 function l_nodegroups ()                { return "/db/tags/nodegroups.php"; }
88 function l_nodegroup ($nodegroup_id)    { return "/db/tags/nodegroups.php?id=$nodegroup_id"; }
89 function l_nodegroup_t ($nodegroup_id,$text) { 
90                                           return href(l_nodegroup($nodegroup_id),$text); }
91
92 function l_events ()                    { return "/db/events/index.php"; }
93 function l_event ($type,$param,$id)     { return "/db/events/index.php?type=$type&$param=$id"; }
94
95 function l_peers()                      { return "/db/peers/index.php"; }
96 function l_peer($peer_id)               { return "/db/peers/index.php?id=$peer_id"; }
97
98 function l_comon($id_name,$id_value)    { return "/db/nodes/comon.php?$id_name=$id_value"; }
99 function l_sirius()                     { return "/db/sirius/index.php"; }
100 function l_about()                      { return "/db/about.php"; }
101 function l_doc_plcapi()                 { return "/db/doc/PLCAPI.php"; }
102 function l_doc_nmapi()                  { return "/db/doc/NMAPI.php"; }
103 function l_admin()                      { return "/db/adminsearch.php"; }
104
105 function l_logout()                     { return "/planetlab/logout.php"; }
106 function l_sulogout()                   { return "/planetlab/sulogout.php"; }
107 function l_reset_password()             { return "/db/persons/reset_password.php"; }
108 function l_person_register()            { return "/db/persons/register.php"; }
109 function l_site_register()              { return "/db/sites/register.php"; }
110 function l_sites_pending()              { return "/db/sites/join_request.php"; }
111
112 function tabs_events()                  { return array('image'=>'/planetlab/icons/event.png','height'=>18);}
113 function tabs_comon()                   { return array('image'=>'/planetlab/icons/comon.png','height'=>18);}
114
115 // returns array ['url' => path, 'values' => hash (key=>value)* ]
116 function split_url ($full_url) {
117   list($url,$args) = explode("?",$full_url);
118   $values=array();
119   if ($args) {
120     $pairs=explode("&",$args);
121     foreach ($pairs as $pair) {
122       list ($name,$value) = explode("=",$pair);
123       $values[$name]=$value;
124     }
125   }
126   return array("url"=>$url,"values"=>$values);
127 }
128
129 //////////////////////////////////////////////////////////// validation functions
130 function topdomain ($hostname) {
131   $exploded=array_reverse(explode(".",$hostname));
132   return $exploded[0];
133 }
134
135 function is_valid_email_addr ($email) {
136   if (ereg("^.+@.+\\..+$", $email) ) {
137     return true;
138   } else {
139     return false;
140   }
141 }
142
143 function is_valid_url ($url) {
144   if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
145     return true;
146   } else {
147     return false;
148   }
149 }
150
151 function is_valid_ip ($ip) {
152   if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
153       // it's at least in the right format, now check to see if
154       // each part is equal to less than 255
155       $parts= explode( '.', $ip );
156       $count= count($parts);
157
158       for( $i= 0; $i < $count; $i++ ) {
159         if( intval($parts[$i]) > 255 )
160           return false;
161       }
162
163       return true;
164   } else {
165     return false;
166   }
167 }
168
169 function is_valid_network_addr($network_addr,$mask) {
170   $lNetwork= ip2long($network_addr);
171   $lMask= ip2long($mask);
172
173   // are they the correct format?
174   if( $lNetwork == -1 || $lMask == -1 )
175     return false;
176
177   // is network address valid for the mask?
178   if( ($lNetwork & $lMask) != $lNetwork )
179     return false;
180
181   return true;
182 }
183
184
185 // returns whether or not a network address is in the reserved space
186 // in the case of a invalid network address, false will be returned.
187 function is_reserved_network_addr($network_addr) {
188   $lNetwork= ip2long($network_addr);
189
190   if( $lNetwork == -1 )
191     return false;
192
193   // does the network address fall in a reserved block?
194   $reserved_ips = array (
195                          array('10.0.0.0','10.255.255.255'),
196                          array('172.16.0.0','172.31.0.0'),
197                          array('192.168.0.0','192.168.255.0')
198                          );
199   foreach ($reserved_ips as $r) {
200     $min = ip2long($r[0]);
201     $max = ip2long($r[1]);
202       
203     if (($lNetwork >= $min) && ($lNetwork <= $max))
204       return true;
205   }
206
207   return false;
208 }
209
210 ////////////////////////////////////////////////////////////  peer & peerscopes
211 function plc_role_global_hash ($api) {
212   $hash=array();
213   $roles=$api->GetRoles();
214   foreach ($roles as $role) {
215     $hash[$role['role_id']]=$role['name'];
216   }
217   return $hash;
218 }
219   
220 ////////////////////////////////////////////////////////////  peer & peerscopes
221 // when shortnames are needed on peers
222 function plc_peer_global_hash ($api) {
223   $peer_columns=array('peer_id','shortname');
224   $peer_filter=array();
225   $peers = $api->GetPeers($peer_filter,$peer_columns);
226   
227   $peer_hash=array();
228   if ($peers) foreach ($peers as $peer) {
229     $peer_hash[$peer['peer_id']]=$peer;
230   }
231 }
232
233 function plc_peer_shortname ($peer_hash,$peer_id) {
234   if ( ! $peer_id ) {
235     return PLC_SHORTNAME;
236   } else {
237      return $peer_hash[$node['peer_id']]['shortname'];
238   }
239 }
240
241 function plc_peer_label ($peer) { 
242   if (! $peer) {
243     return "Local object on " . PLC_NAME . " (" . PLC_SHORTNAME . ")";
244   }
245   $result= $peer['peername'] . " (" . $peer['shortname'] . ")";
246   $result= href(l_peer($peer['peer_id']),$result);
247   return plc_foreign_text($result);
248 }
249
250 // to set the background to grey on foreign objects
251 // return true if the peer is local 
252 function plc_peer_block_start ($peer_hash,$peer_id) {
253   if ( ! $peer_id ) {
254     print "<div>";
255     return true;
256   } else {
257     // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
258     printf ('<div class="plc-foreign plc-%s>"',strtolower(plc_peer_shortname($peer_hash,$peer_id)));
259     return false;
260   }
261 }
262
263 function plc_peer_block_end () {
264   print "</div>\n";
265 }
266
267 // interpret standard syntax for peerscope
268 function plc_peer_info ($api,$peerscope) {
269   switch ($peerscope) {
270   case '':
271     $peer_filter=array();
272     $peer_label="all peers";
273     break;
274   case 'local':
275     $peer_filter=array("peer_id"=>NULL);
276     $peer_label=PLC_SHORTNAME;
277     break;
278   case 'foreign':
279     $peer_filter=array("~peer_id"=>NULL);
280     $peer_label="foreign peers";
281     break;
282   default:
283     if (is_int ($peerscope)) {
284       $peer_id=intval($peerscope);
285       $peers=$api->GetPeers(array("peer_id"=>$peer_id));
286     } else {
287       $peers=$api->GetPeers(array("shortname"=>$peerscope));
288     }
289     if ($peers) {
290       $peer=$peers[0];
291       $peer_id=$peer['peer_id'];
292       $peer_filter=array("peer_id"=>$peer_id);
293       $peer_label='peer "' . $peer['shortname'] . '"';
294     } else {
295       $peer_filter=array();
296       $peer_label="[no such peer " . $peerscope . "]";
297     }
298     break;
299   }
300   return array ($peer_filter,$peer_label);
301 }
302
303 //////////////////////////////////////////////////////////// nodegroups
304 // hash by 'tagname=value'
305 function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
306   $filter=NULL;
307   // xxx somehow this does not work; I've checked that the feature is working from plcsh
308   // but I suspect the php marshalling or something; no time to fix, get all nodegroups for now
309   // if ($tagnames) $filter=array("tagname"=>$tagnames);
310   $nodegroups=$api->GetNodeGroups($filter);
311   $hash=array();
312   if ($nodegroups) foreach ($nodegroups as $nodegroup) {
313       $key=$nodegroup['tagname']."=".$nodegroup['value'];
314       $hash[$key]=$nodegroup;
315     }
316   return $hash;
317 }
318   
319 //////////////////////////////////////////////////////////// titles
320 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
321 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
322
323 //////////////////////////////////////////////////////////// nav tabs
324 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node($node_id)); }
325 function tabs_site($site) { return array('Site ' . $site['name']=>l_site($site_id)); }
326 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice($slice_id)); }
327
328 //////////////////////////////////////////////////////////// presentation
329 // builds a table from an array of strings, with the given class
330 // attempt to normalize the delete buttons and confirmations
331 function plc_delete_button($width=15) {
332   return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
333 }
334
335 function plc_js_confirm($message) {
336   return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
337 }
338
339 function plc_delete_link($url,$delete_message,$visible) {
340   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
341 }
342
343 function plc_delete_link_button($url,$delete_message,$width=15) {
344   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
345 }
346
347 function plc_event_button($type,$param,$id) {
348   return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
349 }
350
351 function plc_comon_button ($id_name, $id_value,$target="") {
352   $result='<a ';
353   if (!empty($target)) {
354     $result.='target="' . $target . '" ';
355   }
356   $result.='href="' . l_comon($id_name,$id_value) . '">';
357   $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
358   return $result;
359 }
360
361 function plc_vertical_table ($messages, $class="") {
362   // pretty print the cell
363   if ( empty( $messages) ) return "";
364   $formatted = "";
365   $formatted .= "<table";
366   if ($class) $formatted .= " class='" . $class . "'";
367   $formatted .= ">";
368   foreach ($messages as $message) {
369     $formatted .= "<tr><td>" . $message . "</td></tr>";
370   }
371   $formatted .= "</table>";
372   return $formatted;
373 }
374
375 //////////////////////////////////////////////////////////// various mappers
376 // could not figure how to use anonymous lambdas..
377 function get_site_id ($site) { return $site['site_id'];}
378 function get_tagname ($tag) { return $tag['tagname'];}
379
380 ////////////////////////////////////////////////////////////
381 function plc_section ($text,$line=true) {
382   if ($line) { print "<hr/>";}
383   print "<h2 class=plc> $text </h2>\n";
384 }
385
386 function plc_error ($text) {
387   // should use the same channel as the php errors..
388   print "<div class='plc-error'> Error " . $text . "</div>";
389 }
390
391 function plc_errors ($list) {
392   print( "<div class='plc-error'>" );
393   print( "<p style='font-weight:bold'>The following errors occured:</p>" );
394   print("<ul>");
395   foreach( $errors as $err ) {
396     print( "<li>$err</li>\n" );
397   }
398   print( "</ul></div>\n" );
399 }
400
401 function plc_warning_text ($text)       { return "<span class='plc-warning'>" . $text . "</span>";}
402 function plc_warning ($text)            { print plc_warning_text("Warning " . $text); }
403 function plc_foreign_text($text)        { return "<span class=plc-foreign>$text</span>"; }
404
405 // shows a php variable verbatim with a heading message
406 function plc_debug ($message,$object) {
407   print "<br>" . $message . "<pre>";
408   print_r ($object);
409   print "</pre>";
410 }
411
412
413 ?>