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