81931dfd82caf8ea58f7ddcdfee797a2574ba422
[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 function l_nodes () { return "/db/nodes/index.php"; }
40 function l_node_u ($node_id) { return "/db/nodes/node.php?id=" . $node_id; }
41 function l_node ($node_id) { return href (l_node_u($node_id),$node_id); }
42 function l_node2 ($node_id,$text) { return href (l_node_u($node_id),$text); }
43 function l_nodes_site ($site_id) { return "/db/nodes/index.php?site_id=" . $site_id; }
44 function l_node_add () { return "/db/nodes/add_node.php"; }
45
46 function l_interface_u ($interface_id) { return "/db/nodes/interfaces.php?id=" . $interface_id; }
47 function l_interface_add_u($node_id) { return "/db/nodes/interfaces.php?node_id=" . $node_id; }
48 function l_interface ($interface_id) { return href (l_interface_u($interface_id),$interface_id); }
49 function l_interface2 ($interface_id,$text) { return href (l_interface_u($interface_id),$text); }
50
51 function l_sites () { return "/db/sites/index.php"; }
52 function l_site_u ($site_id) { return "/db/persons/index.php?id=" . $site_id; }
53 function l_site ($site_id) { return href (l_site_u($site_id),$site_id); }
54 function l_site2 ($site_id,$text) { return href (l_site_u($site_id),$text); }
55
56 function l_slices () { return "/db/slices/index.php"; }
57 function l_slice_u ($slice_id) { return "/db/slices/index.php?id=" . $slice_id; }
58 function l_slice ($slice_id) { return href (l_slice_u($slice_id),$slice_id); }
59 function l_slice2 ($slice_id,$text) { return href (l_slice_u($slice_id),$text); }
60 function l_slice_add () { return "/db/slices/add_slice.php"; }
61
62 function l_sliver_u ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=" . $node_id. "&slice_id=" . $slice_id; }
63 function l_sliver3 ($node_id,$slice_id,$text) { return href (l_sliver_u($node_id,$slice_id),$text) ; }
64
65 function l_persons () { return "/db/persons/index.php"; }
66 function l_persons_site ($site_id) { return "/db/persons/index.php?site_id=" . $site_id; }
67 function l_person_u ($person_id) { return "/db/persons/index.php?id=" . $person_id; }
68 function l_person ($person_id) { return href (l_person_u($person_id),$person_id); }
69 function l_person2 ($person_id,$text) { return href (l_person_u($person_id),$text); }
70
71 function l_tags () { return "/db/tags/index.php"; }
72 function l_tags_node () { return "/db/tags/index.php?type=node"; }
73 # xxx cleanup duplicate pages
74 #function l_tags_interface () { return "/db/tags/index.php?type=interface"; }
75 function l_tags_interface () { return "/db/nodes/settings.php"; }
76 function l_tags_slice () { return "/db/tags/index.php?type=slice"; }
77
78 function l_nodegroups () { return "/db/tags/node_groups.php"; }
79 function l_nodegroup_u ($nodegroup_id) { return "/db/tags/node_groups.php?id=" . $nodegroup_id; }
80 function l_nodegroup2 ($nodegroup_id,$text) { return href(l_nodegroup_u($nodegroup_id),$text); }
81
82 function l_events () { return '/db/events/index.php'; }
83 function l_event ($type,$param,$id) { return '/db/events/index.php?type=' . $type . '&' . $param . '=' . $id; }
84
85 function l_peers() { return "/db/peers/index.php"; }
86 function l_peer_u($peer_id) { return "/db/peers/index.php?id=" . $peer_id; }
87 function l_comon($id_name,$id_value) { return '/db/nodes/comon.php?' . $id_name . "=" . $id_value; }
88 function l_sirius() { return "/db/sirius/index.php"; }
89 function l_about() { return "/db/about.php"; }
90 function l_doc_plcapi() { return "/db/doc/PLCAPI.php"; }
91 function l_doc_nmapi() { return "/db/doc/NMAPI.php"; }
92
93 function l_logout() { return "/planetlab/logout.php"; }
94 function l_sulogout() { return "/planetlab/sulogout.php"; }
95 function l_reset_password() { return "/db/persons/reset_password.php"; }
96 function l_person_register() { return "/db/persons/register.php"; }
97 function l_site_register() { return "/db/sites/register.php"; }
98 function l_site_pending() { return "/db/sites/join_request.php"; }
99 function l_site_status() { return "/db/sites/peers.php"; }
100
101 //////////////////////////////////////////////////////////// validation functions
102 function topdomain ($hostname) {
103   $exploded=array_reverse(explode(".",$hostname));
104   return $exploded[0];
105 }
106
107 function is_valid_email_addr ($email) {
108   if (ereg("^.+@.+\\..+$", $email) ) {
109     return true;
110   } else {
111     return false;
112   }
113 }
114
115 function is_valid_url ($url) {
116   if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
117     return true;
118   } else {
119     return false;
120   }
121 }
122
123 function is_valid_ip ($ip) {
124   if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) {
125       // it's at least in the right format, now check to see if
126       // each part is equal to less than 255
127       $parts= explode( '.', $ip );
128       $count= count($parts);
129
130       for( $i= 0; $i < $count; $i++ ) {
131         if( intval($parts[$i]) > 255 )
132           return false;
133       }
134
135       return true;
136   } else {
137     return false;
138   }
139 }
140
141 function is_valid_network_addr($network_addr,$mask) {
142   $lNetwork= ip2long($network_addr);
143   $lMask= ip2long($mask);
144
145   // are they the correct format?
146   if( $lNetwork == -1 || $lMask == -1 )
147     return false;
148
149   // is network address valid for the mask?
150   if( ($lNetwork & $lMask) != $lNetwork )
151     return false;
152
153   return true;
154 }
155
156
157 // returns whether or not a network address is in the reserved space
158 // in the case of a invalid network address, false will be returned.
159 function is_reserved_network_addr($network_addr) {
160   $lNetwork= ip2long($network_addr);
161
162   if( $lNetwork == -1 )
163     return false;
164
165   // does the network address fall in a reserved block?
166   $reserved_ips = array (
167                          array('10.0.0.0','10.255.255.255'),
168                          array('172.16.0.0','172.31.0.0'),
169                          array('192.168.0.0','192.168.255.0')
170                          );
171   foreach ($reserved_ips as $r) {
172     $min = ip2long($r[0]);
173     $max = ip2long($r[1]);
174       
175     if (($lNetwork >= $min) && ($lNetwork <= $max))
176       return true;
177   }
178
179   return false;
180 }
181
182 ////////////////////////////////////////////////////////////  peer & peerscopes
183 // when shortnames are needed on peers
184 function plc_peer_get_hash ($api) {
185   $peer_columns=array('peer_id','shortname');
186   $peer_filter=array();
187   $peers = $api->GetPeers($peer_filter,$peer_columns);
188   
189   $peer_hash=array();
190   foreach ($peers as $peer) {
191     $peer_hash[$peer['peer_id']]=$peer;
192   }
193 }
194
195 function plc_peer_shortname ($peer_hash,$peer_id) {
196   if ( ! $peer_id ) {
197     return PLC_SHORTNAME;
198   } else {
199      return $peer_hash[$node['peer_id']]['shortname'];
200   }
201 }
202
203 // to set the background to grey on foreign objects
204 function plc_peer_block_start ($peer_hash,$peer_id) {
205   if ( ! $peer_id ) {
206     print "<div>";
207   } else {
208     // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
209     printf ('<div class="plc-foreign plc-%s>"',strtolower(plc_peer_shortname($peer_hash,$peer_id)));
210   }
211 }
212
213 function plc_peer_block_end () {
214   print "</div>\n";
215 }
216
217 // interpret standard syntax for peerscope
218 function plc_peer_info ($api,$peerscope) {
219   switch ($peerscope) {
220   case '':
221     $peer_filter=array();
222     $peer_label="all peers";
223     break;
224   case 'local':
225     $peer_filter=array("peer_id"=>NULL);
226     $peer_label=PLC_SHORTNAME;
227     break;
228   case 'foreign':
229     $peer_filter=array("~peer_id"=>NULL);
230     $peer_label="foreign peers";
231     break;
232   default:
233     if (is_int ($peerscope)) {
234       $peer_id=intval($peerscope);
235       $peers=$api->GetPeers(array("peer_id"=>$peer_id));
236     } else {
237       $peers=$api->GetPeers(array("shortname"=>$peerscope));
238     }
239     if ($peers) {
240       $peer=$peers[0];
241       $peer_id=$peer['peer_id'];
242       $peer_filter=array("peer_id"=>$peer_id);
243       $peer_label='peer "' . $peer['shortname'] . '"';
244     } else {
245       $peer_filter=array();
246       $peer_label="[no such peer " . $peerscope . "]";
247     }
248     break;
249   }
250   return array ($peer_filter,$peer_label);
251 }
252
253 //////////////////////////////////////////////////////////// titles
254 function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
255 function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
256
257 //////////////////////////////////////////////////////////// nav tabs
258 function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node_u($node_id)); }
259 function tabs_site($site) { return array('Site ' . $site['name']=>l_site_u($site_id)); }
260 function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice_u($slice_id)); }
261
262 //////////////////////////////////////////////////////////// presentation
263 // builds a table from an array of strings, with the given class
264 // attempt to normalize the delete buttons and confirmations
265 function plc_delete_button($width=15) {
266   return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/icons/delete.png"></span>';
267 }
268
269 function plc_js_confirm($message) {
270   return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
271 }
272
273 function plc_delete_link($url,$delete_message,$visible) {
274   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
275 }
276
277 function plc_delete_link_button($url,$delete_message,$width=15) {
278   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
279 }
280
281 function plc_event_button($type,$param,$id) {
282   return '<a href="' . l_event($type,$param,$id) . '"> <span title="Related events"> <img src="/planetlab/icons/event.png" width=18></span></a>';
283 }
284
285 function plc_comon_button ($id_name, $id_value,$target="") {
286   $result='<a ';
287   if (!empty($target)) {
288     $result.='target="' . $target . '" ';
289   }
290   $result.='href="' . l_comon($id_name,$id_value) . '">';
291   $result.='<span title="Link to Comon"> <img src="/planetlab/icons/comon.png" width="18"></span></a>';
292   return $result;
293 }
294
295 function plc_vertical_table ($messages, $class="") {
296   // pretty print the cell
297   if ( empty( $messages) ) return "";
298   $formatted = "";
299   $formatted .= "<table ";
300   if ($class) $formatted .= "class='" . $class . "'";
301   $formatted .= ">";
302   foreach ($messages as $message) {
303     $formatted .= "<tr><td>" . $message . "</td></tr>";
304   }
305   $formatted .= "</table>";
306   return $formatted;
307 }
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 ($list) {
316   print( "<div class='plc-error'>" );
317   print( "<p style='font-weight:bold'>The following errors occured:</p>" );
318   print("<ul>");
319   foreach( $errors as $err ) {
320     print( "<li>$err</li>\n" );
321   }
322   print( "</ul></div>\n" );
323 }
324
325 function plc_warning ($text) {
326   print "<div class='plc-warning'> Warning " . $text . "</div>";
327 }
328
329 // shows a php variable verbatim with a heading message
330 function plc_debug ($message,$object) {
331   print "<br>" . $message . "<pre>";
332   print_r ($object);
333   print "</pre>";
334 }
335
336 ?>